Class: Rley::Engine
- Inherits:
-
Object
- Object
- Rley::Engine
- Defined in:
- lib/rley/engine.rb
Overview
Implementation of the GoF Facade design pattern. An Engine object provides a higher-level interface that shields Rley client code from the lower-level classes.
Instance Attribute Summary collapse
-
#configuration ⇒ EngineConfig
readonly
The engine's configuration.
-
#grammar ⇒ Rley::Syntax::Grammar
readonly
The grammar of the language to parse.
Instance Method Summary collapse
-
#build_grammar(&aBlock) ⇒ Rley::Syntax::Grammar
Factory method.
-
#convert(aRawParse) ⇒ Rley::PTree::ParseTree, Rley::SPPF::ParseForest
Convert raw parse result into a more convenient representation (parse tree or parse forest) as specified by the configuration.
-
#initialize {|configuration| ... } ⇒ Engine
constructor
Constructor.
-
#parse(aTokenizer) ⇒ Parser::GFGParsing
Parse the sequence of tokens produced by the given tokenizer object.
-
#pforest_visitor(aPForest) ⇒ ParseForestVisitor
Build a visitor for the given parse forest.
-
#ptree_visitor(aPTree) ⇒ ParseTreeVisitor
Build a visitor for the given parse tree.
-
#to_pforest(aRawParse) ⇒ Rley::SPPF::ParseForest
Convert raw parse result into a parse forest representation.
-
#to_ptree(aRawParse) ⇒ Rley::PTree::ParseTree
Convert raw parse result into a parse tree representation.
-
#use_grammar(aGrammar) ⇒ Rley::Syntax::Grammar
Use the given grammar.
Constructor Details
#initialize {|configuration| ... } ⇒ Engine
Constructor.
45 46 47 48 |
# File 'lib/rley/engine.rb', line 45 def initialize @configuration = EngineConfig.new yield configuration if block_given? end |
Instance Attribute Details
#configuration ⇒ EngineConfig (readonly)
Returns the engine's configuration.
34 35 36 |
# File 'lib/rley/engine.rb', line 34 def configuration @configuration end |
#grammar ⇒ Rley::Syntax::Grammar (readonly)
Returns the grammar of the language to parse.
38 39 40 |
# File 'lib/rley/engine.rb', line 38 def grammar @grammar end |
Instance Method Details
#build_grammar(&aBlock) ⇒ Rley::Syntax::Grammar
Factory method.
63 64 65 66 |
# File 'lib/rley/engine.rb', line 63 def build_grammar(&aBlock) builder = Rley::Notation::GrammarBuilder.new(&aBlock) @grammar = builder.grammar end |
#convert(aRawParse) ⇒ Rley::PTree::ParseTree, Rley::SPPF::ParseForest
Convert raw parse result into a more convenient representation (parse tree or parse forest) as specified by the configuration.
100 101 102 103 104 105 106 107 |
# File 'lib/rley/engine.rb', line 100 def convert(aRawParse) case configuration.parse_repr when :parse_tree to_ptree(aRawParse) when :parse_forest to_pforest(aRawParse) end end |
#parse(aTokenizer) ⇒ Parser::GFGParsing
Parse the sequence of tokens produced by the given tokenizer object.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rley/engine.rb', line 78 def parse(aTokenizer) tokens = [] aTokenizer.each do |a_token| next unless a_token term_name = a_token.terminal term_symb = grammar.name2symbol[term_name] a_token.instance_variable_set(:@terminal, term_symb) tokens << a_token end parser = build_parser(grammar) parser.gf_graph.diagnose if configuration.diagnose result = parser.parse(tokens) result.tidy_up! result end |
#pforest_visitor(aPForest) ⇒ ParseForestVisitor
Build a visitor for the given parse forest
147 148 149 |
# File 'lib/rley/engine.rb', line 147 def pforest_visitor(aPForest) ParseForestVisitor.new(aPForest) end |
#ptree_visitor(aPTree) ⇒ ParseTreeVisitor
Build a visitor for the given parse tree
140 141 142 |
# File 'lib/rley/engine.rb', line 140 def ptree_visitor(aPTree) return ParseTreeVisitor.new(aPTree) end |
#to_pforest(aRawParse) ⇒ Rley::SPPF::ParseForest
Convert raw parse result into a parse forest representation
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/rley/engine.rb', line 126 def to_pforest(aRawParse) factory = ParseRep::ParseForestFactory.new(aRawParse) if configuration.repr_builder == :default result = factory.create(nil) else result = factory.create(configuration.repr_builder) end result end |
#to_ptree(aRawParse) ⇒ Rley::PTree::ParseTree
Convert raw parse result into a parse tree representation
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/rley/engine.rb', line 112 def to_ptree(aRawParse) factory = ParseRep::ParseTreeFactory.new(aRawParse) if configuration.repr_builder == :default result = factory.create(nil) else result = factory.create(configuration.repr_builder) end result end |
#use_grammar(aGrammar) ⇒ Rley::Syntax::Grammar
Use the given grammar.
71 72 73 |
# File 'lib/rley/engine.rb', line 71 def use_grammar(aGrammar) @grammar = aGrammar end |