Class: Parslet::Cause
- Inherits:
-
Object
- Object
- Parslet::Cause
- Defined in:
- lib/parslet/cause.rb
Overview
Represents a cause why a parse did fail. A lot of these objects are constructed - not all of the causes turn out to be failures for the whole parse.
Instance Attribute Summary collapse
-
#message ⇒ String, Array
readonly
A string or an array of message pieces that provide failure information.
-
#pos ⇒ Fixnum
readonly
Location of the error.
-
#source ⇒ Parslet::Source
readonly
Source that was parsed when this error happend.
Class Method Summary collapse
-
.format(source, pos, str, children = []) ⇒ Parslet::Cause
Appends ‘at line LINE char CHAR’ to the string given.
Instance Method Summary collapse
-
#ascii_tree ⇒ Object
Returns an ascii tree representation of the causes of this node and its children.
-
#children ⇒ Array<Parslet::Cause>
When this cause is part of a tree of error causes: child nodes for this node.
-
#initialize(message, source, pos, children) ⇒ Cause
constructor
A new instance of Cause.
-
#raise(exception_klass = Parslet::ParseFailed) ⇒ Object
Signals to the outside that the parse has failed.
-
#set_label(l) ⇒ Object
Update error message to include context provided by label Update all child causes too (the same context applies to all causes).
- #to_s ⇒ Object
Constructor Details
#initialize(message, source, pos, children) ⇒ Cause
Returns a new instance of Cause.
7 8 9 10 |
# File 'lib/parslet/cause.rb', line 7 def initialize(, source, pos, children) @message, @source, @pos, @children, @context = , source, pos, children, nil end |
Instance Attribute Details
#message ⇒ String, Array (readonly)
Returns A string or an array of message pieces that provide failure information. Use #to_s to get a formatted string.
14 15 16 |
# File 'lib/parslet/cause.rb', line 14 def @message end |
#pos ⇒ Fixnum (readonly)
Location of the error.
23 24 25 |
# File 'lib/parslet/cause.rb', line 23 def pos @pos end |
#source ⇒ Parslet::Source (readonly)
Returns Source that was parsed when this error happend. Mainly used for line number information.
18 19 20 |
# File 'lib/parslet/cause.rb', line 18 def source @source end |
Class Method Details
.format(source, pos, str, children = []) ⇒ Parslet::Cause
Appends ‘at line LINE char CHAR’ to the string given. Use pos
to override the position of the source
. This method returns an object that can be turned into a string using #to_s.
44 45 46 |
# File 'lib/parslet/cause.rb', line 44 def self.format(source, pos, str, children=[]) self.new(str, source, pos, children) end |
Instance Method Details
#ascii_tree ⇒ Object
Returns an ascii tree representation of the causes of this node and its children.
76 77 78 79 80 |
# File 'lib/parslet/cause.rb', line 76 def ascii_tree StringIO.new.tap { |io| recursive_ascii_tree(self, io, [true]) }. string end |
#children ⇒ Array<Parslet::Cause>
When this cause is part of a tree of error causes: child nodes for this node. Very often carries the reasons for this cause.
29 30 31 |
# File 'lib/parslet/cause.rb', line 29 def children @children ||= [] end |
#raise(exception_klass = Parslet::ParseFailed) ⇒ Object
Signals to the outside that the parse has failed. Use this in conjunction with .format for nice error messages.
68 69 70 71 |
# File 'lib/parslet/cause.rb', line 68 def raise(exception_klass=Parslet::ParseFailed) exception = exception_klass.new(self.to_s, self) Kernel.raise exception end |
#set_label(l) ⇒ Object
Update error message to include context provided by label Update all child causes too (the same context applies to all causes)
50 51 52 53 |
# File 'lib/parslet/cause.rb', line 50 def set_label(l) @context = " when parsing #{l}" children.each { |c| c.set_label(l) } end |
#to_s ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/parslet/cause.rb', line 55 def to_s line, column = source.line_and_column(pos) # Allow message to be a list of objects. Join them here, since we now # really need it. Array().map { |o| o.respond_to?(:to_slice) ? o.str.inspect : o.to_s }.join + " at line #{line} char #{column}#{@context}." end |