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 ⇒ Integer
The current parse position.
-
#pos=(n) ⇒ Integer
Set the current parse position.
Methods inherited from Util::Node
#==, #[], [], #attrs, #can_equal?, #child, #children, #each, #empty?, #eql?, #inspect, #method_missing, #name, #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
.
43 44 45 46 47 |
# File 'lib/rattler/runtime/parser.rb', line 43 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)
The source that this parser parses
51 52 53 |
# File 'lib/rattler/runtime/parser.rb', line 51 def source @source end |
Class Method Details
.parse!(source, options = {}) ⇒ Object
Parse source
and raise a SyntaxError if the parse fails.
24 25 26 |
# File 'lib/rattler/runtime/parser.rb', line 24 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.
34 35 36 |
# File 'lib/rattler/runtime/parser.rb', line 34 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.
108 109 110 111 112 113 |
# File 'lib/rattler/runtime/parser.rb', line 108 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.
121 122 123 124 125 126 |
# File 'lib/rattler/runtime/parser.rb', line 121 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.
134 135 136 137 138 139 140 141 |
# File 'lib/rattler/runtime/parser.rb', line 134 def fail_parse if block_given? fail! { yield } else fail! end throw :parse_failed end |
#failure ⇒ ParseFailure
Return the last parse failure
151 152 153 154 155 |
# File 'lib/rattler/runtime/parser.rb', line 151 def failure if failure? @__failure__ ||= ParseFailure.new(source, @failure_pos, @failure_msg) end end |
#failure? ⇒ Boolean
Return true if there is a parse failure
145 146 147 |
# File 'lib/rattler/runtime/parser.rb', line 145 def failure? !@failure_pos.nil? end |
#parse ⇒ Object
Parse or register a parse failure
56 57 58 59 |
# File 'lib/rattler/runtime/parser.rb', line 56 def parse catch(:parse_failed) { return finish __parse__ } false end |
#parse! ⇒ Object
Parse or raise a SyntaxError
66 67 68 |
# File 'lib/rattler/runtime/parser.rb', line 66 def parse! parse or raise_error end |
#parse_fully ⇒ Object
Parse the entire source or register a parse failure
73 74 75 |
# File 'lib/rattler/runtime/parser.rb', line 73 def parse_fully (result = parse) && (@scanner.eos? || fail { :EOF }) && result end |
#parse_fully! ⇒ Object
Parse the entire source or raise a SyntaxError
83 84 85 |
# File 'lib/rattler/runtime/parser.rb', line 83 def parse_fully! parse_fully or raise_error end |
#pos ⇒ Integer
The current parse position
89 90 91 |
# File 'lib/rattler/runtime/parser.rb', line 89 def pos @scanner.pos end |
#pos=(n) ⇒ Integer
Set the current parse position
96 97 98 |
# File 'lib/rattler/runtime/parser.rb', line 96 def pos=(n) @scanner.pos = n end |