Skip to content

Migrating .qvr source between grammar revisions

QVR's grammar changes between releases. qvr migrate rewrites .qvr source written for one grammar revision into source for a later one: it respells each construct whose syntax changed and refuses, with its location, each construct that has no mechanical rewrite. The migrated file is parsed with the target revision's grammar before it is written, so a migration either produces source that the target grammar accepts or stops with a diagnostic. Parsing checks the syntax only; the hash gate checks that the repository's own hops keep meaning, and checking and running a migrated model, with qvr check and a few representative runs, tests its types and behavior.

What qvr migrate does

For each file, the migrator parses the source with the grammar of the source revision, rewrites the constructs whose syntax changed, and parses the result with the grammar of the target revision before writing it:

flowchart LR
    A["Source bytes<br/><small>revision X</small>"] -->|"parse with X"| B["Versioned tree-sitter grammar"]
    B --> C["Parse-tree schema"] -->|"collect rewrites"| D["X → Y hop"]
    D --> E{"Rewrites disjoint?"}
    E -->|"yes"| F["Apply the rewrites to the source bytes"]
    F -->|"parse + validate with Y"| G{"Whole file valid?"}
    G -->|"yes"| H["Source bytes<br/><small>revision Y</small>"]
    E -->|"no"| STOP1["Stop with the location of the conflict"]
    G -->|"no"| STOP2["Stop with file diagnostic"]
    class A qv-input
    class B,C,D,F qv-checked
    class E,G qv-decision
    class H qv-output
    class STOP1,STOP2 qv-foundation

A rewrite replaces only the bytes of the construct it changes, so comments, blank lines, and formatting elsewhere in the file are kept byte for byte. A construct that has no mechanical rewrite stops the migration with its line, its column, and what to write instead; the migrator never approximates or drops it. A file in which nothing changed is returned unchanged after both parses.

A migration across several revisions composes the conversions between adjacent revisions. The installed package includes the grammar of every revision as a compiled parser, so qvr migrate never compiles a parser at run time; a source checkout compiles each revision's parser once, on first use.

Markdown and literate modules

qvr migrate also migrates the fenced qvr blocks of Markdown files, including literate .qvr.md modules, when they are named directly or found under a directory argument. Each block is rewritten in place and keeps its indentation. The comment on the line before a fence selects how the block is migrated:

