flexr 1.1.0 → 1.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.
Files changed (101) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -0
  3. data/CHANGELOG.md +116 -0
  4. data/CONTRIBUTING.md +13 -1
  5. data/README.md +6 -2
  6. data/Rakefile +41 -5
  7. data/Steepfile +7 -0
  8. data/benchmark/golden/calculator_lexer.rb +276 -0
  9. data/benchmark/golden/graphql_lexer.rb +310 -0
  10. data/benchmark/golden/json_lexer.rb +292 -0
  11. data/benchmark/golden/regexp_tokenizer.rb +293 -0
  12. data/benchmark/golden/ruby_subset_lexer.rb +280 -0
  13. data/benchmark/golden/sql_lexer.rb +293 -0
  14. data/benchmark/golden/toml_lexer.rb +292 -0
  15. data/benchmark/golden/toy_lang_lexer.rb +273 -0
  16. data/benchmark/golden/with_lrama_lexer.rb +264 -0
  17. data/benchmark/golden/with_racc_lexer.rb +264 -0
  18. data/benchmark/lrama_lexer.rb +165 -0
  19. data/benchmark/matrix.rb +79 -0
  20. data/benchmark/profile.rb +109 -0
  21. data/benchmark/run.rb +4 -1
  22. data/benchmark/state_dispatch.rb +78 -0
  23. data/docs/README.md +1 -2
  24. data/docs/RELEASING.md +9 -2
  25. data/docs/migration-v2.md +73 -0
  26. data/docs/perf-log.md +108 -0
  27. data/docs/reference/README.md +2 -0
  28. data/docs/reference/cli.md +71 -5
  29. data/docs/reference/compatibility.md +9 -2
  30. data/docs/reference/diagnostics.md +338 -0
  31. data/docs/reference/dsl.md +8 -5
  32. data/docs/reference/errors.md +28 -0
  33. data/docs/reference/generated-artifacts.md +11 -0
  34. data/docs/reference/public-api.md +32 -8
  35. data/docs/reference/regexp.md +19 -7
  36. data/docs/reference/ruby-lsp.md +34 -0
  37. data/docs/reference/testing.md +57 -0
  38. data/examples/graphql/README.md +14 -0
  39. data/examples/graphql/lexer.flexr.rb +29 -0
  40. data/examples/sql/README.md +16 -0
  41. data/examples/sql/lexer.flexr.rb +23 -0
  42. data/examples/toml/README.md +14 -0
  43. data/examples/toml/lexer.flexr.rb +23 -0
  44. data/gemfiles/ruby_lsp.gemfile +4 -0
  45. data/gemfiles/types.gemfile +7 -0
  46. data/lib/flexr/automaton/accel.rb +11 -1
  47. data/lib/flexr/automaton/analysis.rb +161 -7
  48. data/lib/flexr/automaton/compiler.rb +175 -24
  49. data/lib/flexr/cli.rb +117 -17
  50. data/lib/flexr/codegen/firstmatch.rb +1 -7
  51. data/lib/flexr/codegen/table.rb +2 -2
  52. data/lib/flexr/configuration.rb +8 -0
  53. data/lib/flexr/deprecations.rb +26 -0
  54. data/lib/flexr/diagnostics.rb +39 -3
  55. data/lib/flexr/dsl.rb +11 -9
  56. data/lib/flexr/errors.rb +6 -6
  57. data/lib/flexr/generated.rb +27 -15
  58. data/lib/flexr/generator.rb +44 -29
  59. data/lib/flexr/grammar.rb +41 -0
  60. data/lib/flexr/ir.rb +2 -2
  61. data/lib/flexr/migration.rb +31 -0
  62. data/lib/flexr/minitest.rb +24 -0
  63. data/lib/flexr/options.rb +4 -2
  64. data/lib/flexr/regexp/matcher.rb +26 -0
  65. data/lib/flexr/regexp/normalizer.rb +4 -8
  66. data/lib/flexr/regexp/parser.rb +127 -21
  67. data/lib/flexr/regexp/tokenizer.rb +4 -4
  68. data/lib/flexr/rspec.rb +51 -0
  69. data/lib/flexr/runtime/core.rb +21 -15
  70. data/lib/flexr/runtime/errors.rb +6 -4
  71. data/lib/flexr/runtime/interpreter.rb +47 -45
  72. data/lib/flexr/runtime/location.rb +5 -4
  73. data/lib/flexr/runtime/token.rb +1 -0
  74. data/lib/flexr/runtime.rb +6 -2
  75. data/lib/flexr/source/passthrough.rb +14 -11
  76. data/lib/flexr/source/prism_reader.rb +45 -14
  77. data/lib/flexr/testing.rb +47 -0
  78. data/lib/flexr/trace.rb +197 -0
  79. data/lib/flexr/unicode/property.rb +1 -2
  80. data/lib/flexr/unicode/reference_regexp.rb +8 -9
  81. data/lib/flexr/version.rb +1 -1
  82. data/lib/flexr.rb +1 -0
  83. data/lib/ruby_lsp/flexr/addon.rb +114 -0
  84. data/sig/flexr.rbs +185 -0
  85. data/site/src/layouts/SiteLayout.astro +8 -2
  86. data/site/src/pages/index.astro +69 -155
  87. data/site/src/styles/custom.css +4 -287
  88. data/site/src/styles/landing.css +239 -0
  89. data/test/ruby_lsp_addon_spec.rb +135 -0
  90. data/tools/docs_verify.rb +1 -0
  91. data/tools/golden.rb +54 -0
  92. data/tools/release_notes.rb +16 -0
  93. metadata +44 -11
  94. data/benchmark/golden/calculator_lexer.sha256 +0 -1
  95. data/benchmark/golden/json_lexer.sha256 +0 -1
  96. data/benchmark/golden/regexp_tokenizer.sha256 +0 -1
  97. data/benchmark/golden/ruby_subset_lexer.sha256 +0 -1
  98. data/benchmark/golden/toy_lang_lexer.sha256 +0 -1
  99. data/benchmark/golden/with_lrama_lexer.sha256 +0 -1
  100. data/benchmark/golden/with_racc_lexer.sha256 +0 -1
  101. data/docs/releases/v1.1.0.md +0 -44
