packcr 0.0.4 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) 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 +25 -0
  5. data/lib/packcr/code_block.rb +2 -3
  6. data/lib/packcr/context.rb +59 -56
  7. data/lib/packcr/generator.rb +13 -11
  8. data/lib/packcr/node/action_node.rb +14 -5
  9. data/lib/packcr/node/alternate_node.rb +20 -12
  10. data/lib/packcr/node/capture_node.rb +11 -11
  11. data/lib/packcr/node/charclass_node.rb +37 -13
  12. data/lib/packcr/node/eof_node.rb +23 -0
  13. data/lib/packcr/node/error_node.rb +17 -12
  14. data/lib/packcr/node/expand_node.rb +11 -5
  15. data/lib/packcr/node/predicate_node.rb +15 -24
  16. data/lib/packcr/node/quantity_node.rb +23 -18
  17. data/lib/packcr/node/reference_node.rb +21 -5
  18. data/lib/packcr/node/root_node.rb +60 -0
  19. data/lib/packcr/node/rule_node.rb +26 -3
  20. data/lib/packcr/node/sequence_node.rb +46 -31
  21. data/lib/packcr/node/string_node.rb +12 -12
  22. data/lib/packcr/node.rb +35 -4
  23. data/lib/packcr/parser.rb +2939 -2661
  24. data/lib/packcr/stream.rb +8 -13
  25. data/lib/packcr/templates/context/header.c.erb +29 -0
  26. data/lib/packcr/templates/context/source.c.erb +1292 -0
  27. data/lib/packcr/templates/context/source.rb.erb +406 -0
  28. data/lib/packcr/templates/node/action.c.erb +16 -0
  29. data/lib/packcr/templates/node/action.rb.erb +21 -0
  30. data/lib/packcr/templates/node/alternate.c.erb +34 -0
  31. data/lib/packcr/templates/node/alternate.rb.erb +40 -0
  32. data/lib/packcr/templates/node/capture.c.erb +17 -0
  33. data/lib/packcr/templates/node/capture.rb.erb +14 -0
  34. data/lib/packcr/templates/node/charclass.c.erb +47 -0
  35. data/lib/packcr/templates/node/charclass.rb.erb +49 -0
  36. data/lib/packcr/templates/node/charclass_any.c.erb +5 -0
  37. data/lib/packcr/templates/node/charclass_any.rb.erb +7 -0
  38. data/lib/packcr/templates/node/charclass_fail.c.erb +1 -0
  39. data/lib/packcr/templates/node/charclass_fail.rb.erb +1 -0
  40. data/lib/packcr/templates/node/charclass_one.c.erb +19 -0
  41. data/lib/packcr/templates/node/charclass_one.rb.erb +23 -0
  42. data/lib/packcr/templates/node/charclass_utf8.c.erb +49 -0
  43. data/lib/packcr/templates/node/charclass_utf8.rb.erb +50 -0
  44. data/lib/packcr/templates/node/charclass_utf8_reverse.rb.erb +51 -0
  45. data/lib/packcr/templates/node/eof.c.erb +1 -0
  46. data/lib/packcr/templates/node/eof.rb.erb +3 -0
  47. data/lib/packcr/templates/node/error.c.erb +28 -0
  48. data/lib/packcr/templates/node/error.rb.erb +34 -0
  49. data/lib/packcr/templates/node/expand.c.erb +16 -0
  50. data/lib/packcr/templates/node/expand.rb.erb +16 -0
  51. data/lib/packcr/templates/node/predicate.c.erb +30 -0
  52. data/lib/packcr/templates/node/predicate.rb.erb +28 -0
  53. data/lib/packcr/templates/node/predicate_neg.c.erb +23 -0
  54. data/lib/packcr/templates/node/predicate_neg.rb.erb +22 -0
  55. data/lib/packcr/templates/node/quantify_many.c.erb +45 -0
  56. data/lib/packcr/templates/node/quantify_many.rb.erb +47 -0
  57. data/lib/packcr/templates/node/quantify_one.c.erb +21 -0
  58. data/lib/packcr/templates/node/quantify_one.rb.erb +23 -0
  59. data/lib/packcr/templates/node/reference.c.erb +5 -0
  60. data/lib/packcr/templates/node/reference.rb.erb +9 -0
  61. data/lib/packcr/templates/node/reference_reverse.rb.erb +9 -0
  62. data/lib/packcr/templates/node/rule.c.erb +19 -0
  63. data/lib/packcr/templates/node/rule.rb.erb +23 -0
  64. data/lib/packcr/templates/node/sequence.c.erb +12 -0
  65. data/lib/packcr/templates/node/sequence.rb.erb +12 -0
  66. data/lib/packcr/templates/node/string_many.c.erb +11 -0
  67. data/lib/packcr/templates/node/string_many.rb.erb +10 -0
  68. data/lib/packcr/templates/node/string_one.c.erb +8 -0
  69. data/lib/packcr/templates/node/string_one.rb.erb +10 -0
  70. data/lib/packcr/tokenizer.rb +2948 -0
  71. data/lib/packcr/util.rb +0 -8
  72. data/lib/packcr/version.rb +1 -1
  73. data/lib/packcr.rb +1 -2
  74. metadata +87 -8
  75. data/lib/packcr/buffer.rb +0 -47
