Class: Aurum::Grammar::SyntaxRules

Inherits:
Struct
  • Object
show all
Defined in:
lib/aurum/grammar/syntax_rules.rb

Defined Under Namespace

Classes: Precedence, Production, Symbol

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSyntaxRules

Returns a new instance of SyntaxRules.



5
6
7
8
# File 'lib/aurum/grammar/syntax_rules.rb', line 5

def initialize
  @precedences, @productions, @terminals, @literals = {}, {}, {}, [].to_set
  @precedences_update = true
end

Instance Attribute Details

#literalsObject (readonly)

Returns the value of attribute literals.



4
5
6
# File 'lib/aurum/grammar/syntax_rules.rb', line 4

def literals
  @literals
end

#precedencesObject

Returns the value of attribute precedences

Returns:

  • (Object)

    the current value of precedences



3
4
5
# File 'lib/aurum/grammar/syntax_rules.rb', line 3

def precedences
  @precedences
end

#productions(nonterminal) ⇒ Object

Returns the value of attribute productions

Returns:

  • (Object)

    the current value of productions



3
4
5
# File 'lib/aurum/grammar/syntax_rules.rb', line 3

def productions
  @productions
end

Instance Method Details

#add_literal(literal) ⇒ Object



20
21
22
# File 'lib/aurum/grammar/syntax_rules.rb', line 20

def add_literal literal
  @literals << literal
end

#add_operator_precedence(operator, precedence) ⇒ Object



24
25
26
27
# File 'lib/aurum/grammar/syntax_rules.rb', line 24

def add_operator_precedence operator, precedence
  @precedences_update = true
  @precedences[operator] = precedence
end

#add_syntax_rule(production) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/aurum/grammar/syntax_rules.rb', line 10

def add_syntax_rule production
  @precedences_update = true
  nonterminal = production.nonterminal
  production.symbols.each {|s| @terminals[s.name] = s if s.is_terminal && !s.precedence}
  @productions[nonterminal] = [] unless @productions.has_key?(nonterminal)
  return false if @productions[nonterminal].include?(production)
  @productions[nonterminal] << production
  true
end

#assign_operator_precedence_to_symbolsObject



37
38
39
40
41
42
43
44
# File 'lib/aurum/grammar/syntax_rules.rb', line 37

def assign_operator_precedence_to_symbols
  return unless @precedences_update
  @precedences_update = false
  for name, precedence in @precedences
    symbol = @terminals[name]
    symbol.precedence = precedence if symbol
  end
end

#operator_precedence(symbol_name) ⇒ Object



33
34
35
# File 'lib/aurum/grammar/syntax_rules.rb', line 33

def operator_precedence symbol_name
  @precedences[symbol_name]
end