tabnas-c (Rust)
A C23 grammar plugin for the
tabnas parsing engine, crate
tabnas_c. It parses C source, plus the common GCC, Clang, and MSVC
extensions, into a CONCRETE syntax tree.
Concrete means the tree keeps what it parsed. Every token carries its
own source text and the span it came from, comments ride along as
tokens, a #define keeps its body, and a compiler extension survives as
itself rather than being normalised away. The section
What the tree keeps measures exactly how far
that goes.
The plugin layers on two others. The relaxed-JSON grammar of
tabnas-jsonic supplies the val
rule the C grammar hangs its expressions on, and is also the dialect
the grammar document itself is written in.
tabnas-expr supplies Pratt-style
expression parsing with the C operator table.
This is the Rust port of the canonical TypeScript implementation,
@tabnas/c; the TypeScript
version is authoritative and this crate tracks it. The Go port,
github.com/tabnas/c/go,
has the same shape. All three run the same shared fixtures, and the
same 100-program Csmith corpus.
Use
Parse a translation unit:
parse builds a parser once and reuses it.
Building one costs far more than a small parse, so keep your own for
anything past a one-off call:
A parser is Send + Sync and parses through &self, so one instance
serves every thread.
Tokens, spans, and trivia
A token node carries tname (the token identity), src (its text) and
span (start, end, line, col). A comment is a token like any
other, with a TRIVIA_ name:
Spans count Unicode scalar values, the chars of the source. The
canonical TypeScript counts UTF-16 code units and Go counts bytes, so
the three agree on every ASCII input, this port and the canonical agree
on anything inside the Basic Multilingual Plane, and all three drift
apart past it: after a block comment holding one astral character, the
int of int x; starts at 8 here, at 9 in the canonical, and at 11 in
Go. The shared fixtures are ASCII for that reason.
Extensions
Plain C23, the whole preprocessor included, is the default. The GCC, Clang and MSVC constructs are opt-in, and with them switched off the rules that parse them are stripped from the grammar document before it is installed, rather than merely left unreachable:
use COptions;
Names the source never declares
C cannot be parsed without knowing which identifiers are typedef names:
uint32_t x; declares x only if uint32_t is already a type. The
parser does not follow #include, so tell it what a header would have
said:
CMeta carries a [SymbolTable] and a [MacroTable], the same pair
the canonical plugin hangs off ctx.meta.cmeta.
Install it yourself
make and make_with are shorthand for installing the jsonic base
grammar and then this one. Do it by hand when the instance needs other
plugins too:
use Tabnas;
The plugin refuses a bare engine: it hangs its expression alternates on
the base grammar's val rule, and an instance without one has nothing
to hang them on. A second install returns early rather than giving the
instance a second copy of every alternate.
What the tree keeps
The tree keeps every token verbatim, at a span that names exactly the
text the token carries, in source order, and without overlaps. The
crate's round-trip test, tokens_are_verbatim_and_ordered in
tests/roundtrip_test.rs, reassembles every fixture input from its
tree and holds the port to that.
Three things are kept somewhere other than a token, and one group is not kept at all. Both lists are measured, and the same inputs behave the same way in the canonical TypeScript, so a port that kept more of them would fail the shared fixtures:
- An expression's OPERATOR is a field, not a token.
1 + 2gives abinary_expressionwithop: "+"and two operand children, so the operator text survives while its span does not. The same holds for an assignment, a unary operator andsizeof. - A call's parentheses and a
_BitInt(37)width are dropped. - A parameter list inside a parenthesised declarator, as in
int qsort(void *, int (*)(const void *, const void *));, is dropped. - A comment that no later token can take as leading trivia, which means one at the very end of the input, is dropped.
So the tree is concrete about names, keywords, literals, comments between tokens, macro bodies and compiler extensions, and it is not a byte-for-byte record of an expression's operator positions.
Limits
The tree is realized into engine values by a recursive walk, and the
value it builds recurses again when it is rendered or dropped. A Rust
stack overflow aborts the process, so the walk stops at
REALIZE_DEPTH_CAP levels and the parse reports the engine's cancel
code instead:
The cap is measured rather than copied: a translation unit of N nested
compound statements realizes at depth N + 2, and an unoptimized build
spends a little under a kilobyte of stack per level, so a thread with
the 1 MiB stack a small runtime hands out gives out somewhere past 300.
The cap sits below that. How much room ordinary source leaves under it
is a second measurement, taken 2026-09-22: the deepest of the shared
fixtures realizes at 17, and the deepest of the 100 Csmith programs at
146, which is over half the cap. Every one of those programs still
returns a tree, and the corpus test would report cancel for any that
stopped short.
Options
| Option | Type | Default | Effect |
|---|---|---|---|
extended |
bool |
false |
Parse the GCC, Clang and MSVC extensions, the inline assembly and attribute forms included. |
COptions::from_value reads the same option out of the JSON bag
use_plugin merges over the declared defaults, so
parser.use_plugin(tabnas_c::plugin(), Some(options)) and
make_with agree.
Install
The engine, the jsonic base grammar, the expr operator plugin, and the fixture runner are unpublished, so each is a path dependency on a sibling checkout:
[]
= { = "../c/rs" }
= { = "../parser/rs" }
= { = "../jsonic/rs" }
= { = "../expr/rs" }
Clone parser, json, jsonic, expr and support next to this
repository before building.
Differences from the canonical TypeScript
Three, each measured against a run of the canonical under Node and
recorded, with the measurement behind it, in the repository's
DIVERGENCE.md. The short version:
- A ternary in a declaration initializer, as in
int a = b ? c : d;, builds a tree that holds itself. The canonical throwsRangeError: Maximum call stack size exceededand Go dies withfatal error: stack overflow; this port reports thecancelcode and the caller carries on. - Deeply nested source is bounded by the realize cap described above. The canonical has no cap and aborts instead.
- A prefix operator as the whole of a brace initializer item, the
-5ofstatic int g[2] = {-5,1};, is left by the canonical as the raw operator arraytabnas-exprhanded it. This port drops the array, so the item comes back with no children and the tokens of that expression are not in the tree. Every shared fixture passes either way; 37 of the 100 Csmith programs carry at least one such item, and the corpus test names them.
Build and test
bash ../ci/rust/run.sh runs the whole gate, including a lockfile
check, and leaves the tree as it found it.
The grammar lives in ts/c-grammar.jsonic at the repository root and
is copied here by ts/embed-grammar.js. Edit it there, then run
make embed from the repository root.
License
MIT