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
@@ -5,7 +5,7 @@ class Packcr
5
5
  case gen.lang
6
6
  when :c
7
7
  erbout = +""
8
- erbout << "{\n 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});\n".freeze
8
+ erbout << "{\n packcr_thunk_t *const thunk = packcr_thunk__create_leaf(ctx->auxil, packcr_action_#{gen.rule.name}_#{index}, #{gen.rule.vars.length}, #{gen.rule.capts.length});\n".freeze
9
9
 
10
10
  vars.each do |var|
11
11
  erbout << " thunk->data.leaf.values.buf[#{var.index}] = &(chunk->values.buf[#{var.index}]);\n".freeze
@@ -18,7 +18,7 @@ class Packcr
18
18
  if gen.location
19
19
  erbout << " thunk->data.leaf.capt0.range.start_loc = chunk->pos_loc;\n thunk->data.leaf.capt0.range.end_loc = ctx->position_offset_loc;\n".freeze
20
20
  end
21
- erbout << " pcc_thunk_array__add(ctx->auxil, &chunk->thunks, thunk);\n}\n".freeze
21
+ erbout << " packcr_thunk_array__add(ctx->auxil, &chunk->thunks, thunk);\n}\n".freeze
22
22
 
23
23
  erbout
24
24
  when :rb
@@ -53,6 +53,23 @@ class Packcr
53
53
  erbout << " )\n)\n".freeze
54
54
 
55
55
  erbout
56
+ when :rs
57
+ erbout = +""
58
+ erbout << "answer.push_leaf(\n Action::#{Packcr.camelize(gen.rule.name)}#{index},\n self.input.position_offset,\n &[".freeze
59
+
60
+ vars.each_with_index do |var, i|
61
+ erbout << "#{", " if i > 0}#{var.index}".freeze
62
+ end
63
+ erbout << "],\n &[".freeze
64
+
65
+ capts.each_with_index do |capt, i|
66
+ erbout << "#{", " if i > 0}#{capt.index}".freeze
67
+ end
68
+ erbout << "],\n);\n".freeze
69
+
70
+ erbout
71
+ else
72
+ raise "unknown lang #{gen.lang}"
56
73
  end
57
74
  end
58
75
  end
@@ -9,7 +9,7 @@ class Packcr
9
9
  erbout << "{\n const size_t p = ctx->position_offset;\n".freeze
10
10
 
11
11
  if gen.location
12
- erbout << " const pcc_location_t p_loc = ctx->position_offset_loc;\n".freeze
12
+ erbout << " const packcr_location_t p_loc = ctx->position_offset_loc;\n".freeze
13
13
  end
14
14
  erbout << " const size_t n = chunk->thunks.len;\n".freeze
15
15
 
@@ -19,13 +19,12 @@ class Packcr
19
19
  r = expr.reachability
20
20
 
21
21
  erbout << "#{gen.generate_code(expr, l, 4, false)}".freeze
22
- case r
23
- when Packcr::CODE_REACH__ALWAYS_SUCCEED
22
+ if r == Packcr::CODE_REACH__ALWAYS_SUCCEED
24
23
  if c
25
24
  erbout << " /* unreachable codes omitted */\n".freeze
26
25
  end
27
26
  break
28
- when Packcr::CODE_REACH__BOTH
27
+ elsif r == Packcr::CODE_REACH__BOTH
29
28
  erbout << " goto L#{format("%04d", m)};\n".freeze
30
29
  end
31
30
  erbout << "L#{format("%04d", l)}:;\n ctx->position_offset = p;\n".freeze
@@ -33,7 +32,7 @@ class Packcr
33
32
  if gen.location
34
33
  erbout << " ctx->position_offset_loc = p_loc;\n".freeze
35
34
  end
36
- erbout << " pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);\n".freeze
35
+ erbout << " packcr_thunk_array__revert(ctx->auxil, &chunk->thunks, n);\n".freeze
37
36
 
38
37
  next if c
39
38
 
@@ -64,15 +63,14 @@ class Packcr
64
63
  r = expr.reachability
65
64
 
66
65
  erbout << "#{gen.generate_code(expr, l, 4, false, oncut: onfail)}".freeze
