ibex-runtime 0.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 (102) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +21 -0
  3. data/README.md +337 -0
  4. data/lib/ibex/runtime/ast_data.rb +23 -0
  5. data/lib/ibex/runtime/cst/annotation.rb +16 -0
  6. data/lib/ibex/runtime/cst/cursor.rb +68 -0
  7. data/lib/ibex/runtime/cst/diff.rb +80 -0
  8. data/lib/ibex/runtime/cst/editing.rb +56 -0
  9. data/lib/ibex/runtime/cst/editor.rb +94 -0
  10. data/lib/ibex/runtime/cst/green/builder.rb +211 -0
  11. data/lib/ibex/runtime/cst/green/cache.rb +150 -0
  12. data/lib/ibex/runtime/cst/green/node.rb +78 -0
  13. data/lib/ibex/runtime/cst/green/token.rb +91 -0
  14. data/lib/ibex/runtime/cst/green/trivia.rb +36 -0
  15. data/lib/ibex/runtime/cst/incremental/blender.rb +233 -0
  16. data/lib/ibex/runtime/cst/incremental/lexed_syntax.rb +28 -0
  17. data/lib/ibex/runtime/cst/incremental/parse_memo.rb +95 -0
  18. data/lib/ibex/runtime/cst/incremental/relexer.rb +134 -0
  19. data/lib/ibex/runtime/cst/incremental/session.rb +229 -0
  20. data/lib/ibex/runtime/cst/incremental/token_memo.rb +66 -0
  21. data/lib/ibex/runtime/cst/kind.rb +133 -0
  22. data/lib/ibex/runtime/cst/parse_result.rb +41 -0
  23. data/lib/ibex/runtime/cst/rewriter.rb +75 -0
  24. data/lib/ibex/runtime/cst/serialize.rb +127 -0
  25. data/lib/ibex/runtime/cst/serialized_tree.rb +44 -0
  26. data/lib/ibex/runtime/cst/source_text.rb +112 -0
  27. data/lib/ibex/runtime/cst/syntax_node.rb +432 -0
  28. data/lib/ibex/runtime/cst/syntax_token.rb +160 -0
  29. data/lib/ibex/runtime/cst/text_edit.rb +77 -0
  30. data/lib/ibex/runtime/cst/typed_node.rb +87 -0
  31. data/lib/ibex/runtime/cst/validator.rb +428 -0
  32. data/lib/ibex/runtime/cst.rb +38 -0
  33. data/lib/ibex/runtime/event.rb +44 -0
  34. data/lib/ibex/runtime/event_jsonl_tracer.rb +37 -0
  35. data/lib/ibex/runtime/event_sanitizer.rb +273 -0
  36. data/lib/ibex/runtime/generated_lexer.rb +514 -0
  37. data/lib/ibex/runtime/jsonl_tracer.rb +75 -0
  38. data/lib/ibex/runtime/lexer_input.rb +107 -0
  39. data/lib/ibex/runtime/location_span.rb +114 -0
  40. data/lib/ibex/runtime/observation.rb +82 -0
  41. data/lib/ibex/runtime/parser.rb +3027 -0
  42. data/lib/ibex/runtime/parser_sync_recovery.rb +108 -0
  43. data/lib/ibex/runtime/repair.rb +126 -0
  44. data/lib/ibex/runtime/repair_priority_queue.rb +60 -0
  45. data/lib/ibex/runtime/repair_search.rb +318 -0
  46. data/lib/ibex/runtime/resource_limits.rb +64 -0
  47. data/lib/ibex/runtime/table_format.rb +11 -0
  48. data/lib/ibex/runtime/version.rb +7 -0
  49. data/lib/ibex/runtime.rb +27 -0
  50. data/lib/ibex/tables/compact.rb +186 -0
  51. data/lib/ibex/tables/compact_actions.rb +135 -0
  52. data/lib/ibex/tables/compact_productions.rb +167 -0
  53. data/sig/ibex/runtime/ast_data.rbs +12 -0
  54. data/sig/ibex/runtime/cst/annotation.rbs +13 -0
  55. data/sig/ibex/runtime/cst/cursor.rbs +34 -0
  56. data/sig/ibex/runtime/cst/diff.rbs +29 -0
  57. data/sig/ibex/runtime/cst/editing.rbs +23 -0
  58. data/sig/ibex/runtime/cst/editor.rbs +43 -0
  59. data/sig/ibex/runtime/cst/green/builder.rbs +84 -0
  60. data/sig/ibex/runtime/cst/green/cache.rbs +60 -0
  61. data/sig/ibex/runtime/cst/green/node.rbs +49 -0
  62. data/sig/ibex/runtime/cst/green/token.rbs +59 -0
  63. data/sig/ibex/runtime/cst/green/trivia.rbs +29 -0
  64. data/sig/ibex/runtime/cst/incremental/blender.rbs +114 -0
  65. data/sig/ibex/runtime/cst/incremental/lexed_syntax.rbs +20 -0
  66. data/sig/ibex/runtime/cst/incremental/parse_memo.rbs +59 -0
  67. data/sig/ibex/runtime/cst/incremental/relexer.rbs +54 -0
  68. data/sig/ibex/runtime/cst/incremental/session.rbs +89 -0
  69. data/sig/ibex/runtime/cst/incremental/token_memo.rbs +31 -0
  70. data/sig/ibex/runtime/cst/kind.rbs +93 -0
  71. data/sig/ibex/runtime/cst/parse_result.rbs +35 -0
  72. data/sig/ibex/runtime/cst/rewriter.rbs +31 -0
  73. data/sig/ibex/runtime/cst/serialize.rbs +39 -0
  74. data/sig/ibex/runtime/cst/serialized_tree.rbs +34 -0
  75. data/sig/ibex/runtime/cst/source_text.rbs +51 -0
  76. data/sig/ibex/runtime/cst/syntax_node.rbs +195 -0
  77. data/sig/ibex/runtime/cst/syntax_token.rbs +112 -0
  78. data/sig/ibex/runtime/cst/text_edit.rbs +33 -0
  79. data/sig/ibex/runtime/cst/typed_node.rbs +50 -0
  80. data/sig/ibex/runtime/cst/validator.rbs +108 -0
  81. data/sig/ibex/runtime/cst.rbs +9 -0
  82. data/sig/ibex/runtime/event.rbs +26 -0
  83. data/sig/ibex/runtime/event_jsonl_tracer.rbs +25 -0
  84. data/sig/ibex/runtime/event_sanitizer.rbs +92 -0
  85. data/sig/ibex/runtime/generated_lexer.rbs +169 -0
  86. data/sig/ibex/runtime/jsonl_tracer.rbs +42 -0
  87. data/sig/ibex/runtime/lexer_input.rbs +44 -0
  88. data/sig/ibex/runtime/location_span.rbs +64 -0
  89. data/sig/ibex/runtime/observation.rbs +35 -0
  90. data/sig/ibex/runtime/parser.rbs +926 -0
  91. data/sig/ibex/runtime/parser_sync_recovery.rbs +60 -0
  92. data/sig/ibex/runtime/repair.rbs +92 -0
  93. data/sig/ibex/runtime/repair_priority_queue.rbs +27 -0
  94. data/sig/ibex/runtime/repair_search.rbs +114 -0
  95. data/sig/ibex/runtime/resource_limits.rbs +39 -0
  96. data/sig/ibex/runtime/table_format.rbs +11 -0
  97. data/sig/ibex/runtime/version.rbs +7 -0
  98. data/sig/ibex/runtime.rbs +6 -0
  99. data/sig/ibex/tables/compact.rbs +70 -0
  100. data/sig/ibex/tables/compact_actions.rbs +57 -0
  101. data/sig/ibex/tables/compact_productions.rbs +68 -0
  102. metadata +144 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 05a9f9e7cb92bb15ef6efbfb632e4d846a3b3f8c4eb2fbccba549001b5e970ab
