flexr 2.1.0 → 2.2.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/CHANGELOG.md +33 -0
- data/README.md +4 -0
- data/benchmark/dfa_stretch.rb +149 -0
- data/benchmark/lrama_lexer.rb +79 -96
- data/benchmark/regexp_parser_corpus.rb +35 -0
- data/benchmark/regexp_parser_parity.rb +89 -0
- data/benchmark/regexp_parser_scanner.rb +159 -0
- data/docs/README.md +4 -1
- data/docs/adr/0021-ruby-34-minimum.md +23 -0
- data/docs/adr/0022-immutable-tokens-and-locations.md +22 -0
- data/docs/adr/0023-stable-explicit-firstmatch.md +21 -0
- data/docs/adr/0024-opt-in-ractor-sharing.md +24 -0
- data/docs/adr/0025-explicit-whole-token-acceleration.md +26 -0
- data/docs/adr/0026-retain-direct-dispatch.md +22 -0
- data/docs/adr/0033-action-free-recognition.md +33 -0
- data/docs/adr/0034-lexer-checkpoints.md +25 -0
- data/docs/adr/0035-parser-token-expectations.md +27 -0
- data/docs/adr/0038-replay-named-regexp-captures.md +23 -0
- data/docs/adr/0039-input-stack.md +25 -0
- data/docs/adr/0040-runtime-delimiters.md +23 -0
- data/docs/adr/0041-post-lexical-indentation.md +24 -0
- data/docs/adr/0042-capture-delegation.md +23 -0
- data/docs/adr/0043-expand-reusable-rules-explicitly.md +27 -0
- data/docs/adr/0044-import-oedipus-firstmatch.md +26 -0
- data/docs/assets/playground-absence-and-diagnostics.gif +0 -0
- data/docs/explanation/backends.md +5 -4
- data/docs/how-to/integrate-with-lrama.md +9 -8
- data/docs/how-to/integrate-with-racc.md +9 -0
- data/docs/how-to/migrate-from-oedipus-lex.md +43 -0
- data/docs/how-to/parser-driven-lexing.md +48 -0
- data/docs/how-to/try-playground-demos.md +13 -0
- data/docs/internals/regexp-parser-experiment.md +67 -0
- data/docs/perf-log.md +120 -8
- data/docs/reference/README.md +1 -1
- data/docs/reference/actions.md +106 -0
- data/docs/reference/cli.md +75 -4
- data/docs/reference/diagnostics.md +3 -9
- data/docs/reference/dsl.md +66 -3
- data/docs/reference/public-api.md +8 -1
- data/docs/reference/regexp.md +27 -4
- data/docs/reference/ruby-lsp.md +15 -0
- data/docs/reference/runtime.md +163 -2
- data/docs/tutorial/build-a-calculator-parser.md +47 -0
- data/examples/with_lrama/README.md +3 -3
- data/lib/flexr/automaton/accel.rb +1 -0
- data/lib/flexr/automaton/compiler.rb +15 -0
- data/lib/flexr/automaton/dfa.rb +0 -33
- data/lib/flexr/automaton/keyword_tables.rb +14 -8
- data/lib/flexr/automaton/reference_dfa.rb +38 -0
- data/lib/flexr/cli.rb +118 -18
- data/lib/flexr/codegen/table.rb +7 -0
- data/lib/flexr/configuration.rb +18 -1
- data/lib/flexr/dsl.rb +14 -3
- data/lib/flexr/generated.rb +4 -0
- data/lib/flexr/generator.rb +39 -7
- data/lib/flexr/importer/oedipus_lex.rb +229 -0
- data/lib/flexr/importer.rb +7 -2
- data/lib/flexr/ir.rb +1 -1
- data/lib/flexr/options.rb +3 -0
- data/lib/flexr/project_template.rb +46 -0
- data/lib/flexr/project_templates/common/.gitignore +1 -0
- data/lib/flexr/project_templates/common/lexer.flexr.rb +12 -0
- data/lib/flexr/project_templates/common/test_calculator.rb +15 -0
- data/lib/flexr/project_templates/lrama/Gemfile +9 -0
- data/lib/flexr/project_templates/lrama/README.md +17 -0
- data/lib/flexr/project_templates/lrama/Rakefile +18 -0
- data/lib/flexr/project_templates/lrama/calculator.rb +28 -0
- data/lib/flexr/project_templates/lrama/parser.y +72 -0
- data/lib/flexr/project_templates/racc/Gemfile +8 -0
- data/lib/flexr/project_templates/racc/README.md +15 -0
- data/lib/flexr/project_templates/racc/Rakefile +14 -0
- data/lib/flexr/project_templates/racc/calculator.rb +22 -0
- data/lib/flexr/project_templates/racc/parser.y +19 -0
- data/lib/flexr/regexp/ast.rb +2 -0
- data/lib/flexr/regexp/parser.rb +13 -11
- data/lib/flexr/regexp/tokenizer.rb +2 -2
- data/lib/flexr/rules.rb +53 -0
- data/lib/flexr/runtime/buffer.rb +1 -1
- data/lib/flexr/runtime/captures.rb +46 -0
- data/lib/flexr/runtime/channels.rb +2 -1
- data/lib/flexr/runtime/checkpoint.rb +116 -0
- data/lib/flexr/runtime/core.rb +55 -43
- data/lib/flexr/runtime/delegation.rb +123 -0
- data/lib/flexr/runtime/delimiters.rb +89 -0
- data/lib/flexr/runtime/firstmatch.rb +266 -0
- data/lib/flexr/runtime/indentation.rb +235 -0
- data/lib/flexr/runtime/input_stack.rb +48 -0
- data/lib/flexr/runtime/interpreter.rb +36 -314
- data/lib/flexr/runtime/recognizer.rb +106 -0
- data/lib/flexr/runtime.rb +10 -0
- data/lib/flexr/skeleton/verification.rb +103 -0
- data/lib/flexr/skeleton.rb +208 -0
- data/lib/flexr/source/fixes.rb +145 -0
- data/lib/flexr/source/prism_reader.rb +187 -25
- data/lib/flexr/testing.rb +5 -2
- data/lib/flexr/unicode/reference_regexp.rb +29 -11
- data/lib/flexr/version.rb +1 -1
- data/lib/ruby_lsp/flexr/addon.rb +88 -13
- data/lib/ruby_lsp/flexr/navigation.rb +231 -0
- data/lib/ruby_lsp/flexr/ranges.rb +40 -0
- data/sig/flexr.rbs +33 -3
- data/site/README.md +22 -7
- data/site/ruby/presets/heredoc.flexr.rb +15 -0
- data/site/ruby/presets/html_javascript.rb +12 -0
- data/site/ruby/presets/html_js.flexr.rb +14 -0
- data/site/ruby/presets/indentation.flexr.rb +14 -0
- data/site/scripts/prepare-playground.mjs +16 -6
- data/site/scripts/stable-release.mjs +4 -1
- data/site/src/content/docs/learn/parser-integration.md +23 -8
- data/site/src/content/docs/reference/cli.md +3 -0
- data/site/src/content/docs/reference/dsl.md +1 -0
- data/site/src/content/docs/reference/runtime.md +26 -0
- data/site/src/pages/playground.astro +15 -1
- data/site/src/playground/client.js +30 -1
- data/site/src/playground/issue.mjs +19 -0
- data/site/src/playground/runtime.mjs +4 -3
- data/site/src/playground/worker.mjs +1 -1
- data/site/tests/browser/playground.spec.mjs +64 -0
- data/site/tests/playground-issue.test.mjs +15 -0
- data/site/tests/playground-release.test.mjs +62 -11
- data/site/tests/playground-runtime.test.mjs +48 -0
- data/test/ruby_lsp_addon_spec.rb +174 -0
- metadata +63 -1
|
@@ -8,9 +8,11 @@ end
|
|
|
8
8
|
|
|
9
9
|
module Flexr
|
|
10
10
|
module Runtime
|
|
11
|
-
Match = Struct.new(:rule, :start_pos, :end_pos, :total_end_pos, keyword_init: true)
|
|
11
|
+
Match = Struct.new(:rule, :start_pos, :end_pos, :total_end_pos, :capture, keyword_init: true)
|
|
12
12
|
|
|
13
13
|
class Interpreter
|
|
14
|
+
include Firstmatch if defined?(Firstmatch)
|
|
15
|
+
|
|
14
16
|
def initialize(lexer)
|
|
15
17
|
@lexer = lexer
|
|
16
18
|
klass = lexer.class
|
|
@@ -19,12 +21,12 @@ module Flexr
|
|
|
19
21
|
@config = klass.__flexr_config
|
|
20
22
|
end
|
|
21
23
|
|
|
22
|
-
def scan
|
|
24
|
+
def scan(expected = nil)
|
|
23
25
|
machine = @compiled.machines.fetch(@lexer.state)
|
|
24
26
|
position = @lexer.byte_pos
|
|
25
27
|
return nil unless @lexer.valid_utf8_at?(position)
|
|
26
|
-
return scan_firstmatch(position) if @config.backend == :firstmatch
|
|
27
|
-
if fast_path?(machine)
|
|
28
|
+
return scan_firstmatch(position, expected) if @config.backend == :firstmatch
|
|
29
|
+
if !expected && fast_path?(machine)
|
|
28
30
|
if machine.whole_tokens
|
|
29
31
|
match = @lexer.__flexr_whole_token_match(machine.whole_tokens, position)
|
|
30
32
|
return match if match
|
|
@@ -35,10 +37,10 @@ module Flexr
|
|
|
35
37
|
|
|
36
38
|
buffer = @lexer.buffer
|
|
37
39
|
state = machine.dfa.start
|
|
38
|
-
best =
|
|
40
|
+
best = nil
|
|
39
41
|
cursor = position
|
|
40
|
-
best = consider_acceptances(machine, state, cursor, position, buffer, best) if @config.options[:allow_empty_match]
|
|
41
|
-
acceleration_regions =
|
|
42
|
+
best = consider_acceptances(machine, state, cursor, position, buffer, best, expected) if @config.options[:allow_empty_match]
|
|
43
|
+
acceleration_regions = acceleration_regions_for(machine) if !expected && acceleration_enabled?
|
|
42
44
|
|
|
43
45
|
while buffer.ensure_available?(cursor + 1)
|
|
44
46
|
if acceleration_regions && (region = acceleration_regions[state]) &&
|
|
@@ -63,7 +65,7 @@ module Flexr
|
|
|
63
65
|
cursor += 1
|
|
64
66
|
acceptance = machine.dfa.accepts[state].first
|
|
65
67
|
ensure_token_size!(cursor, position, rule: acceptance&.rule_index)
|
|
66
|
-
best = consider_acceptances(machine, state, cursor, position, buffer, best)
|
|
68
|
+
best = consider_acceptances(machine, state, cursor, position, buffer, best, expected)
|
|
67
69
|
end
|
|
68
70
|
best
|
|
69
71
|
end
|
|
@@ -106,7 +108,7 @@ module Flexr
|
|
|
106
108
|
rule = rules.fetch(acceptance.rule_index)
|
|
107
109
|
next if best && cursor == best.total_end_pos && rule.loses_priority_tie?(best.rule)
|
|
108
110
|
|
|
109
|
-
best ||= (@
|
|
111
|
+
best ||= (@fast_match ||= Match.new)
|
|
110
112
|
best.rule = rule
|
|
111
113
|
best.start_pos = position
|
|
112
114
|
best.end_pos = cursor
|
|
@@ -138,7 +140,7 @@ module Flexr
|
|
|
138
140
|
rule = rules.fetch(acceptance.rule_index)
|
|
139
141
|
next if best && cursor == best.total_end_pos && rule.loses_priority_tie?(best.rule)
|
|
140
142
|
|
|
141
|
-
best ||= (@
|
|
143
|
+
best ||= (@fast_match ||= Match.new)
|
|
142
144
|
best.rule = rule
|
|
143
145
|
best.start_pos = position
|
|
144
146
|
best.end_pos = cursor
|
|
@@ -149,100 +151,16 @@ module Flexr
|
|
|
149
151
|
|
|
150
152
|
private
|
|
151
153
|
|
|
152
|
-
def reference_match(position, buffer)
|
|
153
|
-
return unless reference_rules?
|
|
154
|
-
|
|
155
|
-
candidates = @rules.filter_map do |rule|
|
|
156
|
-
next unless rule_active?(rule)
|
|
157
|
-
next unless rule.patterns.any? { |pattern| reference_pattern?(pattern) }
|
|
158
|
-
|
|
159
|
-
matches = rule.patterns.each_with_index.filter_map do |pattern, pattern_index|
|
|
160
|
-
condition = rule.pattern_conditions.fetch(pattern_index)
|
|
161
|
-
next if condition.bol_only && !@lexer.beginning_of_line?
|
|
162
|
-
|
|
163
|
-
match = streamed_match(
|
|
164
|
-
pattern, buffer, position, reference: reference_pattern?(pattern),
|
|
165
|
-
limit: :token, rule: rule
|
|
166
|
-
)
|
|
167
|
-
next unless match&.begin(0)&.zero?
|
|
168
|
-
|
|
169
|
-
end_position = position + match[0].bytesize
|
|
170
|
-
next unless @lexer.utf8_boundary?(end_position)
|
|
171
|
-
next if condition.end_anchor && !end_anchor_match?(buffer, end_position)
|
|
172
|
-
|
|
173
|
-
[match, condition]
|
|
174
|
-
end
|
|
175
|
-
match, _condition = matches.max_by { |item| item[0][0].bytesize }
|
|
176
|
-
next unless match&.begin(0)&.zero?
|
|
177
|
-
|
|
178
|
-
end_position = position + match[0].bytesize
|
|
179
|
-
next unless @lexer.utf8_boundary?(end_position)
|
|
180
|
-
|
|
181
|
-
trailing = trailing_length(rule, buffer, end_position)
|
|
182
|
-
next if rule.trailing && trailing.nil?
|
|
183
|
-
|
|
184
|
-
ensure_token_size!(end_position, position, rule: rule)
|
|
185
|
-
Match.new(rule: rule, start_pos: position, end_pos: end_position,
|
|
186
|
-
total_end_pos: end_position + (trailing || 0))
|
|
187
|
-
end
|
|
188
|
-
candidates.max_by { |candidate| [candidate.total_end_pos, -candidate.rule.index] }
|
|
189
|
-
end
|
|
190
|
-
|
|
191
|
-
def reference_rules?
|
|
192
|
-
return false unless @config.backend == :firstmatch
|
|
193
|
-
return @reference_rules unless @reference_rules.nil?
|
|
194
|
-
|
|
195
|
-
@reference_rules = @rules.any? do |rule|
|
|
196
|
-
rule.patterns.any? { |pattern| reference_pattern?(pattern) }
|
|
197
|
-
end
|
|
198
|
-
end
|
|
199
|
-
|
|
200
154
|
def fast_path?(machine)
|
|
201
155
|
@fast_paths ||= {}
|
|
202
156
|
return @fast_paths[machine.dfa] if @fast_paths.key?(machine.dfa)
|
|
203
157
|
|
|
204
158
|
rules = machine.dfa.rule_ids.map { |rule_index| @rules.fetch(rule_index) }
|
|
205
159
|
@fast_paths[machine.dfa] = !@config.options[:allow_empty_match] &&
|
|
206
|
-
|
|
160
|
+
rules.none?(&:trailing) && rules.none?(&:capture_patterns) &&
|
|
207
161
|
rules.none? { |rule| rule.pattern_conditions.any? { |condition| condition&.bol_only || condition&.end_anchor } }
|
|
208
162
|
end
|
|
209
163
|
|
|
210
|
-
def reference_pattern?(pattern)
|
|
211
|
-
Regexp::Matcher.reference?(pattern, encoding: @config.encoding, unicode: @config.options[:unicode] == true)
|
|
212
|
-
end
|
|
213
|
-
|
|
214
|
-
def scan_firstmatch(position)
|
|
215
|
-
buffer = @lexer.buffer
|
|
216
|
-
@rules.sort_by(&:index).each do |rule|
|
|
217
|
-
next unless rule_active?(rule)
|
|
218
|
-
|
|
219
|
-
matches = rule.patterns.each_with_index.filter_map do |pattern, pattern_index|
|
|
220
|
-
condition = rule.pattern_conditions.fetch(pattern_index)
|
|
221
|
-
next if condition.bol_only && !@lexer.beginning_of_line?
|
|
222
|
-
|
|
223
|
-
regexp = pattern.is_a?(::Regexp) ? pattern : ::Regexp.new(::Regexp.escape(pattern.to_s))
|
|
224
|
-
streamed_match(
|
|
225
|
-
regexp, buffer, position, reference: reference_pattern?(regexp),
|
|
226
|
-
limit: :token, rule: rule
|
|
227
|
-
)
|
|
228
|
-
&.then { |match| [match, condition] }
|
|
229
|
-
end
|
|
230
|
-
match, condition = matches.select { |candidate| candidate[0].begin(0).zero? }
|
|
231
|
-
.max_by { |candidate| candidate[0][0].bytesize }
|
|
232
|
-
next unless match
|
|
233
|
-
|
|
234
|
-
end_position = position + match[0].bytesize
|
|
235
|
-
next unless @lexer.utf8_boundary?(end_position)
|
|
236
|
-
next if condition.end_anchor && !end_anchor_match?(buffer, end_position)
|
|
237
|
-
trailing = trailing_length(rule, buffer, end_position)
|
|
238
|
-
next if rule.trailing && trailing.nil?
|
|
239
|
-
|
|
240
|
-
ensure_token_size!(end_position, position, rule: rule)
|
|
241
|
-
return reusable_match(rule, position, end_position, end_position + (trailing || 0))
|
|
242
|
-
end
|
|
243
|
-
nil
|
|
244
|
-
end
|
|
245
|
-
|
|
246
164
|
def trailing_length(rule, buffer, position)
|
|
247
165
|
return 0 unless rule.trailing
|
|
248
166
|
|
|
@@ -279,218 +197,23 @@ module Flexr
|
|
|
279
197
|
)
|
|
280
198
|
end
|
|
281
199
|
|
|
282
|
-
def
|
|
283
|
-
pattern = regexp_pattern(pattern)
|
|
284
|
-
minimum = minimum_match_bytes(pattern)
|
|
285
|
-
loop do
|
|
286
|
-
subject, tail = stream_subject(buffer, position)
|
|
287
|
-
match = match_stream_pattern(pattern, subject, reference: reference)
|
|
288
|
-
ensure_stream_limit!(limit, position, match[0].bytesize, rule) if match
|
|
289
|
-
return match if match && match[0].bytesize < subject.bytesize
|
|
290
|
-
return match if match && %i[eof invalid].include?(tail)
|
|
291
|
-
if !match && subject.bytesize >= minimum && tail != :incomplete
|
|
292
|
-
first_byte = subject.getbyte(0)
|
|
293
|
-
return nil unless first_byte && possible_first_byte?(pattern, first_byte)
|
|
294
|
-
return nil if %i[eof invalid].include?(tail)
|
|
295
|
-
end
|
|
296
|
-
ensure_stream_limit!(limit, position, subject.bytesize, rule) unless match
|
|
297
|
-
@lexer.consume_step!
|
|
298
|
-
return match unless can_refill_match?(buffer)
|
|
299
|
-
end
|
|
300
|
-
end
|
|
301
|
-
|
|
302
|
-
def match_stream_pattern(pattern, subject, reference:)
|
|
303
|
-
Regexp::Matcher.match(pattern, subject, encoding: @config.encoding,
|
|
304
|
-
unicode: @config.options[:unicode] == true, reference: reference)
|
|
305
|
-
end
|
|
306
|
-
|
|
307
|
-
def stream_subject(buffer, position)
|
|
308
|
-
return [buffer.byteslice(position...buffer.bytesize).to_s.b, buffer.eof_loaded? ? :eof : :end] unless @lexer.utf8_input?
|
|
309
|
-
|
|
310
|
-
cursor = position
|
|
311
|
-
while cursor < buffer.bytesize
|
|
312
|
-
status, length = utf8_status(buffer, cursor)
|
|
313
|
-
break unless status == :complete
|
|
314
|
-
|
|
315
|
-
cursor += length
|
|
316
|
-
end
|
|
317
|
-
tail = if cursor < buffer.bytesize
|
|
318
|
-
utf8_status(buffer, cursor).first
|
|
319
|
-
elsif buffer.eof_loaded?
|
|
320
|
-
:eof
|
|
321
|
-
else
|
|
322
|
-
:end
|
|
323
|
-
end
|
|
324
|
-
subject = buffer.byteslice(position, cursor - position).to_s.dup.force_encoding(Encoding::UTF_8)
|
|
325
|
-
[subject, tail]
|
|
326
|
-
end
|
|
327
|
-
|
|
328
|
-
def utf8_status(buffer, position)
|
|
329
|
-
return [:eof, 0] if position >= buffer.bytesize
|
|
330
|
-
|
|
331
|
-
first = buffer.getbyte(position)
|
|
332
|
-
return [:complete, 1] if first <= 0x7f
|
|
333
|
-
return [:invalid, 1] unless first.between?(0xc2, 0xf4)
|
|
334
|
-
|
|
335
|
-
length = if first <= 0xdf
|
|
336
|
-
2
|
|
337
|
-
elsif first <= 0xef
|
|
338
|
-
3
|
|
339
|
-
else
|
|
340
|
-
4
|
|
341
|
-
end
|
|
342
|
-
return [:incomplete, length] unless buffer.ensure_available?(position + length)
|
|
343
|
-
return [:invalid, length] unless buffer.valid_utf8_at?(position)
|
|
344
|
-
|
|
345
|
-
[:complete, length]
|
|
346
|
-
end
|
|
347
|
-
|
|
348
|
-
def can_refill_match?(buffer)
|
|
349
|
-
buffer.ensure_available?(buffer.bytesize + 1)
|
|
350
|
-
end
|
|
351
|
-
|
|
352
|
-
def minimum_match_bytes(pattern)
|
|
353
|
-
return 1 if posix_pattern?(pattern)
|
|
354
|
-
|
|
355
|
-
ast = Regexp::Parser.new(pattern.source, options: pattern.options,
|
|
356
|
-
encoding: pattern.encoding, unicode: true).parse
|
|
357
|
-
minimum_ast_bytes(ast)
|
|
358
|
-
rescue CompileError, RegexpError
|
|
359
|
-
1
|
|
360
|
-
end
|
|
361
|
-
|
|
362
|
-
def regexp_pattern(pattern)
|
|
363
|
-
return pattern if pattern.is_a?(::Regexp)
|
|
364
|
-
|
|
365
|
-
::Regexp.new(::Regexp.escape(pattern.to_s))
|
|
366
|
-
end
|
|
367
|
-
|
|
368
|
-
def minimum_ast_bytes(node)
|
|
369
|
-
case node
|
|
370
|
-
when Regexp::AST::Empty, Regexp::AST::Anchor, Regexp::AST::Fail then 0
|
|
371
|
-
when Regexp::AST::ByteRange then 1
|
|
372
|
-
when Regexp::AST::CodepointRange then utf8_length(node.lo)
|
|
373
|
-
when Regexp::AST::CharClass
|
|
374
|
-
ranges = node.ranges.flat_map do |range|
|
|
375
|
-
range.first == Regexp::AST::Property ? property_ranges(range, ignorecase: range[3] == true) : [range]
|
|
376
|
-
end
|
|
377
|
-
ranges = complement_codepoint_ranges(ranges) if node.negated
|
|
378
|
-
ranges.map { |lo, _hi| utf8_length(lo) }.min || 1
|
|
379
|
-
when Regexp::AST::Seq then node.children.sum { |child| minimum_ast_bytes(child) }
|
|
380
|
-
when Regexp::AST::Alt then node.children.map { |child| minimum_ast_bytes(child) }.min || 0
|
|
381
|
-
when Regexp::AST::Repeat then node.minimum * minimum_ast_bytes(node.child)
|
|
382
|
-
else minimum_unknown_bytes(node)
|
|
383
|
-
end
|
|
384
|
-
end
|
|
385
|
-
|
|
386
|
-
def minimum_unknown_bytes(node)
|
|
387
|
-
return 0 if node.is_a?(Regexp::AST::Star)
|
|
388
|
-
|
|
389
|
-
1
|
|
390
|
-
end
|
|
391
|
-
|
|
392
|
-
def possible_first_byte?(pattern, byte)
|
|
393
|
-
return true if posix_pattern?(pattern)
|
|
394
|
-
|
|
395
|
-
ast = Regexp::Parser.new(pattern.source, options: pattern.options,
|
|
396
|
-
encoding: pattern.encoding, unicode: true).parse
|
|
397
|
-
binary = !@lexer.utf8_input?
|
|
398
|
-
first_byte_ranges(ast, binary: binary).any? { |lo, hi| byte.between?(lo, hi) }
|
|
399
|
-
rescue CompileError, RegexpError
|
|
400
|
-
true
|
|
401
|
-
end
|
|
402
|
-
|
|
403
|
-
def first_byte_ranges(node, binary: false)
|
|
404
|
-
case node
|
|
405
|
-
when Regexp::AST::Empty, Regexp::AST::Anchor, Regexp::AST::Fail then []
|
|
406
|
-
when Regexp::AST::ByteRange then [[node.lo, node.hi]]
|
|
407
|
-
when Regexp::AST::CodepointRange
|
|
408
|
-
ranges = [[node.lo, node.hi]]
|
|
409
|
-
return ranges if binary
|
|
410
|
-
|
|
411
|
-
ranges.flat_map { |lo, hi| Unicode::Utf8Splitter.split(lo, hi).map(&:first).map(&:first) }
|
|
412
|
-
.map { |value| [value, value] }
|
|
413
|
-
when Regexp::AST::CharClass
|
|
414
|
-
ranges = node.ranges.flat_map do |range|
|
|
415
|
-
range.first == Regexp::AST::Property ? property_ranges(range, ignorecase: range[3] == true) : [range]
|
|
416
|
-
end
|
|
417
|
-
ranges = complement_codepoint_ranges(ranges) if node.negated
|
|
418
|
-
return ranges if binary
|
|
419
|
-
|
|
420
|
-
ranges.flat_map { |lo, hi| Unicode::Utf8Splitter.split(lo, hi).map(&:first).map(&:first) }
|
|
421
|
-
.map { |value| [value, value] }
|
|
422
|
-
when Regexp::AST::Seq
|
|
423
|
-
ranges = []
|
|
424
|
-
node.children.each do |child|
|
|
425
|
-
ranges.concat(first_byte_ranges(child, binary: binary))
|
|
426
|
-
break unless nullable?(child)
|
|
427
|
-
end
|
|
428
|
-
ranges
|
|
429
|
-
when Regexp::AST::Alt
|
|
430
|
-
node.children.flat_map { |child| first_byte_ranges(child, binary: binary) }
|
|
431
|
-
when Regexp::AST::Star, Regexp::AST::Repeat
|
|
432
|
-
first_byte_ranges(node.child, binary: binary)
|
|
433
|
-
else
|
|
434
|
-
first_byte_fallback
|
|
435
|
-
end
|
|
436
|
-
end
|
|
437
|
-
|
|
438
|
-
def first_byte_fallback
|
|
439
|
-
[]
|
|
440
|
-
end
|
|
441
|
-
|
|
442
|
-
def nullable?(node)
|
|
443
|
-
case node
|
|
444
|
-
when Regexp::AST::Empty, Regexp::AST::Anchor, Regexp::AST::Star then true
|
|
445
|
-
when Regexp::AST::Repeat then node.minimum.zero? || nullable?(node.child)
|
|
446
|
-
when Regexp::AST::Seq then node.children.all? { |child| nullable?(child) }
|
|
447
|
-
when Regexp::AST::Alt then node.children.any? { |child| nullable?(child) }
|
|
448
|
-
else false
|
|
449
|
-
end
|
|
450
|
-
end
|
|
451
|
-
|
|
452
|
-
def utf8_length(codepoint)
|
|
453
|
-
return 1 if codepoint <= 0x7f
|
|
454
|
-
return 2 if codepoint <= 0x7ff
|
|
455
|
-
return 3 if codepoint <= 0xffff
|
|
456
|
-
|
|
457
|
-
4
|
|
458
|
-
end
|
|
459
|
-
|
|
460
|
-
def complement_codepoint_ranges(ranges)
|
|
461
|
-
result = []
|
|
462
|
-
cursor = 0
|
|
463
|
-
ranges.sort_by(&:first).each do |lo, hi|
|
|
464
|
-
result << [cursor, lo - 1] if cursor < lo
|
|
465
|
-
cursor = [cursor, hi + 1].max
|
|
466
|
-
end
|
|
467
|
-
result << [cursor, 0x10ffff] if cursor <= 0x10ffff
|
|
468
|
-
result
|
|
469
|
-
end
|
|
470
|
-
|
|
471
|
-
def property_ranges(range, ignorecase:)
|
|
472
|
-
ranges = Unicode::Property.ranges(range[2])
|
|
473
|
-
ranges = Unicode::CaseFold.merge(ranges.flat_map { |lo, hi| Unicode::CaseFold.ranges(lo, hi) }) if ignorecase
|
|
474
|
-
range[1] ? complement_codepoint_ranges(ranges) : ranges
|
|
475
|
-
end
|
|
476
|
-
|
|
477
|
-
def posix_pattern?(pattern)
|
|
478
|
-
pattern.is_a?(::Regexp) && pattern.source.match?(/\[:(?:\^)?[a-z]+:\]/)
|
|
479
|
-
end
|
|
480
|
-
|
|
481
|
-
def consider_acceptances(machine, state, cursor, position, buffer, best)
|
|
200
|
+
def consider_acceptances(machine, state, cursor, position, buffer, best, expected = nil)
|
|
482
201
|
machine.dfa.accepts[state].each do |acceptance|
|
|
483
202
|
candidate = @rules.fetch(acceptance.rule_index)
|
|
484
203
|
next if acceptance.bol_only && !@lexer.beginning_of_line?
|
|
485
204
|
next unless @lexer.utf8_boundary?(cursor)
|
|
486
205
|
next unless !acceptance.end_anchor || end_anchor_match?(buffer, cursor)
|
|
206
|
+
next if expected && !expected_rule?(candidate, expected, buffer, position, cursor)
|
|
487
207
|
|
|
488
208
|
trailing_size = trailing_length(candidate, buffer, cursor)
|
|
489
209
|
next if candidate.trailing && trailing_size.nil?
|
|
490
210
|
|
|
491
211
|
total_end = cursor + (trailing_size || 0)
|
|
492
212
|
next if best && total_end < best.total_end_pos
|
|
493
|
-
|
|
213
|
+
if best && total_end == best.total_end_pos
|
|
214
|
+
next if candidate.loses_priority_tie?(best.rule)
|
|
215
|
+
next if candidate.capture_patterns && candidate.equal?(best.rule) && cursor == best.end_pos
|
|
216
|
+
end
|
|
494
217
|
|
|
495
218
|
ensure_token_size!(cursor, position, rule: candidate)
|
|
496
219
|
best = @match ||= Match.new
|
|
@@ -498,10 +221,27 @@ module Flexr
|
|
|
498
221
|
best.start_pos = position
|
|
499
222
|
best.end_pos = cursor
|
|
500
223
|
best.total_end_pos = total_end
|
|
224
|
+
best.capture = candidate.capture_patterns&.[](acceptance.pattern_index)
|
|
501
225
|
end
|
|
502
226
|
best
|
|
503
227
|
end
|
|
504
228
|
|
|
229
|
+
def expected_rule?(rule, expected, buffer, start_pos, end_pos = nil)
|
|
230
|
+
return true if rule.skip? || rule.channel
|
|
231
|
+
return true if @config.options[:indentation] && @lexer.__flexr_layout_candidate?(expected, start_pos, end_pos)
|
|
232
|
+
return rule.emits.any? { |type| expected.key?(type) } if rule.emits
|
|
233
|
+
return true unless rule.emit?
|
|
234
|
+
|
|
235
|
+
type = rule.action[1]
|
|
236
|
+
if (table = @config.options[:keyword_tables]&.[](type))
|
|
237
|
+
return expected.key?(type) || table.any? { |_, entry| expected.key?(entry.first) } unless end_pos
|
|
238
|
+
|
|
239
|
+
lexeme = buffer.byteslice(start_pos, end_pos - start_pos).force_encoding(Encoding::UTF_8)
|
|
240
|
+
type = table[lexeme]&.first || type
|
|
241
|
+
end
|
|
242
|
+
expected.key?(type)
|
|
243
|
+
end
|
|
244
|
+
|
|
505
245
|
def acceleration_enabled?
|
|
506
246
|
mode = @config.options.fetch(:accel, :auto)
|
|
507
247
|
mode != :none && !(mode == :auto && @auto_acceleration_off)
|
|
@@ -602,24 +342,6 @@ module Flexr
|
|
|
602
342
|
text_start = @lexer.more_text_start || position
|
|
603
343
|
@lexer.defer_token_size_check!(end_position - text_start, rule: rule)
|
|
604
344
|
end
|
|
605
|
-
|
|
606
|
-
def ensure_stream_limit!(limit, position, size, rule)
|
|
607
|
-
if limit == :lookahead
|
|
608
|
-
@lexer.ensure_lookahead_size!(size, rule: rule)
|
|
609
|
-
else
|
|
610
|
-
ensure_token_size!(position + size, position, rule: rule)
|
|
611
|
-
end
|
|
612
|
-
end
|
|
613
|
-
|
|
614
|
-
def rule_active?(rule)
|
|
615
|
-
return true if rule.states.include?(:initial) && @lexer.state == :initial
|
|
616
|
-
|
|
617
|
-
state = @config.states.fetch(@lexer.state)
|
|
618
|
-
return true if state.inclusive && rule.states.include?(:initial)
|
|
619
|
-
|
|
620
|
-
rule.states.include?(@lexer.state)
|
|
621
|
-
end
|
|
622
|
-
|
|
623
345
|
end
|
|
624
346
|
end
|
|
625
347
|
end
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Flexr
|
|
4
|
+
class Recognizer
|
|
5
|
+
def initialize(compiled, config, state)
|
|
6
|
+
@dfa = shareable(compiled.machines.fetch(state).dfa)
|
|
7
|
+
rules = compiled.rules
|
|
8
|
+
@identities = rules.map { |rule| rule.emit? ? rule.action[1] : rule.index }.freeze
|
|
9
|
+
ranks = Array.new(rules.length)
|
|
10
|
+
rules.sort_by { |rule| [-(rule.respond_to?(:priority) ? rule.priority : 0), rule.index] }
|
|
11
|
+
.each_with_index { |rule, rank| ranks[rule.index] = rank }
|
|
12
|
+
@ranks = ranks.freeze
|
|
13
|
+
trailing = Array.new(rules.length)
|
|
14
|
+
@dfa.rule_ids.each do |index|
|
|
15
|
+
rule = rules.fetch(index)
|
|
16
|
+
next unless rule.trailing
|
|
17
|
+
|
|
18
|
+
dfa = rule.trailing_dfa || Flexr.compile_pattern(
|
|
19
|
+
rule.trailing, encoding: config.encoding, options: config.options.slice(:unicode, :max_dfa_states)
|
|
20
|
+
)
|
|
21
|
+
trailing[index] = shareable(dfa)
|
|
22
|
+
end
|
|
23
|
+
@trailing = trailing.freeze
|
|
24
|
+
@utf8 = config.encoding == Encoding::UTF_8
|
|
25
|
+
freeze
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def match(input, offset)
|
|
29
|
+
raise TypeError, "recognizer input must be a String and offset an Integer" unless input.is_a?(String) && offset.is_a?(Integer)
|
|
30
|
+
raise RangeError, "recognizer offset is outside the input" unless offset.between?(0, input.bytesize)
|
|
31
|
+
return if offset == input.bytesize
|
|
32
|
+
|
|
33
|
+
best_rule = best_finish = best_total = nil
|
|
34
|
+
walk(@dfa, input, offset) do |rule, finish|
|
|
35
|
+
dfa = @trailing[rule]
|
|
36
|
+
total = dfa ? trailing_end(dfa, input, finish) : finish
|
|
37
|
+
next unless total
|
|
38
|
+
next if best_total && (total < best_total || (total == best_total && @ranks[rule] > @ranks[best_rule]))
|
|
39
|
+
|
|
40
|
+
best_rule = rule
|
|
41
|
+
best_finish = finish
|
|
42
|
+
best_total = total
|
|
43
|
+
end
|
|
44
|
+
return unless best_rule
|
|
45
|
+
|
|
46
|
+
identity = @identities.fetch(best_rule)
|
|
47
|
+
block_given? ? yield(identity, best_finish) : [identity, best_finish]
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
def shareable(dfa)
|
|
53
|
+
Ractor.shareable?(dfa) ? dfa : Generated.shareable_dfa(dfa)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def trailing_end(dfa, input, start)
|
|
57
|
+
ending = nil
|
|
58
|
+
walk(dfa, input, start) { |_, finish| ending = finish }
|
|
59
|
+
ending
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def walk(dfa, input, start)
|
|
63
|
+
state = dfa.start
|
|
64
|
+
cursor = boundary = start
|
|
65
|
+
bol = start.zero? || input.getbyte(start - 1) == 0x0a
|
|
66
|
+
while state
|
|
67
|
+
byte = input.getbyte(cursor)
|
|
68
|
+
if cursor == boundary
|
|
69
|
+
boundary = byte && scalar_end(input, cursor, byte)
|
|
70
|
+
return if cursor == start && byte && !boundary
|
|
71
|
+
|
|
72
|
+
dfa.accepts.fetch(state).each do |acceptance|
|
|
73
|
+
next if acceptance.bol_only && !bol
|
|
74
|
+
next if acceptance.end_anchor && byte && byte != 0x0a
|
|
75
|
+
|
|
76
|
+
yield acceptance.rule_index, cursor
|
|
77
|
+
end
|
|
78
|
+
return unless boundary
|
|
79
|
+
end
|
|
80
|
+
state = dfa.transition(state, byte)
|
|
81
|
+
cursor += 1
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def scalar_end(input, cursor, first)
|
|
86
|
+
return cursor + 1 if !@utf8 || first < 0x80
|
|
87
|
+
|
|
88
|
+
second = input.getbyte(cursor + 1)
|
|
89
|
+
return unless continuation?(second)
|
|
90
|
+
return cursor + 2 if first.between?(0xc2, 0xdf)
|
|
91
|
+
|
|
92
|
+
if first.between?(0xe0, 0xef)
|
|
93
|
+
return if (first == 0xe0 && second < 0xa0) || (first == 0xed && second >= 0xa0)
|
|
94
|
+
return cursor + 3 if continuation?(input.getbyte(cursor + 2))
|
|
95
|
+
elsif first.between?(0xf0, 0xf4)
|
|
96
|
+
return if (first == 0xf0 && second < 0x90) || (first == 0xf4 && second >= 0x90)
|
|
97
|
+
return cursor + 4 if continuation?(input.getbyte(cursor + 2)) && continuation?(input.getbyte(cursor + 3))
|
|
98
|
+
end
|
|
99
|
+
nil
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def continuation?(byte)
|
|
103
|
+
byte && (byte & 0xc0) == 0x80
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
data/lib/flexr/runtime.rb
CHANGED
|
@@ -27,6 +27,7 @@ require_relative "automaton/types"
|
|
|
27
27
|
require_relative "automaton/byte_class_set"
|
|
28
28
|
require_relative "automaton/nfa"
|
|
29
29
|
require_relative "automaton/dfa"
|
|
30
|
+
require_relative "automaton/reference_dfa"
|
|
30
31
|
require_relative "automaton/compiler"
|
|
31
32
|
require_relative "automaton/analysis"
|
|
32
33
|
require_relative "automaton/minimizer"
|
|
@@ -37,12 +38,21 @@ require_relative "runtime/location"
|
|
|
37
38
|
require_relative "runtime/token"
|
|
38
39
|
require_relative "runtime/buffer"
|
|
39
40
|
require_relative "runtime/errors"
|
|
41
|
+
require_relative "runtime/firstmatch"
|
|
40
42
|
require_relative "runtime/interpreter"
|
|
43
|
+
require_relative "runtime/recognizer"
|
|
41
44
|
require_relative "runtime/core"
|
|
45
|
+
require_relative "runtime/checkpoint"
|
|
46
|
+
require_relative "runtime/input_stack"
|
|
47
|
+
require_relative "runtime/indentation"
|
|
42
48
|
require_relative "runtime/channels"
|
|
49
|
+
require_relative "runtime/captures"
|
|
50
|
+
require_relative "runtime/delegation"
|
|
51
|
+
require_relative "runtime/delimiters"
|
|
43
52
|
require_relative "dsl"
|
|
44
53
|
require_relative "keywords"
|
|
45
54
|
require_relative "lexer"
|
|
55
|
+
require_relative "rules"
|
|
46
56
|
require_relative "generated"
|
|
47
57
|
|
|
48
58
|
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "open3"
|
|
4
|
+
require "rbconfig"
|
|
5
|
+
require "tmpdir"
|
|
6
|
+
|
|
7
|
+
module Flexr
|
|
8
|
+
class Skeleton
|
|
9
|
+
class Verification
|
|
10
|
+
MODES = %i[runtime generated standalone].freeze
|
|
11
|
+
RUNNER = <<~RUBY
|
|
12
|
+
require "json"
|
|
13
|
+
mode, path, origin, class_name, snapshot_path, result_path = ARGV
|
|
14
|
+
request = JSON.parse(STDIN.binmode.read)
|
|
15
|
+
require "flexr" unless mode == "standalone"
|
|
16
|
+
if mode == "runtime"
|
|
17
|
+
load origin
|
|
18
|
+
else
|
|
19
|
+
eval(File.read(path, encoding: "UTF-8"), TOPLEVEL_BINDING, origin, 1)
|
|
20
|
+
end
|
|
21
|
+
require snapshot_path
|
|
22
|
+
snapshot = Flexr::Testing::Snapshot
|
|
23
|
+
klass = class_name.split("::").reject(&:empty?).inject(Object) { |scope, name| scope.const_get(name, false) }
|
|
24
|
+
config = klass.__flexr_config
|
|
25
|
+
if mode == "runtime"
|
|
26
|
+
request.fetch("runtime_options").each { |method, value| klass.public_send(method, value.to_sym) }
|
|
27
|
+
limit = request["max_dfa_states"]
|
|
28
|
+
config.options[:max_dfa_states] = limit if limit
|
|
29
|
+
end
|
|
30
|
+
outcomes = request.fetch("cases").map do |item|
|
|
31
|
+
value = item.fetch("input")
|
|
32
|
+
input = value.is_a?(String) ? value : value.fetch("bytes").pack("C*")
|
|
33
|
+
lexer = klass.new(input.force_encoding(config.encoding), filename: origin)
|
|
34
|
+
lexer.begin_state(item.fetch("state").to_sym)
|
|
35
|
+
tokens = []
|
|
36
|
+
error = begin
|
|
37
|
+
while (token = lexer.next_token)
|
|
38
|
+
tokens << token
|
|
39
|
+
end
|
|
40
|
+
nil
|
|
41
|
+
rescue StandardError => caught
|
|
42
|
+
report = { class: caught.class.name, message: snapshot.encode(caught.message) }
|
|
43
|
+
%i[filename byte_pos line text rule].each do |field|
|
|
44
|
+
report[field] = snapshot.encode(caught.public_send(field)) if caught.respond_to?(field)
|
|
45
|
+
end
|
|
46
|
+
report[:code] = caught.diagnostic&.code if caught.respond_to?(:diagnostic)
|
|
47
|
+
report
|
|
48
|
+
end
|
|
49
|
+
{ tokens: snapshot.encode(tokens), state: lexer.state, byte_pos: lexer.byte_pos, error: error }
|
|
50
|
+
end
|
|
51
|
+
File.binwrite(result_path, JSON.generate(outcomes))
|
|
52
|
+
RUBY
|
|
53
|
+
|
|
54
|
+
def initialize(spec, class_name:, options:)
|
|
55
|
+
@spec = File.expand_path(spec)
|
|
56
|
+
@class_name = class_name
|
|
57
|
+
@options = options
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def compare(cases)
|
|
61
|
+
outcomes = Dir.mktmpdir("flexr-skeleton-verify-") do |directory|
|
|
62
|
+
request = {
|
|
63
|
+
cases: cases.map { |item| item.slice(:state, :input) },
|
|
64
|
+
runtime_options: @options.slice(:backend, :token_kind, :accel),
|
|
65
|
+
max_dfa_states: @options[:max_dfa_states]
|
|
66
|
+
}
|
|
67
|
+
MODES.to_h { |mode| [mode, run_mode(mode, directory, request)] }
|
|
68
|
+
end
|
|
69
|
+
mismatches = cases.each_with_index.filter_map do |item, index|
|
|
70
|
+
results = outcomes.transform_values { |entries| entries.fetch(index) }
|
|
71
|
+
next if results.values.uniq.one?
|
|
72
|
+
|
|
73
|
+
item.slice(:state, :input).merge(outcomes: results)
|
|
74
|
+
end
|
|
75
|
+
{ modes: MODES, compared: cases.size, matched: cases.size - mismatches.size, mismatches: mismatches }
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
private
|
|
79
|
+
|
|
80
|
+
def run_mode(mode, directory, request)
|
|
81
|
+
source_path = @spec
|
|
82
|
+
if mode != :runtime
|
|
83
|
+
source_path = File.join(directory, "#{mode}.rb")
|
|
84
|
+
options = @options.merge(standalone: mode == :standalone)
|
|
85
|
+
Generator.new(@spec, output: source_path, options: options).generate
|
|
86
|
+
end
|
|
87
|
+
result_path = File.join(directory, "#{mode}.json")
|
|
88
|
+
snapshot_path = File.expand_path("../testing.rb", __dir__)
|
|
89
|
+
command = [RbConfig.ruby]
|
|
90
|
+
command.push("-I", File.expand_path("../..", __dir__)) unless mode == :standalone
|
|
91
|
+
command.push("-e", RUNNER, mode.to_s, source_path, @spec, @class_name, snapshot_path, result_path)
|
|
92
|
+
_, stderr, status = Open3.capture3(*command, stdin_data: JSON.generate(request), binmode: true)
|
|
93
|
+
unless status.success? && File.file?(result_path)
|
|
94
|
+
detail = stderr.lines.first&.strip
|
|
95
|
+
detail = "worker exited without producing a report (#{status})" if detail.nil? || detail.empty?
|
|
96
|
+
raise CompileError, "skeleton #{mode} verification failed: #{detail}"
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
JSON.parse(File.binread(result_path))
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|