Class: AsciiMath::SymbolTableBuilder

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

Instance Method Summary collapse

Constructor Details

#initialize(allow_symbol_overwrites: true) ⇒ SymbolTableBuilder

Returns a new instance of SymbolTableBuilder.



3
4
5
6
# File 'lib/asciimath/symbol_table.rb', line 3

def initialize(allow_symbol_overwrites: true)
  @table = {}
  @allow_symbol_overwrites = allow_symbol_overwrites
end

Instance Method Details

#add(*args) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/asciimath/symbol_table.rb', line 8

def add(*args)
  raise 'Insufficient arguments' if args.length < 3

  entry = {}
  if args.last.is_a?(Hash)
    entry.merge!(args.pop)
  end
  entry[:type] = args.pop
  entry[:value] = args.pop

  entry.freeze
  args.map(&:freeze).each do |name|
    raise "Symbol overwrites are disallowed, but were attempted for #{name}" if !@allow_symbol_overwrites && !@table[name].nil?

    @table[name] = entry
  end
end

#buildObject



26
27
28
# File 'lib/asciimath/symbol_table.rb', line 26

def build
  @table.dup.freeze
end