Class: Collie::Analyzer::SymbolTable

Inherits:
Object
  • Object
show all
Defined in:
lib/collie/analyzer/symbol_table.rb

Overview

Symbol table for tracking declared tokens and nonterminals

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSymbolTable

Returns a new instance of SymbolTable.



11
12
13
14
15
# File 'lib/collie/analyzer/symbol_table.rb', line 11

def initialize
  @tokens = {} # name => {type_tag:, location:, usage_count:}
  @nonterminals = {} # name => {location:, usage_count:}
  @types = {} # type_tag => [names]
end

Instance Attribute Details

#nonterminalsObject (readonly)

Returns the value of attribute nonterminals.



9
10
11
# File 'lib/collie/analyzer/symbol_table.rb', line 9

def nonterminals
  @nonterminals
end

#tokensObject (readonly)

Returns the value of attribute tokens.



9
10
11
# File 'lib/collie/analyzer/symbol_table.rb', line 9

def tokens
  @tokens
end

#typesObject (readonly)

Returns the value of attribute types.



9
10
11
# File 'lib/collie/analyzer/symbol_table.rb', line 9

def types
  @types
end

Instance Method Details

#add_nonterminal(name, location: nil) ⇒ Object



24
25
26
27
28
# File 'lib/collie/analyzer/symbol_table.rb', line 24

def add_nonterminal(name, location: nil)
  return if @nonterminals.key?(name)

  @nonterminals[name] = { location: location, usage_count: 0 }
end

#add_token(name, type_tag: nil, location: nil) ⇒ Object

Raises:



17
18
19
20
21
22
# File 'lib/collie/analyzer/symbol_table.rb', line 17

def add_token(name, type_tag: nil, location: nil)
  raise Error, "Token '#{name}' already declared at #{@tokens[name][:location]}" if @tokens.key?(name)

  @tokens[name] = { type_tag: type_tag, location: location, usage_count: 0 }
  (@types[type_tag] ||= []) << name if type_tag
end

#declared?(name) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/collie/analyzer/symbol_table.rb', line 50

def declared?(name)
  token?(name) || nonterminal?(name)
end

#duplicate_symbolsObject



62
63
64
# File 'lib/collie/analyzer/symbol_table.rb', line 62

def duplicate_symbols
  @tokens.keys & @nonterminals.keys
end

#nonterminal?(name) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/collie/analyzer/symbol_table.rb', line 46

def nonterminal?(name)
  @nonterminals.key?(name)
end

#token?(name) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/collie/analyzer/symbol_table.rb', line 42

def token?(name)
  @tokens.key?(name)
end

#unused_nonterminalsObject



58
59
60
# File 'lib/collie/analyzer/symbol_table.rb', line 58

def unused_nonterminals
  @nonterminals.select { |_name, info| info[:usage_count].zero? }.keys
end

#unused_tokensObject



54
55
56
# File 'lib/collie/analyzer/symbol_table.rb', line 54

def unused_tokens
  @tokens.select { |_name, info| info[:usage_count].zero? }.keys
end

#use_nonterminal(name) ⇒ Object



36
37
38
39
40
# File 'lib/collie/analyzer/symbol_table.rb', line 36

def use_nonterminal(name)
  return unless @nonterminals.key?(name)

  @nonterminals[name][:usage_count] += 1
end

#use_token(name) ⇒ Object



30
31
32
33
34
# File 'lib/collie/analyzer/symbol_table.rb', line 30

def use_token(name)
  return unless @tokens.key?(name)

  @tokens[name][:usage_count] += 1
end