packcr 0.0.4 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/exe/packcr +3 -0
  3. data/lib/packcr/broadcast.rb +17 -0
  4. data/lib/packcr/cli.rb +27 -0
  5. data/lib/packcr/code_block.rb +2 -4
  6. data/lib/packcr/context.rb +66 -61
  7. data/lib/packcr/generated/context.rb +440 -0
  8. data/lib/packcr/generated/node/action_node.rb +60 -0
  9. data/lib/packcr/generated/node/alternate_node.rb +98 -0
  10. data/lib/packcr/generated/node/capture_node.rb +39 -0
  11. data/lib/packcr/generated/node/charclass_node.rb +372 -0
  12. data/lib/packcr/generated/node/eof_node.rb +20 -0
  13. data/lib/packcr/generated/node/error_node.rb +67 -0
  14. data/lib/packcr/generated/node/expand_node.rb +30 -0
  15. data/lib/packcr/generated/node/predicate_node.rb +140 -0
  16. data/lib/packcr/generated/node/quantity_node.rb +167 -0
  17. data/lib/packcr/generated/node/reference_node.rb +70 -0
  18. data/lib/packcr/generated/node/rule_node.rb +63 -0
  19. data/lib/packcr/generated/node/sequence_node.rb +42 -0
  20. data/lib/packcr/generated/node/string_node.rb +60 -0
  21. data/lib/packcr/generator.rb +14 -11
  22. data/lib/packcr/node/action_node.rb +18 -7
  23. data/lib/packcr/node/alternate_node.rb +22 -12
  24. data/lib/packcr/node/capture_node.rb +14 -12
  25. data/lib/packcr/node/charclass_node.rb +44 -24
  26. data/lib/packcr/node/eof_node.rb +25 -0
  27. data/lib/packcr/node/error_node.rb +20 -13
  28. data/lib/packcr/node/expand_node.rb +18 -9
  29. data/lib/packcr/node/predicate_node.rb +19 -26
  30. data/lib/packcr/node/quantity_node.rb +31 -24
  31. data/lib/packcr/node/reference_node.rb +31 -9
  32. data/lib/packcr/node/root_node.rb +61 -0
  33. data/lib/packcr/node/rule_node.rb +29 -3
  34. data/lib/packcr/node/sequence_node.rb +48 -31
  35. data/lib/packcr/node/string_node.rb +19 -16
  36. data/lib/packcr/node.rb +34 -5
  37. data/lib/packcr/parser.rb +4493 -3896
  38. data/lib/packcr/stream.rb +22 -22
  39. data/lib/packcr/templates/context/header.c.erb +29 -0
  40. data/lib/packcr/templates/context/source.c.erb +1069 -0
  41. data/lib/packcr/templates/context/source.rb.erb +341 -0
  42. data/lib/packcr/templates/node/action.c.erb +16 -0
  43. data/lib/packcr/templates/node/action.rb.erb +21 -0
  44. data/lib/packcr/templates/node/alternate.c.erb +34 -0
  45. data/lib/packcr/templates/node/alternate.rb.erb +39 -0
  46. data/lib/packcr/templates/node/capture.c.erb +17 -0
  47. data/lib/packcr/templates/node/capture.rb.erb +14 -0
  48. data/lib/packcr/templates/node/charclass.c.erb +47 -0
  49. data/lib/packcr/templates/node/charclass.rb.erb +49 -0
  50. data/lib/packcr/templates/node/charclass_any.c.erb +5 -0
  51. data/lib/packcr/templates/node/charclass_any.rb.erb +7 -0
  52. data/lib/packcr/templates/node/charclass_fail.c.erb +1 -0
  53. data/lib/packcr/templates/node/charclass_fail.rb.erb +1 -0
  54. data/lib/packcr/templates/node/charclass_one.c.erb +19 -0
  55. data/lib/packcr/templates/node/charclass_one.rb.erb +23 -0
  56. data/lib/packcr/templates/node/charclass_utf8.c.erb +49 -0
  57. data/lib/packcr/templates/node/charclass_utf8.rb.erb +50 -0
  58. data/lib/packcr/templates/node/charclass_utf8_reverse.rb.erb +51 -0
  59. data/lib/packcr/templates/node/eof.c.erb +1 -0
  60. data/lib/packcr/templates/node/eof.rb.erb +3 -0
  61. data/lib/packcr/templates/node/error.c.erb +28 -0
  62. data/lib/packcr/templates/node/error.rb.erb +34 -0
  63. data/lib/packcr/templates/node/expand.c.erb +16 -0
  64. data/lib/packcr/templates/node/expand.rb.erb +16 -0
  65. data/lib/packcr/templates/node/predicate.c.erb +30 -0
  66. data/lib/packcr/templates/node/predicate.rb.erb +28 -0
  67. data/lib/packcr/templates/node/predicate_neg.c.erb +23 -0
  68. data/lib/packcr/templates/node/predicate_neg.rb.erb +22 -0
  69. data/lib/packcr/templates/node/quantity_many.c.erb +45 -0
  70. data/lib/packcr/templates/node/quantity_many.rb.erb +47 -0
  71. data/lib/packcr/templates/node/quantity_one.c.erb +21 -0
  72. data/lib/packcr/templates/node/quantity_one.rb.erb +23 -0
  73. data/lib/packcr/templates/node/reference.c.erb +17 -0
  74. data/lib/packcr/templates/node/reference.rb.erb +21 -0
  75. data/lib/packcr/templates/node/reference_reverse.rb.erb +21 -0
  76. data/lib/packcr/templates/node/rule.c.erb +26 -0
  77. data/lib/packcr/templates/node/rule.rb.erb +30 -0
  78. data/lib/packcr/templates/node/sequence.c.erb +12 -0
  79. data/lib/packcr/templates/node/sequence.rb.erb +12 -0
  80. data/lib/packcr/templates/node/string_many.c.erb +11 -0
  81. data/lib/packcr/templates/node/string_many.rb.erb +10 -0
  82. data/lib/packcr/templates/node/string_one.c.erb +8 -0
  83. data/lib/packcr/templates/node/string_one.rb.erb +10 -0
  84. data/lib/packcr/util.rb +21 -24
  85. data/lib/packcr/version.rb +1 -1
  86. data/lib/packcr.rb +9 -13
  87. metadata +117 -10
  88. data/lib/packcr/buffer.rb +0 -47
