Reference

Version 0.0.0, in development. The first published version will be 0.1.0.

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 type and its source range position. A node that holds source lines keeps, in data, the scanner's record of each line (so the semantic reading can rebuild lines from the tree alone). data is 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, never data.
    typeParentFieldsInternal (not promised)Meaning
    rootyeschildrendata.openFence (the mark of a fence left open at the end, else null)The whole document. Its direct children are the document settings and blank lines and comments before the first page
    pageyeschildren, body (from after the delimiter and complete settings to the next page), boundary (the +++ line; absent on the implicit first page)noneOne page. Pages divide the source without gaps (the line break just before the next page belongs to this page)
    settingsyesscope (document or page), body (inside the delimiters), children (sourceLine)noneA closed YAML settings block. Values are interpreted separately
    notesyesbody, closed, children (sourceLine)noneNotes ("""). Elements and delimiters inside are isolated as content. Unspoken [[ blocks are part of the body (Elements)
    memoyesattributes (tagToken, each an attribute assignment of the opening ::: line), body, closed, children (sourceLine)noneA memo (::: key=value … :::) on a page. Its text is isolated as content and never output (Memos)
    elementyestagName (the name, else null), children, opening and closing (a multi-line element's opening and closing lines; closing has the bracket's position bracket and whether it is on the body's last line, inline)data.line and data.close (the scanner's records of the opening and closing lines)A bracketed element
    elementTagyeschildren (tagToken)noneAn element's tag
    tagTokenno (value)kind (selector, attributeAssignment, bareWord; selector is a tag name with any joined shorthands or a standalone ID or class shorthand; bareWord is a bare word outside the tag's grammar, kept as written, Elements), value (the word as written), and for attribute assignments separator (the position of =) and valueKind (the kind of the value as written), and for a list items (Value kinds in the tree)noneA 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 needed finding, fence (code, math), settingValueAtdata.line (the scanner's record of the line)One source line
    rawno (value)valuenoneThe source of an element's body

    A sourceLine's kind is one of these. Element lines (a one-line element, an opening line, a closing line) never become a sourceLine; they are the element and its opening and closing.

    kindMeaning
    settingA key: value line inside settings delimiters. settingValueAt is 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 inside
    memoA memo's ::: lines and the lines inside (Memos)
    fencedA line inside a code fence or $$, and the fence lines. fence is code or math (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.start and position.end are half-open. Lines and columns count from 1, offset from 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 sourceLine children. 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 with generated: '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.

  • 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's Node, Parent and Literal, and the type checker ensures it. Point and Position are unist's, and offset is always present. As unist intends, data holds 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 as vfile-location does.
    • 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.
  • 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 --json names the version as astVersion and the library exports it as AST_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 n has its JSON Schema at https://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.ts declares with the schema's, compare koma parse --json with the library's tree less data, 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.