Class: Rattler::Runtime::ParseFailure

Inherits:
Object
  • Object
show all
Defined in:
lib/rattler/runtime/parse_failure.rb

Overview

A ParseFailure represents a position and explanation of a failed parse.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, pos) ⇒ ParseFailure #initialize(source, pos, message) ⇒ ParseFailure #initialize(source, pos, rule_name) ⇒ ParseFailure

Create a new parse error object.

Overloads:

  • #initialize(source, pos) ⇒ ParseFailure

    Create a new parse error object with no message.

    Parameters:

    • source (String)

      the source code being parsed

    • pos (Integer)

      the position of the error in the source code

  • #initialize(source, pos, message) ⇒ ParseFailure

    Create a new parse error object using message as the message.

    Parameters:

    • source (String)

      the source code being parsed

    • pos (Integer)

      the position of the error in the source code

    • message (String)

      a message explaining the error

  • #initialize(source, pos, rule_name) ⇒ ParseFailure

    Create a new parse error object using "#{rule_name} expected" as the message.

    Parameters:

    • source (String)

      the source code being parsed

    • pos (Integer)

      the position of the error in the source code

    • rule_name (Symbol)

      the name of the rule that failed



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rattler/runtime/parse_failure.rb', line 28

def initialize(source, pos, message_or_rule_name=nil)
  @source = source
  @pos = pos
  if message_or_rule_name
    @message ||= case message_or_rule_name
    when Symbol then "#{message_or_rule_name} expected"
    else message_or_rule_name
    end
  end
  @lc = Rattler::Util::LineCounter.new(source)
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



40
41
42
# File 'lib/rattler/runtime/parse_failure.rb', line 40

def message
  @message
end

#posObject (readonly)

Returns the value of attribute pos.



40
41
42
# File 'lib/rattler/runtime/parse_failure.rb', line 40

def pos
  @pos
end

Instance Method Details

#columnInteger

Returns the (1-based) column number where the parse error occurred.

Returns:

  • (Integer)

    the (1-based) column number where the parse error occurred



44
45
46
# File 'lib/rattler/runtime/parse_failure.rb', line 44

def column
  @lc.column(pos)
end

#lineInteger

Returns the (1-based) line number where the parse error occurred.

Returns:

  • (Integer)

    the (1-based) line number where the parse error occurred



50
51
52
# File 'lib/rattler/runtime/parse_failure.rb', line 50

def line
  @lc.line(pos)
end

#to_sString

Return a string representation of the parse error suitable for showing to the user, e.g. “parse error at line 17, column 42: expr expected”

Returns:

  • (String)

    a string representation of the parse error suitable for showing to the user



59
60
61
# File 'lib/rattler/runtime/parse_failure.rb', line 59

def to_s
  message ? "#{intro_str}:\n #{message}" : intro_str
end