flexr 2.0.0 → 2.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.
Files changed (89) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +28 -0
  3. data/CONTRIBUTING.md +10 -7
  4. data/Rakefile +11 -1
  5. data/benchmark/golden/calculator_lexer.rb +6 -14
  6. data/benchmark/golden/graphql_lexer.rb +6 -14
  7. data/benchmark/golden/json_lexer.rb +18 -28
  8. data/benchmark/golden/regexp_tokenizer.rb +6 -14
  9. data/benchmark/golden/ruby_subset_lexer.rb +6 -14
  10. data/benchmark/golden/sql_lexer.rb +6 -14
  11. data/benchmark/golden/toml_lexer.rb +6 -14
  12. data/benchmark/golden/toy_lang_lexer.rb +6 -14
  13. data/benchmark/golden/with_lrama_lexer.rb +6 -14
  14. data/benchmark/golden/with_racc_lexer.rb +6 -14
  15. data/benchmark/matrix.rb +31 -4
  16. data/docs/RELEASING.md +13 -2
  17. data/docs/adr/0003-leftmost-longest.md +3 -0
  18. data/docs/adr/0019-measured-performance-floor.md +4 -9
  19. data/docs/adr/0027-per-engine-performance-targets.md +45 -0
  20. data/docs/adr/0028-dfa-absence-groups.md +28 -0
  21. data/docs/adr/0029-pattern-algebra.md +30 -0
  22. data/docs/adr/0030-classify-keywords-after-matching.md +24 -0
  23. data/docs/adr/0031-trivia-channels.md +24 -0
  24. data/docs/adr/0032-explicit-rule-priority.md +18 -0
  25. data/docs/migration-v2.md +4 -3
  26. data/docs/perf-log.md +258 -0
  27. data/docs/reference/compatibility.md +5 -0
  28. data/docs/reference/diagnostics.md +36 -7
  29. data/docs/reference/dsl.md +113 -8
  30. data/docs/reference/public-api.md +1 -0
  31. data/docs/reference/regexp.md +33 -1
  32. data/docs/reference/runtime.md +6 -2
  33. data/docs/reference/tokens-and-locations.md +33 -0
  34. data/lib/flexr/automaton/accel.rb +1 -0
  35. data/lib/flexr/automaton/analysis.rb +8 -5
  36. data/lib/flexr/automaton/compiler.rb +327 -54
  37. data/lib/flexr/automaton/dfa.rb +1 -1
  38. data/lib/flexr/automaton/dfa_operations.rb +63 -0
  39. data/lib/flexr/automaton/keyword_tables.rb +113 -0
  40. data/lib/flexr/automaton/nfa.rb +22 -1
  41. data/lib/flexr/cli.rb +64 -6
  42. data/lib/flexr/codegen/table.rb +44 -30
  43. data/lib/flexr/configuration.rb +14 -0
  44. data/lib/flexr/diagnostics.rb +1 -0
  45. data/lib/flexr/dsl.rb +28 -7
  46. data/lib/flexr/generated.rb +4 -1
  47. data/lib/flexr/generator.rb +62 -14
  48. data/lib/flexr/importer.rb +1 -1
  49. data/lib/flexr/ir.rb +9 -1
  50. data/lib/flexr/keywords.rb +65 -0
  51. data/lib/flexr/pattern.rb +76 -0
  52. data/lib/flexr/regexp/ast.rb +28 -2
  53. data/lib/flexr/regexp/normalizer.rb +35 -1
  54. data/lib/flexr/regexp/parser.rb +7 -4
  55. data/lib/flexr/regexp/tokenizer.rb +8 -16
  56. data/lib/flexr/runtime/channels.rb +70 -0
  57. data/lib/flexr/runtime/core.rb +12 -5
  58. data/lib/flexr/runtime/interpreter.rb +11 -7
  59. data/lib/flexr/runtime/token.rb +6 -0
  60. data/lib/flexr/runtime.rb +6 -3
  61. data/lib/flexr/source/prism_reader.rb +52 -10
  62. data/lib/flexr/source/static_eval.rb +19 -0
  63. data/lib/flexr/trace.rb +18 -12
  64. data/lib/flexr/version.rb +1 -1
  65. data/lib/ruby_lsp/flexr/addon.rb +30 -4
  66. data/sig/flexr.rbs +18 -4
  67. data/site/README.md +16 -0
  68. data/site/astro.config.mjs +8 -0
  69. data/site/package.json +3 -2
  70. data/site/ruby/playground.rb +9 -0
  71. data/site/ruby/presets/absence.flexr.rb +7 -0
  72. data/site/ruby/presets/keywords.flexr.rb +9 -0
  73. data/site/ruby/presets/pattern.flexr.rb +11 -0
  74. data/site/ruby/presets/priority.flexr.rb +7 -0
  75. data/site/ruby/presets/trivia.flexr.rb +8 -0
  76. data/site/scripts/prepare-docs.mjs +23 -0
  77. data/site/scripts/prepare-playground.mjs +26 -2
  78. data/site/scripts/stable-release.mjs +16 -0
  79. data/site/src/components/DocsTitle.astro +30 -0
  80. data/site/src/content/docs/benchmarks.md +11 -1
  81. data/site/src/docs-navigation.ts +14 -0
  82. data/site/src/pages/playground.astro +7 -0
  83. data/site/tests/browser/documentation-versions.spec.mjs +33 -0
  84. data/site/tests/browser/playground.spec.mjs +35 -0
  85. data/site/tests/playground-release.test.mjs +71 -0
  86. data/site/tests/playground-runtime.test.mjs +70 -0
  87. data/test/ruby_lsp_addon_spec.rb +71 -0
  88. data/tools/release_notes.rb +15 -0
  89. metadata +24 -1
