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
@@ -0,0 +1,13 @@
1
+ let p_inner = self.input.position_offset;
2
+ <%- if gen.location -%>
3
+ TODO
4
+ <%- end -%>
5
+ {
6
+ <%- %><%= gen.generate_code(expr, onfail, 4, false) -%>
7
+ }
8
+ let q = self.input.position_offset;
9
+ answer.capts[<%= index %>].start = p_inner;
10
+ answer.capts[<%= index %>].end = q;
11
+ <%- if gen.location -%>
12
+ TODO
13
+ <%- end -%>
@@ -1,7 +1,7 @@
1
1
  {
2
2
  <%- if !a && charclass =~ /\A[^\\]-.\z/ -%>
3
3
  char c;
4
- if (pcc_refill_buffer(ctx, 1) < 1) goto L<%= format("%04d", onfail) %>;
4
+ if (packcr_refill_buffer(ctx, 1) < 1) goto L<%= format("%04d", onfail) %>;
5
5
  c = ctx->buffer.buf[ctx->position_offset];
6
6
  <%- if a -%>
7
7
  if (c >= '<%= Packcr.escape_character(charclass[0]) %>' && c <= '<%= Packcr.escape_character(charclass[2]) %>') goto L<%= format("%04d", onfail) %>;
@@ -9,12 +9,12 @@
9
9
  if (!(c >= '<%= Packcr.escape_character(charclass[0]) %>' && c <= '<%= Packcr.escape_character(charclass[2]) %>')) goto L<%= format("%04d", onfail) %>;
10
10
  <%- end -%>
11
11
  <%- if gen.location -%>
12
- pcc_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, 1);
12
+ packcr_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, 1);
13
13
  <%- end -%>
14
14
  ctx->position_offset++;
15
15
  <%- else -%>
16
16
  char c;
17
- if (pcc_refill_buffer(ctx, 1) < 1) goto L<%= format("%04d", onfail) %>;
17
+ if (packcr_refill_buffer(ctx, 1) < 1) goto L<%= format("%04d", onfail) %>;
18
18
  c = ctx->buffer.buf[ctx->position_offset];
19
19
  <%- if a -%>
20
20
  if (
@@ -40,7 +40,7 @@
40
40
  )) goto L<%= format("%04d", onfail) %>;
41
41
  <%- end -%>
42
42
  <%- if gen.location -%>
43
- pcc_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, 1);
43
+ packcr_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, 1);
44
44
  <%- end -%>
45
45
  ctx->position_offset++;
46
46
  <%- end -%>
@@ -1,5 +1,5 @@
1
- if (pcc_refill_buffer(ctx, 1) < 1) goto L<%= format("%04d", onfail) %>;
1
+ if (packcr_refill_buffer(ctx, 1) < 1) goto L<%= format("%04d", onfail) %>;
2
2
  <%- if gen.location -%>
3
- pcc_location_forward(&ctx->position_offset, ctx->buffer.buf + ctx->position_offset, 1);
3
+ packcr_location_forward(&ctx->position_offset, ctx->buffer.buf + ctx->position_offset, 1);
4
4
  <%- end -%>
5
5
  ctx->position_offset++;
@@ -1,19 +1,19 @@
1
1
  <%- if a -%>
2
2
  if (
3
- pcc_refill_buffer(ctx, 1) < 1 ||
3
+ packcr_refill_buffer(ctx, 1) < 1 ||
4
4
  ctx->buffer.buf[ctx->position_offset] == '<%= Packcr.escape_character(charclass[i]) %>'
5
5
  ) goto L<%= format("%04d", onfail) %>;
6
6
  <%- if gen.location -%>
7
- pcc_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, 1);
7
+ packcr_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, 1);
8
8
  <%- end -%>
9
9
  ctx->position_offset++;
10
10
  <%- else -%>
11
11
  if (
12
- pcc_refill_buffer(ctx, 1) < 1 ||
12
+ packcr_refill_buffer(ctx, 1) < 1 ||
13
13
  ctx->buffer.buf[ctx->position_offset] != '<%= Packcr.escape_character(charclass[0]) %>'
14
14
  ) goto L<%= format("%04d", onfail) %>;
15
15
  <%- if gen.location -%>
16
- pcc_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, 1);
16
+ packcr_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, 1);
17
17
  <%- end -%>
18
18
  ctx->position_offset++;
19
19
  <%- end -%>
