flexr 1.2.0 → 2.0.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 (82) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -1
  3. data/CHANGELOG.md +26 -26
  4. data/CONTRIBUTING.md +38 -7
  5. data/README.md +4 -7
  6. data/Rakefile +150 -63
  7. data/benchmark/baselines/json.json +2 -2
  8. data/benchmark/golden/calculator_lexer.rb +15 -15
  9. data/benchmark/golden/graphql_lexer.rb +3 -3
  10. data/benchmark/golden/json_lexer.rb +25 -25
  11. data/benchmark/golden/regexp_tokenizer.rb +23 -23
  12. data/benchmark/golden/ruby_subset_lexer.rb +3 -3
  13. data/benchmark/golden/sql_lexer.rb +3 -3
  14. data/benchmark/golden/toml_lexer.rb +3 -3
  15. data/benchmark/golden/toy_lang_lexer.rb +9 -9
  16. data/benchmark/golden/with_lrama_lexer.rb +3 -3
  17. data/benchmark/golden/with_racc_lexer.rb +3 -3
  18. data/docs/RELEASING.md +1 -7
  19. data/docs/explanation/backends.md +17 -0
  20. data/docs/how-to/deploy-a-standalone-lexer.md +1 -1
  21. data/docs/how-to/tune-performance.md +12 -0
  22. data/docs/migration-v2.md +3 -2
  23. data/docs/perf-log.md +88 -0
  24. data/docs/reference/cli.md +1 -1
  25. data/docs/reference/compatibility.md +7 -8
  26. data/docs/reference/dsl.md +4 -0
  27. data/docs/reference/generated-artifacts.md +31 -4
  28. data/docs/reference/public-api.md +5 -4
  29. data/docs/reference/runtime.md +2 -2
  30. data/docs/reference/tokens-and-locations.md +8 -0
  31. data/examples/json/lexer.flexr.rb +6 -6
  32. data/lib/flexr/automaton/accel.rb +58 -0
  33. data/lib/flexr/automaton/compiler.rb +4 -1
  34. data/lib/flexr/automaton/types.rb +1 -1
  35. data/lib/flexr/codegen/table.rb +6 -0
  36. data/lib/flexr/dsl.rb +2 -1
  37. data/lib/flexr/generated.rb +43 -7
  38. data/lib/flexr/generator.rb +21 -8
  39. data/lib/flexr/minitest.rb +4 -4
  40. data/lib/flexr/regexp/tokenizer.rb +24 -24
  41. data/lib/flexr/runtime/core.rb +28 -4
  42. data/lib/flexr/runtime/interpreter.rb +8 -1
  43. data/lib/flexr/runtime/location.rb +35 -13
  44. data/lib/flexr/runtime/record.rb +14 -0
  45. data/lib/flexr/runtime/token.rb +7 -2
  46. data/lib/flexr/runtime.rb +3 -2
  47. data/lib/flexr/source/prism_reader.rb +8 -2
  48. data/lib/flexr/unicode/property.rb +1 -2
  49. data/lib/flexr/unicode/reference_regexp.rb +1 -2
  50. data/lib/flexr/version.rb +1 -1
  51. data/sig/flexr.rbs +18 -12
  52. data/site/README.md +54 -3
  53. data/site/astro.config.mjs +3 -1
  54. data/site/package.json +12 -5
  55. data/site/playwright.config.mjs +16 -0
  56. data/site/pnpm-lock.yaml +218 -88
  57. data/site/ruby/playground.rb +57 -0
  58. data/site/scripts/prepare-playground.mjs +35 -0
  59. data/site/scripts/verify-site.mjs +5 -3
  60. data/site/src/content/docs/concepts/matching-semantics.md +4 -2
  61. data/site/src/content/docs/concepts/security-model.md +9 -1
  62. data/site/src/content/docs/learn/generation.md +1 -1
  63. data/site/src/content/docs/reference/dsl.md +2 -2
  64. data/site/src/content/docs/reference/public-api.md +4 -2
  65. data/site/src/content/docs/reference/tokens-and-locations.md +3 -0
  66. data/site/src/layouts/SiteLayout.astro +3 -1
  67. data/site/src/pages/playground.astro +40 -38
  68. data/site/src/playground/client.js +211 -0
  69. data/site/src/playground/graph-worker.mjs +11 -0
  70. data/site/src/playground/presets.mjs +33 -0
  71. data/site/src/playground/runtime.mjs +34 -0
  72. data/site/src/playground/share.mjs +30 -0
  73. data/site/src/playground/worker.mjs +29 -0
  74. data/site/src/styles/custom.css +8 -0
  75. data/site/tests/browser/playground.spec.mjs +99 -0
  76. data/site/tests/playground-runtime.test.mjs +85 -0
  77. data/tools/docs_verify.rb +2 -2
  78. data/tools/verification_failure.rb +119 -0
  79. metadata +17 -7
  80. data/lib/flexr/deprecations.rb +0 -26
  81. data/site/public/playground.js +0 -189
  82. data/tools/release_notes.rb +0 -16
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
  # Generated by flexr. DO NOT EDIT.
