packcr 0.0.8 → 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 (37) hide show
  1. checksums.yaml +4 -4
  2. data/lib/packcr/context.rb +14 -5
  3. data/lib/packcr/generated/context.rb +189 -85
  4. data/lib/packcr/generated/node/action_node.rb +17 -0
  5. data/lib/packcr/generated/node/alternate_node.rb +52 -6
  6. data/lib/packcr/generated/node/capture_node.rb +15 -0
  7. data/lib/packcr/generated/node/charclass_node.rb +67 -0
  8. data/lib/packcr/generated/node/eof_node.rb +7 -0
  9. data/lib/packcr/generated/node/error_node.rb +7 -0
  10. data/lib/packcr/generated/node/expand_node.rb +7 -0
  11. data/lib/packcr/generated/node/predicate_node.rb +33 -0
  12. data/lib/packcr/generated/node/quantity_node.rb +54 -0
  13. data/lib/packcr/generated/node/reference_node.rb +14 -0
  14. data/lib/packcr/generated/node/rule_node.rb +31 -0
  15. data/lib/packcr/generated/node/sequence_node.rb +18 -0
  16. data/lib/packcr/generated/node/string_node.rb +24 -0
  17. data/lib/packcr/node/reference_node.rb +1 -0
  18. data/lib/packcr/node/rule_node.rb +2 -1
  19. data/lib/packcr/parser.rb +403 -254
  20. data/lib/packcr/stream.rb +1 -1
  21. data/lib/packcr/templates/context/source.c.erb +30 -26
  22. data/lib/packcr/templates/context/source.rb.erb +160 -159
  23. data/lib/packcr/templates/context/source.rs.erb +625 -0
  24. data/lib/packcr/templates/node/action.rs.erb +6 -0
  25. data/lib/packcr/templates/node/alternate.rs.erb +39 -0
  26. data/lib/packcr/templates/node/capture.rs.erb +13 -0
  27. data/lib/packcr/templates/node/charclass_utf8.rs.erb +41 -0
  28. data/lib/packcr/templates/node/predicate_neg.rs.erb +24 -0
  29. data/lib/packcr/templates/node/quantity_many.rs.erb +50 -0
  30. data/lib/packcr/templates/node/reference.rs.erb +3 -0
  31. data/lib/packcr/templates/node/rule.rs.erb +34 -0
  32. data/lib/packcr/templates/node/sequence.rs.erb +12 -0
  33. data/lib/packcr/templates/node/string_many.rs.erb +10 -0
  34. data/lib/packcr/templates/node/string_one.rs.erb +9 -0
  35. data/lib/packcr/util.rb +9 -0
  36. data/lib/packcr/version.rb +1 -1
  37. metadata +16 -65
data/lib/packcr/stream.rb CHANGED
@@ -21,7 +21,7 @@ class Packcr
21
21
  end
22
22
 
23
23
  def write_text(s)
24
- write s.gsub(/\r\n/, "\n")
24
+ write s.gsub("\r\n", "\n")
25
25
  end
26
26
 
27
27
  def write_line_directive(fname, lineno)
@@ -1,9 +1,9 @@
1
1
  /* A packrat parser generated by PackCR <%= Packcr::VERSION %> */
2
2
 
3
- <%- code(:esource).each do |code| -%>
4
- <%= stream.get_code_block(code, 0, @iname) -%>
5
- <%- end -%>
6
3
  <%- if !code(:esource).empty? -%>
4
+ <%- code(:esource).each do |code| -%>
5
+ <%= stream.get_code_block(code, 0, @iname) -%>
6
+ <%- end -%>
7
7
 
8
8
  <%- end -%>
9
9
  #ifdef _MSC_VER
@@ -994,6 +994,33 @@ static void packcr_do_action(packcr_context_t *ctx, const packcr_thunk_array_t *
994
994
  }
995
995
  }
996
996
 
