Module: Antelope::Ace::Grammar::Precedences
- Included in:
- Antelope::Ace::Grammar
- Defined in:
- lib/antelope/ace/grammar/precedences.rb
Overview
Manages precedence for tokens.
Instance Method Summary collapse
-
#generate_precedence ⇒ Array<Ace::Precedence>
private
Generates the precedence rules.
-
#precedence ⇒ Array<Ace::Precedence>
Accesses the generated precedence list.
-
#precedence_for(token) ⇒ Ace::Precedence
Finds a precedence rule for a given token.
Instance Method Details
#generate_precedence ⇒ Array<Ace::Precedence> (private)
Generates the precedence rules. Loops through the compiler
given precedence settings, and then adds two default
precedence rules; one for :$ (level 0, nonassoc), and one
for :_ (level 1, nonassoc).
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/antelope/ace/grammar/precedences.rb', line 43 def generate_precedence size = @compiler.[:prec].size + 1 precedence = @compiler.[:prec]. each_with_index.map do |prec, i| Ace::Precedence.new(prec[0], prec[1..-1].to_set, size - i) end precedence << Ace::Precedence.new(:nonassoc, [:"$"].to_set, 0) << Ace::Precedence.new(:nonassoc, [:_].to_set, 1) precedence.sort_by { |_| _.level }.reverse end |
#precedence ⇒ Array<Ace::Precedence>
Accesses the generated precedence list. Lazily generates the precedence rules on the go, and then caches it.
14 15 16 |
# File 'lib/antelope/ace/grammar/precedences.rb', line 14 def precedence @_precedence ||= generate_precedence end |
#precedence_for(token) ⇒ Ace::Precedence
Finds a precedence rule for a given token. If no direct
rule is defined for that token, it will check for a rule
defined for the special symbol, :_. By default, there
is always a rule defined for :_.
25 26 27 28 29 30 31 32 33 |
# File 'lib/antelope/ace/grammar/precedences.rb', line 25 def precedence_for(token) token = token.name if token.is_a?(Token) set = Set.new([token, :_]) precedence. select { |pr| set.intersect?(pr.tokens) }. first end |