class Rley::Engine
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.
Attributes
@!attribute [r] configuration @return [EngineConfig] the engine’s configuration
@!attribute [r] grammar @return [Rley::Syntax::Grammar] the grammar of the language to parse
Public Class Methods
Source
# File lib/rley/engine.rb, line 46 def initialize @configuration = EngineConfig.new yield configuration if block_given? end
Constructor. @example Produce a parse forest
Engine.new do |config| config.parse_repr = :parse_forest end
Public Instance Methods
Source
# File lib/rley/engine.rb, line 64 def build_grammar(&aBlock) builder = Rley::RGN::GrammarBuilder.new(&aBlock) @grammar = builder.grammar end
Factory method. @param aBlock [Proc, Lambda] Code block for creating the grammar. @return [Rley::Syntax::Grammar] the grammar of the language to parse. @example Grammar for array of integers
instance = Engine.new instance.build_grammar do add_terminals('LBRACKET', 'RBRACKET', 'COMMA', 'INTEGER') add_production('start' => 'array') add_production('array' => 'LBRACKET elements RBRACKET') add_production('array' => 'LBRACKET RBRACKET') add_production('elements' => 'elements COMMA INTEGER') add_production('elements' => 'INTEGER') end
Source
# File lib/rley/engine.rb, line 103 def convert(aRawParse) case configuration.parse_repr when :parse_tree to_ptree(aRawParse) when :parse_forest to_pforest(aRawParse) end end
Convert raw parse result into a more convenient representation (parse tree or parse forest) as specified by the configuration. @param aRawParse [Parser::GFGParsing] @return [Rley::PTree::ParseTree, Rley::SPPF::ParseForest
]
Source
# File lib/rley/engine.rb, line 79 def parse(aTokenizer) tokens = [] aTokenizer.each do |a_token| next unless a_token if a_token.terminal.is_a?(String) term_name = a_token.terminal term_symb = grammar.name2symbol[term_name] a_token.instance_variable_set(:@terminal, term_symb) end tokens << a_token end parser = build_parser(grammar) parser.gf_graph.diagnose if configuration.diagnose result = parser.parse(tokens) result.tidy_up! result end
Parse the sequence of tokens produced by the given tokenizer object. @param aTokenizer [#each] @return [Parser::GFGParsing]
Source
# File lib/rley/engine.rb, line 151 def pforest_visitor(aPForest) ParseForestVisitor.new(aPForest) end
Build a visitor for the given parse forest @param aPForest [SPPF::ParseForest] @return [ParseForestVisitor]
Source
# File lib/rley/engine.rb, line 144 def ptree_visitor(aPTree) return ParseTreeVisitor.new(aPTree) end
Build a visitor for the given parse tree @param aPTree [PTree::ParseTree] @return [ParseTreeVisitor]
Source
# 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 # @type var result : Rley::SPPF::ParseForest result end
Convert raw parse result into a parse forest representation @param aRawParse [Parser::GFGParsing] @return [Rley::SPPF::ParseForest]
Source
# 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 result end
Convert raw parse result into a parse tree representation @param aRawParse [Parser::GFGParsing] @return [Rley::PTree::ParseTree]
Source
# File lib/rley/engine.rb, line 72 def use_grammar(aGrammar) @grammar = aGrammar end
Use the given grammar. @param aGrammar [Rley::Syntax::Grammar] @return [Rley::Syntax::Grammar] the grammar of the language to parse.
Protected Instance Methods
Source
# File lib/rley/engine.rb, line 157 def build_parser(aGrammar) Parser::GFGEarleyParser.new(aGrammar) end