@@ -12,10 +12,12 @@ consuming tokens, or the [CLI](cli.md) when validating and generating files.
12
12
  - [Actions](actions.md) — methods available inside rule and EOF actions.
13
13
  - [Tokens and locations](tokens-and-locations.md) — exact return shapes and
14
14
  offsets.
15
+ - [Testing](testing.md) — RSpec and Minitest assertions and token snapshots.
15
16
  - [Errors](errors.md) — exception and input-recovery behavior.
16
17
  - [Regexp compatibility](regexp.md) — supported syntax and alternatives.
17
18
  - [CLI](cli.md) — commands, options, output, and exit statuses.
18
19
  - [Diagnostics](diagnostics.md) — diagnostic code catalog.
20
+ - [Ruby LSP](ruby-lsp.md) — editor diagnostics and rule hovers.
19
21
  - [Generated artifacts](generated-artifacts.md) — static evaluation, output
20
22
  headers, dependencies, and standalone files.
21
23
  - [Public API](public-api.md) — stable, experimental, and internal boundaries.
@@ -18,12 +18,13 @@ Without a command, `flexr SPEC.rb` generates Ruby. `SPEC.rb` is normally a
18
18
  | `stats SPEC` | Report DFA states, byte classes, and table sizes | JSON |
19
19
  | `tokens SPEC` | Print names declared by `emits` | Space-separated names |
20
20
  | `dot SPEC` | Render the DFA as Graphviz DOT | DOT on stdout |
21
- | `trace SPEC` | Print DFA states, accepts, and transitions | Text on stdout |
21
+ | `trace SPEC` | Print DFA states, or trace input when supplied | Human or JSON input trace |
22
22
  | `explain SPEC` | List parsed rules and patterns | Text on stdout |
23
23
  | `bench SPEC` | Run the benchmark harness | Benchmark output |
24
24
  | `import INPUT` | Translate flex/Rexical input | Ruby on stdout or `--output` |
25
25
 
26
- `trace` describes the automaton; it does not trace a particular input string.
26
+ `trace` without input describes the automaton. To execute a specification and
27
+ inspect rule selection, pass `--input STRING` or `--input-file PATH`.
27
28
  `dot` marks accepting states and accelerated regions for visual inspection.
28
29
 
29
30
  ## Options
@@ -39,11 +40,16 @@ Without a command, `flexr SPEC.rb` generates Ruby. `SPEC.rb` is normally a
39
40
  | `--table-compression VALUE` | `none`, `rows`, `full` | Generation |
40
41
  | `--table-format VALUE` | `literal`, `packed` | Generation |
41
42
  | `--max-dfa-states N` | Positive integer | Compilation |
42
- | `-W, --warn LEVEL` | `all`, `default`, `none` | Check |
43
+ | `-W, --warn LEVEL` | `all`, `default`, `none` | Check and generation |
43
44
  | `--warn-as-error` | Flag | Check and generation |
44
45
  | `--color WHEN` | `auto`, `always`, `never` | Human diagnostics |
45
- | `--format FMT` | `human`, `json` | Check and stats |
46
+ | `--format FMT` | `human`, `json`, `lrama` | `lrama` is for tokens only |
47
+ | `--grammar PATH` | Grammar path | Compare `%token` names with `emits` during check |
48
+ | `--target 2.0` | none | Static check migration requirements and structured-token declarations |
46
49
  | `--rule N` | Non-negative integer | Explain |
50
+ | `--input STRING` | Literal input, including an empty string | Trace |
51
+ | `--input-file PATH` | Input file | Trace and bench |
52
+ | `--generated` | Run the generated scanner | Input trace |
47
53
 
48
54
  The complete help output is kept as a checked snapshot:
49
55
 
@@ -64,11 +70,66 @@ Options:
64
70
  -W, --warn LEVEL all | default | none
65
71
  --warn-as-error
66
72
  --color WHEN auto | always | never
67
- --format FMT human | json
73
+ --format FMT human | json | lrama (tokens only)
74
+ --grammar PATH compare emits with grammar %token declarations (check only)
75
+ --input STRING trace input (executes SPEC actions)
76
+ --input-file PATH trace or benchmark input
77
+ --generated trace generated scanner
78
+ --target 2.0 report migration requirements (static check only)
68
79
  <!-- flexr-help:end -->
69
80
 
