Skip to content

Grammar assets and the release migration

grammars/qvr/grammar.js is the only authored description of the QVR surface. Everything else that knows the grammar is derived from it and pinned by tests, so a grammar change is finished only when every derived asset agrees with it and the release migration hop accounts for every released form the change affects. This page describes that procedure; the migration guide describes qvr migrate as users run it.

The derived assets

Asset Read by Pinned by
grammars/qvr/src/{grammar.json,node-types.json,parser.c} quivers.dsl, which compiles parser.c on first use into a content-addressed library under ~/.cache/quivers tests/dsl/test_qiec_grammar_assets.py (regenerated from grammar.js and compared)
src/quivers/dsl/_grammar_data/grammar.json installed wheels and keyword introspection the same module
grammars/qvr/queries/highlights.scm, its Zed copy, and the vocabularies of editors/vscode-qvr/syntaxes/qvr.tmLanguage.json tree-sitter highlighters, Zed, VS Code the same module and tests/cli/test_editor_release.py
grammars/qvr/vcs/parsers/HEAD/source and GRAMMAR_ASSETS["HEAD"] in src/quivers/_grammar_manifest.py qvr migrate, which parses the current grammar with this snapshot after checking its hashes tests/dsl/test_qiec_grammar_assets.py, tests/cli/test_migrate_chain.py
the panproto schema of the working grammar and SCHEMA_COMMITS["HEAD"] qvr migrate --check and the coverage tests tests/cli/test_migrate_chain.py, tests/cli/test_migrate_release_hop.py
grammars/qvr/vcs/parsers/HEAD/qvr.dylib and editors/vscode-qvr/vscode-qvr-<version>.vsix local snapshot builds and the VS Code release tools/check_editor_release.py --full

Regenerating after a grammar edit

python tools/grammar_assets.py regenerate

The command holds a lock in the git directory, so two runs in one checkout take turns. It:

  1. runs tree-sitter generate on a private copy of the grammar and installs the generated files only if generation succeeds, then prints the size of the parse-action table against tree-sitter's limit of 65535 entries;
  2. runs the tree-sitter corpus tests in grammars/qvr/test/corpus;
  3. copies grammar.json into the package data;
  4. regenerates the highlight query, its Zed copy, and the TextMate vocabularies;
  5. refreshes the HEAD migration snapshot and rewrites GRAMMAR_ASSETS["HEAD"];
  6. compiles the parser quivers.dsl loads.

The exit status is nonzero if any step fails, including a corpus test. python tools/grammar_assets.py check makes the same comparisons and writes nothing.

The parse-action table is the grammar's binding size budget, and tests/dsl/test_qiec_grammar_assets.py fails when it exceeds 35000 entries.

Adding grammar cheaply

tree-sitter expands every choice and optional written inside a rule into separate productions of that rule, and every production carries its own copy of the parse states for the rest of the rule. A rule with three inline optional parts and a list in three layouts therefore has dozens of productions, and the states after them multiply. The grammar keeps its table small by giving each varying part its own rule:

  1. Never call bracketedList or parenthesizedList inside a larger seq. Each list has three layouts (inline, indented, hanging), so an inline call triples the enclosing rule. Give the list its own rule and refer to it. When the list is not a node of its own, make the rule hidden (_rule_args: $ => bracketedList(...)): a hidden rule produces no node and its fields belong to the parent, so the parse tree is unchanged.
  2. Factor long optional groups out of long seqs into hidden rules for the same reason, and prefer one hidden rule shared by every declaration that spells a construct the same way (_qiec_static_arguments serves calls and effect requests).
  3. Reuse the existing expression rules (_let_arith, _qiec_value, _object_expr, option_block) rather than adding a sublanguage. Each new position that an expression rule can be followed by different tokens in costs states for the whole expression ladder.
  4. Keywords are free at the start of a declaration. The grammar's word makes a keyword contextual wherever an identifier cannot occur; a keyword in the middle of an expression is not.
  5. Measure before committing. python tools/grammar_assets.py check prints the budget; tree-sitter generate --report-states-for-rule - in a scratch copy of grammars/qvr lists the rules with the most states.

Moving every inline list of the grammar into its own rule reduced the table from 62214 to 17178 entries with identical parse trees.

The asset ritual

