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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: edb2c916db82e07c56290ab6365d50bf9a0a148278ca811a100063031315bbf5
4
- data.tar.gz: 13c08660e91681ab2143766d58072c4c4e3d081b5899a18568a772bfd80810b9
3
+ metadata.gz: c5b0574ea7d9a996d3c0c6d5d598d401514d769752f41d6a152da75ce15d7ab9
4
+ data.tar.gz: 99fcbc8c588e6a160238784c50e6ec64fd7e2c504555692c07de90e1e88c66ab
5
5
  SHA512:
6
- metadata.gz: aef18f76825b6544c03a3115a1dfeb8cd469560fb4bff23b2fa3e57cc9504e780a318ebfcc06d1ff7c8807c217b813a6a2768d154a8596cf7103347fd97e399e
7
- data.tar.gz: 0355b004bc8ff95a00fd4be5fd4644f4cce1a0f2feaad49da48ef023fc594453de208600d542a26a6bc6fcb4a645864770816ed8d4b4411e8607860801d1c43c
6
+ metadata.gz: 1144c4cade7a85225a4c29bcd70ab820b28058c301ccac13b528deb73f0b0d032fa922abe02ba316b1ff76d17b9f1296342ed1670b8953c8457cc59e0de53c1d
7
+ data.tar.gz: e2fa5a101322040d2ba9497c2147bf532a13976bb997c255502609dc194bbbe0ec83449c695da015fd0b5a3e3b3d5d73404300f64bb9d0cc33afa864b79a98f4
data/.rubocop.yml CHANGED
@@ -2,6 +2,7 @@ AllCops:
2
2
  TargetRubyVersion: 3.1
3
3
  NewCops: enable
4
4
  Exclude:
5
+ - "benchmark/golden/**/*"
5
6
  - "lib/flexr/codegen/**/*"
6
7
  - "lib/flexr/unicode/data/**/*"
7
8
  - "lib/flexr/regexp/tokenizer.rb"
