Class: Language::Parser
- Inherits:
-
Object
- Object
- Language::Parser
- Defined in:
- lib/language/parser.rb,
lib/language/parser/str.rb,
lib/language/parser/absent.rb,
lib/language/parser/interuption.rb,
lib/language/parser/end_of_input.rb,
lib/language/parser/str/not_found.rb,
lib/language/parser/absent/present.rb,
lib/language/parser/not_end_of_input.rb
Defined Under Namespace
Classes: Absent, EndOfInput, Interuption, NotEndOfInput, Str
Instance Attribute Summary collapse
-
#buffer ⇒ Object
Returns the value of attribute buffer.
-
#cursor ⇒ Object
Returns the value of attribute cursor.
-
#input ⇒ Object
Returns the value of attribute input.
-
#output ⇒ Object
Returns the value of attribute output.
-
#root ⇒ Object
Returns the value of attribute root.
Instance Method Summary collapse
- #aka(name) ⇒ Object
- #buffer? ⇒ Boolean
- #consume(n) ⇒ Object
-
#initialize(root:, input:, cursor: 0, buffer: "", output: Output.new) ⇒ Parser
constructor
A new instance of Parser.
- #next?(string) ⇒ Boolean
- #output? ⇒ Boolean
- #parse(check_end_of_input: true) ⇒ Object
Constructor Details
#initialize(root:, input:, cursor: 0, buffer: "", output: Output.new) ⇒ Parser
Returns a new instance of Parser.
7 8 9 10 11 12 13 |
# File 'lib/language/parser.rb', line 7 def initialize(root:, input:, cursor: 0, buffer: "", output: Output.new) @root = root @input = input @cursor = cursor @buffer = buffer @output = output end |
Instance Attribute Details
#buffer ⇒ Object
Returns the value of attribute buffer.
5 6 7 |
# File 'lib/language/parser.rb', line 5 def buffer @buffer end |
#cursor ⇒ Object
Returns the value of attribute cursor.
5 6 7 |
# File 'lib/language/parser.rb', line 5 def cursor @cursor end |
#input ⇒ Object
Returns the value of attribute input.
5 6 7 |
# File 'lib/language/parser.rb', line 5 def input @input end |
#output ⇒ Object
Returns the value of attribute output.
5 6 7 |
# File 'lib/language/parser.rb', line 5 def output @output end |
#root ⇒ Object
Returns the value of attribute root.
5 6 7 |
# File 'lib/language/parser.rb', line 5 def root @root end |
Instance Method Details
#aka(name) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/language/parser.rb', line 32 def aka(name) if @buffer.empty? @output = Output.new({ name => @output }) else @output[name] = Output.new(@buffer) @buffer = "" end end |
#buffer? ⇒ Boolean
45 46 47 |
# File 'lib/language/parser.rb', line 45 def buffer? @buffer != "" end |
#consume(n) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/language/parser.rb', line 25 def consume(n) raise EndOfInput, self unless @cursor + n <= @input.size @buffer += @input[@cursor, n] @cursor += n end |
#next?(string) ⇒ Boolean
41 42 43 |
# File 'lib/language/parser.rb', line 41 def next?(string) @input[@cursor...(@cursor + string.size)] == string end |
#output? ⇒ Boolean
49 50 51 |
# File 'lib/language/parser.rb', line 49 def output? @output.present? end |
#parse(check_end_of_input: true) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/language/parser.rb', line 15 def parse(check_end_of_input: true) @root.parse(self) unless @cursor == @input.size || !check_end_of_input raise NotEndOfInput, self end @output.present? ? @output : Output.new(@buffer.empty? ? nil : @buffer) end |