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
|
@@ -19,7 +19,16 @@ At EOF the method returns `[false, "$end"]`. Keep the names in `emits` aligned
|
|
|
19
19
|
with the grammar's `%token` declarations. `flexr tokens SPEC.flexr.rb` prints
|
|
20
20
|
the declared names for a build-time comparison.
|
|
21
21
|
|
|
22
|
+
A parser adapter that already knows which terminals are valid at its current
|
|
23
|
+
position can pass their Symbols to `lexer.racc_next_token(expect: terminals)`.
|
|
24
|
+
The adapter needs to provide this array for each request. See
|
|
25
|
+
[parser-driven lexing](parser-driven-lexing.md) for examples.
|
|
26
|
+
|
|
22
27
|
The complete executable lexer is in
|
|
23
28
|
[examples/with_racc](../../examples/with_racc/README.md). Generate the lexer
|
|
24
29
|
before running a parser build if the parser should consume a committed Ruby
|
|
25
30
|
artifact.
|
|
31
|
+
|
|
32
|
+
Generate a complete working example with `flexr new NAME --parser racc`.
|
|
33
|
+
Follow the [calculator tutorial](../tutorial/build-a-calculator-parser.md#racc)
|
|
34
|
+
to build and test it.
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Migrate from oedipus_lex
|
|
2
|
+
|
|
3
|
+
Select the source format explicitly because both Rexical and oedipus_lex use
|
|
4
|
+
`.rex` files.
|
|
5
|
+
|
|
6
|
+
```sh
|
|
7
|
+
flexr import lexer.rex --from oedipus_lex -o lexer.flexr.rb
|
|
8
|
+
flexr check lexer.flexr.rb
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
The generated specification uses `backend :firstmatch`. Rules retain their
|
|
12
|
+
source order, and alternatives within a regexp retain Ruby's matching order.
|
|
13
|
+
Compare the old and new token pairs on your inputs before switching parsers.
|
|
14
|
+
|
|
15
|
+
## Supported forms
|
|
16
|
+
|
|
17
|
+
The importer accepts one lexer class in a UTF-8 file. It handles
|
|
18
|
+
slash-delimited regexps with no flag, `/i`, or `/o`, and interpolation of
|
|
19
|
+
previously declared regexp macros such as `/#{NUMBER}/`. A rule without
|
|
20
|
+
an action skips its match. So do the blocks `{ nil }` and `{ next }`.
|
|
21
|
+
|
|
22
|
+
A one-line action can return `[kind, value]`. The kind can be a symbol,
|
|
23
|
+
string, or `text`. The value can be a literal, `text`, or a call to
|
|
24
|
+
`text.to_i`, `text.to_f`, or `text.to_sym` without arguments.
|
|
25
|
+
|
|
26
|
+
Unsupported input produces line-specific diagnostics and exits with status
|
|
27
|
+
`1`. Use the reported lines to identify code that needs to move or change.
|
|
28
|
+
The importer writes output only after the whole input passes validation.
|
|
29
|
+
|
|
30
|
+
## Trial against ruby_parser
|
|
31
|
+
|
|
32
|
+
The `lib/ruby_lexer.rex` shipped with
|
|
33
|
+
[ruby_parser 3.22.0](https://rubygems.org/gems/ruby_parser/versions/3.22.0)
|
|
34
|
+
uses 88 rules, including 64 inside 15 guard groups. The native generator
|
|
35
|
+
also reports 13 regexp macros, 12 predicates, and 35 method actions. The
|
|
36
|
+
lexer enables `lineno` and `column` and runs a six-line `start` hook for each
|
|
37
|
+
token. The trial produced 111 diagnostics and left the output path empty.
|
|
38
|
+
|
|
39
|
+
A manual port needs to preserve the prefix guards and parser-dependent
|
|
40
|
+
state, then rewrite the actions and coordinate their input positions with
|
|
41
|
+
Flexr's runtime. The
|
|
42
|
+
[oedipus_lex rules reference](https://docs.seattlerb.org/oedipus_lex/)
|
|
43
|
+
describes these source constructs.
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# Select a token using parser context
|
|
2
|
+
|
|
3
|
+
Pass `expect:` when the parser knows which terminal types it can consume.
|
|
4
|
+
The array applies to the next token request, including any skipped input
|
|
5
|
+
or trivia before that token.
|
|
6
|
+
|
|
7
|
+
## Ruby division and regexp literals
|
|
8
|
+
|
|
9
|
+
This subset handles the two meanings of `/`. At the start of an expression,
|
|
10
|
+
the parser can request `REGEXP`; after an operand, it can request `DIV`.
|
|
11
|
+
|
|
12
|
+
```ruby
|
|
13
|
+
class SlashLexer < Flexr::Lexer
|
|
14
|
+
rule(/[ \t]+/, skip: true)
|
|
15
|
+
rule(%r{/(?:[^/\\\n]|\\.)+/}, emit: :REGEXP)
|
|
16
|
+
rule("/", emit: :DIV)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
SlashLexer.new("/a/").next_token(expect: [:REGEXP]) # => [:REGEXP, "/a/"]
|
|
20
|
+
SlashLexer.new("/a/").next_token(expect: [:DIV]) # => [:DIV, "/"]
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## JavaScript template text and expressions
|
|
24
|
+
|
|
25
|
+
The parser requests text and delimiters between backticks. After `${` it
|
|
26
|
+
requests expression tokens until the closing brace. This subset recognizes
|
|
27
|
+
a text segment and one identifier inside an interpolation.
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
class TemplateLexer < Flexr::Lexer
|
|
31
|
+
rule("`", emit: :BACKTICK)
|
|
32
|
+
rule('${', emit: :INTERPOLATE)
|
|
33
|
+
rule("}", emit: :CLOSE_BRACE)
|
|
34
|
+
rule(/[a-z_][a-z_0-9]*/i, emit: :IDENT)
|
|
35
|
+
rule(/(?:[^`$\\]|\\.)+/, emits: :TEMPLATE_TEXT) { emit :TEMPLATE_TEXT, text }
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
lexer = TemplateLexer.new('`hi ${name}`')
|
|
39
|
+
lexer.next_token(expect: [:BACKTICK]) # => [:BACKTICK, "`"]
|
|
40
|
+
lexer.next_token(expect: %i[TEMPLATE_TEXT INTERPOLATE BACKTICK]) # => [:TEMPLATE_TEXT, "hi "]
|
|
41
|
+
lexer.next_token(expect: %i[TEMPLATE_TEXT INTERPOLATE BACKTICK]) # => [:INTERPOLATE, "${"]
|
|
42
|
+
lexer.next_token(expect: [:IDENT]) # => [:IDENT, "name"]
|
|
43
|
+
lexer.next_token(expect: [:CLOSE_BRACE]) # => [:CLOSE_BRACE, "}"]
|
|
44
|
+
lexer.next_token(expect: %i[TEMPLATE_TEXT INTERPOLATE BACKTICK]) # => [:BACKTICK, "`"]
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
In a parser loop, compute the array from the current parser state. For a
|
|
48
|
+
token/value pair and the Racc EOF convention, use `racc_next_token(expect:)`.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Try the Playground demos
|
|
2
|
+
|
|
3
|
+
Each link loads a lexer specification and its input. Select **Run** to inspect
|
|
4
|
+
the tokens, diagnostics, and match decisions.
|
|
5
|
+
|
|
6
|
+
- [Stop at the first closing comment delimiter](https://ydah.github.io/flexr/playground/#eyJzb3VyY2UiOiJjbGFzcyBMZXhlciA8IEZsZXhyOjpMZXhlclxuICBydWxlKCVyey9cXCooP35cXCovKVxcKi99LCBlbWl0OiA6Q09NTUVOVClcbiAgcnVsZSgvICsvLCBza2lwOiB0cnVlKVxuZW5kIiwiaW5wdXQiOiIvKiBmaXJzdCAqLyAvKiBzZWNvbmQgKi8iLCJtb2RlIjoiZ2VuZXJhdGVkIn0).
|
|
7
|
+
The absence group stops each comment at its own closing delimiter.
|
|
8
|
+
- [Reproduce the shadowed `if` rule](https://ydah.github.io/flexr/playground/#eyJzb3VyY2UiOiJjbGFzcyBMZXhlciA8IEZsZXhyOjpMZXhlclxuICBydWxlKC9bYS16XSsvLCBlbWl0OiA6SURFTlQpXG4gIHJ1bGUoXCJpZlwiLCBlbWl0OiA6SUYpXG5lbmQiLCJpbnB1dCI6ImlmIiwibW9kZSI6ImdlbmVyYXRlZCJ9).
|
|
9
|
+
The diagnostic gives `if` as an example because the earlier identifier rule wins a tie.
|
|
10
|
+
- [Subtract the keyword from the identifier pattern](https://ydah.github.io/flexr/playground/#eyJzb3VyY2UiOiJjbGFzcyBMZXhlciA8IEZsZXhyOjpMZXhlclxuICBJREVOVCA9IHBhdHRlcm4oL1thLXpdKy8pXG4gIEtFWVdPUkQgPSBwYXR0ZXJuKFwiaWZcIilcbiAgcnVsZShJREVOVCAtIEtFWVdPUkQsIGVtaXQ6IDpJREVOVClcbiAgcnVsZShLRVlXT1JELCBlbWl0OiA6SUYpXG4gIHJ1bGUoLyArLywgc2tpcDogdHJ1ZSlcbmVuZCIsImlucHV0IjoiaWYgaWZmeSIsIm1vZGUiOiJnZW5lcmF0ZWQifQ).
|
|
11
|
+
The same input rule can still recognize longer identifiers such as `iffy`.
|
|
12
|
+
|
|
13
|
+
The absence and subtraction demos require flexr 2.1 or later.
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# regexp_parser scanner experiment
|
|
2
|
+
|
|
3
|
+
The [experimental Flexr scanner](../../benchmark/regexp_parser_scanner.rb)
|
|
4
|
+
uses exclusive states and the state stack for nested character sets, named
|
|
5
|
+
captures to classify POSIX classes and quantifier suffixes, and `upto` to read
|
|
6
|
+
inline group comments. It recognizes absence group prefixes and tracks their
|
|
7
|
+
nesting. The experiment asks how much of the existing scanner contract can be
|
|
8
|
+
expressed with those facilities.
|
|
9
|
+
|
|
10
|
+
The comparator targets `Regexp::Scanner.scan` from the released
|
|
11
|
+
[`regexp_parser` 2.13.4 gem](https://rubygems.org/gems/regexp_parser/versions/2.13.4).
|
|
12
|
+
It compares every token's type, name, text, encoding, and zero-based half-open
|
|
13
|
+
character offsets. The prototype covers literal runs, anchors, groups and
|
|
14
|
+
assertion prefixes, common escapes, quantifiers, nested sets, and global
|
|
15
|
+
extended spacing.
|
|
16
|
+
|
|
17
|
+
## Reproduce
|
|
18
|
+
|
|
19
|
+
Download the [v2.13.4 tag](https://github.com/ammar/regexp_parser/tree/v2.13.4)
|
|
20
|
+
for its specs. The published gem supplies the Ragel-generated reference scanner.
|
|
21
|
+
Run the comparator from a Flexr checkout with the active Ruby's gem environment.
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
curl -fL https://codeload.github.com/ammar/regexp_parser/tar.gz/refs/tags/v2.13.4 -o /tmp/regexp_parser-v2.13.4.tar.gz
|
|
25
|
+
tar -xzf /tmp/regexp_parser-v2.13.4.tar.gz -C /tmp
|
|
26
|
+
gem install 'regexp_parser:2.13.4' 'leto:2.4.0' 'regexp_property_values:1.5.2' 'rspec:3.13.2' 'warning:1.6.0'
|
|
27
|
+
ruby -Ilib benchmark/regexp_parser_parity.rb /tmp/regexp_parser-2.13.4 spec/scanner
|
|
28
|
+
ruby -Ilib benchmark/regexp_parser_parity.rb /tmp/regexp_parser-2.13.4
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
RSpec runs upstream's examples with the released scanner. The observer
|
|
32
|
+
records main-Ractor scanner requests, including those sent by lexer and parser
|
|
33
|
+
examples. After RSpec passes, the comparator replays distinct requests based on
|
|
34
|
+
source bytes, encoding, Regexp flags, and the `options:` argument. The JSON
|
|
35
|
+
report lists counts and a small reproducer for each gap. The replay compares
|
|
36
|
+
returned arrays and tracks upstream errors separately. Callback behavior,
|
|
37
|
+
reentrant calls, other Ractors, and exact error classes and messages would need
|
|
38
|
+
separate prototype coverage.
|
|
39
|
+
|
|
40
|
+
## Results
|
|
41
|
+
|
|
42
|
+
Ruby 4.0.9, `regexp_parser` 2.13.4.
|
|
43
|
+
|
|
44
|
+
| Upstream specs | Passing RSpec examples | Unique requests | Exact tokens | Unsupported | Different tokens | Upstream errors |
|
|
45
|
+
| --- | ---: | ---: | ---: | ---: | ---: | ---: |
|
|
46
|
+
| `spec/scanner` | 821 | 552 | 239 | 221 | 0 | 92 |
|
|
47
|
+
| `spec` | 2,116 | 1,617 | 520 | 1,005 | 0 | 92 |
|
|
48
|
+
|
|
49
|
+
The prototype rejected all 92 requests for which the published scanner raised.
|
|
50
|
+
For requests that returned tokens upstream, the largest gap was Unicode
|
|
51
|
+
property handling. Each row below counts the first unsupported construct per
|
|
52
|
+
request.
|
|
53
|
+
|
|
54
|
+
| First unsupported construct | `spec/scanner` | `spec` | Example |
|
|
55
|
+
| --- | ---: | ---: | --- |
|
|
56
|
+
| Unicode property escapes | 115 | 793 | `\p{Greek}` |
|
|
57
|
+
| Backreferences, subexpression calls, and numeric escape ambiguity | 39 | 85 | `(a)\1` |
|
|
58
|
+
| Inline option switches and scoped options | 24 | 58 | `(?x:a b)` |
|
|
59
|
+
| Conditional groups | 18 | 34 | `(a)(?(1)b)` |
|
|
60
|
+
| Control and meta escapes | 22 | 29 | `\cA` |
|
|
61
|
+
| High-byte hex escapes and encoding | 3 | 6 | `\x94\x95` with `Regexp::NOENCODING` |
|
|
62
|
+
|
|
63
|
+
A replacement would need to preserve property alias validation, group and
|
|
64
|
+
option state, capture-dependent numeric escape classification, and the
|
|
65
|
+
scanner's error and callback contracts. These require work beyond token
|
|
66
|
+
recognition. This experiment measured token parity on its subset and left
|
|
67
|
+
performance unmeasured.
|
data/docs/perf-log.md
CHANGED
|
@@ -1,5 +1,79 @@
|
|
|
1
1
|
# Performance log
|
|
2
2
|
|
|
3
|
+
## DFA stretch experiments (2026-11-22)
|
|
4
|
+
|
|
5
|
+
Run `ruby benchmark/dfa_stretch.rb` to reproduce the results below. The probe
|
|
6
|
+
walks compiled flexr DFA rows and checks them against lexer and recognizer
|
|
7
|
+
results. It uses fixed ASCII fixtures and adds no scanner options or backends.
|
|
8
|
+
Ruby 4.0.9 and 3.4.9 produced the same results on arm64-darwin25.
|
|
9
|
+
|
|
10
|
+
### Shortest match
|
|
11
|
+
|
|
12
|
+
For `aaa!`, the earliest accept is `SINGLE` at byte 1. The latest is `RUN`
|
|
13
|
+
at byte 3, and the lexer emits `[:RUN, "aaa"]`. A separate grammar declaring
|
|
14
|
+
`/a+/` before `"a"` also has an accept at byte 1, but flexr's `firstmatch`
|
|
15
|
+
backend takes Ruby's `"aaa"` match for `/a+/`. OCamllex describes `shortest`
|
|
16
|
+
as choosing the shortest matching prefix.
|
|
17
|
+
[OCaml 5.4 reference manual, section 17.2.3](https://ocaml.org/manual/5.4/ocaml-5.4-refman.pdf)
|
|
18
|
+
|
|
19
|
+
An early exit from the DFA can select the shortest eligible prefix when the
|
|
20
|
+
body length is the ranking length. Trailing context makes that distinction
|
|
21
|
+
observable. On `abcd`, `"a"` followed by `"bcd"` ends its body at byte 1 and its
|
|
22
|
+
context at byte 4. The competing `"ab"` ends at byte 2. The current recognizer
|
|
23
|
+
chooses `"a"` because its full match is longer. A shortest policy must specify
|
|
24
|
+
whether this example compares 1 to 2 or 4 to 2. Before exposing an option,
|
|
25
|
+
agree on scope and on that ordering, then check UTF-8 and anchor eligibility,
|
|
26
|
+
parser expectations, and priority ties across runtime, generated, and
|
|
27
|
+
standalone scanners. Trace and diagnostic witnesses would need the same policy.
|
|
28
|
+
|
|
29
|
+
### REJECT
|
|
30
|
+
|
|
31
|
+
The compiled history for `aaa!`, in retry order, is `RUN@3`, `PAIR@2`,
|
|
32
|
+
`RUN@2`, `SINGLE@1`, `RUN@1`. `PAIR` has higher priority than `RUN` at byte 2.
|
|
33
|
+
This history includes both the lower-ranked rule at the same endpoint and the
|
|
34
|
+
same rule at shorter endpoints. Keeping just the losing rules at the winning
|
|
35
|
+
byte or a single match per rule would discard candidates. Recording every
|
|
36
|
+
acceptance took 2, 10, 66, and 1,026 entries for 1, 8, 64, and 1,024 `a` bytes.
|
|
37
|
+
Flex describes `yyreject()` as continuing directly to the next-ranked choice
|
|
38
|
+
after the effects of the preceding action have occurred.
|
|
39
|
+
[Flex manual, Actions](https://westes.github.io/flex/manual/Actions.html)
|
|
40
|
+
|
|
41
|
+
In the probe, the first action appends `[:run, "aaa"]` to an array and switches
|
|
42
|
+
the scanner to `:inside` at byte 3. Checkpoint restoration returns the scanner
|
|
43
|
+
to `:initial` at byte 0. The parser filter selects `[:PAIR, "aa"]` on the next
|
|
44
|
+
call, and the array still holds the original entry. The filter lets a parser
|
|
45
|
+
choose token kinds before any action runs, so it cannot select a shorter
|
|
46
|
+
`RUN` after `RUN`'s action. A REJECT API would need an explicit action contract
|
|
47
|
+
for emissions, state transitions, input changes, and effects already visible
|
|
48
|
+
to callers. It would also need bounded retry work and streaming retention.
|
|
49
|
+
The probe leaves REJECT at the feasibility stage until that contract exists.
|
|
50
|
+
|
|
51
|
+
### Tagged captures
|
|
52
|
+
|
|
53
|
+
`/(?<head>a|aa)(?<tail>a+)/` and `/(?<head>aa|a)(?<tail>a+)/` compile to identical
|
|
54
|
+
three-state DFAs. The probe checks every state/byte transition and acceptance.
|
|
55
|
+
For `aaa!`, both recognize a three-byte body with the same rule and pattern
|
|
56
|
+
indices. Ruby replay gives `head = "a", tail = "aa"` for the first and
|
|
57
|
+
`head = "aa", tail = "a"` for the second. The respective boundaries are
|
|
58
|
+
`[0, 1), [1, 3)` and `[0, 2), [2, 3)`.
|
|
59
|
+
|
|
60
|
+
The bounded model runs two compiled lanes, `/aa+/` and `/aaa+/`, in one pass.
|
|
61
|
+
It retains the two active states and the split/end offsets of the last accept.
|
|
62
|
+
The alternative order selects the split if both lanes accept. The model agreed
|
|
63
|
+
with recognition and capture replay on 126 matching cases and six cases with
|
|
64
|
+
no match. It handles this fixed-width-head example. A general TDFA needs tag
|
|
65
|
+
updates and a way to distinguish and choose competing subgroup paths. re2c
|
|
66
|
+
provides separate leftmost-greedy and POSIX policies.
|
|
67
|
+
[re2c manual, Submatch extraction](https://re2c.org/manual/manual_c.html#submatch-extraction)
|
|
68
|
+
|
|
69
|
+
Keep lazy replay as described in [ADR 0038](adr/0038-replay-named-regexp-captures.md).
|
|
70
|
+
Before replacing it, compare subgroup values and byte ranges against Ruby for
|
|
71
|
+
ambiguous alternatives, empty and optional groups, repetitions, and Unicode.
|
|
72
|
+
Then check trailing context, streaming, delegation, and generated artifacts.
|
|
73
|
+
Measure state growth, code size, and capture throughput under the supported
|
|
74
|
+
Ruby engines. This small model establishes how to preserve the split in one
|
|
75
|
+
case. Throughput remains an open question for a general TDFA.
|
|
76
|
+
|
|
3
77
|
## State-specific dispatch experiment (2026-11-20)
|
|
4
78
|
|
|
5
79
|
`benchmark/state_dispatch.rb` compares generated table dispatch, the existing
|
|
@@ -29,10 +103,10 @@ justify adding and maintaining another backend. No regression baseline changes
|
|
|
29
103
|
are warranted by this experiment.
|
|
30
104
|
## Lrama standalone recognizer experiment (2026-11-20)
|
|
31
105
|
|
|
32
|
-
`benchmark/lrama_lexer.rb`
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
106
|
+
At flexr revision `80b5d13`, `benchmark/lrama_lexer.rb` built a standalone
|
|
107
|
+
grammar-token recognizer and adapted Lrama's lexer to use it for normal
|
|
108
|
+
grammar tokens. It retained Lrama's trivia, C-code scanning, identifier
|
|
109
|
+
lookahead, token classes, locations, and parser. The adapter deliberately uses private scanner positions; this
|
|
36
110
|
is an experiment, not a supported Lrama integration or a production replacement.
|
|
37
111
|
|
|
38
112
|
```sh
|
|
@@ -48,10 +122,48 @@ On Ruby 4.0.9, arm64-darwin25, three corpus passes took 0.0434 seconds with
|
|
|
48
122
|
Lrama's scanner and 11.9124 seconds with the standalone recognizer adapter.
|
|
49
123
|
These short local timings are illustrative, not a statistical benchmark.
|
|
50
124
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
125
|
+
The standalone experiment stopped at local benchmarking. The embedded
|
|
126
|
+
recognizer experiment below revisits the adapter overhead.
|
|
127
|
+
|
|
128
|
+
## Lrama embedded recognizer experiment (2026-11-22)
|
|
129
|
+
|
|
130
|
+
The benchmark uses Flexr's `Recognizer#match` with the original grammar text
|
|
131
|
+
and the current byte offset. The embedded lexer recognizes symbols, integers,
|
|
132
|
+
and identifiers. The pinned Lrama scanner checks `%{`, `%}`, and `%%` before
|
|
133
|
+
directives. Integers and identifiers start with different bytes from the
|
|
134
|
+
other token kinds. Lrama handles quoted tokens, tags, directives, trivia,
|
|
135
|
+
identifier lookahead, and C declarations. The subclass reads Lrama's
|
|
136
|
+
scanner and advances it through `StringScanner#pos=`.
|
|
137
|
+
|
|
138
|
+
On Lrama revision `22de9d99f6402290a637627f3516a1a2db6c9aab`, the complete
|
|
139
|
+
token classes, kinds, values, source locations, and parser errors matched
|
|
140
|
+
across 78 fixtures. Both lexers successfully parsed 77 fixtures. Flexr
|
|
141
|
+
recognized 2,618 of 3,767 tokens (69.5 percent), including 1,539 symbols,
|
|
142
|
+
1,067 identifiers, and 12 integers.
|
|
143
|
+
|
|
144
|
+
Each measurement uses Ruby 4.0.9 on arm64-darwin25 and the same 78 inputs.
|
|
145
|
+
The harness checks token parity before timing, then runs three warmup and
|
|
146
|
+
30 measured passes per lexer. It uses the same Lrama parser in both modes
|
|
147
|
+
and swaps the lexer class outside the timed passes. The table reports
|
|
148
|
+
medians from three processes per engine, with the ratio calculated per sample.
|
|
149
|
+
|
|
150
|
+
| Engine | Native seconds | Embedded seconds | Embedded / native runtime |
|
|
151
|
+
|---|---:|---:|---:|
|
|
152
|
+
| Interpreter | 0.3712 | 0.4425 | 1.195x |
|
|
153
|
+
| YJIT | 0.2872 | 0.2949 | 1.026x |
|
|
154
|
+
| ZJIT | 0.3750 | 0.3870 | 1.047x |
|
|
155
|
+
|
|
156
|
+
All nine samples met the 2.0x target for complete parser passes.
|
|
157
|
+
Use the same pinned Lrama checkout as in the previous experiment.
|
|
158
|
+
|
|
159
|
+
```sh
|
|
160
|
+
for engine in --disable=yjit,zjit --yjit --zjit; do
|
|
161
|
+
bundle exec ruby "$engine" -Ilib -r ./benchmark/lrama_lexer \
|
|
162
|
+
-e 'Flexr::Benchmarking::LramaLexer.run(ARGV.fetch(0), iterations: 30)' /tmp/lrama
|
|
163
|
+
done
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## Lexer throughput benchmarks
|
|
55
167
|
|
|
56
168
|
The benchmark harness reports bytes/s, tokens/s, and allocations per token.
|
|
57
169
|
Numbers are intentionally recorded only after running the harness on the
|
data/docs/reference/README.md
CHANGED
|
@@ -17,7 +17,7 @@ consuming tokens, or the [CLI](cli.md) when validating and generating files.
|
|
|
17
17
|
- [Regexp compatibility](regexp.md) — supported syntax and alternatives.
|
|
18
18
|
- [CLI](cli.md) — commands, options, output, and exit statuses.
|
|
19
19
|
- [Diagnostics](diagnostics.md) — diagnostic code catalog.
|
|
20
|
-
- [Ruby LSP](ruby-lsp.md) — editor diagnostics and
|
|
20
|
+
- [Ruby LSP](ruby-lsp.md) — editor diagnostics, navigation, and completion.
|
|
21
21
|
- [Generated artifacts](generated-artifacts.md) — static evaluation, output
|
|
22
22
|
headers, dependencies, and standalone files.
|
|
23
23
|
- [Public API](public-api.md) — stable, experimental, and internal boundaries.
|
data/docs/reference/actions.md
CHANGED
|
@@ -18,6 +18,7 @@ instance as `self`.
|
|
|
18
18
|
|---|---|
|
|
19
19
|
| `text` | Matched text, including a `more` prefix when present |
|
|
20
20
|
| `text_bytesize` | Byte size of `text` |
|
|
21
|
+
| `captures` | Frozen hash of named groups from the original rule match |
|
|
21
22
|
| `last_location` | A `Runtime::Location` for the current text span |
|
|
22
23
|
| `lineno` / `line` | One-based current line number |
|
|
23
24
|
| `byte_pos` | Current byte offset |
|
|
@@ -27,6 +28,70 @@ instance as `self`.
|
|
|
27
28
|
|
|
28
29
|
`text` is memoized for the current action. `last_location` uses the same span;
|
|
29
30
|
`more` makes the span include all joined matches.
|
|
31
|
+
See [named captures](regexp.md#captures) for group values and match bounds.
|
|
32
|
+
|
|
33
|
+
`push_input(input, filename: nil)` queues another source for the next token
|
|
34
|
+
boundary. The current action keeps its match text and location. See
|
|
35
|
+
[input stack](runtime.md#input-stack) for EOF and location behavior.
|
|
36
|
+
|
|
37
|
+
## Delegate a capture to another lexer
|
|
38
|
+
|
|
39
|
+
`delegate(ChildLexer, capture: :name)` scans the exact byte range of a named
|
|
40
|
+
capture with a new child lexer. Call it from a default-channel rule action
|
|
41
|
+
that has the named capture. An optional group that did not match contributes
|
|
42
|
+
no tokens. An empty capture is an empty child input, so its EOF action can
|
|
43
|
+
still emit a token.
|
|
44
|
+
|
|
45
|
+
The child starts in its initial state. Its default-channel tokens are
|
|
46
|
+
inserted in order before any ordinary token emitted by the parent action.
|
|
47
|
+
The parent keeps its lexical state and original match. If the action uses
|
|
48
|
+
`less`, the delegated range must fit inside the final match.
|
|
49
|
+
An indentation-aware parent treats the delegating rule as one content match.
|
|
50
|
+
|
|
51
|
+
Both lexers need the same encoding. A parent with `token_kind :struct`
|
|
52
|
+
requires a structured child. Child token locations, attached trivia, and
|
|
53
|
+
lexical errors use the original byte offsets and coordinates in the parent
|
|
54
|
+
file. The child reads only the capture. Its beginning of line and EOF
|
|
55
|
+
conditions are relative to that input. Attempts to switch to another input
|
|
56
|
+
with `push_input` raise `ArgumentError`. Parent array and yield modes return
|
|
57
|
+
their usual token pairs.
|
|
58
|
+
|
|
59
|
+
A child uses the parent's error mode and scan limits. Delegated work counts
|
|
60
|
+
toward the parent's step limit, with a maximum of 32 nested children. Custom
|
|
61
|
+
error handlers stay with the lexer on which they were installed. The action
|
|
62
|
+
receives `nil` from `delegate` after staging the child tokens.
|
|
63
|
+
|
|
64
|
+
For a small HTML lexer, use an absence group to keep the closing tag out of
|
|
65
|
+
the capture. Put each lexer in its own source file for generation.
|
|
66
|
+
|
|
67
|
+
```ruby
|
|
68
|
+
# js.flexr.rb
|
|
69
|
+
require "flexr" unless defined?(Flexr::Lexer)
|
|
70
|
+
|
|
71
|
+
class JsLexer < Flexr::Lexer
|
|
72
|
+
token_kind :struct
|
|
73
|
+
rule(/[a-z_][a-z_0-9]*/, emit: :JS_IDENT)
|
|
74
|
+
rule(/[ \t\r\n]+/, channel: :space, emit: :SPACE)
|
|
75
|
+
end
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
```ruby
|
|
79
|
+
# html.flexr.rb
|
|
80
|
+
require "flexr"
|
|
81
|
+
require_relative "js"
|
|
82
|
+
|
|
83
|
+
class HtmlLexer < Flexr::Lexer
|
|
84
|
+
token_kind :struct
|
|
85
|
+
rule(%r{<script>(?<js>(?~</script>))</script>}) { delegate JsLexer, capture: :js }
|
|
86
|
+
rule(/[^<]+/, emit: :HTML_TEXT)
|
|
87
|
+
end
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Generate `js.rb` as a regular compiled artifact and `html.rb` in either
|
|
91
|
+
generated mode. For standalone HTML, the child shares its embedded runtime.
|
|
92
|
+
The child must use features available in that runtime. Add
|
|
93
|
+
`option :delegation` if the call to `delegate` happens in a helper outside
|
|
94
|
+
the lexer source.
|
|
30
95
|
|
|
31
96
|
## State methods
|
|
32
97
|
|
|
@@ -48,3 +113,44 @@ must also be a codepoint boundary. Returning to the same byte and state is a
|
|
|
48
113
|
non-progress error; state-changing rescan cycles are detected as well. `more`
|
|
49
114
|
is finalized after the action; call `emit` only after the complete text has
|
|
50
115
|
been assembled.
|
|
116
|
+
|
|
117
|
+
## Dynamic delimiters
|
|
118
|
+
|
|
119
|
+
Call `scan_until(delimiter)` or `scan_until_line(delimiter)` from a matched
|
|
120
|
+
rule's action. Both start at the current `byte_pos` and return the bytes before
|
|
121
|
+
the first matching delimiter. A delimiter at the cursor returns an empty
|
|
122
|
+
string. The scan consumes the delimiter and extends the current match, so
|
|
123
|
+
call `emit` afterwards to give the token the full location. `text` includes
|
|
124
|
+
the rule's original match, the body, and the delimiter.
|
|
125
|
+
|
|
126
|
+
`scan_until` matches a literal string at byte boundaries in binary mode and
|
|
127
|
+
at UTF-8 scalar boundaries in UTF-8 mode. `scan_until_line` requires the
|
|
128
|
+
delimiter to occupy a whole line starting at the beginning of that line. It
|
|
129
|
+
consumes the following LF or CRLF when present and accepts a delimiter on the
|
|
130
|
+
last line at EOF. Line endings in the returned body are preserved.
|
|
131
|
+
|
|
132
|
+
Pass a nonempty string. UTF-8 lexers require a valid UTF-8 delimiter, and a
|
|
133
|
+
line delimiter may contain neither LF nor CR. A missing delimiter returns
|
|
134
|
+
`nil` and leaves the current match and position unchanged. A streaming buffer
|
|
135
|
+
can load bytes while looking for it. Token, buffer, and work limits still
|
|
136
|
+
apply. Call `error!` to report a missing required delimiter through the
|
|
137
|
+
configured error policy.
|
|
138
|
+
|
|
139
|
+
For a tagged heredoc, use a named capture from the opener to select the
|
|
140
|
+
closing line at run time.
|
|
141
|
+
|
|
142
|
+
```ruby
|
|
143
|
+
rule(/<<(?<tag>[A-Z_]+)\r?\n/) do
|
|
144
|
+
body = scan_until_line(captures[:tag])
|
|
145
|
+
if body
|
|
146
|
+
emit :HEREDOC, body
|
|
147
|
+
else
|
|
148
|
+
error!("missing heredoc terminator")
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
`less` can adjust where the scan starts or shorten its result. `more` can
|
|
154
|
+
carry the extended match into the next action. Standalone generation embeds
|
|
155
|
+
these helpers when the lexer source calls them. Add `option :dynamic_delimiters`
|
|
156
|
+
for indirect calls that generation cannot see.
|
data/docs/reference/cli.md
CHANGED
|
@@ -4,24 +4,28 @@ The executable is `flexr`, with usage:
|
|
|
4
4
|
|
|
5
5
|
```text
|
|
6
6
|
flexr [COMMAND] SPEC.rb [options]
|
|
7
|
+
flexr new NAME --parser lrama|racc
|
|
7
8
|
```
|
|
8
9
|
|
|
9
10
|
Without a command, `flexr SPEC.rb` generates Ruby. `SPEC.rb` is normally a
|
|
10
11
|
`.flexr.rb` file, except for `import`, which accepts flex `.l` and Rexical
|
|
11
|
-
`.rex` input.
|
|
12
|
+
`.rex` input. Use `--from oedipus_lex` for the other `.rex` dialect.
|
|
12
13
|
|
|
13
14
|
## Commands
|
|
14
15
|
|
|
15
16
|
| Command | Purpose | Main output |
|
|
16
17
|
|---|---|---|
|
|
17
18
|
| `check SPEC` | Compile and render diagnostics | Human or JSON diagnostics |
|
|
19
|
+
| `fix SPEC` | Apply supported diagnostic fixes | Updated spec, or full source with `--dry-run` |
|
|
18
20
|
| `stats SPEC` | Report DFA states, byte classes, and table sizes | JSON |
|
|
19
21
|
| `tokens SPEC` | Print names declared by `emits` | Space-separated names |
|
|
20
22
|
| `dot SPEC` | Render the DFA as Graphviz DOT | DOT on stdout |
|
|
21
23
|
| `trace SPEC` | Print DFA states, or trace input when supplied | Human or JSON input trace |
|
|
24
|
+
| `skeleton SPEC` | Build shortest inputs that cover DFA transitions | Human or JSON coverage report |
|
|
22
25
|
| `explain SPEC` | List parsed rules and patterns | Text on stdout |
|
|
23
26
|
| `bench SPEC` | Run the benchmark harness | Benchmark output |
|
|
24
|
-
| `import INPUT` | Translate flex
|
|
27
|
+
| `import INPUT` | Translate flex, Rexical, or oedipus_lex input | Ruby on stdout or `--output` |
|
|
28
|
+
| `new NAME --parser PARSER` | Create a Racc or Lrama calculator project | New project directory |
|
|
25
29
|
|
|
26
30
|
`trace` without input describes the automaton. To execute a specification and
|
|
27
31
|
inspect rule selection, pass `--input STRING` or `--input-file PATH`.
|
|
@@ -46,17 +50,22 @@ inspect rule selection, pass `--input STRING` or `--input-file PATH`.
|
|
|
46
50
|
| `--format FMT` | `human`, `json`, `lrama` | `lrama` is for tokens only |
|
|
47
51
|
| `--grammar PATH` | Grammar path | Compare `%token` names with `emits` during check |
|
|
48
52
|
| `--target 2.0` | none | Static check migration requirements and structured-token declarations |
|
|
53
|
+
| `--dry-run` | Flag | Preview fixes on stdout |
|
|
49
54
|
| `--rule N` | Non-negative integer | Explain |
|
|
50
55
|
| `--input STRING` | Literal input, including an empty string | Trace |
|
|
51
56
|
| `--input-file PATH` | Input file | Trace and bench |
|
|
52
57
|
| `--generated` | Run the generated scanner | Input trace |
|
|
58
|
+
| `--verify` | Execute the generated cases in all three modes | Skeleton |
|
|
59
|
+
| `--from oedipus_lex` | Select the oedipus_lex `.rex` dialect | Import |
|
|
60
|
+
| `--parser PARSER` | `lrama` or `racc` | New project |
|
|
53
61
|
|
|
54
62
|
The complete help output is kept as a checked snapshot:
|
|
55
63
|
|
|
56
64
|
<!-- flexr-help:start -->
|
|
57
65
|
Usage: flexr [COMMAND] SPEC.rb [options]
|
|
66
|
+
flexr new NAME --parser lrama|racc
|
|
58
67
|
|
|
59
|
-
Commands: check, stats, tokens, dot, explain, trace, bench, import
|
|
68
|
+
Commands: check, fix, stats, tokens, dot, explain, trace, skeleton, bench, import, new
|
|
60
69
|
Options:
|
|
61
70
|
-o, --output PATH generated output path
|
|
62
71
|
-b, --backend NAME table | direct | firstmatch | auto
|
|
@@ -75,9 +84,39 @@ Options:
|
|
|
75
84
|
--input STRING trace input (executes SPEC actions)
|
|
76
85
|
--input-file PATH trace or benchmark input
|
|
77
86
|
--generated trace generated scanner
|
|
87
|
+
--verify verify skeleton across all modes (executes SPEC actions)
|
|
78
88
|
--target 2.0 report migration requirements (static check only)
|
|
89
|
+
--dry-run print the fixed spec without writing (fix only)
|
|
90
|
+
--from oedipus_lex read an oedipus_lex .rex file (import only)
|
|
79
91
|
<!-- flexr-help:end -->
|
|
80
92
|
|
|
93
|
+
## Automatic fixes
|
|
94
|
+
|
|
95
|
+
`flexr fix lexer.flexr.rb --dry-run` prints the proposed Ruby source.
|
|
96
|
+
Run `flexr fix lexer.flexr.rb` to apply it. The command validates the resulting
|
|
97
|
+
specification before saving and preserves its line endings.
|
|
98
|
+
|
|
99
|
+
The command reads the specification statically. Ruby LSP offers the same edits
|
|
100
|
+
as quick fixes for DFA backends.
|
|
101
|
+
|
|
102
|
+
- `FLEXR-W013` makes ordinary groups in direct regexp rule arguments non-capturing.
|
|
103
|
+
Rules with named captures keep their original groups.
|
|
104
|
+
- `FLEXR-W001` raises the priority of a shadowed literal with `emit:` when the
|
|
105
|
+
DFA shows that every previous winner can still win elsewhere. After the
|
|
106
|
+
change, the literal emits its own token type on equal-length matches.
|
|
107
|
+
- `FLEXR-E014` rewrites a quoted lazy dot-star to an absence group that stops
|
|
108
|
+
at the first closing quote. It preserves the regexp's newline handling.
|
|
109
|
+
- `FLEXR-I003` groups adjacent literal rules into a keyword table when the
|
|
110
|
+
compiler verifies the change and there are no intervening comments.
|
|
111
|
+
|
|
112
|
+
## New parser project
|
|
113
|
+
|
|
114
|
+
`flexr new NAME --parser PARSER` creates a directory in the current directory.
|
|
115
|
+
The name must start with a letter or digit and may also contain hyphens and
|
|
116
|
+
underscores. Choose a name that is available. Follow the generated README or
|
|
117
|
+
the [calculator parser tutorial](../tutorial/build-a-calculator-parser.md)
|
|
118
|
+
to install dependencies, build the parser, and run the tests.
|
|
119
|
+
|
|
81
120
|
## Input trace
|
|
82
121
|
|
|
83
122
|
```sh
|
|
@@ -114,6 +153,38 @@ tool, not a benchmark. Firstmatch candidate analysis reads the remaining
|
|
|
114
153
|
buffered input to inspect rules beyond the selected first match. It obeys the
|
|
115
154
|
lexer's buffer limit. Normal lexers do not load or run tracing code.
|
|
116
155
|
|
|
156
|
+
## DFA coverage with skeleton
|
|
157
|
+
|
|
158
|
+
```sh
|
|
159
|
+
flexr skeleton examples/calculator/lexer.flexr.rb
|
|
160
|
+
flexr skeleton examples/calculator/lexer.flexr.rb --format json --verify
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
The command reads the specification statically. For each lexical state,
|
|
164
|
+
it finds a shortest input that reaches each DFA transition. A transition is
|
|
165
|
+
identified by its source state, byte class, and destination. Several
|
|
166
|
+
transitions can share one input. Each case starts at byte zero in the named
|
|
167
|
+
lexical state, at the beginning of a line. The empty input exercises EOF.
|
|
168
|
+
UTF-8 witnesses finish their last character, and transitions with no valid
|
|
169
|
+
witness are listed as uncovered. Use `--format json` to preserve binary inputs
|
|
170
|
+
as byte arrays when they cannot be represented as UTF-8 text.
|
|
171
|
+
|
|
172
|
+
The JSON report contains `coverage` totals and a `cases` array. Each case
|
|
173
|
+
lists its lexical `state`, `input`, ordered `transitions`, and
|
|
174
|
+
`rule_bodies`. A rule body records its rule index and byte length from the
|
|
175
|
+
beginning of the input, including bodies accepted before the final transition.
|
|
176
|
+
The beginning- and end-of-line conditions are checked at those positions.
|
|
177
|
+
Rule selection, trailing context, and actions can change what a lexer emits.
|
|
178
|
+
|
|
179
|
+
`--verify` runs the specification and its actions in separate Ruby processes
|
|
180
|
+
for runtime, generated, and standalone modes. Use this option on trusted
|
|
181
|
+
specifications. The comparison includes tokens emitted before any error,
|
|
182
|
+
the last byte position and lexical state, and any reported errors. Token
|
|
183
|
+
values use the [snapshot representation](testing.md). Differing outcomes
|
|
184
|
+
are listed under `verification.mismatches`, and the command exits with
|
|
185
|
+
status 1. Matching errors are a valid parity result. Uncovered edges appear
|
|
186
|
+
in the report and leave the exit status unchanged.
|
|
187
|
+
|
|
117
188
|
## Exit statuses
|
|
118
189
|
|
|
119
190
|
Compare a lexer with Lrama/Bison-style declarations:
|
|
@@ -133,7 +204,7 @@ grammar author's responsibility.
|
|
|
133
204
|
| Status | Meaning |
|
|
134
205
|
|---:|---|
|
|
135
206
|
| `0` | Command completed; warnings do not fail by default |
|
|
136
|
-
| `1` | Specification, input, import, or
|
|
207
|
+
| `1` | Specification, input, import, generation, or project creation failure |
|
|
137
208
|
| `2` | Invalid command-line usage |
|
|
138
209
|
|
|
139
210
|
`check --format json` writes one JSON array to stdout. Human diagnostics go to
|
|
@@ -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` |
|
|
146
|
+
| `FLEXR-E018` | A rule pattern, trailing context, channel, priority, or `emits:` annotation is invalid | Check the accepted values for the option named in the diagnostic |
|
|
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 |
|
|
@@ -212,9 +212,8 @@ escaped quotes and line feeds.
|
|
|
212
212
|
For numbered backreferences, the help uses a capture with only two possible
|
|
213
213
|
values. Rewrite `rule(/(a|b)\1/)` or `rule(/(a|b)\k<1>/)` as
|
|
214
214
|
`rule(/(?:aa|bb)/)` and keep the action. This accepts `aa` and `bb`, while
|
|
215
|
-
rejecting `ab` and `ba`.
|
|
216
|
-
|
|
217
|
-
the rule into states and compare the matched text in actions instead.
|
|
215
|
+
rejecting `ab` and `ba`. If the repeated text cannot be enumerated, split the
|
|
216
|
+
rule into states and compare the matched text in actions.
|
|
218
217
|
|
|
219
218
|
For a non-recursive subexpression call, the help shows
|
|
220
219
|
`rule(/(a|b)\g<1>/)` becoming `rule(/[ab]{2}/)` with the same action.
|
|
@@ -222,11 +221,6 @@ A call reuses the expression, not the previously captured text: this example
|
|
|
222
221
|
accepts `aa`, `ab`, `ba`, and `bb`. Recursive calls need a parser or a state
|
|
223
222
|
stack; they cannot be expanded by this example.
|
|
224
223
|
|
|
225
|
-
For named captures used only for grouping, rewrite `rule(/(?<word>ab)/)` or
|
|
226
|
-
`rule(/(?'word'ab)/)` as `rule(/(?:ab)/)` and keep the action. Backreferences
|
|
227
|
-
and conditions that depend on the capture need a separate rewrite; removing
|
|
228
|
-
the name alone does not preserve them.
|
|
229
|
-
|
|
230
224
|
For the conditional example `rule(/(a)?(?(1)b|c)/)`, use `rule(/(?:ab|c)/)`
|
|
231
225
|
with the same action. The original matches `ab` when the optional `a` was
|
|
232
226
|
captured, or `c` otherwise. Other conditions need their own alternatives,
|