81
+ ## Input trace
82
+
83
+ ```sh
84
+ flexr trace lexer.flexr.rb --input 'if == total'
85
+ flexr trace lexer.flexr.rb --input-file input.txt --generated --format json
86
+ ```
87
+
88
+ Input tracing executes the specification's Ruby code and actions. Use trusted
89
+ specifications, as with running a lexer normally. Actions run once, including
90
+ actions that skip input or change state. `--generated` generates and evaluates
91
+ the scanner in memory; it does not write an output file.
92
+
93
+ Each step reports byte offsets, matched text, the lexical state before and
94
+ after the action, candidate rule numbers and lengths, the selected rule, and
95
+ the action result (`emit` or `skip`). Human candidate lengths use
96
+ `body/total`, with trailing context included only in the total. `rule_order`
97
+ means several candidates tied at the winning total length; `longest` means a
98
+ longer candidate won. `first_match` reports the experimental backend's rule
99
+ priority and preserves its regexp alternation behavior. `only_match` means
100
+ there was one candidate. EOF actions appear with reason `eof`; recovered
101
+ unmatched bytes appear with reason `unmatched` and no selected rule.
102
+
103
+ `--format json` returns an object with a `steps` array. Each candidate contains
104
+ `rule`, `patterns`, `length`, `trailing_length`, and `total_length`; lengths are
105
+ bytes. Steps include `byte_begin`, `byte_end`, `next_byte`, `state`,
106
+ `next_state`, `text`, `bytes`, `candidates`, `rule`, `patterns`, `reason`,
107
+ `action`, and `token`. `byte_end` is the scanner's matched end, while
108
+ `next_byte` reflects action changes such as `less`. `token` is the emitted
109
+ token's Ruby inspection string, or null when none was emitted. Invalid UTF-8
110
+ bytes are replaced in display text; `bytes` preserves the exact matched bytes.
111
+
112
+ Tracing adds candidate analysis and retains steps in memory; it is a debugging
113
+ tool, not a benchmark. Firstmatch candidate analysis reads the remaining
114
+ buffered input to inspect rules beyond the selected first match. It obeys the
115
+ lexer's buffer limit. Normal lexers do not load or run tracing code.
116
+
70
117
  ## Exit statuses
71
118
 
119
+ Compare a lexer with Lrama/Bison-style declarations:
120
+
121
+ ```sh
122
+ flexr check lexer.flexr.rb --grammar parse.y --warn-as-error
123
+ flexr tokens lexer.flexr.rb --format lrama
124
+ ```
125
+
126
+ The check compares named `%token` declarations before the grammar's first `%%`
127
+ with `emits`. It handles multiline declarations, numeric token IDs, type tags,
128
+ aliases, and comments. Literal character tokens are excluded. This checks token
129
+ names, not grammar validity or whether every action actually emits its declared
130
+ token. Exported names must be identifiers; numeric IDs and aliases remain the
131
+ grammar author's responsibility.
132
+
72
133
  | Status | Meaning |
73
134
  |---:|---|
74
135
  | `0` | Command completed; warnings do not fail by default |
@@ -78,3 +139,8 @@ Options:
78
139
  `check --format json` writes one JSON array to stdout. Human diagnostics go to
79
140
  stdout for `check` and errors go to stderr for command failures. Use
80
141
  `--warn-as-error` when a warning should fail CI.
