class Hermes::Addr::Token

Attributes

data[RW]
quot[RW]
sym[RW]

Public Class Methods

new(sym, data = nil, quot = nil) click to toggle source
# File lib/hermes/addrs.rb, line 165
def initialize sym, data = nil, quot = nil
  @sym, @data, @quot = sym, data, quot
end

Public Instance Methods

===(oth) click to toggle source
# File lib/hermes/addrs.rb, line 175
def === oth
  case oth
    when Symbol then @sym == oth
    when Token  then self == oth
  end
end
compact!() click to toggle source
# File lib/hermes/addrs.rb, line 229
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
encode() click to toggle source
# File lib/hermes/addrs.rb, line 219
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) click to toggle source
# File lib/hermes/addrs.rb, line 182
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
inspect() click to toggle source
# File lib/hermes/addrs.rb, line 169
def inspect
  d = ": #{@data.inspect}" if @data
  d << " Q" if @quot
  "<##@sym#{d}>"
end
needs_quote?() click to toggle source
# File lib/hermes/addrs.rb, line 250
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
quote() click to toggle source
# File lib/hermes/addrs.rb, line 206
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
text() click to toggle source
# File lib/hermes/addrs.rb, line 193
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_s() click to toggle source
# File lib/hermes/addrs.rb, line 189
def to_s
  text
end

Private Instance Methods

data_map_join() { |x| ... } click to toggle source
# File lib/hermes/addrs.rb, line 262
def data_map_join
  @data.map { |x| yield x }.join
end
encoded() click to toggle source
# File lib/hermes/addrs.rb, line 275
def encoded
  if @quot or HeaderExt.needs? @data then
    c = HeaderExt.new Addr.encoding_parameters
    c.encode_whole @data
  else
    @data
  end
end
escaped(h, c) click to toggle source
# File lib/hermes/addrs.rb, line 324
def escaped h, c
  if h then
    [h].pack "H2"
  else
    case c
      when "n" then "\n"
      when "r" then "\r"
      when "t" then "\t"
      when "f" then "\f"
      when "v" then "\v"
      when "b" then "\b"
      when "a" then "\a"
      when "e" then "\e"
      when "0" then "\0"
      else          c
    end
  end
end
lex_bslash(h, str) click to toggle source
# File lib/hermes/addrs.rb, line 347
def lex_bslash h, str
  str.slice! /\A(?:x(..)|.)/
  y = escaped $1, $&
  Token[ :char, y, true]
end
lex_dquote(h, str) click to toggle source
# File lib/hermes/addrs.rb, line 359
def lex_dquote h, str
  str.slice! /\A((?:[^\"]|\.)*)"?/
  y = $1.gsub /\(x(..)|.)/ do |c,x|
    escaped x, c
  end
  Token[ :char, y, true]
end
lex_other(h, str) click to toggle source
# File lib/hermes/addrs.rb, line 366
def lex_other h, str
  until str.empty? or SPECIAL.has_key? str.head do
    h << (str.eat 1)
  end
  Token[ :char, h]
end
lex_space(h, str) click to toggle source
# File lib/hermes/addrs.rb, line 343
def lex_space h, str
  str.slice! /\A\s*/
  :space
end
lex_squote(h, str) click to toggle source
# File lib/hermes/addrs.rb, line 352
def lex_squote h, str
  str.slice! /\A((?:[^\']|\.)*)'?/
  y = $1.gsub /\(x(..)|.)/ do |c,x|
    escaped x, c
  end
  Token[ :char, y, true]
end
lexer(str) { |t| ... } click to toggle source
# File lib/hermes/addrs.rb, line 286
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) { |Token| ... } click to toggle source
# File lib/hermes/addrs.rb, line 306
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
quoted() click to toggle source
# File lib/hermes/addrs.rb, line 266
def quoted
  if @quot then
    q = @data.gsub "\"" do |c| "\\" + c end
    %Q"#{q}"%
  else
    @data
  end
end