@@ -2,7 +2,7 @@
2
2
  <%- i = a ? 1 : 0 -%>
3
3
  {
4
4
  int u;
5
- const size_t n = pcc_get_char_as_utf32(ctx, &u);
5
+ const size_t n = packcr_get_char_as_utf32(ctx, &u);
6
6
  if (n == 0) goto L<%= format("%04d", onfail) %>;
7
7
  <%- if charclass && !(a && n == 1) # not '.' or '[^]' -%>
8
8
  <%- u0 = 0 -%>
@@ -43,7 +43,7 @@
43
43
  <%- end -%>
44
44
  <%- end -%>
45
45
  <%- if gen.location -%>
46
- pcc_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, n);
46
+ packcr_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, n);
47
47
  <%- end -%>
48
48
  ctx->position_offset += n;
49
49
  }
@@ -0,0 +1,41 @@
1
+ <%- a = charclass && charclass[0] == '^' -%>
2
+ <%- i = a ? 1 : 0 -%>
3
+ <%- any_code = !charclass || (a && n == 1) -%>
4
+ let (<%= any_code ? "_" : "" %>u, n) = self.get_char_as_utf32();
5
+ if n == 0 {
6
+ break 'L<%= format("%04d", onfail) %>;
7
+ }
8
+ <%- unless any_code -%>
9
+ if <% if !a -%>
10
+ <%- %>!(<% end -%>
11
+ <%- while i < n -%>
12
+ <%- if charclass[i] == '\\' && i + 1 < n -%>
13
+ <%- i += 1 -%>
14
+ <%- end -%>
15
+ <%- u = charclass[i].ord -%>
16
+ <%- i += 1 -%>
17
+ <%- if r -%>
18
+ <%- # character range -%>
19
+ <%- %>(0x<%= format("%06x", u0) %>..=0x<%= format("%06x", u) %>).contains(&u)<% -%>
20
+ <%- if i < n %> || <% end -%>
21
+ <%- u0 = 0 -%>
22
+ <%- r = false -%>
23
+ <%- elsif charclass[i] != "-" || i == n - 1 # the individual '-' character is valid when it is at the first or the last position -%>
24
+ <%- # single character -%>
25
+ <%- %>u == 0x<%= format("%06x", u) %><% if i < n %> || <% end -%>
26
+ <%- u0 = 0 -%>
27
+ <%- r = false -%>
28
+ <%- elsif charclass[i] == "-" -%>
29
+ <%- i += 1 -%>
30
+ <%- u0 = u -%>
31
+ <%- r = true -%>
32
+ <%- else -%>
33
+ <%- raise "unexpected charclass #{charclass[i]}" -%>
34
+ <%- end -%>
35
+ <%- end -%>
36
+ <%- if !a -%>) <% end -%>
37
+ <%- %>{
38
+ break 'L<%= format("%04d", onfail) %>;
39
+ }
40
+ <%- end -%>
41
+ self.input.position_offset += n;
@@ -1 +1 @@
1
- if (pcc_refill_buffer(ctx, 1) >= 1) goto L<%= format("%04d", onfail) %>;
1
+ if (packcr_refill_buffer(ctx, 1) >= 1) goto L<%= format("%04d", onfail) %>;
@@ -5,8 +5,8 @@
5
5
  goto L<%= format("%04d", m) %>;
6
6
  L<%= format("%04d", l) %>:;
7
7
  {
8
- pcc_value_t null;
9
- 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 %>);
8
+ packcr_value_t null;
9
+ 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 %>);
10
10
  <%- vars.each do |var| -%>
11
11
  thunk->data.leaf.values.buf[<%= var.index %>] = &(chunk->values.buf[<%= var.index %>]);
12
12
  <%- end -%>
@@ -19,9 +19,9 @@ L<%= format("%04d", l) %>:;
19
19
  thunk->data.leaf.capt0.range.start_loc = chunk->pos_loc;
20
20
  thunk->data.leaf.capt0.range.end_loc = ctx->position_offset_loc;
21
21
  <%- end -%>
22
- memset(&null, 0, sizeof(pcc_value_t)); /* in case */
22
+ memset(&null, 0, sizeof(packcr_value_t)); /* in case */
23
23
  thunk->data.leaf.action(ctx, thunk, &null);
24
- pcc_thunk__destroy(ctx->auxil, thunk);
24
+ packcr_thunk__destroy(ctx->auxil, thunk);
25
25
  }
