Class: Loxxy::FrontEnd::RawParser
- Inherits:
-
Object
- Object
- Loxxy::FrontEnd::RawParser
- Defined in:
- lib/loxxy/front_end/raw_parser.rb
Overview
A Lox parser that produce concrete parse trees. Concrete parse trees are the default kind of parse tree generated by the Rley library. They consist of two node types only:
- NonTerminalNode
- TerminalNode A NonTerminalNode has zero or more child nodes (called subnodes) A TerminalNode is leaf node, that is, it has no child node. While concrete parse tree nodes can be generated out of the box, they have the following drawbacks:
- Generic node classes that aren't always suited for the needs of the language being processing.
- Concrete parse tree tend to be deeply nested, which may complicate further processing.
Instance Attribute Summary collapse
-
#engine ⇒ Rley::Engine
readonly
A facade object for the Rley parsing library.
Instance Method Summary collapse
-
#initialize ⇒ RawParser
constructor
A new instance of RawParser.
-
#parse(source) ⇒ Rley::ParseTree
Parse the given Lox program into a parse tree.
Constructor Details
#initialize ⇒ RawParser
Returns a new instance of RawParser.
26 27 28 29 30 31 32 33 34 |
# File 'lib/loxxy/front_end/raw_parser.rb', line 26 def initialize # Create a Rley facade object @engine = Rley::Engine.new do |cfg| cfg.diagnose = true end # Step 1. Load Lox grammar @engine.use_grammar(Loxxy::FrontEnd::Grammar) end |
Instance Attribute Details
#engine ⇒ Rley::Engine (readonly)
Returns A facade object for the Rley parsing library.
24 25 26 |
# File 'lib/loxxy/front_end/raw_parser.rb', line 24 def engine @engine end |
Instance Method Details
#parse(source) ⇒ Rley::ParseTree
Parse the given Lox program into a parse tree.
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/loxxy/front_end/raw_parser.rb', line 39 def parse(source) lexer = Scanner.new(source) result = engine.parse(lexer.tokens) unless result.success? # Stop if the parse failed... line1 = "Parsing failed\n" line2 = "Reason: #{result.failure_reason.}" raise StandardError, line1 + line2 end return engine.convert(result) # engine.to_ptree(result) end |