Commit 7d32728
authored
tree-sitter: v1 grammar — parses examples/01-single-module.twasm end-to-end (zero ERROR nodes) (#62)
## Summary
Extends the Phase 0 tree-sitter scaffold (PR #58, region-decls only) to
**v1 grammar** that parses `examples/01-single-module.twasm`
**end-to-end with zero parse errors**.
## Verification (local)
```bash
cd tools/tree-sitter-twasm
npm install
tree-sitter generate # no conflicts; parser.c emitted
tree-sitter parse ../../examples/01-single-module.twasm
# → 0 ERROR / 0 MISSING nodes, exit 0, full AST for all 134 lines
```
## What v1 now covers (beyond region declarations)
- **`memory_decl`** — `memory Name { initial: N; maximum: N; place X at
N; }`
- **`function_decl`** — `fn name(params) -> ret effects { ... } { body
}`
- `parameter_list` with `region_handle_type` (`&`, `&mut`, `own`)
- `effects_clause` — `Read / Write / Alloc / Free / ReadRegion(R) /
WriteRegion(R)`
- `return_type` including `void`
- **Statements** — `region.get`, `region.set`, `region.scan` (with
`where` + binding + body), `let [mut] name [: type] = expr;`,
assignment, `if`/`else`, `return`
- **Expressions** — literals (int/float/bool/null), `identifier_expr`,
`region_var` (`$x`), `binary_expr` (full operator set with precedence),
`unary_expr`, `is_null_expr`, `paren_expr`
- **Field paths** — including nested access (`.pos.x`)
- **`region_target`** — bare-identifier form for post-null-check
references
Precedence chain: `['unary', 'mul', 'add', 'cmp', 'and', 'or']` with
unary at top.
## Still deferred (next Track A PR)
- Imports / exports
- Invariant declarations + proof statements
- `const` declarations
- Block-expression `if ... yield`
- Match on union regions
- L13–L16 surface (`isolated`, `session`, `capability`, `choreography`)
- L11/L12 surface (`cost_bound`, `fresh`, `version_of`, `region.sync`)
- Lifetime annotations on function decls
- Striated region layout
By production count v1 covers ~60–70% of `spec/grammar.ebnf`.
## Files
| File | Change |
|---|---|
| `grammar.js` | ~80 → ~270 lines |
| `corpus/functions.txt` | new — 6 corpus tests covering memory_decl,
if/else, is_null guard with bare-ident target, scan, nested field paths
|
| `queries/highlights.scm` | extended with
function/memory/if/effect/handle-mode/literal/builtin categories |
| `README.md` | updated to reflect v1 scope |
| `tree-sitter.json` | new — tree-sitter-cli metadata |
| `package.json` | minor metadata tweaks |
## CI integration
Still deferred to the next Track A PR. When the grammar reaches full
EBNF parity, the install-and-test cost (~30MB tree-sitter-cli toolchain
on every PR run) is justified by the regression value. Today the 6-test
corpus + the local parse-of-example-01 covers the regression risk well
enough.
## Phase 0 impact
This PR plus the existing Track A scaffold (PR #58) moves the Phase 0
§Track A checklist from "scaffold landed" to "scaffold + v1 grammar
landed, parses simplest example end-to-end". Remaining Track A items
(full EBNF parity, Idris2 parser, ReScript cut, codegen v0) are still
ahead.
## Test plan
- [x] `tree-sitter generate` succeeds (no conflicts)
- [x] `tree-sitter parse examples/01-single-module.twasm` returns 0
ERROR / 0 MISSING nodes
- [ ] Corpus tests pass on a CI runner with tree-sitter-cli installed
(deferred to next PR with CI wiring)
- [ ] Smoke test, structural E2E, claim-envelope, property,
security-envelope, proof regression — none touch
tools/tree-sitter-twasm/ so no regressions expected
---
_Generated by [Claude
Code](https://claude.ai/code/session_01ExgUTJmU5UQQNLKynwxDjm)_6 files changed
Lines changed: 576 additions & 69 deletions
File tree
- tools/tree-sitter-twasm
- corpus
- queries
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
39 | 44 | | |
40 | 45 | | |
41 | 46 | | |
| |||
66 | 71 | | |
67 | 72 | | |
68 | 73 | | |
69 | | - | |
70 | | - | |
| 74 | + | |
| 75 | + | |
71 | 76 | | |
72 | 77 | | |
73 | 78 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
0 commit comments