Class: Rattler::Runtime::Parser
- Inherits:
-
Util::Node
- Object
- Util::Node
- Rattler::Runtime::Parser
- Defined in:
- lib/rattler/runtime/parser.rb
Overview
Parser
is the base class for all parsers in the Rattler framework.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#source ⇒ String
readonly
The source that this parser parses.
Class Method Summary collapse
-
.parse!(source, options = {}) ⇒ Object
Parse
source
and raise a SyntaxError if the parse fails. -
.parse_fully!(source, options = {}) ⇒ Object
Parse the entirety of
source
and raise a SyntaxError if the parse fails.
Instance Method Summary collapse
-
#fail ⇒ false
Fail and register a parse failure, unless a failure has already occurred at the same or later position in the source.
-
#fail! ⇒ false
Fail and register a parse failure, unless a failure has already occurred at a later position in the source.
-
#fail_parse ⇒ false
Fail the same as
#fail
but cause the entire parse to fail immediately. -
#failure ⇒ ParseFailure
Return the last parse failure.
-
#failure? ⇒ Boolean
Return true if there is a parse failure.
-
#initialize(source, options = {}) ⇒ Parser
constructor
Create a new parser to parse
source
. -
#parse ⇒ Object
Parse or register a parse failure.
-
#parse! ⇒ Object
Parse or raise a SyntaxError.
-
#parse_fully ⇒ Object
Parse the entire source or register a parse failure.
-
#parse_fully! ⇒ Object
Parse the entire source or raise a SyntaxError.
-
#pos ⇒ Fixnum
The current parse position.
-
#pos=(n) ⇒ Fixnum
Set the current parse position.
Methods inherited from Util::Node
#==, #[], [], #attrs, #can_equal?, #child, #children, #each, #empty?, #eql?, #inspect, #method_missing, #name, #pretty_print, #pretty_print_cycle, #respond_to?, #same_contents?, #to_graphviz, #with_attrs, #with_attrs!, #with_children
Constructor Details
#initialize(source, options = {}) ⇒ Parser
Create a new parser to parse source
.
33 34 35 36 37 |
# File 'lib/rattler/runtime/parser.rb', line 33 def initialize(source, ={}) @source = source @scanner = StringScanner.new(source) @tab_size = [:tab_size] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Rattler::Util::Node
Instance Attribute Details
#source ⇒ String (readonly)
Returns the source that this parser parses.
40 41 42 |
# File 'lib/rattler/runtime/parser.rb', line 40 def source @source end |
Class Method Details
.parse!(source, options = {}) ⇒ Object
Parse source
and raise a SyntaxError if the parse fails.
14 15 16 |
# File 'lib/rattler/runtime/parser.rb', line 14 def self.parse!(source, ={}) self.new(source, ).parse! end |
.parse_fully!(source, options = {}) ⇒ Object
Parse the entirety of source
and raise a SyntaxError if the parse fails.
24 25 26 |
# File 'lib/rattler/runtime/parser.rb', line 24 def self.parse_fully!(source, ={}) self.new(source, ).parse_fully! end |
Instance Method Details
#fail ⇒ false
Fail and register a parse failure, unless a failure has already occurred at the same or later position in the source.
96 97 98 99 100 101 |
# 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! ⇒ false
Fail and register a parse failure, unless a failure has already occurred at a later position in the source.
109 110 111 112 113 114 |
# 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 ⇒ false
Fail the same as #fail
but cause the entire parse to fail immediately.
122 123 124 125 126 127 128 129 |
# File 'lib/rattler/runtime/parser.rb', line 122 def fail_parse if block_given? fail! { yield } else fail! end throw :parse_failed end |
#failure ⇒ ParseFailure
Return the last parse failure
139 140 141 142 143 |
# File 'lib/rattler/runtime/parser.rb', line 139 def failure if failure? @__failure__ ||= ParseFailure.new(source, @failure_pos, @failure_msg) end end |
#failure? ⇒ Boolean
Return true if there is a parse failure
133 134 135 |
# File 'lib/rattler/runtime/parser.rb', line 133 def failure? !@failure_pos.nil? end |
#parse ⇒ Object
Parse or register a parse failure
45 46 47 48 |
# File 'lib/rattler/runtime/parser.rb', line 45 def parse catch(:parse_failed) { return finish __parse__ } false end |
#parse! ⇒ Object
Parse or raise a SyntaxError
55 56 57 |
# File 'lib/rattler/runtime/parser.rb', line 55 def parse! parse or raise_error end |
#parse_fully ⇒ Object
Parse the entire source or register a parse failure
62 63 64 |
# File 'lib/rattler/runtime/parser.rb', line 62 def parse_fully (result = parse) && (@scanner.eos? || fail { :EOF }) && result end |
#parse_fully! ⇒ Object
Parse the entire source or raise a SyntaxError
72 73 74 |
# File 'lib/rattler/runtime/parser.rb', line 72 def parse_fully! parse_fully or raise_error end |
#pos ⇒ Fixnum
Returns the current parse position.
77 78 79 |
# File 'lib/rattler/runtime/parser.rb', line 77 def pos @scanner.pos end |
#pos=(n) ⇒ Fixnum
Set the current parse position
84 85 86 |
# File 'lib/rattler/runtime/parser.rb', line 84 def pos=(n) @scanner.pos = n end |