Class: Glaemscribe::API::Charset

Inherits:
Object
  • Object
show all
Defined in:
lib/api/charset.rb

Defined Under Namespace

Classes: Char, SequenceChar, Swap, VirtualChar

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Charset

Returns a new instance of Charset.



210
211
212
213
214
215
216
# File 'lib/api/charset.rb', line 210

def initialize(name)
  @name           = name
  @chars          = []
  @errors         = []
  @virtual_chars  = []
  @swaps          = []
end

Instance Attribute Details

#charsObject (readonly)

Returns the value of attribute chars.



29
30
31
# File 'lib/api/charset.rb', line 29

def chars
  @chars
end

#errorsObject

Returns the value of attribute errors.



28
29
30
# File 'lib/api/charset.rb', line 28

def errors
  @errors
end

#nameObject (readonly)

Returns the value of attribute name.



26
27
28
# File 'lib/api/charset.rb', line 26

def name
  @name
end

#swapsObject (readonly)

Returns the value of attribute swaps.



31
32
33
# File 'lib/api/charset.rb', line 31

def swaps
  @swaps
end

#virtual_charsObject (readonly)

Returns the value of attribute virtual_chars.



30
31
32
# File 'lib/api/charset.rb', line 30

def virtual_chars
  @virtual_chars
end

Instance Method Details

#[](symbol) ⇒ Object



304
305
306
# File 'lib/api/charset.rb', line 304

def [](symbol)
  @lookup_table[symbol]
end

#add_char(line, code, names) ⇒ Object

Pass integer (utf8 num) and array (of strings)



219
220
221
222
223
224
225
226
227
228
229
# File 'lib/api/charset.rb', line 219

def add_char(line, code, names)
  return if names.empty? || names.include?("?") # Ignore characters with '?'

  c         = Char.new
  c.line    = line
  c.code    = code
  c.names   = names
  c.str     = code.chr('UTF-8')
  c.charset = self
  @chars << c
end

#add_sequence_char(line, names, seq) ⇒ Object



244
245
246
247
248
249
250
251
252
253
# File 'lib/api/charset.rb', line 244

def add_sequence_char(line, names, seq)
  return if names.empty? || names.include?("?") # Ignore characters with '?'

  c             = SequenceChar.new
  c.line        = line
  c.names       = names
  c.sequence    = seq.split.reject{|token| token.empty? }
  c.charset     = self
  @chars << c
end

#add_swap(line, target, triggers) ⇒ Object



255
256
257
258
259
260
261
# File 'lib/api/charset.rb', line 255

def add_swap(line, target, triggers)
  return if target.empty? || triggers.empty?

  s             = Swap.new(target, triggers)
  s.line        = line
  @swaps << s
end

#add_virtual_char(line, classes, names, reversed = false, default = nil) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/api/charset.rb', line 231

def add_virtual_char(line, classes, names, reversed = false, default = nil)
  return if names.empty? || names.include?("?") # Ignore characters with '?'

  c           = VirtualChar.new
  c.line      = line
  c.names     = names
  c.classes   = classes # We'll check errors in finalize
  c.charset   = self
  c.reversed  = reversed
  c.default   = default
  @chars << c
end

#finalizeObject



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/api/charset.rb', line 263

def finalize
  @errors         = []
  @lookup_table   = {}
  @virtual_chars  = [] # A convenient filtered array
  @swap_lookup    = {}

  @chars.each { |c|
    c.names.each { |cname|
      found = @lookup_table[cname]
      if found
        @errors << Glaeml::Error.new(c.line, "Character #{cname} found twice.")
      else
        @lookup_table[cname] = c
      end
    }
  }

  @chars.each{ |c|
    if c.class == VirtualChar
      c.finalize
      @virtual_chars << c
    end
  }

  @chars.each{|c|
    if c.class == SequenceChar
      c.finalize
    end
  }

  @swaps.each{ |s|
    trig = s.finalize(self)
    if trig
      trig.names.each{ |n|
        @swap_lookup[n] = s
      }
    end
  }
  API::Debug::log("Finalized charset '#{@name}', #{@lookup_table.count} symbols loaded.")
end

#n2c(symbol) ⇒ Object



308
309
310
# File 'lib/api/charset.rb', line 308

def n2c(symbol)
  self[symbol]
end

#swap_for_trigger(trigger_name) ⇒ Object



312
313
314
# File 'lib/api/charset.rb', line 312

def swap_for_trigger(trigger_name)
  @swap_lookup[trigger_name]
end