packcr 0.0.7 → 0.1.0

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 (59) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +21 -0
  3. data/README.md +73 -0
  4. data/lib/packcr/context.rb +15 -6
  5. data/lib/packcr/generated/context.rb +290 -143
  6. data/lib/packcr/generated/node/action_node.rb +19 -2
  7. data/lib/packcr/generated/node/alternate_node.rb +54 -8
  8. data/lib/packcr/generated/node/capture_node.rb +16 -1
  9. data/lib/packcr/generated/node/charclass_node.rb +76 -9
  10. data/lib/packcr/generated/node/eof_node.rb +8 -1
  11. data/lib/packcr/generated/node/error_node.rb +9 -2
  12. data/lib/packcr/generated/node/expand_node.rb +9 -2
  13. data/lib/packcr/generated/node/predicate_node.rb +35 -2
  14. data/lib/packcr/generated/node/quantity_node.rb +84 -31
  15. data/lib/packcr/generated/node/reference_node.rb +16 -2
  16. data/lib/packcr/generated/node/rule_node.rb +38 -7
  17. data/lib/packcr/generated/node/sequence_node.rb +18 -0
  18. data/lib/packcr/generated/node/string_node.rb +28 -4
  19. data/lib/packcr/node/reference_node.rb +1 -0
  20. data/lib/packcr/node/rule_node.rb +2 -1
  21. data/lib/packcr/parser.rb +993 -838
  22. data/lib/packcr/stream.rb +1 -1
  23. data/lib/packcr/templates/context/header.c.erb +3 -3
  24. data/lib/packcr/templates/context/source.c.erb +438 -420
  25. data/lib/packcr/templates/context/source.rb.erb +185 -167
  26. data/lib/packcr/templates/context/source.rs.erb +625 -0
  27. data/lib/packcr/templates/node/action.c.erb +2 -2
  28. data/lib/packcr/templates/node/action.rs.erb +6 -0
  29. data/lib/packcr/templates/node/alternate.c.erb +2 -2
  30. data/lib/packcr/templates/node/alternate.rs.erb +39 -0
  31. data/lib/packcr/templates/node/capture.c.erb +2 -2
  32. data/lib/packcr/templates/node/capture.rs.erb +13 -0
  33. data/lib/packcr/templates/node/charclass.c.erb +4 -4
  34. data/lib/packcr/templates/node/charclass_any.c.erb +2 -2
  35. data/lib/packcr/templates/node/charclass_one.c.erb +4 -4
  36. data/lib/packcr/templates/node/charclass_utf8.c.erb +2 -2
  37. data/lib/packcr/templates/node/charclass_utf8.rs.erb +41 -0
  38. data/lib/packcr/templates/node/eof.c.erb +1 -1
  39. data/lib/packcr/templates/node/error.c.erb +4 -4
  40. data/lib/packcr/templates/node/expand.c.erb +2 -2
  41. data/lib/packcr/templates/node/predicate.c.erb +1 -1
  42. data/lib/packcr/templates/node/predicate_neg.c.erb +1 -1
  43. data/lib/packcr/templates/node/predicate_neg.rs.erb +24 -0
  44. data/lib/packcr/templates/node/quantity_many.c.erb +9 -7
  45. data/lib/packcr/templates/node/quantity_many.rs.erb +50 -0
  46. data/lib/packcr/templates/node/quantity_one.c.erb +9 -7
  47. data/lib/packcr/templates/node/quantity_one.rb.erb +4 -4
  48. data/lib/packcr/templates/node/reference.c.erb +6 -6
  49. data/lib/packcr/templates/node/reference.rs.erb +3 -0
  50. data/lib/packcr/templates/node/rule.c.erb +9 -9
  51. data/lib/packcr/templates/node/rule.rs.erb +34 -0
  52. data/lib/packcr/templates/node/sequence.rs.erb +12 -0
  53. data/lib/packcr/templates/node/string_many.c.erb +2 -2
  54. data/lib/packcr/templates/node/string_many.rs.erb +10 -0
  55. data/lib/packcr/templates/node/string_one.c.erb +2 -2
  56. data/lib/packcr/templates/node/string_one.rs.erb +9 -0
  57. data/lib/packcr/util.rb +10 -1
  58. data/lib/packcr/version.rb +1 -1
  59. metadata +18 -65
@@ -1,12 +1,141 @@
1
1
  # A packrat parser generated by PackCR <%= Packcr::VERSION %>
2
- <%- if !code(:source).empty? -%>
3
2
 
4
- <%- code(:source).each do |code| -%>
3
+ <%- if !code(:esource).empty? -%>
4
+ <%- code(:esource).each do |code| -%>
5
5
  <%= stream.get_code_block(code, 0, @iname) -%>