3
3
  # source: lib/flexr/regexp/tokenizer.flexr.rb
4
- # spec-digest: sha256:998e6b294eb91830713c1d9916cb09fb0fe061feed68c505a0906358f0a039bf
5
- # artifact-schema: 1
6
- # runtime-abi: 1
4
+ # spec-digest: sha256:a9c3e840a6b33c0cb45b25200a372b0ce4f578497f96cc58a591ccd751954f16
5
+ # artifact-schema: 2
6
+ # runtime-abi: 2
7
7
  # unicode: 15.1.0
8
8
  # backend: table
9
9
  # compiled: true
@@ -17,7 +17,7 @@ module Flexr
17
17
  # This specification is intentionally small: it is a dogfood target for
18
18
  # the source lexer path, not the parser's implementation.
19
19
  class SourceLexer < Flexr::Lexer
20
- Flexr::Generated.install_compiled!(self, { artifact: {schema_version: 1, compiler_version: "1.2.0", 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: [/\\p\{[A-Za-z_][A-Za-z0-9_]*\}/], pattern_conditions: [[1, 0, false, false]], trailing: nil, action: proc{ emit :PROPERTY, text.byteslice(3...-1) }, states: [:initial], bol_only: false, end_anchor: nil }, { index: 2, patterns: [/\\./], pattern_conditions: [[2, 0, false, false]], trailing: nil, action: proc{ emit :ESCAPE, text }, states: [:initial], bol_only: false, end_anchor: nil }, { index: 3, patterns: [/\[[^\]\n]*\]/], pattern_conditions: [[3, 0, false, false]], trailing: nil, action: proc{ emit :CHAR_CLASS, text }, states: [:initial], bol_only: false, end_anchor: nil }, { index: 4, patterns: [/[?*+]|\{[0-9]+(?:,[0-9]*)?\}/], pattern_conditions: [[4, 0, false, false]], trailing: nil, action: proc{ emit :QUANTIFIER, text }, states: [:initial], bol_only: false, end_anchor: nil }, { index: 5, patterns: [/\|/], pattern_conditions: [[5, 0, false, false]], trailing: nil, action: proc{ emit :ALTERNATION, text }, states: [:initial], bol_only: false, end_anchor: nil }, { index: 6, patterns: [/\(/], pattern_conditions: [[6, 0, false, false]], trailing: nil, action: proc{ emit :GROUP_OPEN, text }, states: [:initial], bol_only: false, end_anchor: nil }, { index: 7, patterns: [/\)/], pattern_conditions: [[7, 0, false, false]], trailing: nil, action: proc{ emit :GROUP_CLOSE, text }, states: [:initial], bol_only: false, end_anchor: nil }, { index: 8, patterns: [/\^|\$/], pattern_conditions: [[8, 0, false, false]], trailing: nil, action: proc{ emit :ANCHOR, text }, states: [:initial], bol_only: false, end_anchor: nil }, { index: 9, patterns: [/\./], pattern_conditions: [[9, 0, false, false]], trailing: nil, action: proc{ emit :DOT, text }, states: [:initial], bol_only: false, end_anchor: nil }, { index: 10, patterns: [/[^\\\[\]().|?*+{}^$ \t\r\n]/], pattern_conditions: [[10, 0, false, false]], trailing: nil, action: proc{ emit :LITERAL, text }, states: [:initial], bol_only: false, end_anchor: nil }], backend: :table, token_kind: :array, encoding: Encoding::BINARY, declared_tokens: [:PROPERTY, :ESCAPE, :CHAR_CLASS, :QUANTIFIER, :ALTERNATION, :GROUP_OPEN, :GROUP_CLOSE, :ANCHOR, :DOT, :LITERAL], options: {}, eof_rules: { }, states: [], inclusive_states: {initial: true}, compiled: { machines: { :initial => { state_name: :initial, dfa: {accepts: [[], [[10, 0, false, false]], [[0, 0, false, false]], [[8, 0, false, false]], [[6, 0, false, false]], [[7, 0, false, false]], [[4, 0, false, false]], [[9, 0, false, false]], [], [], [], [[5, 0, false, false]], [[3, 0, false, false]], [[2, 0, false, false]], [[2, 0, false, false]], [], [], [], [], [[1, 0, false, false]]], ec: [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 7, 7, 7, 8, 9, 9, 9, 10, 11, 12, 12, 13, 14, 15, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 19, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 23, 24, 25, 26, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 29, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 31, 32, 33, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34], class_count: 35, state_count: 20, start: 0, rule_ids: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], packed: {base: [0, 0, 12, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 3, 22, 15, 4, 21, 0], default: [1, nil, nil, nil, nil, nil, nil, nil, 8, 13, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil], next: [nil, 2, 2, nil, 2, nil, 2, nil, 3, nil, 4, 5, 6, 2, 2, 7, 2, 15, 2, 6, nil, 17, 8, 9, nil, 3, nil, 12, nil, nil, 14, 10, 11, nil, 16, 17, 18, 6, 18, 15, nil, 18, 18, 18, 18, 18, nil, 18, nil, 18, 18, 18, nil, nil, 19, 6], check: [nil, 0, 0, 9, 0, 8, 0, nil, 0, nil, 0, 0, 0, 2, 2, 0, 2, 10, 2, 0, nil, 17, 0, 0, 0, 0, nil, 8, nil, nil, 9, 0, 0, 0, 14, 15, 16, 17, 18, 15, nil, 16, 18, 16, 16, 16, nil, 18, nil, 18, 18, 18, nil, nil, 18, 15]}} } }, states: [:initial], stats: {initial: {states: 20, classes: 35, accepting_states: 12}}, diagnostics: [] } })
20
+ Flexr::Generated.install_compiled!(self, { artifact: {schema_version: 2, compiler_version: "2.0.0", runtime_abi_version: 2, 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: [/\\p\{[A-Za-z_][A-Za-z0-9_]*\}/], pattern_conditions: [[1, 0, false, false]], trailing: nil, action: :__flexr_generated_method, states: [:initial], bol_only: false, end_anchor: nil }, { index: 2, patterns: [/\\./], pattern_conditions: [[2, 0, false, false]], trailing: nil, action: :__flexr_generated_method, states: [:initial], bol_only: false, end_anchor: nil }, { index: 3, patterns: [/\[[^\]\n]*\]/], pattern_conditions: [[3, 0, false, false]], trailing: nil, action: :__flexr_generated_method, states: [:initial], bol_only: false, end_anchor: nil }, { index: 4, patterns: [/[?*+]|\{[0-9]+(?:,[0-9]*)?\}/], pattern_conditions: [[4, 0, false, false]], trailing: nil, action: :__flexr_generated_method, states: [:initial], bol_only: false, end_anchor: nil }, { index: 5, patterns: [/\|/], pattern_conditions: [[5, 0, false, false]], trailing: nil, action: :__flexr_generated_method, states: [:initial], bol_only: false, end_anchor: nil }, { index: 6, patterns: [/\(/], pattern_conditions: [[6, 0, false, false]], trailing: nil, action: :__flexr_generated_method, states: [:initial], bol_only: false, end_anchor: nil }, { index: 7, patterns: [/\)/], pattern_conditions: [[7, 0, false, false]], trailing: nil, action: :__flexr_generated_method, states: [:initial], bol_only: false, end_anchor: nil }, { index: 8, patterns: [/\^|\$/], pattern_conditions: [[8, 0, false, false]], trailing: nil, action: :__flexr_generated_method, states: [:initial], bol_only: false, end_anchor: nil }, { index: 9, patterns: [/\./], pattern_conditions: [[9, 0, false, false]], trailing: nil, action: :__flexr_generated_method, states: [:initial], bol_only: false, end_anchor: nil }, { index: 10, patterns: [/[^\\\[\]().|?*+{}^$ \t\r\n]/], pattern_conditions: [[10, 0, false, false]], trailing: nil, action: :__flexr_generated_method, states: [:initial], bol_only: false, end_anchor: nil }], backend: :table, token_kind: :array, encoding: Encoding::BINARY, declared_tokens: [:PROPERTY, :ESCAPE, :CHAR_CLASS, :QUANTIFIER, :ALTERNATION, :GROUP_OPEN, :GROUP_CLOSE, :ANCHOR, :DOT, :LITERAL], options: {}, eof_rules: { }, states: [], inclusive_states: {initial: true}, compiled: { machines: { :initial => { state_name: :initial, dfa: {accepts: [[], [[10, 0, false, false]], [[0, 0, false, false]], [[8, 0, false, false]], [[6, 0, false, false]], [[7, 0, false, false]], [[4, 0, false, false]], [[9, 0, false, false]], [], [], [], [[5, 0, false, false]], [[3, 0, false, false]], [[2, 0, false, false]], [[2, 0, false, false]], [], [], [], [], [[1, 0, false, false]]], ec: [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 7, 7, 7, 8, 9, 9, 9, 10, 11, 12, 12, 13, 14, 15, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 19, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 23, 24, 25, 26, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 29, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 31, 32, 33, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34], class_count: 35, state_count: 20, start: 0, rule_ids: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], packed: {base: [0, 0, 12, 0, 0, 0, 0, 0, 3, 1, 0, 0, 0, 0, 3, 22, 15, 4, 21, 0], default: [1, nil, nil, nil, nil, nil, nil, nil, 8, 13, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil], next: [nil, 2, 2, nil, 2, nil, 2, nil, 3, nil, 4, 5, 6, 2, 2, 7, 2, 15, 2, 6, nil, 17, 8, 9, nil, 3, nil, 12, nil, nil, 14, 10, 11, nil, 16, 17, 18, 6, 18, 15, nil, 18, 18, 18, 18, 18, nil, 18, nil, 18, 18, 18, nil, nil, 19, 6], check: [nil, 0, 0, 9, 0, 8, 0, nil, 0, nil, 0, 0, 0, 2, 2, 0, 2, 10, 2, 0, nil, 17, 0, 0, 0, 0, nil, 8, nil, nil, 9, 0, 0, 0, 14, 15, 16, 17, 18, 15, nil, 16, 18, 16, 16, 16, nil, 18, nil, 18, 18, 18, nil, nil, 18, 15]}} } }, states: [:initial], stats: {initial: {states: 20, classes: 35, accepting_states: 12}}, diagnostics: [] } })
21
21
  def scan_one
