class Rattler::Runtime::Parser
Parser
is the base class for all parsers in the Rattler
framework.
Attributes
@return [String] the source that this parser parses
Public Class Methods
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
Public Instance Methods
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 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
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
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 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 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 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 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
@return [Fixnum] the current parse position
# File lib/rattler/runtime/parser.rb, line 77 def pos @scanner.pos end
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 the registered failure
# File lib/rattler/runtime/parser.rb, line 169 def clear_failure @failure_pos = nil @failure_msg = nil @__failure__ = nil end
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 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 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