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.
46 47 48 49 |
# File 'lib/rley/engine.rb', line 46 def initialize() @configuration = EngineConfig.new yield configuration if block_given? end |
Instance Attribute Details
#configuration ⇒ EngineConfig (readonly)
Returns the engine’s configuration.
35 36 37 |
# File 'lib/rley/engine.rb', line 35 def configuration @configuration end |
#grammar ⇒ Rley::Syntax::Grammar (readonly)
Returns the grammar of the language to parse.
39 40 41 |
# File 'lib/rley/engine.rb', line 39 def grammar @grammar end |
Instance Method Details
#build_grammar(&aBlock) ⇒ Rley::Syntax::Grammar
Factory method.
64 65 66 67 |
# File 'lib/rley/engine.rb', line 64 def build_grammar(&aBlock) builder = Rley::Syntax::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.
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rley/engine.rb', line 101 def convert(aRawParse) result = case configuration.parse_repr when :parse_tree to_ptree(aRawParse) when :parse_forest to_pforest(aRawParse) end return result end |
#parse(aTokenizer) ⇒ Parser::GFGParsing
Parse the sequence of tokens produced by the given tokenizer object.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/rley/engine.rb', line 79 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! return result end |
#pforest_visitor(aPForest) ⇒ ParseForestVisitor
Build a visitor for the given parse forest
150 151 152 |
# File 'lib/rley/engine.rb', line 150 def pforest_visitor(aPForest) return ParseForestVisitor.new(aPForest) end |
#ptree_visitor(aPTree) ⇒ ParseTreeVisitor
Build a visitor for the given parse tree
143 144 145 |
# File 'lib/rley/engine.rb', line 143 def ptree_visitor(aPTree) return ParseTreeVisitor.new(aPTree) end |
#to_pforest(aRawParse) ⇒ Rley::SPPF::ParseForest
Convert raw parse result into a parse forest representation
129 130 131 132 133 134 135 136 137 138 |
# File 'lib/rley/engine.rb', line 129 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 return result end |
#to_ptree(aRawParse) ⇒ Rley::PTree::ParseTree
Convert raw parse result into a parse tree representation
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/rley/engine.rb', line 115 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 return result end |
#use_grammar(aGrammar) ⇒ Rley::Syntax::Grammar
Use the given grammar.
72 73 74 |
# File 'lib/rley/engine.rb', line 72 def use_grammar(aGrammar) @grammar = aGrammar end |