Repository navigation
Emission
As of v1.0.0
typify emit writes a declared language's grammar as data. A program other than the reader, written in any language, can then know which lines are well formed in the language. The first concrete use is constrained decoding.
typify emit DECLARATIONS LANGUAGE
typify emit DECLARATIONS LANGUAGE --gbnfDECLARATIONS is a declaration file and LANGUAGE is the name of one of its bodies. For the Catalog language shown on the Command line page:
typify emit languages.typify Catalog
typify emit languages.typify Catalog --gbnfBy default, and with --json, emit prints the JSON grammar: the language's grammar as data, stating which lines are well formed in the language. With --gbnf, it prints the GBNF text of the same grammar, the grammar format that constrained-decoding runtimes read. Either way it prints the grammar alone, a document and not a report: never a diagnostics key and never a note, which check reports. On a failed declaration, emit prints {"ok": false, "diagnostics": [...]} under --json, and the diagnostics' text projection otherwise, and exits with status 1, as check does. --gbnf given with --json is a usage error, E_USAGE, with --gbnf as actual. See Command line for the exit statuses.
typify emit languages.typify Catalog prints Catalog's JSON grammar (the reader prints compact canonical JSON with object members in sorted key order; the block below orders and spaces them for reading):
{"language": "Catalog", "version": 1, "comment": "#", "caps": {"states": 4096, "repetition": 256, "depth": 8, "length": 4096}, "classes": {"Catalog.category": {"literals": ["book", "journal"]}, "Catalog.title": {"pattern": "[^=]+"}}, "forms": [{"verb": "item", "shape": "colon", "production": "item : <category> = <title>"}]}| Member | What it holds |
|---|---|
language |
The language's name, as written after declare :. |
version |
The language's own version, from its version line. |
comment |
The comment prefix of the language's programs, with the meaning of the body's comment line. It is always present, and empty when comments are disabled. Two bodies that differ only in their comment line accept different lines. |
caps |
The language's effective caps, an object with the members states, repetition, depth and length. |
classes |
Every class definition the language reaches, inherited ones included, each under its qualified name, Body.name. |
forms |
One object per form. |
Catalog declares no comment line, so its prefix is the default #. It declares no cap line, so its caps are the meta body's defaults: states 4096, repetition 256, depth 8 and length 4096. Its classes are Catalog.category, an alternation of literals only, and Catalog.title, a pattern. Its one form is a colon line whose verb is item.
Each class definition is a tree of these nodes:
| Node | What it describes |
|---|---|
{"literals": [...]} |
An alternation of literals only. |
{"pattern": "..."} |
A slash pattern. |
{"ref": "Body.name"} |
A reference to another class, by its body and its name. |
{"seq": [...]} |
A sequence, written by juxtaposition. |
{"alt": [...]} |
An alternation that is not all literals. |
{"repeat": {"node": ..., "min": m, "max": n}} |
A repetition of a node, at least m and at most n times; max is null when unbounded. |
{"except": {"node": ..., "literals": [...]}} |
A class with except, and the literals it removes. |
{"builtin": "letter"}, {"builtin": "canonical-path"} or {"builtin": "relative-path"}
|
One of the three built-in classes. |
A form object holds its shape, its production and its verb. The verb is the literal left field of a colon or bang form, or null when the left field holds a placeholder or several words. The object of an arrow form also holds its left and right productions.
typify emit languages.typify Catalog --gbnf prints the GBNF text. The rules below are schematic: they show the readable structure of the rules for Catalog's form and classes, not the printed text.
root ::= line
line ::= "item : " category " = " title
category ::= "book" | "journal"
title ::= [^=]+
The printed text adds the rules for the line shapes and for the reserved ? line of Questions; this page does not show them. It also keeps three things this readable structure leaves out: the refused code points, which it refuses; the caps; and the exact formation of a line, spacing included, as described under Accepts exactly. It uses the readable structure only where that structure is equivalent.
The GBNF text fixes the language it recognizes, not the names of its rules. Apart from the root, a rule's name is not part of what a host must reproduce, and no expectation can rest on one.
The emitted grammar accepts exactly the lines that the reader accepts as well formed under the declaration's forms and classes, with the line shapes, for every declaration that passes the declaration-time check described on Classes. This judgment on single lines is the formation judgment, and it is all the grammar carries.
It models a line as the reader reads it. The line's outer blanks are stripped, and the verb and the argument are each stripped of their outer blanks at the split. Inside the argument, a capture is the exact text that the form's literals delimit, untrimmed, and its class must match all of it.
The declaration-time check is what makes the claim true. The reader reads a line once, left to right, and never backtracks: a capture ends where the literal that follows its placeholder first appears. A grammar may cut the same line anywhere a cut fits, so on some lines the two could disagree. At declaration time the reader checks, as an emptiness test on the product automaton, that no class can hold the literal that follows its placeholder, and rejects a declaration that fails the check with the code E_CLASS_OVERLAP. The derived work bound of the check is reported by typify check DECLARATIONS --bounds, apart from the normal check response and from the grammar; see Command line and Classes.
For example, take a form with two placeholders and a literal between them. If the first placeholder's class could contain that literal, a line holding the literal twice could be cut in two places: the reader would cut at the first, and a grammar could cut at the second. Such a declaration is rejected before any program is read.
Counts, order, the results of library calls and the resolution of names are judgments on whole programs and declarations. The reader makes them after formation, and a grammar of single lines cannot carry them. So the reader can refuse a program whose every line the grammar accepts:
- A Catalog program with no
itemline failscount : item = 1..*, with the codeE_PROGRAM_COUNT. The grammar judges one line at a time and has no line to refuse. - Under the Mounts language of Command line,
share : /is well formed, since the root/is a member ofcanonical-path, and the grammar accepts it. Its rule then callsparentof/, which is an error with the codeE_LIBRARY_PARENT. - A library call whose result falls outside the class that receives it is refused with the code
E_TERM_CLASS, however well formed the line it came from.
These refusals are semantic tests beside the grammar, never grammar failures. Constraints over a whole document, for counts and order, are an option for version 1.1 of the emitted grammar; the grammar of 1.0 carries none.
A class is written in the notation of Classes: quoted literals such as "book", patterns between slashes such as /[^=]+/, references by bare name, sequence by juxtaposition, alternation by |, and except. Inside a pattern, \/, \|, \", \\, \. and the escapes \d, \w, \s, \xHH, \uHHHH and \u{H…H}, which abbreviate ranges, are written with a backslash, and the pattern ends at the first unescaped /. An unescaped . matches any allowed code point. See Classes.
Emission escapes by code point: where the emitted grammar escapes a character of a class, it names that character by its Unicode code point.
Strings in Typify are sequences of Unicode code points, so that no host's string model leaks into the language. A class's language is its pattern's language intersected with the allowed code points, so a negated range such as [^=], or ., never admits a refused code point; see Classes. A class is legal whatever code points it names, and a class whose language is empty after the intersection carries the note N_CLASS_EMPTY, not an error. The emitted grammar names the same code points as the classes the reader checks, and admits no refused one.
Declaration files are written in a language of their own, the meta language: the verbs declare, version, is, form, does, term, count, example and the rest. The reader owns the meta forms as one table. It reads every declaration file through that table, emit Typify prints the grammar that the table gives, and a question about a meta word asked of the meta language Typify, such as ? : form, is answered from it; see Questions.
typify emit Typifyprints the grammar of the meta language, as JSON in the shape of The JSON grammar, or as GBNF under --gbnf. It takes no declaration file. Its output is a grammar, not declaration source. The meta language's self-declaration, typify.typify, is written by the release build from the reader's own meta source into every release archive, at typify/share/typify.typify, beside bin/; no CLI command prints it, and the repository keeps no committed copy of it. Because the build writes it from the reader's own table, the two cannot drift, and a release carries the reader and the self-declaration it reads together.
The shipped file reads cleanly under the reader of its release:
typify check typify/share/typify.typify --jsonIts text is the reader's own meta source, byte for byte, so its body is the meta body itself (a copy that differs by even one byte, a trailing blank line included, is read as an ordinary file and refused): its declare : Typify line and its form with the verb ? are not refused, as they would be in any other declaration file (see Questions).
The meta body declares primitives, so every meta constructor is a primitive, is rewritten by a rule, or is declared fact : <constructor>, as in any body that declares a primitive; see Terms and counts. Its kernel primitives are declared effect : <primitive> = external, so the reader treats each as external (and ask answers each with its effect line), and plan over the meta body is refused with E_USAGE.
typify.typify describes the language inside the language. It is not a self-interpreter: the reader interprets every level, the meta language included.
The reader is the contract. A constrained decoder is a consumer of the emitted grammar. Where the reader and a decoder disagree on whether a line is well formed, the reader is what ships, and the disagreement is a failing test. A well-formed line whose program the reader refuses later, for a count, the order, a library result or a name, is no such disagreement; see What the grammar does not carry.
Six law tests gate emission. Each states a law that the reader, its meta table and the emitted grammar must keep. No single comparison of bytes stands in for them.
-
A verdict corpus with refusals. Lines with their expected verdicts, refusals included, written blind: from the language's text alone, by someone other than the author of the meta table. The corpus lives in
test/corpus/and is imported verbatim. The reader's own fixtures never count as the corpus. -
A structural round trip.
read(print(D)) = D, whereprintis the reader's internal printer of declaration text (emit-declarations), not thetypify emitcommand, over the public and blind declarations. -
A differential with delimiter insertions. Test lines are made by inserting a form's own literals into the text of its captures, which is where a reader that never backtracks and a grammar that may cut anywhere could part. The reader's verdict on each line is compared with that of a matcher in
test/and of a pinned constrained decoder. The comparison is of formation only. Refusals such asparentof/, a library result outside its receiving class, or an empty program undercount = 1are semantic tests beside it, never grammar failures. The forms come from outside the project: five line formats with more than one placeholder, restricted test dialects of documented syntax references (an HTTP header field, systemdBindPaths, an OpenSSHRemoteForward, an APT one-line source and apasswdrecord), public intest/decoder-corpus.json. - The declaration-time emptiness check of Classes: no class may hold the literal that follows its placeholder in a form.
- Mutation strength of at least 95 percent over the meta definitions. Every single-point mutation of the meta table fails a test other than the round trip. The round trip alone may pass such a mutation, since the same mutated table both prints and reads.
-
Byte identity, kept as smoke.
typify.typify, read and printed again by the reader's internal declaration printer, gives the same bytes, and the canonical JSON grammar emits byte-identically. A printer that leaves out a whole form can still reproduce its own output, so this test is a smoke test, not the proof.
Emission lets a frozen model, one whose weights do not change, be taught a declared language. The runtime that runs the model reads the emitted GBNF and holds the model's output to lines that are well formed in the language. Counts, order, library results and names are still the reader's to judge; see What the grammar does not carry.
Typify is coupled to no model and no runner. It prints a grammar; the model and the runner live elsewhere and read it. A model can also be given the answer lines of a question about a word, with the JSON form of the answer beside them; see Questions.