6
6
  <%- end -%>
7
- <%- end -%>
8
7
 
8
+ <%- end -%>
9
9
  class <%= class_name %>
10
+ <%- code(:location).each do |code| -%>
11
+ <%- %><%= stream.get_code_block(code, 2, @iname) -%>
12
+
13
+ <%- end -%>
14
+ <%- code(:source).each do |code| -%>
15
+ <%= stream.get_code_block(code, 2, @iname) -%>
16
+
17
+ <%- end -%>
18
+ class LrMemoTable
19
+ def initialize
20
+ @memos = {}
21
+ end
22
+
23
+ def clear
24
+ @memos.clear
25
+ end
26
+
27
+ def []=(index, rule_name, memo)
28
+ entry = @memos[index] ||= {}
29
+ entry[rule_name] = memo
30
+ end
31
+
32
+ def [](index, rule_name)
33
+ @memos.dig(index, rule_name)
34
+ end
35
+ end
36
+
37
+ class LrMemo
38
+ attr_accessor :grow, :answer, :offset, :fail
39
+ <%- if @location -%>
40
+ attr_accessor :offset_loc
41
+ <%- end -%>
42
+
43
+ def initialize(offset<% if @location %>, offset_loc<% end %>)
44
+ @offset = offset
45
+ <%- if @location -%>
46
+ @offset_loc = offset_loc
47
+ <%- end -%>
48
+ @fail = true
49
+ @grow = false
50
+ end
51
+
52
+ def answer=(answer)
53
+ @fail = nil
54
+ @answer = answer
55
+ end
56
+ end
57
+
58
+ class ThunkChunk
59
+ attr_accessor :thunks, :capts, :pos, :values
60
+ <%- if @location -%>
61
+ attr_accessor :pos_loc
62
+ <%- end -%>
63
+
64
+ def initialize
65
+ super
66
+ @thunks = []
67
+ @capts = {}
68
+ @pos = 0
69
+ @values = {}
70
+ end
71
+
72
+ def resize_captures(len)
73
+ len.times do |i|
74
+ @capts[i] = Capture.new
75
+ end
76
+ end
77
+ end
78
+
79
+ class ThunkLeaf
80
+ attr_accessor :capt0, :capts, :value_refs, :action
81
+
82
+ def initialize(action, capt0 = Capture.new, value_refs = {}, capts = {})
83
+ @value_refs = value_refs
84
+ @capts = capts
85
+ @capt0 = capt0
86
+ @action = action
87
+ end
88
+
89
+ def do_action(ctx, values, index)
90
+ ctx.public_send(action, self, values, index)
91
+ end
92
+ end
93
+
94
+ class ThunkNode
95
+ attr_accessor :thunks, :values, :index
96
+
97
+ def initialize(thunks, values, index)
98
+ @thunks = thunks
99
+ @values = values
100
+ @index = index
101
+ values[index] ||= Value.new if values
102
+ end
103
+
104
+ def do_action(ctx, _values, _index)
105
+ @thunks.each do |thunk|
106
+ thunk.do_action(ctx, @values, @index)
107
+ end
108
+ end
109
+
110
+ def clear
111
+ @thunks.clear
112
+ end
113
+ end
114
+
115
+ class Capture
116
+ attr_accessor :range_start, :range_end
117
+ <%- if @location -%>
118
+ attr_accessor :start_loc, :end_loc
119
+ <%- end -%>
120
+
121
+ def initialize(range_start = 0, range_end = 0<% if @location %>, start_loc = nil, end_loc = nil<% end%>)
122
+ @range_start = range_start
123
+ @range_end = range_end
124
+ <%- if @location -%>
125
+ @start_loc = start_loc || Location.new
126
+ @end_loc = end_loc || Location.new
127
+ <%- end -%>
128
+ end
129
+
130
+ def capture_string(buffer)
131
+ @capture_string ||= buffer[@range_start, @range_end - @range_start]
132
+ end
133
+ end
134
+
135
+ class Value
136
+ attr_accessor :value
137
+ end
138
+
10
139
  def initialize(<% if @auxil_type %><%= auxil_type %>, <% end %>debug: false)
11
140
  <%- if @utf8 -%>
12
141
  @buffer = +""
@@ -84,47 +213,6 @@ class <%= class_name %>
84
213
  nil while parse
85
214
  end
86
215
 
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
216
  def grow_lr(rule, offset<% if @location %>, offset_loc<% end %>)
129
217
  while true
130
218
  old_offset = @position_offset
@@ -166,6 +254,9 @@ class <%= class_name %>
166
254
  memo.grow = false
