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
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "flexr"
|
|
4
|
+
|
|
5
|
+
module RegexpParserParity
|
|
6
|
+
class Unsupported < StandardError; end
|
|
7
|
+
|
|
8
|
+
class Scanner < Flexr::Lexer
|
|
9
|
+
OPENERS = {
|
|
10
|
+
"(" => %i[group capture], "(?:" => %i[group passive], "(?>" => %i[group atomic],
|
|
11
|
+
"(?~" => %i[group absence], "(?=" => %i[assertion lookahead], "(?!" => %i[assertion nlookahead],
|
|
12
|
+
"(?<=" => %i[assertion lookbehind], "(?<!" => %i[assertion nlookbehind]
|
|
13
|
+
}.freeze
|
|
14
|
+
METACHARS = { "." => %i[meta dot], "|" => %i[meta alternation], "^" => %i[anchor bol], "$" => %i[anchor eol] }.freeze
|
|
15
|
+
QUANTIFIERS = { "?" => "zero_or_one", "*" => "zero_or_more", "+" => "one_or_more" }.freeze
|
|
16
|
+
ASCII_ESCAPES = %i[bell backspace escape form_feed newline carriage tab vertical_tab].zip(%w[a b e f n r t v]).to_h.invert.freeze
|
|
17
|
+
SYMBOL_ESCAPES = %i[dot alternation bol eol zero_or_one zero_or_more one_or_more group_open group_close
|
|
18
|
+
interval_open interval_close set_open set_close backslash].zip('.|^$?*+(){}[]\\'.chars).to_h.invert.freeze
|
|
19
|
+
ESCAPED_ANCHORS = %i[bos eos eos_ob_eol word_boundary nonword_boundary match_start].zip(%w[A z Z b B G]).to_h.invert.freeze
|
|
20
|
+
TYPES = %i[digit nondigit hex nonhex space nonspace word nonword linebreak xgrapheme].zip(%w[d D h H s S w W R X]).to_h.invert.freeze
|
|
21
|
+
SET_SYMBOL_ESCAPES = SYMBOL_ESCAPES.slice("[", "]", "\\").freeze
|
|
22
|
+
SET_TYPES = TYPES.except("R", "X").freeze
|
|
23
|
+
POSIX_CLASSES = %w[alnum alpha ascii blank cntrl digit graph lower print punct space upper word xdigit].freeze
|
|
24
|
+
|
|
25
|
+
state :extended, inclusive: true do
|
|
26
|
+
rule(/#[^\n]*\n?/) { piece(:free_space, :comment) }
|
|
27
|
+
rule(/[ \t\r\n\f\v]+/) { piece(:free_space, :whitespace) }
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
OPENERS.each { |spelling, (type, name)| rule(spelling) { open_group(type, name) } }
|
|
31
|
+
METACHARS.each { |spelling, (type, name)| rule(spelling) { piece(type, name) } }
|
|
32
|
+
rule(/\(\?<[^=0-9!>-][^>]*>/) { open_group(:group, :named_ab) }
|
|
33
|
+
rule(/\(\?'[^0-9'-][^']*'/) { open_group(:group, :named_sq) }
|
|
34
|
+
rule(pattern("(?#") + upto(")")) { piece(:group, :comment) }
|
|
35
|
+
rule("(?(") { unsupported!("conditionals") }
|
|
36
|
+
rule("(?") { unsupported!("option groups or invalid group prefixes") }
|
|
37
|
+
rule(")") do
|
|
38
|
+
unsupported!("unbalanced groups") if @group_depth.zero?
|
|
39
|
+
@group_depth -= 1
|
|
40
|
+
piece(:group, :close)
|
|
41
|
+
end
|
|
42
|
+
rule("[") { open_set }
|
|
43
|
+
rule(/[?*+](?<modifier>[?+])?/) do
|
|
44
|
+
base = QUANTIFIERS.fetch(text[0])
|
|
45
|
+
suffix = { "?" => "_reluctant", "+" => "_possessive" }[captures[:modifier]]
|
|
46
|
+
piece(:quantifier, :"#{base}#{suffix}")
|
|
47
|
+
end
|
|
48
|
+
rule(/\{[0-9]+\}|\{(?:[0-9]+,[0-9]*|,[0-9]+)\}\??/) do
|
|
49
|
+
piece(:quantifier, text.end_with?("?") ? :interval_reluctant : :interval)
|
|
50
|
+
end
|
|
51
|
+
rule(/\\[1-9]|\\[kg][<']/) { unsupported!("backreferences, calls or ambiguous numeric escapes") }
|
|
52
|
+
rule(/\\0[0-7]{0,2}/) { piece(:escape, :octal) }
|
|
53
|
+
rule(/\\./m) { escape(set: false) }
|
|
54
|
+
rule(/[^\\\[().|?*+{^$# \t\r\n\f\v]+/) { piece(:literal_run, :literal) }
|
|
55
|
+
rule(/[ \t\r\n\f\v]+|[{#]/) { piece(:literal_run, :literal) }
|
|
56
|
+
|
|
57
|
+
state :set do
|
|
58
|
+
rule("[") { open_set }
|
|
59
|
+
rule("]") do
|
|
60
|
+
pop
|
|
61
|
+
piece(:set, :close)
|
|
62
|
+
end
|
|
63
|
+
rule("&&") { piece(:set, :intersection) }
|
|
64
|
+
rule("^") { @previous == %i[set open] ? piece(:set, :negate) : piece(:literal, :literal) }
|
|
65
|
+
rule("-", followed_by: /\]|&&/) { piece(:literal, :literal) }
|
|
66
|
+
rule("-") { @previous.first == :set ? piece(:literal, :literal) : piece(:set, :range) }
|
|
67
|
+
rule(/\[:(?<negated>\^)?(?<name>[^\[\]]*):\]/) do
|
|
68
|
+
name = captures.fetch(:name)
|
|
69
|
+
unsupported!("unknown POSIX classes") unless POSIX_CLASSES.include?(name)
|
|
70
|
+
piece(captures[:negated] ? :nonposixclass : :posixclass, name.to_sym)
|
|
71
|
+
end
|
|
72
|
+
rule(/\\[0-7]{1,3}/) { piece(:escape, :octal) }
|
|
73
|
+
rule(/\\./m) { escape(set: true) }
|
|
74
|
+
rule(/[^\\]/) { piece(:literal, :literal) }
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
all_states do
|
|
78
|
+
rule(/\\u\{[0-9A-Fa-f]{1,6}(?:[ \t\r\n\f\v][0-9A-Fa-f]{1,6})*\}/) { piece(:escape, :codepoint_list) }
|
|
79
|
+
rule(/\\u[0-9A-Fa-f]{4}/) { piece(:escape, :codepoint) }
|
|
80
|
+
rule(/(?:\\x[89A-Fa-f][0-9A-Fa-f])+/) { unsupported!("high hexadecimal escapes and regexp encoding") }
|
|
81
|
+
rule(/\\x[0-9A-Fa-f]{1,2}/) { piece(:escape, :hex) }
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def self.scan(input, options)
|
|
85
|
+
if input.is_a?(Regexp)
|
|
86
|
+
raise Unsupported, "options with Regexp inputs" unless options.nil?
|
|
87
|
+
|
|
88
|
+
options = input.options
|
|
89
|
+
input = input.source
|
|
90
|
+
end
|
|
91
|
+
source = input.dup.force_encoding(Encoding::UTF_8)
|
|
92
|
+
raise Unsupported, "invalid UTF-8 bytes" unless source.valid_encoding?
|
|
93
|
+
|
|
94
|
+
new(source).scan(extended: (options || 0).anybits?(Regexp::EXTENDED))
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def scan(extended:)
|
|
98
|
+
@group_depth = 0
|
|
99
|
+
begin_state(:extended) if extended
|
|
100
|
+
pieces = tokens
|
|
101
|
+
unsupported!("unbalanced groups") unless @group_depth.zero?
|
|
102
|
+
unsupported!("unbalanced sets") if state == :set
|
|
103
|
+
|
|
104
|
+
position = 0
|
|
105
|
+
output = []
|
|
106
|
+
previous_run = false
|
|
107
|
+
pieces.each do |(type, name), lexeme|
|
|
108
|
+
finish = position + lexeme.length
|
|
109
|
+
run = type == :literal_run
|
|
110
|
+
if run && previous_run
|
|
111
|
+
output.last[2] << lexeme
|
|
112
|
+
output.last[4] = finish
|
|
113
|
+
else
|
|
114
|
+
output << [run ? :literal : type, name, lexeme.dup, position, finish]
|
|
115
|
+
end
|
|
116
|
+
previous_run = run
|
|
117
|
+
position = finish
|
|
118
|
+
end
|
|
119
|
+
output
|
|
120
|
+
rescue Flexr::LexError
|
|
121
|
+
unsupported!("unrecognized input")
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
private
|
|
125
|
+
|
|
126
|
+
def open_group(type, name)
|
|
127
|
+
@group_depth += 1
|
|
128
|
+
piece(type, name)
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def open_set
|
|
132
|
+
push(:set)
|
|
133
|
+
piece(:set, :open)
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def escape(set:)
|
|
137
|
+
letter = text[1]
|
|
138
|
+
unsupported!("Unicode properties") if %w[p P].include?(letter)
|
|
139
|
+
unsupported!("control, meta or incomplete escapes") if %w[C M c u x].include?(letter)
|
|
140
|
+
return piece(:keep, :mark) if letter == "K" && !set
|
|
141
|
+
return piece(:anchor, ESCAPED_ANCHORS[letter]) if !set && ESCAPED_ANCHORS.key?(letter)
|
|
142
|
+
|
|
143
|
+
type = (set ? SET_TYPES : TYPES)[letter]
|
|
144
|
+
return piece(:type, type) if type
|
|
145
|
+
|
|
146
|
+
symbol = (set ? SET_SYMBOL_ESCAPES : SYMBOL_ESCAPES)[letter]
|
|
147
|
+
piece(:escape, ASCII_ESCAPES[letter] || symbol || :literal)
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def piece(type, name)
|
|
151
|
+
@previous = [type, name]
|
|
152
|
+
emit(@previous, text)
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def unsupported!(reason)
|
|
156
|
+
raise Unsupported, reason
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|
data/docs/README.md
CHANGED
|
@@ -9,18 +9,20 @@ pages explain how to use those specifications.
|
|
|
9
9
|
| You want to... | Read |
|
|
10
10
|
|---|---|
|
|
11
11
|
| Build your first lexer | [Build a calculator lexer](tutorial/build-a-calculator-lexer.md) |
|
|
12
|
+
| Generate a working parser project | [Build a calculator parser](tutorial/build-a-calculator-parser.md) |
|
|
12
13
|
| Run a specification | [Run at runtime](how-to/run-a-lexer-at-runtime.md) |
|
|
13
14
|
| Generate Ruby | [Generate a lexer](how-to/generate-a-lexer.md) |
|
|
14
15
|
| Ship one file | [Deploy a standalone lexer](how-to/deploy-a-standalone-lexer.md) |
|
|
15
16
|
| Add states or trailing context | [States](how-to/use-states.md), [trailing context](how-to/use-trailing-context.md) |
|
|
16
17
|
| Integrate a parser | [Racc](how-to/integrate-with-racc.md), [Lrama](how-to/integrate-with-lrama.md) |
|
|
17
|
-
| Migrate an existing lexer | [flex](how-to/migrate-from-flex.md), [Rexical](how-to/migrate-from-rexical.md) |
|
|
18
|
+
| Migrate an existing lexer | [flex](how-to/migrate-from-flex.md), [Rexical](how-to/migrate-from-rexical.md), [oedipus_lex](how-to/migrate-from-oedipus-lex.md) |
|
|
18
19
|
| Look up exact behavior | [Reference](reference/README.md) |
|
|
19
20
|
| Understand design trade-offs | [Explanation](explanation/) |
|
|
20
21
|
|
|
21
22
|
## Tutorial
|
|
22
23
|
|
|
23
24
|
- [Build a calculator lexer](tutorial/build-a-calculator-lexer.md)
|
|
25
|
+
- [Build a calculator parser](tutorial/build-a-calculator-parser.md)
|
|
24
26
|
|
|
25
27
|
## How-to
|
|
26
28
|
|
|
@@ -35,6 +37,7 @@ pages explain how to use those specifications.
|
|
|
35
37
|
- [Integrate with Lrama](how-to/integrate-with-lrama.md)
|
|
36
38
|
- [Migrate from flex](how-to/migrate-from-flex.md)
|
|
37
39
|
- [Migrate from Rexical](how-to/migrate-from-rexical.md)
|
|
40
|
+
- [Migrate from oedipus_lex](how-to/migrate-from-oedipus-lex.md)
|
|
38
41
|
- [Tune performance](how-to/tune-performance.md)
|
|
39
42
|
|
|
40
43
|
## Reference
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# ADR 0021: Require Ruby 3.4 for v2
|
|
2
|
+
|
|
3
|
+
## Context
|
|
4
|
+
|
|
5
|
+
The v1 runtime supported Ruby 3.1 and the generator had a separate Ruby 3.3
|
|
6
|
+
minimum. Version 2 changes the runtime contracts for structured tokens and
|
|
7
|
+
generated artifacts. That major release gives us one upgrade point for Ruby
|
|
8
|
+
and artifact compatibility.
|
|
9
|
+
|
|
10
|
+
## Decision
|
|
11
|
+
|
|
12
|
+
Require Ruby 3.4 or newer for the runtime, generator, and standalone output.
|
|
13
|
+
Set the gem's Ruby requirement and the CI minimum to the same version. Run a
|
|
14
|
+
generated-only check at that minimum so the deployment path is tested
|
|
15
|
+
independently of the generator.
|
|
16
|
+
|
|
17
|
+
Prism remains a build-time dependency for the generator, as described in
|
|
18
|
+
[ADR 0018](0018-prism-for-generator-only.md). Alternate Ruby implementations
|
|
19
|
+
must meet the same language requirement. Support is tracked in the
|
|
20
|
+
[compatibility reference](../reference/compatibility.md).
|
|
21
|
+
|
|
22
|
+
Upgrade Ruby before installing flexr 2 and regenerate existing lexers as
|
|
23
|
+
described in the [migration guide](../migration-v2.md).
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# ADR 0022: Make structured tokens and locations immutable
|
|
2
|
+
|
|
3
|
+
## Context
|
|
4
|
+
|
|
5
|
+
Tokens describe what the lexer emitted and locations describe the span it
|
|
6
|
+
consumed. Mutable `Struct` fields let consumers rewrite those records after
|
|
7
|
+
emission. Locations also need to support lazy column readers without retaining
|
|
8
|
+
the lexer.
|
|
9
|
+
|
|
10
|
+
## Decision
|
|
11
|
+
|
|
12
|
+
Use Ruby `Data` for `Token` and `Location`. Keep `token_kind :struct` as the
|
|
13
|
+
option that selects these records and preserve attribute readers, keyword
|
|
14
|
+
construction, and read-only indexing. Consumers use `with` to create a record
|
|
15
|
+
with changed fields. The default `:array` token format remains available.
|
|
16
|
+
|
|
17
|
+
Store lazy columns in a frozen pair so reading them needs neither the lexer
|
|
18
|
+
nor record mutation. Record immutability is shallow. An action that emits a
|
|
19
|
+
mutable value leaves ownership of that value with the application.
|
|
20
|
+
|
|
21
|
+
See the [token and location reference](../reference/tokens-and-locations.md)
|
|
22
|
+
and the [migration guide](../migration-v2.md#immutable-tokens-and-locations).
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# ADR 0023: Stabilize explicit firstmatch selection
|
|
2
|
+
|
|
3
|
+
## Context
|
|
4
|
+
|
|
5
|
+
First-match selection helps port rule sets that depend on source order.
|
|
6
|
+
Runtime and generated lexers already supported this behavior behind an
|
|
7
|
+
experimental flag. For example, when `/a/` precedes `/ab/`, a first-match
|
|
8
|
+
lexer can consume just `a` from `ab`. A longest-match lexer consumes `ab`.
|
|
9
|
+
|
|
10
|
+
## Decision
|
|
11
|
+
|
|
12
|
+
Selecting `backend :firstmatch` is sufficient to opt in to stable first-match
|
|
13
|
+
behavior. Runtime and generated lexers select the first matching rule in
|
|
14
|
+
source order and honor Ruby regexp alternative order within a rule. Keep
|
|
15
|
+
accepting `option :experimental` for older specifications. Report potential
|
|
16
|
+
differences from longest match through `FLEXR-W010`.
|
|
17
|
+
|
|
18
|
+
`table` remains the default. It, `direct`, and `auto` follow the
|
|
19
|
+
leftmost-longest contract described in [ADR 0003](0003-leftmost-longest.md).
|
|
20
|
+
Choose `firstmatch` when the source rule ordering is part of the lexer
|
|
21
|
+
contract.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# ADR 0024: Prepare generated lexers for Ractor sharing explicitly
|
|
2
|
+
|
|
3
|
+
## Context
|
|
4
|
+
|
|
5
|
+
Generated lexers can share compiled transition tables between Ractors while
|
|
6
|
+
each Ractor owns its lexer and input. Packed tables materialize rows lazily,
|
|
7
|
+
and some actions or matcher caches carry state that cannot be shared.
|
|
8
|
+
|
|
9
|
+
## Decision
|
|
10
|
+
|
|
11
|
+
Call `Flexr::Generated.shareable!(Lexer)` in the main Ractor before creating
|
|
12
|
+
workers. Preparation materializes dense transition rows and makes the
|
|
13
|
+
compiled data and configuration shareable. Each worker then constructs its
|
|
14
|
+
own lexer. The explicit call puts the memory cost and preparation timing
|
|
15
|
+
under the application's control.
|
|
16
|
+
|
|
17
|
+
Preparation raises `ArgumentError` for runtime specifications, captured
|
|
18
|
+
action closures, EOF blocks, and firstmatch patterns that need the Unicode
|
|
19
|
+
reference matcher. Its shared caches are unsafe to use across Ractors. Use
|
|
20
|
+
`table`, `direct`, or `auto` for those patterns. User-defined methods still
|
|
21
|
+
need to follow Ruby's Ractor rules.
|
|
22
|
+
|
|
23
|
+
See the [generated artifacts reference](../reference/generated-artifacts.md#ractor-sharing)
|
|
24
|
+
for a working example.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# ADR 0025: Keep whole-token acceleration opt-in
|
|
2
|
+
|
|
3
|
+
## Context
|
|
4
|
+
|
|
5
|
+
The compiler can derive a complete-token expression from a proven ASCII
|
|
6
|
+
DFA branch. Calling `StringScanner#skip` on that expression advances the
|
|
7
|
+
scanner by the same longest accepted prefix as DFA traversal.
|
|
8
|
+
|
|
9
|
+
The [2026-11-20 JSON benchmark](../perf-log.md#2026-11-20-whole-token-acceleration)
|
|
10
|
+
measured gains for explicit StringScanner mode on the interpreter and YJIT
|
|
11
|
+
after adding this optimization. On ZJIT the same comparison fell from 2.029
|
|
12
|
+
to 1.838 MB/s in runtime mode and from 0.875 to 0.791 MB/s in generated mode.
|
|
13
|
+
Those measurements describe the tested corpus and Ruby build.
|
|
14
|
+
|
|
15
|
+
## Decision
|
|
16
|
+
|
|
17
|
+
Build whole-token plans only for an explicit `accel :strscan` request. Use
|
|
18
|
+
the plans for proven branches on eligible frozen input and fall back to DFA
|
|
19
|
+
traversal otherwise. Preserve checks for streaming, resource limits, and
|
|
20
|
+
matching semantics. See the [backend explanation](../explanation/backends.md#acceleration-and-packing)
|
|
21
|
+
for the eligibility rules.
|
|
22
|
+
|
|
23
|
+
The default `accel :auto` continues to select region acceleration. Revisit
|
|
24
|
+
whole-token defaults when separate interpreter and JIT measurements justify
|
|
25
|
+
automatic selection. The DFA remains the matching contract described in
|
|
26
|
+
[ADR 0006](0006-accel-not-scanner.md).
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# ADR 0026: Retain direct dispatch after the state cases experiment
|
|
2
|
+
|
|
3
|
+
## Context
|
|
4
|
+
|
|
5
|
+
The [state dispatch experiment](../perf-log.md#state-specific-dispatch-experiment-2026-11-20)
|
|
6
|
+
compared table dispatch, flattened direct dispatch, and per-state Ruby
|
|
7
|
+
`case` branches. It checked token equality before timing on a 140,000-byte
|
|
8
|
+
JSON input using Ruby 4.0.9 on arm64-darwin.
|
|
9
|
+
|
|
10
|
+
| Engine | Direct MB/s | State cases MB/s |
|
|
11
|
+
|---|---:|---:|
|
|
12
|
+
| Interpreter | 1.729 | 1.709 |
|
|
13
|
+
| YJIT | 2.796 | 2.768 |
|
|
14
|
+
| ZJIT | 0.425 | 0.483 |
|
|
15
|
+
|
|
16
|
+
## Decision
|
|
17
|
+
|
|
18
|
+
Keep the existing direct backend and `auto` policy. The interpreter and YJIT
|
|
19
|
+
samples favored direct dispatch. The ZJIT sample favored state cases, but
|
|
20
|
+
needs longer warm-up measurements before it can justify maintaining another
|
|
21
|
+
backend. Keep the experiment and its equivalence checks as a benchmark for
|
|
22
|
+
future work.
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# ADR 0033: Match one rule at an absolute byte offset
|
|
2
|
+
|
|
3
|
+
## Context
|
|
4
|
+
|
|
5
|
+
Parser integrations need to recognize a rule in an existing string without
|
|
6
|
+
managing a second input cursor or executing lexer actions. Sharing the
|
|
7
|
+
compiled automaton keeps rule selection consistent across runtime and
|
|
8
|
+
generated lexers.
|
|
9
|
+
|
|
10
|
+
## Decision
|
|
11
|
+
|
|
12
|
+
Expose `Lexer.recognizer` as a shareable snapshot of one compiled lexical
|
|
13
|
+
state. Keep the DFA, lookahead machines, and fixed rule identities in the
|
|
14
|
+
snapshot. Materialize lazy generated transition rows during construction.
|
|
15
|
+
This makes the snapshot shareable independently of the lexer class and its
|
|
16
|
+
actions.
|
|
17
|
+
|
|
18
|
+
Use longest-match selection with trailing context, line anchors, and subject
|
|
19
|
+
encoding. Return the body end and the static `emit:` symbol when one exists.
|
|
20
|
+
Use the source-rule index for dynamic, skip, and default actions. The
|
|
21
|
+
recognizer computes rule identity before emission, including keyword
|
|
22
|
+
classification. Native `firstmatch` uses Ruby regexp alternative order and
|
|
23
|
+
raises an explicit error at recognizer construction.
|
|
24
|
+
|
|
25
|
+
Keep scan positions on the stack and expose a block form in addition to the
|
|
26
|
+
convenience tuple. A warmed Ruby 4.0.9 run measured 100,000 allocations
|
|
27
|
+
for 100,000 successful tuple calls. The same measurement recorded zero
|
|
28
|
+
allocations for block calls with ASCII, multibyte, and trailing-context
|
|
29
|
+
matches.
|
|
30
|
+
|
|
31
|
+
Include the native scanner helpers in `firstmatch` standalones. An ordinary
|
|
32
|
+
DFA standalone fixture with the recognizer used 90,990 bytes after separating
|
|
33
|
+
the helpers, compared with 102,424 bytes when it included them.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# ADR 0034: Checkpoint a lexer's position and state
|
|
2
|
+
|
|
3
|
+
## Context
|
|
4
|
+
|
|
5
|
+
Parser and incremental-processing clients need to resume a lexer at a saved
|
|
6
|
+
position. The runtime tracks its lexical stack, line and column, accumulated
|
|
7
|
+
text, and EOF actions alongside the byte offset.
|
|
8
|
+
|
|
9
|
+
## Decision
|
|
10
|
+
|
|
11
|
+
Expose `checkpoint`, `restore`, and `seek` on each lexer in runtime, generated,
|
|
12
|
+
and standalone mode. A checkpoint is an opaque immutable value tied to the
|
|
13
|
+
lexer instance. Restoring it reuses that instance's buffer and restores the
|
|
14
|
+
cursor, lexical stack, token bookkeeping, and EOF state. The scanning step
|
|
15
|
+
counter remains cumulative. Callers control application state changed by
|
|
16
|
+
actions.
|
|
17
|
+
|
|
18
|
+
Seek is a byte-oriented move that validates a buffered offset and derives
|
|
19
|
+
coordinates without running actions. It retains the lexical state and stack
|
|
20
|
+
while starting fresh token and EOF bookkeeping. A checkpoint holds cursor
|
|
21
|
+
state without retaining additional input. The streaming buffer's existing
|
|
22
|
+
retention policy determines which positions remain available.
|
|
23
|
+
|
|
24
|
+
See the [runtime reference](../reference/runtime.md#checkpoint-and-seek)
|
|
25
|
+
for IO and UTF-8 boundaries.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# ADR 0035: Filter rule selection by parser token expectations
|
|
2
|
+
|
|
3
|
+
## Context
|
|
4
|
+
|
|
5
|
+
The same text can have different meanings depending on the grammar position.
|
|
6
|
+
A parser may know which terminal types it can accept before it requests the
|
|
7
|
+
next token. Flexr already tracks every accepting rule in each DFA state.
|
|
8
|
+
|
|
9
|
+
## Decision
|
|
10
|
+
|
|
11
|
+
Accept an optional `expect:` array in `next_token` and `racc_next_token`. Use
|
|
12
|
+
`emit:` and rule-level `emits:` to determine eligibility. Consult the keyword
|
|
13
|
+
table using the exact candidate lexeme. Keep skip rules, trivia, and rules
|
|
14
|
+
without known output types eligible so they can advance the input or update
|
|
15
|
+
state. A multi-type annotation supplies eligibility information for a rule.
|
|
16
|
+
The action still decides which type to emit.
|
|
17
|
+
|
|
18
|
+
Use the interpreter's DFA acceptance lists for filtered longest-match
|
|
19
|
+
scans. Anchors, trailing context, and tie breakers retain their semantics.
|
|
20
|
+
The scan still observes the configured resource limits. Filter candidates in
|
|
21
|
+
the native scanner for `firstmatch` to preserve regexp and rule order.
|
|
22
|
+
|
|
23
|
+
Keep the existing generated scan path for unrestricted calls. Same-ABI
|
|
24
|
+
generated lexers already embed enough DFA data for the updated runtime to
|
|
25
|
+
filter `emit:` rules. New artifacts carry rule-level annotations as optional
|
|
26
|
+
metadata. An existing standalone artifact needs regeneration to embed the
|
|
27
|
+
updated runtime.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# ADR 0038: Resolve named captures over the winning rule body
|
|
2
|
+
|
|
3
|
+
## Context
|
|
4
|
+
|
|
5
|
+
A byte-oriented DFA can recognize a named group like an ordinary group.
|
|
6
|
+
It cannot recover how the selected match divides text among groups when
|
|
7
|
+
several paths accept the same input.
|
|
8
|
+
|
|
9
|
+
## Decision
|
|
10
|
+
|
|
11
|
+
Keep groups transparent in DFA compilation and remember which pattern
|
|
12
|
+
supplied the selected match. For longest-match scanners, replay that
|
|
13
|
+
pattern with Ruby's regexp engine between absolute anchors on the body.
|
|
14
|
+
Use the same character classes and Unicode data used to compile the DFA.
|
|
15
|
+
Firstmatch already has Ruby match data and can use that directly.
|
|
16
|
+
|
|
17
|
+
Expose a frozen hash in the action. The values describe the body before
|
|
18
|
+
action controls change the token span. Replay runs only when the action
|
|
19
|
+
reads `captures`. It preserves Ruby's rules for selecting among ambiguous
|
|
20
|
+
group spans while retaining flexr's longest-match rule and position tracking.
|
|
21
|
+
|
|
22
|
+
Generated scanners carry replay expressions only for patterns with named
|
|
23
|
+
groups. Artifacts with that metadata check the runtime's capture support.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# ADR 0039: Suspend input across include boundaries
|
|
2
|
+
|
|
3
|
+
## Context
|
|
4
|
+
|
|
5
|
+
A lexer action may recognize an include directive and need to insert another
|
|
6
|
+
source into the token stream. Both sources need their own coordinates,
|
|
7
|
+
EOF actions, and lexical states. An action can still use the directive's
|
|
8
|
+
matched text and location after it calls `push_input`.
|
|
9
|
+
|
|
10
|
+
## Decision
|
|
11
|
+
|
|
12
|
+
Validate a new buffer when `push_input` queues it. Activate it between token
|
|
13
|
+
matches and save the previous buffer and cursor. Start the new file at byte
|
|
14
|
+
zero with the initial lexical state. Run its EOF actions before restoring
|
|
15
|
+
the suspended buffer and cursor. Keep work accounting, cancellation, and the
|
|
16
|
+
configured recovery policy on the lexer instance.
|
|
17
|
+
|
|
18
|
+
Associate each checkpoint with the active input identity. Its restoration
|
|
19
|
+
operates on that input's cursor, while pending source pushes and application
|
|
20
|
+
state follow the actions that scheduled them. Keep IO open under caller
|
|
21
|
+
ownership so a child can consume it incrementally.
|
|
22
|
+
|
|
23
|
+
Embed the separate input stack helper only when a standalone grammar calls
|
|
24
|
+
`push_input` or explicitly selects `option :input_stack`. Generated rules
|
|
25
|
+
use the same buffer transition as interpreted rules.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# ADR 0040: Scan action-defined delimiters in the lexer runtime
|
|
2
|
+
|
|
3
|
+
## Context
|
|
4
|
+
|
|
5
|
+
Tagged heredocs and raw strings specify their delimiter in the source.
|
|
6
|
+
A rule can recognize the opener and determine its closing text at run time.
|
|
7
|
+
|
|
8
|
+
## Decision
|
|
9
|
+
|
|
10
|
+
Allow a matched rule's action to scan from its current byte position with
|
|
11
|
+
`scan_until` or `scan_until_line`. Track a matching delimiter prefix while
|
|
12
|
+
reading the existing streaming buffer so a delimiter can span chunks. Line
|
|
13
|
+
mode accepts a complete LF, CRLF, or EOF-terminated line at the beginning of
|
|
14
|
+
that line. Both methods enforce the lexer's resource limits.
|
|
15
|
+
|
|
16
|
+
Commit the cursor and match end after finding the delimiter. Return the
|
|
17
|
+
body before it. The regular token finalization updates coordinates for the
|
|
18
|
+
extended match and preserves the existing behavior of `less` and `more`.
|
|
19
|
+
Return `nil` at EOF if no delimiter matched, leaving the current match intact.
|
|
20
|
+
The action can decide whether to call `error!`.
|
|
21
|
+
|
|
22
|
+
Keep delimiter search in a standalone runtime helper. Embed it when the
|
|
23
|
+
grammar directly calls either method or selects `option :dynamic_delimiters`.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# ADR 0041: Insert indentation tokens after rule selection
|
|
2
|
+
|
|
3
|
+
## Context
|
|
4
|
+
|
|
5
|
+
In indentation-sensitive grammars, the parser needs tokens that mark entry
|
|
6
|
+
to and exit from a block. A block boundary depends on earlier line widths
|
|
7
|
+
and balanced brackets. The existing DFA selects individual rule matches.
|
|
8
|
+
|
|
9
|
+
## Decision
|
|
10
|
+
|
|
11
|
+
Configure the newline, indent, and dedent token types in the lexer DSL.
|
|
12
|
+
Record leading spaces and tabs before the runtime can discard the matched
|
|
13
|
+
bytes. Use default-channel newlines and original bracket lexemes to decide
|
|
14
|
+
when a line can change layout. Leave ordinary rule matching and token
|
|
15
|
+
actions on their existing path.
|
|
16
|
+
|
|
17
|
+
Emit synthetic tokens before the next visible rule token or at EOF. Queue
|
|
18
|
+
the scanned token behind those layout tokens. A checkpoint includes the
|
|
19
|
+
queue, the indentation widths, and the open brackets. Use a checkpoint for
|
|
20
|
+
nonzero positioning. Rewind to zero when layout needs to be read again.
|
|
21
|
+
|
|
22
|
+
Embed the layout processor in standalone artifacts only for lexers that
|
|
23
|
+
declare `indentation`. Keep the processor above any trivia-channel wrapper
|
|
24
|
+
so it uses the same visible token stream as the parser.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# ADR 0042: Delegate a capture with its original source coordinates
|
|
2
|
+
|
|
3
|
+
## Context
|
|
4
|
+
|
|
5
|
+
A host grammar can recognize an embedded region and delegate its contents to
|
|
6
|
+
another lexer. Equal strings can occur at several points in a single match.
|
|
7
|
+
The host needs the selected group's byte offset to locate the child's tokens.
|
|
8
|
+
|
|
9
|
+
## Decision
|
|
10
|
+
|
|
11
|
+
Reuse the named capture replay and its byte offsets. Give the child a slice
|
|
12
|
+
of the accepted match and run its usual scanner, including its actions. The
|
|
13
|
+
parent retains its own cursor and state. Translate the child's positions
|
|
14
|
+
and report them in the host file. Share the parent's configured limits and
|
|
15
|
+
bound nested delegation.
|
|
16
|
+
|
|
17
|
+
Only publish staged tokens after the child scan succeeds. Drain them through
|
|
18
|
+
the usual consumption API before scanning a new host token. A parser adapter
|
|
19
|
+
can consume the same token pairs through `racc_next_token`.
|
|
20
|
+
|
|
21
|
+
Embed the optional runtime helper when the standalone lexer source calls
|
|
22
|
+
`delegate` or selects `option :delegation`. Compile the embedded grammar as
|
|
23
|
+
a separate artifact that can load against the host's runtime.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# ADR 0043: Expand reusable lexer rules explicitly
|
|
2
|
+
|
|
3
|
+
## Context
|
|
4
|
+
|
|
5
|
+
Source order decides ties between rules of equal length and priority.
|
|
6
|
+
Inheritance would make a rule's position depend on declarations in another
|
|
7
|
+
class.
|
|
8
|
+
|
|
9
|
+
## Decision
|
|
10
|
+
|
|
11
|
+
Define reusable rules in modules extended with `Flexr::Rules`. A consuming
|
|
12
|
+
lexer calls `use` at the position where those declarations belong. The
|
|
13
|
+
rules receive new indexes and the caller's active states. The existing
|
|
14
|
+
priority and channel behavior applies to each copy. Lexer subclasses start
|
|
15
|
+
with their own specification.
|
|
16
|
+
|
|
17
|
+
The static generator resolves a named rule-set module in the same source
|
|
18
|
+
file and reports argument or declaration locations for unsupported forms.
|
|
19
|
+
Generated actions retain their original Ruby blocks so their lexical
|
|
20
|
+
variables and constants behave as in runtime mode.
|
|
21
|
+
|
|
22
|
+
## Consequences
|
|
23
|
+
|
|
24
|
+
Static generation supports one direct declaration per rule-set module.
|
|
25
|
+
External rule sets and dynamically selected modules can be used with the
|
|
26
|
+
runtime lexer. Standalone generation includes rule-set support only when
|
|
27
|
+
the source declares such a module.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# ADR 0044: Preserve first-match order when importing oedipus_lex
|
|
2
|
+
|
|
3
|
+
## Context
|
|
4
|
+
|
|
5
|
+
Rexical and oedipus_lex share the `.rex` extension. Flexr's existing
|
|
6
|
+
Rexical migration emits longest-match rules and reports known changes in
|
|
7
|
+
rule selection. The [oedipus_lex rules reference](https://docs.seattlerb.org/oedipus_lex/)
|
|
8
|
+
describes Ruby regexps and source-order rule selection.
|
|
9
|
+
|
|
10
|
+
## Decision
|
|
11
|
+
|
|
12
|
+
Use `flexr import --from oedipus_lex` to select the format. Emit
|
|
13
|
+
`backend :firstmatch` and keep the source rule order and regexp alternative
|
|
14
|
+
order. Resolve only declared regexp macros. Translate skip actions and
|
|
15
|
+
statically recognizable token pairs. Validate the regexp against the
|
|
16
|
+
firstmatch backend's supported syntax before emitting it.
|
|
17
|
+
|
|
18
|
+
Report unsupported code and directives with original file and line numbers.
|
|
19
|
+
Write output only when every part of the specification is supported. Keep Rexical import behavior when no format is supplied.
|
|
20
|
+
|
|
21
|
+
## Evidence
|
|
22
|
+
|
|
23
|
+
The [migration guide](../how-to/migrate-from-oedipus-lex.md#trial-against-ruby_parser)
|
|
24
|
+
records the unsupported parts of the ruby_parser 3.22.0 lexer. A separate
|
|
25
|
+
parity check with oedipus_lex 2.6.3 matched 274 token pairs across six inputs
|
|
26
|
+
in runtime, generated, and standalone Flexr modes on Ruby 3.4.9 and 4.0.9.
|
|
Binary file
|
|
@@ -21,10 +21,11 @@ backend is recorded in generated headers.
|
|
|
21
21
|
|
|
22
22
|
## `firstmatch`
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
Stable compatibility behavior selected explicitly with `backend :firstmatch`.
|
|
25
|
+
Runtime and generated lexers select the first matching rule in source order,
|
|
26
|
+
even when a later rule could consume more text. `FLEXR-W010` reports potential
|
|
27
|
+
differences from longest-match backends. See
|
|
28
|
+
[ADR 0023](../adr/0023-stable-explicit-firstmatch.md) for the decision.
|
|
28
29
|
|
|
29
30
|
## Acceleration and packing
|
|
30
31
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# Integrate with Lrama
|
|
2
2
|
|
|
3
|
-
Lrama
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
Lrama generates a C parser that reads tokens from its C `yylex` function.
|
|
4
|
+
The Flexr lexer supplies the token kinds and values. A Ruby driver can send
|
|
5
|
+
those pairs to the compiled parser, as in the generated calculator project.
|
|
6
6
|
|
|
7
7
|
```ruby
|
|
8
8
|
class Lexer < Flexr::Lexer
|
|
@@ -13,9 +13,10 @@ class Lexer < Flexr::Lexer
|
|
|
13
13
|
end
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
the value returned by the lexer.
|
|
16
|
+
Keep the names in `emits` aligned with the grammar's `%token` declarations.
|
|
17
|
+
Use `flexr check lexer.flexr.rb --grammar parser.y` to compare the names.
|
|
19
18
|
|
|
20
|
-
|
|
21
|
-
[
|
|
19
|
+
Generate a working project with `flexr new NAME --parser lrama`.
|
|
20
|
+
The [calculator tutorial](../tutorial/build-a-calculator-parser.md#lrama)
|
|
21
|
+
shows how to build and test it with a C compiler. The [lexer example](../../examples/with_lrama/README.md)
|
|
22
|
+
shows how to inspect the token stream.
|