Class: Hermeneutics::Cli::ImapTools::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/hermeneutics/cli/imap/parser.rb

Constant Summary collapse

RE =
%r/\A\s*(?:
  (\()|
  (\))|
  ("(?:[^\\"]|\\.)*")|
  (\{\d+\}\z)|
  ((?:\[[^\]]*\]|[^ \t)])+)|
)/x

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



93
94
95
# File 'lib/hermeneutics/cli/imap/parser.rb', line 93

def initialize
  @list = []
end

Class Method Details

.compile(input, compiler) ⇒ Object



85
86
87
88
89
# File 'lib/hermeneutics/cli/imap/parser.rb', line 85

def compile input, compiler
  p = run input
  p.walk compiler
  compiler.result
end

.run(input) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/hermeneutics/cli/imap/parser.rb', line 40

def run input
  p = new
  l = input.readline
  loop do
    if l.empty? then
      break if p.closed?
      l = input.readline
    end
    l.slice! RE
    case
      when $1 then
        p.step_in
      when $2 then
        p.step_out
      when $3 then
        r = UTF7.decode $3
        p.add r
      when $4 then
        n = $4[1,$4.length-2].to_i
        r = ""
        while n > 0 do
          l = input.readline
          l or raise "No more data after {#$4}"
          m = l.length
          if n <= m then
            r << (l.slice! 0, n)
          else
            r << l << "\n"
            l.clear
            n -= 2
          end
          n -= m
        end
        p.add r
      when $5 then
        r = $5.nil_if "NIL"
        r = UTF7.decode r if r
        p.add r
      else
        raise "Error reading '#$''"
    end
  end
  p
end

Instance Method Details

#add(token) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/hermeneutics/cli/imap/parser.rb', line 114

def add token
  if @sub then
    @sub.add token
  else
    @list.push token
  end
end

#closed?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/hermeneutics/cli/imap/parser.rb', line 122

def closed?
  not @sub
end

#step_inObject



97
98
99
100
101
102
103
# File 'lib/hermeneutics/cli/imap/parser.rb', line 97

def step_in
  if @sub then
    @sub.step_in
  else
    @sub = self.class.new
  end
end

#step_outObject



105
106
107
108
109
110
111
112
# File 'lib/hermeneutics/cli/imap/parser.rb', line 105

def step_out
  if @sub.closed? then
    @list.push @sub
    @sub = nil
  else
    @sub.step_out
  end
end

#walk(compiler) ⇒ Object



126
127
128
129
130
131
132
133
134
135
# File 'lib/hermeneutics/cli/imap/parser.rb', line 126

def walk compiler
  closed? or raise "Object was not fully parsed. Rest: #@sub"
  @list.each { |x|
    case x
      when self.class then compiler.step do x.walk compiler end
      else                 compiler.add x
    end
  }
  compiler.finish
end