KAST: Koma abstract syntax tree
The parser builds this syntax tree from .koma source for linting, formatting, and semantic interpretation. The source remains authoritative. Each tree represents one revision of that source, as defined in the parser and AST boundary. The tree belongs to the language side. Rendering and export compute their presentation models separately from it.
The implementation is src/core/lang/ast/tree.ts (types) and src/core/lang/parser.ts (parseKoma(source) builds it; parseWithDiagnostics(source) also returns diagnostics). Node kinds, their fields and positions are covered by conformance (see Conformance) and, with the kinds of sourceLine, change only under the language's compatibility policy. data and its content (such as the scanner's records of lines, the Internal column of Syntax tree nodes's table) and the internals of the implementation that builds the tree are not. Each change to the promised shape also takes the next AST version (The AST version).
- Each node of the Koma AST holds set fields for its kind (
type). How to write Nothing is written by hand:parseKoma(source)builds the tree. A tool that reads the tree reads the fields in the table below and the kinds of line. Details- Every node has a
typeand its source rangeposition. A node that holds source lines keeps, indata, the scanner's record of each line (so the semantic reading can rebuild lines from the tree alone).datais internal and not promised: its values and shape may change between versions, so tools that read the tree read the Fields and the line kinds, neverdata.
typeParent Fields Internal (not promised) Meaning rootyes childrendata.openFence(the mark of a fence left open at the end, elsenull)The whole document. Its direct children are the document settings and blank lines and comments before the first page pageyes children,body(from after the delimiter and complete settings to the next page),boundary(the+++line; absent on the implicit first page)none One page. Pages divide the source without gaps (the line break just before the next page belongs to this page) settingsyes scope(documentorpage),body(inside the delimiters),children(sourceLine)none A closed YAML settings block. Values are interpreted separately notesyes body,closed,children(sourceLine)none Notes ( """). Elements and delimiters inside are isolated as content. Unspoken[[blocks are part of the body (Elements)memoyes attributes(tagToken, each an attribute assignment of the opening:::line),body,closed,children(sourceLine)none A memo ( ::: key=value…:::) on a page. Its text is isolated as content and never output (Memos)elementyes tagName(the name, elsenull),children,openingandclosing(a multi-line element's opening and closing lines;closinghas the bracket's positionbracketand whether it is on the body's last line,inline)data.lineanddata.close(the scanner's records of the opening and closing lines)A bracketed element elementTagyes children(tagToken)none An element's tag tagTokenno ( value)kind(selector,attributeAssignment,bareWord;selectoris a tag name with any joined shorthands or a standalone ID or class shorthand;bareWordis a bare word outside the tag's grammar, kept as written, Elements),value(the word as written), and for attribute assignmentsseparator(the position of=) andvalueKind(the kind of the value as written), and for a listitems(Value kinds in the tree)none A word of the tag. Duplicates and invalid values are kept. What a value means (units, a colour or a reference) is not in it sourceLineno ( value)kind(the kind of line, in the table below),value, and when neededfinding,fence(code,math),settingValueAtdata.line(the scanner's record of the line)One source line rawno ( value)valuenone The source of an element's body A
sourceLine'skindis one of these. Element lines (a one-line element, an opening line, a closing line) never become asourceLine; they are theelementand itsopeningandclosing.kindMeaning settingA key: valueline inside settings delimiters.settingValueAtis where its value starts (Page settings, Settings YAML)setting-otherA line inside settings delimiters that the subset does not read (nesting, a list and so on) frontA delimiter around settings: ===for the document's,---for a page's (Document settings, Page settings)blankA blank line before a page's content (at the start of the file, after a page delimiter, inside settings) pageA page delimiter +++(Page boundaries)attribute-lineA line of an element tag that continues after its opening line (Tag and content) commentA comment line (Page boundaries) blockedA line isolated with its block and reported: settings out of place or unclosed, an unused :::block and so on (Document settings, Page settings)notesA notes delimiter """and the lines insidememoA memo's :::lines and the lines inside (Memos)fencedA line inside a code fence or $$, and the fence lines.fenceiscodeormath(Code and math are isolated)literalA line of the body of a math, code or diagram element, kept as written textA line of text. A bracket line that stays text says why in finding(When brackets are an element)- Ranges:
position.startandposition.endare half-open. Lines and columns count from 1,offsetfrom 0, in UTF-16 code units. CRLF is two units and one line; a lone CR is one line too. A leading byte order mark in the input is included in positions. - Never dropped: the written order and duplicates of tag names and attributes; quoting; unknown settings; bare text; comments; unclosed YAML, fences and elements. Unreadable parts keep their source and diagnostics.
- Unquoted text directly on a page stays as the page's
sourceLinechildren. The semantic reading reads each run as a[box:]without position (Placement); the tree has no implicit box node, because the syntax tree shows only what is written, and where one box ends is a semantic decision made from the kinds of lines. In the semantic tree (Node) that box is a container withgenerated: 'unplaced-text'and no brackets in the source. - The tree and diagnostics round-trip through JSON.
- Test: Unit tests cover each node type, field, range, and JSON round trip. Compare the two tables with
tree.ts. Fail if a node, field, internal value, or line type occurs in only one source, or an internal value appears in a public field list. The bundled examples must produce only listed node and line types.
Principle: The syntax tree keeps source positions and the structure written there. Semantic interpretation is a separate step.
- Every node has a
- The Koma AST follows unist. How to write Nothing is written by hand; unist tools (such as
unist-util-visit) walk the tree as it is. Details- Each node type is built on
@types/unist'sNode,ParentandLiteral, and the type checker ensures it.PointandPositionare unist's, andoffsetis always present. As unist intends,dataholds only line information for rebuilding the tree, never canonical values of the language (internal and not promised, Syntax tree nodes).
- unist tools (
unist-util-visit,unist-builder,unist-util-remove-position) can walk it, build it and strip positions. Positions are computed by an index of its own (positionIndex), which answers asvfile-locationdoes.
- App: Use type and test dependencies without adding runtime dependencies.
- Test: Unit tests compare unist traversal with the internal walk, verify that removing positions retains the other fields, and compare every position of arbitrary strings with
vfile-location. - Principle: Reuse the unist node and position conventions.
- Each node type is built on
- The promised shape of the Koma AST has a version, the AST version, and any change to that shape takes the next one. How to write Nothing is written by hand.
koma parse --jsonnames the version asastVersionand the library exports it asAST_VERSION; a tool checks the number before it reads the tree. Details- The promised shape is what Syntax tree nodes to Value kinds in the tree list: the node kinds, their fields, the kinds of
sourceLine, of values and of list items, and the positions. A change to any of them, even an added field or kind, takes the next whole number.data, the readable output and the parser's internals change without it. - Version
nhas its JSON Schema athttps://komalang.org/schemas/koma-parse-v<n>.schema.json, with every node closed, so that a tool can tell a version it does not know from a broken tree. A published version's schema never changes; it stays in the package and on the site beside later ones. - The AST version is separate from the language version: a release that changes what notation means may keep the tree's shape, and the other way round.
- The current version is 1.
- Test: Unit tests fix the shape of version 1's schema, compare the node fields and kinds
packages/koma/index.d.tsdeclares with the schema's, comparekoma parse --jsonwith the library's tree lessdata, and pin the output for a small source, positions and all.
Principle: 3 (make conditions visible). The tree's shape changes only with a number a reader can see and check, and a closed schema at that number's address says exactly what the number means; an open schema would let a change pass unseen.
- The promised shape is what Syntax tree nodes to Value kinds in the tree list: the node kinds, their fields, the kinds of