class Rattler::Runtime::Parser

Parser is the base class for all parsers in the Rattler framework.

Attributes

source[R]

@return [String] the source that this parser parses

Public Class Methods

new(source, options={}) click to toggle source

Create a new parser to parse source.

@param [String] source the source to parse @option options [Integer] :tab_size (8) tab size to use to calculate

column numbers
# File lib/rattler/runtime/parser.rb, line 33
def initialize(source, options={})
  @source = source
  @scanner = StringScanner.new(source)
  @tab_size = options[:tab_size]
end
parse!(source, options={}) click to toggle source

Parse source and raise a {SyntaxError} if the parse fails.

@param (see initialize) @raise (see parse!) @return (see parse!)

# File lib/rattler/runtime/parser.rb, line 14
def self.parse!(source, options={})
  self.new(source, options).parse!
end
parse_fully!(source, options={}) click to toggle source

Parse the entirety of source and raise a {SyntaxError} if the parse fails.

@param (see initialize) @raise (see parse!) @return (see parse!)

# File lib/rattler/runtime/parser.rb, line 24
def self.parse_fully!(source, options={})
  self.new(source, options).parse_fully!
end

Public Instance Methods

fail() { || ... } click to toggle source

Fail and register a parse failure, unless a failure has already occurred at the same or later position in the source.

@yieldreturn [String, Symbol] a failure message or rule name

@see ParseFailure

@return [false]

# File lib/rattler/runtime/parser.rb, line 96
def fail # :yield:
  pos = @scanner.pos
  unless failure? and @failure_pos >= pos
    register_failure pos, (block_given? ? yield : nil)
  end
end
fail!() { || ... } click to toggle source

Fail and register a parse failure, unless a failure has already occurred at a later position in the source.

@yieldreturn (see fail)

@return (see fail)

# File lib/rattler/runtime/parser.rb, line 109
def fail! # :yield:
  pos = @scanner.pos
  unless failure? and @failure_pos > pos
    register_failure pos, (block_given? ? yield : nil)
  end
end
fail_parse() { || ... } click to toggle source

Fail the same as #fail but cause the entire parse to fail immediately.

@yieldreturn (see fail)

@return (see fail)

# File lib/rattler/runtime/parser.rb, line 122
def fail_parse
  if block_given?
    fail! { yield }
  else
    fail!
  end
  throw :parse_failed
end
failure() click to toggle source

Return the last parse failure @return [ParseFailure] the last parse failure

# File lib/rattler/runtime/parser.rb, line 139
def failure
  if failure?
    @__failure__ ||= ParseFailure.new(source, @failure_pos, @failure_msg)
  end
end
failure?() click to toggle source

Return true if there is a parse failure @return [Boolean] true if there is a parse failure

# File lib/rattler/runtime/parser.rb, line 133
def failure?
  !@failure_pos.nil?
end
parse() click to toggle source

Parse or register a parse failure

@return the parse result

# File lib/rattler/runtime/parser.rb, line 45
def parse
  catch(:parse_failed) { return finish __parse__ }
  false
end
parse!() click to toggle source

Parse or raise a {SyntaxError}

@raise [SyntaxError] a {SyntaxError} if the parse fails

@return (see parse)

# File lib/rattler/runtime/parser.rb, line 55
def parse!
  parse or raise_error
end
parse_fully() click to toggle source

Parse the entire source or register a parse failure

@return the parse result if the entire source was matched

# File lib/rattler/runtime/parser.rb, line 62
def parse_fully
  (result = parse) && (@scanner.eos? || fail { :EOF }) && result
end
parse_fully!() click to toggle source

Parse the entire source or raise a {SyntaxError}

@raise [SyntaxError] a {SyntaxError} if the parse fails or the entire

source is not matched

@return (see parse_fully)

# File lib/rattler/runtime/parser.rb, line 72
def parse_fully!
  parse_fully or raise_error
end
pos() click to toggle source

@return [Fixnum] the current parse position

# File lib/rattler/runtime/parser.rb, line 77
def pos
  @scanner.pos
end
pos=(n) click to toggle source

Set the current parse position @param [Integer] n the new parse position @return [Fixnum] the new parse position

# File lib/rattler/runtime/parser.rb, line 84
def pos=(n)
  @scanner.pos = n
end

Protected Instance Methods

clear_failure() click to toggle source

Clear the registered failure

# File lib/rattler/runtime/parser.rb, line 169
def clear_failure
  @failure_pos = nil
  @failure_msg = nil
  @__failure__ = nil
end
finish(final_result) click to toggle source

Finish any necessary clean-up based on the final parse result. @param final_result the final parse result @return final_result

# File lib/rattler/runtime/parser.rb, line 150
def finish(final_result)
  clear_failure if final_result
  final_result
end
raise_error() click to toggle source

Raise a {SyntaxError} for the last parse failure @raise [SyntaxError] a {SyntaxError} for the last parse failure @return [nothing]

# File lib/rattler/runtime/parser.rb, line 178
def raise_error
  raise SyntaxError, failure.to_s
end
register_failure(position, message) click to toggle source

Register a parse failure

@param [Integer] position the position of the failure @param [String, Symbol] message a failure message or rule name or nil

@return [false]

# File lib/rattler/runtime/parser.rb, line 161
def register_failure(position, message)
  @failure_pos = position
  @failure_msg = message
  @__failure__ = nil
  false
end