26
26
  goto L<%= format("%04d", onfail) %>;
27
27
  L<%= format("%04d", m) %>:;
@@ -1,6 +1,6 @@
1
1
  {
2
2
  const size_t n = chunk->capts.buf[<%= index %>].range.end - chunk->capts.buf[<%= index %>].range.start;
3
- if (pcc_refill_buffer(ctx, n) < n) goto L<%= format("%04d", onfail) %>;
3
+ if (packcr_refill_buffer(ctx, n) < n) goto L<%= format("%04d", onfail) %>;
4
4
  if (n > 0) {
5
5
  const char *const p = ctx->buffer.buf + ctx->position_offset;
6
6
  const char *const q = ctx->buffer.buf + chunk->capts.buf[<%= index %>].range.start;
@@ -9,7 +9,7 @@
9
9
  if (p[i] != q[i]) goto L<%= format("%04d", onfail) %>;
10
10
  }
11
11
  <%- if gen.location -%>
12
- pcc_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, n);
12
+ packcr_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, n);
13
13
  <%- end -%>
14
14
  ctx->position_offset += n;
15
15
  }
@@ -4,7 +4,7 @@
4
4
  {
5
5
  const size_t p = ctx->position_offset;
6
6
  <%- if gen.location -%>
7
- const pcc_location_t p_loc = ctx->position_offset_loc;
7
+ const packcr_location_t p_loc = ctx->position_offset_loc;
8
8
  <%- end -%>
9
9
  <%= gen.generate_code(expr, l, 4, false) -%>
10
10
  <%- if r != Packcr::CODE_REACH__ALWAYS_FAIL -%>
@@ -3,7 +3,7 @@
3
3
  {
4
4
  const size_t p = ctx->position_offset;
5
5
  <%- if gen.location -%>
6
- const pcc_location_t p_loc = ctx->position_offset_loc;
6
+ const packcr_location_t p_loc = ctx->position_offset_loc;
7
7
  <%- end -%>
8
8
  <%= gen.generate_code(expr, l, 4, false) -%>
9
9
  <%- if r != Packcr::CODE_REACH__ALWAYS_FAIL -%>
@@ -0,0 +1,24 @@
1
+ <%- l = gen.next_label -%>
2
+ <%- r = expr.reachability -%>
3
+ let p = self.input.position_offset;
4
+ <%- if gen.location -%>
5
+ TODO
6
+ <%- end -%>
7
+ 'L<%= format("%04d", l) %>: {
8
+ {
9
+ <%- %><%= gen.generate_code(expr, l, 4, false) -%>
10
+ }
11
+ <%- if r != Packcr::CODE_REACH__ALWAYS_FAIL -%>
12
+ self.input.position_offset = p;
13
+ <%- if gen.location -%>
14
+ TODO
15
+ <%- end -%>
16
+ break 'L<%= format("%04d", onfail) %>;
17
+ <%- end -%>
18
+ }
19
+ <%- if r != Packcr::CODE_REACH__ALWAYS_SUCCEED -%>
20
+ self.input.position_offset = p;
21
+ <%- if gen.location -%>
22
+ TODO
23
+ <%- end -%>
24
+ <%- end -%>
@@ -1,8 +1,9 @@
1
1
  {
2
+ <%- r = expr.reachability -%>
2
3
  <%- if min > 0 -%>
3
4
  const size_t p0 = ctx->position_offset;
4
5
  <%- if gen.location -%>
5
- const pcc_location_t p0_loc = ctx->position_offset_loc;
6
+ const packcr_location_t p0_loc = ctx->position_offset_loc;
6
7
  <%- end -%>
7
8
  const size_t n0 = chunk->thunks.len;
8
9
  <%- end -%>
@@ -13,12 +14,13 @@
13
14
  for (i = 0; i < <%= max %>; i++) {
14
15
  <%- end -%>
15
16
  const size_t p = ctx->position_offset;
16
- <%- if gen.location -%>
17
- const pcc_location_t p_loc = ctx->position_offset_loc;
18
- <%- end -%>
17
+ <%- if r != Packcr::CODE_REACH__ALWAYS_SUCCEED -%>
18
+ <%- if gen.location -%>
19
+ const packcr_location_t p_loc = ctx->position_offset_loc;
20
+ <%- end -%>
19
21
  const size_t n = chunk->thunks.len;
22
+ <%- end -%>
20
23
  <%- l = gen.next_label -%>
21
- <%- r = expr.reachability -%>
22
24
  <%= gen.generate_code(expr, l, 8, false) -%>
23
25
  if (ctx->position_offset == p) break;
24
26
  <%- if r != Packcr::CODE_REACH__ALWAYS_SUCCEED -%>
@@ -28,7 +30,7 @@
28
30
  <%- if gen.location -%>
29
31
  ctx->position_offset_loc = p_loc;
30
32
  <%- end -%>
31
- pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
33
+ packcr_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
32
34
  break;
33
35
  <%- end -%>
34
36
  }
@@ -38,7 +40,7 @@
38
40
  <%- if gen.location -%>
39
41
  ctx->position_offset_loc = p0_loc;
40
42
  <%- end -%>
41
- pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n0);
43
+ packcr_thunk_array__revert(ctx->auxil, &chunk->thunks, n0);
42
44
  goto L<%= format("%04d", onfail) %>;
43
45
  }
