Class: Racc::SymbolTable

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/racc/grammar.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSymbolTable

Returns a new instance of SymbolTable.



950
951
952
953
954
955
956
# 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

#anchorObject (readonly)

Returns the value of attribute anchor.



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

def anchor
  @anchor
end

#dummyObject (readonly)

Returns the value of attribute dummy.



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

def dummy
  @dummy
end

#errorObject (readonly)

Returns the value of attribute error.



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

def error
  @error
end

#nt_baseObject (readonly)

Returns the value of attribute nt_base.



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

def nt_base
  @nt_base
end

#symbolsObject (readonly) Also known as: to_a

Returns the value of attribute symbols.



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

def symbols
  @symbols
end

Instance Method Details

#[](id) ⇒ Object



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

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

#delete(sym) ⇒ Object



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

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

#each(&block) ⇒ Object



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

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

#each_nonterminal(&block) ⇒ Object



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

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

#each_terminal(&block) ⇒ Object



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

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

#fixObject



1009
1010
1011
1012
1013
1014
1015
1016
1017
# 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

#intern(val, dummy = false) ⇒ Object



966
967
968
969
970
971
972
973
# 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

#nonterminalsObject



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

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

#nt_maxObject



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

def nt_max
  @symbols.size
end

#terminals(&block) ⇒ Object



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

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