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.



868
869
870
871
872
873
874
# File 'lib/racc/grammar.rb', line 868

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.



877
878
879
# File 'lib/racc/grammar.rb', line 877

def anchor
  @anchor
end

#dummyObject (readonly)

Returns the value of attribute dummy.



876
877
878
# File 'lib/racc/grammar.rb', line 876

def dummy
  @dummy
end

#errorObject (readonly)

Returns the value of attribute error.



878
879
880
# File 'lib/racc/grammar.rb', line 878

def error
  @error
end

#nt_baseObject (readonly)

Returns the value of attribute nt_base.



901
902
903
# File 'lib/racc/grammar.rb', line 901

def nt_base
  @nt_base
end

#symbolsObject (readonly) Also known as: to_a

Returns the value of attribute symbols.



893
894
895
# File 'lib/racc/grammar.rb', line 893

def symbols
  @symbols
end

Instance Method Details

#[](id) ⇒ Object



880
881
882
# File 'lib/racc/grammar.rb', line 880

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

#delete(sym) ⇒ Object



896
897
898
899
# File 'lib/racc/grammar.rb', line 896

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

#each(&block) ⇒ Object



907
908
909
# File 'lib/racc/grammar.rb', line 907

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

#each_nonterminal(&block) ⇒ Object



923
924
925
# File 'lib/racc/grammar.rb', line 923

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

#each_terminal(&block) ⇒ Object



915
916
917
# File 'lib/racc/grammar.rb', line 915

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

#fixObject



927
928
929
930
931
932
933
934
935
# File 'lib/racc/grammar.rb', line 927

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



884
885
886
887
888
889
890
891
# File 'lib/racc/grammar.rb', line 884

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

#nonterminalsObject



919
920
921
# File 'lib/racc/grammar.rb', line 919

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

#nt_maxObject



903
904
905
# File 'lib/racc/grammar.rb', line 903

def nt_max
  @symbols.size
end

#terminals(&block) ⇒ Object



911
912
913
# File 'lib/racc/grammar.rb', line 911

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