997
+ <%= prefix %>_context_t *<%= prefix %>_create(<%= auxil_def %>auxil) {
998
+ return packcr_context__create(auxil);
999
+ }
1000
+
1001
+ void <%= prefix %>_destroy(<%= prefix %>_context_t *ctx) {
1002
+ packcr_context__destroy(ctx);
1003
+ }
1004
+ <%- if !@root.rules.empty? -%>
1005
+
1006
+ <%- @root.rules.each do |rule| -%>
1007
+ static packcr_thunk_chunk_t *packcr_evaluate_rule_<%= rule.name %>(packcr_context_t *ctx, size_t offset<% if @location %>, packcr_location_t offset_loc<% end %>, packcr_rule_set_t *limits);
1008
+ <%- end -%>
1009
+ <%- end -%>
1010
+
1011
+ int <%= prefix %>_parse(<%= prefix %>_context_t *ctx, <%= value_def %>*ret) {
1012
+ size_t pos = ctx->buffer_start_position;
1013
+ <%- if !@root.rules.empty? -%>
1014
+ if (packcr_apply_rule(ctx, packcr_evaluate_rule_<%= @root.rules[0].name %>, &ctx->thunks, ret, ctx->position_offset<% if @location %>, ctx->position_offset_loc<% end %>, NULL))
1015
+ packcr_do_action(ctx, &ctx->thunks, ret);
1016
+ else
1017
+ PACKCR_ERROR(ctx->auxil);
1018
+ packcr_commit_buffer(ctx);
1019
+ <%- end -%>
1020
+ packcr_thunk_array__revert(ctx->auxil, &ctx->thunks, 0);
1021
+ return pos != ctx->buffer_start_position && packcr_refill_buffer(ctx, 1) >= 1;
1022
+ }
1023
+
997
1024
  <%- @root.rules.each do |rule| -%>
998
1025
  <%- rule.codes.each do |code| -%>
