Class: Glaemscribe::API::Charset

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

Defined Under Namespace

Classes: Char, VirtualChar

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Charset

Returns a new instance of Charset.



126
127
128
129
130
131
# File 'lib/api/charset.rb', line 126

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



185
186
187
# File 'lib/api/charset.rb', line 185

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

#add_char(line, code, names) ⇒ Object

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



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/api/charset.rb', line 134

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



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/api/charset.rb', line 146

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



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/api/charset.rb', line 159

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