# Agents Guide — the Rust port (`rs/`)
Read [`../AGENTS.md`](../AGENTS.md) first: it holds the repository
layout, the TypeScript-canonical contract, the shared fixtures, the
divergence rules and the untrusted-input stance. This file is only what
is specific to the crate.
## What this crate is
`tabnas-c`, lib `tabnas_c`. A plugin for the `tabnas` engine that
installs the C grammar on an instance that already carries the jsonic
base grammar. From 0.6 every declaration is built by the grammar's own
rules, and `tabnas-expr` reads every expression with C's operator table,
as in TypeScript and Go (`../docs/rules.md`).
TypeScript is canonical. `ts/src/c.ts` defines behaviour, option names
and defaults; `ts/c-grammar.jsonic` defines the rule chain and is copied
verbatim into `rs/c-grammar.jsonic`, `go/c-grammar.jsonic` and the
`grammarText` literal in `ts/src/c.ts` by `ts/embed-grammar.js`. Never
edit a copy. Edit the source and run `make embed` from the repository
root.
## Layout
| `src/lib.rs` | the plugin, `COptions`, `CMeta`, `make`/`parse`, token registration, engine options, grammar install |
| `src/matchers.rs` | the imperative lex matchers: identifiers, numbers, strings, punctuators, trivia, header names |
| `src/tokens.rs`, `src/sets.rs` | token names, keyword tables and the token sets the grammar dispatches on |
| `src/cst.rs` | the tree arena, `Item`, `Span`, and `realize` |
| `src/state.rs` | per-parse state: symbol table, macro table, lex mode, the arena itself |
| `src/rt.rs` | the runtime the ported handlers are written against: rule nodes, the `k`/`u` bags, the lookahead walk |
| `src/refs*.rs` | the grammar's `@name` handlers, split the way `ts/src/c.ts` groups them |
| `src/expr_grammar.rs` | C's expressions: the operator table `tabnas-expr` reads them with, the evaluator that makes C's nodes from expr's operations, the links between an operation's tokens, and `val`'s own alternates: sizeof's type form, statement expressions, `_Generic`, brace lists, a cast's type name and C's atoms (port of `ts/src/expr-grammar.ts`) |
| `src/fallback.rs` | the armed fallback: the token record, the chomp's end, the fence, the bail and catch-all alternates every rule below `external_declaration` gets, and the local scope `asm_operand` catches a failure in (port of `ts/src/fallback.ts`) |
| `src/members.rs` | `member(node, name)`, the named members the tree no longer holds, read from a node's children (port of `ts/src/members.ts`) |
| `src/trivia.rs` | the sub-lex hook that buffers comments onto the next token |
| `translate/` | GENERATED by `../ts/embed-translate.js`, never edited: copies of `tabnas.plugin.json` and `alchemy/render.alc`, which `src/lib.rs` serves as `translate()`, `manifest_text()` and `render_text()`; `tests/translate_test.rs` holds the copies to the files (see "Translation" in `../AGENTS.md`) |
## Three things the canonical does that Rust cannot do directly
**The parse state is a thread local, not a live object.** The canonical
hangs the symbol table, the macro table, the lexer mode and the tree on
`ctx.meta.cmeta` as JavaScript objects that matchers and handlers mutate
through shared references. An engine `Value` is copied on write and
cannot hold a native handle, so all of it lives in `state.rs` behind a
thread local, and the values that travel through a parse are HANDLES
into it (`{"#node": 12}`). `crate::realize` turns the handles back into
plain engine values at the parse boundary, which is why a caller driving
a `tabnas` instance directly has to call it before the next parse on
that thread. `parse_with` does it for you.
**A rule's node is a shared cell, and the parent's link to it is
frozen.** In the canonical every rule object has a `node` field of its
own, and a rule reads its finished child through `rule.child.node`.
Here a rule's node is an `Rc<RefCell<Value>>` that a pushed or replaced
rule SHARES with the rule it came from, and `set_node` installs a fresh
cell rather than writing through the shared one, which is what
`r.node = v` means in the canonical. The engine freezes the parent's
link to a pushed child at the FIRST link of a replacement chain,
matching how TypeScript never relinks `rule.child`. Put those together
and a rule that builds its node on a LATER link of an `r:` chain hands
the parent nothing: the frozen link still names the cell the parent
itself is holding, so the parent reads its own node back as the child's.
No rule of C's grammar builds its node that way. `tabnas-expr`'s rules
do, a cast's among them, and write the node they build onto the link the
parent reads; `test/spec/cast.tsv` is the fixture that fails if a cast's
node does not reach the context that pushed `val`.
**The engine's lookahead buffer has a different shape.** In the
canonical engine and in Go, `ctx.t` is an array that never shrinks:
consuming a token blanks the slot it vacates with a `NOTOKEN` sentinel.
Here `context.t` is a `Vec` of real tokens that shortens as the parse
consumes. `fetchDeep` cannot lex past a blanked slot, so a dispatch
validator that has already looked far ahead once is blind until the
parse catches up, and that blindness DECIDES which of the two
declaration paths a declaration takes. `rt.rs` keeps the high-water mark
of `context.t.len()` to stand in for the canonical array's length; see
the comment above `note_lookahead`. `e2e.tsv` is the row that fails if
this is wrong, and it fails by holding three declarations to a strict
route where the canonical arms them.
**A rule cannot write to its ancestors.** The canonical's handlers write
to a pusher's `u` and to an ancestor's node freely. Here a rule holds a
snapshot of its parent, so what a handler would write upward is kept on
the node (a flag in the arena, as `isTypedef` is) or cleared by the rule
that owns it when its close runs (the conditional branch's opener, in
the group's loop). And where the canonical catches what a handler throws
in an armed declaration, this port leaves a panic a panic: over the
conformance sweeps no handler throws (`src/fallback.rs`).
## Tests
| `tests/parity_test.rs` | the shared `test/spec/*.tsv` fixtures, through `tabnas-support` |
| `tests/roundtrip_test.rs` | the concreteness contract: tokens verbatim, in order, at the spans they claim |
| `tests/csmith_test.rs` | the 100-program Csmith corpus against the golden fixtures under `ts/test`, each tree held to the invariants `tests/tree_test.rs` holds |
| `tests/tree_test.rs` | every fixture's tree: each subtree held once, scalar members, a span on every node, every comment kept but one after the last token |
| `tests/repeat_test.rs` | every repetition a replace loop: over 10,000 items of each of 21, the deepest rule is what one item needs |
| `tests/path_dispatch_test.rs` | the path-dispatch catalogue `ts/test/spec/path-dispatch.tsv`: the `declKind` each shape comes out as, read from where the canonical reads it |
| `tests/c_test.rs` | behaviour that a fixture row cannot express: the plugin API, the options, the symbol table |
| `tests/limits_test.rs` | untrusted input: deep nesting, long input, unterminated constructs, odd Unicode |
| `tests/perf_test.rs` | that parse time stays linear in input size |
| `tests/streaming_test.rs` | `make_streaming()` against `make()`: every pass, every token the lexer gives and every ending alike, over the fixtures and the Csmith corpus, from the text and from a reader; what a streaming parse holds is `streaming_tests` in `src/lib.rs` (see "The streaming parser" in `../AGENTS.md`) |
| `tests/divergent_test.rs` | `test/divergent.tsv`, the register that fails when a recorded divergence REGRESSES and when it is repaired; from 0.6 it holds no row |
| `tests/version_test.rs` | `Cargo.toml`, `VERSION`, `ts/package.json` and `go/c.go` agree |
Run one file with `cargo test --test <name>`. The whole gate is
`bash ../ci/rust/run.sh`.
**Do not leave a scratch test behind.** A file named `zz_*.rs` or
`spike*.rs` under `tests/` is a developer aid, and it is a defect to
commit one.
## Adding a fixture
A fixture in `test/spec` runs in all three runtimes, so it has to pass
in all three: `npm test` from `ts/`, `go test ./...` from `go/`, and
`cargo test --test parity_test` from here. Read
[`../test/AGENTS.md`](../test/AGENTS.md) for the column format and the
ASCII rule.
## Divergences
Any input where this port answers differently from the canonical
TypeScript needs an entry in [`../DIVERGENCE.md`](../DIVERGENCE.md) with
a measured table, and a row in `test/divergent.tsv` where a cell can
express it, or a Rust test where it cannot. Prose alone is not a
record.
## Prose
`rs/README.md` is in the gated set (`ts/scripts/gated-docs.cjs`), so it
follows `docs/STYLE-GUIDE.md`: no em dash, no first person, no banned
phrase, and every `rust` fence is a complete `fn main` that runs as a
doctest. This file is an agent guide and is outside that set — em dashes
are house convention here.