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.



166
167
168
# File 'lib/hermeneutics/addrs.rb', line 166

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

Instance Attribute Details

#dataObject

Returns the value of attribute data.



160
161
162
# File 'lib/hermeneutics/addrs.rb', line 160

def data
  @data
end

#quotObject

Returns the value of attribute quot.



160
161
162
# File 'lib/hermeneutics/addrs.rb', line 160

def quot
  @quot
end

#symObject

Returns the value of attribute sym.



160
161
162
# File 'lib/hermeneutics/addrs.rb', line 160

def sym
  @sym
end

Class Method Details

.lexer(str) ⇒ Object



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

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



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

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



176
177
178
179
180
181
# File 'lib/hermeneutics/addrs.rb', line 176

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

#compact!Object



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

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



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

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



183
184
185
186
187
188
# File 'lib/hermeneutics/addrs.rb', line 183

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



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

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

#needs_quote?Boolean

Returns:

  • (Boolean)


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

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



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

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



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

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



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

def to_s
  text
end