Class: Glaemscribe::API::Charset

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

Defined Under Namespace

Classes: Char, SequenceChar, VirtualChar

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Charset

Returns a new instance of Charset.



169
170
171
172
173
174
# File 'lib/api/charset.rb', line 169

def initialize(name)
  @name           = name
  @chars          = []
  @errors         = []
  @virtual_chars  = []
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

#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



245
246
247
# File 'lib/api/charset.rb', line 245

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

#add_char(line, code, names) ⇒ Object

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



177
178
179
180
181
182
183
184
185
186
187
# File 'lib/api/charset.rb', line 177

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



202
203
204
205
206
207
208
209
210
211
# File 'lib/api/charset.rb', line 202

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_virtual_char(line, classes, names, reversed = false, default = nil) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/api/charset.rb', line 189

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



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/api/charset.rb', line 213

def finalize
  @errors         = []
  @lookup_table   = {}
  @virtual_chars  = [] # A convenient filtered array
  
  @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
  }
  
  API::Debug::log("Finalized charset '#{@name}', #{@lookup_table.count} symbols loaded.")
end