67
- case r
68
- when Packcr::CODE_REACH__ALWAYS_SUCCEED
66
+ if r == Packcr::CODE_REACH__ALWAYS_SUCCEED
69
67
  if c
70
68
  erbout << " # unreachable codes omitted\n".freeze
71
69
  end
72
70
  erbout << " end\n".freeze
73
71
 
74
72
  break
75
- when Packcr::CODE_REACH__BOTH
73
+ elsif r == Packcr::CODE_REACH__BOTH
76
74
  erbout << " throw(#{m})\n".freeze
77
75
  end
78
76
  erbout << " end\n".freeze
@@ -91,6 +89,54 @@ class Packcr
91
89
  erbout << "end\n".freeze
92
90
 
93
91
  erbout
92
+ when :rs
93
+ erbout = +""
94
+ m = gen.next_label
95
+ erbout << "'L#{format("%04d", m)}: {\n let p = self.input.position_offset;\n".freeze
96
+
97
+ if gen.location
98
+ erbout << " TODO\n".freeze
99
+ end
100
+ nodes.each_with_index do |expr, i|
101
+ erbout << " {\n".freeze
102
+
103
+ c = i + 1 < nodes.length
104
+ if expr.reversible?(gen)
105
+
106
+ erbout << "#{gen.generate_code(expr, m, 8, false, reverse: true, oncut: onfail)}".freeze
107
+ else
108
+ l = gen.next_label
109
+ erbout << " 'L#{format("%04d", l)}: {\n".freeze
110
+
111
+ r = expr.reachability
112
+
113
+ erbout << "#{gen.generate_code(expr, l, 12, false, oncut: onfail)}".freeze
114
+ if r == Packcr::CODE_REACH__ALWAYS_SUCCEED
115
+ if c
116
+ erbout << " // unreachable codes omitted\n".freeze
117
+ end
118
+ erbout << " }\n".freeze
119
+
120
+ break
121
+ elsif r == Packcr::CODE_REACH__BOTH
122
+ erbout << " break 'L#{format("%04d", m)};\n".freeze
123
+ end
124
+ erbout << " }\n".freeze
125
+ end
126
+ erbout << " }\n self.input.position_offset = p;\n".freeze
127
+
128
+ if gen.location
129
+ erbout << " TODO\n".freeze
130
+ end
131
+ next if c
132
+
133
+ erbout << " break 'L#{format("%04d", onfail)};\n".freeze
134
+ end
135
+ erbout << "} // 'L#{format("%04d", m)}\n".freeze
136
+
137
+ erbout
138
+ else
139
+ raise "unknown lang #{gen.lang}"
94
140
  end
95
141
  end
96
142
  end
@@ -8,7 +8,7 @@ class Packcr
8
8
  erbout << "{\n const size_t p = ctx->position_offset;\n size_t q;\n".freeze
9
9
 
10
10
  if gen.location
11
- erbout << " pcc_location_t p_loc = ctx->position_offset_loc;\n pcc_location_t q_loc;\n".freeze
11
+ erbout << " packcr_location_t p_loc = ctx->position_offset_loc;\n packcr_location_t q_loc;\n".freeze
12
12
  end
13
13
  erbout << "#{gen.generate_code(expr, onfail, 4, false)} q = ctx->position_offset;\n chunk->capts.buf[#{index}].range.start = p;\n chunk->capts.buf[#{index}].range.end = q;\n".freeze
14
14
 
@@ -32,6 +32,21 @@ class Packcr
32
32
  erbout << "q_loc#{gen.level} = @position_offset_loc\ncapt#{gen.level}.start_loc = p_loc#{gen.level}\ncapt#{gen.level}.end_loc = q_loc#{gen.level}\n".freeze
33
33
  end
34
34
  erbout
35
+ when :rs
36
+ erbout = +""
37
+ erbout << "let p_inner = self.input.position_offset;\n".freeze
38
+
39
+ if gen.location
40
+ erbout << "TODO\n".freeze
41
+ end
42
+ erbout << "{\n#{gen.generate_code(expr, onfail, 4, false)}}\nlet q = self.input.position_offset;\nanswer.capts[#{index}].start = p_inner;\nanswer.capts[#{index}].end = q;\n".freeze
43
+
44
+ if gen.location
45
+ erbout << "TODO\n".freeze
46
+ end
47
+ erbout
48
+ else
49
+ raise "unknown lang #{gen.lang}"
35
50
  end
