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.



168
169
170
# File 'lib/hermeneutics/addrs.rb', line 168

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

Instance Attribute Details

#dataObject

Returns the value of attribute data.



162
163
164
# File 'lib/hermeneutics/addrs.rb', line 162

def data
  @data
end

#quotObject

Returns the value of attribute quot.



162
163
164
# File 'lib/hermeneutics/addrs.rb', line 162

def quot
  @quot
end

#symObject

Returns the value of attribute sym.



162
163
164
# File 'lib/hermeneutics/addrs.rb', line 162

def sym
  @sym
end

Class Method Details

.lexer(str) ⇒ Object



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

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



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

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



178
179
180
181
182
183
# File 'lib/hermeneutics/addrs.rb', line 178

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

#compact!Object



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

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



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

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



185
186
187
188
189
190
# File 'lib/hermeneutics/addrs.rb', line 185

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



172
173
174
175
176
# File 'lib/hermeneutics/addrs.rb', line 172

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

#needs_quote?Boolean

Returns:

  • (Boolean)


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

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



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

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



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

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



192
193
194
# File 'lib/hermeneutics/addrs.rb', line 192

def to_s
  text
end