142
+
143
+ `--warn all` also includes informational equal-length rule overlaps
144
+ (`FLEXR-I001`). These reports include an input example and do not fail
145
+ `--warn-as-error`. See [Diagnostics](diagnostics.md#informational-diagnostics)
146
+ for the supported backends and states.
@@ -12,6 +12,12 @@
12
12
  Runtime mode does not require Prism. Generated mode uses Prism only while
13
13
  reading the source; normal generated output does not need the generator.
14
14
 
15
+ ## flexr 1.x maintenance
16
+
17
+ The 1.x series receives fixes for critical bugs and security issues through
18
+ March 31, 2027. Use the [migration guide](../migration-v2.md) to prepare an
19
+ upgrade to 2.0. The 1.x runtime continues to support Ruby 3.1 and later.
20
+
15
21
  ## Matching and encoding
16
22
 
17
23
  UTF-8 and binary are the supported specification encodings. The automaton is
@@ -34,5 +40,6 @@ unless a migration is explicitly documented. Generated artifacts include a
34
40
  payload digest and Unicode version so stale or cross-version output can be
35
41
  detected. Do not assume that internal table layouts are stable.
36
42
 
37
- Stable backends are `table`, `direct`, and `auto`. `firstmatch` is experimental
38
- and does not share the longest-match promise.
43
+ All four backends are stable. `table`, `direct`, and `auto` use longest-match
44
+ selection; `firstmatch` follows rule and regexp alternative order. It does not
45
+ share the longest-match promise and must be selected explicitly.
@@ -3,8 +3,132 @@
3
3
  Diagnostics have a code, severity, message, optional location, help, and note.
4
4
  Use `flexr check SPEC --format json` to consume the structured form.
5
5
 
6
+ ## Source context in human output
7
+
8
+ When a diagnostic has a source range, human output includes the file, line,
9
+ column, source line, and carets. For example, given `lexer.flexr.rb`:
10
+
11
+ ```ruby
12
+ class Lexer < Flexr::Lexer
13
+ rule(/[a-z]+/, skip: true)
14
+ rule(/if/, skip: true)
15
+ end
16
+ ```
17
+
18
+ `flexr check lexer.flexr.rb --color never` prints:
19
+
20
+ ```text
21
+ warning[FLEXR-W001]: rule /if/ is unreachable (shadowed by rule /[a-z]+/)
22
+ --> lexer.flexr.rb:3:3
23
+ |
24
+ 3 | rule(/if/, skip: true)
25
+ | ^^^^^^^^^^^^^^^^^^^^^^
26
+ help: remove it, reorder the rules, or make its language distinct
27
+ note: example: "if" selects rule /[a-z]+/ (same length, earlier rule wins)
28
+ ```
29
+
30
+ Line and column numbers are one-based. Columns count characters rather than
31
+ UTF-8 bytes. A multiline range shows its first source line; an empty range
32
+ gets one caret. Tabs are preserved, but wide or combining characters can make
33
+ caret alignment differ from terminal display widths.
34
+
35
+ Source ranges are available for:
36
+
37
+ - Ruby syntax errors (`FLEXR-E010`).
38
+ - Static DSL errors: unsupported encoding (`FLEXR-E011`), `reject` actions
39
+ (`FLEXR-E013`), unresolved expressions or unsupported DSL structure
40
+ (`FLEXR-E017`), and invalid pattern or trailing-context types (`FLEXR-E018`).
41
+ - Rule compilation errors, including empty matches (`FLEXR-E005`), repetition
42
+ limits (`FLEXR-E007`), misplaced anchors (`FLEXR-E009`), and unsupported regexp
43
+ constructs (`FLEXR-E014`), including errors in trailing context.
44
+ - Rule warnings: `FLEXR-W001`, `FLEXR-W003`, `FLEXR-W010`, `FLEXR-W012`,
45
+ `FLEXR-W013`, and `FLEXR-W014`.
46
+ - Rule overlap information: `FLEXR-I001`, pointing to the later rule.
47
+ - State and specification diagnostics: DFA state limits (`FLEXR-E006`), empty
48
+ states (`FLEXR-W002`), large transition tables (`FLEXR-W011`), and slow DFA
49
+ construction (`FLEXR-W016`).
50
+
51
+ Rule diagnostics highlight the complete rule declaration. `FLEXR-W010` points
52
+ to the later rule in the reported pair. `FLEXR-W014` points to the first rule
53
+ emitting the undeclared token, with one warning per token.
54
+
55
+ Compiler diagnostics identify rules by their patterns rather than numeric
56
+ indices. Regexp options, quoted strings, arrays of patterns, and `followed_by:`
57
+ context are preserved in these labels. For longest-match specifications without
58
+ trailing context, `FLEXR-W001` also names the patterns that shadow the unreachable
59
+ rule; otherwise, its example identifies the verified winner. `FLEXR-E006` lists
60
+ the patterns in the state being compiled. String trailing context is shown as
61
+ its compiled literal regexp. These labels also appear in JSON messages and when
62
+ source ranges are unavailable.
63
+
64
+ `FLEXR-W001` notes include a shortest nonempty input, measured in bytes, and
65
+ the rule selected for that input. Examples for named lexical states
66
+ include the state name. Control characters and binary bytes are escaped, and
67
+ the same note is preserved in JSON and generated artifacts.
68
+
69
+ For `firstmatch`, the candidate is checked with the scanner's regexp matcher
70
+ without running actions. The note identifies the first rule selected and its
71
+ matched byte count, which can be shorter than the example input. For example,
72
+ `"ab"` can select an earlier `/a/` rule with a one-byte match.
73
+
74
+ For states using `followed_by:`, the candidate includes the shortest trailing
75
+ input and is checked using the active rules. Longest-match backends compare
76
+ the combined body and lookahead length; `firstmatch` keeps rule-order priority.
77
+ The note separates consumed bytes from lookahead bytes. For example, two rules
78
+ with body `/a/` and trailing context `/b+/` produce this note with backend `table`:
79
+
80
+ ```text
81
+ example: "ab" selects rule /a/, followed_by: /b+/ (longest total match, then rule order; matched 1 byte, looked ahead 1 byte)
82
+ ```
83
+
84
+ If a checked candidate selects the supposedly unreachable rule in any lexical
85
+ state, W001 is omitted for that rule. No rule actions run during these checks.
86
+ An example is omitted when the candidate is empty, invalid in the specification's
87
+ encoding, or cannot be confirmed by the matcher. This includes trailing patterns
88
+ that the runtime cannot compile. Candidate generation does not search for an
89
+ alternative if anchor or encoding checks reject the shortest candidate.
90
+
91
+ Static DSL errors highlight the offending DSL call. Unresolved expressions
92
+ point to the expression instead; ambiguous lexer selection points to the
93
+ second lexer class containing DSL calls.
94
+
95
+ State diagnostics point to the first declaration of the affected state.
96
+ `FLEXR-E006` identifies the state being compiled, while `FLEXR-W011` identifies
97
+ the state with the largest transition table. The implicit `initial` state
98
+ and the total construction time (`FLEXR-W016`) point to the lexer class.
99
+
100
+ Rules and states created directly through the runtime DSL do not carry source
101
+ ranges. Diagnostics without ranges, or whose source cannot be read, retain
102
+ the message, help, and note without a source excerpt.
103
+
104
+ For direct rendering, pass the matching source text and optional file path to
105
+ `DiagnosticSet#render(source: source, path: path)`. Without a path, the label is
106
+ `(source)`. JSON output keeps `location` as a pair of byte offsets with an
107
+ exclusive end; source excerpts are not added to JSON.
108
+
109
+ Runtime input diagnostics (`FLEXR-E003`, `FLEXR-E012`, `FLEXR-E013`,
110
+ and `FLEXR-E021` through `FLEXR-E027`)
111
+ use `[byte_pos, byte_pos]` to mark their reported position in the original
112
+ input. Guard notes identify the affected rule by its pattern when available,
113
+ using the same labels as compiler diagnostics. Pass the complete input to
114
+ `DiagnosticSet#render` to show its line and caret; see
115
+ [errors and recovery](errors.md). These offsets remain absolute
116
+ when a streaming buffer discards consumed input. If the CLI reports a runtime
117
+ input failure, it reads the error's `filename` for context, preserving the
118
+ input's original line endings. To bound memory use while reporting input
119
+ failures, the CLI omits excerpts for input files larger than 64 KiB. An absent
120
+ or unreadable input file also produces the diagnostic without an excerpt.
121
+
6
122
  ## Errors
7
123
 
124
+ If Prism cannot be loaded (`FLEXR-E019`), the diagnostic points to the start of
125
+ the specification and suggests installing the dependency. This is a file-level
126
+ failure; no Ruby syntax has been parsed yet.
127
+
128
+ Incompatible generated artifacts (`FLEXR-E020`) identify the start of the file
129
+ that installs the artifact, when that file exists. Direct installation calls
130
+ without a readable caller file still report the regeneration guidance.
131
+
8
132
  | Code | Meaning | First fix to try |
9
133
  |---|---|---|
10
134
  | `FLEXR-E001` | Regexp syntax is malformed | Correct the regexp syntax |
@@ -30,6 +154,162 @@ Use `flexr check SPEC --format json` to consume the structured form.
30
154
  | `FLEXR-E026` | A cancellation callback stopped lexing | Resume with a new lexer when ready |
31
155
  | `FLEXR-E027` | An error callback returned an unknown action | Return `:skip`, `:raise`, `:token`, or `:halt` |
32
156
 
157
+ ### Regexp rewrite help
158
+
159
+ For positive lookahead, `FLEXR-E014` includes a concrete example:
160
+
161
+ ```text
162
+ error[FLEXR-E014]: look-around is not supported by flexr
163
+ help: for a trailing positive lookahead, rewrite rule(/a(?=b)/) as rule(/a/, followed_by: /b/); keep the action
164
+ ```
165
+
166
+ Keep the action or `emit:` / `skip:` options on the rewritten rule. The example
167
+ consumes `a` only when `b` follows, leaving `b` for the next token. The combined
168
+ body and trailing length participates in longest-match selection.
169
+
170
+ Adapt this example to positive lookahead at the end of a rule's pattern,
171
+ preserving the regexp options needed by each part. Lookahead inside a pattern
172
+ needs a separate rewrite. Negative lookahead and lookbehind receive separate
173
+ guidance to use rules or states because `followed_by:` cannot express those
174
+ assertions.
175
+
176
+ For lazy and possessive quantifiers, `FLEXR-E014` shows a negated character
177
+ class for unescaped, single-line quoted text:
178
+
179
+ ```text
180
+ error[FLEXR-E014]: lazy or possessive quantifier is not supported by flexr
181
+ help: for unescaped single-line quoted text, use a negated character class: rule(/"[^"\n]*"/); keep the action; removing lazy or possessive modifiers can change matches
182
+ ```
183
+
184
+ For this form of quoted text, `/".*?"/` and `/"[^"\n]*+"/` can both be
185
+ rewritten as `/"[^"\n]*"/`. Excluding the closing quote makes the match stop
186
+ at the first delimiter, so adjacent strings remain separate tokens. Excluding
187
+ `\n` preserves the line-feed restriction of `.` without `/m`. Escaped quotes
188
+ and multiline strings need a different pattern. Simply dropping a lazy or
189
+ possessive modifier can change which inputs match or how much text is consumed.
190
+
191
+ For atomic groups, the same quoted-text rule can replace `/"(?>[^"\n]*)"/`.
192
+ The repeated character class cannot consume the closing quote, so this example
193
+ does not need backtracking to find it. The diagnostic warns against removing
194
+ atomic groups in general: [Ruby's atomic grouping](https://docs.ruby-lang.org/en/4.0/Regexp.html)
195
+ can change whether the rest of a pattern matches. The example still excludes
196
+ escaped quotes and line feeds.
197
+
198
+ For numbered backreferences, the help uses a capture with only two possible
199
+ values. Rewrite `rule(/(a|b)\1/)` or `rule(/(a|b)\k<1>/)` as
200
+ `rule(/(?:aa|bb)/)` and keep the action. This accepts `aa` and `bb`, while
201
+ rejecting `ab` and `ba`. It reproduces the matched text; flexr does not expose
202
+ capture values to actions. If the repeated text cannot be enumerated, split
203
+ the rule into states and compare the matched text in actions instead.
204
+
205
+ For a non-recursive subexpression call, the help shows
206
+ `rule(/(a|b)\g<1>/)` becoming `rule(/[ab]{2}/)` with the same action.
207
+ A call reuses the expression, not the previously captured text: this example
208
+ accepts `aa`, `ab`, `ba`, and `bb`. Recursive calls need a parser or a state
209
+ stack; they cannot be expanded by this example.
210
+
211
+ For named captures used only for grouping, rewrite `rule(/(?<word>ab)/)` or
212
+ `rule(/(?'word'ab)/)` as `rule(/(?:ab)/)` and keep the action. Backreferences
213
+ and conditions that depend on the capture need a separate rewrite; removing
214
+ the name alone does not preserve them.
215
+
216
+ For the conditional example `rule(/(a)?(?(1)b|c)/)`, use `rule(/(?:ab|c)/)`
217
+ with the same action. The original matches `ab` when the optional `a` was
218
+ captured, or `c` otherwise. Other conditions need their own alternatives,
219
+ separate rules, or state checks.
220
+
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]*`.
226
+
227
+ For word boundaries, the help uses examples where the surrounding literals
228
+ already determine the assertion. Rewrite `rule(/foo\b:/)` as `rule(/foo:/)`:
229
+ `o` is a word character and `:` is not. Similarly, `rule(/foo\Bbar/)` becomes
230
+ `rule(/foobar/)` because `o` and `b` are both word characters. Keep the action.
231
+ These examples do not cover boundaries at token edges, such as `/if\b/`;
232
+ those need separate rules or state checks. Inside a character class, `[\b]`
233
+ remains a supported backspace escape. The escapes `\A`, `\B`, `\G`, `\z`,
234
+ `\Z`, `\K`, `\g`, and `\X` inside character classes receive literal-character examples,
235
+ such as rewriting `rule(/[\A]/)` as `rule(/[A]/)` with the same action.
236
+
237
+ For `\G` at the start of a rule, rewrite `rule(/\Ga/)` as `rule(/a/)` and
238
+ keep the action. Every flexr rule already starts matching at the current
239
+ lexer position, including after earlier tokens. A `\G` inside a larger
240
+ pattern needs a separate rewrite.
241
+
242
+ For input without line feeds, `rule(/\Aa/)` can become `rule(/^a/)`, and
243
+ `rule(/a\z/)` or `rule(/a\Z/)` can become `rule(/a$/)`. Keep the action.
244
+ This requires a guarantee about the entire input, not just the token pattern.
245
+ [Ruby's line and string anchors](https://docs.ruby-lang.org/en/4.0/Regexp.html)
246
+ differ when line feeds occur: `^` can match after a line feed, `$` can match
247
+ before one, and `\Z` also permits a final line feed. Do not apply these
248
+ examples to multiline input.
249
+
250
+ For the match-reset example `rule(/a\Kb/) { emit :B }` in `:initial`, the help
251
+ splits the prefix and suffix into separate rules:
252
+
253
+ ```ruby
254
+ rule(/a/, followed_by: /b/) { begin_state :suffix }
255
+ state(:suffix) do
256
+ rule(/b/) { begin_state :initial; emit :B }
257
+ end
258
+ ```
259
+
260
+ The prefix is consumed only when `b` follows. The suffix rule returns to
261
+ `:initial` before running the original action, so `text` and the token location
262
+ cover only `b`. Keep the original action in place of `emit :B`, choose an unused
263
+ state name, and return to the original state when adapting the example.
264
+ Larger patterns and competing rules need a separate review; this is not an
265
+ automatic replacement for every `\K`.
266
+
267
+ Escape diagnostics also include concrete examples. For an octal tab,
268
+ rewrite `rule(/\011/)` as `rule(/\x09/)`. For an unknown escape such as `\q`,
269
+ use `rule(/q/)` only if `q` is the intended literal character. Keep the original
270
+ action and regexp options when adapting either example.
271
+
272
+ Inside a character class, `\1` is a character code. Its diagnostic recommends
273
+ an explicit hexadecimal escape such as `rule(/[\x01]/)`, which matches the same
274
+ control character as `rule(/[\1]/)`.
275
+
276
+ When raw regexp sources contain control or meta escapes, their help gives
277
+ hexadecimal alternatives: `rule(/\cA/)` and `rule(/\C-A/)` become
278
+ `rule(/\x01/)`, while `rule(/\M-a/n)` becomes `rule(/\xE1/n)` for binary input.
279
+ Keep the action and use the corresponding hexadecimal value for other
280
+ characters. Ruby may already normalize these escapes in regexp literals;
281
+ the diagnostics apply when the escape reaches the parser unchanged.
282
+
283
+ For a standalone `\X` token with ASCII-only input, use:
284
+
285
+ ```ruby
286
+ rule(/\r\n|[\x00-\x7f]/) { emit :GRAPHEME }
287
+ ```
288
+
289
+ This keeps CRLF together and consumes other ASCII characters individually.
290
+ Keep the original action; the CRLF alternative must come first for `firstmatch`.
291
+ The example does not cover Unicode graphemes or `\X` inside larger patterns.
292
+ Unicode graphemes need separate segmentation, such as Ruby's
293
+ [`String#each_grapheme_cluster`](https://docs.ruby-lang.org/en/4.0/String.html#method-i-each_grapheme_cluster).
294
+
295
+ For `\R`, the help chooses a standalone line-break rule for the specification's
296
+ encoding. In UTF-8, use:
297
+
298
+ ```ruby
299
+ rule(/\r\n|[\n\v\f\r\u0085\u2028\u2029]/) { emit :NEWLINE }
300
+ ```
301
+
302
+ This covers the [line breaks recognized by Ruby's `\R`](https://docs.ruby-lang.org/en/4.0/Regexp.html#class-Regexp-label-Shorthand+Character+Classes).
303
+ With `encoding Encoding::BINARY`, the example uses only ASCII line breaks:
304
+
305
+ ```ruby
306
+ rule(/\r\n|[\n\v\f\r]/n) { emit :NEWLINE }
307
+ ```
308
+
309
+ CRLF comes first so that `firstmatch` also consumes the pair as one token.
310
+ Apply these examples as separate token rules. Replacing `\R` inside a larger
311
+ expression can change how the surrounding pattern matches.
312
+
33
313
  ## Warnings
34
314
 
35
315
  | Code | Meaning | First fix to try |
@@ -43,7 +323,65 @@ Use `flexr check SPEC --format json` to consume the structured form.
43
323
  | `FLEXR-W013` | Capturing group is treated as non-capturing | Use `(?:...)` |
44
324
  | `FLEXR-W014` | Emitted token is not declared | Add it to `emits` or accept the warning |
45
325
  | `FLEXR-W016` | DFA construction took more than 0.5 seconds | Use generated mode for startup |
326
+ | `FLEXR-W017` | Grammar token is missing from `emits` | Declare and emit it, or remove it from the grammar |
327
+ | `FLEXR-W018` | Token in `emits` is missing from the grammar | Add a `%token` declaration or remove it from `emits` |
328
+ | `FLEXR-W020` | Structured records become immutable in v2.0 | Review consumer mutations; use `record.with(...)` in v2 |
46
329
 
47
330
  Warnings are reported by `check` according to `--warn`. The default omits the
48
331
  wall-clock `FLEXR-W016`; `--warn all` includes it, and `--warn none` suppresses
49
332
  warnings. `--warn-as-error` turns selected warnings into a failed command.
333
+
334
+ ## Informational diagnostics
335
+
336
+ `flexr check --target 2.0` includes `FLEXR-I002` with Ruby and artifact
337
+ requirements. See the [v2 migration guide](../migration-v2.md) for the static
338
+ check's limits and opt-in runtime deprecation warnings.
339
+
340
+ `check --warn all` also reports `FLEXR-I001` when multiple rules match the same
341
+ input at the same length. This can happen even when both rules are reachable.
342
+ For example, `rule(%w[a c], emit: :FIRST)` followed by
343
+ `rule(%w[b c], emit: :SECOND)` produces:
344
+
345
+ ```text
346
+ info[FLEXR-I001]: rule ["b", "c"] overlaps rule ["a", "c"] at the same length
347
+ help: reorder the rules if the later rule should take priority
348
+ note: example: "c" selects rule ["a", "c"] (1 byte, earlier rule wins)
349
+ ```
350
+
351
+ Source context highlights the later rule. At most one shortest nonempty example
352
+ is reported for each later rule in each lexical state; this is not an exhaustive
353
+ list of overlapping rule pairs. Named states appear in the note, and lengths
354
+ are measured in bytes. Invalid input for the specification's encoding is omitted.
355
+
356
+ For `firstmatch`, the candidate is checked with the scanner's regexp matcher.
357
+ The diagnostic is emitted only if the first selected rule and the later rule
358
+ match the same number of bytes. For example, `/a|ab/` followed by `/ab/` does not
359
+ produce a tie for `"ab"`: the first rule matches one byte and the second matches
360
+ two. The note reports the actual matched byte count, including zero when empty
361
+ matches are allowed. No actions run during the check. If the shortest candidate
362
+ does not confirm a tie, no alternative candidate is searched.
363
+
364
+ For states using `followed_by:`, the candidate combines the shortest body with
365
+ the shortest trailing input. The check compares the total length, so rules can
366
+ overlap even when they consume different numbers of bytes. For example,
367
+ `rule(/a/, followed_by: /bc/, emit: :FIRST)` followed by
368
+ `rule(/ab/, followed_by: /c/, emit: :SECOND)` produces:
369
+
370
+ ```text
371
+ info[FLEXR-I001]: rule /ab/, followed_by: /c/ overlaps rule /a/, followed_by: /bc/ at the same total length
372
+ help: reorder the rules if the later rule should take priority
373
+ note: example: "abc" selects rule /a/, followed_by: /bc/ (same total length, earlier rule wins; matched 1 byte, looked ahead 2 bytes)
374
+ ```
375
+
376
+ The winner is checked against the full candidate, including trailing matches
377
+ and end anchors. The note separates the bytes consumed from those looked ahead.
378
+ For `firstmatch`, it says `first match wins` and requires the selected rule and
379
+ the later rule to have equal total lengths. Empty input, invalid character
380
+ boundaries, and candidates the runtime's trailing matcher cannot verify are
381
+ omitted. A rejected shortest candidate does not trigger a search for alternatives.
382
+
383
+ This analysis covers `table`, `direct`, `auto`, and `firstmatch` states.
384
+ `--warn default` and `--warn none` omit this information, and
385
+ `--warn-as-error` does not turn informational diagnostics into failures.
386
+ Generation with `--warn all` also preserves the information in generated and
387
+ standalone artifacts.
@@ -49,10 +49,13 @@ does not restrict `emit` at runtime.
49
49
 
50
50
  ## `backend(name)`
51
51
 
52
- Selects `:table`, `:direct`, `:auto`, or the experimental `:firstmatch`
52
+ Selects `:table`, `:direct`, `:auto`, or `:firstmatch`
53
53
  backend. `:table`, `:direct`, and `:auto` preserve the stable longest-match
54
- contract. `:firstmatch` requires `option :experimental` and can change the
55
- meaning of overlapping rules.
54
+ contract. `:firstmatch` selects the first matching rule in source order,
55
+ even when a later rule could consume more text. Within a rule, alternation
56
+ also follows Ruby regexp order. Choose it when rule priority is intentional;
57
+ the compiler reports possible differences from longest-match as `FLEXR-W010`.
58
+ The default remains `:table`.
56
59
 
57
60
  ## `token_kind(name)`
58
61
 
@@ -69,8 +72,8 @@ model is valid.
69
72
  ## `option(*values)`
70
73
 
71
74
  Adds boolean options to the specification. Stable options include
72
- `:unicode`, `:eager_columns`, and `:allow_empty_match`. `:experimental` is a
73
- capability opt-in for experimental behavior such as `:firstmatch`.
75
+ `:unicode`, `:eager_columns`, and `:allow_empty_match`. `:experimental` remains
76
+ accepted for compatibility; it is no longer required by `:firstmatch`.
74
77
  `:standalone` is build metadata used by standalone generation. Unknown option
75
78
  names raise immediately so misspellings cannot silently change behavior.
76
79
 
@@ -16,6 +16,34 @@ text, and optional diagnostic. `Flexr::Runtime::TokenTooLargeError` is a
16
16
  [diagnostics catalog](diagnostics.md). `StateStackOverflowError` is raised when
17
17
  a `push` would exceed `max_state_stack`.
18
18
 
19
+ Undefined state transitions (`FLEXR-E003`) and `reject` calls (`FLEXR-E013`)
20
+ at runtime continue to raise `Flexr::CompileError`. These errors also expose
21
+ the input `filename` and `byte_pos`, with a diagnostic point range at the
22
+ current cursor. During a rule action this is after the matched input, unless
23
+ the action moved the cursor with `less`. EOF actions point to the end of input.
24
+ Static `reject` diagnostics still highlight the rule in the specification.
25
+
26
+ Guard diagnostics include a point range at `byte_pos` when it is known. This
27
+ is the guard's reported input position, which may be the start of a token or
28
+ the end of the loaded buffer. In a rescue handler, render it with the complete
29
+ original input:
30
+
31
+ ```ruby
32
+ diagnostics = Flexr::DiagnosticSet.new << error.diagnostic
33
+ warn diagnostics.render(source: original_input, path: error.filename)
34
+ ```
35
+
36
+ With `retain_input: false`, `lexer.input` contains only the retained buffer;
37
+ keep the original input separately if a source excerpt is needed. JSON
38
+ diagnostics retain absolute byte offsets even after input has been discarded.
39
+
40
+ When a guard knows the affected rule, its diagnostic note shows the pattern,
41
+ including regexp options and trailing context, for example `rule /a+/i at
42
+ byte 0`. The error's `rule` attribute still returns the numeric rule index.
43
+ Errors constructed with only a numeric index retain a numeric label; guards
44
+ without an identified rule omit the note. The diagnostic location continues
45
+ to refer to the input, not the rule declaration.
46
+
19
47
  The default `error_mode: :raise` raises the lexical error. `:token` queues
20
48
  `[:error, text]`; `:panic` discards the unmatched byte and continues. An
21
49
  `on_error` callback can return `:skip`, `:raise`, `:token`, or `:halt` for
@@ -75,3 +75,14 @@ These settings affect artifact size and loading cost, not matching semantics.
75
75
  Standalone output embeds a runtime core, not the compiler or generator. The
76
76
  regexp reference matcher and vendored Unicode tables are added only for a
77
77
  `firstmatch` backend or trailing-context rules that can use that matcher.
78
+
79
+ The generator compiles `followed_by:` expressions into read-only DFA tables
80
+ stored with their rules. These tables use the selected compression and table
81
+ format, so scanning trailing context does not require the compiler at runtime.
82
+ Any errors found while compiling them are reported at the rule declaration
83
+ before an existing output file is replaced.
84
+
85
+ The optional `trailing_dfa` rule field is compatible with schema 1. The runtime
86
+ continues to compile trailing context lazily for older generated files without
87
+ this field. Regenerate older standalone lexers that use `followed_by:` to fix
88
+ their missing `Flexr.compile_pattern` error.
@@ -24,16 +24,40 @@ in the reference pages and are covered by the test suite.
24
24
  constants through autoload. Applications that want an explicit load boundary
25
25
  can require `flexr/runtime`, `flexr/generator`, or `flexr/cli` directly.
26
26
 
27
- ## Experimental
27
+ Runtime and generated `firstmatch` modes are stable and use the same rule-order
28
+ semantics. The former `option :experimental` opt-in remains accepted but is
29
+ unnecessary. Differences from longest-match behavior are reported as
30
+ `FLEXR-W010` rather than making generation fail.
31
+ ## Type signatures
32
+
33
+ The gem includes RBS signatures in `sig/flexr.rbs` for the stable API above.
34
+ Steep can discover them through an application's RBS collection or an explicit
35
+ `library "flexr"` entry. User-defined token values and internal regexp ASTs
36
+ remain `untyped`; the signatures do not make internal tables stable APIs.
37
+ Token and location fields are nullable because their public constructors allow
38
+ omitted keywords. The token representation remains a union of arrays and
39
+ structured tokens because `token_kind` is selected dynamically.
40
+ Cancellation signatures accept Procs, Methods, and callable objects taking zero
41
+ arguments or the lexer. `on_error=` accepts a callable returning `:skip`,
42
+ `:raise`, `:token`, or `:halt`; assigning nil clears the handler.
43
+
44
+ Validate the signatures and type-check the consumer example with:
28
45
 
29
- - `backend :firstmatch` and `--backend firstmatch`.
30
- - `option :experimental` as the opt-in for experimental behavior.
31
- - Any future backend or option explicitly marked experimental in its reference.
46
+ ```sh
47
+ BUNDLE_GEMFILE=gemfiles/types.gemfile bundle install
48
+ BUNDLE_GEMFILE=gemfiles/types.gemfile bundle exec rbs -I sig validate
49
+ BUNDLE_GEMFILE=gemfiles/types.gemfile bundle exec steep check
50
+ ```
51
+
52
+ The type-checking tools require Ruby 3.3 or newer and run in a separate CI
53
+ matrix for Ruby 3.3, 3.4, and 4.0. They add no runtime dependency to flexr.
54
+ This checks the public signatures and a typed consumer, not the entire internal
55
+ implementation.
56
+
57
+ ## Experimental
32
58
 
33
- Experimental behavior may change semantics or output between minor releases.
34
- `firstmatch` requires the opt-in. Runtime and generated `firstmatch` modes use
35
- the same rule-order semantics; overlap with longest-match behavior is reported
36
- as `FLEXR-W010` rather than making generation fail.
59
+ Future backends or options explicitly marked experimental in their reference
60
+ may change semantics or output between minor releases.
37
61
 
38
62
  ## Internal; compatibility is not guaranteed
39
63