36
51
  end
37
52
  end
@@ -5,7 +5,7 @@ class Packcr
5
5
  case gen.lang
6
6
  when :c
7
7
  erbout = +""
8
- erbout << "{\n char c;\n if (pcc_refill_buffer(ctx, 1) < 1) goto L#{format("%04d", onfail)};\n c = ctx->buffer.buf[ctx->position_offset];\n".freeze
8
+ erbout << "{\n char c;\n if (packcr_refill_buffer(ctx, 1) < 1) goto L#{format("%04d", onfail)};\n c = ctx->buffer.buf[ctx->position_offset];\n".freeze
9
9
  if !a && charclass =~ /\A[^\\]-.\z/
10
10
 
11
11
  if a
@@ -45,7 +45,7 @@ class Packcr
45
45
  end
46
46
  end
47
47
  if gen.location
48
- erbout << " pcc_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, 1);\n".freeze
48
+ erbout << " packcr_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, 1);\n".freeze
49
49
  end
50
50
  erbout << " ctx->position_offset++;\n}\n".freeze
51
51
 
@@ -92,6 +92,8 @@ class Packcr
92
92
  end
93
93
  erbout << "@position_offset += 1\n".freeze
94
94
  erbout
95
+ else
96
+ raise "unknown lang #{gen.lang}"
95
97
  end
96
98
  end
97
99
 
@@ -99,10 +101,10 @@ class Packcr
99
101
  case gen.lang
100
102
  when :c
101
103
  erbout = +""
102
- erbout << "if (pcc_refill_buffer(ctx, 1) < 1) goto L#{format("%04d", onfail)};\n".freeze
104
+ erbout << "if (packcr_refill_buffer(ctx, 1) < 1) goto L#{format("%04d", onfail)};\n".freeze
103
105
 
104
106
  if gen.location
105
- erbout << "pcc_location_forward(&ctx->position_offset, ctx->buffer.buf + ctx->position_offset, 1);\n".freeze
107
+ erbout << "packcr_location_forward(&ctx->position_offset, ctx->buffer.buf + ctx->position_offset, 1);\n".freeze
106
108
  end
107
109
  erbout << "ctx->position_offset++;\n".freeze
108
110
 
@@ -117,6 +119,8 @@ class Packcr
117
119
  erbout << "@position_offset += 1\n".freeze
118
120
 
119
121
  erbout
122
+ else
123
+ raise "unknown lang #{gen.lang}"
120
124
  end
121
125
  end
122
126
 
@@ -132,6 +136,8 @@ class Packcr
132
136
  erbout << "throw(#{onfail})\n".freeze
133
137
 
134
138
  erbout
139
+ else
140
+ raise "unknown lang #{gen.lang}"
135
141
  end
136
142
  end
137
143
 
@@ -140,14 +146,14 @@ class Packcr
140
146
  when :c
141
147
  erbout = +""
142
148
  if a
143
- erbout << "if (\n pcc_refill_buffer(ctx, 1) < 1 ||\n ctx->buffer.buf[ctx->position_offset] == '#{Packcr.escape_character(charclass[i])}'\n) goto L#{format("%04d", onfail)};\n".freeze
149
+ erbout << "if (\n packcr_refill_buffer(ctx, 1) < 1 ||\n ctx->buffer.buf[ctx->position_offset] == '#{Packcr.escape_character(charclass[i])}'\n) goto L#{format("%04d", onfail)};\n".freeze
144
150
 
145
151
  else
146
- erbout << "if (\n pcc_refill_buffer(ctx, 1) < 1 ||\n ctx->buffer.buf[ctx->position_offset] != '#{Packcr.escape_character(charclass[0])}'\n) goto L#{format("%04d", onfail)};\n".freeze
152
+ erbout << "if (\n packcr_refill_buffer(ctx, 1) < 1 ||\n ctx->buffer.buf[ctx->position_offset] != '#{Packcr.escape_character(charclass[0])}'\n) goto L#{format("%04d", onfail)};\n".freeze
147
153
 
