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
data/lib/flexr/cli.rb
CHANGED
|
@@ -9,7 +9,7 @@ require_relative "migration"
|
|
|
9
9
|
|
|
10
10
|
module Flexr
|
|
11
11
|
module CLI
|
|
12
|
-
COMMANDS = %w[check stats tokens dot explain trace bench import].freeze
|
|
12
|
+
COMMANDS = %w[check fix stats tokens dot explain trace skeleton bench import new].freeze
|
|
13
13
|
EXIT_OK = 0
|
|
14
14
|
EXIT_FAILURE = 1
|
|
15
15
|
EXIT_USAGE = 2
|
|
@@ -18,20 +18,23 @@ module Flexr
|
|
|
18
18
|
|
|
19
19
|
def run(argv, out: $stdout, err: $stderr)
|
|
20
20
|
args = argv.dup
|
|
21
|
-
|
|
21
|
+
command = COMMANDS.include?(args.first) ? args.shift.to_sym : :generate
|
|
22
|
+
if args.include?("--help") || args.include?("-h")
|
|
23
|
+
return command == :new ? new_project_usage(out) : usage(out)
|
|
24
|
+
end
|
|
22
25
|
return version(out) if args.delete("--version")
|
|
26
|
+
return new_project(args, out) if command == :new
|
|
23
27
|
|
|
24
|
-
|
|
25
|
-
options, output, rule_number, benchmark_args, positionals, trace_args = parse(args, command)
|
|
28
|
+
options, output, rule_number, benchmark_args, positionals, command_args = parse(args, command)
|
|
26
29
|
raise ArgumentError, "lrama format is only valid for tokens" if options.format == :lrama && command != :tokens
|
|
27
30
|
spec = positionals.shift
|
|
28
31
|
raise ArgumentError, "a SPEC.rb path is required" unless spec
|
|
29
32
|
raise ArgumentError, "unexpected argument: #{positionals.first}" unless positionals.empty?
|
|
30
33
|
|
|
31
|
-
execute(command, spec, options, output, rule_number, benchmark_args, out, err,
|
|
34
|
+
execute(command, spec, options, output, rule_number, benchmark_args, out, err, command_args: command_args)
|
|
32
35
|
rescue ArgumentError => e
|
|
33
36
|
err.puts "error: #{e.message}"
|
|
34
|
-
usage(err, status: EXIT_USAGE)
|
|
37
|
+
command == :new ? new_project_usage(err, status: EXIT_USAGE) : usage(err, status: EXIT_USAGE)
|
|
35
38
|
rescue Flexr::Error, Errno::ENOENT, Errno::EACCES, SyntaxError => e
|
|
36
39
|
err.puts render_error(e, options: options, path: spec,
|
|
37
40
|
normalize_source: command == :generate || (command == :check && options&.eval_mode))
|
|
@@ -41,13 +44,42 @@ module Flexr
|
|
|
41
44
|
EXIT_FAILURE
|
|
42
45
|
end
|
|
43
46
|
|
|
47
|
+
def new_project(args, out)
|
|
48
|
+
require "optparse"
|
|
49
|
+
require_relative "project_template"
|
|
50
|
+
|
|
51
|
+
parser = nil
|
|
52
|
+
OptionParser.new do |options|
|
|
53
|
+
options.require_exact = true
|
|
54
|
+
options.on("--parser PARSER") do |value|
|
|
55
|
+
raise ArgumentError, "--parser may only be given once" if parser
|
|
56
|
+
|
|
57
|
+
parser = value
|
|
58
|
+
end
|
|
59
|
+
end.parse!(args)
|
|
60
|
+
raise ArgumentError, "a project name is required" if args.empty?
|
|
61
|
+
raise ArgumentError, "unexpected argument: #{args[1]}" if args.length > 1
|
|
62
|
+
|
|
63
|
+
ProjectTemplate.create(args.first, parser: parser)
|
|
64
|
+
out.puts "Created #{args.first} with #{parser}. See #{args.first}/README.md to get started."
|
|
65
|
+
EXIT_OK
|
|
66
|
+
rescue OptionParser::ParseError => e
|
|
67
|
+
raise ArgumentError, e.message
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def new_project_usage(out, status: EXIT_OK)
|
|
71
|
+
out.puts "Usage: flexr new NAME --parser lrama|racc"
|
|
72
|
+
out.puts "Create a calculator project in a new directory in the current directory."
|
|
73
|
+
status
|
|
74
|
+
end
|
|
75
|
+
|
|
44
76
|
def parse(args, command)
|
|
45
77
|
options = Options.default
|
|
46
78
|
output = nil
|
|
47
79
|
rule_number = nil
|
|
48
80
|
benchmark_args = []
|
|
49
81
|
positionals = []
|
|
50
|
-
|
|
82
|
+
command_args = {}
|
|
51
83
|
parsing_options = true
|
|
52
84
|
|
|
53
85
|
until args.empty?
|
|
@@ -73,7 +105,11 @@ module Flexr
|
|
|
73
105
|
when "--standalone"
|
|
74
106
|
options.set(:standalone, true)
|
|
75
107
|
when "--eval"
|
|
108
|
+
raise ArgumentError, "fix requires a statically readable spec" if command == :fix
|
|
76
109
|
options.eval_mode = true
|
|
110
|
+
when "--dry-run"
|
|
111
|
+
raise ArgumentError, "--dry-run is only valid for fix" unless command == :fix
|
|
112
|
+
options.dry_run = true
|
|
77
113
|
when "--table-compression"
|
|
78
114
|
options.set(:table_compression, required_argument!(args, argument).to_sym)
|
|
79
115
|
when "--table-format"
|
|
@@ -91,23 +127,29 @@ module Flexr
|
|
|
91
127
|
when "--target"
|
|
92
128
|
raise ArgumentError, "--target is only valid for the check command" unless command == :check
|
|
93
129
|
options.target = required_argument!(args, argument)
|
|
130
|
+
when "--from"
|
|
131
|
+
raise ArgumentError, "--from is only valid for import" unless command == :import
|
|
132
|
+
options.import_format = required_argument!(args, argument).to_sym
|
|
94
133
|
when "--rule"
|
|
95
134
|
rule_number = Integer(required_argument!(args, argument), 10)
|
|
96
135
|
raise ArgumentError, "--rule must be non-negative" if rule_number.negative?
|
|
97
136
|
when "--grammar"
|
|
98
137
|
raise ArgumentError, "--grammar is only valid for check" unless command == :check
|
|
99
138
|
options.grammar = required_argument!(args, argument)
|
|
139
|
+
when "--verify"
|
|
140
|
+
raise ArgumentError, "--verify is only valid for the skeleton command" unless command == :skeleton
|
|
141
|
+
command_args[:verify] = true
|
|
100
142
|
when "--input", "--generated"
|
|
101
143
|
raise ArgumentError, "#{argument} is only valid for the trace command" unless command == :trace
|
|
102
144
|
if argument == "--generated"
|
|
103
|
-
|
|
145
|
+
command_args[:generated] = true
|
|
104
146
|
else
|
|
105
|
-
|
|
147
|
+
command_args[:input] = args.shift || raise(ArgumentError, "--input requires a value")
|
|
106
148
|
end
|
|
107
149
|
when "--input-file"
|
|
108
150
|
value = required_argument!(args, argument)
|
|
109
151
|
if command == :trace
|
|
110
|
-
|
|
152
|
+
command_args[:input_file] = value
|
|
111
153
|
elsif command == :bench
|
|
112
154
|
benchmark_args.push(argument, value)
|
|
113
155
|
else
|
|
@@ -122,20 +164,23 @@ module Flexr
|
|
|
122
164
|
end
|
|
123
165
|
|
|
124
166
|
options.validate!
|
|
125
|
-
raise ArgumentError, "
|
|
167
|
+
raise ArgumentError, "skeleton requires static compilation; remove --eval" if command == :skeleton && options.eval_mode
|
|
168
|
+
raise ArgumentError, "choose --input or --input-file" if command_args.key?(:input) && command_args[:input_file]
|
|
126
169
|
raise ArgumentError, "--generated requires --input or --input-file" if
|
|
127
|
-
|
|
128
|
-
[options, output, rule_number, benchmark_args, positionals,
|
|
170
|
+
command_args[:generated] && !command_args.key?(:input) && !command_args[:input_file]
|
|
171
|
+
[options, output, rule_number, benchmark_args, positionals, command_args]
|
|
129
172
|
rescue ArgumentError => e
|
|
130
173
|
raise e if e.message.start_with?("unsupported ", "--rule")
|
|
131
174
|
|
|
132
175
|
raise ArgumentError, "invalid option value: #{e.message}"
|
|
133
176
|
end
|
|
134
177
|
|
|
135
|
-
def execute(command, spec, options, output, rule_number, benchmark_args, out, err,
|
|
178
|
+
def execute(command, spec, options, output, rule_number, benchmark_args, out, err, command_args: {})
|
|
136
179
|
case command
|
|
137
180
|
when :check
|
|
138
181
|
check(spec, options, out)
|
|
182
|
+
when :fix
|
|
183
|
+
fix(spec, options, out)
|
|
139
184
|
when :stats
|
|
140
185
|
print_stats(spec, options, out)
|
|
141
186
|
when :tokens
|
|
@@ -153,15 +198,17 @@ module Flexr
|
|
|
153
198
|
when :explain
|
|
154
199
|
print_explanation(spec, rule_number, out)
|
|
155
200
|
when :trace
|
|
156
|
-
if
|
|
157
|
-
print_input_trace(spec, options,
|
|
201
|
+
if command_args.key?(:input) || command_args[:input_file]
|
|
202
|
+
print_input_trace(spec, options, command_args, out)
|
|
158
203
|
else
|
|
159
204
|
print_trace(spec, options, out)
|
|
160
205
|
end
|
|
206
|
+
when :skeleton
|
|
207
|
+
print_skeleton(spec, options, out, verify: command_args[:verify])
|
|
161
208
|
when :bench
|
|
162
209
|
run_benchmark(spec, options, benchmark_args, out)
|
|
163
210
|
when :import
|
|
164
|
-
result = Importer.import(spec)
|
|
211
|
+
result = Importer.import(spec, from: options.import_format)
|
|
165
212
|
result.warnings.each { |warning| err.puts "warning: #{warning}" }
|
|
166
213
|
return EXIT_FAILURE unless result.complete?
|
|
167
214
|
|
|
@@ -179,6 +226,39 @@ module Flexr
|
|
|
179
226
|
end
|
|
180
227
|
end
|
|
181
228
|
|
|
229
|
+
def fix(spec, options, out)
|
|
230
|
+
require_relative "source/fixes"
|
|
231
|
+
path = File.realpath(spec)
|
|
232
|
+
parsed = read_spec(path)
|
|
233
|
+
original = parsed
|
|
234
|
+
begin
|
|
235
|
+
compiled = compile(parsed, overrides: { warn_level: :all })
|
|
236
|
+
rescue CompileError => e
|
|
237
|
+
repairs = Source::Fixes.regexp_edits(parsed, e.diagnostic)
|
|
238
|
+
if repairs.empty?
|
|
239
|
+
index = parsed.rules.index { |rule| rule.span == e.diagnostic&.location }
|
|
240
|
+
e.diagnostic.location = original.rules.fetch(index).span if index
|
|
241
|
+
raise
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
repaired = Source::Fixes.apply(parsed.source, repairs)
|
|
245
|
+
parsed = Source::PrismReader.new(repaired, path: path).read
|
|
246
|
+
retry
|
|
247
|
+
end
|
|
248
|
+
edits = Source::Fixes.edits(parsed, compiled)
|
|
249
|
+
fixed = Source::Fixes.apply(parsed.source, edits)
|
|
250
|
+
compile(Source::PrismReader.new(fixed, path: path).read) unless edits.empty?
|
|
251
|
+
|
|
252
|
+
if options.dry_run
|
|
253
|
+
out.write(fixed)
|
|
254
|
+
elsif fixed != original.source
|
|
255
|
+
raise CompileError, "spec changed while preparing fixes: #{spec}" unless File.binread(path) == original.source.b
|
|
256
|
+
|
|
257
|
+
ArtifactWriter.write!(path, fixed)
|
|
258
|
+
end
|
|
259
|
+
EXIT_OK
|
|
260
|
+
end
|
|
261
|
+
|
|
182
262
|
def check(spec, options, out)
|
|
183
263
|
compiled = if options.eval_mode
|
|
184
264
|
generator = Generator.new(spec, eval_mode: true, options: options.generator_options)
|
|
@@ -387,6 +467,21 @@ module Flexr
|
|
|
387
467
|
EXIT_OK
|
|
388
468
|
end
|
|
389
469
|
|
|
470
|
+
def print_skeleton(spec, options, out, verify:)
|
|
471
|
+
require_relative "skeleton"
|
|
472
|
+
parsed = read_spec(spec)
|
|
473
|
+
compiled = compile(parsed, overrides: options.overrides)
|
|
474
|
+
report = Skeleton.new(compiled, encoding: parsed.config.fetch(:encoding)).report
|
|
475
|
+
if verify
|
|
476
|
+
require_relative "skeleton/verification"
|
|
477
|
+
report[:verification] = Skeleton::Verification.new(
|
|
478
|
+
spec, class_name: parsed.class_name, options: options.generator_options
|
|
479
|
+
).compare(report.fetch(:cases))
|
|
480
|
+
end
|
|
481
|
+
out.puts(options.format == :json ? JSON.pretty_generate(report) : Skeleton.human(report))
|
|
482
|
+
report.dig(:verification, :mismatches)&.any? ? EXIT_FAILURE : EXIT_OK
|
|
483
|
+
end
|
|
484
|
+
|
|
390
485
|
def run_benchmark(spec, options, benchmark_args, out)
|
|
391
486
|
require_relative "../../benchmark/run"
|
|
392
487
|
args = ["--spec", spec, *benchmark_args]
|
|
@@ -414,6 +509,7 @@ module Flexr
|
|
|
414
509
|
if (tables = config_options[:keyword_tables])
|
|
415
510
|
klass.__flexr_config.options[:keyword_tables] = tables
|
|
416
511
|
end
|
|
512
|
+
klass.indentation(**config_options[:indentation]) if config_options[:indentation]
|
|
417
513
|
config_options.slice(*Configuration::BOOLEAN_OPTIONS).each do |name, value|
|
|
418
514
|
klass.option(name) if value
|
|
419
515
|
end
|
|
@@ -478,8 +574,9 @@ module Flexr
|
|
|
478
574
|
def usage(out, status: EXIT_OK)
|
|
479
575
|
out.puts <<~USAGE
|
|
480
576
|
Usage: flexr [COMMAND] SPEC.rb [options]
|
|
577
|
+
flexr new NAME --parser lrama|racc
|
|
481
578
|
|
|
482
|
-
Commands: check, stats, tokens, dot, explain, trace, bench, import
|
|
579
|
+
Commands: check, fix, stats, tokens, dot, explain, trace, skeleton, bench, import, new
|
|
483
580
|
Options:
|
|
484
581
|
-o, --output PATH generated output path
|
|
485
582
|
-b, --backend NAME table | direct | firstmatch | auto
|
|
@@ -498,7 +595,10 @@ module Flexr
|
|
|
498
595
|
--input STRING trace input (executes SPEC actions)
|
|
499
596
|
--input-file PATH trace or benchmark input
|
|
500
597
|
--generated trace generated scanner
|
|
598
|
+
--verify verify skeleton across all modes (executes SPEC actions)
|
|
501
599
|
--target 2.0 report migration requirements (static check only)
|
|
600
|
+
--dry-run print the fixed spec without writing (fix only)
|
|
601
|
+
--from oedipus_lex read an oedipus_lex .rex file (import only)
|
|
502
602
|
USAGE
|
|
503
603
|
status
|
|
504
604
|
end
|
data/lib/flexr/codegen/table.rb
CHANGED
|
@@ -28,6 +28,7 @@ module Flexr
|
|
|
28
28
|
def source(indent: " ", direct: false)
|
|
29
29
|
lines = []
|
|
30
30
|
whole_tokens = compiled.machines.values.any?(&:whole_tokens)
|
|
31
|
+
captures = compiled.rules.any?(&:capture_patterns)
|
|
31
32
|
scan_cache = direct ? "@__flexr_generated_scan_layout" : "@__flexr_generated_scan_fast"
|
|
32
33
|
fast_path = direct ? "__flexr_generated_direct_layout" : "__flexr_generated_fast_path?"
|
|
33
34
|
lines << "#{indent}def scan_one"
|
|
@@ -130,6 +131,7 @@ module Flexr
|
|
|
130
131
|
lines << "#{indent} best.start_pos = position"
|
|
131
132
|
lines << "#{indent} best.end_pos = cursor"
|
|
132
133
|
lines << "#{indent} best.total_end_pos = cursor"
|
|
134
|
+
lines << "#{indent} best.capture = rule.capture_patterns&.[](acceptance.pattern_index)" if captures
|
|
133
135
|
lines << "#{indent} end"
|
|
134
136
|
lines << "#{indent} return best"
|
|
135
137
|
lines << "#{indent} end"
|
|
@@ -170,6 +172,7 @@ module Flexr
|
|
|
170
172
|
lines << "#{indent} best.start_pos = position"
|
|
171
173
|
lines << "#{indent} best.end_pos = cursor"
|
|
172
174
|
lines << "#{indent} best.total_end_pos = cursor"
|
|
175
|
+
lines << "#{indent} best.capture = rule.capture_patterns&.[](acceptance.pattern_index)" if captures
|
|
173
176
|
lines << "#{indent} end"
|
|
174
177
|
lines << "#{indent} best"
|
|
175
178
|
lines << "#{indent}end"
|
|
@@ -268,11 +271,15 @@ module Flexr
|
|
|
268
271
|
lines << "#{indent} next if best && cursor == best.total_end_pos &&"
|
|
269
272
|
priority_check = compiled.rules.any? { |rule| !rule.priority.zero? }
|
|
270
273
|
lines << "#{indent} #{priority_check ? 'rule.loses_priority_tie?(best.rule)' : 'rule.index > best.rule.index'}"
|
|
274
|
+
if captures
|
|
275
|
+
lines << "#{indent} next if best && cursor == best.end_pos && rule.equal?(best.rule) && rule.capture_patterns"
|
|
276
|
+
end
|
|
271
277
|
lines << "#{indent} best ||= (@__flexr_generated_match ||= Flexr::Runtime::Match.new)"
|
|
272
278
|
lines << "#{indent} best.rule = rule"
|
|
273
279
|
lines << "#{indent} best.start_pos = start_position"
|
|
274
280
|
lines << "#{indent} best.end_pos = cursor"
|
|
275
281
|
lines << "#{indent} best.total_end_pos = cursor"
|
|
282
|
+
lines << "#{indent} best.capture = rule.capture_patterns&.[](acceptance.pattern_index)" if captures
|
|
276
283
|
lines << "#{indent} end"
|
|
277
284
|
lines << "#{indent} best"
|
|
278
285
|
lines << "#{indent}end"
|
data/lib/flexr/configuration.rb
CHANGED
|
@@ -5,7 +5,7 @@ module Flexr
|
|
|
5
5
|
BACKENDS = %i[table direct firstmatch auto].freeze
|
|
6
6
|
TOKEN_KINDS = %i[array struct yield].freeze
|
|
7
7
|
ACCELERATORS = %i[auto strscan regexp none].freeze
|
|
8
|
-
BOOLEAN_OPTIONS = %i[unicode eager_columns allow_empty_match experimental standalone].freeze
|
|
8
|
+
BOOLEAN_OPTIONS = %i[unicode eager_columns allow_empty_match experimental standalone input_stack delegation dynamic_delimiters].freeze
|
|
9
9
|
|
|
10
10
|
module_function
|
|
11
11
|
|
|
@@ -47,6 +47,23 @@ module Flexr
|
|
|
47
47
|
raise CompileError.new(diagnostic.message, diagnostic: diagnostic)
|
|
48
48
|
end
|
|
49
49
|
|
|
50
|
+
def rule_emits!(value, emit: nil, location: nil)
|
|
51
|
+
return if value.nil?
|
|
52
|
+
|
|
53
|
+
types = value.is_a?(Symbol) ? [value] : value
|
|
54
|
+
return types.uniq.freeze if !emit && types.is_a?(Array) && !types.empty? && types.all?(Symbol)
|
|
55
|
+
|
|
56
|
+
message = emit ? "emits: cannot be combined with emit:" : "emits: requires a Symbol or a nonempty Array of Symbols"
|
|
57
|
+
diagnostic = Diagnostics.error("FLEXR-E018", message, location: location)
|
|
58
|
+
raise CompileError.new(message, diagnostic: diagnostic)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def expected_tokens!(value)
|
|
62
|
+
return value if value.is_a?(Array) && value.all?(Symbol)
|
|
63
|
+
|
|
64
|
+
raise ArgumentError, "expect: must be an Array of token Symbols"
|
|
65
|
+
end
|
|
66
|
+
|
|
50
67
|
def validated_symbol!(value, allowed, name)
|
|
51
68
|
symbol = value.to_sym
|
|
52
69
|
return symbol if allowed.include?(symbol)
|
data/lib/flexr/dsl.rb
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
module Flexr
|
|
4
4
|
module DSL
|
|
5
|
-
DSL_METHODS = %i[rule pattern upto state all_states on_eof emits keywords backend token_kind encoding option accel].freeze
|
|
5
|
+
DSL_METHODS = %i[rule use pattern upto state all_states on_eof emits keywords indentation backend token_kind encoding option accel].freeze
|
|
6
6
|
def inherited(child)
|
|
7
7
|
super
|
|
8
8
|
child.__flexr_reset!
|
|
@@ -25,7 +25,7 @@ module Flexr
|
|
|
25
25
|
@__flexr_compile_mutex = Monitor.new
|
|
26
26
|
end
|
|
27
27
|
|
|
28
|
-
def rule(pattern, skip: false, emit: nil, followed_by: nil, channel: :default, priority: 0, &action)
|
|
28
|
+
def rule(pattern, skip: false, emit: nil, followed_by: nil, channel: :default, priority: 0, emits: nil, &action)
|
|
29
29
|
__flexr_mutate! do
|
|
30
30
|
patterns = normalize_patterns(pattern)
|
|
31
31
|
channel = Configuration.rule_channel!(channel)
|
|
@@ -38,7 +38,8 @@ module Flexr
|
|
|
38
38
|
@__flexr_rules << IR::Rule.new(
|
|
39
39
|
index: @__flexr_rules.length, patterns: patterns, trailing: normalize_trailing(followed_by),
|
|
40
40
|
action: rule_action, states: states, bol_only: false, end_anchor: nil, channel: channel,
|
|
41
|
-
priority: Configuration.rule_priority!(priority)
|
|
41
|
+
priority: Configuration.rule_priority!(priority),
|
|
42
|
+
emits: Configuration.rule_emits!(emits, emit: emit)
|
|
42
43
|
)
|
|
43
44
|
include Runtime::Channels if channel
|
|
44
45
|
end
|
|
@@ -140,6 +141,14 @@ module Flexr
|
|
|
140
141
|
__flexr_compiled
|
|
141
142
|
end
|
|
142
143
|
|
|
144
|
+
def recognizer(state: :initial)
|
|
145
|
+
raise ArgumentError, "recognizer requires a longest-match backend; firstmatch uses Ruby regexp order" if
|
|
146
|
+
@__flexr_config.backend == :firstmatch
|
|
147
|
+
raise ArgumentError, "undefined lexer state #{state.inspect}" unless @__flexr_states.key?(state)
|
|
148
|
+
|
|
149
|
+
Recognizer.new(compile!, @__flexr_config, state)
|
|
150
|
+
end
|
|
151
|
+
|
|
143
152
|
def __flexr_spec
|
|
144
153
|
IR::Spec.new(
|
|
145
154
|
class_name: name,
|
|
@@ -202,11 +211,13 @@ module Flexr
|
|
|
202
211
|
end
|
|
203
212
|
|
|
204
213
|
def freeze_specification!
|
|
214
|
+
include Runtime::Captures if @__flexr_rules.any?(&:capture_patterns)
|
|
205
215
|
@__flexr_rules.each do |rule|
|
|
206
216
|
rule.patterns.freeze
|
|
207
217
|
rule.states.freeze
|
|
208
218
|
rule.pattern_conditions&.each(&:freeze)
|
|
209
219
|
rule.pattern_conditions&.freeze
|
|
220
|
+
rule.capture_patterns&.freeze
|
|
210
221
|
rule.freeze
|
|
211
222
|
end
|
|
212
223
|
@__flexr_states.each_value(&:freeze)
|
data/lib/flexr/generated.rb
CHANGED
|
@@ -27,6 +27,8 @@ module Flexr
|
|
|
27
27
|
config.fetch(:options, {}).each do |option, value|
|
|
28
28
|
if option == :accel
|
|
29
29
|
klass.accel(value)
|
|
30
|
+
elsif option == :indentation
|
|
31
|
+
klass.indentation(**value)
|
|
30
32
|
elsif value == true
|
|
31
33
|
klass.option(option)
|
|
32
34
|
else
|
|
@@ -183,7 +185,9 @@ module Flexr
|
|
|
183
185
|
states: Array(definition.fetch(:states)).map(&:to_sym),
|
|
184
186
|
bol_only: definition.fetch(:bol_only, false), end_anchor: definition[:end_anchor],
|
|
185
187
|
location: definition[:span] || definition[:location], channel: channel,
|
|
188
|
+
emits: Configuration.rule_emits!(definition[:emits]),
|
|
186
189
|
priority: definition[:priority],
|
|
190
|
+
capture_patterns: definition[:capture_patterns],
|
|
187
191
|
trailing_dfa: definition[:trailing_dfa] && Generated.load_dfa(definition[:trailing_dfa]),
|
|
188
192
|
pattern_conditions: Array(definition[:pattern_conditions]).map do |condition|
|
|
189
193
|
next unless condition
|
data/lib/flexr/generator.rb
CHANGED
|
@@ -17,13 +17,13 @@ module Flexr
|
|
|
17
17
|
version.rb errors.rb diagnostics.rb action_resolver.rb configuration.rb ir.rb
|
|
18
18
|
unicode/version.rb automaton/types.rb automaton/dfa.rb automaton/accel.rb
|
|
19
19
|
runtime/record.rb runtime/location.rb runtime/token.rb runtime/buffer.rb runtime/errors.rb
|
|
20
|
-
runtime/interpreter.rb runtime/core.rb dsl.rb lexer.rb generated.rb
|
|
20
|
+
runtime/interpreter.rb runtime/recognizer.rb runtime/core.rb runtime/checkpoint.rb dsl.rb lexer.rb generated.rb
|
|
21
21
|
].freeze
|
|
22
22
|
|
|
23
23
|
MATCHER_RUNTIME_SOURCES = %w[
|
|
24
24
|
regexp/ast.rb regexp/parser.rb regexp/unsupported.rb
|
|
25
25
|
unicode/utf8_splitter.rb unicode/data/properties.rb unicode/data/case_folding.rb
|
|
26
|
-
unicode/property.rb unicode/reference_regexp.rb unicode/case_fold.rb regexp/matcher.rb
|
|
26
|
+
unicode/property.rb unicode/reference_regexp.rb unicode/case_fold.rb regexp/matcher.rb automaton/reference_dfa.rb
|
|
27
27
|
].freeze
|
|
28
28
|
|
|
29
29
|
def initialize(path, output: nil, eval_mode: false, options: {})
|
|
@@ -59,6 +59,10 @@ module Flexr
|
|
|
59
59
|
payload = generated_payload(parsed, compiled)
|
|
60
60
|
indent = Source::Passthrough.indentation(parsed.source, parsed.first_dsl_offset)
|
|
61
61
|
install = "Flexr::Generated.install_compiled!(self, #{payload})\n"
|
|
62
|
+
if parsed.config.dig(:options, :indentation)
|
|
63
|
+
guard = 'Flexr::Generated.incompatible_artifact!("runtime lacks indentation support") unless respond_to?(:indentation)'
|
|
64
|
+
install = "#{guard}\n#{indent}#{install}"
|
|
65
|
+
end
|
|
62
66
|
if parsed.rules.any? { |rule| !rule.priority.to_i.zero? }
|
|
63
67
|
guard = 'Flexr::Generated.incompatible_artifact!("runtime does not support explicit rule priority")' \
|
|
64
68
|
' unless Flexr::IR::Rule.method_defined?(:loses_priority_tie?)'
|
|
@@ -69,6 +73,16 @@ module Flexr
|
|
|
69
73
|
' unless defined?(Flexr::Runtime::Channels)'
|
|
70
74
|
install = "#{guard}\n#{indent}#{install}"
|
|
71
75
|
end
|
|
76
|
+
if compiled.rules.any?(&:capture_patterns)
|
|
77
|
+
guard = 'Flexr::Generated.incompatible_artifact!("runtime does not support named regexp captures")' \
|
|
78
|
+
' unless defined?(Flexr::Runtime::Captures)'
|
|
79
|
+
install = "#{guard}\n#{indent}#{install}"
|
|
80
|
+
end
|
|
81
|
+
if uses_delegation?(parsed)
|
|
82
|
+
guard = 'Flexr::Generated.incompatible_artifact!("runtime does not support lexer delegation")' \
|
|
83
|
+
' unless defined?(Flexr::Runtime::DelegatedTokens)'
|
|
84
|
+
install = "#{guard}\n#{indent}#{install}"
|
|
85
|
+
end
|
|
72
86
|
install = "#{install}#{Codegen::Table.new(compiled).source(indent: indent, direct: effective_backend(parsed) == :direct)}"
|
|
73
87
|
install = "#{install}#{generated_action_source(parsed, indent)}"
|
|
74
88
|
edits = parsed.dsl_edits.dup
|
|
@@ -94,7 +108,7 @@ module Flexr
|
|
|
94
108
|
"# eval: #{@eval_mode}",
|
|
95
109
|
"# standalone: #{standalone?(parsed)}"
|
|
96
110
|
].join("\n")
|
|
97
|
-
prefix = standalone?(parsed) ? "raise LoadError, \"flexr requires Ruby 3.4 or newer\" if RUBY_VERSION < \"3.4\"\nrequire \"json\"\nrequire \"monitor\"\n#{embedded_runtime(parsed)}\n" : ""
|
|
111
|
+
prefix = standalone?(parsed) ? "raise LoadError, \"flexr requires Ruby 3.4 or newer\" if RUBY_VERSION < \"3.4\"\nrequire \"json\"\nrequire \"monitor\"\n#{embedded_runtime(parsed, compiled)}\n" : ""
|
|
98
112
|
Source::Passthrough.insert_after_preamble(result, "#{header}\n#{prefix}")
|
|
99
113
|
end
|
|
100
114
|
|
|
@@ -105,11 +119,14 @@ module Flexr
|
|
|
105
119
|
end
|
|
106
120
|
priority = ("priority: #{rule.priority}, " unless rule.priority.to_i.zero?)
|
|
107
121
|
channel = "channel: #{ruby_literal(rule.channel)}, " if rule.channel
|
|
122
|
+
emits = "emits: #{ruby_literal(rule.emits)}, " if rule.emits
|
|
123
|
+
capture_patterns = compiled.rules.fetch(rule.index).capture_patterns
|
|
124
|
+
captures = "capture_patterns: #{ruby_literal(capture_patterns)}, " if capture_patterns
|
|
108
125
|
"{ index: #{rule.index}, patterns: #{ruby_literal(rule.patterns)}, pattern_conditions: #{ruby_literal(conditions)}, trailing: #{ruby_literal(rule.trailing)}, " \
|
|
109
126
|
"#{trailing_dfa_expression(rule, parsed, compiled.rules.fetch(rule.index).trailing_dfa)}" \
|
|
110
127
|
"#{priority}" \
|
|
111
128
|
"action: #{generated_rule_action(rule)}, states: #{rule.states.inspect}, " \
|
|
112
|
-
"#{channel}bol_only: #{ruby_literal(rule.bol_only)}, end_anchor: #{ruby_literal(rule.end_anchor)} }"
|
|
129
|
+
"#{channel}#{emits}#{captures}bol_only: #{ruby_literal(rule.bol_only)}, end_anchor: #{ruby_literal(rule.end_anchor)} }"
|
|
113
130
|
end
|
|
114
131
|
"{ artifact: #{ruby_literal(artifact_metadata)}, rules: [#{definitions.join(', ')}], " \
|
|
115
132
|
"backend: #{ruby_literal(effective_backend(parsed))}, " \
|
|
@@ -135,7 +152,7 @@ module Flexr
|
|
|
135
152
|
rules = parsed.rules.map do |rule|
|
|
136
153
|
IR::Rule.new(index: rule.index, patterns: rule.patterns, trailing: normalize_trailing(rule.trailing),
|
|
137
154
|
action: rule.action, states: rule.states, bol_only: rule.bol_only,
|
|
138
|
-
end_anchor: rule.end_anchor, location: rule.span, channel: rule.channel, priority: rule.priority)
|
|
155
|
+
end_anchor: rule.end_anchor, location: rule.span, channel: rule.channel, priority: rule.priority, emits: rule.emits)
|
|
139
156
|
rescue CompileError => e
|
|
140
157
|
e.diagnostic.location ||= rule.span if e.diagnostic
|
|
141
158
|
raise
|
|
@@ -242,12 +259,19 @@ module Flexr
|
|
|
242
259
|
@options.fetch(:table_compression, parsed.config[:options]&.fetch(:table_compression, :rows) || :rows).to_sym
|
|
243
260
|
end
|
|
244
261
|
|
|
245
|
-
def embedded_runtime(parsed)
|
|
262
|
+
def embedded_runtime(parsed, compiled)
|
|
246
263
|
sources = RUNTIME_SOURCES.dup
|
|
264
|
+
sources << "runtime/input_stack.rb" if parsed.uses_input_stack || parsed.config.dig(:options, :input_stack)
|
|
265
|
+
sources << "runtime/indentation.rb" if parsed.config.dig(:options, :indentation)
|
|
266
|
+
sources << "runtime/delimiters.rb" if parsed.uses_dynamic_delimiters || parsed.config.dig(:options, :dynamic_delimiters)
|
|
267
|
+
sources.insert(sources.index("runtime/interpreter.rb"), "runtime/firstmatch.rb") if effective_backend(parsed) == :firstmatch
|
|
247
268
|
sources.insert(6, *MATCHER_RUNTIME_SOURCES) if matcher_runtime?(parsed)
|
|
248
269
|
sources.insert(6, "pattern.rb") if parsed.uses_patterns ||
|
|
249
270
|
parsed.rules.any? { |rule| rule.trailing.is_a?(Pattern) || rule.patterns.any?(Pattern) }
|
|
250
|
-
sources << "runtime/channels.rb" if parsed.rules.any?(&:channel)
|
|
271
|
+
sources << "runtime/channels.rb" if parsed.uses_rule_sets || parsed.rules.any?(&:channel) || uses_delegation?(parsed)
|
|
272
|
+
sources << "runtime/captures.rb" if compiled.rules.any?(&:capture_patterns)
|
|
273
|
+
sources << "runtime/delegation.rb" if uses_delegation?(parsed)
|
|
274
|
+
sources << "rules.rb" if parsed.uses_rule_sets
|
|
251
275
|
sources.map do |relative|
|
|
252
276
|
File.binread(File.expand_path(relative, __dir__)).gsub(/\r\n?/, "\n")
|
|
253
277
|
end.join("\n")
|
|
@@ -257,6 +281,10 @@ module Flexr
|
|
|
257
281
|
effective_backend(parsed) == :firstmatch || parsed.rules.any?(&:trailing)
|
|
258
282
|
end
|
|
259
283
|
|
|
284
|
+
def uses_delegation?(parsed)
|
|
285
|
+
parsed.uses_delegation || parsed.config.dig(:options, :delegation)
|
|
286
|
+
end
|
|
287
|
+
|
|
260
288
|
def encoding_expression(encoding)
|
|
261
289
|
encoding == Encoding::BINARY ? "Encoding::BINARY" : "Encoding::UTF_8"
|
|
262
290
|
end
|
|
@@ -414,6 +442,9 @@ module Flexr
|
|
|
414
442
|
# --eval is intentionally explicit; the generated file still contains
|
|
415
443
|
# executable action source, but its metadata is deterministic for this run.
|
|
416
444
|
parsed = klass.__flexr_spec
|
|
445
|
+
if (indentation = parsed.options[:indentation])
|
|
446
|
+
(passthrough.config[:options] ||= {})[:indentation] = indentation
|
|
447
|
+
end
|
|
417
448
|
if (tables = parsed.options[:keyword_tables])
|
|
418
449
|
(passthrough.config[:options] ||= {})[:keyword_tables] = tables
|
|
419
450
|
passthrough.config[:declared_tokens] |= parsed.declared_tokens
|
|
@@ -423,6 +454,7 @@ module Flexr
|
|
|
423
454
|
runtime_rule = runtime_rules.fetch(rule.index)
|
|
424
455
|
rule.patterns = runtime_rule.patterns
|
|
425
456
|
rule.trailing = runtime_rule.trailing
|
|
457
|
+
rule.emits = runtime_rule.emits
|
|
426
458
|
rule.priority = runtime_rule.priority
|
|
427
459
|
next unless rule.dynamic_channel || rule.channel || runtime_rule.channel
|
|
428
460
|
|