Class: Pegex::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/pegex/parser.rb

Defined Under Namespace

Classes: PegexParseError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Parser

Returns a new instance of Parser.

Yields:

  • (_self)

Yield Parameters:

  • _self (Pegex::Parser)

    the object that the method was called on



13
14
15
16
17
18
19
20
21
# File 'lib/pegex/parser.rb', line 13

def initialize
  @position = 0
  @farthest = 0
  @optimized = false
  @debug = false
  @throw_on_error = true
  # @debug = true
  yield self if block_given?
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



11
12
13
# File 'lib/pegex/parser.rb', line 11

def debug
  @debug
end

#grammarObject

Returns the value of attribute grammar.



7
8
9
# File 'lib/pegex/parser.rb', line 7

def grammar
  @grammar
end

#parentObject

Returns the value of attribute parent.



9
10
11
# File 'lib/pegex/parser.rb', line 9

def parent
  @parent
end

#receiverObject

Returns the value of attribute receiver.



8
9
10
# File 'lib/pegex/parser.rb', line 8

def receiver
  @receiver
end

#ruleObject

Returns the value of attribute rule.



10
11
12
# File 'lib/pegex/parser.rb', line 10

def rule
  @rule
end

Instance Method Details

#format_error(msg) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/pegex/parser.rb', line 260

def format_error msg
  buffer = @buffer
  position = @farthest
  real_pos = @position

  line = buffer[0, position].scan(/\n/).size + 1
  column = position - (buffer.rindex("\n", position) || -1)

  pretext = @buffer[
    position < 50 ? 0 : position - 50,
    position < 50 ? position : 50
  ]
  context = @buffer[position, 50]
  pretext.gsub! /.*\n/m, ''
  context.gsub! /\n/, "\\n"

  return <<"..."
Error parsing Pegex document:
msg:      #{msg}
line:     #{line}
column:   #{column}
context:  #{pretext}#{context}
#{' ' * (pretext.length + 10)}^
position: #{position} (#{real_pos} pre-lookahead)
...
end

#match_all(list, parent = nil) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/pegex/parser.rb', line 190

def match_all list, parent=nil
  position, set, len = @position, [], 0
  list.each do |elem|
    if match = match_next(elem)
      if !elem['+asr'] and !elem['-skip']
        set.concat match
        len += 1
      end
    else
      @farthest = position if (@position = position) > @farthest
      return false
    end
  end
  set = [set] if len > 1
  return set
end

#match_any(list, parent = nil) ⇒ Object



207
208
209
210
211
212
213
214
# File 'lib/pegex/parser.rb', line 207

def match_any list, parent=nil
  list.each do |elem|
    if (match = match_next elem)
      return match
    end
  end
  return false
end

#match_err(error, parent = nil) ⇒ Object



216
217
218
# File 'lib/pegex/parser.rb', line 216

def match_err error, parent=nil
  throw_error error
end

#match_next(next_) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/pegex/parser.rb', line 118

def match_next next_
  return match_next_with_sep next_ if next_['.sep']

  rule, method, kind, min, max, assertion =
    next_.values_at 'rule', 'method', 'kind', '+min', '+max', '+asr'

  position, match, count = @position, [], 0

  while return_ = method.call(rule, next_)
    position = @position unless assertion
    count += 1
    match.concat return_ unless return_.equal? $pegex_nil
    break if max == 1
  end
  if max != 1
    match = [match]
    @farthest = position if (@position = position) > @farthest
  end
  result = (count >= min and (max == 0 or count <= max)) ^ (assertion == -1)
  if not result or assertion
    @farthest = position if (@position = position) > @farthest
  end

  return result ? next_['-skip'] ? [] : match : false
end

#match_next_with_sep(next_) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/pegex/parser.rb', line 144

def match_next_with_sep next_
  rule, method, kind, min, max, sep =
    next_.values_at 'rule', 'method', 'kind', '+min', '+max', '.sep'

  position, match, count, scount, smin, smax =
    @position, [], 0, 0, sep.values_at('+min', '+max')

  while return_ = method.call(rule, next_)
    position = @position
    count += 1
    match.concat return_
    return_ = match_next(sep) or break
    match.concat return_
    scount += 1
  end
  if max != 1
    match = [match]
  end
  result = count >= min and (max == 0 or count <= max)
  if count == scount and not sep['+eok']
    @farthest = position if (@position = position) > @farthest
  end

  return result ? next_['-skip'] ? [] : match : false
end

#match_ref(ref, parent) ⇒ Object



170
171
172
173
174
175
176
177
# File 'lib/pegex/parser.rb', line 170

