class Rattler::Runtime::ParseFailure

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

Attributes

message[R]
pos[R]

Public Class Methods

new(source, pos, message_or_rule_name=nil) click to toggle source

Create a new parse error object.

@overload initialize(source, pos)

Create a new parse error object with no message.
@param [String] source the source code being parsed
@param [Integer] pos the position of the error in the source code

@overload initialize(source, pos, message)

Create a new parse error object using +message+ as the message.
@param [String] source the source code being parsed
@param [Integer] pos the position of the error in the source code
@param [String] message a message explaining the error

@overload initialize(source, pos, rule_name)

Create a new parse error object using <tt>"#{rule_name} expected"</tt>
as the message.
@param [String] source the source code being parsed
@param [Integer] pos the position of the error in the source code
@param [Symbol] rule_name the name of the rule that failed
# 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

Public Instance Methods

column() click to toggle source

@return [Integer] the (1-based) column number where the parse error

occurred
# File lib/rattler/runtime/parse_failure.rb, line 44
def column
  @lc.column(pos)
end
line() click to toggle source

@return [Integer] the (1-based) line number where the parse error

occurred
# File lib/rattler/runtime/parse_failure.rb, line 50
def line
  @lc.line(pos)
end
to_s() click to toggle source

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”

@return [String] a string representation of the parse error suitable for

showing to the user
# File lib/rattler/runtime/parse_failure.rb, line 59
def to_s
  message ? "#{intro_str}:\n #{message}" : intro_str
end

Private Instance Methods

intro_str() click to toggle source
# File lib/rattler/runtime/parse_failure.rb, line 65
def intro_str
  "parse error #{pos_str}"
end
pos_str() click to toggle source
# File lib/rattler/runtime/parse_failure.rb, line 69
def pos_str
  "at line #{line}, column #{column}"
end