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
@@ -6,7 +6,7 @@ byte-oriented automaton. All matches start at the current lexer position.
6
6
  | Ruby construct | Status | Notes and alternative |
7
7
  |---|---|---|
8
8
  | Literals, concatenation, alternation | Supported | |
9
- | Groups and `(?:...)` | Supported | Captures are treated as non-capturing and warn |
9
+ | Unnamed groups and `(?:...)` | Supported | Captures are treated as non-capturing and warn |
10
10
  | Character classes and ranges | Supported | Includes trailing `-`, POSIX classes, and `&&` intersection |
11
11
  | Unicode `\p{...}` / `\P{...}` | Supported | Uses the vendored UCD snapshot |
12
12
  | `*`, `+`, `?` | Supported | Greedy forms only |
@@ -15,14 +15,26 @@ byte-oriented automaton. All matches start at the current lexer position.
15
15
  | `/i`, `/m`, `/x`, `/n` | Supported | Inline forms are supported where Ruby permits them |
16
16
  | `\d`, `\w`, `\s` | Supported | Unicode-aware with `option :unicode` in UTF-8 |
17
17
  | `\h`, `\H` | Supported | Hexadecimal digits and their complement |
18
- | Lookahead / lookbehind | Unsupported | Use `followed_by:` or a state; `FLEXR-E014` |
19
- | Backreferences | Unsupported | Split the language into states or actions |
20
- | `\b`, `\B`, `\A`, `\z`, `\G`, `\K` | Unsupported | Use rule boundaries or state/action logic |
21
- | Lazy or possessive quantifiers | Unsupported | Use a negated character class or separate rule |
22
- | Conditional, atomic, and capture-dependent groups | Unsupported | Rewrite as DFA-compatible rules |
18
+ | Octal escapes such as `\011` | Unsupported | Use a hexadecimal escape such as `\x09` |
19
+ | `\R` | Unsupported | Use the standalone line-break rule shown by `FLEXR-E014` for the specification's encoding |
20
+ | Lookahead / lookbehind | Unsupported | Use `followed_by:` for trailing positive lookahead; other forms need rules or states; `FLEXR-E014` |
21
+ | Backreferences | Unsupported | Enumerate finite repeated strings, or use states and action checks |
22
+ | `\b`, `\B` | Unsupported | Use the literal-boundary examples in `FLEXR-E014` where applicable; token edges need rules or state checks |
23
+ | `\A`, `\z`, `\Z` | Unsupported | The `^` / `$` examples in `FLEXR-E014` apply only to input without line feeds |
24
+ | `\G` | Unsupported | At the start of a rule, omit it: rules already match at the current lexer position |
25
+ | `\K` | Unsupported | Use the prefix/suffix state example in `FLEXR-E014` where applicable to preserve token text and location |
26
+ | Subexpression calls `\g<...>` | Unsupported | Inline non-recursive expressions; recursive calls need a parser or state stack |
27
+ | `\X` | Unsupported | Use the ASCII-only standalone token example in `FLEXR-E014` where applicable; Unicode graphemes need separate segmentation |
28
+ | Lazy or possessive quantifiers | Unsupported | For unescaped single-line quoted text, use `/"[^"\n]*"/`; other patterns need a separate rewrite |
29
+ | Atomic groups | Unsupported | Use the delimited-text example in `FLEXR-E014` where applicable; removing atomic groups can change matches |
30
+ | Named captures | Unsupported | Use `(?:...)` when only grouping is needed; capture-dependent patterns need a separate rewrite |
31
+ | Conditional groups | Unsupported | Use the finite alternatives shown by `FLEXR-E014` where applicable, or separate rules and state checks |
32
+ | Absence groups `(?~...)` | Unsupported | For a single excluded literal, use the negated character class shown by `FLEXR-E014`; larger expressions need a separate rewrite |
23
33
 
24
34
  Malformed syntax produces `FLEXR-E001`. Unsupported constructs produce
25
35
  `FLEXR-E014`; the diagnostic help normally names the supported replacement.
