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:



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

#configurationEngineConfig (readonly)

Returns the engine's configuration.

Returns:



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

def configuration
  @configuration
end

#grammarRley::Syntax::Grammar (readonly)

Returns the grammar of the language to parse.

Returns:



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.

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:



63
64
65
66
# File 'lib/rley/engine.rb', line 63

def build_grammar(&aBlock)
  builder = Rley::Notation::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.



100
101
102
103
104
105
106
107
# File 'lib/rley/engine.rb', line 100

def convert(aRawParse)
  case configuration.parse_repr
    when :parse_tree
      to_ptree(aRawParse)
    when :parse_forest
      to_pforest(aRawParse)
  end
end

#parse(aTokenizer) ⇒ Parser::GFGParsing

Parse the sequence of tokens produced by the given tokenizer object.

Parameters:

  • aTokenizer (#each)

Returns:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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!

  result
end

#pforest_visitor(aPForest) ⇒ ParseForestVisitor

Build a visitor for the given parse forest

Parameters:

Returns:



147
148
149
# File 'lib/rley/engine.rb', line 147

def pforest_visitor(aPForest)
  ParseForestVisitor.new(aPForest)
end

#ptree_visitor(aPTree) ⇒ ParseTreeVisitor

Build a visitor for the given parse tree

Parameters:

Returns:



140
141
142
# File 'lib/rley/engine.rb', line 140

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:



126
127
128
129
130
131
132
133
134
135
# File 'lib/rley/engine.rb', line 126

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

  result
end

#to_ptree(aRawParse) ⇒ Rley::PTree::ParseTree

Convert raw parse result into a parse tree representation

Parameters:

Returns:



112
113
114
115
116
117
118
119
120
121
# File 'lib/rley/engine.rb', line 112

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

#use_grammar(aGrammar) ⇒ Rley::Syntax::Grammar

Use the given grammar.

Parameters:

Returns:



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

def use_grammar(aGrammar)
  @grammar = aGrammar
end