packcr 0.0.4 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) 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 +27 -0
  5. data/lib/packcr/code_block.rb +2 -4
  6. data/lib/packcr/context.rb +66 -61
  7. data/lib/packcr/generated/context.rb +440 -0
  8. data/lib/packcr/generated/node/action_node.rb +60 -0
  9. data/lib/packcr/generated/node/alternate_node.rb +98 -0
  10. data/lib/packcr/generated/node/capture_node.rb +39 -0
  11. data/lib/packcr/generated/node/charclass_node.rb +372 -0
  12. data/lib/packcr/generated/node/eof_node.rb +20 -0
  13. data/lib/packcr/generated/node/error_node.rb +67 -0
  14. data/lib/packcr/generated/node/expand_node.rb +30 -0
  15. data/lib/packcr/generated/node/predicate_node.rb +140 -0
  16. data/lib/packcr/generated/node/quantity_node.rb +167 -0
  17. data/lib/packcr/generated/node/reference_node.rb +70 -0
  18. data/lib/packcr/generated/node/rule_node.rb +63 -0
  19. data/lib/packcr/generated/node/sequence_node.rb +42 -0
  20. data/lib/packcr/generated/node/string_node.rb +60 -0
  21. data/lib/packcr/generator.rb +14 -11
  22. data/lib/packcr/node/action_node.rb +18 -7
  23. data/lib/packcr/node/alternate_node.rb +22 -12
  24. data/lib/packcr/node/capture_node.rb +14 -12
  25. data/lib/packcr/node/charclass_node.rb +44 -24
  26. data/lib/packcr/node/eof_node.rb +25 -0
  27. data/lib/packcr/node/error_node.rb +20 -13
  28. data/lib/packcr/node/expand_node.rb +18 -9
  29. data/lib/packcr/node/predicate_node.rb +19 -26
  30. data/lib/packcr/node/quantity_node.rb +31 -24
  31. data/lib/packcr/node/reference_node.rb +31 -9
  32. data/lib/packcr/node/root_node.rb +61 -0
  33. data/lib/packcr/node/rule_node.rb +29 -3
  34. data/lib/packcr/node/sequence_node.rb +48 -31
  35. data/lib/packcr/node/string_node.rb +19 -16
  36. data/lib/packcr/node.rb +34 -5
  37. data/lib/packcr/parser.rb +4493 -3896
  38. data/lib/packcr/stream.rb +22 -22
  39. data/lib/packcr/templates/context/header.c.erb +29 -0
  40. data/lib/packcr/templates/context/source.c.erb +1069 -0
  41. data/lib/packcr/templates/context/source.rb.erb +341 -0
  42. data/lib/packcr/templates/node/action.c.erb +16 -0
  43. data/lib/packcr/templates/node/action.rb.erb +21 -0
  44. data/lib/packcr/templates/node/alternate.c.erb +34 -0
  45. data/lib/packcr/templates/node/alternate.rb.erb +39 -0
  46. data/lib/packcr/templates/node/capture.c.erb +17 -0
  47. data/lib/packcr/templates/node/capture.rb.erb +14 -0
  48. data/lib/packcr/templates/node/charclass.c.erb +47 -0
  49. data/lib/packcr/templates/node/charclass.rb.erb +49 -0
  50. data/lib/packcr/templates/node/charclass_any.c.erb +5 -0
  51. data/lib/packcr/templates/node/charclass_any.rb.erb +7 -0
  52. data/lib/packcr/templates/node/charclass_fail.c.erb +1 -0
  53. data/lib/packcr/templates/node/charclass_fail.rb.erb +1 -0
  54. data/lib/packcr/templates/node/charclass_one.c.erb +19 -0
  55. data/lib/packcr/templates/node/charclass_one.rb.erb +23 -0
  56. data/lib/packcr/templates/node/charclass_utf8.c.erb +49 -0
  57. data/lib/packcr/templates/node/charclass_utf8.rb.erb +50 -0
  58. data/lib/packcr/templates/node/charclass_utf8_reverse.rb.erb +51 -0
  59. data/lib/packcr/templates/node/eof.c.erb +1 -0
  60. data/lib/packcr/templates/node/eof.rb.erb +3 -0
  61. data/lib/packcr/templates/node/error.c.erb +28 -0
  62. data/lib/packcr/templates/node/error.rb.erb +34 -0
  63. data/lib/packcr/templates/node/expand.c.erb +16 -0
  64. data/lib/packcr/templates/node/expand.rb.erb +16 -0
  65. data/lib/packcr/templates/node/predicate.c.erb +30 -0
  66. data/lib/packcr/templates/node/predicate.rb.erb +28 -0
  67. data/lib/packcr/templates/node/predicate_neg.c.erb +23 -0
  68. data/lib/packcr/templates/node/predicate_neg.rb.erb +22 -0
  69. data/lib/packcr/templates/node/quantity_many.c.erb +45 -0
  70. data/lib/packcr/templates/node/quantity_many.rb.erb +47 -0
  71. data/lib/packcr/templates/node/quantity_one.c.erb +21 -0
  72. data/lib/packcr/templates/node/quantity_one.rb.erb +23 -0
  73. data/lib/packcr/templates/node/reference.c.erb +17 -0
  74. data/lib/packcr/templates/node/reference.rb.erb +21 -0
  75. data/lib/packcr/templates/node/reference_reverse.rb.erb +21 -0
  76. data/lib/packcr/templates/node/rule.c.erb +26 -0
  77. data/lib/packcr/templates/node/rule.rb.erb +30 -0
  78. data/lib/packcr/templates/node/sequence.c.erb +12 -0
  79. data/lib/packcr/templates/node/sequence.rb.erb +12 -0
  80. data/lib/packcr/templates/node/string_many.c.erb +11 -0
  81. data/lib/packcr/templates/node/string_many.rb.erb +10 -0
  82. data/lib/packcr/templates/node/string_one.c.erb +8 -0
  83. data/lib/packcr/templates/node/string_one.rb.erb +10 -0
  84. data/lib/packcr/util.rb +21 -24
  85. data/lib/packcr/version.rb +1 -1
  86. data/lib/packcr.rb +9 -13
  87. metadata +117 -10
  88. data/lib/packcr/buffer.rb +0 -47