44
46
  <%- end -%>
@@ -0,0 +1,50 @@
1
+ <%- if min > 0 -%>
2
+ let p0 = self.input.position_offset;
3
+ <%- if gen.location -%>
4
+ TODO
5
+ <%- end -%>
6
+ <%- end -%>
7
+ <%- use_count = max >= 0 || min > 0 -%>
8
+ <%- if use_count -%>
9
+ let mut i = -1;
10
+ <%- end -%>
11
+ <%- m = gen.next_label -%>
12
+ 'L<%= format("%04d", m) %>: loop {
13
+ <%- if use_count -%>
14
+ i += 1;
15
+ <%- end -%>
16
+ <%- if max >= 0 -%>
17
+ if i >= <%= max %> { break; }
18
+ <%- end -%>
19
+ let p = self.input.position_offset;
20
+ <%- if r != Packcr::CODE_REACH__ALWAYS_SUCCEED -%>
21
+ <%- if gen.location -%>
22
+ TODO
23
+ <%- end -%>
24
+ <%- end -%>
25
+ <%- l = gen.next_label -%>
26
+ <%- r = expr.reachability -%>
27
+ 'L<%= format("%04d", l) %>: {
28
+ <%- %><%= gen.generate_code(expr, l, 8, false) -%>
29
+ if self.input.position_offset == p {
30
+ break 'L<%= format("%04d", m) %>;
31
+ }
32
+ <%- if r != Packcr::CODE_REACH__ALWAYS_SUCCEED -%>
33
+ continue 'L<%= format("%04d", m) %>;
34
+ }
35
+ self.input.position_offset = p;
36
+ <%- if gen.location -%>
37
+ TODO
38
+ <%- end -%>
39
+ break 'L<%= format("%04d", m) %>;
40
+ <%- end -%>
41
+ }
42
+ <%- if min > 0 -%>
43
+ if i < <%= min %> {
44
+ self.input.position_offset = p0;
45
+ <%- if gen.location -%>
46
+ TODO
47
+ <%- end -%>
48
+ break 'L<%= format("%04d", onfail) %>;
49
+ }
50
+ <%- end -%>
@@ -1,13 +1,15 @@
1
+ <%- r = expr.reachability -%>
2
+ <%- if r == Packcr::CODE_REACH__ALWAYS_SUCCEED -%>
3
+ <%= gen.generate_code(expr, nil, 0, true) -%>
4
+ <%- else -%>
1
5
  {
2
6
  <%- l = gen.next_label -%>
3
7
  const size_t p = ctx->position_offset;
4
- <%- if gen.location -%>
5
- const pcc_location_t p_loc = ctx->position_offset_loc;
6
- <%- end -%>
8
+ <%- if gen.location -%>
9
+ const packcr_location_t p_loc = ctx->position_offset_loc;
10
+ <%- end -%>
7
11
  const size_t n = chunk->thunks.len;
8
- <%- r = expr.reachability -%>
9
12
  <%= gen.generate_code(expr, l, 4, false) -%>
10
- <%- if r != Packcr::CODE_REACH__ALWAYS_SUCCEED -%>
11
13
  <%- m = gen.next_label -%>
12
14
  goto L<%= format("%04d", m) %>;
13
15
  L<%= format("%04d", l) %>:;
@@ -15,7 +17,7 @@ L<%= format("%04d", l) %>:;
15
17
  ctx->position_offset_loc = p_loc;
16
18
  <%- end -%>
17
19
  ctx->position_offset = p;
18
- pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
20
+ packcr_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
19
21
  L<%= format("%04d", m) %>:;
20
- <%- end -%>
21
22
  }
