Class: ORB::Parser
- Inherits:
-
Object
- Object
- ORB::Parser
- Defined in:
- lib/orb/parser.rb
Overview
The ‘Parser` is responsible for converting a list of tokens produced by the `Lexer` into an Abstract Syntax Tree (AST). Any errors encountered during parsing are stored in `@errors` and can be accessed after parsing.
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(tokens, options = {}) ⇒ Parser
constructor
Create a new parser instance, use ‘Parser.parse` instead.
-
#parse ⇒ Object
(also: #parse!)
Parse the tokens into a tree of nodes.
Constructor Details
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
10 11 12 |
# File 'lib/orb/parser.rb', line 10 def errors @errors end |
#tokens ⇒ Object (readonly)
Returns the value of attribute tokens.
10 11 12 |
# File 'lib/orb/parser.rb', line 10 def tokens @tokens end |
Class Method Details
.parse(tokens, options = {}) ⇒ Object
13 14 15 |
# File 'lib/orb/parser.rb', line 13 def parse(tokens, = {}) new(tokens, ).parse end |
Instance Method Details
#parse ⇒ Object Also known as: parse!
Parse the tokens into a tree of nodes. The ‘@current` index is used to keep track of the current token being parsed within the stream of tokens.
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/orb/parser.rb', line 30 def parse return @root if @tokens.empty? @current = 0 next_token while @current < @tokens.length # If there are any nodes left in the stack, they are unmatched tokens raise ORB::ParserError, "Unmatched #{@nodes.last.class}" if @nodes.length > 1 # Return the root node @root end |