Marker Migration
none, <!-- compile: standalone -->, <!-- compile: qiec --> the block is a complete file
<!-- compile: cumulative -->, <!-- compile: qiec-cumulative --> the block continues the earlier cumulative blocks of the file and is migrated together with them
<!-- compile: false --> the block is a fragment: it is migrated on its own if it parses, and listed for review either way
a ```qvr example fence the block is an example's steps: it is migrated as the example declaration it spells, and listed for review when the source revision cannot read that declaration

In a .qvr.md module every unmarked block is part of one tangled module, so the blocks are migrated cumulatively; prose, prose references, and {experiment} directives are left byte for byte. A block whose body only includes a .qvr file is left alone, since the included file is migrated itself. Every fragment is reported on standard output as review PATH:LINE: ..., saying whether it migrated, so that no fragment is passed over silently.

Common invocations

--from names the grammar revision the source was written for, as a release tag such as vX.Y.Z. It is required, since a .qvr file does not record its revision and the migrator does not guess. --to defaults to the grammar of the installed release.

Migrate one file in place:

qvr migrate --from vX.Y.Z model.qvr

Quivers 0.72.1 uses the v0.72.0 grammar revision. To migrate a model written for either 0.72 release into 0.73.0, run:

qvr migrate --from v0.72.0 --to v0.73.0 model.qvr

Migrate every .qvr file under a directory:

qvr migrate --from vX.Y.Z models/

Migrate between two given revisions:

qvr migrate --from vX.Y.Z --to vA.B.C model.qvr

Migrate the QVR blocks of a documentation tree and report its fragments:

qvr migrate --from vX.Y.Z docs/

Report what would change and write nothing:

qvr migrate --from vX.Y.Z --dry-run models/

Write migrated copies to another directory:

qvr migrate --from vX.Y.Z --output migrated/ models/

Check the whole chain of conversions without migrating any file (--check):

qvr migrate --check

What survives migration

Kept:

  • Everything outside a rewritten construct. A rewrite replaces only the bytes of the tokens it changes, so the rest of the file, its layout included, is kept byte for byte.
  • Top-level comments. File headers and comments between declarations pass through unchanged.
  • Comments in bodies. Comments inside declaration bodies pass through as written, in document order among the rewritten entries.
  • Comments in lexicon blocks.
  • Doc comments. #! lines move with the declaration they document.
  • Comments in multi-line brackets. Where the source revision allows a multi-line [...], (...), or {...}, comments inside it are kept.

Changed or dropped:

  • Comments inside single-line brackets. A one-line bracketed list cannot contain a comment, so a comment there must move into a multi-line bracket, opened with a newline after the bracket, to be kept.

The migration chain

The chain is the sequence of grammar revisions, one per release whose grammar differs, declared as the tuple CHAIN in quivers.cli.migrations. Each adjacent pair has one of three kinds of hop:

  • a converter that rewrites the declarations whose syntax changed;
  • an empty converter table, for a pair whose changes need no rewriting, with both endpoints still parsed;
  • an identity hop, for revisions with identical parser assets and schema, which checks the source against the next revision and keeps its bytes.

A hop that only adds forms does not invent indices, effect rows, or handlers in older source. Several releases may share one grammar, and then one parser serves them all. qvr migrate --check lists every hop.

The frozen 0.72.0 to 0.73.0 hop contains the changes between those grammars. The v0.73.0 to HEAD hop is an identity until development opens the next release hop. During development, that hop collects the rewrites of each changed feature. Each feature contributes span edits over one parse of the file; edits of two features that touch the same bytes are refused rather than ordered, so the result does not depend on the order in which the features are applied, and a construct that has no mechanical rewrite is refused with its line, its column, and what to write instead.

--check mode: coverage against the VCS

The grammar's history is kept as a panproto repository, one commit per distinct grammar revision, with each grammar stored as a schema whose vertices are its rule names. qvr migrate --check compares the schemas of every adjacent pair on the chain and reports:

  • added: rules in the target grammar that the source grammar lacks;
  • removed: rules in the source grammar that the target grammar lacks;
  • UNCOVERED removed rules: removed rules for which the hop declares no converter in its SOURCE_RULE_COVERAGE. A source file that uses such a rule fails to migrate at that declaration.

The command exits with a nonzero code when any pair has an uncovered removal or an identity hop whose grammars unexpectedly differ.

Meaning preservation: the hash gate

Parse gates prove that a hop's output is valid source, not that it means what its input meant. A syntactic hop must also preserve the content hash of every checked declaration it rewrites, and qvr hash --check-migrations checks that it does: every hop of the chain, and every feature of the release hop on its own, is applied to each corpus file that is valid source at the hop's revision, the result is migrated on to HEAD and checked, and its core hashes must equal the file's own. Renaming consistently, reformatting, and moving comments pass; a hop that changes a constant, renames inconsistently, or rewrites a construct it should have left alone fails, naming the hashes that appeared and disappeared.

qvr hash --check-migrations                       # the whole corpus
qvr hash --check-migrations docs/examples/source  # one directory

A hop that may change meaning says so: an entry in SEMANTIC_HOPS in quivers.cli.migrations, or a semantic reason on a feature of the release hop. The gate does not hold such a hop to the invariant, and still checks every other feature of the release hop. CI runs the gate over the whole corpus.

Migrating dependents of a package

A grammar hop moves source from one revision of the language to the next. A dependency's interface change is the same problem one level up: when a package renames a family, an export, or a sample site, every module of a dependent package that names it, and every run record of a program that draws it, must follow. qvr migrate --dependency does that with the same machinery:

  1. Update the manifest. Point the dependency at its new version (a new rev, version, or path) and leave qvr.lock as it is: the lockfile records the version migrated from.
  2. Migrate. From inside the package:
qvr migrate --dependency worlds@1.0.0..2.0.0 --dry-run runs/*.json
qvr migrate --dependency worlds@1.0.0..2.0.0 runs/*.json

The lens is the one the new version emitted with qvr bump (lenses/ in its source), or is computed from the two versions' interfaces when it emitted none. Every module of the package is resolved against the old build, every name of a renamed symbol is respelled by a token-local edit, and the edits go through the same gates as a grammar hop: no two may overlap, none may leave trailing whitespace, and the result must parse. The migrated module is then resolved against the new build, and a module in which a new name would resolve to anything but the renamed symbol (another declaration in scope captures it) is refused. Run records given as paths get the migrated program's site table and site values, matched by site identity. 3. Review what has no lens. A place that names a symbol the new version removed is listed with its file, line, and column, its file is left unchanged, and the command exits 1. Rewrite those places by hand against the new interface. 4. Lock. qvr lock pins the new version; qvr build then checks everything against it.

A dependency lens only respells names, so it preserves meaning in the sense of the hash gate: each migrated module's declarations have the content hashes against the new dependency that they had against the old one, and a migrated run record replays to the same values.

VCS blame on migration failure

When a hop has no converter for a declaration's kind, the migrator looks up the rule in the grammar history and prints, for instance:

qvr migrate [vP.Q.R -> vX.Y.Z]: no converter for 'continuous_decl'.
VCS blame: introduced at vM.N.O; last present at vM.N.O.

The declaration is then checked against the target grammar and stops the migration if it does not parse there. The message names the release whose hop needs a converter.

Limits

  • No backward migration. Migration runs forward along the chain only.
  • Text-based emission. The migrator writes each target declaration as text and then parses it, rather than building it as a panproto schema and printing that.

Maintaining the migration chain

A grammar change is prepared together with its migration: the change adds a feature module to the release hop, regenerates the grammar's derived assets with python tools/grammar_assets.py ritual (which appends the working grammar to the schema history with python grammars/qvr/vcs/build_schemas.py --revision HEAD and pins the new commit), and migrates the repository's own .qvr files and documentation. Released schema identities are immutable, so build_schemas.py is never run bare, which re-commits every revision and stops at the first that exists, or with --reset, which gives every released revision a new commit id and breaks every pin. Changing the QVR language gives the whole procedure, and the grammar asset procedure describes each asset and the release preparation that freezes the working grammar under a release's name.

The migrators do not read the grammar history to perform a migration: they walk the parsed source. The history serves --check and the blame message. grammars/qvr/vcs/README.md describes the workflow, and The QVR DSL: Overview the syntax.