packcr 0.0.8 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/packcr/context.rb +14 -5
- data/lib/packcr/generated/context.rb +189 -85
- data/lib/packcr/generated/node/action_node.rb +17 -0
- data/lib/packcr/generated/node/alternate_node.rb +52 -6
- data/lib/packcr/generated/node/capture_node.rb +15 -0
- data/lib/packcr/generated/node/charclass_node.rb +67 -0
- data/lib/packcr/generated/node/eof_node.rb +7 -0
- data/lib/packcr/generated/node/error_node.rb +7 -0
- data/lib/packcr/generated/node/expand_node.rb +7 -0
- data/lib/packcr/generated/node/predicate_node.rb +33 -0
- data/lib/packcr/generated/node/quantity_node.rb +54 -0
- data/lib/packcr/generated/node/reference_node.rb +14 -0
- data/lib/packcr/generated/node/rule_node.rb +31 -0
- data/lib/packcr/generated/node/sequence_node.rb +18 -0
- data/lib/packcr/generated/node/string_node.rb +24 -0
- data/lib/packcr/node/reference_node.rb +1 -0
- data/lib/packcr/node/rule_node.rb +2 -1
- data/lib/packcr/parser.rb +403 -254
- data/lib/packcr/stream.rb +1 -1
- data/lib/packcr/templates/context/source.c.erb +30 -26
- data/lib/packcr/templates/context/source.rb.erb +160 -159
- data/lib/packcr/templates/context/source.rs.erb +625 -0
- data/lib/packcr/templates/node/action.rs.erb +6 -0
- data/lib/packcr/templates/node/alternate.rs.erb +39 -0
- data/lib/packcr/templates/node/capture.rs.erb +13 -0
- data/lib/packcr/templates/node/charclass_utf8.rs.erb +41 -0
- data/lib/packcr/templates/node/predicate_neg.rs.erb +24 -0
- data/lib/packcr/templates/node/quantity_many.rs.erb +50 -0
- data/lib/packcr/templates/node/reference.rs.erb +3 -0
- data/lib/packcr/templates/node/rule.rs.erb +34 -0
- data/lib/packcr/templates/node/sequence.rs.erb +12 -0
- data/lib/packcr/templates/node/string_many.rs.erb +10 -0
- data/lib/packcr/templates/node/string_one.rs.erb +9 -0
- data/lib/packcr/util.rb +9 -0
- data/lib/packcr/version.rb +1 -1
- metadata +16 -65
data/lib/packcr/parser.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# A packrat parser generated by PackCR 0.0
|
1
|
+
# A packrat parser generated by PackCR 0.1.0
|
2
2
|
|
3
3
|
class Packcr
|
4
4
|
class Parser
|
@@ -41,6 +41,114 @@ class Packcr
|
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
|
+
class LrMemoTable
|
45
|
+
def initialize
|
46
|
+
@memos = {}
|
47
|
+
end
|
48
|
+
|
49
|
+
def clear
|
50
|
+
@memos.clear
|
51
|
+
end
|
52
|
+
|
53
|
+
def []=(index, rule_name, memo)
|
54
|
+
entry = @memos[index] ||= {}
|
55
|
+
entry[rule_name] = memo
|
56
|
+
end
|
57
|
+
|
58
|
+
def [](index, rule_name)
|
59
|
+
@memos.dig(index, rule_name)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
class LrMemo
|
64
|
+
attr_accessor :grow, :answer, :offset, :fail, :offset_loc
|
65
|
+
|
66
|
+
def initialize(offset, offset_loc)
|
67
|
+
@offset = offset
|
68
|
+
@offset_loc = offset_loc
|
69
|
+
@fail = true
|
70
|
+
@grow = false
|
71
|
+
end
|
72
|
+
|
73
|
+
def answer=(answer)
|
74
|
+
@fail = nil
|
75
|
+
@answer = answer
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
class ThunkChunk
|
80
|
+
attr_accessor :thunks, :capts, :pos, :values, :pos_loc
|
81
|
+
|
82
|
+
def initialize
|
83
|
+
super
|
84
|
+
@thunks = []
|
85
|
+
@capts = {}
|
86
|
+
@pos = 0
|
87
|
+
@values = {}
|
88
|
+
end
|
89
|
+
|
90
|
+
def resize_captures(len)
|
91
|
+
len.times do |i|
|
92
|
+
@capts[i] = Capture.new
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
class ThunkLeaf
|
98
|
+
attr_accessor :capt0, :capts, :value_refs, :action
|
99
|
+
|
100
|
+
def initialize(action, capt0 = Capture.new, value_refs = {}, capts = {})
|
101
|
+
@value_refs = value_refs
|
102
|
+
@capts = capts
|
103
|
+
@capt0 = capt0
|
104
|
+
@action = action
|
105
|
+
end
|
106
|
+
|
107
|
+
def do_action(ctx, values, index)
|
108
|
+
ctx.public_send(action, self, values, index)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
class ThunkNode
|
113
|
+
attr_accessor :thunks, :values, :index
|
114
|
+
|
115
|
+
def initialize(thunks, values, index)
|
116
|
+
@thunks = thunks
|
117
|
+
@values = values
|
118
|
+
@index = index
|
119
|
+
values[index] ||= Value.new if values
|
120
|
+
end
|
121
|
+
|
122
|
+
def do_action(ctx, _values, _index)
|
123
|
+
@thunks.each do |thunk|
|
124
|
+
thunk.do_action(ctx, @values, @index)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def clear
|
129
|
+
@thunks.clear
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
class Capture
|
134
|
+
attr_accessor :range_start, :range_end, :start_loc, :end_loc
|
135
|
+
|
136
|
+
def initialize(range_start = 0, range_end = 0, start_loc = nil, end_loc = nil)
|
137
|
+
@range_start = range_start
|
138
|
+
@range_end = range_end
|
139
|
+
@start_loc = start_loc || Location.new
|
140
|
+
@end_loc = end_loc || Location.new
|
141
|
+
end
|
142
|
+
|
143
|
+
def capture_string(buffer)
|
144
|
+
@capture_string ||= buffer[@range_start, @range_end - @range_start]
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
class Value
|
149
|
+
attr_accessor :value
|
150
|
+
end
|
151
|
+
|
44
152
|
def initialize(ctx = nil, ifile = nil, debug: false)
|
45
153
|
@buffer = +""
|
46
154
|
|
@@ -108,6 +216,89 @@ class Packcr
|
|
108
216
|
nil while parse
|
109
217
|
end
|
110
218
|
|
219
|
+
def grow_lr(rule, offset, offset_loc)
|
220
|
+
while true
|
221
|
+
old_offset = @position_offset
|
222
|
+
@position_offset = offset
|
223
|
+
@position_offset_loc = offset_loc
|
224
|
+
answer = public_send(rule, offset, offset_loc, limits: { rule => true })
|
225
|
+
if !answer || @position_offset <= old_offset
|
226
|
+
break
|
227
|
+
end
|
228
|
+
|
229
|
+
memo = @memos[offset, rule]
|
230
|
+
memo.answer = answer
|
231
|
+
memo.offset = @position_offset
|
232
|
+
memo.offset_loc = @position_offset_loc
|
233
|
+
end
|
234
|
+
end
|
235
|
+
|
236
|
+
def rule_answer(rule)
|
237
|
+
offset = @position_offset
|
238
|
+
offset_loc = @position_offset_loc
|
239
|
+
memo = @memos[offset, rule]
|
240
|
+
|
241
|
+
if !memo
|
242
|
+
memo = LrMemo.new(offset, offset_loc)
|
243
|
+
@memos[offset, rule] = memo
|
244
|
+
answer = public_send(rule, offset, offset_loc)
|
245
|
+
memo.answer = answer
|
246
|
+
memo.offset = @position_offset
|
247
|
+
memo.offset_loc = @position_offset_loc
|
248
|
+
if memo.grow
|
249
|
+
grow_lr(rule, offset, offset_loc)
|
250
|
+
memo.grow = false
|
251
|
+
answer = memo.answer
|
252
|
+
@position_offset = memo.offset
|
253
|
+
@position_offset_loc = memo.offset_loc
|
254
|
+
end
|
255
|
+
answer
|
256
|
+
elsif memo.fail
|
257
|
+
memo.answer = nil
|
258
|
+
memo.grow = true
|
259
|
+
nil
|
260
|
+
else
|
261
|
+
@position_offset = memo.offset
|
262
|
+
@position_offset_loc = memo.offset_loc
|
263
|
+
memo.answer
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
def apply_rule(rule, thunks, values, index, offset, offset_loc, limits: nil)
|
268
|
+
if limits
|
269
|
+
limits = limits.merge(rule => true)
|
270
|
+
answer = public_send(rule, offset, offset_loc, limits: limits)
|
271
|
+
memo = @memos[offset, rule]
|
272
|
+
if !answer || @position_offset <= memo.offset
|
273
|
+
if memo
|
274
|
+
answer = memo.answer
|
275
|
+
@position_offset = memo.offset
|
276
|
+
@position_offset_loc = memo.offset_loc
|
277
|
+
end
|
278
|
+
else
|
279
|
+
memo.answer = answer
|
280
|
+
memo.offset = @position_offset
|
281
|
+
memo.offset_loc = @position_offset_loc
|
282
|
+
end
|
283
|
+
else
|
284
|
+
answer = rule_answer(rule)
|
285
|
+
end
|
286
|
+
|
287
|
+
if !answer
|
288
|
+
return false
|
289
|
+
end
|
290
|
+
|
291
|
+
values ||= @global_values
|
292
|
+
thunks << ThunkNode.new(answer.thunks, values, index)
|
293
|
+
true
|
294
|
+
end
|
295
|
+
|
296
|
+
def do_action(thunks, values, index)
|
297
|
+
thunks.each do |thunk|
|
298
|
+
thunk.do_action(self, values, index)
|
299
|
+
end
|
300
|
+
end
|
301
|
+
|
111
302
|
def action_statement_0(__packcr_in, __packcr_vars, __packcr_index)
|
112
303
|
____ = (__packcr_vars[__packcr_index] ||= Value.new).value if __packcr_vars
|
113
304
|
__0 = __packcr_in.capt0.capture_string(@buffer)
|
@@ -296,7 +487,7 @@ class Packcr
|
|
296
487
|
|
297
488
|
def action_directive_string_0(__packcr_in, __packcr_vars, __packcr_index)
|
298
489
|
____ = (__packcr_vars[__packcr_index] ||= Value.new).value if __packcr_vars
|
299
|
-
strings = (__packcr_in.value_refs[0]
|
490
|
+
strings = (__packcr_in.value_refs[0] ||= Value.new).value
|
300
491
|
__0 = __packcr_in.capt0.capture_string(@buffer)
|
301
492
|
__0s = @buffer_start_position + __packcr_in.capt0.range_start
|
302
493
|
__0e = @buffer_start_position + __packcr_in.capt0.range_end
|
@@ -309,7 +500,7 @@ class Packcr
|
|
309
500
|
|
310
501
|
def action_directive_string_1(__packcr_in, __packcr_vars, __packcr_index)
|
311
502
|
____ = (__packcr_vars[__packcr_index] ||= Value.new).value if __packcr_vars
|
312
|
-
strings = (__packcr_in.value_refs[0]
|
503
|
+
strings = (__packcr_in.value_refs[0] ||= Value.new).value
|
313
504
|
__0 = __packcr_in.capt0.capture_string(@buffer)
|
314
505
|
__0s = @buffer_start_position + __packcr_in.capt0.range_start
|
315
506
|
__0e = @buffer_start_position + __packcr_in.capt0.range_end
|
@@ -322,7 +513,7 @@ class Packcr
|
|
322
513
|
|
323
514
|
def action_directive_string_2(__packcr_in, __packcr_vars, __packcr_index)
|
324
515
|
____ = (__packcr_vars[__packcr_index] ||= Value.new).value if __packcr_vars
|
325
|
-
strings = (__packcr_in.value_refs[0]
|
516
|
+
strings = (__packcr_in.value_refs[0] ||= Value.new).value
|
326
517
|
__0 = __packcr_in.capt0.capture_string(@buffer)
|
327
518
|
__0s = @buffer_start_position + __packcr_in.capt0.range_start
|
328
519
|
__0e = @buffer_start_position + __packcr_in.capt0.range_end
|
@@ -363,9 +554,26 @@ class Packcr
|
|
363
554
|
__packcr_vars[__packcr_index].value = ____ if __packcr_vars
|
364
555
|
end
|
365
556
|
|
557
|
+
def action_directive_comment_0(__packcr_in, __packcr_vars, __packcr_index)
|
558
|
+
____ = (__packcr_vars[__packcr_index] ||= Value.new).value if __packcr_vars
|
559
|
+
__0 = __packcr_in.capt0.capture_string(@buffer)
|
560
|
+
__0s = @buffer_start_position + __packcr_in.capt0.range_start
|
561
|
+
__0e = @buffer_start_position + __packcr_in.capt0.range_end
|
562
|
+
__0sl = @buffer_start_position_loc + __packcr_in.capt0.start_loc
|
563
|
+
__0el = @buffer_start_position_loc + __packcr_in.capt0.end_loc
|
564
|
+
__1 = __packcr_in.capts[0].capture_string(@buffer)
|
565
|
+
__1s = @buffer_start_position + __packcr_in.capts[0].range_start
|
566
|
+
__1e = @buffer_start_position + __packcr_in.capts[0].range_end
|
567
|
+
__1sl = @buffer_start_position_loc + __packcr_in.capts[0].start_loc
|
568
|
+
__1el = @buffer_start_position_loc + __packcr_in.capts[0].end_loc
|
569
|
+
Packcr::BroadCast.new(@ctx.code(:eheader), @ctx.code(:esource)) << Packcr::CodeBlock.new(@ctx.line_comment_code(__1), __0sl.linenum, __0sl.charnum)
|
570
|
+
|
571
|
+
__packcr_vars[__packcr_index].value = ____ if __packcr_vars
|
572
|
+
end
|
573
|
+
|
366
574
|
def action_lang_strings_0(__packcr_in, __packcr_vars, __packcr_index)
|
367
575
|
____ = (__packcr_vars[__packcr_index] ||= Value.new).value if __packcr_vars
|
368
|
-
strings = (__packcr_in.value_refs[0]
|
576
|
+
strings = (__packcr_in.value_refs[0] ||= Value.new).value
|
369
577
|
string = (__packcr_in.value_refs[1] ||= Value.new).value
|
370
578
|
__0 = __packcr_in.capt0.capture_string(@buffer)
|
371
579
|
__0s = @buffer_start_position + __packcr_in.capt0.range_start
|
@@ -423,8 +631,8 @@ class Packcr
|
|
423
631
|
|
424
632
|
def action_rule_0(__packcr_in, __packcr_vars, __packcr_index)
|
425
633
|
____ = (__packcr_vars[__packcr_index] ||= Value.new).value if __packcr_vars
|
426
|
-
name = (__packcr_in.value_refs[0]
|
427
|
-
expr = (__packcr_in.value_refs[1]
|
634
|
+
name = (__packcr_in.value_refs[0] ||= Value.new).value
|
635
|
+
expr = (__packcr_in.value_refs[1] ||= Value.new).value
|
428
636
|
__0 = __packcr_in.capt0.capture_string(@buffer)
|
429
637
|
__0s = @buffer_start_position + __packcr_in.capt0.range_start
|
430
638
|
__0e = @buffer_start_position + __packcr_in.capt0.range_end
|
@@ -830,7 +1038,7 @@ class Packcr
|
|
830
1038
|
__0e = @buffer_start_position + __packcr_in.capt0.range_end
|
831
1039
|
__0sl = @buffer_start_position_loc + __packcr_in.capt0.start_loc
|
832
1040
|
__0el = @buffer_start_position_loc + __packcr_in.capt0.end_loc
|
833
|
-
____ =
|
1041
|
+
____ = Packcr.escape_varriables(code, @ctx.lang)
|
834
1042
|
|
835
1043
|
__packcr_vars[__packcr_index].value = ____ if __packcr_vars
|
836
1044
|
end
|
@@ -977,6 +1185,16 @@ class Packcr
|
|
977
1185
|
@position_offset = pos2
|
978
1186
|
@position_offset_loc = p_loc2
|
979
1187
|
answer.thunks[n2..-1] = []
|
1188
|
+
if limits && @position_offset == offset && !limits[:evaluate_rule_directive_comment]
|
1189
|
+
if apply_rule(:evaluate_rule_directive_comment, answer.thunks, nil, 0, offset, offset_loc, limits: limits)
|
1190
|
+
throw(1)
|
1191
|
+
end
|
1192
|
+
elsif apply_rule(:evaluate_rule_directive_comment, answer.thunks, nil, 0, offset, offset_loc)
|
1193
|
+
throw(1)
|
1194
|
+
end
|
1195
|
+
@position_offset = pos2
|
1196
|
+
@position_offset_loc = p_loc2
|
1197
|
+
answer.thunks[n2..-1] = []
|
980
1198
|
if limits && @position_offset == offset && !limits[:evaluate_rule_rule]
|
981
1199
|
if apply_rule(:evaluate_rule_rule, answer.thunks, nil, 0, offset, offset_loc, limits: limits)
|
982
1200
|
throw(1)
|
@@ -1081,12 +1299,25 @@ class Packcr
|
|
1081
1299
|
@position_offset_loc = p_loc2
|
1082
1300
|
answer.thunks[n2..-1] = []
|
1083
1301
|
catch(4) do
|
1302
|
+
if refill_buffer(2) < 2 ||
|
1303
|
+
@buffer[@position_offset, 2] != "rs"
|
1304
|
+
|
1305
|
+
throw(4)
|
1306
|
+
end
|
1307
|
+
@position_offset_loc = @position_offset_loc.forward(@buffer, @position_offset, 2)
|
1308
|
+
@position_offset += 2
|
1309
|
+
throw(1)
|
1310
|
+
end
|
1311
|
+
@position_offset = pos2
|
1312
|
+
@position_offset_loc = p_loc2
|
1313
|
+
answer.thunks[n2..-1] = []
|
1314
|
+
catch(5) do
|
1084
1315
|
if limits && @position_offset == offset && !limits[:evaluate_rule_identifier]
|
1085
1316
|
if !apply_rule(:evaluate_rule_identifier, answer.thunks, nil, 0, offset, offset_loc, limits: limits)
|
1086
|
-
throw(
|
1317
|
+
throw(5)
|
1087
1318
|
end
|
1088
1319
|
elsif !apply_rule(:evaluate_rule_identifier, answer.thunks, nil, 0, offset, offset_loc)
|
1089
|
-
throw(
|
1320
|
+
throw(5)
|
1090
1321
|
end
|
1091
1322
|
answer.thunks.push(
|
1092
1323
|
ThunkLeaf.new(
|
@@ -1903,43 +2134,160 @@ class Packcr
|
|
1903
2134
|
if refill_buffer(8) < 8 ||
|
1904
2135
|
@buffer[@position_offset, 8] != "%capture"
|
1905
2136
|
|
1906
|
-
throw(0)
|
1907
|
-
end
|
1908
|
-
@position_offset_loc = @position_offset_loc.forward(@buffer, @position_offset, 8)
|
1909
|
-
@position_offset += 8
|
1910
|
-
if limits && @position_offset == offset && !limits[:evaluate_rule_spaces]
|
1911
|
-
if !apply_rule(:evaluate_rule_spaces, answer.thunks, nil, 0, offset, offset_loc, limits: limits)
|
1912
|
-
throw(0)
|
2137
|
+
throw(0)
|
2138
|
+
end
|
2139
|
+
@position_offset_loc = @position_offset_loc.forward(@buffer, @position_offset, 8)
|
2140
|
+
@position_offset += 8
|
2141
|
+
if limits && @position_offset == offset && !limits[:evaluate_rule_spaces]
|
2142
|
+
if !apply_rule(:evaluate_rule_spaces, answer.thunks, nil, 0, offset, offset_loc, limits: limits)
|
2143
|
+
throw(0)
|
2144
|
+
end
|
2145
|
+
elsif !apply_rule(:evaluate_rule_spaces, answer.thunks, nil, 0, offset, offset_loc)
|
2146
|
+
throw(0)
|
2147
|
+
end
|
2148
|
+
catch(1) do
|
2149
|
+
pos3 = @position_offset
|
2150
|
+
p_loc3 = @position_offset_loc
|
2151
|
+
n3 = answer.thunks.length
|
2152
|
+
catch(2) do
|
2153
|
+
if refill_buffer(2) < 2 ||
|
2154
|
+
@buffer[@position_offset, 2] != "on"
|
2155
|
+
|
2156
|
+
throw(2)
|
2157
|
+
end
|
2158
|
+
@position_offset_loc = @position_offset_loc.forward(@buffer, @position_offset, 2)
|
2159
|
+
@position_offset += 2
|
2160
|
+
throw(1)
|
2161
|
+
end
|
2162
|
+
@position_offset = pos3
|
2163
|
+
@position_offset_loc = p_loc3
|
2164
|
+
answer.thunks[n3..-1] = []
|
2165
|
+
catch(3) do
|
2166
|
+
if refill_buffer(4) < 4 ||
|
2167
|
+
@buffer[@position_offset, 4] != "true"
|
2168
|
+
|
2169
|
+
throw(3)
|
2170
|
+
end
|
2171
|
+
@position_offset_loc = @position_offset_loc.forward(@buffer, @position_offset, 4)
|
2172
|
+
@position_offset += 4
|
2173
|
+
throw(1)
|
2174
|
+
end
|
2175
|
+
@position_offset = pos3
|
2176
|
+
@position_offset_loc = p_loc3
|
2177
|
+
answer.thunks[n3..-1] = []
|
2178
|
+
throw(0)
|
2179
|
+
end
|
2180
|
+
answer.thunks.push(
|
2181
|
+
ThunkLeaf.new(
|
2182
|
+
:action_directive_value_0,
|
2183
|
+
Capture.new(
|
2184
|
+
answer.pos, @position_offset,
|
2185
|
+
answer.pos_loc, @position_offset_loc,
|
2186
|
+
),
|
2187
|
+
{},
|
2188
|
+
{},
|
2189
|
+
),
|
2190
|
+
)
|
2191
|
+
@level -= 1
|
2192
|
+
debug { warn "#{" " * @level}MATCH directive_value #{answer.pos} #{@buffer[answer.pos...@position_offset].inspect}" }
|
2193
|
+
return answer
|
2194
|
+
end
|
2195
|
+
@level -= 1
|
2196
|
+
debug { warn "#{" " * @level}NOMATCH directive_value #{answer.pos} #{@buffer[answer.pos...@position_offset].inspect}" }
|
2197
|
+
nil
|
2198
|
+
end
|
2199
|
+
|
2200
|
+
def evaluate_rule_directive_comment(offset, offset_loc, limits: nil)
|
2201
|
+
answer = ThunkChunk.new
|
2202
|
+
answer.pos = @position_offset
|
2203
|
+
answer.pos_loc = @position_offset_loc
|
2204
|
+
debug { warn "#{" " * @level}EVAL directive_comment #{answer.pos} #{@buffer[answer.pos..-1].inspect}" }
|
2205
|
+
@level += 1
|
2206
|
+
answer.resize_captures(1)
|
2207
|
+
catch(0) do
|
2208
|
+
if refill_buffer(2) < 2 ||
|
2209
|
+
@buffer[@position_offset, 2] != "%#"
|
2210
|
+
|
2211
|
+
throw(0)
|
2212
|
+
end
|
2213
|
+
@position_offset_loc = @position_offset_loc.forward(@buffer, @position_offset, 2)
|
2214
|
+
@position_offset += 2
|
2215
|
+
pos3 = @position_offset
|
2216
|
+
p_loc3 = @position_offset_loc
|
2217
|
+
n3 = answer.thunks.length
|
2218
|
+
catch(2) do
|
2219
|
+
catch(1) do
|
2220
|
+
if limits && @position_offset == offset && !limits[:evaluate_rule_spaces]
|
2221
|
+
if !apply_rule(:evaluate_rule_spaces, answer.thunks, nil, 0, offset, offset_loc, limits: limits)
|
2222
|
+
throw(1)
|
2223
|
+
end
|
2224
|
+
elsif !apply_rule(:evaluate_rule_spaces, answer.thunks, nil, 0, offset, offset_loc)
|
2225
|
+
throw(1)
|
2226
|
+
end
|
2227
|
+
throw(2)
|
2228
|
+
end
|
2229
|
+
@position_offset_loc = p_loc3
|
2230
|
+
@position_offset = pos3
|
2231
|
+
answer.thunks[n3..-1] = []
|
2232
|
+
end
|
2233
|
+
pos3 = @position_offset
|
2234
|
+
p_loc3 = @position_offset_loc
|
2235
|
+
i4 = 0
|
2236
|
+
pos4 = nil
|
2237
|
+
p_loc4 = nil
|
2238
|
+
n4 = nil
|
2239
|
+
catch(3) do
|
2240
|
+
pos4 = @position_offset
|
2241
|
+
p_loc4 = @position_offset_loc
|
2242
|
+
n4 = answer.thunks.length
|
2243
|
+
if refill_buffer(1) < 1
|
2244
|
+
throw(3)
|
2245
|
+
end
|
2246
|
+
u5 = @buffer[@position_offset]
|
2247
|
+
if u5 == "\n"
|
2248
|
+
|
2249
|
+
throw(3)
|
1913
2250
|
end
|
1914
|
-
|
1915
|
-
|
2251
|
+
@position_offset_loc = @position_offset_loc.forward(@buffer, @position_offset, 1)
|
2252
|
+
@position_offset += 1
|
2253
|
+
i4 += 1
|
2254
|
+
if @position_offset != pos4
|
2255
|
+
redo
|
2256
|
+
end
|
2257
|
+
pos4 = nil
|
1916
2258
|
end
|
1917
|
-
|
2259
|
+
if pos4
|
2260
|
+
@position_offset = pos4
|
2261
|
+
@position_offset_loc = p_loc4
|
2262
|
+
answer.thunks[n4..-1] = []
|
2263
|
+
end
|
2264
|
+
q3 = @position_offset
|
2265
|
+
capt3 = answer.capts[0]
|
2266
|
+
capt3.range_start = pos3
|
2267
|
+
capt3.range_end = q3
|
2268
|
+
q_loc3 = @position_offset_loc
|
2269
|
+
capt3.start_loc = p_loc3
|
2270
|
+
capt3.end_loc = q_loc3
|
2271
|
+
catch(4) do
|
1918
2272
|
pos3 = @position_offset
|
1919
2273
|
p_loc3 = @position_offset_loc
|
1920
2274
|
n3 = answer.thunks.length
|
1921
|
-
|
1922
|
-
if
|
1923
|
-
|
1924
|
-
|
1925
|
-
throw(2)
|
2275
|
+
if limits && @position_offset == offset && !limits[:evaluate_rule_lf]
|
2276
|
+
if apply_rule(:evaluate_rule_lf, answer.thunks, nil, 0, offset, offset_loc, limits: limits)
|
2277
|
+
throw(4)
|
1926
2278
|
end
|
1927
|
-
|
1928
|
-
|
1929
|
-
throw(1)
|
2279
|
+
elsif apply_rule(:evaluate_rule_lf, answer.thunks, nil, 0, offset, offset_loc)
|
2280
|
+
throw(4)
|
1930
2281
|
end
|
1931
2282
|
@position_offset = pos3
|
1932
2283
|
@position_offset_loc = p_loc3
|
1933
2284
|
answer.thunks[n3..-1] = []
|
1934
|
-
|
1935
|
-
if
|
1936
|
-
|
1937
|
-
|
1938
|
-
throw(3)
|
2285
|
+
if limits && @position_offset == offset && !limits[:evaluate_rule_EOF]
|
2286
|
+
if apply_rule(:evaluate_rule_EOF, answer.thunks, nil, 0, offset, offset_loc, limits: limits)
|
2287
|
+
throw(4)
|
1939
2288
|
end
|
1940
|
-
|
1941
|
-
|
1942
|
-
throw(1)
|
2289
|
+
elsif apply_rule(:evaluate_rule_EOF, answer.thunks, nil, 0, offset, offset_loc)
|
2290
|
+
throw(4)
|
1943
2291
|
end
|
1944
2292
|
@position_offset = pos3
|
1945
2293
|
@position_offset_loc = p_loc3
|
@@ -1948,21 +2296,21 @@ class Packcr
|
|
1948
2296
|
end
|
1949
2297
|
answer.thunks.push(
|
1950
2298
|
ThunkLeaf.new(
|
1951
|
-
:
|
2299
|
+
:action_directive_comment_0,
|
1952
2300
|
Capture.new(
|
1953
2301
|
answer.pos, @position_offset,
|
1954
2302
|
answer.pos_loc, @position_offset_loc,
|
1955
2303
|
),
|
1956
2304
|
{},
|
1957
|
-
|
2305
|
+
answer.capts.slice(0),
|
1958
2306
|
),
|
1959
2307
|
)
|
1960
2308
|
@level -= 1
|
1961
|
-
debug { warn "#{" " * @level}MATCH
|
2309
|
+
debug { warn "#{" " * @level}MATCH directive_comment #{answer.pos} #{@buffer[answer.pos...@position_offset].inspect}" }
|
1962
2310
|
return answer
|
1963
2311
|
end
|
1964
2312
|
@level -= 1
|
1965
|
-
debug { warn "#{" " * @level}NOMATCH
|
2313
|
+
debug { warn "#{" " * @level}NOMATCH directive_comment #{answer.pos} #{@buffer[answer.pos...@position_offset].inspect}" }
|
1966
2314
|
nil
|
1967
2315
|
end
|
1968
2316
|
|
@@ -2889,8 +3237,7 @@ class Packcr
|
|
2889
3237
|
throw(5)
|
2890
3238
|
end
|
2891
3239
|
u8 = @buffer[@position_offset]
|
2892
|
-
if !
|
2893
|
-
[" ", "\t", "\v", "\f", "\r", "\n"].include?(u8)
|
3240
|
+
if ![" ", "\t", "\v", "\f", "\r", "\n"].include?(u8)
|
2894
3241
|
|
2895
3242
|
throw(5)
|
2896
3243
|
end
|
@@ -2987,8 +3334,7 @@ class Packcr
|
|
2987
3334
|
throw(8)
|
2988
3335
|
end
|
2989
3336
|
u8 = @buffer[@position_offset]
|
2990
|
-
if !
|
2991
|
-
[" ", "\t", "\v", "\f", "\r", "\n"].include?(u8)
|
3337
|
+
if ![" ", "\t", "\v", "\f", "\r", "\n"].include?(u8)
|
2992
3338
|
|
2993
3339
|
throw(8)
|
2994
3340
|
end
|
@@ -3057,8 +3403,7 @@ class Packcr
|
|
3057
3403
|
throw(11)
|
3058
3404
|
end
|
3059
3405
|
u8 = @buffer[@position_offset]
|
3060
|
-
if !
|
3061
|
-
[" ", "\t", "\v", "\f", "\r", "\n"].include?(u8)
|
3406
|
+
if ![" ", "\t", "\v", "\f", "\r", "\n"].include?(u8)
|
3062
3407
|
|
3063
3408
|
throw(11)
|
3064
3409
|
end
|
@@ -3222,9 +3567,8 @@ class Packcr
|
|
3222
3567
|
throw(14)
|
3223
3568
|
end
|
3224
3569
|
u6 = @buffer[@position_offset]
|
3225
|
-
if !(
|
3226
|
-
|
3227
|
-
)
|
3570
|
+
if !u6.between?("1", "9")
|
3571
|
+
|
3228
3572
|
throw(14)
|
3229
3573
|
end
|
3230
3574
|
@position_offset_loc = @position_offset_loc.forward(@buffer, @position_offset, 1)
|
@@ -3241,9 +3585,8 @@ class Packcr
|
|
3241
3585
|
throw(15)
|
3242
3586
|
end
|
3243
3587
|
u7 = @buffer[@position_offset]
|
3244
|
-
if !(
|
3245
|
-
|
3246
|
-
)
|
3588
|
+
if !u7.between?("0", "9")
|
3589
|
+
|
3247
3590
|
throw(15)
|
3248
3591
|
end
|
3249
3592
|
@position_offset_loc = @position_offset_loc.forward(@buffer, @position_offset, 1)
|
@@ -3733,8 +4076,7 @@ class Packcr
|
|
3733
4076
|
throw(1)
|
3734
4077
|
end
|
3735
4078
|
u6 = @buffer[@position_offset]
|
3736
|
-
if !
|
3737
|
-
[" ", "\t", "\v", "\f", "\r", "\n"].include?(u6)
|
4079
|
+
if ![" ", "\t", "\v", "\f", "\r", "\n"].include?(u6)
|
3738
4080
|
|
3739
4081
|
throw(1)
|
3740
4082
|
end
|
@@ -3826,8 +4168,7 @@ class Packcr
|
|
3826
4168
|
throw(2)
|
3827
4169
|
end
|
3828
4170
|
u5 = @buffer[@position_offset]
|
3829
|
-
if !
|
3830
|
-
[" ", "\t", "\v", "\f", "\r", "\n"].include?(u5)
|
4171
|
+
if ![" ", "\t", "\v", "\f", "\r", "\n"].include?(u5)
|
3831
4172
|
|
3832
4173
|
throw(2)
|
3833
4174
|
end
|
@@ -4364,8 +4705,8 @@ class Packcr
|
|
4364
4705
|
end
|
4365
4706
|
u3 = @buffer[@position_offset]
|
4366
4707
|
if !(
|
4367
|
-
(
|
4368
|
-
(
|
4708
|
+
u3.between?("a", "z") ||
|
4709
|
+
u3.between?("A", "Z") ||
|
4369
4710
|
u3 == "_"
|
4370
4711
|
)
|
4371
4712
|
throw(0)
|
@@ -4385,10 +4726,10 @@ class Packcr
|
|
4385
4726
|
end
|
4386
4727
|
u4 = @buffer[@position_offset]
|
4387
4728
|
if !(
|
4388
|
-
(
|
4389
|
-
(
|
4729
|
+
u4.between?("a", "z") ||
|
4730
|
+
u4.between?("A", "Z") ||
|
4390
4731
|
u4 == "_" ||
|
4391
|
-
(
|
4732
|
+
u4.between?("0", "9")
|
4392
4733
|
)
|
4393
4734
|
throw(1)
|
4394
4735
|
end
|
@@ -4448,8 +4789,7 @@ class Packcr
|
|
4448
4789
|
throw(1)
|
4449
4790
|
end
|
4450
4791
|
u3 = @buffer[@position_offset]
|
4451
|
-
if !
|
4452
|
-
[" ", "\t", "\v", "\f", "\r", "\n"].include?(u3)
|
4792
|
+
if ![" ", "\t", "\v", "\f", "\r", "\n"].include?(u3)
|
4453
4793
|
|
4454
4794
|
throw(1)
|
4455
4795
|
end
|
@@ -4694,197 +5034,6 @@ class Packcr
|
|
4694
5034
|
debug { warn "#{" " * @level}NOMATCH EOF #{answer.pos} #{@buffer[answer.pos...@position_offset].inspect}" }
|
4695
5035
|
nil
|
4696
5036
|
end
|
4697
|
-
|
4698
|
-
def grow_lr(rule, offset, offset_loc)
|
4699
|
-
while true
|
4700
|
-
old_offset = @position_offset
|
4701
|
-
@position_offset = offset
|
4702
|
-
@position_offset_loc = offset_loc
|
4703
|
-
answer = public_send(rule, offset, offset_loc, limits: { rule => true })
|
4704
|
-
if !answer || @position_offset <= old_offset
|
4705
|
-
break
|
4706
|
-
end
|
4707
|
-
|
4708
|
-
memo = @memos[offset, rule]
|
4709
|
-
memo.answer = answer
|
4710
|
-
memo.offset = @position_offset
|
4711
|
-
memo.offset_loc = @position_offset_loc
|
4712
|
-
end
|
4713
|
-
end
|
4714
|
-
|
4715
|
-
def rule_answer(rule)
|
4716
|
-
offset = @position_offset
|
4717
|
-
offset_loc = @position_offset_loc
|
4718
|
-
memo = @memos[offset, rule]
|
4719
|
-
|
4720
|
-
if !memo
|
4721
|
-
memo = LrMemo.new(offset, offset_loc)
|
4722
|
-
@memos[offset, rule] = memo
|
4723
|
-
answer = public_send(rule, offset, offset_loc)
|
4724
|
-
memo.answer = answer
|
4725
|
-
memo.offset = @position_offset
|
4726
|
-
memo.offset_loc = @position_offset_loc
|
4727
|
-
if memo.grow
|
4728
|
-
grow_lr(rule, offset, offset_loc)
|
4729
|
-
memo.grow = false
|
4730
|
-
answer = memo.answer
|
4731
|
-
@position_offset = memo.offset
|
4732
|
-
@position_offset_loc = memo.offset_loc
|
4733
|
-
end
|
4734
|
-
answer
|
4735
|
-
elsif memo.fail
|
4736
|
-
memo.answer = nil
|
4737
|
-
memo.grow = true
|
4738
|
-
nil
|
4739
|
-
else
|
4740
|
-
@position_offset = memo.offset
|
4741
|
-
@position_offset_loc = memo.offset_loc
|
4742
|
-
memo.answer
|
4743
|
-
end
|
4744
|
-
end
|
4745
|
-
|
4746
|
-
def apply_rule(rule, thunks, values, index, offset, offset_loc, limits: nil)
|
4747
|
-
if limits
|
4748
|
-
limits = limits.merge(rule => true)
|
4749
|
-
answer = public_send(rule, offset, offset_loc, limits: limits)
|
4750
|
-
memo = @memos[offset, rule]
|
4751
|
-
if !answer || @position_offset <= memo.offset
|
4752
|
-
if memo
|
4753
|
-
answer = memo.answer
|
4754
|
-
@position_offset = memo.offset
|
4755
|
-
@position_offset_loc = memo.offset_loc
|
4756
|
-
end
|
4757
|
-
else
|
4758
|
-
memo.answer = answer
|
4759
|
-
memo.offset = @position_offset
|
4760
|
-
memo.offset_loc = @position_offset_loc
|
4761
|
-
end
|
4762
|
-
else
|
4763
|
-
answer = rule_answer(rule)
|
4764
|
-
end
|
4765
|
-
|
4766
|
-
if !answer
|
4767
|
-
return false
|
4768
|
-
end
|
4769
|
-
|
4770
|
-
values ||= @global_values
|
4771
|
-
thunks << ThunkNode.new(answer.thunks, values, index)
|
4772
|
-
true
|
4773
|
-
end
|
4774
|
-
|
4775
|
-
def do_action(thunks, values, index)
|
4776
|
-
thunks.each do |thunk|
|
4777
|
-
thunk.do_action(self, values, index)
|
4778
|
-
end
|
4779
|
-
end
|
4780
|
-
|
4781
|
-
class LrMemoTable
|
4782
|
-
def initialize
|
4783
|
-
@memos = {}
|
4784
|
-
end
|
4785
|
-
|
4786
|
-
def clear
|
4787
|
-
@memos.clear
|
4788
|
-
end
|
4789
|
-
|
4790
|
-
def []=(index, rule_name, memo)
|
4791
|
-
entry = @memos[index] ||= {}
|
4792
|
-
entry[rule_name] = memo
|
4793
|
-
end
|
4794
|
-
|
4795
|
-
def [](index, rule_name)
|
4796
|
-
@memos.dig(index, rule_name)
|
4797
|
-
end
|
4798
|
-
end
|
4799
|
-
|
4800
|
-
class LrMemo
|
4801
|
-
attr_accessor :grow, :answer, :offset, :fail, :offset_loc
|
4802
|
-
|
4803
|
-
def initialize(offset, offset_loc)
|
4804
|
-
@offset = offset
|
4805
|
-
@offset_loc = offset_loc
|
4806
|
-
@fail = true
|
4807
|
-
@grow = false
|
4808
|
-
end
|
4809
|
-
|
4810
|
-
def answer=(answer)
|
4811
|
-
@fail = nil
|
4812
|
-
@answer = answer
|
4813
|
-
end
|
4814
|
-
end
|
4815
|
-
|
4816
|
-
class ThunkChunk
|
4817
|
-
attr_accessor :thunks, :capts, :pos, :values, :pos_loc
|
4818
|
-
|
4819
|
-
def initialize
|
4820
|
-
super
|
4821
|
-
@thunks = []
|
4822
|
-
@capts = {}
|
4823
|
-
@pos = 0
|
4824
|
-
@values = {}
|
4825
|
-
end
|
4826
|
-
|
4827
|
-
def resize_captures(len)
|
4828
|
-
len.times do |i|
|
4829
|
-
@capts[i] = Capture.new
|
4830
|
-
end
|
4831
|
-
end
|
4832
|
-
end
|
4833
|
-
|
4834
|
-
class ThunkLeaf
|
4835
|
-
attr_accessor :capt0, :capts, :value_refs, :action
|
4836
|
-
|
4837
|
-
def initialize(action, capt0 = Capture.new, value_refs = {}, capts = {})
|
4838
|
-
@value_refs = value_refs
|
4839
|
-
@capts = capts
|
4840
|
-
@capt0 = capt0
|
4841
|
-
@action = action
|
4842
|
-
end
|
4843
|
-
|
4844
|
-
def do_action(ctx, values, index)
|
4845
|
-
ctx.public_send(action, self, values, index)
|
4846
|
-
end
|
4847
|
-
end
|
4848
|
-
|
4849
|
-
class ThunkNode
|
4850
|
-
attr_accessor :thunks, :values, :index
|
4851
|
-
|
4852
|
-
def initialize(thunks, values, index)
|
4853
|
-
@thunks = thunks
|
4854
|
-
@values = values
|
4855
|
-
@index = index
|
4856
|
-
values[index] ||= Value.new if values
|
4857
|
-
end
|
4858
|
-
|
4859
|
-
def do_action(ctx, _values, _index)
|
4860
|
-
@thunks.each do |thunk|
|
4861
|
-
thunk.do_action(ctx, @values, @index)
|
4862
|
-
end
|
4863
|
-
end
|
4864
|
-
|
4865
|
-
def clear
|
4866
|
-
@thunks.clear
|
4867
|
-
end
|
4868
|
-
end
|
4869
|
-
|
4870
|
-
class Capture
|
4871
|
-
attr_accessor :range_start, :range_end, :start_loc, :end_loc
|
4872
|
-
|
4873
|
-
def initialize(range_start = 0, range_end = 0, start_loc = nil, end_loc = nil)
|
4874
|
-
@range_start = range_start
|
4875
|
-
@range_end = range_end
|
4876
|
-
@start_loc = start_loc || Location.new
|
4877
|
-
@end_loc = end_loc || Location.new
|
4878
|
-
end
|
4879
|
-
|
4880
|
-
def capture_string(buffer)
|
4881
|
-
@capture_string ||= buffer[@range_start, @range_end - @range_start]
|
4882
|
-
end
|
4883
|
-
end
|
4884
|
-
|
4885
|
-
class Value
|
4886
|
-
attr_accessor :value
|
4887
|
-
end
|
4888
5037
|
end
|
4889
5038
|
|
4890
5039
|
class Parser
|