data/CHANGELOG.md ADDED
@@ -0,0 +1,116 @@
1
+ # Changelog
2
+
3
+ Notable changes to flexr, newest first. Release dates use Japan Standard Time
4
+ (UTC+09:00).
5
+
6
+ ## 1.2.0
7
+
8
+ ### Upgrade to 2.0
9
+
10
+ - Run `flexr check SPEC --target 2.0` to identify structured-token declarations and
11
+ see the Ruby and generated-artifact requirements for 2.0.
12
+ - Set `RUBYOPT=-W:deprecated` to find record mutations, unknown error modes,
13
+ older Ruby versions, and generated lexers that need regeneration in 2.0.
14
+ See the [migration guide](https://github.com/ydah/flexr/blob/v2.0.0/docs/migration-v2.md).
15
+ - 1.2 retains Ruby 3.1 runtime support, mutable `Struct` tokens and locations,
16
+ and generated artifact ABI 1. The Ruby 3.3 minimum for static generation
17
+ stays in effect.
18
+
19
+ ### Added
20
+
21
+ - `trace --input` and `trace --input-file` show candidate lengths, rule
22
+ selection, and actions in human or JSON output for runtime and generated lexers.
23
+ - Diagnostics identify specification locations and show examples for shadowed
24
+ and overlapping rules. Unsupported regexp constructs include rewrite guidance.
25
+ - Compare `emits` with parser declarations using `check --grammar PATH` and
26
+ export token declarations using `tokens --format lrama`.
27
+ - Optional Ruby LSP diagnostics and rule hovers, RSpec and Minitest token
28
+ assertions, and RBS signatures for the stable public API.
29
+ - Runnable TOML, GraphQL, and SQL subset lexer examples.
30
+
31
+ ### Changed
32
+
33
+ - The explicit `firstmatch` backend is stable and no longer requires
34
+ `option :experimental`. The default still selects the longest match.
35
+
36
+ ### Fixed
37
+
38
+ - Generated lexers preserve UTF-8 action literals and surrounding source.
39
+ - Missing Prism and incompatible generated-artifact errors identify the
40
+ relevant source file and show how to resolve the problem.
41
+
42
+ ## [1.1.0](https://github.com/ydah/flexr/compare/v1.0.0...v1.1.0) - 2026-08-13
43
+
44
+ ### Added
45
+
46
+ - Runtime limits for lookahead, retained buffer size, and scanning steps, plus a
47
+ cancellation callback. `retain_input: false` discards consumed input so
48
+ streaming lexers can keep a sliding buffer.
49
+ - Regexp support for open repetition (`{n,}`), character-class intersection
50
+ (`&&`), trailing hyphens in classes, and hexadecimal classes (`\h` and `\H`).
51
+ - Explicit `flexr/runtime`, `flexr/generator`, and `flexr/cli` entry points.
52
+ `require "flexr"` loads build tools lazily.
53
+
54
+ ### Changed
55
+
56
+ - Unicode properties and POSIX classes now compile into the DFA, using the
57
+ same vendored Unicode 15.1.0 data for longest-match selection.
58
+ - Compiler construction and minimization avoid repeated work. Generated
59
+ scanners use state-specific fast paths, and token locations track positions
60
+ incrementally instead of rescanning consumed input.
61
+ - Packed tables and the `direct` backend avoid retaining duplicate transition
62
+ tables. The `auto` backend selects a representation using estimated table
63
+ size and lookup cost.
64
+
65
+ ### Fixed
66
+
67
+ - Aligned action precedence, nested state membership, and structured errors
68
+ across runtime, generated, and standalone modes.
69
+ - Static generation preserves heredocs and `__END__` data, resolves constants
70
+ in source order, and retains interpolated regexp options. Unsupported DSL
71
+ control flow and ambiguous lexer classes are rejected with `FLEXR-E017`.
72
+ - Generation refuses to overwrite its input, including symlink and hard-link
73
+ aliases. Output writes are atomic and leave unchanged files untouched;
74
+ non-`.flexr.rb` inputs default to a separate `.generated.rb` output.
75
+ - UTF-8 `less` and empty-match handling respect codepoint boundaries.
76
+ Token limits apply before actions run, and non-progressing scan cycles raise
77
+ a diagnostic instead of looping indefinitely.
78
+
79
+ ### Upgrade notes
80
+
81
+ - Regenerate lexer artifacts when upgrading from v1.0.0. Generated files now
82
+ record schema, runtime ABI, compiler, and Unicode versions; missing or
83
+ incompatible metadata raises `FLEXR-E020`.
84
+ - The retained input buffer now defaults to a 64 MiB limit. Adjust
85
+ `max_buffer_size` when retaining larger inputs, or use `retain_input: false`
86
+ for streaming lexers that can discard consumed prefixes.
87
+ - Specifications become immutable after compilation. Later DSL changes raise
88
+ `Flexr::FrozenSpecificationError`; unknown option names and conflicting state
89
+ declarations are rejected immediately.
90
+ - Unknown alphabetic regexp escapes are rejected instead of treated as
91
+ literals. Unknown `on_error` callback results raise
92
+ `InvalidRecoveryActionError` instead of falling back to the default policy.
93
+ - `firstmatch` remains experimental. Runtime and generated modes share its
94
+ rule-order behavior; differences from longest-match behavior produce
95
+ `FLEXR-W010` instead of aborting generation.
96
+
97
+ ## [1.0.0](https://github.com/ydah/flexr/tree/v1.0.0) - 2026-08-11
98
+
99
+ Initial release.
100
+
101
+ ### Added
102
+
103
+ - A pure Ruby lexer DSL with deterministic longest-match selection and source
104
+ order as the tie-breaker, available in runtime and generated modes.
105
+ - Static source generation with Prism, standalone output that does not require
106
+ the flexr gem, and a Rake task for generating lexers during builds.
107
+ - Lexical states, trailing context, EOF actions, token locations, streaming IO,
108
+ UTF-8 and binary input, and Unicode 15.1.0 property data.
109
+ - Stable `table`, `direct`, and `auto` backends, optional scanner acceleration,
110
+ packed tables, and an experimental `firstmatch` backend.
111
+ - CLI diagnostics, rule and DFA inspection, Graphviz output, benchmarks, and
112
+ import tooling for flex and Rexical specifications.
113
+ - Calculator, JSON, toy-language, Ruby-subset, Racc, and Lrama examples.
114
+
115
+ Runtime requires Ruby 3.1 or newer. Generation requires Ruby 3.3 or newer with
116
+ Prism available.
data/CONTRIBUTING.md CHANGED
@@ -22,12 +22,24 @@ bundle exec rake modes:equivalence generated:verify golden:verify
22
22
  bundle exec rake test:differential fuzz
23
23
  ```
24
24
 
25
- The full CI workflow also checks Unicode invariants, acceleration equivalence,
25
+ The CI workflows also check Unicode invariants, acceleration equivalence,
26
26
  generated-only loading, Graphviz output, coverage, and benchmark regressions.
27
27
  Do not update a generated golden file to hide a semantic change; inspect the
28
28
  generated diff and update the compatibility documentation when the change is
29
29
  intentional.
30
30
 
31
+ Golden snapshots under `benchmark/golden/` preserve generated Ruby with the
32
+ compiler version and its derived digest normalized. The comparison includes
33
+ user code, artifact schema, runtime ABI, and Unicode data. Use
34
+ `bundle exec rake golden:update` after reviewing an intentional output change.
35
+ `generated:verify` checks that the committed tokenizer is reproduced exactly.
36
+
37
+ Markdown-only pushes and pull requests run documentation verification without
38
+ the test matrices or other Ruby checks. Changes limited to `site/` or its Pages
39
+ workflow skip Ruby CI and follow the Pages workflow's triggers. Changes that
40
+ also include code or Ruby configuration still run the full Ruby checks. The
41
+ daily scheduled runs retain all validation layers.
42
+
31
43
  ## Documentation changes
32
44
 
33
45
  README is an entry point. Put task-oriented instructions in `docs/how-to/`,
data/README.md CHANGED
@@ -1,10 +1,13 @@
1
1
  # flexr
2
2
 
3
- [![CI](https://github.com/ydah/flexr/actions/workflows/main.yml/badge.svg)](https://github.com/ydah/flexr/actions/workflows/main.yml)
3
+ [![Runtime](https://github.com/ydah/flexr/actions/workflows/runtime.yml/badge.svg)](https://github.com/ydah/flexr/actions/workflows/runtime.yml)
4
4
  [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE.txt)
5
5
 
6
6
  [Product site](https://ydah.github.io/flexr/) · [Playground](https://ydah.github.io/flexr/playground/) · [Documentation](docs/README.md)
7
7
 
8
+ This branch maintains flexr 1.x. See the [support policy](docs/reference/compatibility.md#flexr-1x-maintenance)
9
+ and the [migration guide](docs/migration-v2.md) when planning an upgrade to 2.0.
10
+
8
11
  Ruby-native lexer generator for parser authors who want ordinary Ruby
9
12
  specifications and deterministic generated output.
10
13
 
@@ -22,7 +25,7 @@ use the same rules and actions.
22
25
  ## Quick start
23
26
 
24
27
  ```sh
25
- gem install flexr
28
+ gem install flexr -v '~> 1.0'
26
29
  ```
27
30
 
28
31
  ```ruby
@@ -89,6 +92,7 @@ matches your task:
89
92
  - [Explanation](docs/explanation/) — understand matching, backends, Unicode,
90
93
  generation, and security decisions.
91
94
  - [Examples](examples/) — executable specifications and parser integrations.
95
+ - [Changelog](CHANGELOG.md) — release highlights and upgrade notes.
92
96
 
93
97
  ## Compatibility and stability
94
98
 
data/Rakefile CHANGED
@@ -10,6 +10,7 @@ require "rspec/core/rake_task"
10
10
  require "tmpdir"
11
11
  require "flexr"
12
12
  require_relative "tools/regexp_tokenizer_reference"
13
+ require_relative "tools/golden"
13
14
 
14
15
  RSpec::Core::RakeTask.new(:spec)
15
16
  task test: :spec
@@ -24,6 +25,9 @@ module FlexrVerification
24
25
  %r{/examples/calculator/} => "if ifx == = 12 + 3",
25
26
  %r{/examples/json/} => '{"answer": 42}',
26
27
  %r{/examples/toy_lang/} => "answer + 12",
28
+ %r{/examples/toml/} => 'title = "flexr"',
29
+ %r{/examples/graphql/} => 'query { user(id: 12) { name } }',
30
+ %r{/examples/sql/} => 'select name from users where id >= 12;',
27
31
  %r{/examples/ruby_subset/} => 'class Foo "ok" end',
28
32
  %r{/examples/with_racc/} => "12 + 3",
29
33
  %r{/examples/with_lrama/} => "12 - 3",
@@ -40,6 +44,9 @@ module FlexrVerification
40
44
  %r{/examples/calculator/} => "CalculatorExample::Lexer",
41
45
  %r{/examples/json/} => "JsonExample::Lexer",
42
46
  %r{/examples/toy_lang/} => "ToyLang::Lexer",
47
+ %r{/examples/toml/} => "TomlExample::Lexer",
48
+ %r{/examples/graphql/} => "GraphqlExample::Lexer",
49
+ %r{/examples/sql/} => "SqlExample::Lexer",
43
50
  %r{/examples/ruby_subset/} => "RubySubset::Lexer",
44
51
  %r{/examples/with_racc/} => "WithRacc::RaccLexer",
45
52
  %r{/examples/with_lrama/} => "WithLrama::LramaLexer",
@@ -53,7 +60,7 @@ module FlexrVerification
53
60
  end
54
61
 
55
62
  def golden_path(spec)
56
- name = "#{File.basename(File.dirname(spec))}_#{File.basename(spec, '.flexr.rb')}.sha256"
63
+ name = "#{File.basename(File.dirname(spec))}_#{File.basename(spec, '.flexr.rb')}.rb"
57
64
  File.join(ROOT, "benchmark/golden", name)
58
65
  end
59
66
 
@@ -275,11 +282,30 @@ end
275
282
  task "golden:verify" do
276
283
  FlexrVerification::VERIFICATION_SPECS.each do |spec|
277
284
  golden = FlexrVerification.golden_path(spec)
278
- abort "missing golden file: #{golden}" unless File.file?(golden)
285
+ abort "missing golden snapshot: #{golden}" unless File.file?(golden)
286
+
287
+ expected = FlexrVerification.normalized_source(File.read(golden, encoding: Encoding::UTF_8))
288
+ actual = FlexrVerification::Golden.normalize(FlexrVerification.generated_source(spec))
289
+ next if expected == actual
290
+
291
+ actual_path = File.join(FlexrVerification::ROOT, "tmp/golden", File.basename(golden))
292
+ FileUtils.mkdir_p(File.dirname(actual_path))
293
+ File.binwrite(actual_path, actual)
294
+ begin
295
+ diff, = Open3.capture3("git", "--no-pager", "diff", "--no-index", "--color=never", "--", golden, actual_path)
296
+ warn diff unless diff.empty?
297
+ rescue Errno::ENOENT => e
298
+ warn "git diff unavailable: #{e.message}"
299
+ end
300
+ abort "golden mismatch: #{spec}\ncompare with: git diff --no-index -- #{golden.dump} #{actual_path.dump}"
301
+ end
302
+ end
279
303
 
280
- expected = File.read(golden).strip
281
- actual = Digest::SHA256.hexdigest(FlexrVerification.generated_source(spec))
282
- abort "golden mismatch: #{spec} (expected #{expected}, got #{actual})" unless expected == actual
304
+ task "golden:update" do
305
+ FlexrVerification::VERIFICATION_SPECS.each do |spec|
306
+ golden = FlexrVerification.golden_path(spec)
307
+ FileUtils.mkdir_p(File.dirname(golden))
308
+ File.binwrite(golden, FlexrVerification::Golden.normalize(FlexrVerification.generated_source(spec)))
283
309
  end
284
310
  end
285
311
 
@@ -389,6 +415,16 @@ task "bench:regression" do
389
415
  puts stdout
390
416
  end
391
417
 
418
+ desc "Profile runtime and generated scanning phases with TracePoint"
419
+ task "bench:profile" do
420
+ ruby "benchmark/profile.rb", "--iterations", ENV.fetch("FLEXR_BENCHMARK_ITERATIONS", "1")
421
+ end
422
+
423
+ desc "Measure interpreter, YJIT, and ZJIT where supported by this Ruby"
424
+ task "bench:matrix" do
425
+ ruby "benchmark/matrix.rb", "--iterations", ENV.fetch("FLEXR_BENCHMARK_ITERATIONS", "3")
426
+ end
427
+
392
428
  namespace :test do
393
429
  task :differential do
394
430
  patterns = [/[a-z]+/, /a(?:b|c)?/, /[0-9]{1,3}/, /[^\n]+/, /foo/,
data/Steepfile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ target :public_api do
4
+ signature "sig"
5
+ signature "spec/types"
6
+ check "spec/types/public_api.rb"
7
+ end
@@ -0,0 +1,276 @@
1
+ # frozen_string_literal: true
2
+ # Generated by flexr. DO NOT EDIT.
3
+ # source: examples/calculator/lexer.flexr.rb
4
+ # spec-digest: <normalized>
5
+ # artifact-schema: 1
6
+ # runtime-abi: 1
7
+ # unicode: 15.1.0
8
+ # backend: table
9
+ # compiled: true
10
+ # eval: false
11
+ # standalone: false
12
+
13
+ require "flexr"
14
+
15
+ module CalculatorExample
16
+ class Lexer < Flexr::Lexer
17
+ Flexr::Generated.install_compiled!(self, { artifact: {schema_version: 1, compiler_version: "<compiler-version>", runtime_abi_version: 1, unicode_version: "15.1.0"}, rules: [{ index: 0, patterns: [/[ \t\r\n]+/], pattern_conditions: [[0, 0, false, false]], trailing: nil, action: :skip, states: [:initial], bol_only: false, end_anchor: nil }, { index: 1, patterns: [/==/], pattern_conditions: [[1, 0, false, false]], trailing: nil, action: proc{ emit :EQ }, states: [:initial], bol_only: false, end_anchor: nil }, { index: 2, patterns: [/=/], pattern_conditions: [[2, 0, false, false]], trailing: nil, action: proc{ emit :ASSIGN }, states: [:initial], bol_only: false, end_anchor: nil }, { index: 3, patterns: [/if/], pattern_conditions: [[3, 0, false, false]], trailing: nil, action: proc{ emit :IF }, states: [:initial], bol_only: false, end_anchor: nil }, { index: 4, patterns: [/[a-z_][a-z0-9_]*/], pattern_conditions: [[4, 0, false, false]], trailing: nil, action: proc{ emit :IDENT }, states: [:initial], bol_only: false, end_anchor: nil }, { index: 5, patterns: [/[0-9]+/], pattern_conditions: [[5, 0, false, false]], trailing: nil, action: proc{ emit :INTEGER, text.to_i }, states: [:initial], bol_only: false, end_anchor: nil }, { index: 6, patterns: [/\+/], pattern_conditions: [[6, 0, false, false]], trailing: nil, action: proc{ emit :PLUS }, states: [:initial], bol_only: false, end_anchor: nil }], backend: :table, token_kind: :array, encoding: Encoding::UTF_8, declared_tokens: [:EQ, :ASSIGN, :IF, :IDENT, :INTEGER, :PLUS], options: {}, eof_rules: { }, states: [], inclusive_states: {initial: true}, compiled: { machines: { :initial => { state_name: :initial, dfa: {accepts: [[], [[0, 0, false, false]], [[6, 0, false, false]], [[5, 0, false, false]], [[2, 0, false, false]], [[4, 0, false, false]], [[4, 0, false, false]], [[1, 0, false, false]], [[3, 0, false, false], [4, 0, false, false]]], ec: [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 14, 15, 15, 15, 15, 15, 16, 17, 17, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20], class_count: 21, state_count: 9, start: 0, rule_ids: [0, 1, 2, 3, 4, 5, 6], packed: {base: [0, 1, 0, 1, 1, 11, 22, 0, 33], default: [nil, nil, nil, nil, nil, nil, nil, nil, nil], next: [nil, 1, 1, 1, 1, 1, 1, 2, nil, 3, 3, 4, 7, 5, nil, 5, 5, 5, 6, 5, 5, nil, nil, nil, 5, nil, 5, 5, 5, 5, 5, 5, nil, nil, nil, 5, nil, 5, 8, 5, 5, 5, 5, nil, nil, nil, 5, nil, 5, 5, 5, 5, 5], check: [nil, 0, 1, 0, 1, 0, 1, 0, nil, 0, 3, 0, 4, 0, nil, 0, 0, 0, 0, 0, 5, nil, nil, nil, 5, nil, 5, 5, 5, 5, 5, 6, nil, nil, nil, 6, nil, 6, 6, 6, 6, 6, 8, nil, nil, nil, 8, nil, 8, 8, 8, 8, 8]}} } }, states: [:initial], stats: {initial: {states: 9, classes: 21, accepting_states: 8}}, diagnostics: [] } })
18
+ def scan_one
19
+ if @__flexr_generated_scan_state != @state
20
+ machine = self.class.__flexr_compiled.machines.fetch(@state)
21
+ @__flexr_generated_scan_state = @state
22
+ @__flexr_generated_scan_dfa = machine.dfa
23
+ @__flexr_generated_scan_fast = __flexr_generated_fast_path?(@__flexr_generated_scan_dfa)
24
+ @__flexr_generated_scan_simple = __flexr_generated_simple_fast_path?(@__flexr_generated_scan_dfa)
25
+ end
26
+ dfa = @__flexr_generated_scan_dfa
27
+ return Flexr::Runtime::Interpreter.new(self).scan unless @__flexr_generated_scan_fast
28
+ position = byte_pos
29
+ return nil unless valid_utf8_at?(position)
30
+ return __flexr_generated_scan_simple(dfa, position) if @__flexr_generated_scan_simple
31
+ cursor = position
32
+ current = dfa.start
33
+ direct = dfa.direct
34
+ direct_nxt = direct&.fetch(:nxt)
35
+ direct_classes = direct&.fetch(:classes)
36
+ ec = dfa.ec
37
+ source = buffer.stable_source
38
+ guarded_steps = scan_steps_guarded?
39
+ token_start = more_text_start || position
40
+ token_limit = max_token_size
41
+ check_token_limit = token_limit_required?
42
+ best = nil
43
+ accel_mode = @accel_mode
44
+ accelerate = accel_mode != :none && !(accel_mode == :auto && @__flexr_generated_auto_accel_off)
45
+ while cursor < (source ? source.bytesize : buffer.bytesize) || buffer.ensure_available?(cursor + 1)
46
+ if accelerate
47
+ region = __flexr_generated_acceleration_region(dfa, current)
48
+ acceptance = dfa.accepts[current].first
49
+ token_rule = acceptance&.rule_index
50
+ accelerated_end = __flexr_generated_accelerate(region, cursor, token_rule, position) if region
51
+ if accelerated_end && accelerated_end > cursor
52
+ cursor = accelerated_end
53
+ best = __flexr_generated_acceptance(dfa, current, position, cursor, best)
54
+ next
55
+ end
56
+ end
57
+ byte = source ? source.getbyte(cursor) : buffer.getbyte(cursor)
58
+ consume_step! if guarded_steps
59
+ current = if direct_nxt
60
+ class_id = ec[byte]
61
+ value = direct_nxt[(current * direct_classes) + class_id]
62
+ value >= 0 ? value : nil
63
+ else
64
+ dfa.transition(current, byte)
65
+ end
66
+ break unless current
67
+ cursor += 1
68
+ resource_acceptance = dfa.accepts[current].first
69
+ resource_rule = resource_acceptance&.rule_index
70
+ if check_token_limit
71
+ size = cursor - token_start
72
+ defer_token_size_check!(size, rule: resource_rule) if size > token_limit
73
+ end
74
+ best = __flexr_generated_acceptance(dfa, current, position, cursor, best)
75
+ end
76
+ best
77
+ end
78
+ def __flexr_generated_scan_simple(dfa, position)
79
+ current = dfa.start
80
+ direct = dfa.direct
81
+ direct_nxt = direct&.fetch(:nxt)
82
+ direct_classes = direct&.fetch(:classes)
83
+ ec = dfa.ec
84
+ accepts = dfa.accepts
85
+ rules = self.class.__flexr_rules
86
+ source = buffer.stable_source
87
+ guarded_steps = scan_steps_guarded?
88
+ token_start = more_text_start || position
89
+ token_limit = max_token_size
90
+ check_token_limit = token_limit_required?
91
+ cursor = position
92
+ best = nil
93
+ accel_mode = @accel_mode
94
+ accelerate = accel_mode != :none && !(accel_mode == :auto && @__flexr_generated_auto_accel_off)
95
+ if source && !guarded_steps && !check_token_limit && !accelerate
96
+ while cursor < source.bytesize
97
+ byte = source.getbyte(cursor)
98
+ current = if direct_nxt
99
+ class_id = ec[byte]
100
+ value = direct_nxt[(current * direct_classes) + class_id]
101
+ value >= 0 ? value : nil
102
+ else
103
+ dfa.transition(current, byte)
104
+ end
105
+ break unless current
106
+ cursor += 1
107
+ acceptance = accepts[current].first
108
+ next unless acceptance
109
+ rule = rules.fetch(acceptance.rule_index)
110
+ best ||= (@__flexr_generated_match ||= Flexr::Runtime::Match.new)
111
+ best.rule = rule
112
+ best.start_pos = position
113
+ best.end_pos = cursor
114
+ best.total_end_pos = cursor
115
+ end
116
+ return best
117
+ end
118
+ while cursor < (source ? source.bytesize : buffer.bytesize) || buffer.ensure_available?(cursor + 1)
119
+ if accelerate
120
+ region = __flexr_generated_acceleration_region(dfa, current)
121
+ acceptance = accepts[current].first
122
+ accelerated_end = __flexr_generated_accelerate(region, cursor, acceptance&.rule_index, position) if region
123
+ if accelerated_end && accelerated_end > cursor
124
+ cursor = accelerated_end
125
+ best = __flexr_generated_acceptance(dfa, current, position, cursor, best)
126
+ next
127
+ end
128
+ end
129
+ byte = source ? source.getbyte(cursor) : buffer.getbyte(cursor)
130
+ consume_step! if guarded_steps
131
+ current = if direct_nxt
132
+ class_id = ec[byte]
133
+ value = direct_nxt[(current * direct_classes) + class_id]
134
+ value >= 0 ? value : nil
135
+ else
136
+ dfa.transition(current, byte)
137
+ end
138
+ break unless current
139
+ cursor += 1
140
+ acceptance = accepts[current].first
141
+ if check_token_limit
142
+ size = cursor - token_start
143
+ defer_token_size_check!(size, rule: acceptance&.rule_index) if size > token_limit
144
+ end
145
+ next unless acceptance
146
+ rule = rules.fetch(acceptance.rule_index)
147
+ best ||= (@__flexr_generated_match ||= Flexr::Runtime::Match.new)
148
+ best.rule = rule
149
+ best.start_pos = position
150
+ best.end_pos = cursor
151
+ best.total_end_pos = cursor
152
+ end
153
+ best
154
+ end
155
+ def __flexr_generated_acceleration_region(dfa, state)
156
+ @__flexr_generated_accel_regions ||= {}
157
+ regions = (@__flexr_generated_accel_regions[dfa] ||= Flexr::Automaton::Accel.extract(dfa).to_h { |region| [region.state, region] })
158
+ region = regions[state]
159
+ return unless region
160
+ return if @__flexr_generated_disabled_auto_accel&.key?(region)
161
+ return if utf8_input? && region.bytes.any? { |byte| byte >= 128 }
162
+ return if dfa.accepts[state].any? do |acceptance|
163
+ rule = self.class.__flexr_rules.fetch(acceptance.rule_index)
164
+ acceptance.bol_only || acceptance.end_anchor || rule.trailing
165
+ end
166
+ region
167
+ end
168
+ def __flexr_generated_accelerate(region, position, token_rule, token_start)
169
+ mode = @accel_mode
170
+ cursor = position
171
+ matched = false
172
+ loop do
173
+ if cursor >= buffer.bytesize
174
+ return matched ? cursor : nil if buffer.eof_loaded?
175
+ return matched ? cursor : nil unless buffer.ensure_available?(cursor + 1)
176
+ end
177
+ segment = buffer.source
178
+ segment_position = cursor - buffer.base_offset
179
+ if segment.encoding == ::Encoding::UTF_8 && region.utf8_regexp
180
+ regexp = region.utf8_regexp
181
+ else
182
+ segment = segment.b unless segment.encoding == ::Encoding::BINARY
183
+ regexp = region.regexp
184
+ end
185
+ length = begin
186
+ if %i[strscan auto].include?(mode) && defined?(::StringScanner)
187
+ @__flexr_generated_accel_scanner ||= ::StringScanner.new("".b)
188
+ @__flexr_generated_accel_scanner.string = segment
189
+ @__flexr_generated_accel_scanner.pos = segment_position
190
+ @__flexr_generated_accel_scanner.skip(regexp)
191
+ else
192
+ match = regexp.match(segment.byteslice(segment_position..).to_s, 0)
193
+ match && match.begin(0).zero? ? match[0].bytesize : nil
194
+ end
195
+ rescue ArgumentError
196
+ nil
197
+ end
198
+ __flexr_generated_record_auto_accel_miss(region) if mode == :auto && length.to_i < 8
199
+ return matched ? cursor : nil unless length&.positive?
200
+ matched = true
201
+ cursor += length
202
+ consume_step!(length)
203
+ defer_token_size_check!(cursor - (more_text_start || token_start), rule: token_rule)
204
+ return cursor if cursor < buffer.bytesize || buffer.eof_loaded?
205
+ end
206
+ end
207
+ def __flexr_generated_record_auto_accel_miss(region)
208
+ @__flexr_generated_auto_accel_misses ||= Hash.new(0)
209
+ @__flexr_generated_auto_accel_misses[region] += 1
210
+ @__flexr_generated_auto_accel_miss_total = @__flexr_generated_auto_accel_miss_total.to_i + 1
211
+ @__flexr_generated_auto_accel_off = true if @__flexr_generated_auto_accel_miss_total >= 9
212
+ return if @__flexr_generated_auto_accel_misses[region] < 3
213
+ @__flexr_generated_disabled_auto_accel ||= {}
214
+ @__flexr_generated_disabled_auto_accel[region] = true
215
+ end
216
+ def __flexr_generated_fast_path?(dfa)
217
+ @__flexr_generated_fast_paths ||= {}
218
+ return @__flexr_generated_fast_paths[dfa] if @__flexr_generated_fast_paths.key?(dfa)
219
+ rules = dfa.rule_ids.map { |rule_index| self.class.__flexr_rules.fetch(rule_index) }
220
+ @__flexr_generated_fast_paths[dfa] = self.class.__flexr_config.backend != :firstmatch &&
221
+ self.class.__flexr_config.options[:allow_empty_match] != true && rules.none?(&:trailing)
222
+ end
223
+ def __flexr_generated_simple_fast_path?(dfa)
224
+ @__flexr_generated_simple_fast_paths ||= {}
225
+ return @__flexr_generated_simple_fast_paths[dfa] if @__flexr_generated_simple_fast_paths.key?(dfa)
226
+ rules = dfa.rule_ids.map { |rule_index| self.class.__flexr_rules.fetch(rule_index) }
227
+ @__flexr_generated_simple_fast_paths[dfa] = rules.all? do |rule|
228
+ rule.pattern_conditions.all? { |condition| condition && !condition.bol_only && !condition.end_anchor }
229
+ end
230
+ end
231
+ def __flexr_generated_acceptance(dfa, state, start_position, cursor, best)
232
+ dfa.accepts[state].each do |acceptance|
233
+ next if acceptance.bol_only && !beginning_of_line?
234
+ next if acceptance.end_anchor && !(buffer.eof?(cursor) || buffer.getbyte(cursor) == 0x0a)
235
+ rule = self.class.__flexr_rules.fetch(acceptance.rule_index)
236
+ next if best && cursor < best.total_end_pos
237
+ next if best && cursor == best.total_end_pos &&
238
+ rule.index > best.rule.index
239
+ best ||= (@__flexr_generated_match ||= Flexr::Runtime::Match.new)
240
+ best.rule = rule
241
+ best.start_pos = start_position
242
+ best.end_pos = cursor
243
+ best.total_end_pos = cursor
244
+ end
245
+ best
246
+ end
247
+ def __flexr_generated_execute(rule)
248
+ case rule.index
249
+ when 0
250
+ when 1
251
+ instance_exec(&self.class.__flexr_generated_actions.fetch(rule.index))
252
+ when 2
253
+ instance_exec(&self.class.__flexr_generated_actions.fetch(rule.index))
254
+ when 3
255
+ instance_exec(&self.class.__flexr_generated_actions.fetch(rule.index))
256
+ when 4
257
+ instance_exec(&self.class.__flexr_generated_actions.fetch(rule.index))
258
+ when 5
259
+ instance_exec(&self.class.__flexr_generated_actions.fetch(rule.index))
260
+ when 6
261
+ instance_exec(&self.class.__flexr_generated_actions.fetch(rule.index))
262
+ else
263
+ instance_exec(&rule.action)
264
+ end
265
+ end
266
+
267
+
268
+
269
+ __flexr_bind_generated_action(1, proc{ emit :EQ })
270
+ __flexr_bind_generated_action(2, proc{ emit :ASSIGN })
271
+ __flexr_bind_generated_action(3, proc{ emit :IF })
272
+ __flexr_bind_generated_action(4, proc{ emit :IDENT })
273
+ __flexr_bind_generated_action(5, proc{ emit :INTEGER, text.to_i })
274
+ __flexr_bind_generated_action(6, proc{ emit :PLUS })
275
+ end
276
+ end