Specification · Chapter 1 / 7
Lexical structure
How a .koma file is read as characters and lines, before any element is recognised.
Grammar
The individual rules are normative. The grammar outlines the structure. Indentation depth starts at zero, with two spaces per level. Root delimiter lines (===, ---, +++, :::, """) start in column 0.
# Document and pages
Document <- BOM? Preamble Page (PageBreak Page)*
Preamble <- BlankLine* (DocumentSettings BlankLine*)?
DocumentSettings <- DelimiterLine('=') YamlLine* DelimiterLine('=')
Page <- (BlankLine / CommentLine(0))* PageSettings? PageItem*
PageSettings <- DelimiterLine('-') YamlLine* DelimiterLine('-')
PageBreak <- DelimiterLine('+')
PageItem <- Element(0) / Notes / Memo / CommentLine(0) / BlankLine / Recovery / PageText
Notes <- '"""' (HorizontalSpace+ AttributeAssignment)* HorizontalSpace* LineEnd NoteLine* '"""' HorizontalSpace* LineEnd
Memo <- ':::' ':'* (HorizontalSpace+ AttributeAssignment)+ HorizontalSpace* LineEnd MemoLine* ':::' ':'* HorizontalSpace* LineEnd
# Elements and tags
Element(depth) <- Indent(depth) (SingleLineElement / MultiLineElement(depth))
SingleLineElement <- '[' Tag HorizontalSpace* ':' ' '? Content? ']' HorizontalSpace* LineEnd
MultiLineElement(depth) <- '[' Tag AttributeLine(depth)* TagEnd(depth) ' '? Content? LineEnd ContentLine* ElementEnd(depth)
Tag <- TagName? IdShorthand? ClassShorthand* (HorizontalSpace+ Attribute)*
AttributeLine(depth) <- HorizontalSpace* LineEnd Indent(depth + 1) Attribute (HorizontalSpace+ Attribute)*
TagEnd(depth) <- (HorizontalSpace* LineEnd Indent(depth + 1))? HorizontalSpace* ':'
# Attributes and values
Attribute <- IdShorthand / ClassShorthand / AttributeAssignment
IdShorthand <- '#' Id
ClassShorthand <- '.' ClassName
AttributeAssignment <- AttributeName '=' AttributeValue
AttributeName <- Word
AttributeValue <- QuotedString / ValueList / UnquotedScalar
QuotedString <- '"' (Escape / !('"' / '\\' / LineEnd) .)* '"'
UnquotedScalar <- (Dimension / Percentage / Number / HashValue / Word) &(HorizontalSpace / ':' / ']' / LineEnd)
Word <- [A-Za-z] [A-Za-z0-9_-]*
Number <- [+-]? (Digit+ ('.' Digit+)? / '.' Digit+) ([eE] [+-]? Digit+)?
Percentage <- Number '%'
Dimension <- Number Unit
HashValue <- '#' Id
ValueList <- '(' HorizontalSpace* (ValueListItem (HorizontalSpace* ',' HorizontalSpace* ValueListItem)*)? HorizontalSpace* ')'
ValueListItem <- QuotedString / ValueList &(HorizontalSpace* (',' / ')')) / Expression
Expression <- (Parenthesized / !(',' / ')' / '(' / '"' / LineEnd) .)+
Parenthesized <- '(' (Parenthesized / !(')' / LineEnd) .)* ')'
# Common lexical rules
DelimiterLine(c) <- c c c c* ReservedDelimiterSuffix? HorizontalSpace* LineEnd
ReservedDelimiterSuffix <- HorizontalSpace+ (!LineEnd .)+
BlankLine <- HorizontalSpace* LineEnd
CommentLine(depth) <- Indent(depth) ';' (!LineEnd .)* LineEnd
Indent(0) <- ''
Indent(depth + 1) <- ' ' Indent(depth)
HorizontalSpace <- ' ' / '\t'
LineEnd <- '\r\n' / '\n' / '\r'
# Recovery and diagnostics
Recovery <- MisplacedPageSettings / ReservedBlock
MisplacedPageSettings <- DelimiterLine('-')
# Marks a reserved block. Its full extent is described below.
ReservedBlockStart <- ':::' ':'*
- A
+++in column 0 breaks the page even inside an element, code, math, CSS, YAML or notes. Recognize page breaks first and confine other structures to a page (Page boundaries). PageTextis prose written directly on a page. Each consecutive run enters the page's flow as one[box:]with no explicit position. A line beginning with[in column 0 that producesmissing-colonorunclosed-elementfalls outside that prose (Placement).- An ID or class shorthand can also be joined to the tag name. Attribute lines are two spaces deeper than the opening line. The tag-ending
:can follow the last token or stand alone at the same depth as an attribute line (Elements and Tag and content). ContentLineandElementEndfollow the rules for the kind of content. An ordinary multiline element closes with a]on its own line at the opening depth or at the end of the last content line. Comments are recognized at the start of a page or notes line or after structural indentation in an element. Code and math fences and raw element content keep them as text (Page boundaries, Elements, Text inside elements and Code and math are isolated).Recoveryshows the main line-based recovery cases. A---after content is preserved as a line. AReservedBlockis isolated starting at a:::in column 0 that does not open aMemoand ending at its matching closing line or the end of the page. An unquoted attribute value with no lexical kind is preserved, and the attribute's rules determine its interpretation and diagnostic (Document settings, Page settings and The kinds of a value).
Characters and line ends
Encoding and line ends
A .koma file is UTF-8 text. Line ends are read as LF, and the line ends that were read are kept when saving.
How to write
Save as UTF-8. Line ends may be LF, CRLF or CR (Page boundaries).
Details
- A file with mixed line ends is saved with the most common one (LF on a tie).
- A UTF-8 file that began with a byte order mark keeps it when saved.
Code and math fences
Code and math are isolated
Everything inside a code or math fence is content: no element and no page starts there.
How to write
Fence with lines of ` or ~~~ (four or more are fine too; a fence closes at a line of the same character, at least as many as opened it), or with lines of $$.
Example
Output of koma export
Source
[title: Inside a fence is content]
[main:
```
[box: this line is code, not an element]
```
]Details
- The exception is a
+++in column 0, which is a page break even inside a fence (Page boundaries).
Math delimiters
Inline and display math
Math in text is written $...$, and displayed math $$ ... $$.
How to write
Math in text is $x^2$; displayed math is enclosed in $$. The delimiters of displayed math may stand on their own lines or on the formula's line (the recommended form is in Text inside elements).
An escaped dollar sign
\$ is not a math delimiter but a literal $.
How to write
To write $ as a character, as in prices, write \$ (\$5).
Attribute values
The kinds of a value
An attribute's value has one of seven kinds, decided by how it is written; only a value that is not a string or a list may go unquoted.
How to write it
| Kind | Form | Examples |
|---|---|---|
| Word | a letter, then letters, digits, - and _ | end, elbow, stix2, fig-3 |
| Number | a CSS number (a sign, a fraction, an exponent) | 2, -0.5, .5, 1e-3 |
| Percentage | a number and % | 40%, -5% |
| Length | a number and a CSS unit | 240px, 0px, 2em |
# value | #, then letters, digits, - and _ (as an #id) | #plot, #2d287f, #図1 |
| String | always double-quoted | "assets/fig.svg", "Distance from the nest", "sin(x); cos(x)" |
| List | values apart by commas in parentheses | (0, 40), ("a", "b") |
[image x=10% w=240px src="assets/decay.svg" alt="How sound fades" crop=(0, 0, 10%, 0):]
[box #clap x=10% y=40% w=20%:]
[box #wall x=70% y=40% w=20%:]
[line from=#clap to=#wall arrow=end:]Details
- A number is a CSS
<number>: a sign+or-,1.5and.5(1.is not a number), and an exponent witheorE. A dimension consists of a number and a CSS unit (lengths, angles, times, resolutions andfr; case does not matter). Which units an attribute takes stays as Elements says (%andpxforx,wand the like,pxfor a line's ends); any other CSS unit isbad-unit. A number with a unit CSS does not have (3foo) has no kind. - A
#value: a colour (#and 3, 4, 6 or 8 hexadecimal digits, as in CSS) and a reference to an element (from=#a) are the same#value as written, and the attribute decides which it is (as CSS's hash).from=#abcreads as a reference, never a colour. - Only a word, a number, a percentage, a dimension or a
#value may go unquoted. A value starting with:, a blank,/or., or holding Japanese, an emoji, a URL or an expression (drive(t)) is a string and is quoted. An unquoted value of no kind (src=assets/a.svg,alt=日本語,marker=⚠,f=drive(t)) keeps its source, reads as a string, is reported asinvalid-unquoted-scalar(warning) and has one fix that adds the quotes. The fix keeps what is read, sokoma check --writeandkoma format --writemake it. On a length attribute, a value of no kind is reported bymissing-unitorbad-unit. - A quoted value on an attribute of a set kind (a length; a number such as
step,rotateorweight; a word such asarrow; an attribute that takes a list) reads as it does unquoted (w="240px"), and the formatter (The standard form) takes the quotes off. On an attribute with no definition (data-and the like), a quoted value is kept apart as a string. - A number written with a sign, a fraction or an exponent (
+1,.5,1e3) on a text attribute (label,idand the like) reads as the text it was written as. - Escapes inside a string are as in JSON (an invalid one is
bad-value).style="…"stays a string of CSS declarations.
Lists of values
Several values are written as a list: values apart by commas in parentheses.
How to write it
[curve f="x -> sin(x)" domain=(-pi, pi) range=(-1, 1):]
[table src="assets/results.csv" widths=(1, 3, 2):]
[line .smooth points=((430, 500), (760, 300), (1160, 300)):]Details
- A list is written in parentheses only. Square brackets are never used for values, and there is no other form for a tuple.
- An item is a value of a kind (a word, a number, a percentage, a dimension, a
#value or a string, The kinds of a value) or a list. Blanks around a comma are free; the formatter writes(a, b)(one space after each comma, none inside the parentheses). An empty list()and a list of one item(a)may be written; the attribute decides whether they mean anything. - An unquoted value starting with
(is a list when its first parenthesis closes at its end, or never closes. A value whose parenthesis closes before its end ((1-x)*x,(draft)v2) is no list but a value of no kind (The kinds of a value). - An item of more than one word (
2 * pi,-pi) is an expression, read only by the attributes that take expressions (domainandrange). Parentheses inside an expression pair ((0, (1 + 2) * pi)). An expression on any other attribute isbad-value. - An item of
cropis a percentage from 0, or0; an item ofwidthsis a number above 0 (both written as CSS numbers). - Each attribute takes its own number of items; another number is reported as
bad-value, the attribute has no effect and the source is kept:croptakes 4 (top, right, bottom, left, in the order of CSSinset()),domainandrange2,pointstwo or more lists of two numbers (points=((0, 0), (100, 50))),widthsone or more (as many as the table has columns). - A trailing comma (
(a, b,)), an empty item ((a, , b)) and a parenthesis left open are reported asbad-value; the attribute has no effect and the source is kept. A list left open ends at a:or]outside quotes, or at a blank before the nextkey=, so the attributes after it are read as their own. Neither the app nor MCPsetAttributesrewrites a tag holding a list left open. - The
:that ends a tag is the first:outside quotes and lists (a:or]in a string inside a list is the value's). A list does not run across lines. - The attributes that take a list:
crop=(top, right, bottom, left),widths=(…),domain=(left, right),range=(bottom, top),points=((x, y), …)(pairs of unitless logical px). - There are no dictionaries (
{…}). Pairs of a name and a value are attributes, and nesting is written with child elements. A{at the start of an attribute's value is reserved and isbad-valueunquoted.