@@ -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<%= format("%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<%= format("%06x", u0) %> && u <= 0x<%= format("%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<%= format("%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<%= format("%04d", onfail) %>;
41
+ <%- else -%>
42
+ )) goto L<%= format("%04d", onfail) %>;
43
+ <%- end -%>
44
+ <%- end -%>
45
+ <%- if gen.location -%>
46
+ pcc_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, n);
47
+ <%- end -%>
48
+ ctx->position_offset += 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[@position_offset]
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
+ @position_offset_loc = @position_offset_loc.forward(@buffer, @position_offset, 1)
49
+ <%- end -%>
50
+ @position_offset += 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[@position_offset]
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
+ @position_offset_loc = @position_offset_loc.forward(@buffer, @position_offset, 1)
40
+ <%- end -%>
41
+ @position_offset += 1
42
+ throw(<%= onsuccess %>)
43
+ end
44
+ <%- else # '.' or '[^]' -%>
45
+ <%- if gen.location -%>
46
+ @position_offset_loc = @position_offset_loc.forward(@buffer, @position_offset, 1)
47
+ <%- end -%>
48
+ @position_offset += 1
49
+ throw(<%= onsuccess %>)
50
+ <%- end -%>
51
+ end
@@ -0,0 +1 @@
1
+ if (pcc_refill_buffer(ctx, 1) >= 1) goto L<%= format("%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<%= format("%04d", m) %>;
6
+ L<%= format("%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->position_offset;
18
+ <%- if gen.location -%>
19
+ thunk->data.leaf.capt0.range.start_loc = chunk->pos_loc;
20
+ thunk->data.leaf.capt0.range.end_loc = ctx->position_offset_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<%= format("%04d", onfail) %>;
27
+ L<%= format("%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, @position_offset,
14
+ <%- if gen.location -%>
15
+ answer.pos_loc,@position_offset_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<%= format("%04d", onfail) %>;
4
+ if (n > 0) {
5
+ const char *const p = ctx->buffer.buf + ctx->position_offset;
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<%= format("%04d", onfail) %>;
10
+ }
11
+ <%- if gen.location -%>
12
+ pcc_location_forward(&ctx->position_offset_loc, ctx->buffer.buf + ctx->position_offset, n);
13
+ <%- end -%>
14
+ ctx->position_offset += 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[@position_offset, 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
+ @position_offset_loc = @position_offset_loc.forward(@buffer, @position_offset, n<%= gen.level %>)
14
+ <%- end -%>
15
+ @position_offset += 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->position_offset;
6
+ <%- if gen.location -%>
7
+ const pcc_location_t p_loc = ctx->position_offset_loc;
8
+ <%- end -%>
9
+ <%= gen.generate_code(expr, l, 4, false) -%>
10
+ <%- if r != Packcr::CODE_REACH__ALWAYS_FAIL -%>
11
+ ctx->position_offset = p;
12
+ <%- if gen.location -%>
13
+ ctx->position_offset_loc = p_loc;
14
+ <%- end -%>
15
+ <%- end -%>
16
+ <%- if r == Packcr::CODE_REACH__BOTH -%>
17
+ goto L<%= format("%04d", m) %>;
18
+ <%- end -%>
19
+ <%- if r != Packcr::CODE_REACH__ALWAYS_SUCCEED -%>
20
+ L<%= format("%04d", l) %>:;
21
+ ctx->position_offset = p;
22
+ <%- if gen.location -%>
23
+ ctx->position_offset_loc = p_loc;
24
+ <%- end -%>
25
+ goto L<%= format("%04d", onfail) %>;
26
+ <%- end -%>
27
+ <%- if r == Packcr::CODE_REACH__BOTH -%>
28
+ L<%= format("%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 %> = @position_offset
6
+ <%- if gen.location -%>
7
+ p_loc<%= gen.level %> = @position_offset_pos
8
+ <%- end -%>
9
+ catch(<%= l %>) do
10
+ <%- %><%= gen.generate_code(expr, l, 4, false) -%>
11
+ <%- if r != Packcr::CODE_REACH__ALWAYS_FAIL -%>
12
+ @position_offset = pos<%= gen.level %>
13
+ <%- if gen.location -%>
14
+ @position_offset_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
+ @position_offset = pos<%= gen.level %>
23
+ <%- if gen.location -%>
24
+ @position_offset_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->position_offset;
5
+ <%- if gen.location -%>
6
+ const pcc_location_t p_loc = ctx->position_offset_loc;
7
+ <%- end -%>
8
+ <%= gen.generate_code(expr, l, 4, false) -%>
9
+ <%- if r != Packcr::CODE_REACH__ALWAYS_FAIL -%>
10
+ ctx->position_offset = p;
11
+ <%- if gen.location -%>
12
+ ctx->position_offset_loc = p_loc;
13
+ <%- end -%>
14
+ goto L<%= format("%04d", onfail) %>;
15
+ <%- end -%>
16
+ <%- if r != Packcr::CODE_REACH__ALWAYS_SUCCEED -%>
17
+ L<%= format("%04d", l) %>:;
18
+ ctx->position_offset = p;
19
+ <%- if gen.location -%>
20
+ ctx->position_offset_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 %> = @position_offset
4
+ <%- if gen.location -%>
5
+ p_loc<%= gen.level %> = @position_offset_loc
6
+ <%- end -%>
7
+ catch(<%= l %>) do
8
+ <%- %><%= gen.generate_code(expr, l, 2, false) -%>
9
+ <%- if r != Packcr::CODE_REACH__ALWAYS_FAIL -%>
10
+ @position_offset = pos<%= gen.level %>
11
+ <%- if gen.location -%>
12
+ @position_offset_loc = p_loc<%= gen.level %>
13
+ <%- end -%>
14
+ throw(<%= onfail %>)
15
+ <%- end -%>
16
+ end
17
+ <%- if r != Packcr::CODE_REACH__ALWAYS_SUCCEED -%>
18
+ @position_offset = pos<%= gen.level %>
19
+ <%- if gen.location -%>
20
+ @position_offset_loc = p_loc<%= gen.level %>
21
+ <%- end -%>
22
+ <%- end -%>
@@ -0,0 +1,45 @@
1
+ {
2
+ <%- if min > 0 -%>
3
+ const size_t p0 = ctx->position_offset;
4
+ <%- if gen.location -%>
5
+ const pcc_location_t p0_loc = ctx->position_offset_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->position_offset;
16
+ <%- if gen.location -%>
17
+ const pcc_location_t p_loc = ctx->position_offset_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->position_offset == p) break;
24
+ <%- if r != Packcr::CODE_REACH__ALWAYS_SUCCEED -%>
25
+ continue;
26
+ L<%= format("%04d", l) %>:;
27
+ ctx->position_offset = p;
28
+ <%- if gen.location -%>
29
+ ctx->position_offset_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->position_offset = p0;
38
+ <%- if gen.location -%>
39
+ ctx->position_offset_loc = p0_loc;
40
+ <%- end -%>
41
+ pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n0);
42
+ goto L<%= format("%04d", onfail) %>;
43
+ }
44
+ <%- end -%>
45
+ }
@@ -0,0 +1,47 @@
1
+ <%- if min > 0 -%>
2
+ q<%= gen.level %> = @position_offset
3
+ <%- if gen.location -%>
4
+ q_loc<%= gen.level %> = @position_offset_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 %> = @position_offset
17
+ <%- if gen.location -%>
18
+ p_loc<%= gen.level %> = @position_offset_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 @position_offset != 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
+ @position_offset = pos<%= gen.level %>
32
+ <%- if gen.location -%>
33
+ @position_offset_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
+ @position_offset = q<%= gen.level %>
41
+ <%- if gen.location -%>
42
+ @position_offset_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->position_offset;
4
+ <%- if gen.location -%>
5
+ const pcc_location_t p_loc = ctx->position_offset_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<%= format("%04d", m) %>;
13
+ L<%= format("%04d", l) %>:;
14
+ <%- if gen.location -%>
15
+ ctx->position_offset_loc = p_loc;
16
+ <%- end -%>
17
+ ctx->position_offset = p;
18
+ pcc_thunk_array__revert(ctx->auxil, &chunk->thunks, n);
19
+ L<%= format("%04d", m) %>:;
20
+ <%- end -%>
21
+ }
@@ -0,0 +1,23 @@
1
+ <%- l = gen.next_label -%>
2
+ pos<%= gen.level %> = @position_offset
3
+ <%- if gen.location -%>
4
+ p_loc<%= gen.level %> = @position_offset_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
+ @position_offset_loc = p_loc<%= gen.level %>
19
+ <%- end -%>
20
+ @position_offset = pos<%= gen.level %>
21
+ answer.thunks[n<%= gen.level %>..-1] = []
22
+ end
23
+ <%- end -%>
@@ -0,0 +1,17 @@
1
+ <%- if index != nil -%>
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) {
5
+ l = limits;
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) %>;
8
+ }
9
+ <%- else -%>
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) {
13
+ l = limits;
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) %>;
16
+ }
17
+ <%- end -%>
@@ -0,0 +1,21 @@
1
+ <%- if index != nil -%>
2
+ if limits && @position_offset == offset && !limits[:evaluate_rule_<%= name %>]
3
+ if !apply_rule(:evaluate_rule_<%= name %>, answer.thunks, answer.values, <%= index %>, offset<% if gen.location %>, offset_loc<% end %>, limits: limits)
4
+ throw(<%= onfail %>)
5
+ end
6
+ else
7
+ if !apply_rule(:evaluate_rule_<%= name %>, answer.thunks, answer.values, <%= index %>, offset<% if gen.location %>, offset_loc<% end %>)
8
+ throw(<%= onfail %>)
9
+ end
10
+ end
11
+ <%- else -%>
12
+ if limits && @position_offset == offset && !limits[:evaluate_rule_<%= name %>]
13
+ if !apply_rule(:evaluate_rule_<%= name %>, answer.thunks, nil, 0, offset<% if gen.location %>, offset_loc<% end %>, limits: limits)
14
+ throw(<%= onfail %>)
15
+ end
16
+ else
17
+ if !apply_rule(:evaluate_rule_<%= name %>, answer.thunks, nil, 0, offset<% if gen.location %>, offset_loc<% end %>)
18
+ throw(<%= onfail %>)
19
+ end
20
+ end
21
+ <%- end -%>
@@ -0,0 +1,21 @@
1
+ <%- if index != nil -%>
2
+ if limits && @position_offset == offset && !limits[:evaluate_rule_<%= name %>]
3
+ if apply_rule(:evaluate_rule_<%= name %>, answer.thunks, answer.values, <%= index %>, offset<% if gen.location %>, offset_loc<% end %>, limits: limits)
4
+ throw(<%= onsuccess %>)
5
+ end
6
+ else
7
+ if apply_rule(:evaluate_rule_<%= name %>, answer.thunks, answer.values, <%= index %>, offset<% if gen.location %>, offset_loc<% end %>)
8
+ throw(<%= onsuccess %>)
9
+ end
10
+ end
11
+ <%- else -%>
12
+ if limits && @position_offset == offset && !limits[:evaluate_rule_<%= name %>]
13
+ if apply_rule(:evaluate_rule_<%= name %>, answer.thunks, nil, 0, offset<% if gen.location %>, offset_loc<% end %>, limits: limits)
14
+ throw(<%= onsuccess %>)
15
+ end
16
+ else
17
+ if apply_rule(:evaluate_rule_<%= name %>, answer.thunks, nil, 0, offset<% if gen.location %>, offset_loc<% end %>)
18
+ throw(<%= onsuccess %>)
19
+ end
20
+ end
21
+ <%- end -%>
@@ -0,0 +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);
3
+ chunk->pos = ctx->position_offset;
4
+ <%- if gen.location -%>
5
+ chunk->pos_loc = ctx->position_offset_loc;
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));
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 %>);
11
+ <%- if vars.length > 0 -%>
12
+ pcc_value_table__clear(ctx->auxil, &chunk->values);
13
+ <%- end -%>
14
+ <%- r = expr.reachability -%>
15
+ <%- -%><%= gen.generate_code(expr, 0, 4, false) -%>
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));
18
+ return chunk;
19
+ <%- if r != Packcr::CODE_REACH__ALWAYS_SUCCEED -%>
20
+ L0000:;
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);
24
+ return NULL;
25
+ <%- end -%>
26
+ }
@@ -0,0 +1,30 @@
1
+ def evaluate_rule_<%= name %>(offset<% if gen.location %>, offset_loc<% end %>, limits: nil)
2
+ answer = ThunkChunk.new
3
+ answer.pos = @position_offset
4
+ <%- if gen.location -%>
5
+ answer.pos_loc = @position_offset_loc
6
+ <%- end -%>
7
+ debug { warn "#{ " " * @level}EVAL <%= name %> #{answer.pos} #{@buffer[answer.pos..-1].inspect}" }
8
+ @level += 1
9
+ answer.resize_captures(<%= capts.length %>)
10
+ <%- if vars.length > 0 -%>
11
+ answer.values = {}
12
+ <%- end -%>
13
+ <%- r = expr.reachability -%>
14
+ <%- if r == Packcr::CODE_REACH__ALWAYS_SUCCEED -%>
15
+ <%- -%><%= gen.generate_code(expr, 0, 2, false) -%>
16
+ @level -= 1
17
+ debug { warn "#{ " " * @level}MATCH <%= name %> #{answer.pos} #{@buffer[answer.pos...@position_offset].inspect}" }
18
+ return answer
19
+ <%- else -%>
20
+ catch(0) do
21
+ <%- -%><%= gen.generate_code(expr, 0, 4, false) -%>
22
+ @level -= 1
23
+ debug { warn "#{ " " * @level}MATCH <%= name %> #{answer.pos} #{@buffer[answer.pos...@position_offset].inspect}" }
24
+ return answer
25
+ end
26
+ @level -= 1
27
+ debug { warn "#{ " " * @level}NOMATCH <%= name %> #{answer.pos} #{@buffer[answer.pos...@position_offset].inspect}" }
28
+ return nil
29
+ <%- end -%>
30
+ 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 -%>