Class: Cadenza::Parser
- Inherits:
-
RaccParser
- Object
- Racc::Parser
- RaccParser
- Cadenza::Parser
- Defined in:
- lib/cadenza/parser.rb
Overview
The Parser class takes all tokens retrieved from it’s lexer and forms them into an abstract syntax tree (AST) with a DocumentNode at it’s root.
Parser and RaccParser are tightly coupled to each other but were separated since Racc tends to generate undocumentable parser classes.
Constant Summary
Constants inherited from RaccParser
RaccParser::Racc_arg, RaccParser::Racc_debug_parser, RaccParser::Racc_token_to_s_table
Instance Attribute Summary collapse
-
#lexer ⇒ Lexer
readonly
The lexer object this parser is using to retrieve tokens from.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Parser
constructor
creates a new Parser with the given options.
-
#parse(source) ⇒ DocumentNode
takes the given source object and parses tokens from it, the tokens are then constructed into an abstract syntax tree (AST) with a DocumentNode at the root.
Methods inherited from RaccParser
Constructor Details
#initialize(options = {}) ⇒ Parser
creates a new Cadenza::Parser with the given options
20 21 22 23 24 25 26 |
# File 'lib/cadenza/parser.rb', line 20 def initialize(={}) @lexer = .fetch(:lexer, Cadenza::Lexer.new) raise "Lexers passed to the parser must define next_token" unless @lexer.respond_to?(:next_token) raise "Lexers passed to the parser must define source=" unless @lexer.respond_to?(:source=) end |
Instance Attribute Details
#lexer ⇒ Lexer (readonly)
Returns the lexer object this parser is using to retrieve tokens from.
13 14 15 |
# File 'lib/cadenza/parser.rb', line 13 def lexer @lexer end |
Instance Method Details
#parse(source) ⇒ DocumentNode
takes the given source object and parses tokens from it, the tokens are then constructed into an abstract syntax tree (AST) with a DocumentNode at the root. The root node is then returned.
35 36 37 38 39 40 41 42 43 |
# File 'lib/cadenza/parser.rb', line 35 def parse(source) @lexer.source = source @stack = [DocumentNode.new] do_parse @stack.first end |