167
255
  answer = memo.answer
168
256
  @position_offset = memo.offset
257
+ <%- if @location -%>
258
+ @position_offset_loc = memo.offset_loc
259
+ <%- end -%>
169
260
  end
170
261
  return answer
171
262
  elsif memo.fail
@@ -174,6 +265,9 @@ class <%= class_name %>
174
265
  return nil
175
266
  else
176
267
  @position_offset = memo.offset
268
+ <%- if @location -%>
269
+ @position_offset_loc = memo.offset_loc
270
+ <%- end -%>
177
271
  return memo.answer
178
272
  end
179
273
  end
@@ -184,11 +278,19 @@ class <%= class_name %>
184
278
  answer = public_send(rule, offset<% if @location %>, offset_loc<% end %>, limits: limits)
185
279
  memo = @memos[offset, rule]
186
280
  if !answer || @position_offset <= memo.offset
187
- answer = memo.answer
188
- @position_offset = memo.offset
281
+ if memo
282
+ answer = memo.answer
283
+ @position_offset = memo.offset
284
+ <%- if @location -%>
285
+ @position_offset_loc = memo.offset_loc
286
+ <%- end -%>
287
+ end
189
288
  else
190
289
  memo.answer = answer
191
290
  memo.offset = @position_offset
291
+ <%- if @location -%>
292
+ memo.offset_loc = @position_offset_loc
293
+ <%- end -%>
192
294
  end
193
295
  else
194
296
  answer = rule_answer(rule)
@@ -207,131 +309,47 @@ class <%= class_name %>
207
309
  thunk.do_action(self, values, index)
208
310
  end
209
311
  end
312
+ <%- @root.rules.each do |rule| -%>
313
+ <%- rule.codes.each do |code| -%>
210
314
 
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
315
+ def action_<%= rule.name %>_<%= code.index %>(__packcr_in, __packcr_vars, __packcr_index)
316
+ ____ = (__packcr_vars[__packcr_index] ||= Value.new).value if __packcr_vars
317
+ <%- code.vars.each do |ref| -%>
318
+ <%= ref.var %> = (__packcr_in.value_refs[<%= ref.index %>] ||= Value.new).value
319
+ <%- end -%>
320
+ __0 = __packcr_in.capt0.capture_string(@buffer)
321
+ __0s = @buffer_start_position + __packcr_in.capt0.range_start
322
+ __0e = @buffer_start_position + __packcr_in.capt0.range_end
323
+ <%- if @location -%>
324
+ __0sl = @buffer_start_position_loc + __packcr_in.capt0.start_loc
325
+ __0el = @buffer_start_position_loc + __packcr_in.capt0.end_loc
326
+ <%- end -%>
327
+ <%- if @capture_in_code -%>
328
+ __0c = __packcr_in.capt0
329
+ <%- end -%>
330
+ <%- code.capts.each do |capture| -%>
331
+ __<%= capture.index + 1 %> = __packcr_in.capts[<%= capture.index %>].capture_string(@buffer)
332
+ __<%= capture.index + 1 %>s = @buffer_start_position + __packcr_in.capts[<%= capture.index %>].range_start
333
+ __<%= capture.index + 1 %>e = @buffer_start_position + __packcr_in.capts[<%= capture.index %>].range_end
334
+ <%- if @location -%>
335
+ __<%= capture.index + 1 %>sl = @buffer_start_position_loc + __packcr_in.capts[<%= capture.index %>].start_loc
336
+ __<%= capture.index + 1 %>el = @buffer_start_position_loc + __packcr_in.capts[<%= capture.index %>].end_loc
337
+ <%- end -%>
338
+ <%- if @capture_in_code -%>
339
+ __<%= capture.index + 1 %>c = __packcr_in.capts[<%= capture.index %>]
340
+ <%- end -%>
341
+ <%- end -%>
342
+ <%- %><%= stream.get_code_block(code.code, 4, @iname) -%>
326
343
 
327
- def capture_string(buffer)
328
- @capture_string ||= buffer[@range_start, @range_end - @range_start]
329
- end
344
+ __packcr_vars[__packcr_index].value = ____ if __packcr_vars
330
345
  end
346
+ <%- end -%>
347
+ <%- end -%>
348
+ <%- @root.rules.each do |rule| -%>
331
349
 
332
- class Value
333
- attr_accessor :value
334
- end
350
+ <%- gen = ::Packcr::Generator.new(rule, @ascii, @location, :rb) -%>
351
+ <%- -%><%= gen.generate_code(rule, 0, 2, false) -%>
352
+ <%- end -%>
335
353
  end
336
354
  <%- if !code(:lsource).empty? -%>
337
355