Class: Rley::Engine

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize {|configuration| ... } ⇒ Engine

Constructor.

Yields:



32
33
34
35
# File 'lib/rley/engine.rb', line 32

def initialize()
  @configuration = EngineConfig.new
  yield configuration if block_given?
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



25
26
27
# File 'lib/rley/engine.rb', line 25

def configuration
  @configuration
end

#grammarObject (readonly)

Returns the value of attribute grammar.



29
30
31
# File 'lib/rley/engine.rb', line 29

def grammar
  @grammar
end

Instance Method Details

#build_grammar(&aBlock) ⇒ Object

Factory method.



39
40
41
42
# File 'lib/rley/engine.rb', line 39

def build_grammar(&aBlock)
  builder = Rley::Syntax::GrammarBuilder.new(&aBlock)
  @grammar = builder.grammar
end

#convert(aRawParse) ⇒ Object

Convert raw parse result into a more convenient representation (parse tree or parse forest) as specified by the configuration.



73
74
75
76
77
78
79
80
81
82
# File 'lib/rley/engine.rb', line 73

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.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rley/engine.rb', line 53

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

#ptree_visitor(aPTree) ⇒ ParseTreeVisitor

Build a visitor for the given parse tree



113
114
115
# File 'lib/rley/engine.rb', line 113

def ptree_visitor(aPTree)
  return Rley::ParseTreeVisitor.new(aPTree)
end

#to_ptree(aRawParse) ⇒ Object

Convert raw parse result into a parse tree representation



86
87
88
89
90
91
92
93
94
95
# File 'lib/rley/engine.rb', line 86

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) ⇒ Object

Use the given grammar.



46
47
48
# File 'lib/rley/engine.rb', line 46

def use_grammar(aGrammar)
  @grammar = aGrammar
end