Class: Parslet::Cause

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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(message, source, pos, children)
  @message, @source, @pos, @children = 
    message, source, pos, children
end

Instance Attribute Details

#messageString, Array (readonly)

Returns A string or an array of message pieces that provide failure information. Use #to_s to get a formatted string.

Returns:

  • (String, Array)

    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
  @message
end

#posFixnum (readonly)

Location of the error.

Returns:

  • (Fixnum)

    Position where the error happened. (character offset)



23
24
25
# File 'lib/parslet/cause.rb', line 23

def pos
  @pos
end

#sourceParslet::Source (readonly)

Returns Source that was parsed when this error happend. Mainly used for line number information.

Returns:

  • (Parslet::Source)

    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.

Parameters:

  • source (Parslet::Source)

    source that was parsed when this error happened

  • pos (Fixnum)

    position of error

  • str (String, Array<String>)

    message parts

  • children (Array<Parslet::Cause>) (defaults to: [])

    child nodes for this error tree

Returns:



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_treeObject

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

#childrenArray<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.

Returns:



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_sObject



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(message).map { |o| 
    o.respond_to?(:to_slice) ? 
      o.str.inspect : 
      o.to_s }.join + " at line #{line} char #{column}."
end