36
+ Several `FLEXR-E014` diagnostics include concrete
37
+ [rule rewrite examples](diagnostics.md#regexp-rewrite-help).
26
38
  Unknown alphabetic escapes are rejected rather than silently treated as
27
39
  literals; escaped regexp punctuation remains supported.
28
40
  Empty matches are rejected with `FLEXR-E005` unless `option :allow_empty_match`
@@ -36,6 +48,6 @@ not exceed 1000. `{n,}` uses an unbounded loop after the required copies.
36
48
 
37
49
  ## Captures
38
50
 
39
- Capturing parentheses do not expose match groups to actions. They are treated
51
+ Unnamed capturing parentheses do not expose match groups to actions. They are treated
40
52
  as grouping syntax and produce `FLEXR-W013`; use `(?:...)` and inspect `text`
41
53
  in the action instead.
@@ -0,0 +1,34 @@
1
+ # Ruby LSP integration
2
+
3
+ flexr includes an optional add-on for [Ruby LSP](https://shopify.github.io/ruby-lsp/add-ons.html).
4
+ It supports Ruby LSP `~> 0.26.11`; incompatible server versions skip activation.
5
+ The lexer runtime does not depend on Ruby LSP.
6
+
7
+ Keep flexr in the project's bundle and enable its linter in the editor's Ruby
8
+ LSP configuration. For VS Code, add `flexr` to the existing list:
9
+
10
+ ```json
11
+ {
12
+ "rubyLsp.linters": ["flexr"]
13
+ }
14
+ ```
15
+
16
+ Keep any other linters you use in that array. Ruby LSP discovers the add-on
17
+ automatically. It displays flexr diagnostics for `.flexr.rb` files using the
18
+ current editor contents, including unsaved edits. Rule hovers show compiler
19
+ warnings, counterexamples, and overlap information. Diagnostics and hovers
20
+ use the static reader: they do not evaluate specifications or run actions.
21
+ Syntax errors are reported by Ruby LSP's Prism integration.
22
+
23
+ The add-on uses the same static specification subset as `flexr check`, so
24
+ dynamic Ruby DSL construction must be checked separately. Compilation is
25
+ cached for the most recent document contents. Timing-dependent `FLEXR-W016`
26
+ is omitted; other diagnostics include their help and notes. The add-on honors
27
+ the server's negotiated UTF-8, UTF-16, or UTF-32 position encoding.
28
+
29
+ To run the integration tests against the real Ruby LSP server:
30
+
31
+ ```sh
32
+ BUNDLE_GEMFILE=gemfiles/ruby_lsp.gemfile bundle install
33
+ BUNDLE_GEMFILE=gemfiles/ruby_lsp.gemfile bundle exec rspec -Ilib -Ispec test/ruby_lsp_addon_spec.rb
34
+ ```
@@ -0,0 +1,57 @@
1
+ # Testing lexers
2
+
3
+ The helpers consume the complete input and compare the returned tokens. They
4
+ work with runtime and generated lexer classes, and forward keyword options to
5
+ the lexer constructor. Install the test framework separately; requiring `flexr`
6
+ does not load either framework.
7
+
8
+ ## RSpec
9
+
10
+ ```ruby
11
+ require "flexr/rspec"
12
+
13
+ expect(Lexer).to tokenize("12 + 3").as([
14
+ [:INTEGER, 12], [:PLUS, "+"], [:INTEGER, 3]
15
+ ])
16
+ ```
17
+
18
+ Use `.as` to compare token values exactly, including symbol/string distinctions.
19
+ Lexer errors propagate so that tests can use the usual `raise_error` matcher.
20
+
21
+ ## Minitest
22
+
23
+ ```ruby
24
+ require "minitest/autorun"
25
+ require "flexr/minitest"
26
+
27
+ class LexerTest < Minitest::Test
28
+ include Flexr::Minitest::Assertions
29
+
30
+ def test_numbers
31
+ assert_tokenize Lexer, "12", [[:INTEGER, 12]], chunk_size: 1
32
+ end
33
+ end
34
+ ```
35
+
36
+ ## Token snapshots
37
+
38
+ Store snapshots in a directory owned by the test suite. Snapshot assertions
39
+ fail if the file is missing or differs. They write files only when the call
40
+ explicitly passes `update: true`; review the resulting diff before committing.
41
+
42
+ ```ruby
43
+ expect(Lexer).to tokenize("12 + 3").matching_snapshot("spec/snapshots/sum.json")
44
+ assert_token_snapshot Lexer, "12 + 3", "test/snapshots/sum.json"
45
+ ```
46
+
47
+ To create or replace a snapshot, temporarily add `update: true` to
48
+ `matching_snapshot` or `assert_token_snapshot`, then remove it for normal tests.
49
+ Create the parent directory first.
50
+
51
+ Snapshots use deterministic JSON, preserving symbols and non-UTF-8 bytes with
52
+ tags. Hash entries are sorted by their serialized keys, so insertion order
53
+ does not affect the snapshot. Supported values are nil, booleans, finite
54
+ numbers, strings, symbols, arrays, hashes, `Flexr::Runtime::Token`, and
55
+ `Flexr::Runtime::Location`. Convert custom token values to these types before
56
+ snapshot testing. Snapshot strings compare content rather than Ruby encoding
57
+ tags, allowing ASCII strings from String and IO input to use the same file.
@@ -0,0 +1,14 @@
1
+ # GraphQL subset lexer
2
+
3
+ Tokenizes names, numbers, single-line strings, comments, ignored commas, spread,
4
+ variables, directives, and punctuation. Keywords remain `NAME` tokens so the
5
+ parser can interpret them by context. Values remain source text.
6
+ Block strings, escape validation, and grammar validation are outside this example.
7
+
8
+ ```sh
9
+ ruby -Ilib -e 'load "examples/graphql/lexer.flexr.rb"; p GraphqlExample::Lexer.new("query { user(id: 12) { name } }").tokens'
10
+ bundle exec ruby benchmark/run.rb --spec examples/graphql/lexer.flexr.rb --iterations 1 --json
11
+ ```
12
+
13
+ The benchmark uses a repeated built-in query and compares runtime with generated
14
+ mode. Pass `--input-file PATH` to measure a different corpus.
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "flexr"
4
+
5
+ module GraphqlExample
6
+ class Lexer < Flexr::Lexer
7
+ emits :NAME, :STRING, :NUMBER, :SPREAD, :LBRACE, :RBRACE, :LPAREN,
8
+ :RPAREN, :LBRACKET, :RBRACKET, :COLON, :EQUAL, :BANG, :DOLLAR, :AT, :PIPE, :AMP
9
+ rule(/[ \t\r\n,]+/, skip: true)
10
+ rule(/#[^\n]*/, skip: true)
11
+ rule(/[A-Za-z_][A-Za-z_0-9]*/, emit: :NAME)
12
+ rule(/-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?/, emit: :NUMBER)
13
+ rule(/"(?:\\.|[^"\\\r\n])*"/, emit: :STRING)
14
+ rule(/\.\.\./, emit: :SPREAD)
15
+ rule(/\{/, emit: :LBRACE)
16
+ rule(/\}/, emit: :RBRACE)
17
+ rule(/\(/, emit: :LPAREN)
18
+ rule(/\)/, emit: :RPAREN)
19
+ rule(/\[/, emit: :LBRACKET)
20
+ rule(/\]/, emit: :RBRACKET)
21
+ rule(/:/, emit: :COLON)
22
+ rule(/=/, emit: :EQUAL)
23
+ rule(/!/, emit: :BANG)
24
+ rule(/\$/, emit: :DOLLAR)
25
+ rule(/@/, emit: :AT)
26
+ rule(/\|/, emit: :PIPE)
27
+ rule(/&/, emit: :AMP)
28
+ end
29
+ end
@@ -0,0 +1,16 @@
1
+ # SQL subset lexer
2
+
3
+ Tokenizes a small set of case-insensitive SQL keywords, unquoted names, decimal
4
+ numbers, single-quoted strings (including doubled quotes), line comments, and
5
+ operators. Keywords are uppercased; other values remain source text. Longest
6
+ match keeps `select_name` as one name and `>=` as one operator.
7
+ Quoted identifiers, block comments, vendor-specific syntax, and SQL grammar
8
+ validation are outside this example. Never use this lexer to sanitize SQL.
9
+
10
+ ```sh
11
+ ruby -Ilib -e 'load "examples/sql/lexer.flexr.rb"; p SqlExample::Lexer.new("select name from users where id >= 12;").tokens'
12
+ bundle exec ruby benchmark/run.rb --spec examples/sql/lexer.flexr.rb --iterations 1 --json
13
+ ```
14
+
15
+ The benchmark uses a repeated built-in query and compares runtime with generated
16
+ mode. Pass `--input-file PATH` to measure a different corpus.
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "flexr"
4
+
5
+ module SqlExample
6
+ class Lexer < Flexr::Lexer
7
+ emits :KEYWORD, :NAME, :STRING, :NUMBER, :OPERATOR, :COMMA, :DOT, :SEMICOLON, :LPAREN, :RPAREN
8
+ rule(/[ \t\r\n]+/, skip: true)
9
+ rule(/--[^\n]*/, skip: true)
10
+ rule(/SELECT|FROM|WHERE|AND|OR|NOT|NULL|AS|JOIN|ON|ORDER|BY|GROUP|HAVING|LIMIT|INSERT|INTO|VALUES|UPDATE|SET|DELETE/i) do
11
+ emit :KEYWORD, text.upcase
12
+ end
13
+ rule(/[A-Za-z_][A-Za-z_0-9]*/, emit: :NAME)
14
+ rule(/'(?:''|[^'])*'/, emit: :STRING)
15
+ rule(/[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?/, emit: :NUMBER)
16
+ rule(%r{>=|<=|<>|!=|[=<>+*/-]}, emit: :OPERATOR)
17
+ rule(/,/, emit: :COMMA)
18
+ rule(/\./, emit: :DOT)
19
+ rule(/;/, emit: :SEMICOLON)
20
+ rule(/\(/, emit: :LPAREN)
21
+ rule(/\)/, emit: :RPAREN)
22
+ end
23
+ end
@@ -0,0 +1,14 @@
1
+ # TOML subset lexer
2
+
3
+ Tokenizes bare names, single-line quoted strings, decimal numbers, booleans,
4
+ tables, inline tables, arrays, and comments. Values remain source text.
5
+ This is a lexer example, not a TOML validator: multiline strings, date/time
6
+ values, non-decimal numbers, and escape decoding need additional rules or a parser.
7
+
8
+ ```sh
9
+ ruby -Ilib -e 'load "examples/toml/lexer.flexr.rb"; p TomlExample::Lexer.new(%q(title = "flexr")).tokens'
10
+ bundle exec ruby benchmark/run.rb --spec examples/toml/lexer.flexr.rb --iterations 1 --json
11
+ ```
12
+
13
+ The benchmark uses a repeated built-in document fragment and compares runtime
14
+ with generated mode. Pass `--input-file PATH` to measure a different corpus.
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "flexr"
4
+
5
+ module TomlExample
6
+ class Lexer < Flexr::Lexer
7
+ emits :NAME, :STRING, :NUMBER, :BOOLEAN, :EQUAL, :DOT, :COMMA,
8
+ :LBRACKET, :RBRACKET, :LBRACE, :RBRACE
9
+ rule(/[ \t\r\n]+/, skip: true)
10
+ rule(/#[^\n]*/, skip: true)
11
+ rule(/"(?:\\.|[^"\\\r\n])*"|'[^'\r\n]*'/, emit: :STRING)
12
+ rule(/true|false/, emit: :BOOLEAN)
13
+ rule(/[+-]?[0-9]+(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?/, emit: :NUMBER)
14
+ rule(/[A-Za-z_][A-Za-z_0-9-]*/, emit: :NAME)
15
+ rule(/=/, emit: :EQUAL)
16
+ rule(/\./, emit: :DOT)
17
+ rule(/,/, emit: :COMMA)
18
+ rule(/\[/, emit: :LBRACKET)
19
+ rule(/\]/, emit: :RBRACKET)
20
+ rule(/\{/, emit: :LBRACE)
21
+ rule(/\}/, emit: :RBRACE)
22
+ end
23
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ eval_gemfile "../Gemfile"
4
+ gem "ruby-lsp", "~> 0.26.11"
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec path: ".."
6
+ gem "rbs", "~> 4.0"
7
+ gem "steep", "~> 2.1"
@@ -9,7 +9,7 @@ module Flexr
9
9
 
10
10
  def extract(dfa)
11
11
  dfa.transitions.each_index.filter_map do |state|
12
- bytes = Analysis.self_loop_set(dfa, state)
12
+ bytes = self_loop_set(dfa, state)
13
13
  next if bytes.empty?
14
14
 
15
15
  Region.new(
@@ -20,6 +20,16 @@ module Flexr
20
20
  end
21
21
  end
22
22
 
23
+ def self_loop_set(dfa, state)
24
+ representatives = Array.new(dfa.class_count)
25
+ dfa.ec.each_with_index { |class_id, byte| representatives[class_id] ||= byte }
26
+ self_loops = representatives.each_index.map do |class_id|
27
+ byte = representatives.fetch(class_id)
28
+ byte && dfa.transition(state, byte) == state
29
+ end
30
+ dfa.ec.each_index.select { |byte| self_loops.fetch(dfa.ec.fetch(byte)) }
31
+ end
32
+
23
33
  def regexp_for(bytes, binary: true)
24
34
  source = bytes_to_source(bytes)
25
35
  options = binary ? ::Regexp::NOENCODING : 0
@@ -19,13 +19,7 @@ module Flexr
19
19
  end
20
20
 
21
21
  def self_loop_set(dfa, state)
22
- representatives = Array.new(dfa.class_count)
23
- dfa.ec.each_with_index { |class_id, byte| representatives[class_id] ||= byte }
24
- self_loops = representatives.each_index.map do |class_id|
25
- byte = representatives.fetch(class_id)
26
- byte && dfa.transition(state, byte) == state
27
- end
28
- dfa.ec.each_index.select { |byte| self_loops.fetch(dfa.ec.fetch(byte)) }
22
+ Accel.self_loop_set(dfa, state)
29
23
  end
30
24
 
31
25
  def dead_states(dfa)
@@ -71,6 +65,166 @@ module Flexr
71
65
  representatives.values.sort_by { |byte| [byte.between?(32, 126) ? 0 : 1, byte] }
72
66
  end
73
67
 
68
+ def shadowing_examples(dfa, rule_indices, include_empty: false, include_unshadowed: false)
69
+ remaining = (rule_indices & dfa.rule_ids).to_h { |index| [index, true] }
70
+ return {} if remaining.empty?
71
+
72
+ examples = {}
73
+ queue = [[dfa.start, nil, nil]]
74
+ # Visiting the start again permits a nonempty example when it accepts and loops.
75
+ visited = {}
76
+ bytes = joint_byte_representatives(dfa, dfa)
77
+ cursor = 0
78
+ while cursor < queue.length
79
+ state = queue.fetch(cursor).first
80
+ accepts = dfa.accepts.fetch(state)
81
+ winner = accepts.min_by(&:rule_index)&.rule_index
82
+ if (include_empty || cursor.positive?) && winner
83
+ shadowed = accepts.map(&:rule_index).uniq.select do |index|
84
+ (include_unshadowed || index > winner) && remaining.key?(index)
85
+ end
86
+ unless shadowed.empty?
87
+ input = example_input(queue, cursor)
88
+ shadowed.each do |index|
89
+ examples[index] = [input, winner]
90
+ remaining.delete(index)
91
+ end
92
+ return examples if remaining.empty?
93
+ end
94
+ end
95
+
96
+ bytes.each do |byte|
97
+ target = dfa.transition(state, byte)
98
+ next if target.nil? || visited[target]
99
+
100
+ visited[target] = true
101
+ queue << [target, cursor, byte]
102
+ end
103
+ cursor += 1
104
+ end
105
+ examples
106
+ end
107
+
108
+ def example_input(paths, cursor)
109
+ bytes = []
110
+ while (parent = paths.fetch(cursor)[1])
111
+ bytes << paths.fetch(cursor)[2]
112
+ cursor = parent
113
+ end
114
+ bytes.reverse.pack("C*")
115
+ end
116
+
117
+ def shortest_input(dfa)
118
+ queue = [[dfa.start, nil, nil]]
119
+ visited = { dfa.start => true }
120
+ bytes = joint_byte_representatives(dfa, dfa)
121
+ cursor = 0
122
+ while cursor < queue.length
123
+ state = queue.fetch(cursor).first
124
+ return example_input(queue, cursor) if accepting?(dfa, state)
125
+
126
+ bytes.each do |byte|
127
+ target = dfa.transition(state, byte)
128
+ next if target.nil? || visited[target]
129
+
130
+ visited[target] = true
131
+ queue << [target, cursor, byte]
132
+ end
133
+ cursor += 1
134
+ end
135
+ nil
136
+ end
137
+
138
+ def trailing_example(dfa, rules, target, input, trailing_dfas, encoding:, unicode:, firstmatch:, same_length: false)
139
+ lengths = if firstmatch
140
+ rules.filter_map do |rule|
141
+ next if rule.index > target.index
142
+
143
+ length = firstmatch_length(rule, input, encoding: encoding, unicode: unicode)
144
+ [rule.index, length] if length
145
+ end
146
+ else
147
+ dfa_match_lengths(dfa, input, encoding: encoding)
148
+ end
149
+ matches = {}
150
+ lengths.each do |index, length|
151
+ trailing = trailing_dfas[index]
152
+ lookahead = trailing ? dfa_match_lengths(trailing, input, offset: length).map(&:last).max : 0
153
+ next unless lookahead
154
+
155
+ total = length + lookahead
156
+ next if matches[index] && matches[index].last > total
157
+
158
+ matches[index] = [length, total]
159
+ end
160
+ return unless matches.key?(target.index)
161
+
162
+ winner, (length, total) = matches.max_by { |index, (_, size)| firstmatch ? -index : [size, -index] }
163
+ return if same_length && total != matches.fetch(target.index).last
164
+
165
+ [winner, length, total - length]
166
+ rescue EncodingError
167
+ nil
168
+ end
169
+
170
+ def dfa_match_lengths(dfa, input, offset: 0, encoding: Encoding::BINARY)
171
+ state = dfa.start
172
+ cursor = offset
173
+ matches = []
174
+ loop do
175
+ if encoding != Encoding::UTF_8 || input.byteslice(0, cursor).valid_encoding?
176
+ dfa.accepts.fetch(state).each do |acceptance|
177
+ next if acceptance.end_anchor && cursor < input.bytesize && input.getbyte(cursor) != 0x0a
178
+
179
+ matches << [acceptance.rule_index, cursor - offset]
180
+ end
181
+ end
182
+ break if cursor == input.bytesize
183
+
184
+ state = dfa.transition(state, input.getbyte(cursor))
185
+ break unless state
186
+
187
+ cursor += 1
188
+ end
189
+ matches
190
+ end
191
+
192
+ def firstmatch_example(rules, target, input, encoding:, unicode: false, same_length: false)
193
+ target_length = firstmatch_length(target, input, encoding: encoding, unicode: unicode)
194
+ return unless target_length
195
+
196
+ rules.sort_by(&:index).each do |rule|
197
+ break if rule.index >= target.index
198
+
199
+ length = firstmatch_length(rule, input, encoding: encoding, unicode: unicode)
200
+ next unless length
201
+
202
+ return nil if same_length && length != target_length
203
+
204
+ return [rule.index, length]
205
+ end
206
+ [target.index, target_length]
207
+ rescue EncodingError
208
+ nil
209
+ end
210
+
211
+ def firstmatch_length(rule, input, encoding:, unicode:)
212
+ matches = rule.patterns.each_with_index.filter_map do |pattern, index|
213
+ regexp = pattern.is_a?(::Regexp) ? pattern : ::Regexp.new(::Regexp.escape(pattern.to_s))
214
+ reference = Regexp::Matcher.reference?(regexp, encoding: encoding, unicode: unicode)
215
+ match = Regexp::Matcher.match(regexp, input, encoding: encoding, unicode: unicode, reference: reference)
216
+ next unless match&.begin(0)&.zero?
217
+
218
+ [match[0].bytesize, rule.pattern_conditions.fetch(index)]
219
+ end
220
+ length, condition = matches.max_by(&:first)
221
+ return unless length
222
+ return if encoding == Encoding::UTF_8 && !input.byteslice(0, length).valid_encoding?
223
+ return if condition.end_anchor && length < input.bytesize && input.getbyte(length) != 0x0a
224
+
225
+ length
226
+ end
227
+
74
228
  def accepting?(dfa, state)
75
229
  state && !dfa.accepts.fetch(state).empty?
76
230
  end