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.

Author:

  • Jason Arhart

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



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rattler/runtime/parse_failure.rb', line 38

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.



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

def message
  @message
end

#posObject (readonly)

Returns the value of attribute pos.



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

def pos
  @pos
end

Instance Method Details

#columnInteger

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

Returns:

  • (Integer)

    the column number where the parse error occurred.



54
55
56
# File 'lib/rattler/runtime/parse_failure.rb', line 54

def column
  @lc.column(pos)
end

#lineInteger

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

Returns:

  • (Integer)

    the line number where the parse error occurred.



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

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



69
70
71
# File 'lib/rattler/runtime/parse_failure.rb', line 69

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