Class: Hermeneutics::Addr::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/hermeneutics/addrs.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sym, data = nil, quot = nil) ⇒ Token

Returns a new instance of Token.



170
171
172
# File 'lib/hermeneutics/addrs.rb', line 170

def initialize sym, data = nil, quot = nil
  @sym, @data, @quot = sym, data, quot
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



164
165
166
# File 'lib/hermeneutics/addrs.rb', line 164

def data
  @data
end

#quotObject

Returns the value of attribute quot.



164
165
166
# File 'lib/hermeneutics/addrs.rb', line 164

def quot
  @quot
end

#symObject

Returns the value of attribute sym.



164
165
166
# File 'lib/hermeneutics/addrs.rb', line 164

def sym
  @sym
end

Class Method Details

.lexer(str) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/hermeneutics/addrs.rb', line 291

def lexer str
  if block_given? then
    while str =~ /./m do
      h, str = $&, $'
      t = SPECIAL[ h]
      if respond_to? t, true then
        t = send t, h, str
      end
      unless Token === t then
        t = Token[ *t]
      end
      yield t
    end
  else
    r = []
    lexer str do |t| r.push t end
    r
  end
end

.lexer_decode(str, &block) ⇒ Object



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/hermeneutics/addrs.rb', line 311

def lexer_decode str, &block
  if block_given? then
    HeaderExt.lexer str do |k,s|
      case k
        when :decoded then yield Token[ :char, s, true]
        when :plain   then lexer s, &block
        when :space   then yield Token[ :space]
      end
    end
  else
    r = []
    lexer_decode str do |t| r.push t end
    r
  end
end

Instance Method Details

#===(oth) ⇒ Object



180
181
182
183
184
185
# File 'lib/hermeneutics/addrs.rb', line 180

def === oth
  case oth
    when Symbol then @sym == oth
    when Token  then self == oth
  end
end

#compact!Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/hermeneutics/addrs.rb', line 234

def compact!
  case @sym
    when :text then
      return if @data.length <= 1
      @data = [ Token[ :char, text, needs_quote?]]
    when :addr then
      d = []
      while @data.any? do
        x, y = d.last, @data.shift
        if y === :char and x === :char then
          x.data << y.data
          x.quot ||= y.quot
        else
          y.compact!
          d.push y
        end
      end
      @data = d
  end
end

#encodeObject



224
225
226
227
228
229
230
231
232
# File 'lib/hermeneutics/addrs.rb', line 224

def encode
  case @sym
    when :addr  then data_map_join { |x| x.quote }
    when :text  then data_map_join { |x| x.encode }
    when :char  then encoded
    when :space then " "
    else             SPECIAL_CHARS[ @sym]||""
  end
end

#force_encoding(enc) ⇒ Object



187
188
189
190
191
192
# File 'lib/hermeneutics/addrs.rb', line 187

def force_encoding enc
  case @sym
    when :text  then @data.each { |x| x.force_encoding enc }
    when :char  then @data.force_encoding enc
  end
end

#inspectObject



174
175
176
177
178
# File 'lib/hermeneutics/addrs.rb', line 174

def inspect
  d = ": #{@data.inspect}" if @data
  d << " Q" if @quot
  "<##@sym#{d}>"
end

#needs_quote?Boolean

Returns:

  • (Boolean)


255
256
257
258
259
260
261
262
263
# File 'lib/hermeneutics/addrs.rb', line 255

def needs_quote?
  case @sym
    when :text  then @data.find { |x| x.needs_quote? }
    when :char  then @quot
    when :space then false
    when :addr  then false
    else             true
  end
end

#quoteObject



211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/hermeneutics/addrs.rb', line 211

def quote
  case @sym
    when :text,
         :addr  then data_map_join { |x| x.quote }
    when :char  then quoted
    when :space then " "
    else             SPECIAL_CHARS[ @sym]||""
  end
rescue Encoding::CompatibilityError
  force_encoding Encoding::ASCII_8BIT
  retry
end

#textObject



198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/hermeneutics/addrs.rb', line 198

def text
  case @sym
    when :addr  then data_map_join { |x| x.quote }
    when :text  then data_map_join { |x| x.text }
    when :char  then @data
    when :space then " "
    else             SPECIAL_CHARS[ @sym]||""
  end
rescue Encoding::CompatibilityError
  force_encoding Encoding::ASCII_8BIT
  retry
end

#to_sObject



194
195
196
# File 'lib/hermeneutics/addrs.rb', line 194

def to_s
  text
end