Repository navigation
Classes
As of v1.0.0
A class is a set of strings. A placeholder in a form takes the class of the same name, and the reader checks every capture against that class. Classes also give the argument types of terms and primitives and the placeholders of file templates; see Terms and counts, Primitives and state and Trees.
A body binds a class with an is line, is : <name> = <class>:
is : category = "book" | "journal"
The class after = is a class expression, written as below. A placeholder <category> in a form of the same body takes this class.
A class expression is one of these:
-
Literal. A string in double quotes matches exactly that string:
"book"matchesbookand nothing else. -
Pattern. A regular expression between slashes, anchored to the whole capture:
/[a-z][a-z0-9-]*/matchesmathsandlevel-2. See Patterns. -
Reference. A bare name stands for the class of that name: a class of the same body, a class of the meta body, or a built-in class,
letter,canonical-pathorrelative-path; a run of letters is writtenletter+. Intag except "all" | "none",tagis a reference. -
Sequence. Class expressions written one after another match one after another:
"0x" /[0-9a-f]+/matches0x1f. -
Alternation.
|between class expressions matches either side:"rw" | "ro"matchesrwandro. -
except.<class> except "a" | "b"matches what the class matches, apart from the listed literals:tag except "all" | "none".
except binds weakest: everything before it is the class, and after it comes a list of quoted literals joined by |. Only the listed literals are removed, compared exactly:
is : tag = /[a-z]+/
is : free-tag = tag except "all" | "none"
free-tag matches red and alloy, and refuses all and none.
A sequence joins a literal and a pattern:
is : hex = "0x" /[0-9a-f]+/
hex matches 0x1f and 0x0, and refuses 0x, 1f and 0X1F.
Grouping is written inside a pattern, and a run of letters is letter+.
A bare word that names no class is an error; see Errors.
A reference names a class of the same body, a class of the meta body, or a built-in class. References are acyclic: no class may refer to itself, directly or through other classes, and a cycle is reported once, at its earliest is line (see Errors).
The reader provides three built-in classes:
-
lettermatches one letter: one code point in the Unicode letter categories. A run of letters is writtenletter+. -
canonical-pathmatches an absolute path whose components are non-empty, none of them.or.., over the allowed code points other than U+0020, the space (see Refused code points). The root/, with zero components, is a member; every other member has no trailing slash./and/shares/researchare members;shares/research,/shares/,//shares,/shares/./research,/shares/../archiveand/shares/team notesare not: a path with a space fails the class. So a placeholder of classcanonical-pathcan hold no delimiting space: inform : bind : <path> <mode>, with<path>of that class, the capture cannot hold the space that follows it, and the delimiter check finds no overlap. Membership is lexical: the class reads the string, and nothing resolves a symbolic link.parentof/is still an error; see Library functions. The last component of astackingcell's key is a path, whose class must becanonical-pathor a class the reader can show is a subset of it; see Stacking cells. -
relative-pathmatches one or more components joined by single slashes, with no leading, trailing or repeated slash, none of them.or.., and none holding U+0020, the space.researchandshares/researchare members;/shares/research,shares/,shares//research,./research,shares/../archiveandteam notesare not.
The component rule is one: a component of canonical-path, of relative-path and of the second argument of the library function join (see Library functions) is non-empty, neither . nor .., and holds no U+0020. A relative path joined under a scope therefore always yields a canonical-path.
relative-path marks the arguments that a scope resolves, and the mark is read from the receiving signature. An argument is resolved only at a position that a primitive's or term's signature declares relative-path, or a class that is a bare reference to it, directly or through other bare references, as in primitive : bind : relative-path mode. It is joined to the first argument of the workdir term of the program the scope names, and the resolved value must be a canonical-path; see Scope. A pattern, a literal alternation, a sequence or an except is an ordinary class even when its members are relative strings. A position of class canonical-path is never resolved: a relative capture that reaches it fails its class check, as any capture outside its class does. No mark passes from a form's capture across a signature.
is : alpha = letter+
alpha takes Algebra and Zürich, and refuses R2 and New York: a digit and a space are not letters.
The meta body's classes, the 17 meta classes, are number (versions and caps), multiplicity (count values), prefix (the comment prefix), order-mode and ordering (strict or free), law (cell laws), width (write widths, as in delete <k>, and the k of a keyed count's by <k>, so by 0 is legal), limit (the cap names states, repetition, depth and length), scope-mode (program, parent or root), name (names of bodies, classes, constructors and cells), constructor (a constructor name, defined as name and reported under name on E_CLASS), production, term, pattern, value (the argument capture of example, term, primitive, file, default, writes, reads, ensure, join and guard lines, any run of allowed code points), text (the text of a note line, any run of allowed code points) and verb (the verb class, of the question word). A capture on a line of a declaration that fails one of them is an error with the code E_CLASS, whose class is the meta class's name: version : one fails number, and cap : bogus = 10 fails limit. A question line whose word fails verb is E_QUESTION instead (see Questions).
A body may not declare a class, term, primitive or cell under the name of a library function (parent, join, split, ancestors, prefix, suffix, equal, concat, inherited; see Library functions), a built-in class (letter, canonical-path, relative-path) or any of the 17 meta classes. Such a declaration is refused with the code E_DECLARATION, its data.kind being duplicate and its actual the name: is : name = /[a-z]+/ and is : value = /[a-z]+/ are refused, with actual name and value. A form's verb is not among these declarations: a verb may be spelled letter, canonical-path or relative-path (see Questions). The same refusal holds for a body named Typify, the meta body's name, and for a form with the verb ?, which only the meta body declares, unless the declaration file's text is the reader's own meta source, byte for byte, which is the meta body itself (see Questions).
A pattern is written between slashes in the regular core:
-
Range.
[a-z]matches one code point fromatoz. One bracket may hold several ranges and single code points:[a-z0-9-]matches a lowercase ASCII letter, a digit or a hyphen, the hyphen standing for itself in the last place. -
Negated range. A bracket that opens with
^matches one allowed code point that the bracket does not list:[^=]is one allowed code point other than=. -
Any.
.matches one allowed code point:/.+/takes any capture of one or more allowed code points. -
Alternation.
|inside a pattern matches either side:/0|[1-9][0-9]*/. -
Grouping.
( )makes one element of what it encloses:/[a-z]+(-[a-z]+)*/takesredanddark-red, and refusesred-anddark--red. -
Repetition. After an element:
?zero or one,*zero or more,+one or more,{m}exactly m,{m,n}from m to n.
A plain character such as #, - or a digit stands for itself: /#[0-9a-f]{6}/ is # followed by six lowercase hexadecimal digits. Inside a pattern, \/, \|, \", \\ and \. stand for /, |, ", \ and ., and the escapes \d, \w, \s, \xHH, \uHHHH and \u{H…H}, each H a hexadecimal digit, are abbreviations of ranges. The pattern ends at the first unescaped /: /[0-9]+\/[0-9]+/ takes 3/4, its inner slash escaped, and /[0-9]+\.[0-9]+/ takes 2.5 and refuses 2x5.
A class is anchored to the whole capture: a capture is a member only when all of it, from its first code point to its last, matches the class. A pattern that is the whole class must match the whole capture: /[0-9]+/ refuses 42a, and /0|[1-9][0-9]*/ refuses 007. A pattern or a reference inside a sequence must match the whole of its part: hex takes 0x1f, since "0x" matches 0x and /[0-9a-f]+/ matches all of 1f, and refuses 0x1g, since /[0-9a-f]+/ does not match all of 1g. There is no partial match.
The reader strips a line of its outer blanks and splits a colon line at its first colon (an arrow line at its first arrow); the verb and the argument are each stripped of their outer blanks at that split, since the boundary is structural. Inside the argument a capture is the exact text the form's literals delimit, untrimmed, and its class decides: with form : item : [<word>] and is : word = /[a-z]+/, the argument [ a ] is refused and [a] is accepted. Emission and the declaration-time check model exactly that language.
In the Catalog form item : <category> = <title>, the literal between the two captures is = : a space, an equals sign and a space. The line item : journal = Market Research captures journal and Market Research. The line item : journal = Market Research, with two spaces before =, captures journal followed by one space, and category refuses it.
Strings are sequences of Unicode code points, and they compare exactly. There is no case folding and no normalisation: "book" does not match Book, and é written as one code point (U+00E9) is a different string from e followed by the combining accent U+0301. To take both cases, name both, as in /[A-Za-z]+/.
No class matches:
- a control character (category Cc);
- a format character (Cf), the zero-width joiner U+200D included;
- a line separator or a paragraph separator (Zl, Zp);
- a space separator (Zs) other than U+0020.
The refusal applies to every class at once: a class's language is its pattern's language intersected with the allowed code points. A negated range or . therefore never admits a refused code point, and a class such as /[^=]+/ is accepted: it takes Market Research and refuses a capture that holds a tab.
A class is legal whatever code points it names; only its language is cut. A class whose language is empty after the cut is not an error: it carries the note N_CLASS_EMPTY at its is line, with data holding the class's name. This class names only the tab U+0009, a control character, so its language is empty and its note's data is {"class": "tabs"}:
is : tabs = /\x09+/
A note never changes the exit status (see Diagnostics).
The ordinary space U+0020 is allowed inside a class; that is how a capture such as Market Research is taken. A capture is whatever the form's literals delimit, and its class decides what it may hold. Because the zero-width joiner is refused, no class takes an emoji sequence joined by U+200D.
- No host predicates. A class written
@module:namedoes not exist, and no code outside the class expression decides membership. What a host tests about the world enters a plan as facts; see Primitives and state. - No intersection of two classes and no negation of a class; both are deferred beyond this version. A negated range
[^...]inside a pattern takes one allowed code point; it does not negate a class.
Four caps bound the classes of a body, one cap line each. The meta body declares the defaults:
cap : states = 4096
cap : repetition = 256
cap : depth = 8
cap : length = 4096
states bounds the control states of a class's compact pattern automaton: the minimal automaton of its pattern, before any product with a length bound. length is an independent bounded counter on the capture, and the product of the two is never charged against states. Under cap : length = 4096, the class /.*/ is one control state and a counter, not 4097 states. repetition bounds the repetition, and depth the depth of references: a class whose expression references no class has depth 0, and any other class has one more than the greatest depth of the classes it references. A class whose depth exceeds the cap is E_CAP with cap depth, the cap as limit and the class's depth as actual, at its is line. Exceeding a cap is an error with the code E_CAP, whose cap names the cap. A capture longer than length is E_CAP with the cap length, never E_CLASS, and its range covers the capture; the range of an E_CAP for any other cap covers the is line of the class that exceeds it, the cap line that raises a cap above its default, or the declare line when a cap is lowered below what the meta body's own classes need.
The delimiter check and the check that a body's forms do not overlap may run the product of a class's automaton and its length counter symbolically. The derived work bound of each check is reported by typify check DECLARATIONS --bounds, as text or with --json, separately from the normal check response and from the grammar (see Command line). The bounds are host-dependent: the corpus never compares them, and the report claims neither CPU steps nor a representation another host must copy.
A body may lower a cap with its own line, such as cap : depth = 2; it may never raise one. Every cap is positive: a cap line whose value is 0 is rejected with E_DECLARATION, its data.kind being syntax.
A bare name in an is line that names no class is an error with the code E_CLASS_UNDECLARED. In a body that binds no class named book or journal, this line is rejected, because book and journal are bare names and so references:
is : category = book | journal
Quoted, "book" | "journal", they are literals, and the line binds the class.
A placeholder whose class is undeclared is a different error: a body whose form holds <price> and that binds no class named price is rejected with the code E_DECLARATION, and its data is {"kind": "capture", "actual": "<price>"}, the placeholder as written, angle brackets included. E_CLASS_UNDECLARED is only for a bare name in an is line.
Classes that reach one another through references form a cycle, which is an error. It is reported once, at the earliest is line among them in position order, with the code E_DECLARATION, its data.kind being class-cycle: the range covers that is line whole, and actual is the class that line declares. With is : a = "x" b and, below it, is : b = "y" | a, the one diagnostic stands at the is line of a, with a as actual.
A capture that fails its class is an error with a column. In the Catalog language, the line item : magazine = Weekly is an error: the capture magazine fails the class category. Its code is E_CLASS, and its data is {"class": "category", "token": "magazine"}. The diagnostic names the file, the line and the column; columns count code points, with a byte offset beside them. See Diagnostics.
An example line is checked against the form above it when the declaration is read, the classes of its captures included, and builds its term, library calls included, a template placeholder taking the shortest witness of its class (see Witnesses and the hostile corpus); an example that fails is an error in the declaration (see Terms and counts). Only a (context ...) or an (inherited ...) is not evaluated at an example. Its formation is checked whole when the declaration is read: the constructor it names exists, the expression's sort is a list under each and a string elsewhere, E_TERM_CLASS of kind sort otherwise, and the carrying constructor has its fuel cap when its rules carry an each, a context outside any each requiring none; only its values are read when a program is planned, against its enclosing programs (see Context lookup).
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 in a form. If some member of the class contains that literal, the declaration is rejected with the code E_CLASS_OVERLAP.
A grammar accepts a line if any way of cutting it into captures fits the classes. The reader never backtracks, so it takes one cut. When a capture can hold the literal that ends it, the reader and a grammar built from the same classes can disagree about a line.
The reader rejects this declaration:
#lang typify
declare : Settings
version : 1
is : label = /[A-Za-z =]+/
is : amount = /[A-Za-z]+/
term : set : label amount
form : set : <label> = <amount>
does : set <label> <amount>
example : set : Font Size = Large
end!
The class label can hold = , the literal that follows <label> in the form. Take the line set : Font = Size = Large. Cut at the first = , label is Font and amount is Size = Large, which fails amount. Cut at the second, label is Font = Size and amount is Large, and both fit. A grammar built from these classes accepts the line by the second cut; a reader that takes the first cut refuses it.
With label bound so that it cannot hold = , the declaration passes the check; the rest of the body stands as written:
is : label = /[A-Za-z ]+/
Font Size is still a member of label, and no member holds =.
This check is what makes the grammar that emit writes accept exactly the lines the reader accepts as well formed under the declaration's forms and classes, with the line shapes, for every declaration that passes it. That is the formation judgment. Counts, order, the results of library calls and the resolution of names are judgments on programs and declarations that the reader makes after formation, and a line grammar cannot carry them. The differential of the emission gate compares formation only: a refusal such as parent of /, a library result outside its receiving class, or an empty program under count = 1 is a semantic test beside it, never a grammar failure.
Emission escapes by code point. See Emission.
The build writes three witnesses for every class: its shortest member, a boundary member, and a near miss, which the class refuses. A class whose language is empty after the intersection with the allowed code points has no members and gets no witnesses: the build's witness file records it as empty.
The hostile corpus, test/corpus/hostile.json, is public with the corpus that Test your own host describes: all 107 of its hostile tokens are refused (beside 10 delimiter cases and 9 positive controls, which are accepted). See Contributing for the tests.
From the Catalog declaration (whole in Examples):
is : category = "book" | "journal"
is : title = /[^=]+/
form : item : <category> = <title>
category matches book and journal and nothing else. In title, the negated range [^=] is one allowed code point other than =, and + repeats it: title takes Market Research and Algebra, and refuses any capture that holds = or a refused code point. category cannot hold = , the literal that follows <category>, so the form passes the delimiter check.
From the Mounts declaration (whole in Examples):
is : path = canonical-path
is : mode = "rw" | "ro"
path is the built-in class canonical-path (see References): /shares/research and /shares/archive are paths, and so is the root /; shares/research, /shares/, //shares, /shares/../archive and /shares/team notes, whose last component holds a space, are not. path keys the stacking cell mounts, whose key is the path alone; the last component of a stacking cell's key must be of class canonical-path or a class the reader can show is a subset of it; see Stacking cells. mode matches rw and ro; RW is refused, since nothing folds case. Since path is canonical-path, not relative-path, no argument of Mounts is resolved under a scope.
Since / is a path, the line share : / is well formed, and the grammar that emit writes for Mounts accepts it. Its term share / rw then fails to normalize: the rule computes (parent <p>), and parent of / is an error with the code E_LIBRARY_PARENT (see Normalization). That refusal comes after formation; it is a semantic test, not a grammar failure.
From the Shelf declaration (whole in Trees):
file : shelves/<shelf>/shelf.typify
is : shelf = /[a-z][a-z0-9-]*/
is : state = "open" | "closed"
is : label = /[A-Za-z0-9 ]+/
shelf is the class of the placeholder in the file template: it takes maths and level-2, and refuses Maths and 2nd. state matches open and closed. label takes ASCII letters, digits and spaces, such as Algebra and Linear Algebra 2.
is : natural = /0|[1-9][0-9]*/
is : signed = /-?[0-9]+/
is : currency = /[A-Z]{3}/
is : ticket = /[A-Z]{2,4}-[0-9]{1,6}/
is : colour = /#[0-9a-f]{6}/
is : version = natural "." natural
is : amount = /[0-9]+/ "." /[0-9]{2}/
-
naturalis0, or a digit from 1 to 9 followed by digits:0,7and120match,007does not. -
signedis an optional minus, then digits:-3and42match,+3does not. -
currencyis three capital ASCII letters:EURmatches,eurdoes not. -
ticketis two to four capitals, a hyphen, and one to six digits:OPS-1042matches,O-1does not. -
colouris#and six lowercase hexadecimal digits:#1f6febmatches,#1F6FEBdoes not. -
versionis a sequence of two references tonaturalaround the literal".":1.0and12.30match,1.05and01.2do not. -
amountis a sequence of two patterns around the literal".":12.50matches,12.5and.50do not.