22
22
  if @__flexr_generated_scan_state != @state
23
23
  machine = self.class.__flexr_compiled.machines.fetch(@state)
@@ -251,25 +251,25 @@ module Flexr
251
251
  case rule.index
252
252
  when 0
253
253
  when 1
254
- instance_exec(&self.class.__flexr_generated_actions.fetch(rule.index))
254
+ emit :PROPERTY, text.byteslice(3...-1)
255
255
  when 2
256
- instance_exec(&self.class.__flexr_generated_actions.fetch(rule.index))
256
+ emit :ESCAPE, text
257
257
  when 3
258
- instance_exec(&self.class.__flexr_generated_actions.fetch(rule.index))
258
+ emit :CHAR_CLASS, text
259
259
  when 4
260
- instance_exec(&self.class.__flexr_generated_actions.fetch(rule.index))
260
+ emit :QUANTIFIER, text
261
261
  when 5
262
- instance_exec(&self.class.__flexr_generated_actions.fetch(rule.index))
262
+ emit :ALTERNATION, text
263
263
  when 6
264
- instance_exec(&self.class.__flexr_generated_actions.fetch(rule.index))
264
+ emit :GROUP_OPEN, text
265
265
  when 7
266
- instance_exec(&self.class.__flexr_generated_actions.fetch(rule.index))
266
+ emit :GROUP_CLOSE, text
267
267
  when 8
