Class: Calyx::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/calyx/rule.rb

Overview

Represents a named rule connected to a tree of productions that can be evaluated and a trace which represents where the rule was declared.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, productions, trace) ⇒ Rule

Returns a new instance of Rule.



28
29
30
31
32
# File 'lib/calyx/rule.rb', line 28

def initialize(name, productions, trace)
  @name = name.to_sym
  @tree = productions
  @trace = trace
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



26
27
28
# File 'lib/calyx/rule.rb', line 26

def name
  @name
end

#traceObject (readonly)

Returns the value of attribute trace.



26
27
28
# File 'lib/calyx/rule.rb', line 26

def trace
  @trace
end

#treeObject (readonly)

Returns the value of attribute tree.



26
27
28
# File 'lib/calyx/rule.rb', line 26

def tree
  @tree
end

Class Method Details

.build_ast(productions, registry) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/calyx/rule.rb', line 5

def self.build_ast(productions, registry)
  if productions.first.is_a?(Hash)
    # TODO: test that key is a string

    if productions.first.first.last.is_a?(String)
      # If value of the production is a strings then this is a
      # paired mapping production.
      Production::AffixTable.parse(productions.first, registry)
    else
      # Otherwise, we assume this is a weighted choice declaration and
      # convert the hash to an array
      Syntax::WeightedChoices.parse(productions.first.to_a, registry)
    end
  elsif productions.first.is_a?(Enumerable)
    # TODO: this needs to change to support attributed/tagged grammars
    Syntax::WeightedChoices.parse(productions, registry)
  else
    Syntax::Choices.parse(productions, registry)
  end
end

Instance Method Details

#evaluate(options) ⇒ Object



38
39
40
# File 'lib/calyx/rule.rb', line 38

def evaluate(options)
  tree.evaluate(options)
end

#sizeObject



34
35
36
# File 'lib/calyx/rule.rb', line 34

def size
  tree.size
end