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)
34 35 36 |
# File 'lib/rley/engine.rb', line 34 def configuration @configuration end |
#grammar ⇒ Rley::Syntax::Grammar (readonly)
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::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.
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/rley/engine.rb', line 99 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.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# 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! return result end |
#pforest_visitor(aPForest) ⇒ ParseForestVisitor
Build a visitor for the given parse forest
148 149 150 |
# File 'lib/rley/engine.rb', line 148 def pforest_visitor(aPForest) return ParseForestVisitor.new(aPForest) end |
#ptree_visitor(aPTree) ⇒ ParseTreeVisitor
Build a visitor for the given parse tree
141 142 143 |
# File 'lib/rley/engine.rb', line 141 def ptree_visitor(aPTree) return ParseTreeVisitor.new(aPTree) end |
#to_pforest(aRawParse) ⇒ Rley::SPPF::ParseForest
Convert raw parse result into a parse forest representation
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/rley/engine.rb', line 127 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
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/rley/engine.rb', line 113 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.
71 72 73 |
# File 'lib/rley/engine.rb', line 71 def use_grammar(aGrammar) @grammar = aGrammar end |