268
- instance_exec(&self.class.__flexr_generated_actions.fetch(rule.index))
268
+ emit :ANCHOR, text
269
269
  when 9
270
- instance_exec(&self.class.__flexr_generated_actions.fetch(rule.index))
270
+ emit :DOT, text
271
271
  when 10
272
- instance_exec(&self.class.__flexr_generated_actions.fetch(rule.index))
272
+ emit :LITERAL, text
273
273
  else
274
274
  instance_exec(&rule.action)
275
275
  end
@@ -278,16 +278,16 @@ module Flexr
278
278
 
279
279
 
280
280
 
281
- __flexr_bind_generated_action(1, proc{ emit :PROPERTY, text.byteslice(3...-1) })
282
- __flexr_bind_generated_action(2, proc{ emit :ESCAPE, text })
283
- __flexr_bind_generated_action(3, proc{ emit :CHAR_CLASS, text })
284
- __flexr_bind_generated_action(4, proc{ emit :QUANTIFIER, text })
285
- __flexr_bind_generated_action(5, proc{ emit :ALTERNATION, text })
286
- __flexr_bind_generated_action(6, proc{ emit :GROUP_OPEN, text })
287
- __flexr_bind_generated_action(7, proc{ emit :GROUP_CLOSE, text })
288
- __flexr_bind_generated_action(8, proc{ emit :ANCHOR, text })
289
- __flexr_bind_generated_action(9, proc{ emit :DOT, text })
290
- __flexr_bind_generated_action(10, proc{ emit :LITERAL, text })
281
+
282
+
283
+
284
+
285
+
286
+
287
+
288
+
289
+
290
+
291
291
  end
292
292
  end
293
293
  end
@@ -6,8 +6,7 @@ module Flexr
6
6
  max_lookahead_size: nil, max_buffer_size: 64 * 1024 * 1024,
7
7
  max_state_stack: 1024, max_steps: nil, cancellation: nil, retain_input: true,
8
8
  chunk_size: Runtime::Buffer::DEFAULT_CHUNK_SIZE)
9
- Deprecations.warn("unknown error_mode #{error_mode.inspect} will raise ArgumentError in 2.0") unless
10
- %i[raise token panic].include?(error_mode)
9
+ raise ArgumentError, "unsupported error_mode: #{error_mode.inspect}" unless %i[raise token panic].include?(error_mode)
11
10
  raise ArgumentError, "max_token_size must be non-negative" if max_token_size.to_i.negative?
12
11
  raise ArgumentError, "max_lookahead_size must be non-negative" if max_lookahead_size&.to_i&.negative?
