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.

Examples:

Produce a parse forest

Engine.new do |config|
  config.parse_repr = :parse_forest
end

Yields:



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

#configurationEngineConfig (readonly)

Returns the engine’s configuration.

Returns:



35
36
37
# File 'lib/rley/engine.rb', line 35

def configuration
  @configuration
end

#grammarRley::Syntax::Grammar (readonly)

Returns the grammar of the language to parse.

Returns:



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.

Examples:

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

Parameters:

  • aBlock (Proc, Lambda)

    Code block for creating the grammar.

Returns:



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.

Parameters:

  • aTokenizer (#each)

Returns:



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

Parameters:

Returns:



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

Parameters:

Returns:



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

Parameters:

Returns:



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

Parameters:

Returns:



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.

Parameters:

Returns:



72
73
74
# File 'lib/rley/engine.rb', line 72

def use_grammar(aGrammar)
  @grammar = aGrammar
end