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.



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

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.



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

def anchor
  @anchor
end

#dummyObject (readonly)

Returns the value of attribute dummy.



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

def dummy
  @dummy
end

#errorObject (readonly)

Returns the value of attribute error.



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

def error
  @error
end

#nt_baseObject (readonly)

Returns the value of attribute nt_base.



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

def nt_base
  @nt_base
end

#symbolsObject (readonly) Also known as: to_a

Returns the value of attribute symbols.



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

def symbols
  @symbols
end

Instance Method Details

#[](id) ⇒ Object



882
883
884
# File 'lib/racc/grammar.rb', line 882

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

#delete(sym) ⇒ Object



898
899
900
901
# File 'lib/racc/grammar.rb', line 898

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

#each(&block) ⇒ Object



909
910
911
# File 'lib/racc/grammar.rb', line 909

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

#each_nonterminal(&block) ⇒ Object



925
926
927
# File 'lib/racc/grammar.rb', line 925

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

#each_terminal(&block) ⇒ Object



917
918
919
# File 'lib/racc/grammar.rb', line 917

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

#fixObject



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

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



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

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

#nonterminalsObject



921
922
923
# File 'lib/racc/grammar.rb', line 921

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

#nt_maxObject



905
906
907
# File 'lib/racc/grammar.rb', line 905

def nt_max
  @symbols.size
end

#terminals(&block) ⇒ Object



913
914
915
# File 'lib/racc/grammar.rb', line 913

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