13
12
  raise ArgumentError, "max_buffer_size must be non-negative" if max_buffer_size.to_i.negative?
@@ -178,7 +177,7 @@ module Flexr
178
177
  def text
179
178
  return @matched if @matched
180
179
 
181
- @matched = @buffer.byteslice(@text_start...@match_end)
180
+ @matched = @buffer.byteslice(@text_start, @match_end - @text_start)
182
181
  end
183
182
 
184
183
  def text_bytesize
@@ -235,6 +234,31 @@ module Flexr
235
234
  @token_limit_required
236
235
  end
237
236
 
237
+ def __flexr_whole_token_match(plans, position)
238
+ return unless plans && defined?(::StringScanner) && !scan_steps_guarded? && !token_limit_required?
239
+
240
+ source = @buffer.stable_source
241
+ return unless source&.frozen?
242
+
243
+ byte = source.getbyte(position)
244
+ plan = plans[byte] if byte
245
+ return unless plan
246
+
247
+ scanner = @__flexr_whole_scanner ||= ::StringScanner.new(source)
248
+ scanner.pos = position
249
+ length = scanner.skip(plan.regexp)
250
+ return unless length
251
+
252
+ match = @__flexr_whole_match ||= Runtime::Match.new
253
+ match.rule = self.class.__flexr_rules.fetch(plan.rule_index)
254
+ match.start_pos = position
255
+ match.end_pos = position + length
256
+ match.total_end_pos = match.end_pos
257
+ match
258
+ rescue ArgumentError
259
+ nil
260
+ end
261
+
238
262
  def utf8_input?
239
263
  self.class.__flexr_config.encoding == Encoding::UTF_8
240
264
  end
@@ -539,7 +563,7 @@ module Flexr
539
563
  def location_value(starting, ending)
540
564
  return @matched if @matched && starting == @text_start && ending == @match_end
541
565
 
542
- @buffer.byteslice(starting...ending).to_s
566
+ @buffer.byteslice(starting, ending - starting).to_s
543
567
  end
544
568
 
545
569
  def discard_consumed_input!
@@ -24,7 +24,14 @@ module Flexr
24
24
  position = @lexer.byte_pos
25
25
  return nil unless @lexer.valid_utf8_at?(position)
26
26
  return scan_firstmatch(position) if @config.backend == :firstmatch
27
- return scan_fast(machine, position) if fast_path?(machine)
27
+ if fast_path?(machine)
28
+ if machine.whole_tokens
29
+ match = @lexer.__flexr_whole_token_match(machine.whole_tokens, position)
30
+ return match if match
31
+ end
32
+
33
+ return scan_fast(machine, position)
34
+ end
28
35
 
29
36
  buffer = @lexer.buffer
30
37
  state = machine.dfa.start
@@ -2,26 +2,48 @@
2
2
 
3
3
  module Flexr
4
4
  module Runtime
5
- Location = Struct.new(
6
- :filename, :byte_begin, :byte_end, :line_begin, :column_begin, :line_end, :column_end,
7
- keyword_init: true
8
- ) do
9
- def initialize(**values)
10
- @column_values = values.delete(:column_values)
11
- eager_columns = values.delete(:eager_columns)
12
- super(**values) # rubocop:disable Style/SuperArguments
13
- Struct.instance_method(:[]=).bind_call(self, :column_begin, @column_values.first) if eager_columns && @column_values
14
- Struct.instance_method(:[]=).bind_call(self, :column_end, @column_values.last) if eager_columns && @column_values
5
+ Location = Data.define(:filename, :byte_begin, :byte_end, :line_begin, :column_begin, :line_end, :column_end) do
6
+ include Record
7
+
8
+ def initialize(filename: nil, byte_begin: nil, byte_end: nil, line_begin: nil, column_begin: nil,
9
+ line_end: nil, column_end: nil, column_values: nil, eager_columns: false)
10
+ @column_values = [column_begin || column_values&.first, column_end || column_values&.last].freeze
11
+ filename = filename.dup.freeze if filename.is_a?(String)
12
+ column_begin ||= @column_values&.first if eager_columns
13
+ column_end ||= @column_values&.last if eager_columns
14
+ super(filename: filename, byte_begin: byte_begin, byte_end: byte_end, line_begin: line_begin,
15
+ column_begin: column_begin, line_end: line_end, column_end: column_end)
15
16
  end
16
17
 
17
18
  def column_begin
18
- self[:column_begin] || Struct.instance_method(:[]=).bind_call(self, :column_begin, @column_values&.first)
19
+ @column_values.first
19
20
  end
20
21
 
21
22
  def column_end