23
+ <%- end -%>
@@ -1,13 +1,13 @@
1
+ <%- r = expr.reachability -%>
2
+ <%- if r == Packcr::CODE_REACH__ALWAYS_SUCCEED -%>
3
+ <%- %><%= gen.generate_code(expr, nil, 0, true) -%>
4
+ <%- else -%>
1
5
  <%- l = gen.next_label -%>
2
6
  pos<%= gen.level %> = @position_offset
3
7
  <%- if gen.location -%>
4
8
  p_loc<%= gen.level %> = @position_offset_loc
5
9
  <%- end -%>
6
10
  n<%= gen.level %> = answer.thunks.length
7
- <%- r = expr.reachability -%>
8
- <%- if r == Packcr::CODE_REACH__ALWAYS_SUCCEED -%>
9
- <%- %><%= gen.generate_code(expr, l, 0, false) -%>
10
- <%- else -%>
11
11
  <%- m = gen.next_label -%>
12
12
  catch(<%= m %>) do
13
13
  catch(<%= l %>) do
@@ -1,17 +1,17 @@
1
1
  <%- if index != nil -%>
2
2
  {
3
- pcc_rule_set_t *l = NULL;
4
- if (limits && ctx->position_offset == offset && pcc_rule_set__index(ctx->auxil, limits, pcc_evaluate_rule_<%= name %>) == PCC_VOID_VALUE) {
3
+ packcr_rule_set_t *l = NULL;
4
+ if (limits && ctx->position_offset == offset && packcr_rule_set__index(ctx->auxil, limits, packcr_evaluate_rule_<%= name %>) == PACKCR_VOID_VALUE) {
5
5
  l = limits;
6
6
  }
7
- if (!pcc_apply_rule(ctx, pcc_evaluate_rule_<%= name %>, &chunk->thunks, &(chunk->values.buf[<%= index %>]), offset<% if gen.location %>, offset_loc<% end %>, l)) goto L<%= format("%04d", onfail) %>;
7
+ if (!packcr_apply_rule(ctx, packcr_evaluate_rule_<%= name %>, &chunk->thunks, &(chunk->values.buf[<%= index %>]), offset<% if gen.location %>, offset_loc<% end %>, l)) goto L<%= format("%04d", onfail) %>;
8
8
  }
9
9
  <%- else -%>
10
10
  {
11
- pcc_rule_set_t *l = NULL;
12
- if (limits && ctx->position_offset == offset && pcc_rule_set__index(ctx->auxil, limits, pcc_evaluate_rule_<%= name %>) == PCC_VOID_VALUE) {
11
+ packcr_rule_set_t *l = NULL;
12
+ if (limits && ctx->position_offset == offset && packcr_rule_set__index(ctx->auxil, limits, packcr_evaluate_rule_<%= name %>) == PACKCR_VOID_VALUE) {
13
13
  l = limits;
14
14
  }
15
- if (!pcc_apply_rule(ctx, pcc_evaluate_rule_<%= name %>, &chunk->thunks, NULL, offset<% if gen.location %>, offset_loc<% end %>, l)) goto L<%= format("%04d", onfail) %>;
15
+ if (!packcr_apply_rule(ctx, packcr_evaluate_rule_<%= name %>, &chunk->thunks, NULL, offset<% if gen.location %>, offset_loc<% end %>, l)) goto L<%= format("%04d", onfail) %>;
16
16
  }
17
17
  <%- end -%>
@@ -0,0 +1,3 @@
1
+ if !self.apply_rule(Rule::<%= Packcr.camelize(name) %>, &mut answer, <%= index || 0 %>, offset, limits.clone()) {
2
+ break 'L<%= format("%04d", onfail) %>;
3
+ }
@@ -1,26 +1,26 @@
1
- static pcc_thunk_chunk_t *pcc_evaluate_rule_<%= name %>(pcc_context_t *ctx, size_t offset<% if gen.location %>, pcc_location_t offset_loc<% end %>, pcc_rule_set_t *limits) {
2
- pcc_thunk_chunk_t *const chunk = pcc_thunk_chunk__create(ctx);
1
+ static packcr_thunk_chunk_t *packcr_evaluate_rule_<%= name %>(packcr_context_t *ctx, size_t offset<% if gen.location %>, packcr_location_t offset_loc<% end %>, packcr_rule_set_t *limits) {
2
+ packcr_thunk_chunk_t *const chunk = packcr_thunk_chunk__create(ctx);
3
3
  chunk->pos = ctx->position_offset;
4
4
  <%- if gen.location -%>
5
5
  chunk->pos_loc = ctx->position_offset_loc;
6
6
  <%- end -%>
7
- PCC_DEBUG(ctx->auxil, PCC_DBG_EVALUATE, "<%= name %>", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
7
+ PACKCR_DEBUG(ctx->auxil, PACKCR_DBG_EVALUATE, "<%= name %>", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
8
8
  ctx->level++;
9
- pcc_value_table__resize(ctx->auxil, &chunk->values, <%= vars.length %>);
10
- pcc_capture_table__resize(ctx->auxil, &chunk->capts, <%= capts.length %>);
9
+ packcr_value_table__resize(ctx->auxil, &chunk->values, <%= vars.length %>);
10
+ packcr_capture_table__resize(ctx->auxil, &chunk->capts, <%= capts.length %>);
11
11
  <%- if vars.length > 0 -%>
12
- pcc_value_table__clear(ctx->auxil, &chunk->values);
12
+ packcr_value_table__clear(ctx->auxil, &chunk->values);
13
13
  <%- end -%>
14
14
  <%- r = expr.reachability -%>
15
15
  <%- -%><%= gen.generate_code(expr, 0, 4, false) -%>
16
16
  ctx->level--;
17
- PCC_DEBUG(ctx->auxil, PCC_DBG_MATCH, "<%= name %>", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->position_offset - chunk->pos));
17
+ PACKCR_DEBUG(ctx->auxil, PACKCR_DBG_MATCH, "<%= name %>", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->position_offset - chunk->pos));
18
18
  return chunk;
19
19
  <%- if r != Packcr::CODE_REACH__ALWAYS_SUCCEED -%>
20
20
  L0000:;
21
21
  ctx->level--;
22
- PCC_DEBUG(ctx->auxil, PCC_DBG_NOMATCH, "<%= name %>", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->position_offset - chunk->pos));
23
- pcc_thunk_chunk__destroy(ctx, chunk);
22
+ PACKCR_DEBUG(ctx->auxil, PACKCR_DBG_NOMATCH, "<%= name %>", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->position_offset - chunk->pos));
23
+ packcr_thunk_chunk__destroy(ctx, chunk);
24
24
  return NULL;
25
25
  <%- end -%>
26
26
  }
@@ -0,0 +1,34 @@
1
+ <%- for_ref = has_ref ? "" : "_" -%>
2
+ #[allow(non_snake_case)]
3
+ fn evaluate_rule_<%= name %>(
4
+ &mut self,
5
+ <%= for_ref %>offset: usize,
6
+ <%- if gen.location -%>
7
+ TODO
8
+ <%- end -%>
9
+ <%= for_ref %>limits: Option<RuleSet>,
10
+ ) -> Option<ThunkChunk> {
11
+ let mut answer = ThunkChunk::new(self.input.position_offset);
12
+ <%- if gen.location -%>
13
+ TODO
14
+ <%- end -%>
15
+ self.level += 1;
16
+ answer.capts.resize(<%= capts.length %>);
17
+ <%- if vars.length > 0 -%>
18
+ answer.values.clear();
19
+ <%- end -%>
20
+ <%- r = expr.reachability -%>
21
+ <%- if r == Packcr::CODE_REACH__ALWAYS_SUCCEED -%>
22
+ <%- -%><%= gen.generate_code(expr, 0, 4, false) -%>
23
+ self.level -= 1;
24
+ return Some(answer);
25
+ <%- else -%>
26
+ 'L0000: {
27
+ <%- -%><%= gen.generate_code(expr, 0, 8, false) -%>
28
+ self.level -= 1;
29
+ return Some(answer);
30
+ }
31
+ self.level -= 1;
32
+ return None;
33
+ <%- end -%>
34
+ }
@@ -0,0 +1,12 @@
1
+ <%- if @cut && oncut -%>
2
+ <%- onfail, oncut = oncut, nil -%>
3
+ <%- end -%>
4
+ <%- nodes.each_with_index do |expr, i| -%>
5
+ <%- -%><%= gen.generate_code(expr, onfail, 0, false, oncut: oncut) -%>
6
+ <%- if expr.reachability == Packcr::CODE_REACH__ALWAYS_FAIL -%>
7
+ <%- if i < nodes.length - 1 -%>
8
+ /* unreachable codes omitted */
9
+ <%- end -%>
10
+ <%- break -%>
11
+ <%- end -%>
12
+ <%- end -%>
@@ -1,11 +1,11 @@
1
1
  if (
2
- pcc_refill_buffer(ctx, <%= n %>) < <%= n %> ||
2
+ packcr_refill_buffer(ctx, <%= n %>) < <%= n %> ||
3
3
  <%- (n - 1).times do |i| -%>
4
4
  (ctx->buffer.buf + ctx->position_offset)[<%= i %>] != '<%= Packcr.escape_character(value[i]) %>' ||
5
5
  <%- end -%>
6
6
  (ctx->buffer.buf + ctx->position_offset)[<%= n - 1 %>] != '<%= Packcr.escape_character(value[n - 1]) %>'
7
7
  ) goto L<%= format("%04d", onfail) %>;
