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.
- #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 = , source, pos, children 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.
69 70 71 72 73 |
# File 'lib/parslet/cause.rb', line 69 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.
61 62 63 64 |
# File 'lib/parslet/cause.rb', line 61 def raise(exception_klass=Parslet::ParseFailed) exception = exception_klass.new(self.to_s, self) Kernel.raise exception end |
#to_s ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/parslet/cause.rb', line 48 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}." end |