22
- self[:column_end] || Struct.instance_method(:[]=).bind_call(self, :column_end, @column_values&.last)
23
+ @column_values.last
24
+ end
25
+
26
+ def to_h(&block)
27
+ values = super().merge(column_begin: column_begin, column_end: column_end)
28
+ block ? values.to_h(&block) : values
29
+ end
30
+
31
+ def with(**changes)
32
+ columns = @column_values.dup
33
+ columns[0] = changes[:column_begin] if changes.key?(:column_begin)
34
+ columns[1] = changes[:column_end] if changes.key?(:column_end)
35
+ self.class.new(**deconstruct_keys(nil), **changes, column_values: columns)
36
+ end
37
+
38
+ def ==(other)
39
+ other.instance_of?(self.class) && to_h == other.to_h
40
+ end
41
+
42
+ alias_method :eql?, :==
43
+
44
+ def hash
45
+ [self.class, to_h].hash
23
46
  end
24
47
  end
25
- Deprecations.record_mutations(Location)
26
48
  end
27
49
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Flexr
4
+ module Runtime
5
+ module Record
6
+ def [](key)
7
+ member = key.is_a?(Integer) ? members.fetch(key) : key.to_sym
8
+ raise NameError, "unknown record member #{key.inspect}" unless members.include?(member)
9
+
10
+ public_send(member)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -2,7 +2,12 @@
2
2
 
3
3
  module Flexr
4
4
  module Runtime
5
- Token = Struct.new(:type, :value, :location, keyword_init: true)
6
- Deprecations.record_mutations(Token)
5
+ Token = Data.define(:type, :value, :location) do
6
+ include Record
7
+
8
+ def initialize(type: nil, value: nil, location: nil)
9
+ super
10
+ end
11
+ end
7
12
  end
8
13
  end
data/lib/flexr/runtime.rb CHANGED
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ raise LoadError, "flexr 2.0 requires Ruby 3.4 or newer" if RUBY_VERSION < "3.4"
4
+
3
5
  require "json"
4
6
  require "monitor"
5
7
  require_relative "version"
6
8
  require_relative "errors"
7
9
  require_relative "diagnostics"
8
- require_relative "deprecations"
9
10
  require_relative "action_resolver"
10
11
  require_relative "configuration"
11
12
  require_relative "ir"
@@ -30,6 +31,7 @@ require_relative "automaton/analysis"
30
31
  require_relative "automaton/minimizer"
31
32
  require_relative "automaton/backend_cost_model"
32
33
  require_relative "automaton/accel"
34
+ require_relative "runtime/record"
33
35
  require_relative "runtime/location"
34
36
  require_relative "runtime/token"
35
37
  require_relative "runtime/buffer"
@@ -40,7 +42,6 @@ require_relative "dsl"
40
42
  require_relative "lexer"
41
43
  require_relative "generated"
42
44
 
43
- Flexr::Deprecations.warn("Ruby versions below 3.4 will not be supported in 2.0") if RUBY_VERSION < "3.4"
44
45
 
45
46
  module Flexr
46
47
  class << self
@@ -4,7 +4,7 @@ module Flexr
4
4
  module Source
5
5
  RuleDefinition = Struct.new(
6
6
  :index, :patterns, :trailing, :action_source, :action, :states, :bol_only, :end_anchor,
7
- :pattern_conditions, :bind_action,
7
+ :pattern_conditions, :bind_action, :reads_locals,
8
8
  :span, keyword_init: true
9
9
  )
10
10
  SpecSource = Struct.new(
@@ -278,6 +278,12 @@ module Flexr
278
278
  skip: skip, emit: emit, block: block_action, default: "proc { emit(nil, text) }"
279
279
  )
280
280
  bind_action = !block_action.nil? && action.equal?(block_action)
281
+ reads_locals = false
282
+ if node.block
283
+ each_node_including_self(node.block) do |child|
284
+ reads_locals = true if child.is_a?(Prism::LocalVariableReadNode)
285
+ end
286
+ end
281
287
  rule_index = @rules.length
282
288
  record_rule_edit(node, rule_index, bind_action ? block_action : nil)
283
289
  @rules << RuleDefinition.new(
@@ -285,7 +291,7 @@ module Flexr
285
291
  trailing: options[:followed_by] && static(options[:followed_by], scope: @lexer_scope),
286
292
  action_source: action_source, action: action, states: states.dup,
287
293
  bol_only: false, end_anchor: nil,
288
- span: [node.location.start_offset, node.location.end_offset], bind_action: bind_action
294
+ span: [node.location.start_offset, node.location.end_offset], bind_action: bind_action, reads_locals: reads_locals
289
295
  )
290
296
  end
291
297
 
@@ -13,8 +13,7 @@ module Flexr
13
13
  "Lower" => "Lowercase", "Lowercase" => "Lowercase",
14
14
  "Upper" => "Uppercase", "Uppercase" => "Uppercase"
15
15
  }.freeze
16
- # rubocop:disable-next Style/MutableConstant
17
- CACHE = {}
16
+ CACHE = {} # rubocop:disable Style/MutableConstant
18
17
  module_function
19
18
 
20
19
  def ranges(name, negate: false)
@@ -3,8 +3,7 @@
3
3
  module Flexr