999
1026
  static void packcr_action_<%= rule.name %>_<%= code.index %>(<%= prefix %>_context_t *__packcr_ctx, packcr_thunk_t *__packcr_in, packcr_value_t *__packcr_out) {
@@ -1046,35 +1073,12 @@ static void packcr_action_<%= rule.name %>_<%= code.index %>(<%= prefix %>_conte
1046
1073
 
1047
1074
  <%- end -%>
1048
1075
  <%- end -%>
1049
- <%- @root.rules.each do |rule| -%>
1050
- static packcr_thunk_chunk_t *packcr_evaluate_rule_<%= rule.name %>(packcr_context_t *ctx, size_t offset<% if @location %>, packcr_location_t offset_loc<% end %>, packcr_rule_set_t *limits);
1051
- <%- end -%>
1052
1076
 
1053
1077
  <%- @root.rules.each do |rule| -%>
1054
1078
  <%- gen = ::Packcr::Generator.new(rule, @ascii, @location) -%>
1055
1079
  <%= gen.generate_code(rule, 0, 0, false) -%>
1056
1080
 
1057
1081
  <%- end -%>
1058
- <%= prefix %>_context_t *<%= prefix %>_create(<%= auxil_def %>auxil) {
1059
- return packcr_context__create(auxil);
1060
- }
1061
-
1062
- int <%= prefix %>_parse(<%= prefix %>_context_t *ctx, <%= value_def %>*ret) {
1063
- size_t pos = ctx->buffer_start_position;
1064
- <%- if !@root.rules.empty? -%>
1065
- if (packcr_apply_rule(ctx, packcr_evaluate_rule_<%= @root.rules[0].name %>, &ctx->thunks, ret, ctx->position_offset<% if @location %>, ctx->position_offset_loc<% end %>, NULL))
1066
- packcr_do_action(ctx, &ctx->thunks, ret);
1067
- else
1068
- PACKCR_ERROR(ctx->auxil);
1069
- packcr_commit_buffer(ctx);
1070
- <%- end -%>
1071
- packcr_thunk_array__revert(ctx->auxil, &ctx->thunks, 0);
1072
- return pos != ctx->buffer_start_position && packcr_refill_buffer(ctx, 1) >= 1;
1073
- }
1074
-
1075
- void <%= prefix %>_destroy(<%= prefix %>_context_t *ctx) {
1076
- packcr_context__destroy(ctx);
1077
- }
1078
1082
  <%- if !code(:lsource).empty? -%>
1079
1083
 
1080
1084
  <%- code(:lsource).each do |code| -%>
@@ -1,9 +1,10 @@
1
1
  # A packrat parser generated by PackCR <%= Packcr::VERSION %>
2
- <%- if !code(:esource).empty? -%>
3
2
 
3
+ <%- if !code(:esource).empty? -%>
4
4
  <%- code(:esource).each do |code| -%>
5
5
  <%= stream.get_code_block(code, 0, @iname) -%>
6
6
  <%- end -%>
7
+
7
8
  <%- end -%>
8
9
  class <%= class_name %>
9
10
  <%- code(:location).each do |code| -%>
@@ -14,6 +15,127 @@ class <%= class_name %>
14
15
  <%= stream.get_code_block(code, 2, @iname) -%>
15
16
 
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
+
17
139
  def initialize(<% if @auxil_type %><%= auxil_type %>, <% end %>debug: false)
18
140
  <%- if @utf8 -%>
19
141
  @buffer = +""
@@ -91,47 +213,6 @@ class <%= class_name %>
91
213
  nil while parse
92
214
  end
93
215
 
94
- <%- @root.rules.each do |rule| -%>
95
- <%- rule.codes.each do |code| -%>
96
- def action_<%= rule.name %>_<%= code.index %>(__packcr_in, __packcr_vars, __packcr_index)
97
- ____ = (__packcr_vars[__packcr_index] ||= Value.new).value if __packcr_vars
98
- <%- code.vars.each do |ref| -%>
99
- <%= ref.var %> = (__packcr_in.value_refs[<%= ref.index %>] ||= Value.new).value
100
- <%- end -%>
101
- __0 = __packcr_in.capt0.capture_string(@buffer)
102
- __0s = @buffer_start_position + __packcr_in.capt0.range_start
103
- __0e = @buffer_start_position + __packcr_in.capt0.range_end
104
- <%- if @location -%>
105
- __0sl = @buffer_start_position_loc + __packcr_in.capt0.start_loc
106
- __0el = @buffer_start_position_loc + __packcr_in.capt0.end_loc
107
- <%- end -%>
108
- <%- if @capture_in_code -%>
109
- __0c = __packcr_in.capt0
110
- <%- end -%>
111
- <%- code.capts.each do |capture| -%>
112
- __<%= capture.index + 1 %> = __packcr_in.capts[<%= capture.index %>].capture_string(@buffer)
113
- __<%= capture.index + 1 %>s = @buffer_start_position + __packcr_in.capts[<%= capture.index %>].range_start
114
- __<%= capture.index + 1 %>e = @buffer_start_position + __packcr_in.capts[<%= capture.index %>].range_end
115
- <%- if @location -%>
116
- __<%= capture.index + 1 %>sl = @buffer_start_position_loc + __packcr_in.capts[<%= capture.index %>].start_loc
117
- __<%= capture.index + 1 %>el = @buffer_start_position_loc + __packcr_in.capts[<%= capture.index %>].end_loc
118
- <%- end -%>
119
- <%- if @capture_in_code -%>
120
- __<%= capture.index + 1 %>c = __packcr_in.capts[<%= capture.index %>]
121
- <%- end -%>
122
- <%- end -%>
123
- <%- %><%= stream.get_code_block(code.code, 4, @iname) -%>
124
-
125
- __packcr_vars[__packcr_index].value = ____ if __packcr_vars
126
- end
127
-
128
- <%- end -%>
129
- <%- end -%>
130
- <%- @root.rules.each do |rule| -%>
131
- <%- gen = ::Packcr::Generator.new(rule, @ascii, @location, :rb) -%>
132
- <%- -%><%= gen.generate_code(rule, 0, 2, false) -%>
133
-
134
- <%- end -%>
135
216
  def grow_lr(rule, offset<% if @location %>, offset_loc<% end %>)
136
217
  while true
137
218
  old_offset = @position_offset
@@ -228,127 +309,47 @@ class <%= class_name %>
228
309
  thunk.do_action(self, values, index)
229
310
  end
230
311
  end
312
+ <%- @root.rules.each do |rule| -%>
313
+ <%- rule.codes.each do |code| -%>
231
314
 
232
- class LrMemoTable
233
- def initialize
234
- @memos = {}
235
- end
236
-
237
- def clear
238
- @memos.clear
239
- end
240
-
241
- def []=(index, rule_name, memo)
242
- entry = @memos[index] ||= {}
243
- entry[rule_name] = memo
244
- end
245
-
246
- def [](index, rule_name)
247
- @memos.dig(index, rule_name)
248
- end
249
- end
250
-
251
- class LrMemo
252
- attr_accessor :grow, :answer, :offset, :fail
253
- <%- if @location -%>
254
- attr_accessor :offset_loc
255
- <%- end -%>
256
-
257
- def initialize(offset<% if @location %>, offset_loc<% end %>)
258
- @offset = offset
259
- <%- if @location -%>
260
- @offset_loc = offset_loc
261
- <%- end -%>
262
- @fail = true
263
- @grow = false
264
- end
265
-
266
- def answer=(answer)
267
- @fail = nil
268
- @answer = answer
269
- end
270
- end
271
-
272
- class ThunkChunk
273
- attr_accessor :thunks, :capts, :pos, :values
274
- <%- if @location -%>
275
- attr_accessor :pos_loc
276
- <%- end -%>
277
-
278
- def initialize
279
- super
280
- @thunks = []
281
- @capts = {}
282
- @pos = 0
283
- @values = {}
284
- end
285
-
286
- def resize_captures(len)
287
- len.times do |i|
288
- @capts[i] = Capture.new
289
- end
290
- end
291
- end
292
-
293
- class ThunkLeaf
294
- attr_accessor :capt0, :capts, :value_refs, :action
295
-
296
- def initialize(action, capt0 = Capture.new, value_refs = {}, capts = {})
297
- @value_refs = value_refs
298
- @capts = capts
299
- @capt0 = capt0
300
- @action = action
301
- end
302
-
303
- def do_action(ctx, values, index)
304
- ctx.public_send(action, self, values, index)
305
- end
306
- end
307
-
308
- class ThunkNode
309
- attr_accessor :thunks, :values, :index
310
-
311
- def initialize(thunks, values, index)
312
- @thunks = thunks
313
- @values = values
314
- @index = index
315
- values[index] ||= Value.new if values
316
- end
317
-
318
- def do_action(ctx, _values, _index)
319
- @thunks.each do |thunk|
320
- thunk.do_action(ctx, @values, @index)
321
- end
322
- end
323
-
324
- def clear
325
- @thunks.clear
326
- end
327
- end
328
-
329
- class Capture
330
- attr_accessor :range_start, :range_end
331
- <%- if @location -%>
332
- attr_accessor :start_loc, :end_loc
333
- <%- end -%>
334
-
335
- def initialize(range_start = 0, range_end = 0<% if @location %>, start_loc = nil, end_loc = nil<% end%>)
336
- @range_start = range_start
337
- @range_end = range_end
338
- <%- if @location -%>
339
- @start_loc = start_loc || Location.new
340
- @end_loc = end_loc || Location.new
341
- <%- end -%>
342
- 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) -%>
343
343
 
344
- def capture_string(buffer)
345
- @capture_string ||= buffer[@range_start, @range_end - @range_start]
346
- end
344
+ __packcr_vars[__packcr_index].value = ____ if __packcr_vars
347
345
  end
346
+ <%- end -%>
347
+ <%- end -%>
348
+ <%- @root.rules.each do |rule| -%>
348
349
 
349
- class Value
350
- attr_accessor :value
351
- end
350
+ <%- gen = ::Packcr::Generator.new(rule, @ascii, @location, :rb) -%>
351
+ <%- -%><%= gen.generate_code(rule, 0, 2, false) -%>
352
+ <%- end -%>
352
353
  end
353
354
  <%- if !code(:lsource).empty? -%>
354
355