def match_ref ref, parent
  rule = @tree[ref]
  match = match_next(rule) or return false
  return $dummy unless rule['action']
  @rule, @parent = ref, parent
  result = rule['action'].call(match.first)
  return (result.equal? $pegex_nil) ? result : [result]
end

#match_ref_trace(ref, parent) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/pegex/parser.rb', line 220

def match_ref_trace ref, parent
  rule = @tree[ref]
  trace_on = ! rule['+asr']
  trace "try_#{ref}" if trace_on
  result = nil
  if (result = match_ref ref, parent)
    trace "got_#{ref}" if trace_on
  else
    trace "not_#{ref}" if trace_on
  end
  return result
end

#match_rgx(regexp, parent = nil) ⇒ Object



179
180
181
182
183
184
185
186
187
188
# File 'lib/pegex/parser.rb', line 179

def match_rgx regexp, parent=nil
  position = @position
  string = @buffer[position .. -1]
  (m = string.match regexp) or return false
  position += m[0].length
  match = m[1..-1]
  match = [ match ] if m.length > 2
  @farthest = position if (@position = position) > @farthest
  return match
end

#optimize_grammar(start) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/pegex/parser.rb', line 71

def optimize_grammar start
  return if @optimized
  @tree.each_pair do |name, node|
    next if node.kind_of? String
    optimize_node node
  end
  optimize_node '.ref' => start
  @optimized = true
end

#optimize_node(node) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/pegex/parser.rb', line 81

def optimize_node node
  ['ref', 'rgx', 'all', 'any', 'err', 'code', 'xxx'].each do |kind|
    fail if kind == 'xxx'
    if node['rule'] = node[".#{kind}"]
      node['kind'] = kind
      node['method'] = self.method "match_#{kind}"
      break
    end
  end
  min, max = node.values_at '+min', '+max'
  node['+min'] ||= max == nil ? 1 : 0
  node['+max'] ||= min == nil ? 1 : 0
  node['+asr'] ||= nil
  node['+min'] = node['+min'].to_i
  node['+max'] = node['+max'].to_i

  if ['any', 'all'].include? node['kind']
    node['rule'].each do |elem|
      optimize_node elem
    end
  elsif node['kind'] == 'ref'
    ref = node['rule']
    rule = @tree[ref]
    if @receiver.respond_to? "got_#{ref}"
      rule['action'] = receiver.method "got_#{ref}"
    elsif receiver.respond_to? 'gotrule'
      rule['action'] = receiver.method 'gotrule'
    end
    node['method'] = self.method 'match_ref_trace' if @debug
  elsif node['kind'] == 'rgx'
    node['rule'] = Regexp.new "\\A#{node['.rgx']}"
  end
  if sep = node['.sep']
    optimize_node sep
  end
end

#parse(input, start = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
# File 'lib/pegex/parser.rb', line 23

def parse input, start=nil
  @position = 0
  if input.kind_of? String
    input = Pegex::Input.new do |i|
      i.string = input
    end
  end
  @input = input
  @input.open unless @input.open?
  @buffer = @input.read
  @length = @buffer.length

  fail "No 'grammar'. Can't parse" unless @grammar
  @tree = @grammar.tree ||= @grammar.make_tree

  start_rule_ref = start ||
    @tree['+toprule'] ||
    (@tree['TOP'] ? 'TOP' : nil) or
      fail "No starting rule for Pegex::Parser::parse"

  optimize_grammar start_rule_ref

  fail  "No 'receiver'. Can't parse" unless @receiver

  # XXX does ruby have problems with circulat references
  @receiver.parser = self

  if @receiver.respond_to? 'initial'
    @rule, @parent = $start_rule_ref, {}
  end

  match = match_ref start_rule_ref, {}

  @input.close

  if !match or @position < @length
    throw_error "Parse document failed for some reason"
    return
  end

  if @receiver.respond_to? 'final'
    @rule, @parent = start_rule_ref, {}
    match = [ @receiver.final(match.first) ]
  end

  return match.first
end

#throw_error(msg) ⇒ Object

Raises:



246
247
248
# File 'lib/pegex/parser.rb', line 246

def throw_error msg
  raise msg
end

#trace(action) ⇒ Object



233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/pegex/parser.rb', line 233

def trace action
  indent = !!action.match(/^try_/)
  @indent ||= 0
  @indent -= 1 unless indent
  $stderr.print ' ' * @indent
  @indent += 1 if indent
  snippet = @buffer[@position..-1]
  snippet = snippet[0..30] + '...' if snippet.length > 30;
  snippet.gsub! /\n/, "\\n"
  $stderr.printf "%-30s", action
  $stderr.print indent ? " >#{snippet}<\n" : "\n"
end