4
4
  module Unicode
5
5
  module ReferenceRegexp
6
- # rubocop:disable-next Style/MutableConstant
7
- CACHE = {}
6
+ CACHE = {} # rubocop:disable Style/MutableConstant
8
7
  module_function
9
8
 
10
9
  def match(pattern, subject, encoding:, options: 0, unicode: false)
data/lib/flexr/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Flexr
4
- VERSION = "1.2.0"
4
+ VERSION = "2.0.0"
5
5
  end
data/sig/flexr.rbs CHANGED
@@ -85,22 +85,28 @@ module Flexr
85
85
  end
86
86
 
87
87
  module Runtime
88
- class Token < ::Struct[untyped]
88
+ class Token < ::Data
89
89
  def self.new: (?type: untyped, ?value: untyped, ?location: Location?) -> instance
90
- attr_accessor type: untyped
91
- attr_accessor value: untyped
92
- attr_accessor location: Location?
90
+ attr_reader type: untyped
91
+ attr_reader value: untyped
92
+ attr_reader location: Location?
93
+ def []: (:location | "location") -> Location?
94
+ | (Integer | Symbol | String) -> untyped
95
+ def with: (?type: untyped, ?value: untyped, ?location: Location?) -> self
93
96
  end
94
97
 
95
- class Location < ::Struct[untyped]
98
+ class Location < ::Data
96
99
  def self.new: (?filename: String?, ?byte_begin: Integer?, ?byte_end: Integer?, ?line_begin: Integer?, ?column_begin: Integer?, ?line_end: Integer?, ?column_end: Integer?) -> instance
97
- attr_accessor filename: String?
98
- attr_accessor byte_begin: Integer?
99
- attr_accessor byte_end: Integer?
100
- attr_accessor line_begin: Integer?
101
- attr_accessor line_end: Integer?
102
- attr_accessor column_begin: Integer?
103
- attr_accessor column_end: Integer?
100
+ attr_reader filename: String?
101
+ attr_reader byte_begin: Integer?
102
+ attr_reader byte_end: Integer?
103
+ attr_reader line_begin: Integer?
104
+ attr_reader line_end: Integer?
105
+ attr_reader column_begin: Integer?
106
+ attr_reader column_end: Integer?
107
+ def []: (:filename | "filename") -> String?
108
+ | (Integer | Symbol | String) -> (Integer | String)?
109
+ def with: (?filename: String?, ?byte_begin: Integer?, ?byte_end: Integer?, ?line_begin: Integer?, ?column_begin: Integer?, ?line_end: Integer?, ?column_end: Integer?) -> self
104
110
  end
105
111
 
106
112
  class TokenTooLargeError < LexError
data/site/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # flexr product site
2
2
 
3
- This directory contains the Astro/Starlight product site for flexr. It is a static GitHub Pages site with a custom landing page, documentation routes, and a fixture-backed playground preview.
3
+ This directory contains the Astro/Starlight product site for flexr. The Playground
4
+ runs real Ruby and flexr locally through a pinned ruby.wasm runtime.
4
5
 
5
6
  ## Local development
6
7
 
@@ -14,9 +15,59 @@ Run the same checks used by the Pages workflow:
14
15
 
15
16
  ```sh
16
17
  pnpm verify
18
+ pnpm test
17
19
  pnpm build
20
+ pnpm exec playwright install chromium
21
+ pnpm test:browser
18
22
  ```
19
23
 
20
- The current playground does not execute arbitrary Ruby. It demonstrates the matching decision model from fixed fixtures and keeps `--eval` out of the browser. A future Ruby WASM worker must preserve that security boundary and add runtime/generated equivalence tests before the UI claims full execution.
24
+ `pnpm dev`, `pnpm build`, and `pnpm test` package the current repository's Ruby
25
+ files and copy the pinned runtime into ignored `public/playground-runtime` assets.
26
+ The WASM binary and source bundle are not checked into Git. The deployment build
27
+ produces them, including the Ruby distribution's license and notice.
21
28
 