@@ -0,0 +1,23 @@
1
+ <%- if a -%>
2
+ if (
3
+ refill_buffer(1) < 1 ||
4
+ @buffer[@cur] == "<%= Packcr.escape_character(charclass[0]) %>"
5
+ )
6
+ throw(<%= onfail %>)
7
+ end
8
+ <%- if gen.location -%>
9
+ @cur_loc = @cur_loc.forward(@buffer, @cur, 1)
10
+ <%- end -%>
11
+ @cur += 1
12
+ <%- else -%>
13
+ if (
14
+ refill_buffer(1) < 1 ||
15
+ @buffer[@cur] != "<%= Packcr.escape_character(charclass[0]) %>"
16
+ )
17
+ throw(<%= onfail %>)
18
+ end
19
+ <%- if gen.location -%>
20
+ @cur_loc = @cur_loc.forward(@buffer, @cur, 1)
21
+ <%- end -%>
22
+ @cur += 1
23
+ <%- end -%>
@@ -0,0 +1,49 @@
1
+ <%- a = charclass && charclass[0] == '^' -%>
2
+ <%- i = a ? 1 : 0 -%>
3
+ {
4
+ int u;
5
+ const size_t n = pcc_get_char_as_utf32(ctx, &u);
6
+ if (n == 0) goto L<%= "%04d" % onfail %>;
7
+ <%- if charclass && !(a && n == 1) # not '.' or '[^]' -%>
8
+ <%- u0 = 0 -%>
9
+ <%- r = false -%>
10
+ <%- if a -%>
11
+ if (
12
+ <%- else -%>
13
+ if (!(
14
+ <%- end -%>
15
+ <%- while i < n -%>
16
+ <%- if charclass[i] == '\\' && i + 1 < n -%>
17
+ <%- i += 1 -%>
18
+ <%- end -%>
19
+ <%- u = charclass[i].ord -%>
20
+ <%- i += 1 -%>
21
+ <%- if r -%>
22
+ <%- # character range -%>
23
+ (u >= 0x<%= "%06x" % u0 %> && u <= 0x<%= "%06x" % u %>)<% if i < n %> ||<% end %>
24
+ <%- u0 = 0 -%>
25
+ <%- r = false -%>
26
+ <%- elsif charclass[i] != "-" || i == n - 1 # the individual '-' character is valid when it is at the first or the last position -%>
27
+ <%- # single character -%>
28
+ u == 0x<%= "%06x" % u %><% if i < n %> ||<% end %>
29
+ <%- u0 = 0 -%>
30
+ <%- r = false -%>
31
+ <%- elsif charclass[i] == "-" -%>
32
+ <%- i += 1 -%>
33
+ <%- u0 = u -%>
34
+ <%- r = true -%>
35
+ <%- else -%>
36
+ <%- raise "unexpected charclass #{charclass[i]}" -%>
37
+ <%- end -%>
38
+ <%- end -%>
39
+ <%- if a -%>
40
+ ) goto L<%= "%04d" % onfail %>;
41
+ <%- else -%>
42
+ )) goto L<%= "%04d" % onfail %>;
43
+ <%- end -%>
44
+ <%- end -%>
45
+ <%- if gen.location -%>
46
+ pcc_location_forward(&ctx->cur_loc, ctx->buffer.buf + ctx->cur, n);
47
+ <%- end -%>
48
+ ctx->cur += n;
49
+ }
@@ -0,0 +1,50 @@
1
+ <%- a = charclass && charclass[0] == '^' -%>
2
+ <%- i = a ? 1 : 0 -%>
3
+ if refill_buffer(1) < 1
4
+ throw(<%= onfail %>)
5
+ end
6
+ u<%= gen.level %> = @buffer[@cur]
7
+ <%- if charclass && !(a && n == 1) # not '.' or '[^]' -%>
8
+ <%- u0 = nil -%>
9
+ <%- r = false -%>
10
+ <%- if a -%>
11
+ if (
12
+ <%- else -%>
13
+ if (!(
14
+ <%- end -%>
15
+ <%- while i < n -%>
16
+ <%- if charclass[i] == '\\' && i + 1 < n -%>
17
+ <%- i += 1 -%>
18
+ <%- end -%>
19
+ <%- u = charclass[i] -%>
20
+ <%- i += 1 -%>
21
+ <%- if r -%>
22
+ <%- # character range -%>
23
+ (u<%= gen.level %> >= <%= u0.dump %> && u<%= gen.level %> <= <%= u.dump %>)<% if i < n %> ||<% end %>
24
+ <%- u0 = 0 -%>
25
+ <%- r = false -%>
26
+ <%- elsif charclass[i] != "-" || i == n - 1 # the individual '-' character is valid when it is at the first or the last position -%>
27
+ <%- # single character -%>
28
+ u<%= gen.level %> == <%= u.dump %><% if i < n %> ||<% end %>
29
+ <%- u0 = 0 -%>
30
+ <%- r = false -%>
31
+ <%- elsif charclass[i] == "-" -%>
32
+ <%- i += 1 -%>
33
+ <%- u0 = u -%>
34
+ <%- r = true -%>
35
+ <%- else -%>
36
+ <%- raise "unexpected charclass #{charclass[i]}" -%>
37
+ <%- end -%>
38
+ <%- end -%>
39
+ <%- if a -%>
40
+ )
41
+ <%- else -%>
42
+ ))
43
+ <%- end -%>
44
+ throw(<%= onfail %>)
45
+ end
46
+ <%- end -%>
47
+ <%- if gen.location -%>
48
+ @cur_loc = @cur_loc.forward(@buffer, @cur, 1)
49
+ <%- end -%>
50
+ @cur += 1
@@ -0,0 +1,51 @@
1
+ <%- a = charclass && charclass[0] == '^' -%>
2
+ <%- i = a ? 1 : 0 -%>
3
+ if refill_buffer(1) >= 1
4
+ u<%= gen.level %> = @buffer[@cur]
5
+ <%- if charclass && !(a && n == 1) # not '.' or '[^]' -%>
6
+ <%- u0 = nil -%>
7
+ <%- r = false -%>
8
+ <%- if a -%>
9
+ unless (
10
+ <%- else -%>
11
+ if (
12
+ <%- end -%>
13
+ <%- while i < n -%>
14
+ <%- if charclass[i] == '\\' && i + 1 < n -%>
15
+ <%- i += 1 -%>
16
+ <%- end -%>
17
+ <%- u = charclass[i] -%>
18
+ <%- i += 1 -%>
19
+ <%- if r -%>
20
+ <%- # character range -%>
21
+ (u<%= gen.level %> >= <%= u0.dump %> && u<%= gen.level %> <= <%= u.dump %>)<% if i < n %> ||<% end %>
22
+ <%- u0 = 0 -%>
23
+ <%- r = false -%>
24
+ <%- elsif charclass[i] != "-" || i == n - 1 # the individual '-' character is valid when it is at the first or the last position -%>
25
+ <%- # single character -%>
26
+ u<%= gen.level %> == <%= u.dump %><% if i < n %> ||<% end %>
27
+ <%- u0 = 0 -%>
28
+ <%- r = false -%>
29
+ <%- elsif charclass[i] == "-" -%>
30
+ <%- i += 1 -%>
31
+ <%- u0 = u -%>
32
+ <%- r = true -%>
33
+ <%- else -%>
34
+ <%- raise "unexpected charclass #{charclass[i]}" -%>
35
+ <%- end -%>
36
+ <%- end -%>
37
+ )
38
+ <%- if gen.location -%>
39
+ @cur_loc = @cur_loc.forward(@buffer, @cur, 1)
40
+ <%- end -%>
41
+ @cur += 1
42
+ throw(<%= onsuccess %>)
43
+ end
44
+ <%- else # '.' or '[^]' -%>
45
+ <%- if gen.location -%>
46
+ @cur_loc = @cur_loc.forward(@buffer, @cur, 1)
47
+ <%- end -%>
48
+ @cur += 1
49
+ throw(<%= onsuccess %>)
50
+ <%- end -%>
51
+ end
@@ -0,0 +1 @@
1
+ if (pcc_refill_buffer(ctx, 1) >= 1) goto L<%= "%04d" % onfail %>;
@@ -0,0 +1,3 @@
1
+ if refill_buffer(1) >= 1
2
+ throw(<%= onfail %>)
3
+ end
@@ -0,0 +1,28 @@
1
+ {
2
+ <%- l = gen.next_label -%>
3
+ <%- m = gen.next_label -%>
4
+ <%- -%><%= gen.generate_code(expr, l, 4, true) -%>
5
+ goto L<%= "%04d" % m %>;
6
+ L<%= "%04d" % l %>:;
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 %>);
10
+ <%- vars.each do |var| -%>
11
+ thunk->data.leaf.values.buf[<%= var.index %>] = &(chunk->values.buf[<%= var.index %>]);
12
+ <%- end -%>
13
+ <%- capts.each do |capt| -%>
14
+ thunk->data.leaf.capts.buf[<%= capt.index %>] = &(chunk->capts.buf[<%= capt.index %>]);
15
+ <%- end -%>
16
+ thunk->data.leaf.capt0.range.start = chunk->pos;
17
+ thunk->data.leaf.capt0.range.end = ctx->cur;
18
+ <%- if gen.location -%>
19
+ thunk->data.leaf.capt0.range.start_loc_ptr = &chunk->pos_loc;
20
+ thunk->data.leaf.capt0.range.end_loc_ptr = &ctx->cur_loc;
21
+ <%- end -%>
22
+ memset(&null, 0, sizeof(pcc_value_t)); /* in case */
23
+ thunk->data.leaf.action(ctx, thunk, &null);
24
+ pcc_thunk__destroy(ctx->auxil, thunk);
25
+ }
26
+ goto L<%= "%04d" % onfail %>;
27
+ L<%= "%04d" % m %>:;
28
+ }
@@ -0,0 +1,34 @@
1
+ <%- l = gen.next_label -%>
2
+ <%- m = gen.next_label -%>
3
+ catch(<%= m %>) do
4
+ catch(<%= l %>) do
5
+ <%- -%><%= gen.generate_code(expr, l, 4, false) -%>
6
+ throw(<%= m %>)
7
+ end
8
+
9
+ action_<%= gen.rule.name %>_<%= index %>(
10
+ ThunkLeaf.new(
11
+ :action_<%= gen.rule.name %>_<%= index %>,
12
+ Capture.new(
13
+ answer.pos, @cur,
14
+ <%- if gen.location -%>
15
+ answer.pos_loc,@cur_loc,
16
+ <%- end -%>
17
+ ),
18
+ <%- if vars.empty? -%>
19
+ {},
20
+ <%- else -%>
21
+ answer.values.slice(<% vars.each_with_index do |var, i| %><%= ", " if i > 0 %><%= var.index %><% end %>),
22
+ <%- end -%>
23
+ <%- if capts.empty? -%>
24
+ {},
25
+ <%- else -%>
26
+ answer.capts.slice(<% capts.each_with_index do |capt, i| %><%= ", " if i > 0 %><%= capt.index %><% end %>),
27
+ <%- end -%>
28
+ ),
29
+ nil,
30
+ 0
31
+ )
32
+
33
+ throw(<%= onfail %>)
34
+ end
@@ -0,0 +1,16 @@
1
+ {
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<%= "%04d" % onfail %>;
4
+ if (n > 0) {
5
+ const char *const p = ctx->buffer.buf + ctx->cur;
6
+ const char *const q = ctx->buffer.buf + chunk->capts.buf[<%= index %>].range.start;
7
+ size_t i;
8
+ for (i = 0; i < n; i++) {
9
+ if (p[i] != q[i]) goto L<%= "%04d" % onfail %>;
10
+ }
11
+ <%- if gen.location -%>
12
+ pcc_location_forward(&ctx->cur_loc, ctx->buffer.buf + ctx->cur, n);
13
+ <%- end -%>
14
+ ctx->cur += n;
15
+ }
16
+ }
@@ -0,0 +1,16 @@
1
+ capt<%= gen.level %> = answer.capts[<%= index %>]
2
+ n<%= gen.level %> = capt<%= gen.level %>.range_end - capt<%= gen.level %>.range_start
3
+ if refill_buffer(n<%= gen.level %>) < n<%= gen.level %>
4
+ throw(<%= onfail %>)
5
+ end
6
+ if n<%= gen.level %> > 0
7
+ ptr<%= gen.level %> = @buffer[@cur, n<%= gen.level %>]
8
+ q<%= gen.level %> = @buffer[capt<%= gen.level %>.range_start, n<%= gen.level %>]
9
+ if ptr<%= gen.level %> != q<%= gen.level %>
10
+ throw(<%= onfail %>)
11
+ end
12
+ <%- if gen.location -%>
13
+ @cur_loc = @cur_loc.forward(@buffer, @cur, n<%= gen.level %>)
14
+ <%- end -%>
15
+ @cur += n<%= gen.level %>
16
+ end
@@ -0,0 +1,30 @@
1
+ <%- l = gen.next_label -%>
2
+ <%- m = gen.next_label -%>
3
+ <%- r = expr.reachability -%>
4
+ {
5
+ const size_t p = ctx->cur;
6
+ <%- if gen.location -%>
7
+ const pcc_location_t p_loc = ctx->cur_loc;
8
+ <%- end -%>
9
+ <%= gen.generate_code(expr, l, 4, false) -%>
10
+ <%- if r != Packcr::CODE_REACH__ALWAYS_FAIL -%>
11
+ ctx->cur = p;
12
+ <%- if gen.location -%>
13
+ ctx->cur_loc = p_loc;
14
+ <%- end -%>
15
+ <%- end -%>
16
+ <%- if r == Packcr::CODE_REACH__BOTH -%>
17
+ goto L<%= "%04d" % m %>;
18
+ <%- end -%>
19
+ <%- if r != Packcr::CODE_REACH__ALWAYS_SUCCEED -%>
20
+ L<%= "%04d" % l %>:;
21
+ ctx->cur = p;
22
+ <%- if gen.location -%>
23
+ ctx->cur_loc = p_loc;
24
+ <%- end -%>
25
+ goto L<%= "%04d" % onfail %>;
26
+ <%- end -%>
27
+ <%- if r == Packcr::CODE_REACH__BOTH -%>
28
+ L<%= "%04d" % m %>:;
29
+ <%- end -%>
30
+ }
@@ -0,0 +1,28 @@
1
+ <%- l = gen.next_label -%>
2
+ <%- m = gen.next_label -%>
3
+ <%- r = expr.reachability -%>
4
+ catch(<%= m %>) do
5
+ pos<%= gen.level %> = @cur
6
+ <%- if gen.location -%>
7
+ p_loc<%= gen.level %> = @cur_pos
8
+ <%- end -%>
9
+ catch(<%= l %>) do
10
+ <%- %><%= gen.generate_code(expr, l, 4, false) -%>
11
+ <%- if r != Packcr::CODE_REACH__ALWAYS_FAIL -%>
12
+ @cur = pos<%= gen.level %>
13
+ <%- if gen.location -%>
14
+ @cur_loc = p_loc<%= gen.level %>
15
+ <%- end -%>
16
+ <%- end -%>
17
+ <%- if r == Packcr::CODE_REACH__BOTH -%>
18
+ throw(<%= m %>)
19
+ <%- end -%>
20
+ end
21
+ <%- if r != Packcr::CODE_REACH__ALWAYS_SUCCEED -%>
22
+ @cur = pos<%= gen.level %>
23
+ <%- if gen.location -%>
24
+ @cur_loc = p_loc<%= gen.level %>
25
+ <%- end -%>
26
+ throw(<%= onfail %>)
27
+ <%- end -%>
28
+ end
@@ -0,0 +1,23 @@
1
+ <%- l = gen.next_label -%>
2
+ <%- r = expr.reachability -%>
3
+ {
4
+ const size_t p = ctx->cur;
5
+ <%- if gen.location -%>
6
+ const pcc_location_t p_loc = ctx->cur_loc;
7
+ <%- end -%>
8
+ <%= gen.generate_code(expr, l, 4, false) -%>
9
+ <%- if r != Packcr::CODE_REACH__ALWAYS_FAIL -%>
10
+ ctx->cur = p;
11
+ <%- if gen.location -%>
12
+ ctx->cur_loc = p_loc;
13
+ <%- end -%>
14
+ goto L<%= "%04d" % onfail %>;
15
+ <%- end -%>
16
+ <%- if r != Packcr::CODE_REACH__ALWAYS_SUCCEED -%>
17
+ L<%= "%04d" % l %>:;
18
+ ctx->cur = p;
19
+ <%- if gen.location -%>
20
+ ctx->cur_loc = p_loc;
21
+ <%- end -%>
22
+ <%- end -%>
23
+ }
@@ -0,0 +1,22 @@
1
+ <%- l = gen.next_label -%>
2
+ <%- r = expr.reachability -%>
3
+ pos<%= gen.level %> = @cur
4
+ <%- if gen.location -%>
5
+ p_loc<%= gen.level %> = @cur_loc
6
+ <%- end -%>
7
+ catch(<%= l %>) do
8
+ <%- %><%= gen.generate_code(expr, l, 2, false) -%>
9
+ <%- if r != Packcr::CODE_REACH__ALWAYS_FAIL -%>
10
+ @cur = pos<%= gen.level %>
11
+ <%- if gen.location -%>
12
+ @cur_loc = p_loc<%= gen.level %>
13
+ <%- end -%>
14
+ throw(<%= onfail %>)
15
+ <%- end -%>
16
+ end
17
+ <%- if r != Packcr::CODE_REACH__ALWAYS_SUCCEED -%>
18
+ @cur = pos<%= gen.level %>
19
+ <%- if gen.location -%>
20
+ @cur_loc = p_loc<%= gen.level %>
21
+ <%- end -%>
22
+ <%- end -%>
@@ -0,0 +1,45 @@
1
+ {
2
+ <%- if min > 0 -%>
3
+ const size_t p0 = ctx->cur;
4
+ <%- if gen.location -%>
5
+ const pcc_location_t p0_loc = ctx->cur_loc;
6
+ <%- end -%>
7
+ const size_t n0 = chunk->thunks.len;
8
+ <%- end -%>
9
+ int i;
10
+ <%- if max < 0 -%>
11
+ for (i = 0;; i++) {
12
+ <%- else -%>
13
+ for (i = 0; i < <%= max %>; i++) {
14
+ <%- end -%>
15
+ const size_t p = ctx->cur;
16
+ <%- if gen.location -%>
17
+ const pcc_location_t p_loc = ctx->cur_loc;
18
+ <%- end -%>
19
+ const size_t n = chunk->thunks.len;
20
+ <%- l = gen.next_label -%>
21
+ <%- r = expr.reachability -%>
22
+ <%= gen.generate_code(expr, l, 8, false) -%>
23
+ if (ctx->cur == p) break;
24
+ <%- if r != Packcr::CODE_REACH__ALWAYS_SUCCEED -%>
25
+ continue;
26
+ L<%= "%04d" % l %>:;
27
+ ctx->cur = p;
28
+ <%- if gen.location -%>
29
+ ctx->cur_loc = p_loc;
30
+ <%- end -%>
31
+ pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
32
+ break;
33
+ <%- end -%>
34
+ }
35
+ <%- if min > 0 -%>
36
+ if (i < <%= min %>) {
37
+ ctx->cur = p0;
38
+ <%- if gen.location -%>
39
+ ctx->cur_loc = p0_loc;
40
+ <%- end -%>
41
+ pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n0);
42
+ goto L<%= "%04d" % onfail %>;
43
+ }
44
+ <%- end -%>
45
+ }
@@ -0,0 +1,47 @@
1
+ <%- if min > 0 -%>
2
+ q<%= gen.level %> = @cur
3
+ <%- if gen.location -%>
4
+ q_loc<%= gen.level %> = @cur_loc
5
+ <%- end -%>
6
+ m<%= gen.level %> = answer.thunks.length
7
+ <%- end -%>
8
+ i<%= gen.level %> = 0
9
+ pos<%= gen.level %> = nil
10
+ <%- if gen.location -%>
11
+ p_loc<%= gen.level %> = nil
12
+ <%- end -%>
13
+ n<%= gen.level %> = nil
14
+ <%- l = gen.next_label -%>
15
+ catch(<%= l %>) do
16
+ pos<%= gen.level %> = @cur
17
+ <%- if gen.location -%>
18
+ p_loc<%= gen.level %> = @cur_loc
19
+ <%- end -%>
20
+ n<%= gen.level %> = answer.thunks.length
21
+ <%- r = expr.reachability -%>
22
+ <%- %><%= gen.generate_code(expr, l, 2, false) -%>
23
+ i<%= gen.level %> += 1
24
+ if @cur != pos<%= gen.level %><% if max >= 0 %> || i<%= gen.level %> < <%= max %><% end %>
25
+ redo
26
+ end
27
+ pos<%= gen.level %> = nil
28
+ end
29
+ <%- if r != Packcr::CODE_REACH__ALWAYS_SUCCEED -%>
30
+ if pos<%= gen.level %>
31
+ @cur = pos<%= gen.level %>
32
+ <%- if gen.location -%>
33
+ @cur_loc = p_loc<%= gen.level %>
34
+ <%- end -%>
35
+ answer.thunks[n<%= gen.level %>..-1] = []
36
+ end
37
+ <%- end -%>
38
+ <%- if min > 0 -%>
39
+ if i<%= gen.level %> < <%= min %>
40
+ @cur = q<%= gen.level %>
41
+ <%- if gen.location -%>
42
+ @cur_loc = q_loc<%= gen.level %>
43
+ <%- end -%>
44
+ answer.thunks[m<%= gen.level %>..-1] = []
45
+ throw(<%= onfail %>)
46
+ end
47
+ <%- end -%>
@@ -0,0 +1,21 @@
1
+ {
2
+ <%- l = gen.next_label -%>
3
+ const size_t p = ctx->cur;
4
+ <%- if gen.location -%>
5
+ const pcc_location_t p_loc = ctx->cur_loc;
6
+ <%- end -%>
7
+ const size_t n = chunk->thunks.len;
8
+ <%- r = expr.reachability -%>
9
+ <%= gen.generate_code(expr, l, 4, false) -%>
10
+ <%- if r != Packcr::CODE_REACH__ALWAYS_SUCCEED -%>
11
+ <%- m = gen.next_label -%>
12
+ goto L<%= "%04d" % m %>;
13
+ L<%= "%04d" % l %>:;
14
+ <%- if gen.location -%>
15
+ ctx->cur_loc = p_loc;
16
+ <%- end -%>
17
+ ctx->cur = p;
18
+ pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
19
+ L<%= "%04d" % m %>:;
20
+ <%- end -%>
21
+ }
@@ -0,0 +1,23 @@
1
+ <%- l = gen.next_label -%>
2
+ pos<%= gen.level %> = @cur
3
+ <%- if gen.location -%>
4
+ p_loc<%= gen.level %> = @cur_loc
5
+ <%- end -%>
6
+ 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
+ <%- m = gen.next_label -%>
12
+ catch(<%= m %>) do
13
+ catch(<%= l %>) do
14
+ <%- %><%= gen.generate_code(expr, l, 4, false) -%>
15
+ throw(<%= m %>)
16
+ end
17
+ <%- if gen.location -%>
18
+ @cur_loc = p_loc<%= gen.level %>
19
+ <%- end -%>
20
+ @cur = pos<%= gen.level %>
21
+ answer.thunks[n<%= gen.level %>..-1] = []
22
+ end
23
+ <%- end -%>
@@ -0,0 +1,5 @@
1
+ <%- if index != nil -%>
2
+ if (!pcc_apply_rule(ctx, pcc_evaluate_rule_<%= name %>, &chunk->thunks, &(chunk->values.buf[<%= index %>]))) goto L<%= "%04d" % onfail %>;
3
+ <%- else -%>
4
+ if (!pcc_apply_rule(ctx, pcc_evaluate_rule_<%= name %>, &chunk->thunks, NULL)) goto L<%= "%04d" % onfail %>;
5
+ <%- end -%>
@@ -0,0 +1,9 @@
1
+ <%- if index != nil -%>
2
+ if !apply_rule(:evaluate_rule_<%= name %>, answer.thunks, answer.values, <%= index %>)
3
+ throw(<%= onfail %>)
4
+ end
5
+ <%- else -%>
6
+ if !apply_rule(:evaluate_rule_<%= name %>, answer.thunks, nil, 0)
7
+ throw(<%= onfail %>)
8
+ end
9
+ <%- end -%>
@@ -0,0 +1,9 @@
1
+ <%- if index != nil -%>
2
+ if apply_rule(:evaluate_rule_<%= name %>, answer.thunks, answer.values, <%= index %>)
3
+ throw(<%= onsuccess %>)
4
+ end
5
+ <%- else -%>
6
+ if apply_rule(:evaluate_rule_<%= name %>, answer.thunks, nil, 0)
7
+ throw(<%= onsuccess %>)
8
+ end
9
+ <%- end -%>
@@ -0,0 +1,19 @@
1
+ PCC_DEBUG(ctx->auxil, PCC_DBG_EVALUATE, "<%= name %>", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->buffer.len - chunk->pos));
2
+ ctx->level++;
3
+ pcc_value_table__resize(ctx->auxil, &chunk->values, <%= vars.length %>);
4
+ pcc_capture_table__resize(ctx->auxil, &chunk->capts, <%= capts.length %>);
5
+ <%- if vars.length > 0 -%>
6
+ pcc_value_table__clear(ctx->auxil, &chunk->values);
7
+ <%- end -%>
8
+ <%- r = expr.reachability -%>
9
+ <%- -%><%= gen.generate_code(expr, 0, 4, false) -%>
10
+ ctx->level--;
11
+ PCC_DEBUG(ctx->auxil, PCC_DBG_MATCH, "<%= name %>", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
12
+ return chunk;
13
+ <%- if r != Packcr::CODE_REACH__ALWAYS_SUCCEED -%>
14
+ L0000:;
15
+ ctx->level--;
16
+ PCC_DEBUG(ctx->auxil, PCC_DBG_NOMATCH, "<%= name %>", ctx->level, chunk->pos, (ctx->buffer.buf + chunk->pos), (ctx->cur - chunk->pos));
17
+ pcc_thunk_chunk__destroy(ctx, chunk);
18
+ return NULL;
19
+ <%- end -%>
@@ -0,0 +1,23 @@
1
+ debug { warn "#{ " " * @level}EVAL <%= name %> #{answer.pos} #{@buffer[answer.pos..-1].inspect}" }
2
+ @level += 1
3
+ answer.resize_captures(<%= capts.length %>)
4
+ <%- if vars.length > 0 -%>
5
+ answer.values = {}
6
+ <%- end -%>
7
+ <%- r = expr.reachability -%>
8
+ <%- if r == Packcr::CODE_REACH__ALWAYS_SUCCEED -%>
9
+ <%- -%><%= gen.generate_code(expr, 0, 0, false) -%>
10
+ @level -= 1
11
+ debug { warn "#{ " " * @level}MATCH <%= name %> #{answer.pos} #{@buffer[answer.pos...@cur].inspect}" }
12
+ return answer
13
+ <%- else -%>
14
+ catch(0) do
15
+ <%- -%><%= gen.generate_code(expr, 0, 2, false) -%>
16
+ @level -= 1
17
+ debug { warn "#{ " " * @level}MATCH <%= name %> #{answer.pos} #{@buffer[answer.pos...@cur].inspect}" }
18
+ return answer
19
+ end
20
+ @level -= 1
21
+ debug { warn "#{ " " * @level}NOMATCH <%= name %> #{answer.pos} #{@buffer[answer.pos...@cur].inspect}" }
22
+ return nil
23
+ <%- end -%>
@@ -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 + 1 < nodes.length -%>
8
+ /* unreachable codes omitted */
9
+ <%- end -%>
10
+ <%- break -%>
11
+ <%- end -%>
12
+ <%- end -%>
@@ -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 + 1 < nodes.length -%>
8
+ # unreachable codes omitted
9
+ <%- end -%>
10
+ <%- break -%>
11
+ <%- end -%>
12
+ <%- end -%>
@@ -0,0 +1,11 @@
1
+ if (
2
+ pcc_refill_buffer(ctx, <%= n %>) < <%= n %> ||
3
+ <%- (n - 1).times do |i| -%>
4
+ (ctx->buffer.buf + ctx->cur)[<%= i %>] != '<%= Packcr.escape_character(value[i]) %>' ||
5
+ <%- end -%>
6
+ (ctx->buffer.buf + ctx->cur)[<%= n - 1 %>] != '<%= s = Packcr.escape_character(value[n - 1]) %>'
7
+ ) goto L<%= "%04d" % onfail %>;
8
+ <%- if gen.location -%>
9
+ pcc_location_forward(&ctx->cur_loc, ctx->buffer.buf + ctx->cur, n);
10
+ <%- end -%>
11
+ ctx->cur += <%= n %>;
@@ -0,0 +1,10 @@
1
+ if (
2
+ refill_buffer(<%= n %>) < <%= n %> ||
3
+ @buffer[@cur, <%= n %>] != <%= value[0, n].dump %>
4
+ )
5
+ throw(<%= onfail %>)
6
+ end
7
+ <%- if gen.location -%>
8
+ @cur_loc = @cur_loc.forward(@buffer, @cur, <%= n %>)
9
+ <%- end -%>
10
+ @cur += <%= n %>
@@ -0,0 +1,8 @@
1
+ if (
2
+ pcc_refill_buffer(ctx, 1) < 1 ||
3
+ ctx->buffer.buf[ctx->cur] != '<%= Packcr.escape_character(value[0]) %>'
4
+ ) goto L<%= "%04d" % onfail %>;
5
+ <%- if gen.location -%>
6
+ pcc_location_forward(&ctx->cur_loc, ctx->buffer.buf + ctx->cur, 1);
7
+ <%- end -%>
8
+ ctx->cur++;