8
8
  <%- if gen.location -%>
9
- pcc_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, n);
9
+ packcr_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, <%= n %>);
10
10
  <%- end -%>
11
11
  ctx->position_offset += <%= n %>;
@@ -0,0 +1,10 @@
1
+ if self.refill_buffer(<%= n %>) < <%= n %>
2
+ || self.input.buffer.as_bytes()[self.input.position_offset..(self.input.position_offset + <%= n %>)] != <% -%>
3
+ <%- %>[<%= value[0, n].each_char.map { |c| "b'#{Packcr.escape_character(c)}'" }.join(", ") %>]
4
+ {
5
+ break 'L<%= format("%04d", onfail) %>;
6
+ }
7
+ <%- if gen.location -%>
8
+ TODO
9
+ <%- end -%>
10
+ self.input.position_offset += <%= n %>;
@@ -1,8 +1,8 @@
1
1
  if (
2
- pcc_refill_buffer(ctx, 1) < 1 ||
2
+ packcr_refill_buffer(ctx, 1) < 1 ||
3
3
  ctx->buffer.buf[ctx->position_offset] != '<%= Packcr.escape_character(value[0]) %>'
4
4
  ) goto L<%= format("%04d", onfail) %>;
5
5
  <%- if gen.location -%>
6
- pcc_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, 1);
6
+ packcr_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, 1);
7
7
  <%- end -%>