22
- The repository examples, CLI definitions, and Ruby implementation remain the behavioral source of truth. Site copy links to those artifacts where a summary would otherwise drift.
29
+ The first Run lazily creates a worker and downloads the runtime. Every run starts
30
+ a new Ruby VM, loads the specification, compiles with flexr, executes its actions,
31
+ and returns tokens, real diagnostics, DOT, trace steps, and generated Ruby.
32
+ Prism 1.7.0 is available in the pinned Ruby 4.0.0 WASM build, so both runtime and
33
+ generated execution are tested. The static DSL restrictions of `flexr check` and
34
+ the generator apply. Specifications must be self-contained or use the bundled
35
+ Ruby libraries; additional gems and local companion files are unavailable.
36
+ The [ruby.wasm WASI limitations](https://ruby.github.io/ruby.wasm/README_md.html),
37
+ including unsupported thread creation, also apply.
38
+ The original three presets remain editable starting points. TOML, GraphQL, and
39
+ SQL subset presets load the actual repository example sources, packaged at build
40
+ time, so their lexer rules are maintained in one place.
41
+
42
+ The main page stops execution after five seconds; runtime download/startup has a
43
+ 30-second limit. Source and input are limited to 20,000 characters each, DFA
44
+ construction to 1024 states, and output to 4 MB. Graphviz loads in a separate
45
+ worker only when Draw DFA is selected and has its own five-second limit.
46
+
47
+ Source/input are not uploaded by the application. Share links store them in the
48
+ URL fragment, and opening a link never executes it. Review shared Ruby before
49
+ running it. Workers isolate the UI and have a virtual filesystem; they are not a
50
+ complete hostile-code or memory sandbox. The page applies a CSP, disables common
51
+ network APIs in the Ruby worker after loading assets, and sanitizes rendered SVG.
52
+ Ruby actions can still modify their VM and interact with worker JavaScript.
53
+
54
+ ## Size and startup measurements
55
+
56
+ Measured on 2026-11-21, macOS arm64, Headless Chrome 157, using the local static
57
+ preview server without network throttling:
58
+
59
+ | Asset | Uncompressed bytes | gzip bytes |
60
+ |---|---:|---:|
61
+ | Ruby 4.0.0 and standard library | 32,555,374 | 10,114,484 |
62
+ | flexr Ruby files and bridge | 533,463 | 119,954 |
63
+ | TOML, GraphQL, and SQL presets | 2,750 | 838 |
64
+
65
+ One browser run measured 43 ms for downloading/compiling the runtime and 203 ms
66
+ for starting the VM and loading flexr. These local numbers do not predict a
67
+ remote cold download. The UI reports measured sizes and timings on every run.
68
+ Initial page loading fetches neither Ruby WASM nor Graphviz; the browser test
69
+ checks that no WASM request occurs before Run or when opening a share link.
70
+
71
+ For local Chrome instead of a Playwright download, set
72
+ `PLAYWRIGHT_CHROMIUM_EXECUTABLE` to its executable path. Screenshots from the
73
+ browser tests are written to ignored `test-results/` directories.
@@ -1,9 +1,11 @@
1
- import { defineConfig } from 'astro/config';
1
+ import { defineConfig, passthroughImageService } from 'astro/config';
2
2
  import starlight from '@astrojs/starlight';
3
3
 
4
4
  export default defineConfig({
5
5
  site: 'https://ydah.github.io',
6
6
  base: '/flexr',
7
+ image: { service: passthroughImageService() },
8
+ vite: { worker: { format: 'es' } },
7
9
  integrations: [
8
10
  starlight({
9
11
  title: 'flexr',
data/site/package.json CHANGED
@@ -3,17 +3,24 @@
3
3
  "private": true,
4
4
  "type": "module",
5
5
  "scripts": {
6
- "dev": "astro dev",
7
- "build": "astro check && astro build",
6
+ "prepare:playground": "node scripts/prepare-playground.mjs",
7
+ "dev": "pnpm prepare:playground && astro dev",
8
+ "build": "pnpm prepare:playground && astro check && astro build",
8
9
  "preview": "astro preview",
9
- "verify": "node scripts/verify-site.mjs"
10
+ "verify": "node scripts/verify-site.mjs",
11
+ "test": "pnpm prepare:playground && node --test tests/*.test.mjs",
12
+ "test:browser": "playwright test"
10
13
  },
11
14
  "devDependencies": {
12
- "@astrojs/check": "^0.9.4",
15
+ "@astrojs/check": "^0.9.12",
16
+ "@playwright/test": "1.56.1",
13
17
  "typescript": "^5.7.3"
14
18
  },
15
19
  "dependencies": {
16
- "@astrojs/starlight": "^0.36.0",
20
+ "@astrojs/starlight": "^0.37.7",
21
+ "@ruby/4.0-wasm-wasi": "2.10.1",
22
+ "@ruby/wasm-wasi": "2.10.1",
23
+ "@viz-js/viz": "3.25.0",
17
24
  "astro": "^5.14.1"
18
25
  }
19
26
  }
@@ -0,0 +1,16 @@
1
+ import { defineConfig } from '@playwright/test';
2
+
3
+ export default defineConfig({
4
+ testDir: './tests/browser',
5
+ workers: 1,
6
+ timeout: 45_000,
7
+ use: {
8
+ baseURL: 'http://127.0.0.1:4322',
9
+ launchOptions: process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE ? { executablePath: process.env.PLAYWRIGHT_CHROMIUM_EXECUTABLE } : {},
10
+ },
11
+ webServer: {
12
+ command: 'pnpm exec astro preview --host 127.0.0.1 --port 4322',
13
+ url: 'http://127.0.0.1:4322/flexr/playground/',
14
+ timeout: 30_000,
15
+ },
16
+ });