Class: Aurum::Grammar::SyntaxRules
- Inherits:
-
Struct
- Object
- Struct
- Aurum::Grammar::SyntaxRules
- Defined in:
- lib/aurum/grammar/syntax_rules.rb
Defined Under Namespace
Classes: Precedence, Production, Symbol
Instance Attribute Summary collapse
-
#literals ⇒ Object
readonly
Returns the value of attribute literals.
-
#precedences ⇒ Object
Returns the value of attribute precedences.
-
#productions(nonterminal) ⇒ Object
Returns the value of attribute productions.
Instance Method Summary collapse
- #add_literal(literal) ⇒ Object
- #add_operator_precedence(operator, precedence) ⇒ Object
- #add_syntax_rule(production) ⇒ Object
- #assign_operator_precedence_to_symbols ⇒ Object
-
#initialize ⇒ SyntaxRules
constructor
A new instance of SyntaxRules.
- #operator_precedence(symbol_name) ⇒ Object
Constructor Details
#initialize ⇒ SyntaxRules
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
#literals ⇒ Object (readonly)
Returns the value of attribute literals.
4 5 6 |
# File 'lib/aurum/grammar/syntax_rules.rb', line 4 def literals @literals end |
#precedences ⇒ Object
Returns the value of attribute 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
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_symbols ⇒ Object
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 |