@@ -0,0 +1,341 @@
1
+ # A packrat parser generated by PackCR <%= Packcr::VERSION %>
2
+ <%- if !code(:source).empty? -%>
3
+
4
+ <%- code(:source).each do |code| -%>
5
+ <%= stream.get_code_block(code, 0, @iname) -%>
6
+ <%- end -%>
7
+ <%- end -%>
8
+
9
+ class <%= class_name %>
10
+ def initialize(<% if @auxil_type %><%= auxil_type %>, <% end %>debug: false)
11
+ <%- if @utf8 -%>
12
+ @buffer = +""
13
+ <%- else -%>
14
+ @buffer = +"".b
15
+ <%- end %>
16
+ @buffer_start_position = 0
17
+ @position_offset = 0
18
+ @level = 0
19
+ @thunk = ThunkNode.new([], nil, 0)
20
+ @memos = LrMemoTable.new
21
+ @debug = debug
22
+ @global_values = {}
23
+ <%- if @location -%>
24
+ @buffer_start_position_loc = Location.new
25
+ @position_offset_loc = Location.new
26
+ <%- end -%>
27
+ <%- code(:init).each do |code| -%>
28
+ <%- %><%= stream.get_code_block(code, 4, @iname) -%>
29
+ <%- end -%>
30
+ end
31
+
32
+ def debug
33
+ yield if @debug
34
+ end
35
+
36
+ def getc
37
+ <%- if @utf8 -%>
38
+ $stdin.getc
39
+ <%- else -%>
40
+ $stdin.getc&.b
41
+ <%- end -%>
42
+ end
43
+
44
+ def refill_buffer(num, mode = nil)
45
+ len = @buffer.length
46
+ if len >= @position_offset + num
47
+ return len - @position_offset
48
+ end
49
+ while len < @position_offset + num
50
+ c = getc
51
+ break if !c
52
+ @buffer << c
53
+ len = @buffer.length
54
+ end
55
+ return len - @position_offset
56
+ end
57
+
58
+ def commit_buffer
59
+ @buffer = @buffer[@position_offset, @buffer.length - @position_offset]
60
+ @buffer_start_position += @position_offset
61
+ @memos.clear
62
+ @position_offset = 0
63
+ <%- if @location -%>
64
+ @buffer_start_position_loc = @buffer_start_position_loc + @position_offset_loc
65
+ @position_offset_loc = Location.new
66
+ <%- end -%>
67
+ end
68
+
69
+ def parse
70
+ pos = @buffer_start_position
71
+ <%- if !@root.rules.empty? -%>
72
+ if apply_rule(:evaluate_rule_<%= @root.rules[0].name %>, @thunk.thunks, nil, 0, @buffer_start_position<% if @location %>, @buffer_start_position_loc<% end %>)
73
+ @thunk.do_action(self, nil, 0)
74
+ else
75
+ raise SyntaxError, "can't parse"
76
+ end
77
+ commit_buffer
78
+ <%- end -%>
79
+ @thunk.clear
80
+ refill_buffer(1) >= 1 && pos != @buffer_start_position
81
+ end
82
+
83
+ def run
84
+ nil while parse
85
+ end
86
+
87
+ <%- @root.rules.each do |rule| -%>
88
+ <%- rule.codes.each do |code| -%>
89
+ def action_<%= rule.name %>_<%= code.index %>(__pcc_in, __pcc_vars, __pcc_index)
90
+ ____ = (__pcc_vars[__pcc_index] ||= Value.new).value if __pcc_vars
91
+ <%- code.vars.each do |ref| -%>
92
+ <%= ref.var %> = (__pcc_in.value_refs[<%= ref.index %>] ||= Value.new).value
93
+ <%- end -%>
94
+ __0 = __pcc_in.capt0.capture_string(@buffer)
95
+ __0s = @buffer_start_position + __pcc_in.capt0.range_start
96
+ __0e = @buffer_start_position + __pcc_in.capt0.range_end
97
+ <%- if @location -%>
98
+ __0sl = @buffer_start_position_loc + __pcc_in.capt0.start_loc
99
+ __0el = @buffer_start_position_loc + __pcc_in.capt0.end_loc
100
+ <%- end -%>
101
+ <%- if @capture_in_code -%>
102
+ __0c = __pcc_in.capt0
103
+ <%- end -%>
104
+ <%- code.capts.each do |capture| -%>
105
+ __<%= capture.index + 1 %> = __pcc_in.capts[<%= capture.index %>].capture_string(@buffer)
106
+ __<%= capture.index + 1 %>s = @buffer_start_position + __pcc_in.capts[<%= capture.index %>].range_start
107
+ __<%= capture.index + 1 %>e = @buffer_start_position + __pcc_in.capts[<%= capture.index %>].range_end
108
+ <%- if @location -%>
109
+ __<%= capture.index + 1 %>sl = @buffer_start_position_loc + __pcc_in.capts[<%= capture.index %>].start_loc
110
+ __<%= capture.index + 1 %>el = @buffer_start_position_loc + __pcc_in.capts[<%= capture.index %>].end_loc
111
+ <%- end -%>
112
+ <%- if @capture_in_code -%>
113
+ __<%= capture.index + 1 %>c = __pcc_in.capts[<%= capture.index %>]
114
+ <%- end -%>
115
+ <%- end -%>
116
+ <%- %><%= stream.get_code_block(code.code, 4, @iname) -%>
117
+
118
+ __pcc_vars[__pcc_index].value = ____ if __pcc_vars
119
+ end
120
+
121
+ <%- end -%>
122
+ <%- end -%>
123
+ <%- @root.rules.each do |rule| -%>
124
+ <%- gen = ::Packcr::Generator.new(rule, @ascii, @location, :rb) -%>
125
+ <%- -%><%= gen.generate_code(rule, 0, 2, false) -%>
126
+
127
+ <%- end -%>
128
+ def grow_lr(rule, offset<% if @location %>, offset_loc<% end %>)
129
+ while true
130
+ old_offset = @position_offset
131
+ @position_offset = offset
132
+ <%- if @location -%>
133
+ @position_offset_loc = offset_loc
134
+ <%- end -%>
135
+ answer = public_send(rule, offset<% if @location %>, offset_loc<% end %>, limits: {rule => true})
136
+ if !answer || @position_offset <= old_offset
137
+ break
138
+ end
139
+ memo = @memos[offset, rule]
140
+ memo.answer = answer
141
+ memo.offset = @position_offset
142
+ <%- if @location -%>
143
+ memo.offset_loc = @position_offset_loc
144
+ <%- end -%>
145
+ end
146
+ end
147
+
148
+ def rule_answer(rule)
149
+ offset = @position_offset
150
+ <%- if @location -%>
151
+ offset_loc = @position_offset_loc
152
+ <%- end -%>
153
+ memo = @memos[offset, rule]
154
+
155
+ if !memo
156
+ memo = LrMemo.new(offset<% if @location %>, offset_loc<% end %>)
157
+ @memos[offset, rule] = memo
158
+ answer = public_send(rule, offset<% if @location %>, offset_loc<% end %>)
159
+ memo.answer = answer
160
+ memo.offset = @position_offset
161
+ <%- if @location -%>
162
+ memo.offset_loc = @position_offset_loc
163
+ <%- end -%>
164
+ if memo.grow
165
+ grow_lr(rule, offset<% if @location %>, offset_loc<% end %>)
166
+ memo.grow = false
167
+ answer = memo.answer
168
+ @position_offset = memo.offset
169
+ end
170
+ return answer
171
+ elsif memo.fail
172
+ memo.answer = nil
173
+ memo.grow = true
174
+ return nil
175
+ else
176
+ @position_offset = memo.offset
177
+ return memo.answer
178
+ end
179
+ end
180
+
181
+ def apply_rule(rule, thunks, values, index, offset<% if @location %>, offset_loc<% end %>, limits: nil)
182
+ if limits
183
+ limits = limits.merge(rule => true)
184
+ answer = public_send(rule, offset<% if @location %>, offset_loc<% end %>, limits: limits)
185
+ memo = @memos[offset, rule]
186
+ if !answer || @position_offset <= memo.offset
187
+ answer = memo.answer
188
+ @position_offset = memo.offset
189
+ else
190
+ memo.answer = answer
191
+ memo.offset = @position_offset
192
+ end
193
+ else
194
+ answer = rule_answer(rule)
195
+ end
196
+
197
+ if !answer
198
+ return false
199
+ end
200
+ values ||= @global_values
201
+ thunks << ThunkNode.new(answer.thunks, values, index)
202
+ return true
203
+ end
204
+
205
+ def do_action(thunks, values, index)
206
+ thunks.each do |thunk|
207
+ thunk.do_action(self, values, index)
208
+ end
209
+ end
210
+
211
+ <%- code(:location).each do |code| -%>
212
+ <%- %><%= stream.get_code_block(code, 2, @iname) -%>
213
+
214
+ <%- end -%>
215
+ class LrMemoTable
216
+ def initialize
217
+ @memos = {}
218
+ end
219
+
220
+ def clear
221
+ @memos.clear
222
+ end
223
+
224
+ def []=(index, rule_name, memo)
225
+ entry = @memos[index] ||= {}
226
+ entry[rule_name] = memo
227
+ end
228
+
229
+ def [](index, rule_name)
230
+ @memos.dig(index, rule_name)
231
+ end
232
+ end
233
+
234
+ class LrMemo
235
+ attr_accessor :grow, :answer, :offset, :fail
236
+ <%- if @location -%>
237
+ attr_accessor :offset_loc
238
+ <%- end -%>
239
+
240
+ def initialize(offset<% if @location %>, offset_loc<% end %>)
241
+ @offset = offset
242
+ <%- if @location -%>
243
+ @offset_loc = offset_loc
244
+ <%- end -%>
245
+ @fail = true
246
+ @grow = false
247
+ end
248
+
249
+ def answer=(answer)
250
+ @fail = nil
251
+ @answer = answer
252
+ end
253
+ end
254
+
255
+ class ThunkChunk
256
+ attr_accessor :thunks, :capts, :pos, :values
257
+ <%- if @location -%>
258
+ attr_accessor :pos_loc
259
+ <%- end -%>
260
+
261
+ def initialize
262
+ super
263
+ @thunks = []
264
+ @capts = {}
265
+ @pos = 0
266
+ @values = {}
267
+ end
268
+
269
+ def resize_captures(len)
270
+ len.times do |i|
271
+ @capts[i] = Capture.new
272
+ end
273
+ end
274
+ end
275
+
276
+ class ThunkLeaf
277
+ attr_accessor :capt0, :capts, :value_refs, :action
278
+
279
+ def initialize(action, capt0 = Capture.new, value_refs = {}, capts = {})
280
+ @value_refs = value_refs
281
+ @capts = capts
282
+ @capt0 = capt0
283
+ @action = action
284
+ end
285
+
286
+ def do_action(ctx, values, index)
287
+ ctx.public_send(action, self, values, index)
288
+ end
289
+ end
290
+
291
+ class ThunkNode
292
+ attr_accessor :thunks, :values, :index
293
+
294
+ def initialize(thunks, values, index)
295
+ @thunks = thunks
296
+ @values = values
297
+ @index = index
298
+ values[index] ||= Value.new if values
299
+ end
300
+
301
+ def do_action(ctx, _values, _index)
302
+ @thunks.each do |thunk|
303
+ thunk.do_action(ctx, @values, @index)
304
+ end
305
+ end
306
+
307
+ def clear
308
+ @thunks.clear
309
+ end
310
+ end
311
+
312
+ class Capture
313
+ attr_accessor :range_start, :range_end
314
+ <%- if @location -%>
315
+ attr_accessor :start_loc, :end_loc
316
+ <%- end -%>
317
+
318
+ def initialize(range_start = 0, range_end = 0<% if @location %>, start_loc = nil, end_loc = nil<% end%>)
319
+ @range_start = range_start
320
+ @range_end = range_end
321
+ <%- if @location -%>
322
+ @start_loc = start_loc || Location.new
323
+ @end_loc = end_loc || Location.new
324
+ <%- end -%>
325
+ end
326
+
327
+ def capture_string(buffer)
328
+ @capture_string ||= buffer[@range_start, @range_end - @range_start]
329
+ end
330
+ end
331
+
332
+ class Value
333
+ attr_accessor :value
334
+ end
335
+ end
336
+ <%- if !code(:lsource).empty? -%>
337
+
338
+ <%- code(:lsource).each do |code| -%>
339
+ <%= stream.get_code_block(code, 0, @iname) -%>
340
+ <%- end -%>
341
+ <%- end -%>
@@ -0,0 +1,16 @@
1
+ {
2
+ pcc_thunk_t *const thunk = pcc_thunk__create_leaf(ctx->auxil, pcc_action_<%= gen.rule.name %>_<%= index %>, <%= gen.rule.vars.length %>, <%= gen.rule.capts.length %>);
3
+ <%- vars.each do |var| -%>
4
+ thunk->data.leaf.values.buf[<%= var.index %>] = &(chunk->values.buf[<%= var.index %>]);
5
+ <%- end -%>
6
+ <%- capts.each do |capt| -%>
7
+ thunk->data.leaf.capts.buf[<%= capt.index %>] = &(chunk->capts.buf[<%= capt.index %>]);
8
+ <%- end -%>
9
+ thunk->data.leaf.capt0.range.start = chunk->pos;
10
+ thunk->data.leaf.capt0.range.end = ctx->position_offset;
11
+ <%- if gen.location -%>
12
+ thunk->data.leaf.capt0.range.start_loc = chunk->pos_loc;
13
+ thunk->data.leaf.capt0.range.end_loc = ctx->position_offset_loc;
14
+ <%- end -%>
15
+ pcc_thunk_array__add(ctx->auxil, &chunk->thunks, thunk);
16
+ }
@@ -0,0 +1,21 @@
1
+ answer.thunks.push(
2
+ ThunkLeaf.new(
3
+ :action_<%= gen.rule.name %>_<%= index %>,
4
+ Capture.new(
5
+ answer.pos, @position_offset,
6
+ <%- if gen.location -%>
7
+ answer.pos_loc, @position_offset_loc,
8
+ <%- end -%>
9
+ ),
10
+ <%- if vars.empty? -%>
11
+ {},
12
+ <%- else -%>
13
+ answer.values.slice(<% vars.each_with_index do |var, i| %><%= ", " if i > 0 %><%= var.index %><% end %>),
14
+ <%- end -%>
15
+ <%- if capts.empty? -%>
16
+ {},
17
+ <%- else -%>
18
+ answer.capts.slice(<% capts.each_with_index do |capt, i| %><%= ", " if i > 0 %><%= capt.index %><% end %>),
19
+ <%- end -%>
20
+ )
21
+ )
@@ -0,0 +1,34 @@
1
+ <%- m = gen.next_label -%>
2
+ {
3
+ const size_t p = ctx->position_offset;
4
+ <%- if gen.location -%>
5
+ const pcc_location_t p_loc = ctx->position_offset_loc;
6
+ <%- end -%>
7
+ const size_t n = chunk->thunks.len;
8
+ <%- nodes.each_with_index do |expr, i| -%>
9
+ <%- c = i + 1 < nodes.length -%>
10
+ <%- l = gen.next_label -%>
11
+ <%- r = expr.reachability -%>
12
+ <%- -%><%= gen.generate_code(expr, l, 4, false) -%>
13
+ <%- if r == Packcr::CODE_REACH__ALWAYS_SUCCEED -%>
14
+ <%- if c -%>
15
+ /* unreachable codes omitted */
16
+ <%- end -%>
17
+ <%- break -%>
18
+ <%- else -%>
19
+ <%- if r == Packcr::CODE_REACH__BOTH -%>
20
+ goto L<%= format("%04d", m) %>;
21
+ <%- end -%>
22
+ <%- end -%>
23
+ L<%= format("%04d", l) %>:;
24
+ ctx->position_offset = p;
25
+ <%- if gen.location -%>
26
+ ctx->position_offset_loc = p_loc;
27
+ <%- end -%>
28
+ pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
29
+ <%- if !c -%>
30
+ goto L<%= format("%04d", onfail) %>;
31
+ <%- end -%>
32
+ <%- end -%>
33
+ L<%= format("%04d", m) %>:;
34
+ }
@@ -0,0 +1,39 @@
1
+ <%- m = gen.next_label -%>
2
+ catch(<%= m %>) do
3
+ pos<%= gen.level %> = @position_offset
4
+ <%- if gen.location -%>
5
+ p_loc<%= gen.level %> = @position_offset_loc
6
+ <%- end -%>
7
+ n<%= gen.level %> = answer.thunks.length
8
+ <%- nodes.each_with_index do |expr, i| -%>
9
+ <%- c = i + 1 < nodes.length -%>
10
+ <%- if expr.reversible?(gen) -%>
11
+ <%- %><%= gen.generate_code(expr, m, 2, false, reverse: true, oncut: onfail) -%>
12
+ <%- else -%>
13
+ <%- l = gen.next_label -%>
14
+ catch(<%= l %>) do
15
+ <%- r = expr.reachability -%>
16
+ <%- %><%= gen.generate_code(expr, l, 4, false, oncut: onfail) -%>
17
+ <%- if r == Packcr::CODE_REACH__ALWAYS_SUCCEED -%>
18
+ <%- if c -%>
19
+ # unreachable codes omitted
20
+ <%- end -%>
21
+ end
22
+ <%- break -%>
23
+ <%- else -%>
24
+ <%- if r == Packcr::CODE_REACH__BOTH -%>
25
+ throw(<%= m %>)
26
+ <%- end -%>
27
+ <%- end -%>
28
+ end
29
+ <%- end -%>
30
+ @position_offset = pos<%= gen.level %>
31
+ <%- if gen.location -%>
32
+ @position_offset_loc = p_loc<%= gen.level %>
33
+ <%- end -%>
34
+ answer.thunks[n<%= gen.level %>..-1] = []
35
+ <%- if !c -%>
36
+ throw(<%= onfail %>)
37
+ <%- end -%>
38
+ <%- end -%>
39
+ end
@@ -0,0 +1,17 @@
1
+ {
2
+ const size_t p = ctx->position_offset;
3
+ size_t q;
4
+ <%- if gen.location -%>
5
+ pcc_location_t p_loc = ctx->position_offset_loc;
6
+ pcc_location_t q_loc;
7
+ <%- end -%>
8
+ <%= gen.generate_code(expr, onfail, 4, false) -%>
9
+ q = ctx->position_offset;
10
+ chunk->capts.buf[<%= index %>].range.start = p;
11
+ chunk->capts.buf[<%= index %>].range.end = q;
12
+ <%- if gen.location -%>
13
+ q_loc = ctx->position_offset_loc;
14
+ chunk->capts.buf[<%= index %>].range.start_loc = p_loc;
15
+ chunk->capts.buf[<%= index %>].range.end_loc = q_loc;
16
+ <%- end -%>
17
+ }
@@ -0,0 +1,14 @@
1
+ pos<%= gen.level %> = @position_offset
2
+ <%- if gen.location -%>
3
+ p_loc<%= gen.level %> = @position_offset_loc
4
+ <%- end -%>
5
+ <%- %><%= gen.generate_code(expr, onfail, 0, false) -%>
6
+ q<%= gen.level %> = @position_offset
7
+ capt<%= gen.level %> = answer.capts[<%= index %>]
8
+ capt<%= gen.level %>.range_start = pos<%= gen.level %>
9
+ capt<%= gen.level %>.range_end = q<%= gen.level %>
10
+ <%- if gen.location -%>
11
+ q_loc<%= gen.level %> = @position_offset_loc
12
+ capt<%= gen.level %>.start_loc = p_loc<%= gen.level %>
13
+ capt<%= gen.level %>.end_loc = q_loc<%= gen.level %>
14
+ <%- end -%>
@@ -0,0 +1,47 @@
1
+ {
2
+ <%- if !a && charclass =~ /\A[^\\]-.\z/ -%>
3
+ char c;
4
+ if (pcc_refill_buffer(ctx, 1) < 1) goto L<%= format("%04d", onfail) %>;
5
+ c = ctx->buffer.buf[ctx->position_offset];
6
+ <%- if a -%>
7
+ if (c >= '<%= Packcr.escape_character(charclass[0]) %>' && c <= '<%= Packcr.escape_character(charclass[2]) %>') goto L<%= format("%04d", onfail) %>;
8
+ <%- else -%>
9
+ if (!(c >= '<%= Packcr.escape_character(charclass[0]) %>' && c <= '<%= Packcr.escape_character(charclass[2]) %>')) goto L<%= format("%04d", onfail) %>;
10
+ <%- end -%>
11
+ <%- if gen.location -%>
12
+ pcc_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, 1);
13
+ <%- end -%>
14
+ ctx->position_offset++;
15
+ <%- else -%>
16
+ char c;
17
+ if (pcc_refill_buffer(ctx, 1) < 1) goto L<%= format("%04d", onfail) %>;
18
+ c = ctx->buffer.buf[ctx->position_offset];
19
+ <%- if a -%>
20
+ if (
21
+ <%- else -%>
22
+ if (!(
23
+ <%- end -%>
24
+ <%- i = 0 -%>
25
+ <%- while i < n -%>
26
+ <%- if charclass[i] == "\\" && i + 1 < n -%>
27
+ <%- i += 1 -%>
28
+ <%- end -%>
29
+ <%- if i + 2 < n && charclass[i + 1] == '-' -%>
30
+ (c >= '<%= Packcr.escape_character(charclass[i]) %>' && c <= '<%= Packcr.escape_character(charclass[i + 2]) %>')<%= (i + 3 == n) ? "" : " ||" %>
31
+ <%- i += 2 -%>
32
+ <%- else -%>
33
+ c == '<%= Packcr.escape_character(charclass[i]) %>'<%= (i + 1 == n) ? "" : " ||" %>
34
+ <%- end -%>
35
+ <%- i += 1 -%>
36
+ <%- end -%>
37
+ <%- if a -%>
38
+ ) goto L<%= format("%04d", onfail) %>;
39
+ <%- else -%>
40
+ )) goto L<%= format("%04d", onfail) %>;
41
+ <%- end -%>
42
+ <%- if gen.location -%>
43
+ pcc_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, 1);
44
+ <%- end -%>
45
+ ctx->position_offset++;
46
+ <%- end -%>
47
+ }
@@ -0,0 +1,49 @@
1
+ <%- if !a && charclass =~ /\A[^\\]-.\z/ -%>
2
+ if refill_buffer(1) < 1
3
+ throw(<%= onfail %>)
4
+ end
5
+ c<%= gen.level %> = @buffer[@position_offset]
6
+ <%- if a -%>
7
+ if c<%= gen.level %> >= "<%= Packcr.escape_character(charclass[0]) %>" && c<%= gen.level %> <= "<%= Packcr.escape_character(charclass[2]) %>"
8
+ throw(<%= onfail %>)
9
+ end
10
+ <%- else -%>
11
+ unless c<%= gen.level %> >= "<%= Packcr.escape_character(charclass[0]) %>" && c<%= gen.level %> <= "<%= Packcr.escape_character(charclass[2]) %>"
12
+ throw(<%= onfail %>)
13
+ end
14
+ <%- end -%>
15
+ <%- if gen.location -%>
16
+ @position_offset_loc = @position_offset_loc.forward(@buffer, @position_offset, 1)
17
+ <%- end -%>
18
+ @position_offset += 1
19
+ <%- else -%>
20
+ if refill_buffer(1) < 1
21
+ throw(<%= onfail %>)
22
+ end
23
+ c<%= gen.level %> = @buffer[@position_offset]
24
+ <%- if a -%>
25
+ if (
26
+ <%- else -%>
27
+ unless (
28
+ <%- end -%>
29
+ <%- i = 0 -%>
30
+ <%- while i < n -%>
31
+ <%- if charclass[i] == "\\" && i + 1 < n -%>
32
+ <%- i += 1 -%>
33
+ <%- end -%>
34
+ <%- if i + 2 < n && charclass[i + 1] == '-' -%>
35
+ (c<%= gen.level %> >= "<%= Packcr.escape_character(charclass[i]) %>" && c<%= gen.level %> <= "<%= Packcr.escape_character(charclass[i + 2]) %>")<%= (i + 3 == n) ? "" : " ||" %>
36
+ <%- i += 2 -%>
37
+ <%- else -%>
38
+ c<%= gen.level %> == "<%= Packcr.escape_character(charclass[i]) %>"<%= (i + 1 == n) ? "" : " ||" %>
39
+ <%- end -%>
40
+ <%- i += 1 -%>
41
+ <%- end -%>
42
+ )
43
+ throw(<%= onfail %>)
44
+ end
45
+ <%- if gen.location -%>
46
+ @position_offset_loc = @position_offset_loc.forward(@buffer, @position_offset, 1)
47
+ <%- end -%>
48
+ @position_offset += 1
49
+ <%- end -%>
@@ -0,0 +1,5 @@
1
+ if (pcc_refill_buffer(ctx, 1) < 1) goto L<%= format("%04d", onfail) %>;
2
+ <%- if gen.location -%>
3
+ pcc_location_forward(&ctx->position_offset, ctx->buffer.buf + ctx->position_offset, 1);
4
+ <%- end -%>
5
+ ctx->position_offset++;
@@ -0,0 +1,7 @@
1
+ if refill_buffer(1) < 1
2
+ throw(<%= onfail %>)
3
+ end
4
+ <%- if gen.location -%>
5
+ @position_offset_loc = @position_offset_loc.forward(@buffer, @position_offset, 1)
6
+ <%- end -%>
7
+ @position_offset += 1
@@ -0,0 +1 @@
1
+ goto L<%= format("%04d", onfail) %>;
@@ -0,0 +1 @@
1
+ throw(<%= onfail %>)
@@ -0,0 +1,19 @@
1
+ <%- if a -%>
2
+ if (
3
+ pcc_refill_buffer(ctx, 1) < 1 ||
4
+ ctx->buffer.buf[ctx->position_offset] == '<%= Packcr.escape_character(charclass[i]) %>'
5
+ ) goto L<%= format("%04d", onfail) %>;
6
+ <%- if gen.location -%>
7
+ pcc_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, 1);
8
+ <%- end -%>
9
+ ctx->position_offset++;
10
+ <%- else -%>
11
+ if (
12
+ pcc_refill_buffer(ctx, 1) < 1 ||
13
+ ctx->buffer.buf[ctx->position_offset] != '<%= Packcr.escape_character(charclass[0]) %>'
14
+ ) goto L<%= format("%04d", onfail) %>;
15
+ <%- if gen.location -%>
16
+ pcc_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, 1);
17
+ <%- end -%>
18
+ ctx->position_offset++;
19
+ <%- end -%>
@@ -0,0 +1,23 @@
1
+ <%- if a -%>
2
+ if (
3
+ refill_buffer(1) < 1 ||
4
+ @buffer[@position_offset] == "<%= Packcr.escape_character(charclass[0]) %>"
5
+ )
6
+ throw(<%= onfail %>)
7
+ end
8
+ <%- if gen.location -%>
9
+ @position_offset_loc = @position_offset_loc.forward(@buffer, @position_offset, 1)
10
+ <%- end -%>
11
+ @position_offset += 1
12
+ <%- else -%>
13
+ if (
14
+ refill_buffer(1) < 1 ||
15
+ @buffer[@position_offset] != "<%= Packcr.escape_character(charclass[0]) %>"
16
+ )
17
+ throw(<%= onfail %>)
18
+ end
19
+ <%- if gen.location -%>
20
+ @position_offset_loc = @position_offset_loc.forward(@buffer, @position_offset, 1)
21
+ <%- end -%>
22
+ @position_offset += 1
23
+ <%- end -%>