@@ -10,15 +10,10 @@ runtime/generated ratio was `1.042x`. These figures are recorded in
10
10
 
11
11
  ## Decision
12
12
 
13
- The handwritten comparison is a stretch target, not a release claim. The v1.0
14
- performance floor is a measured, regression-tested `0.18x` handwritten ratio;
15
- the `0.7x` target remains open for a future optimization milestone. The
16
- runtime/generated relationship remains governed by the existing `0.2x` risk
17
- threshold.
18
-
19
- This revision preserves an honest acceptance criterion: the project must not
20
- claim to have reached `0.7x`, and benchmark output must continue to be recorded
21
- from an actual run.
13
+ The measured, regression-tested interpreter floor is `0.18x` of the handwritten
14
+ JSON lexer. [ADR 0027](0027-per-engine-performance-targets.md) defines the
15
+ throughput targets for each engine and the generated/runtime comparison.
16
+ Benchmark output must come from an actual run.
22
17
 
23
18
  The absolute floor is enforced by the local release gate. CI uses a portable
24
19
  variant that keeps the 10% baseline-regression check but omits the absolute
@@ -0,0 +1,45 @@
1
+ # ADR 0027: Set performance targets per Ruby engine
2
+
3
+ ## Context
4
+
5
+ [ADR 0019](0019-measured-performance-floor.md) established a measured release
6
+ floor for the pure-Ruby lexer. The [engine matrix](../perf-log.md#2026-11-20)
7
+ shows that interpreter, YJIT, and ZJIT results differ substantially. Generated
8
+ mode can also be slower than runtime mode on the same engine and input.
9
+
10
+ ## Decision
11
+
12
+ Measure each engine in a separate Ruby process with the same input and lexer.
13
+ Verify equivalent token sequences and record the Ruby version, machine,
14
+ workload, and sample size with the throughput and allocation results.
15
+
16
+ | Engine | JSON throughput relative to handwritten |
17
+ |---|---|
18
+ | Interpreter | Keep the `0.18x` local release floor and target `0.30x`. |
19
+ | YJIT | Target `0.50x`, with a stretch target of `0.70x`. |
20
+ | ZJIT | Track throughput and allocations, and investigate reproducible regressions. |
21
+
22
+ On the reference JSON workload, target a generated/runtime throughput ratio
23
+ of at least `1.0` on each engine and at most 3.4 allocations per emitted token
24
+ in runtime mode. Measure other lexer workloads individually. Run timing
25
+ comparisons on a fixed reference machine and keep the portable CI gate from
26
+ [ADR 0019](0019-measured-performance-floor.md).
27
+
28
+ On the reference machine, run
29
+ `FLEXR_BENCHMARK_CHECK_MATRIX=1 FLEXR_BENCHMARK_ITERATIONS=10 bundle exec rake bench:matrix`.
30
+ The check keeps all three ratio samples for each engine and uses their median.
31
+ An unavailable engine or a median below `1.0` fails the check.
32
+
33
+ For simple scanners on frozen input with scanning guards inactive, generated
34
+ `accel :auto` uses the same DFA-only policy as the interpreter fast path. This
35
+ avoids lazy region-cache writes in the generated scan loop. The region path
36
+ remains available for streaming, guarded input, and explicit `:strscan`.
37
+
38
+ Whole-token native scans require explicit `accel :strscan`. The
39
+ [whole-token measurement](../perf-log.md#2026-11-22-whole-token-selection-by-engine-and-workload)
40
+ found substantial gains on long whitespace runs and generated GraphQL,
41
+ alongside YJIT regressions on compact JSON, TOML, SQL, and the Ruby subset
42
+ even with the DFA fallback. The DFA is the portable default. Any automatic
43
+ choice needs an eligibility check tied to the rule and input shape.
44
+ Measure the cost of that check and verify identical token sequences in the
45
+ selected path and fallback.
@@ -0,0 +1,28 @@
1
+ # ADR 0028: Compile absence groups as DFA difference
2
+
3
+ ## Context
4
+
5
+ Ruby's `(?~X)` syntax can describe text that avoids a terminator or another
6
+ regular language. The generated lexer needs the same accepted language as
7
+ the runtime compiler, including character boundaries and empty matches.
8
+
9
+ ## Decision
10
+
11
+ Let the universe be all strings of valid UTF-8 scalars or all byte strings
12
+ in binary mode. Compile `(?~X)` as the difference between that universe
13
+ and the language of a universe string, then `X`, then another universe
14
+ string. The DFA product treats a missing transition in the forbidden
15
+ language as a rejecting state. It retains only strings in the universe.
16
+ Minimize the product and insert it into the enclosing NFA so concatenation,
17
+ alternation, and longest-match selection continue to work normally.
18
+
19
+ Apply `max_dfa_states` when constructing each operand DFA, the product,
20
+ and the enclosing DFA. Classify empty matches from the language definition.
21
+ If the excluded language contains the empty string, the difference is empty.
22
+ Ruby regexp matching of a nullable excluded expression can instead return
23
+ an empty match, as in `\A(?~a?)\z` on Ruby 4.0.9.
24
+
25
+ Serialized artifacts use their existing DFA representation. The
26
+ `firstmatch` backend retains its Ruby regexp selection for rule bodies,
27
+ so use a longest-match backend for a body containing absence. Trailing
28
+ context already uses a DFA and supports absence.
@@ -0,0 +1,30 @@
1
+ # ADR 0029: Treat patterns as composable regular languages
2
+
3
+ ## Context
4
+
5
+ Lexers often reuse the same identifier or delimiter in several rules.
6
+ Users also need to exclude keywords or restrict the length of an existing
7
+ pattern. These operations can be expressed using the same DFA as rule matching.
8
+
9
+ ## Decision
10
+
11
+ `pattern` and `upto` create immutable values that copy literal inputs and
12
+ can be shared across Ractors. The static reader resolves constant references
13
+ in source order and retains their names for inspection. The runtime accepts
14
+ the same value expressions in normal Ruby class bodies.
15
+
16
+ Compile union and concatenation through the NFA. Compile intersection and
17
+ difference with bounded DFA products. Complement uses the subject alphabet
18
+ as the left operand of difference. Embed resulting DFAs as fragments so the
19
+ enclosing lexer continues to use longest-match and source order.
20
+
21
+ `upto` uses the language of strings ending at the delimiter. Remove the
22
+ outgoing transitions of accepting states before embedding the DFA. This
23
+ makes the earliest completed delimiter definitive even when a later
24
+ occurrence overlaps it.
25
+
26
+ Continue to use the DFA for pattern-based rule bodies and lookahead.
27
+ Require a longest-match backend for these rule bodies because explicit
28
+ `firstmatch` keeps Ruby regexp alternative order. Generated and standalone
29
+ lexers serialize the compiled tables and retain pattern descriptions for
30
+ tracing and diagnostics.
@@ -0,0 +1,24 @@
1
+ # ADR 0030: Classify keywords after rule selection
2
+
3
+ ## Context
4
+
5
+ Reserved words often share the same lexeme language and token value as
6
+ identifiers. Declaring each as a rule adds paths to the automaton and makes
7
+ rule order part of the keyword lookup.
8
+
9
+ ## Decision
10
+
11
+ Keep the identifier rule as the DFA candidate. A keyword table belongs to a
12
+ base `emit:` type. After a rule with that type wins, look up its exact
13
+ lexeme and use the mapped token type. The error policy, match length,
14
+ token value, location, and lexical state follow the winning rule.
15
+
16
+ Validate spellings against the base rules using the compiled DFA. For
17
+ `firstmatch`, the rule's first match must also consume the full spelling.
18
+ Require a declarative base type so generation and diagnostics can resolve it
19
+ statically. Register the base type and all mapped types for parser integration.
20
+
21
+ The generated artifact carries keyword tables as optional data. Generated
22
+ actions for participating rules perform the same lookup. Other generated
23
+ actions and ordinary runtime lexers keep their existing execution paths.
24
+ The tables leave DFA transitions and the generated artifact ABI unchanged.
@@ -0,0 +1,24 @@
1
+ # ADR 0031: Preserve trivia in named token channels
2
+
3
+ ## Context
4
+
5
+ Parser adapters consume semantic tokens. Formatters and editor tools also
6
+ need comments, whitespace, and locations, which discard rules remove from
7
+ the stream.
8
+
9
+ ## Decision
10
+
11
+ Route emitted tokens by the selected rule's channel after matching and its
12
+ action. Keep the same DFA, precedence, and state transitions. The default
13
+ consumers and Racc expose the default channel. `tokens(channels: :all)`
14
+ includes named trivia channels in source order. `channel: :skip` and
15
+ `skip: true` discard the match in both views.
16
+
17
+ Structured tokens carry trivia as immutable metadata outside their existing
18
+ data members. Array and yield tokens keep their two-element shape. Hidden
19
+ tokens retain the byte ranges captured during their actions, including in
20
+ streaming mode. Ordinary lexers use the existing scanner and emission path.
21
+
22
+ Generated artifacts store channel metadata only when named channels are used.
23
+ Ordinary artifacts continue to use the same payload. Standalone generation
24
+ embeds the trivia runtime for lexers that use named channels.
@@ -0,0 +1,18 @@
1
+ # ADR 0032: Break length ties with explicit rule priority
2
+
3
+ ## Context
4
+
5
+ [ADR 0003](0003-leftmost-longest.md) selects the longest rule and uses source
6
+ order for ties. As specifications share rules, rewriting their declaration
7
+ order to favor a specific equal-length match can be awkward or impossible.
8
+
9
+ ## Decision
10
+
11
+ In `table`, `direct` and `auto`, compare the total matched byte length first,
12
+ including trailing context. Then compare integer rule priority, highest
13
+ first, followed by source order. Rules with no explicit priority use zero.
14
+ The standalone and generated formats carry priority only for rules that use it.
15
+
16
+ `firstmatch` retains source-order matching and rejects a nonzero priority.
17
+ Warnings, overlap examples and input traces identify an explicit priority
18
+ when it decides an equal-length match.
data/docs/migration-v2.md CHANGED
@@ -1,8 +1,9 @@
1
1
  # Migrating to flexr 2.0
2
2
 
3
- This guide describes the v2 prerelease. The preceding v1 preparation release
4
- keeps existing behavior and offers opt-in deprecation warnings. In v2 the
5
- contracts below are enforced and those preparation warnings are removed.
3
+ This guide covers the upgrade to flexr 2.0. The
4
+ [1.x maintenance branch](https://github.com/ydah/flexr/tree/maint/1.x) keeps
5
+ existing runtime behavior and provides opt-in deprecation warnings. Flexr 2.0
6
+ enforces the contracts below.
6
7
 
7
8
  Run static checks before upgrading:
8
9
 
data/docs/perf-log.md CHANGED
@@ -266,3 +266,261 @@ other lexers or JIT engines.
266
266
  `rake bench:regression` also passed with the existing baseline metrics, identity,
267
267
  ratio thresholds, and 0.18x floor unchanged. Reproduce the five-iteration sample
268
268
  with `ruby -Ilib benchmark/run.rb --iterations 5 --json`.
269
+
270
+ ## 2026-11-22: generated frozen-input scan
271
+
272
+ The generated simple scanner now follows the runtime's DFA path for frozen
273
+ strings with the default `accel :auto` and no active scan guards. It reads
274
+ the region cache and token-limit parameters only when it needs the fallback
275
+ path. A warmed diagnostic run on `0d1cc81` reported 8,748,740 megamorphic
276
+ instance-variable reads and 1,699,605 writes across five generated scans of
277
+ the JSON corpus with YJIT statistics enabled. Both counters reached zero
278
+ when the acceleration cache fields were initialized up front in an
279
+ experiment. The DFA path avoids those lazy cache writes on eligible input.
280
+
281
+ The comparison below uses Ruby 4.0.9 on arm64-darwin25 and the default
282
+ 140,000-byte JSON input with 50,000 emitted tokens. Baseline `0d1cc81` and
283
+ the updated scanner were run in alternating order. Each result is the median
284
+ of three runs with ten warmup and ten timed iterations per engine. Ratios
285
+ are calculated per run before taking the median.
286
+
287
+ | Engine | Generated MB/s before → after | Generated / runtime before → after | Generated / handwritten before → after |
288
+ |---|---:|---:|---:|
289
+ | Interpreter | 1.874 → 1.964 | 0.992x → 1.036x | 0.223x → 0.234x |
290
+ | YJIT | 3.635 → 5.633 | 0.606x → 0.958x | 0.388x → 0.519x |
291
+ | ZJIT | 0.431 → 2.080 | 0.200x → 0.972x | 0.043x → 0.202x |
292
+
293
+ YJIT generated throughput reaches the 0.50x handwritten target in this sample.
294
+ Both JITs remain below the 1.0x generated/runtime target. Runtime allocations
295
+ stay at 3.600 per token, and generated allocations move from 3.210 to 3.200.
296
+ A separate check compared the full runtime, generated, and standalone token
297
+ sequences for frozen JSON, TOML, GraphQL, SQL, and Ruby subset inputs in all
298
+ three engines.
299
+
300
+ Run the reference JSON matrix with:
301
+
302
+ ```sh
303
+ ruby -Ilib benchmark/matrix.rb --iterations 10
304
+ ```
305
+
306
+ The other lexer samples use one run with five warmup and five timed
307
+ iterations per engine on the same machine. The values are generated/runtime
308
+ throughput ratios. TOML, GraphQL, and SQL use the frozen built-in inputs.
309
+ Ruby subset uses the mutable string loaded by `--input-file`.
310
+
311
+ | Lexer | Interpreter before → after | YJIT before → after | ZJIT before → after |
312
+ |---|---:|---:|---:|
313
+ | TOML | 0.631x → 0.647x | 0.291x → 0.424x | 0.188x → 0.626x |
314
+ | GraphQL | 0.725x → 0.722x | 0.319x → 0.496x | 0.388x → 0.683x |
315
+ | SQL | 0.643x → 0.681x | 0.350x → 0.467x | 0.409x → 0.664x |
316
+ | Ruby subset | 0.845x → 0.849x | 0.419x → 0.404x | 0.244x → 0.243x |
317
+
318
+ Run these workloads with:
319
+
320
+ ```sh
321
+ for lexer in toml graphql sql; do
322
+ ruby -Ilib benchmark/matrix.rb --spec "examples/$lexer/lexer.flexr.rb" --iterations 5
323
+ done
324
+ ruby -e 'STDOUT.write(%q(class Foo "ok" end ) * 10_000)' > /tmp/flexr-ruby-subset-input.txt
325
+ ruby -Ilib benchmark/matrix.rb --spec examples/ruby_subset/lexer.flexr.rb \
326
+ --input-file /tmp/flexr-ruby-subset-input.txt --iterations 5
327
+ ```
328
+
329
+ ## 2026-11-22: packed lookup and token-loop allocations
330
+
331
+ The packed transition lookup and `next_token` use condition-controlled loops,
332
+ avoiding the block allocation on each packed lookup or token call. The token
333
+ loop still returns the result of an exhausted action enumerator. Packed rows
334
+ remain lazy. A warmed check exercises inherited transitions and confirms that
335
+ reading them leaves the dense rows unmaterialized. The generated direct fast
336
+ path keeps its negative transition sentinel as an integer until the scan
337
+ loop exits.
338
+
339
+ The JSON comparison uses Ruby 4.0.9 on arm64-darwin25 and the same 140,000-byte,
340
+ 50,000-token input as the previous sample. Baseline `73a6587` and the updated
341
+ loops ran in alternating order. Each result is the median of three runs with
342
+ ten warmup and ten timed iterations per engine.
343
+
344
+ | Engine | Runtime MB/s before → after | Generated MB/s before → after | Generated / runtime before → after | Generated / handwritten before → after |
345
+ |---|---:|---:|---:|---:|
346
+ | Interpreter | 1.897 → 2.022 | 1.978 → 2.199 | 1.043x → 1.088x | 0.237x → 0.282x |
347
+ | YJIT | 6.033 → 6.482 | 5.720 → 6.743 | 0.948x → 1.012x | 0.523x → 0.728x |
348
+ | ZJIT | 2.092 → 2.342 | 2.083 → 2.388 | 0.994x → 1.010x | 0.206x → 0.269x |
349
+
350
+ Runtime allocations fell from 3.600 to 2.600 per token on all three engines,
351
+ below the 3.4 target. Generated allocations fell from 3.200 to 2.200.
352
+ The YJIT sample reached the 0.70x handwritten stretch target. The three paired
353
+ runs reached the 1.0x generated/runtime target on all three engines, with
354
+ ZJIT ratios from 1.007x to 1.024x. A separate single run through
355
+ `rake bench:matrix` measured 0.980x on YJIT and 0.984x on ZJIT. This
356
+ variation leaves the 1.0x ratio as a measurement target.
357
+
358
+ The full frozen JSON, TOML, GraphQL, SQL, and Ruby subset token sequences
359
+ matched across runtime, generated, and standalone on all three engines.
360
+ Use the matrix commands in the previous section to repeat the timings.
361
+
362
+ The non-JSON runs used five warmup and five measured iterations per engine.
363
+ The allocation counts were identical across engines. Ruby subset timing
364
+ uses the mutable input-file control from the previous section.
365
+
366
+ | Lexer | Generated allocations/token before → after | Interpreter generated/runtime | YJIT generated/runtime | ZJIT generated/runtime |
367
+ |---|---:|---:|---:|---:|
368
+ | TOML | 10.334 → 2.000 | 0.610x → 0.775x | 0.413x → 0.589x | 0.621x → 0.837x |
369
+ | GraphQL | 7.333 → 2.000 | 0.722x → 0.844x | 0.506x → 0.652x | 0.674x → 0.878x |
370
+ | SQL | 9.889 → 2.667 | 0.703x → 0.836x | 0.447x → 0.570x | 0.639x → 0.854x |
371
+ | Ruby subset | 14.778 → 7.009 | 0.824x → 0.952x | 0.416x → 0.414x | 0.243x → 0.250x |
372
+
373
+ ## 2026-11-22: frozen scan EOF check
374
+
375
+ The unguarded generated DFA loop uses the end-of-input result from
376
+ `String#getbyte` to stop scanning. This removes a separate size lookup and
377
+ comparison per byte. A zero byte still participates in the DFA.
378
+
379
+ The paired runs use Ruby 4.0.9 on arm64-darwin25 and the same reference
380
+ JSON input as the previous entry. Run
381
+ `ruby -Ilib benchmark/matrix.rb --iterations 10` three times per revision,
382
+ alternating baseline `76c13b4` and the updated scanner. Each run warms each
383
+ engine with ten scans before timing ten more.
384
+
385
+ | Engine | Generated MB/s before → after | Median generated/runtime before → after | Generated/runtime range after |
386
+ |---|---:|---:|---:|
387
+ | Interpreter | 2.182 → 2.247 | 1.077x → 1.104x | 1.084x–1.108x |
388
+ | YJIT | 6.828 → 6.803 | 1.030x → 1.059x | 1.026x–1.089x |
389
+ | ZJIT | 2.392 → 2.409 | 1.019x → 1.020x | 1.015x–1.027x |
390
+
391
+ Runtime stayed at 2.600 allocations per token and generated stayed at 2.200.
392
+ The ZJIT sample clears the 1.0x target with about two percent headroom on
393
+ this machine. The YJIT generated rate was nearly unchanged despite
394
+ variation in the ratio.
395
+
396
+ A separate experiment saved each accepting state in locals and filled the
397
+ reused match record once after the longest match was known. The
398
+ generated/runtime medians moved from 1.097x to 1.097x for the interpreter,
399
+ 1.037x to 0.978x for YJIT, and 1.006x to 0.992x for ZJIT. The scanner
400
+ continues to update the match record during traversal.
401
+
402
+ ## 2026-11-22: whole-token selection by engine and workload
403
+
404
+ This experiment compares whole-token `StringScanner` plans with the frozen
405
+ DFA path on Ruby 4.0.9, arm64-darwin25. The runtime uses the same lexer with
406
+ `accel :strscan`. Generated measurements include both the complete explicit
407
+ `:strscan` mode and a control that keeps the explicit artifact's whole-token
408
+ plans but sets the instance's `@accel_mode` to `:auto`. The control keeps the
409
+ DFA fallback so its ratio isolates the native whole-token attempt from the
410
+ per-region fallback.
411
+
412
+ Each engine and workload runs in its own process. The test compares all
413
+ token kinds and values before warming up each variant. The reference JSON
414
+ workload uses ten warmup and ten timed scans per sample. The rest use six
415
+ of each. Three samples rotate the order of the variants. The Ruby subset
416
+ input repeats `class Foo "ok" end ` 10,000 times and is frozen for this
417
+ experiment. The two synthetic JSON inputs repeat 680 JSON objects, with
418
+ 192 spaces after each object or 192 ASCII letters in each string value.
419
+ The complete token sequences matched across all variants and engines.
420
+
421
+ Values are throughput relative to the corresponding runtime or generated
422
+ DFA baseline. Each cell lists interpreter, YJIT, and ZJIT in that order.
423
+
424
+ | Workload | Bytes | Runtime explicit | Generated explicit | Generated whole-token only |
425
+ |---|---:|---:|---:|---:|
426
+ | JSON | 140,000 | 1.056x / 0.776x / 0.992x | 0.520x / 0.279x / 0.550x | 1.027x / 0.663x / 1.008x |
427
+ | JSON with long space runs | 140,080 | 3.906x / 2.528x / 4.052x | 1.818x / 0.851x / 1.927x | 3.571x / 1.744x / 3.546x |
428
+ | JSON with long strings | 139,400 | 1.002x / 0.850x / 0.992x | 0.830x / 0.284x / 0.914x | 0.978x / 0.789x / 0.975x |
429
+ | TOML | 160,000 | 1.008x / 0.688x / 0.946x | 0.506x / 0.286x / 0.544x | 1.138x / 0.764x / 1.132x |
430
+ | GraphQL | 320,000 | 1.217x / 0.743x / 1.099x | 1.577x / 1.092x / 1.546x | 1.570x / 1.063x / 1.545x |
431
+ | SQL | 390,000 | 1.026x / 0.758x / 0.947x | 0.700x / 0.439x / 0.306x | 1.153x / 0.714x / 0.934x |
432
+ | Ruby subset | 190,000 | 1.033x / 0.780x / 0.974x | 0.656x / 0.425x / 0.313x | 1.179x / 0.782x / 1.017x |
433
+
434
+ The whole-token attempt paid off on long whitespace runs but slowed the
435
+ similarly sized long-string workload. GraphQL gained in generated mode
436
+ while runtime mode slowed on YJIT. These results leave whole-token
437
+ scanning on explicit `accel :strscan`. A default needs a cheap way to
438
+ recognize profitable matches and measurements that include the cost of
439
+ that choice and its fallback.
440
+
441
+ ## 2026-11-22: select the frozen scan's transition layout at generation
442
+
443
+ The generated artifact already determines whether its state transitions use
444
+ flat direct tables. Selecting the lookup when generating the loop removes
445
+ that choice from each byte scanned on the frozen, unguarded path.
446
+
447
+ On the integrated branch, the EOF change alone measured a ZJIT ratio of
448
+ 0.992x with ten iterations. Adding packed-table lookups to the same loop
449
+ lowered YJIT throughput on the reference direct lexer.
450
+
451
+ Run `FLEXR_BENCHMARK_CHECK_MATRIX=1 FLEXR_BENCHMARK_ITERATIONS=15 bundle exec rake bench:matrix`
452
+ with Ruby 4.0.9 on arm64-darwin25 and the same 140,000-byte JSON input.
453
+ The medians of three fresh processes passed on all engines.
454
+
455
+ | Engine | Generated/runtime samples | Median | Generated/handwritten at the median |
456
+ |---|---:|---:|---:|
457
+ | Interpreter | 1.093x, 1.198x, 1.101x | 1.101x | 0.279x |
458
+ | YJIT | 1.009x, 1.017x, 0.982x | 1.009x | 0.686x |
459
+ | ZJIT | 1.014x, 1.007x, 1.032x | 1.014x | 0.262x |
460
+
461
+ Runtime remained at 2.600 allocations per emitted token. Both JIT ratios have
462
+ limited headroom, and the interpreter remains below the 0.30x target.
463
+
464
+ ## 2026-11-22: cache the direct scan layout per DFA
465
+
466
+ The generated direct scanner gathers the start state, transition table,
467
+ byte classes, acceptances, and rule array in its existing per-DFA cache.
468
+ The scanner reuses that layout when it returns to a lexical state.
469
+ The simple scan reads the cached layout instead of repeating the DFA
470
+ lookups for every token.
471
+
472
+ The comparison uses Ruby 4.0.9 on arm64-darwin25 and the 140,000-byte
473
+ reference JSON workload, which emits 50,000 tokens. Baseline `e6dd953`
474
+ and the updated scanner ran in alternating order. Each row reports medians
475
+ from three runs with ten warmup and ten timed scans per engine.
476
+
477
+ | Engine | Generated MB/s before → after | Generated/runtime before → after | Generated/handwritten before → after |
478
+ |---|---:|---:|---:|
479
+ | Interpreter | 2.265 → 2.422 | 1.115x → 1.184x | 0.286x → 0.315x |
480
+ | YJIT | 7.424 → 8.149 | 1.144x → 1.198x | 0.753x → 0.868x |
481
+ | ZJIT | 2.453 → 2.524 | 1.042x → 1.084x | 0.271x → 0.270x |
482
+
483
+ The separate reference run used the same gate as the previous comparison.
484
+
485
+ ```sh
486
+ FLEXR_BENCHMARK_CHECK_MATRIX=1 FLEXR_BENCHMARK_ITERATIONS=15 bundle exec rake bench:matrix
487
+ ```
488
+
489
+ | Engine | Generated/runtime samples | Median | Generated/handwritten at the median |
490
+ |---|---:|---:|---:|
491
+ | Interpreter | 1.177x, 1.180x, 1.165x | 1.177x | 0.307x |
492
+ | YJIT | 1.062x, 1.064x, 1.073x | 1.064x | 0.763x |
493
+ | ZJIT | 1.034x, 1.068x, 1.060x | 1.060x | 0.274x |
494
+
495
+ The interpreter crossed the 0.30x handwritten target in the reference
496
+ run and all three paired samples. The reference matrix passed on all
497
+ three engines. Runtime and generated allocations stayed at 2.600 and
498
+ 2.200 per emitted token.
499
+
500
+ A separate interpreter screening run tested a preselected winning rule,
501
+ early exit from a terminal accepting state, and their combination.
502
+ The baseline measured 2.300 MB/s generated. The three variants measured
503
+ 2.304, 2.308, and 2.303 MB/s, respectively.
504
+
505
+ A one-rule direct standalone sample measured 99,698 bytes before and
506
+ 99,760 bytes after. The ordinary standalone size checks passed for LF and
507
+ CRLF source files.
508
+
509
+ TOML, GraphQL, and SQL were also measured on temporary copies with
510
+ `backend :direct`. The table reports generated MB/s before and after the
511
+ change. Each measurement used five warmup and five timed scans.
512
+
513
+ | Workload | Interpreter | YJIT | ZJIT |
514
+ |---|---:|---:|---:|
515
+ | TOML | 2.701 → 2.924 | 8.418 → 9.561 | 2.890 → 2.982 |
516
+ | GraphQL | 1.655 → 1.791 | 5.244 → 5.899 | 1.784 → 1.871 |
517
+ | SQL | 2.190 → 2.372 | 6.351 → 6.634 | 2.295 → 2.335 |
518
+
519
+ SQL on ZJIT is the median of three follow-up runs with seven warmup and
520
+ seven timed scans, alternating the two revisions. The initial pair
521
+ slowed both runtime and generated scanners in the candidate process.
522
+ In the follow-up, the generated/runtime medians were 1.024x and 1.035x.
523
+
524
+ Runtime, generated, and standalone produced identical frozen-input
525
+ token sequences on all three engines for the five stock workloads
526
+ and the three direct variants.
@@ -39,6 +39,11 @@ detected. Do not assume that internal table layouts are stable.
39
39
  Version 2 uses artifact schema 2 and runtime ABI 2. Regenerate v1 artifacts
40
40
  after upgrading; incompatible metadata produces `FLEXR-E020`.
41
41
 
42
+ Artifacts built with a new DSL feature require a runtime that provides that
43
+ feature. For example, a non-standalone lexer that defines named patterns with
44
+ `pattern` needs the pattern DSL in its installed runtime. Existing v2
45
+ artifacts continue to load on the newer runtime.
46
+
42
47
  All four backends are stable. `table`, `direct`, and `auto` use longest-match
43
48
  selection; `firstmatch` follows rule and regexp alternative order. It does not
44
49
  share the longest-match promise and must be selected explicitly.
@@ -43,7 +43,7 @@ Source ranges are available for:
43
43
  constructs (`FLEXR-E014`), including errors in trailing context.
44
44
  - Rule warnings: `FLEXR-W001`, `FLEXR-W003`, `FLEXR-W010`, `FLEXR-W012`,
45
45
  `FLEXR-W013`, and `FLEXR-W014`.
46
- - Rule overlap information: `FLEXR-I001`, pointing to the later rule.
46
+ - Rule overlap information: `FLEXR-I001`, pointing to the lower-ranked rule.
47
47
  - State and specification diagnostics: DFA state limits (`FLEXR-E006`), empty
48
48
  states (`FLEXR-W002`), large transition tables (`FLEXR-W011`), and slow DFA
49
49
  construction (`FLEXR-W016`).
@@ -143,7 +143,7 @@ without a readable caller file still report the regeneration guidance.
143
143
  | `FLEXR-E013` | `reject` is not supported | Use states and `less` |
144
144
  | `FLEXR-E014` | Regexp construct is not DFA-compatible | Follow the diagnostic alternative |
145
145
  | `FLEXR-E017` | Pattern is not statically resolvable | Rewrite as a static expression or use trusted `--eval` |
146
- | `FLEXR-E018` | Rule pattern or trailing context has an invalid type | Use a regexp, string, or valid array |
146
+ | `FLEXR-E018` | Rule pattern, trailing context, channel, or priority is invalid | Use a supported pattern, a valid channel symbol, or an integer priority with a longest-match backend |
147
147
  | `FLEXR-E019` | Prism is unavailable for generation | Install the generator dependencies |
148
148
  | `FLEXR-E020` | Generated artifact metadata is missing or incompatible | Regenerate with the installed flexr version |
149
149
  | `FLEXR-E021` | An action or state cycle made no input progress | Consume input or break the state cycle |
@@ -153,6 +153,20 @@ without a readable caller file still report the regeneration guidance.
153
153
  | `FLEXR-E025` | The lexical state stack overflowed | Remove recursive pushes or raise the limit |
154
154
  | `FLEXR-E026` | A cancellation callback stopped lexing | Resume with a new lexer when ready |
155
155
  | `FLEXR-E027` | An error callback returned an unknown action | Return `:skip`, `:raise`, `:token`, or `:halt` |
156
+ | `FLEXR-E028` | A keyword spelling or declaration is invalid, or the default-channel base rule cannot consume the full spelling | Correct the spelling or widen a default-channel rule declared with the base `emit:` type |
157
+ | `FLEXR-E029` | A pattern operation has an empty language | Review the operands or the terminator |
158
+
159
+ ### Pattern operations
160
+
161
+ An empty intersection or difference reports `FLEXR-E029` at the rule.
162
+ The diagnostic gives examples from the operand languages when they have
163
+ matches. A difference between disjoint operands reports `FLEXR-W021`
164
+ with an example accepted by the right operand.
165
+
166
+ `flexr stats` includes each evaluated DFA operation and its operand,
167
+ product, and result state counts. `FLEXR-W011` identifies a product that
168
+ visits at least 16 states and more than doubles the larger operand. The
169
+ `max_dfa_states` limit applies to operands, products, and the final DFA.
156
170
 
157
171
  ### Regexp rewrite help
158
172
 
@@ -218,11 +232,11 @@ with the same action. The original matches `ab` when the optional `a` was
218
232
  captured, or `c` otherwise. Other conditions need their own alternatives,
219
233
  separate rules, or state checks.
220
234
 
221
- For an absence group excluding a single literal, rewrite `rule(/x(?~a)/)`
222
- as `rule(/x[^a]*/)` and keep the action. Both consume `x` and the following
223
- characters up to the next `a` or end of input, including line feeds. The `x`
224
- keeps the rule from matching empty input. Larger absence expressions need a
225
- separate rewrite: `(?~ab)` does not mean `[^ab]*`.
235
+ Use `(?~X)` directly with a longest-match backend. `FLEXR-E014` recommends
236
+ that backend for an absence group in a `firstmatch` rule body. When the
237
+ excluded expression accepts the empty string, an unreachable rule may
238
+ report that the absence language is empty with `FLEXR-W001`. See
239
+ [absence groups](regexp.md#absence-groups) for the matching rules.
226
240
 
227
241
  For word boundaries, the help uses examples where the surrounding literals
228
242
  already determine the assertion. Rewrite `rule(/foo\b:/)` as `rule(/foo:/)`:
@@ -326,6 +340,7 @@ expression can change how the surrounding pattern matches.
326
340
  | `FLEXR-W017` | Grammar token is missing from `emits` | Declare and emit it, or remove it from the grammar |
327
341
  | `FLEXR-W018` | Token in `emits` is missing from the grammar | Add a `%token` declaration or remove it from `emits` |
328
342
  | `FLEXR-W020` | Structured records become immutable in v2.0 | Review consumer mutations; use `record.with(...)` in v2 |
343
+ | `FLEXR-W022` | A competing rule shadows a spelling in a keyword table | Remove the redundant spelling or change the competing rule |
329
344
 
330
345
  Warnings are reported by `check` according to `--warn`. The default omits the
331
346
  wall-clock `FLEXR-W016`; `--warn all` includes it, and `--warn none` suppresses
@@ -333,6 +348,13 @@ warnings. `--warn-as-error` turns selected warnings into a failed command.
333
348
 
334
349
  ## Informational diagnostics
335
350
 
351
+ `flexr check` reports `FLEXR-I003` when consecutive default-channel literal
352
+ rules can share the following default-channel identifier rule using `keywords`.
353
+ The rules must share a priority. The DFA must show that the identifier
354
+ accepts the full spellings without anchors in the same states.
355
+ `--warn none` hides this suggestion. `--warn-as-error` still permits checks
356
+ with informational diagnostics.
357
+
336
358
  `flexr check --target 2.0` includes `FLEXR-I002` with Ruby and artifact
337
359
  requirements. See the [v2 migration guide](../migration-v2.md) for the static
338
360
  check's limits and opt-in runtime deprecation warnings.
@@ -385,3 +407,10 @@ This analysis covers `table`, `direct`, `auto`, and `firstmatch` states.
385
407
  `--warn-as-error` does not turn informational diagnostics into failures.
386
408
  Generation with `--warn all` also preserves the information in generated and
387
409
  standalone artifacts.
410
+
411
+ ### Emit shorthand
412
+
413
+ `flexr check SPEC --warn all` reports `FLEXR-I004` for action blocks consisting
414
+ solely of `emit :TOKEN` or `emit :TOKEN, text`. Use `emit: :TOKEN` on the rule
415
+ to take the shorthand emission path. Actions that transform the value or perform
416
+ other work stay as blocks.