4
+ data.tar.gz: d8c262daf2e6525c80bf475aaed6500ee28fc054c6ef6628d772ae337b6f6a12
5
+ SHA512:
6
+ metadata.gz: 20e654c2c2bac9452fa538a25918fd16475deb3bbd12a8491312709bc1623f7ac8f42381d3b191b061f244c617ca3b20c91528eda676d9ba3fe3f9af91c9dcc8
7
+ data.tar.gz: f69a58947420d425f9b6f957c680c16661272fb74f6bb9d3805584de74f66edb8ef141ee10c3eea7ab5fa84bb226101db63a14bd9e17f6c3690ed20c6374e46c
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Yudai Takada
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,337 @@
1
+ # Ibex
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/ibex.svg)](https://badge.fury.io/rb/ibex)
4
+ [![CI](https://github.com/ydah/ibex/actions/workflows/main.yml/badge.svg)](https://github.com/ydah/ibex/actions/workflows/main.yml)
5
+
6
+ Ibex is a Pure Ruby LR parser generator. It reads racc-compatible grammar
7
+ files, generates parsers with the familiar `do_parse` / `yyparse` API, and
8
+ requires no C or Java extension.
9
+
10
+ > [!IMPORTANT]
11
+ > Ibex is pre-1.0. The current v1.0 decision is
12
+ > [**HOLD**](docs/release-readiness.md): compatibility and scale evidence pass,
13
+ > but cold generation and new-instance runtime still trail Racc's Ruby backend,
14
+ > and the error UX evidence still needs independent review. Compatible mode is
15
+ > the stable baseline; opt-in features have the maturity levels documented in
16
+ > the [stability policy](docs/stability.md).
17
+
18
+ - Try grammar analysis in the
19
+ [browser playground](https://ydah.github.io/ibex/playground/).
20
+ - Start with the [grammar reference](docs/grammar-reference.md).
21
+ - Build lossless and incremental tooling with the
22
+ [Red/Green CST guide](docs/cst.md).
23
+ - Migrate an existing parser with the
24
+ [racc migration guide](docs/racc-migration.md).
25
+ - Browse the [API reference](https://ydah.github.io/ibex/api/).
26
+
27
+ The playground runs in a local Web Worker. It does not upload grammar source
28
+ or execute semantic action bodies.
29
+
30
+ ## At a glance
31
+
32
+ | Concern | Ibex default |
33
+ | --- | --- |
34
+ | Grammar input | racc-compatible `.y` source |
35
+ | Construction | Direct LALR(1) |
36
+ | Generated tables | Compact, immutable Ruby data |
37
+ | Parser API | Pull parsing with `do_parse` / `yyparse` |
38
+ | Generated dependency | The separately versioned `ibex-runtime` gem |
39
+ | Ruby support | Ruby 3.0 or later |
40
+ | Extensions | Disabled unless `pragma extended` or `--mode=extended` is used |
41
+ | Concrete syntax | Stable format-v6 Red/Green CST with `pragma cst` |
42
+ | Semantic actions | Opaque during generation and analysis; executed only by a running parser |
43
+
44
+ The generator and runtime are deliberately separate:
45
+
46
+ ```text
47
+ grammar.y + lexer contract
48
+ |
49
+ v
50
+ ibex -----> parser.rb --------> ibex-runtime
51
+ |
52
+ +--------> optional RBS, IR, reports, diagrams, and docs
53
+ ```
54
+
55
+ Use `-E` when a generated parser must embed the runtime instead of depending
56
+ on `ibex-runtime`.
57
+
58
+ ## Quick start from a checkout
59
+
60
+ Clone the repository and install its locked development dependencies:
61
+
62
+ ```sh
63
+ git clone https://github.com/ydah/ibex.git
64
+ cd ibex
65
+ bundle install
66
+ ```
67
+
68
+ Save this grammar as `calculator.y`:
69
+
70
+ <!-- calculator-grammar:start -->
71
+ ```text
72
+ class Calculator
73
+ token NUM
74
+ preclow
75
+ left '+'
76
+ left '*'
77
+ prechigh
78
+ rule
79
+ expr : expr '+' expr { result = val[0] + val[2] }
80
+ | expr '*' expr { result = val[0] * val[2] }
81
+ | NUM { result = val[0] }
82
+ end
83
+ ---- inner
84
+ def parse_tokens(tokens)
85
+ @tokens = tokens
86
+ do_parse
87
+ end
88
+
89
+ def next_token
90
+ @tokens.shift
91
+ end
92
+ ---- footer
93
+ if $PROGRAM_NAME == __FILE__
94
+ tokens = [[:NUM, 2], ['+', nil], [:NUM, 3], ['*', nil], [:NUM, 4]]
95
+ puts Calculator.new.parse_tokens(tokens)
96
+ end
97
+ ```
98
+ <!-- calculator-grammar:end -->
99
+
100
+ Generate and run the parser without installing local gems:
101
+
102
+ ```sh
103
+ bundle exec ruby -Ilib exe/ibex calculator.y
104
+ bundle exec ruby -Ilib calculator.rb
105
+ # 14
106
+ ```
107
+
108
+ With the local gems installed, the equivalent commands are:
109
+
110
+ ```sh
111
+ ibex calculator.y
112
+ ruby calculator.rb
113
+ ```
114
+
115
+ Ibex writes `calculator.rb`, uses compact tables, and generates a parser that
116
+ requires `ibex-runtime`. Use `-o PATH` to choose the output, `--table=plain`
117
+ for inspectable Hash rows, `--check` to compare without rewriting, or `-E` to
118
+ embed the runtime.
119
+
120
+ ## Generated parser contract
121
+
122
+ ### Tokens and lexers
123
+
124
+ A handwritten pull lexer implements `next_token` and returns:
125
+
126
+ ```ruby
127
+ [token, value]
128
+ [token, value, location]
129
+ ```
130
+
131
+ `false` or `nil` marks EOF. Bare grammar tokens use Ruby symbols such as
132
+ `:NUM`; quoted terminals use strings such as `'+'`. Locations are
133
+ application-owned objects and remain optional.
134
+
135
+ Existing lexers can use the [lexer migration guide](docs/lexer-migration.md).
136
+ Extended grammars may instead generate a streaming lexer from declarative
137
+ regular-expression rules.
138
+
139
+ ### Semantic actions and locations
140
+
141
+ Actions read RHS values through `val`. `@1`, `@2`, and so on address parallel
142
+ semantic locations, while `@$` is the span of the current reduction. Action
143
+ source remains opaque Ruby: Ibex preserves its source mapping but does not
144
+ parse, type-check, or sandbox it.
145
+
146
+ ### Parse lifecycles and errors
147
+
148
+ Generated parsers support:
149
+
150
+ - `do_parse` and yielding `yyparse` for racc-compatible pull parsing;
151
+ - `push(token, value, location)` and `finish(location:)` for caller-driven
152
+ streaming;
153
+ - structured `Ibex::ParseError` values containing the token, expected tokens,
154
+ state, location, and spelling suggestions;
155
+ - yacc-style recovery through `on_error`, `yyerror`, `yyerrok`, and
156
+ `yyaccept`; and
157
+ - immutable, shareable generated tables.
158
+
159
+ Use one parser instance per concurrent parse. Isolated runtime sessions and
160
+ resource budgets are opt-in when application code needs explicit execution
161
+ boundaries.
162
+
163
+ ## Choose a grammar mode
164
+
165
+ | Mode | Use it for | Contract |
166
+ | --- | --- | --- |
167
+ | `default` | Existing racc grammars and conservative migrations | Compatible syntax and runtime behavior |
168
+ | `extended` | New grammars that need composition or generated tooling | Preview, explicitly enabled |
169
+
170
+ Enable extended syntax with `pragma extended` in the grammar or
171
+ `--mode=extended` on the command line:
172
+
173
+ ```text
174
+ class ExtendedParser
175
+ pragma extended
176
+ token NUM KEY VALUE
177
+ rule
178
+ arguments : separated_list(NUM, ',')
179
+ sum : NUM:left '+' NUM:right { result = left + right }
180
+ maybe : NUM?
181
+ many : NUM*
182
+ groups : (KEY VALUE)+
183
+ list(X) : X | list(X) ',' X
184
+ %inline signed(X): '-' X { result = -val[1] }
185
+ negative : signed(NUM)
186
+ end
187
+ ```
188
+
189
+ Extended mode includes nested EBNF groups, named references, parameterized and
190
+ inline rules, multiple entry points, canonical fragment imports, generated
191
+ lexers, CST or typed `Data` AST generation, declarative recovery, semantic
192
+ types, and grammar-declared tests. These features do not silently change the
193
+ default compatible frontend. Maturity is assigned per API: format-v6 batch
194
+ Red/Green CST is Stable, while the wider extended grammar surface remains
195
+ Preview and incremental CST sessions remain Experimental.
196
+
197
+ ## Choose a construction algorithm
198
+
199
+ | Option | When to use it |
200
+ | --- | --- |
201
+ | `--algorithm=lalr` | Default direct LALR(1) construction |
202
+ | `--algorithm=slr` | Grammars whose FOLLOW-set reductions are sufficient |
203
+ | `--algorithm=ielr` | Preview backend for conflicts introduced by LALR state merging |
204
+ | `--algorithm=lr1` | Canonical LR(1) analysis when the larger automaton is acceptable |
205
+
206
+ `--suggest-ielr` checks whether IELR removes an unexpected LALR conflict and
207
+ prints an advisory note when it does.
208
+ That note does not change the selected algorithm or exit status.
209
+
210
+ Conflict freedom is not the same as a proof of unambiguity. `ibex check
211
+ --ambiguity` searches for a concrete sentence with two interpretations, but
212
+ the token and configuration budgets make every result explicitly bounded.
213
+
214
+ ## Integrate generation into a build
215
+
216
+ Treat grammar source as the input and generated Ruby as a reproducible build
217
+ artifact:
218
+
219
+ ```sh
220
+ ibex --warnings=error -o lib/calculator.rb grammar/calculator.y
221
+ ibex --warnings=error --check -o lib/calculator.rb grammar/calculator.y
222
+ ```
223
+
224
+ The first command writes the parser. The second is suitable for CI and fails
225
+ when the committed output is stale without modifying it.
226
+
227
+ Rake projects can declare the dependency directly:
228
+
229
+ ```ruby
230
+ require "ibex/rake_task"
231
+
232
+ Ibex::RakeTask.new("lib/calculator.rb") do |task|
233
+ task.grammar = "grammar/calculator.y"
234
+ task.options = ["--warnings=error"]
235
+ end
236
+ ```
237
+
238
+ The task follows the grammar's fragment dependency closure. Multi-output
239
+ generation can additionally publish a manifest containing input identities,
240
+ output paths, sizes, and SHA-256 digests. Generation renders all requested
241
+ outputs before publishing them; `--watch` retains the last successful output
242
+ when a later edit is invalid.
243
+
244
+ Applications that only execute generated parsers need `ibex-runtime`, not the
245
+ generator. To build and install both current local gems:
246
+
247
+ ```sh
248
+ gem build ibex-runtime.gemspec
249
+ gem build ibex.gemspec
250
+ gem install ./ibex-runtime-0.1.0.gem
251
+ gem install ./ibex-0.1.0.gem
252
+ ```
253
+
254
+ ## Diagnose, verify, and inspect
255
+
256
+ | Goal | Command |
257
+ | --- | --- |
258
+ | Promote grammar warnings to errors | `ibex --warnings=error -C grammar.y` |
259
+ | Collect structured frontend diagnostics | `ibex diagnose --format=json grammar.y` |
260
+ | Explain one conflict | `ibex explain --state=7 --token=ELSE grammar.y` |
261
+ | Search for a concrete ambiguity | `ibex check --ambiguity --algorithm=lr1 grammar.y` |
262
+ | Run grammar-declared tests | `ibex test --coverage=100 grammar.y` |
263
+ | Check canonical formatting | `ibex fmt --check grammar.y` |
264
+ | Render grammar documentation | `ibex doc --format=html -o grammar.html grammar.y` |
265
+ | Emit Automaton IR | `ibex --emit=automaton-ir grammar.y > automaton.json` |
266
+ | Simulate table behavior without actions | `ibex debug automaton.json NUMBER PLUS NUMBER` |
267
+ | Check a racc migration | `ibex migrate-check grammar.y` |
268
+ | Start the language server | `ibex lsp --stdio` |
269
+
270
+ Grammar IR, Automaton IR, generated table formats, diagnostic JSON, runtime
271
+ events, coverage, and manifests are versioned contracts. Reports can also be
272
+ rendered as text, DOT, Mermaid, HTML, railroad SVG, samples, or conflict
273
+ counterexamples.
274
+
275
+ ## Safety boundaries and non-goals
276
+
277
+ - Frontend parsing, diagnosis, formatting, documentation, LSP, static
278
+ migration checks, IR validation, and table simulation do not execute
279
+ semantic actions or user-code sections.
280
+ - Generated parsers, grammar-declared tests, and explicit migration harnesses
281
+ do execute application Ruby. They are not sandboxes.
282
+ - Fragment imports are confined to the declared source root and reject
283
+ traversal, cycles, and symlink escapes.
284
+ - Counterexample, ambiguity, sample, repair, and runtime-budget searches are
285
+ bounded. Exhausting a budget is distinct from finding no witness within it.
286
+ - Ibex generates deterministic LR parsers. It does not provide GLR or
287
+ generalized ambiguity handling. Incremental CST sessions are syntax-only
288
+ and experimental.
289
+ - racc compatibility is a migration surface, not a claim that every generated
290
+ parser is a byte-for-byte or adapter-free replacement.
291
+ - Preview and experimental features may have weaker compatibility guarantees
292
+ than the default mode.
293
+
294
+ ## Documentation by task
295
+
296
+ | When you need to... | Start here |
297
+ | --- | --- |
298
+ | Write a grammar or use the runtime | [Grammar reference](docs/grammar-reference.md) |
299
+ | Build, edit, serialize, or incrementally update syntax trees | [Red/Green CST guide](docs/cst.md) and [migration guide](docs/cst-migration.md) |
300
+ | Learn from executable grammars | [Examples](examples/README.md) |
301
+ | Migrate from racc | [racc migration guide](docs/racc-migration.md) |
302
+ | Adapt a handwritten lexer | [Lexer migration guide](docs/lexer-migration.md) |
303
+ | Configure an editor or LSP client | [Editor setup](docs/editor-setup.md) |
304
+ | Understand the pipeline, IR, algorithms, or tables | [Architecture](docs/architecture.md) |
305
+ | Check API maturity and deprecation rules | [Stability policy](docs/stability.md) |
306
+ | Evaluate readiness, performance, or error UX | [Release readiness](docs/release-readiness.md), [benchmarks](benchmark/README.md), and [error UX](docs/error-ux.md) |
307
+ | Contribute or run every quality gate | [Development guide](docs/development.md) |
308
+ | Review implementation decisions | [Architecture decision records](docs/decisions/README.md) |
309
+
310
+ The published documentation separates the
311
+ [compatibility contract](https://ydah.github.io/ibex/compatibility/),
312
+ [opt-in extensions](https://ydah.github.io/ibex/extensions/), and
313
+ [experimental surfaces](https://ydah.github.io/ibex/experimental/). The
314
+ [grammar gallery](https://ydah.github.io/ibex/gallery/) contains executable
315
+ examples whose declared tests reach complete production coverage.
316
+
317
+ ## Development
318
+
319
+ The [development guide](docs/development.md) contains frontend regeneration,
320
+ RBS/Steep validation, grammar coverage, browser checks, mutation analysis, and
321
+ workflow lint commands. The default local gate is:
322
+
323
+ ```sh
324
+ bundle install
325
+ bundle exec rake
326
+ ```
327
+
328
+ <!-- type-stats:start -->
329
+ The current whole-library `steep stats` result is 20,629 typed calls and 2,275 untyped calls out of 22,904 (90.1% typed).
330
+ The generated signature tree contains 2,084 explicit `untyped` occurrences across 103 files.
331
+ <!-- type-stats:end -->
332
+
333
+ Performance measurements are evidence, not portable scores or CI timing
334
+ thresholds. Follow the [benchmark guide](benchmark/README.md) to reproduce and
335
+ compare results under the same environment and configuration.
336
+
337
+ Ibex is available under the [MIT License](LICENSE.txt).
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ module Ibex
5
+ module Runtime
6
+ # Builds Data classes on Ruby 3.2+ and an immutable Struct-compatible
7
+ # fallback on older supported Rubies.
8
+ module ASTData
9
+ # @rbs (*Symbol members) -> singleton(Object)
10
+ def define(*members)
11
+ return ::Data.define(*members) if defined?(::Data)
12
+
13
+ immutable = Module.new
14
+ immutable.send(:define_method, :initialize) do |*arguments, **keywords|
15
+ super(*arguments, **keywords)
16
+ freeze
17
+ end
18
+ Struct.new(*members, keyword_init: true).tap { |node| node.prepend(immutable) }
19
+ end
20
+ module_function :define
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ module Ibex
5
+ module Runtime
6
+ module CST
7
+ # Opaque identity used to track a syntax occurrence across path-copying edits.
8
+ class SyntaxAnnotation
9
+ # @rbs () -> void
10
+ def initialize
11
+ freeze
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ require_relative "syntax_node" unless defined?(Ibex::Runtime::CST::SyntaxNode)
5
+
6
+ module Ibex
7
+ module Runtime
8
+ module CST
9
+ # Allocation-light cursor over Green elements and absolute offsets.
10
+ # rubocop:disable Naming/PredicateMethod -- cursor movement names follow the public tree-cursor convention.
11
+ class Cursor
12
+ # @rbs! type green = GreenNode | GreenToken
13
+ # @rbs! type frame = [GreenNode, Integer, Integer]
14
+
15
+ attr_reader :green #: green
16
+ attr_reader :offset #: Integer
17
+
18
+ # @rbs (SyntaxNode node) -> void
19
+ def initialize(node)
20
+ @green = node.green
21
+ @offset = node.offset
22
+ @frames = [] #: Array[frame]
23
+ end
24
+
25
+ # @rbs () -> bool
26
+ def goto_first_child
27
+ green = @green
28
+ return false unless green.is_a?(GreenNode)
29
+ return false if green.children.empty?
30
+
31
+ parent = green
32
+ @frames << [parent, 0, @offset]
33
+ @green = parent.children.fetch(0)
34
+ true
35
+ end
36
+
37
+ # @rbs () -> bool
38
+ def goto_next_sibling
39
+ frame = @frames.last
40
+ return false unless frame
41
+
42
+ parent, index, parent_offset = frame
43
+ next_index = index + 1
44
+ return false if next_index >= parent.children.length
45
+
46
+ frame[1] = next_index
47
+ @offset = parent_offset + parent.children.first(next_index).sum(&:full_width)
48
+ @green = parent.children.fetch(next_index)
49
+ true
50
+ end
51
+
52
+ # @rbs () -> bool
53
+ def goto_parent
54
+ frame = @frames.pop
55
+ return false unless frame
56
+
57
+ @green = frame.fetch(0)
58
+ @offset = frame.fetch(2)
59
+ true
60
+ end
61
+
62
+ # @rbs () -> Integer
63
+ def kind = @green.kind
64
+ end
65
+ # rubocop:enable Naming/PredicateMethod
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ require_relative "text_edit" unless defined?(Ibex::Runtime::CST::TextEdit)
5
+
6
+ module Ibex
7
+ module Runtime
8
+ module CST
9
+ # Computes byte edits while skipping physically shared Green subtrees.
10
+ module Diff
11
+ # @rbs (SyntaxNode old_root, SyntaxNode new_root) -> Array[TextEdit]
12
+ def text_edits(old_root, new_root)
13
+ edits = [] #: Array[TextEdit]
14
+ compare(old_root.green, new_root.green, 0, edits)
15
+ edits.freeze
16
+ end
17
+ module_function :text_edits
18
+
19
+ # @rbs (GreenNode | GreenToken old_green, GreenNode | GreenToken new_green,
20
+ # Integer offset, Array[TextEdit] edits) -> void
21
+ def compare(old_green, new_green, offset, edits)
22
+ return if old_green.equal?(new_green)
23
+
24
+ if old_green.is_a?(GreenNode) && new_green.is_a?(GreenNode) &&
25
+ compatible_nodes?(old_green, new_green)
26
+ child_offset = offset
27
+ old_green.children.each_with_index do |old_child, index|
28
+ new_child = new_green.children.fetch(index)
29
+ compare(old_child, new_child, child_offset, edits)
30
+ child_offset += old_child.full_width
31
+ end
32
+ return
33
+ end
34
+
35
+ append_trimmed_edit(old_green.to_source, new_green.to_source, offset, edits)
36
+ end
37
+ module_function :compare
38
+
39
+ # @rbs (GreenNode | GreenToken left, GreenNode | GreenToken right) -> bool
40
+ def compatible_nodes?(left, right)
41
+ left.is_a?(GreenNode) && right.is_a?(GreenNode) &&
42
+ left.kind == right.kind && left.children.length == right.children.length
43
+ end
44
+ module_function :compatible_nodes?
45
+
46
+ # @rbs (String old_text, String new_text, Integer offset, Array[TextEdit] edits) -> void
47
+ def append_trimmed_edit(old_text, new_text, offset, edits)
48
+ return if old_text == new_text
49
+
50
+ prefix = common_prefix(old_text, new_text)
51
+ suffix = common_suffix(old_text, new_text, prefix)
52
+ deleted = old_text.bytesize - prefix - suffix
53
+ inserted = new_text.byteslice(prefix, new_text.bytesize - prefix - suffix) || "".b
54
+ edits << TextEdit.new(start: offset + prefix, delete_length: deleted, insert_text: inserted)
55
+ end
56
+ module_function :append_trimmed_edit
57
+
58
+ # @rbs (String left, String right) -> Integer
59
+ def common_prefix(left, right)
60
+ limit = [left.bytesize, right.bytesize].min
61
+ index = 0
62
+ index += 1 while index < limit && left.getbyte(index) == right.getbyte(index)
63
+ index
64
+ end
65
+ module_function :common_prefix
66
+
67
+ # @rbs (String left, String right, Integer prefix) -> Integer
68
+ def common_suffix(left, right, prefix)
69
+ limit = [left.bytesize, right.bytesize].min - prefix
70
+ index = 0
71
+ index += 1 while index < limit && left.getbyte(-index - 1) == right.getbyte(-index - 1)
72
+ index
73
+ end
74
+ module_function :common_suffix
75
+
76
+ private_class_method :compare, :compatible_nodes?, :append_trimmed_edit, :common_prefix, :common_suffix
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+ # rbs_inline: enabled
3
+
4
+ require_relative "syntax_node" unless defined?(Ibex::Runtime::CST::SyntaxNode)
5
+
6
+ module Ibex
7
+ module Runtime
8
+ module CST
9
+ # Shared path-copying operations for Red syntax elements.
10
+ module Editing
11
+ # @rbs! type element = SyntaxNode | SyntaxToken
12
+ # @rbs! type green = GreenNode | GreenToken
13
+
14
+ # @rbs (element target, green | element replacement) -> SyntaxNode
15
+ def replace(target, replacement)
16
+ green = green_element(replacement)
17
+ current = target
18
+ parent = target.parent
19
+ while parent
20
+ children = parent.green.children.dup
21
+ children[current.index] = green
22
+ green = GreenNode.new(
23
+ kind: parent.green.kind, children: children,
24
+ flags: parent.green.intrinsic_flags, annotations: parent.green.annotations
25
+ )
26
+ current = parent
27
+ parent = parent.parent
28
+ end
29
+ raise TypeError, "the root replacement must be a GreenNode" unless green.is_a?(GreenNode)
30
+
31
+ red_root(target.root, green)
32
+ end
33
+ module_function :replace
34
+
35
+ # @rbs (SyntaxNode previous, GreenNode green) -> SyntaxNode
36
+ def red_root(previous, green)
37
+ source = SourceText.new(green.to_source, file: previous.source_text.file)
38
+ SyntaxNode.new(
39
+ green: green, kinds: previous.kinds, trivia_policy: previous.trivia_policy,
40
+ source_text: source
41
+ )
42
+ end
43
+ module_function :red_root
44
+
45
+ # @rbs (green | element value) -> green
46
+ def green_element(value)
47
+ return value.green if value.is_a?(SyntaxNode) || value.is_a?(SyntaxToken)
48
+ return value if value.is_a?(GreenNode) || value.is_a?(GreenToken)
49
+
50
+ raise TypeError, "replacement must be a Green syntax element or Red syntax view"
51
+ end
52
+ module_function :green_element
53
+ end
54
+ end
55
+ end
56
+ end