python tools/grammar_assets.py ritual

runs regenerate and then completes the assets that change rarely:

  1. rebuilds the HEAD snapshot and its library with grammars/qvr/vcs/build_parsers.py --revision HEAD, and fails if that build produces different snapshot bytes;
  2. appends the working grammar to the schema history with grammars/qvr/vcs/build_schemas.py --revision HEAD and pins the new head commit as SCHEMA_COMMITS["HEAD"];
  3. reinstalls the VS Code extension's dependencies from its lockfile, repackages the tracked archive, and checks it with tools/check_editor_release.py --full;
  4. compares every derived file again, reports whether the Zed extension's pinned grammar commit still names the current parser, and runs the asset test modules.

build_schemas.py --reset is not part of any procedure. It rebuilds the history from git tags alone, which assigns every released revision a new commit id and drops the named revisions that are not git tags, so every pin in SCHEMA_COMMITS stops matching. Released history is immutable; only the working grammar is appended.

The Zed extension pins a fetchable commit whose parser and query bytes match the checkout. Pull-request CI can use an immutable commit on the feature branch. Release preparation updates editors/zed-extension-qvr/extension.toml to the merged commit so the released extension can fetch the grammar from main history.

The release migration hop

During development, one hop package migrates the newest released revision to HEAD. Release preparation freezes that package under src/quivers/cli/migrations/v<released>_to_v<next>/. The new release then migrates to HEAD through an identity hop until the next development hop opens. Each public module of the release hop package describes one language feature as a Feature from quivers.cli.migrations._features. The hop loads these modules in name order:

FEATURE = Feature(
    name="annotations",                       # the module's name
    vertex_collectors={"construct_expr": collect_construct},
    source_rule_coverage=frozenset({"construct_expr"}),
    probes=(IdentityProbe(name="motive", source=b"..."),),
    rewrites=(RewriteExample(name="simple", source=b"...", expected=b"..."),),
    cuts=(Cut(name="computed type", source=b"...", line=3, column=12, replacement="(c(x) : T)"),),
    keywords=(Keyword(word="record", policy="contextual"),),
)

A collector reads the parse of the released source through a SchemaView and returns byte span Edits; vertex_collectors are called once per vertex of their released rule, file_collectors once per file. A construct with no mechanical rewrite is refused by raising refusal(view, position, message, replacement), which reports the line, the column, and what to write instead. The hop parses the source at the released revision, collects every feature's edits, refuses any two edits that touch the same bytes or insertion point, applies them, refuses an introduced trailing space, and parses the result at the hop's TARGET_REVISION. This is HEAD during development and the frozen grammar revision at release. Because edits never overlap, feature order does not affect the result. A feature makes its edits token-local so that another feature can edit inside the same construct.

A feature's declarations are its tests. tests/cli/test_migrate_release_hop.py checks that every IdentityProbe parses to the same tree at both revisions and passes through unchanged, that every RewriteExample migrates to its exact bytes, that every Cut is refused at its line and column with its replacement, and that every keyword is justified: a contextual keyword by a probe that still uses the word as an identifier, a reserved keyword by a rewrite example of the rename the hop performs for it (Keyword(word=..., policy="reserved", rename_to=...)). tests/cli/_migration_support.py provides assert_migrates, assert_identity, and assert_refused, each optionally restricted to one feature, for further cases.

A feature that changes the checked meaning of what it rewrites says why in Feature.semantic; the hop's edge then appears in SEMANTIC_HOPS, and every other hop must preserve the content hash of each declaration it rewrites.

Preparing a release

Release preparation freezes the working grammar under the release's name, as a parser snapshot under grammars/qvr/vcs/parsers/v<next> with its hashes in GRAMMAR_ASSETS and REVISION_SNAPSHOTS and its schema commit in SCHEMA_COMMITS and the panproto tags. It sets the hop's TARGET_REVISION to "v<next>", inserts "v<next>" before "HEAD" in CHAIN, and registers the identity migrator for ("v<next>", "HEAD"), so that the next release starts a new hop package.

Gates

Besides the asset modules above, tests/cli/test_grammar_gates.py parses every .qvr file in the repository with the current grammar and rejects a .qvr file with a line that ends in whitespace, which the layout scanner reports as an error on the following line.