123456789_123456789_123456789_123456789_123456789_

Class: Racc::SymbolTable

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, Enumerable
Inherits: Object
Defined in: lib/racc/grammar.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.newSymbolTable

[ GitHub ]

  
# File 'lib/racc/grammar.rb', line 950

def initialize
  @symbols = []   # :: [Racc::Sym]
  @cache   = {}   # :: {(String|Symbol) => Racc::Sym}
  @dummy  = intern(:$start, true)
  @anchor = intern(false, true)     # Symbol ID = 0
  @error  = intern(:error, false)   # Symbol ID = 1
end

Instance Attribute Details

#anchor (readonly)

[ GitHub ]

  
# File 'lib/racc/grammar.rb', line 959

attr_reader :anchor

#dummy (readonly)

[ GitHub ]

  
# File 'lib/racc/grammar.rb', line 958

attr_reader :dummy

#error (readonly)

[ GitHub ]

  
# File 'lib/racc/grammar.rb', line 960

attr_reader :error

#nt_base (readonly)

[ GitHub ]

  
# File 'lib/racc/grammar.rb', line 983

attr_reader :nt_base

#symbols (readonly) Also known as: #to_a

[ GitHub ]

  
# File 'lib/racc/grammar.rb', line 975

attr_reader :symbols

#to_a (readonly)

Alias for #symbols.

[ GitHub ]

  
# File 'lib/racc/grammar.rb', line 976

alias to_a symbols

Instance Method Details

#[](id)

[ GitHub ]

  
# File 'lib/racc/grammar.rb', line 962

def [](id)
  @symbols[id]
end

#check_terminals (private)

[ GitHub ]

  
# File 'lib/racc/grammar.rb', line 1027

def check_terminals
  return unless @symbols.any? {|s| s.should_terminal? }
  @anchor.should_terminal
  @error.should_terminal
  each_terminal do |t|
    t.should_terminal if t.string_symbol?
  end
  each do |s|
    s.should_terminal if s.assoc
  end
  terminals().reject {|t| t.should_terminal? }.each do |t|
    raise CompileError, "terminal #{t} not declared as terminal"
  end
  nonterminals().select {|n| n.should_terminal? }.each do |n|
    raise CompileError, "symbol #{n} declared as terminal but is not terminal"
  end
end

#delete(sym)

[ GitHub ]

  
# File 'lib/racc/grammar.rb', line 978

def delete(sym)
  @symbols.delete sym
  @cache.delete sym.value
end

#each(&block)

[ GitHub ]

  
# File 'lib/racc/grammar.rb', line 989

def each(&block)
  @symbols.each(&block)
end

#each_nonterminal(&block)

[ GitHub ]

  
# File 'lib/racc/grammar.rb', line 1005

def each_nonterminal(&block)
  @nterms.each(&block)
end

#each_terminal(&block)

[ GitHub ]

  
# File 'lib/racc/grammar.rb', line 997

def each_terminal(&block)
  @terms.each(&block)
end

#fix

[ GitHub ]

  
# File 'lib/racc/grammar.rb', line 1009

def fix
  terms, nterms = @symbols.partition {|s| s.terminal? }
  @symbols = terms + nterms
  @terms = terms
  @nterms = nterms
  @nt_base = terms.size
  fix_ident
  check_terminals
end

#fix_ident (private)

[ GitHub ]

  
# File 'lib/racc/grammar.rb', line 1021

def fix_ident
  @symbols.each_with_index do |t, i|
    t.ident = i
  end
end

#intern(val, dummy = false)

[ GitHub ]

  
# File 'lib/racc/grammar.rb', line 966

def intern(val, dummy = false)
  @cache[val] ||=
      begin
        sym = Sym.new(val, dummy)
        @symbols.push sym
        sym
      end
end

#nonterminals

[ GitHub ]

  
# File 'lib/racc/grammar.rb', line 1001

def nonterminals
  @symbols[@nt_base, @symbols.size - @nt_base]
end

#nt_max

[ GitHub ]

  
# File 'lib/racc/grammar.rb', line 985

def nt_max
  @symbols.size
end

#terminals(&block)

[ GitHub ]

  
# File 'lib/racc/grammar.rb', line 993

def terminals(&block)
  @symbols[0, @nt_base]
end