148
154
  end
149
155
  if gen.location
150
- erbout << " pcc_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, 1);\n".freeze
156
+ erbout << " packcr_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, 1);\n".freeze
151
157
  end
152
158
  erbout << "ctx->position_offset++;\n".freeze
153
159
  erbout
@@ -165,6 +171,8 @@ class Packcr
165
171
  end
166
172
  erbout << "@position_offset += 1\n".freeze
167
173
  erbout
174
+ else
175
+ raise "unknown lang #{gen.lang}"
168
176
  end
169
177
  end
170
178
 
@@ -174,7 +182,7 @@ class Packcr
174
182
  erbout = +""
175
183
  a = charclass && charclass[0] == "^"
176
184
  i = a ? 1 : 0
177
- erbout << "{\n int u;\n const size_t n = pcc_get_char_as_utf32(ctx, &u);\n if (n == 0) goto L#{format("%04d", onfail)};\n".freeze
185
+ erbout << "{\n int u;\n const size_t n = packcr_get_char_as_utf32(ctx, &u);\n if (n == 0) goto L#{format("%04d", onfail)};\n".freeze
178
186
 
179
187
  if charclass && !(a && n == 1) # not '.' or '[^]'
180
188
  u0 = 0
@@ -227,7 +235,7 @@ class Packcr
227
235
  end
228
236
  end
229
237
  if gen.location
230
- erbout << " pcc_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, n);\n".freeze
238
+ erbout << " packcr_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, n);\n".freeze
231
239
  end
232
240
  erbout << " ctx->position_offset += n;\n}\n".freeze
233
241
 
@@ -295,6 +303,63 @@ class Packcr
295
303
  erbout << "@position_offset += 1\n".freeze
296
304
 
297
305
  erbout
306
+ when :rs
307
+ erbout = +""
308
+ a = charclass && charclass[0] == "^"
309
+ i = a ? 1 : 0
310
+ any_code = !charclass || (a && n == 1)
311
+ erbout << "let (#{any_code ? "_" : ""}u, n) = self.get_char_as_utf32();\nif n == 0 {\n break 'L#{format("%04d", onfail)};\n}\n".freeze
312
+
313
+ unless any_code
314
+ erbout << "if ".freeze
315
+ if !a
316
+
317
+ erbout << "!(".freeze
318
+ end
319
+ while i < n
320
+ if charclass[i] == "\\" && i + 1 < n
321
+ i += 1
322
+ end
323
+ u = charclass[i].ord
324
+ i += 1
325
+ if r
326
+ # character range
327
+
328
+ erbout << "(0x#{format("%06x", u0)}..=0x#{format("%06x", u)}).contains(&u)".freeze
329
+
330
+ if i < n
331
+ erbout << " || ".freeze
332
+ end
333
+ u0 = 0
334
+ r = false
335
+ elsif charclass[i] != "-" || i == n - 1 # the individual '-' character is valid when it is at the first or the last position
336
+ # single character
337
+
338
+ erbout << "u == 0x#{format("%06x", u)}".freeze
339
+ if i < n
340
+ erbout << " || ".freeze
341
+ end
342
+ u0 = 0
343
+ r = false
344
+ elsif charclass[i] == "-"
345
+ i += 1
346
+ u0 = u
347
+ r = true
348
+ else
349
+ raise "unexpected charclass #{charclass[i]}"
350
+ end
351
+ end
352
+ if !a
353
+ erbout << ") ".freeze
354
+ end
355
+
356
+ erbout << "{\n break 'L#{format("%04d", onfail)};\n}\n".freeze
357
+ end
358
+ erbout << "self.input.position_offset += n;\n".freeze
359
+
360
+ erbout
361
+ else
362
+ raise "unknown lang #{gen.lang}"
298
363
  end
299
364
  end
300
365
 
@@ -365,6 +430,8 @@ class Packcr
365
430
  erbout << "end\n".freeze
366
431
 
367
432
  erbout
433
+ else
434
+ raise "unknown lang #{gen.lang}"
368
435
  end
369
436
  end
370
437
  end
@@ -5,7 +5,7 @@ class Packcr
5
5
  case gen.lang