8
8
  ctx->position_offset++;
@@ -0,0 +1,9 @@
1
+ if self.refill_buffer(1) < 1
2
+ || self.input.buffer.as_bytes()[self.input.position_offset] != b'<%= Packcr.escape_character(value[0]) %>'
3
+ {
4
+ break 'L<%= format("%04d", onfail) %>;
5
+ }
6
+ <%- if gen.location -%>
7
+ TODO
8
+ <%- end -%>
9
+ self.input.position_offset += 1;
data/lib/packcr/util.rb CHANGED
@@ -5,8 +5,8 @@ class Packcr
5
5
  def unescape_string(str, is_charclass)
6
6
  if is_charclass
7
7
  str.gsub!("\\" * 2) { "\\" * 4 }
8
- str.gsub!("\"") { "\\\"" }
9
8
  end
9
+ str.gsub!("\"") { "\\\"" }
10
10
  str.gsub!(/\\(.)/) do
11
11
  c = ::Regexp.last_match(1)
12
12
  case c
@@ -88,6 +88,15 @@ class Packcr
88
88
  result.gsub!(/^( *?) {0,4}<<<</) { ::Regexp.last_match(1) }
89
89
  result
90
90
  end
91
+
92
+ def escape_varriables(code, lang)
93
+ code = code.gsub("$$", "*out") if lang == :rs
94
+ code.gsub("$", lang == :rb ? "__" : "_")
95
+ end
96
+
97
+ def camelize(string)
98
+ string.sub(/^_/, "Underscore").split(/[^a-zA-Z0-9]+/).map(&:capitalize).join
99
+ end
91
100
  end
92
101
 
93
102
  extend Util
@@ -1,3 +1,3 @@
1
1
  class Packcr
2
- VERSION = "0.0.7".freeze
2
+ VERSION = "0.1.0".freeze
3
3
  end