6
6
  when :c
7
7
  erbout = +""
8
- erbout << "if (pcc_refill_buffer(ctx, 1) >= 1) goto L#{format("%04d", onfail)};\n".freeze
8
+ erbout << "if (packcr_refill_buffer(ctx, 1) >= 1) goto L#{format("%04d", onfail)};\n".freeze
9
9
 
10
10
  erbout
11
11
  when :rb
@@ -13,6 +13,13 @@ class Packcr
13
13
  erbout << "if refill_buffer(1) >= 1\n throw(#{onfail})\nend\n".freeze
14
14
 
15
15
  erbout
16
+ when :rs
17
+ erbout = +""
18
+ erbout << "/* lib/packcr/templates/node/eof.rs.erb */\n\n".freeze
19
+
20
+ erbout
21
+ else
22
+ raise "unknown lang #{gen.lang}"
16
23
  end
17
24
  end
18
25
  end
@@ -10,7 +10,7 @@ class Packcr
10
10
  l = gen.next_label
11
11
  m = gen.next_label
12
12
 
13
- erbout << "#{gen.generate_code(expr, l, 4, true)} goto L#{format("%04d", m)};\nL#{format("%04d", l)}:;\n {\n pcc_value_t null;\n 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});\n".freeze
13
+ erbout << "#{gen.generate_code(expr, l, 4, true)} goto L#{format("%04d", m)};\nL#{format("%04d", l)}:;\n {\n packcr_value_t null;\n packcr_thunk_t *const thunk = packcr_thunk__create_leaf(ctx->auxil, packcr_action_#{gen.rule.name}_#{index}, #{gen.rule.vars.length}, #{gen.rule.capts.length});\n".freeze
14
14
 
15
15
  vars.each do |var|
16
16
  erbout << " thunk->data.leaf.values.buf[#{var.index}] = &(chunk->values.buf[#{var.index}]);\n".freeze
@@ -23,7 +23,7 @@ class Packcr
23
23
  if gen.location
24
24
  erbout << " thunk->data.leaf.capt0.range.start_loc = chunk->pos_loc;\n thunk->data.leaf.capt0.range.end_loc = ctx->position_offset_loc;\n".freeze
25
25
  end
26
- erbout << " memset(&null, 0, sizeof(pcc_value_t)); /* in case */\n thunk->data.leaf.action(ctx, thunk, &null);\n pcc_thunk__destroy(ctx->auxil, thunk);\n }\n goto L#{format("%04d", onfail)};\nL#{format("%04d", m)}:;\n}\n".freeze
26
+ erbout << " memset(&null, 0, sizeof(packcr_value_t)); /* in case */\n thunk->data.leaf.action(ctx, thunk, &null);\n packcr_thunk__destroy(ctx->auxil, thunk);\n }\n goto L#{format("%04d", onfail)};\nL#{format("%04d", m)}:;\n}\n".freeze
27
27
 
28
28
  erbout
29
29
  when :rb
@@ -60,6 +60,13 @@ class Packcr
60
60
  erbout << " ),\n nil,\n 0\n )\n\n throw(#{onfail})\nend\n".freeze
61
61
 
62
62
  erbout
63
+ when :rs
64
+ erbout = +""
65
+ erbout << "/* lib/packcr/templates/node/error.rs.erb */\n\n".freeze
66
+
67
+ erbout
68
+ else
69
+ raise "unknown lang #{gen.lang}"
63
70
  end
64
71
  end
65
72
  end
@@ -5,10 +5,10 @@ class Packcr
5
5
  case gen.lang
6
6
  when :c
7
7
  erbout = +""
8
- erbout << "{\n const size_t n = chunk->capts.buf[#{index}].range.end - chunk->capts.buf[#{index}].range.start;\n if (pcc_refill_buffer(ctx, n) < n) goto L#{format("%04d", onfail)};\n if (n > 0) {\n const char *const p = ctx->buffer.buf + ctx->position_offset;\n const char *const q = ctx->buffer.buf + chunk->capts.buf[#{index}].range.start;\n size_t i;\n for (i = 0; i < n; i++) {\n if (p[i] != q[i]) goto L#{format("%04d", onfail)};\n }\n".freeze
8
+ erbout << "{\n const size_t n = chunk->capts.buf[#{index}].range.end - chunk->capts.buf[#{index}].range.start;\n if (packcr_refill_buffer(ctx, n) < n) goto L#{format("%04d", onfail)};\n if (n > 0) {\n const char *const p = ctx->buffer.buf + ctx->position_offset;\n const char *const q = ctx->buffer.buf + chunk->capts.buf[#{index}].range.start;\n size_t i;\n for (i = 0; i < n; i++) {\n if (p[i] != q[i]) goto L#{format("%04d", onfail)};\n }\n".freeze
9
9
 
10
10
  if gen.location
11
- erbout << " pcc_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, n);\n".freeze
11
+ erbout << " packcr_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, n);\n".freeze
12
12
  end
13
13
  erbout << " ctx->position_offset += n;\n }\n}\n".freeze
14
14
 
@@ -23,6 +23,13 @@ class Packcr
23
23
  erbout << " @position_offset += n#{gen.level}\nend\n".freeze
24
24
 
25
25
  erbout
26
+ when :rs
27
+ erbout = +""
28
+ erbout << "/* lib/packcr/templates/node/expand.rs.erb */\n\n".freeze
29
+
30
+ erbout
31
+ else
32
+ raise "unknown lang #{gen.lang}"
26
33
  end
27
34
  end
28
35
  end
@@ -11,7 +11,7 @@ class Packcr
11
11
  erbout << "{\n const size_t p = ctx->position_offset;\n".freeze
12
12
 
13
13
  if gen.location
14
- erbout << " const pcc_location_t p_loc = ctx->position_offset_loc;\n".freeze
14
+ erbout << " const packcr_location_t p_loc = ctx->position_offset_loc;\n".freeze
15
15
  end
16
16
  erbout << "#{gen.generate_code(expr, l, 4, false)}".freeze
17
17
  if r != Packcr::CODE_REACH__ALWAYS_FAIL
@@ -72,6 +72,8 @@ class Packcr
72
72
  erbout << "end\n".freeze
73
73
 
74
74
  erbout
75
+ else
76
+ raise "unknown lang #{gen.lang}"
75
77
  end
76
78
  end
77
79
 
@@ -84,7 +86,7 @@ class Packcr
84
86
  erbout << "{\n const size_t p = ctx->position_offset;\n".freeze
85
87
 
86
88
  if gen.location
87
- erbout << " const pcc_location_t p_loc = ctx->position_offset_loc;\n".freeze
89
+ erbout << " const packcr_location_t p_loc = ctx->position_offset_loc;\n".freeze
88
90
  end
89
91
  erbout << "#{gen.generate_code(expr, l, 4, false)}".freeze
90
92
  if r != Packcr::CODE_REACH__ALWAYS_FAIL
@@ -133,6 +135,37 @@ class Packcr
133
135
  end
134
136
  end
135
137
  erbout
138
+ when :rs
139
+ erbout = +""
140
+ l = gen.next_label
141
+ r = expr.reachability
142
+ erbout << "let p = self.input.position_offset;\n".freeze
143
+
144
+ if gen.location
145
+ erbout << "TODO\n".freeze
146
+ end
147
+ erbout << "'L#{format("%04d", l)}: {\n {\n#{gen.generate_code(expr, l, 4, false)} }\n".freeze
148
+
149
+ if r != Packcr::CODE_REACH__ALWAYS_FAIL
150
+ erbout << " self.input.position_offset = p;\n".freeze
151
+
152
+ if gen.location
153
+ erbout << " TODO\n".freeze
154
+ end
155
+ erbout << " break 'L#{format("%04d", onfail)};\n".freeze
156
+ end
157
+ erbout << "}\n".freeze
158
+
159
+ if r != Packcr::CODE_REACH__ALWAYS_SUCCEED
160
+ erbout << "self.input.position_offset = p;\n".freeze
161
+
162
+ if gen.location
163
+ erbout << "TODO\n".freeze
164
+ end
165
+ end
166
+ erbout
167
+ else
168
+ raise "unknown lang #{gen.lang}"
136
169
  end
137
170
  end
138
171
  end