The formal model

Done 26 October 2026, and built the same day, but for what "What is built" lists: everything a label does is its definition, the bar and the fence included, and a definition says how its label gathers lines into a block. Built from Peter's rewrite of "In brief". Earlier versions are in the archive.

A MyMark document is read in three steps, and one idea keeps everyday writing simple: elision, the leaving out of a formal notation that is always there to be written.

  1. Lines: the grammar reads the document as lines, each with perhaps a label, and its text. It knows nothing else.
  2. Blocks: the definitions say what each label means: how its line gathers the lines after it into a block, and how that block's content is to be read.
  3. Content: each block's content is read by the grammar its definition chooses: MyMark's inline grammar, MyMark itself again for a block that holds blocks, none at all for verbatim text, or a grammar of its own.

In brief

For MyMark's users, and for anyone defining notations of their own:

The rest of this document is the formal model, for those who want the architecture.

Reading the diagrams

Trees are shown as tree-line diagrams, as pPEGpy prints them. Each line is a node; the bars at its left are its depth, and its children are the lines under it with one more bar. A leaf shows its text in quotes.

add
| int '1'
| mul
| | int '2'
| | int '3'

The same tree can be drawn fatter: below a chosen depth, the trunk, a node's children run on along its line, : going down to a child, , across to a sibling, ; back up. At trunk 0 the whole tree is one line:

add: int '1', mul: int '2', int '3'

The diagrams of lines below are the real output of the grammar given here. Every node also has its span of the input, which a diagram does not show. A fuller guide is Tree diagrams, tree-diagrams.mym.

Lines

The grammar reads the document as lines. A blank line is empty, or holds only space. Any other line has perhaps a label, its first token if that has a label's shape, and then its text. A line that starts with white space has no label.

This is the whole of it, a pPEG grammar (experiments/model/lines.peg):

# The model's line grammar: a document is lines. Each line is blank, or
# has perhaps a label, and its text. It knows no definitions, no blocks
# and no nesting: the bar is a label like any other (in `| | x` the
# label is `|` and the text `| x`), and what it means is a definition.

doc     = (blank / line)*

blank   =: [ \t]* nl / [ \t]+ !.
line    := (label (sp (defn / text))? / text) eol

label   := lead word? suffix? / word suffix? / suffix
lead    =: sym+
word    =: [a-zA-Z0-9]+
suffix  =: (sym / bag)+
bag     : '(' ~[()\n\r]* ')' / '[' ~('[' / ']' / [\n\r])* ']'
sym     : ~[a-zA-Z0-9 \t\n\r(\[]

defn    = (dots sp)? arrow sp name (sp args)? &eol
dots    =: '...'
arrow   =: '=>' / '->'
name    =: [a-zA-Z] [a-zA-Z0-9_]*
args    =: ~[\n\r]+

text    =: ~[\n\r]+
sp      : [ \t]+
eol     : nl / !.
nl      : '\n' / '\r' '\n'?

It knows no definitions, no blocks and no nesting.

These lines:

## A heading

A paragraph is a line of text, or several. A blank line ends it.

- a bullet
- another bullet

are read as:

doc
| line
| | label
| | | lead '##'
| | text 'A heading'
| blank '\n'
| line
| | label
| | | word 'A'
| | text 'paragraph is a line of text, or several. A blank line ends it.'
| blank '\n'
| line
| | label
| | | lead '-'
| | text 'a bullet'
| line
| | label
| | | lead '-'
| | text 'another bullet'

Each line is a node: its label, if it has one, and its text. A blank line is a node too.

Labels

A label has up to three parts: a lead of symbols, a word of letters and digits, and a suffix of symbols and bags: #, ##intro, !info, Note:, [^1]:, .hi(...), |. The grammar knows only this shape.

A prose line's first word has the shape too (A, in the paragraph above). Whether it is a label is for the definitions: a first word that is not a defined label is no label at all, and its line has the empty label, written as nothing.

A few lines that show the edges of the shape:

alpha
| beta
| | gamma
|
|x| is the size
 a line that starts with a space
.hi(...) -> mark

are read as:

doc
| line
| | label
| | | word 'alpha'
| line
| | label
| | | lead '|'
| | text 'beta'
| line
| | label
| | | lead '|'
| | text '| gamma'
| line
| | label
| | | lead '|'
| line
| | label
| | | lead '|'
| | | word 'x'
| | | suffix '|'
| | text 'is the size'
| line
| | text ' a line that starts with a space'
| line
| | label
| | | lead '.'
| | | word 'hi'
| | | suffix '(...)'
| | defn
| | | arrow '->'
| | | name 'mark'

Definitions

A definition is a line: a label, then an arrow and a function name, with arguments if it needs them. A block definition gives a label its meaning, # ... => heading 1; an inline definition is a pattern, *...* -> emphasis; a reference gives a label an address, [CM]: https://commonmark.org.

They are read in this order:

  1. The sheets first: those on the command line, then those the document names with sheets:. All they define is in force from the document's first line: the sheets stand above the document.
  2. The document, top down, once. A block definition counts from the next line on, and a redefinition replaces the old one from there. Every other definition, an inline pattern or a reference, counts for the whole document, above it and below; a document's own wins over a sheet's.
  3. Then the content of every block, with all the inline definitions together.

So a block label's meaning at any line is its nearest definition above that line, in the document or else in the sheets. A label with no definition above means nothing.

Only the top level counts. A definition among the lines of a block is that block's content: it can be shown, quoted or explained without taking effect. A block whose content is read as MyMark again, such as a demo, follows the same rules inside, starting from the definitions in force at its own line; its own definitions stay inside it. And a fenced block's lines are never read as definitions at all.

A label is looked up as written, or by example: ? after the symbols stands for any word (##? for ##intro), and ... for any text with no space ([^...]: for [^1]:).

Gathering lines into blocks

What a label's definition chooses first is how its line gathers the lines after it. There are a few ways, built in; each definition picks one:

KindWhat the line gathersFor
alonenothing: the line is the blocka ruler line (---)
linesthe lines with the empty label after it, up to a blank line or a line with a defined labela paragraph, a heading, most blocks
a runblocks with the same meaning after it, across blank lines, into one block that holds themlist items, into a list
to a closerevery line up to its matching closing linea fence
nestedthe bar lines after it, at the next level: each line's text after its first bar is a line of the contenta callout, a list item, a tree, a demo

A fold is a line labelled : : it carries on the line before it, as one line, with a soft break.

The bar is no part of the grammar: it is a label whose definition, in the boot sheet, is "a line of the content of the block above", which is what a reader takes it to mean. That the lines of a block can hold blocks of their own, which hold more, is not a further rule: it emerges, and what emerges is a tree, the common structure of all structured text read by a grammar. A bar line with no block above it, after a blank line, is a block of its own with nothing at its root, and what that means is a definition too (in std, a table, if it is one).

A run ends at the first block that is not one of its own; blank lines do not end it. Two runs in a row, with nothing between, are written formally (see Elision).

Reading a block's content

What a label's definition chooses second is how its block's content is read:

Nesting, by example

!info A box
| with a second line
|
| + one
| | + one point one
| + two

is read by the grammar as lines, each bar a label:

doc
| line
| | label
| | | lead '!'
| | | word 'info'
| | text 'A box'
| line
| | label
| | | lead '|'
| | text 'with a second line'
| line
| | label
| | | lead '|'
| line
| | label
| | | lead '|'
| | text '+ one'
| line
| | label
| | | lead '|'
| | text '| + one point one'
| line
| | label
| | | lead '|'
| | text '+ two'

The callout !info gathers the bar lines after it (nested), and reads its content as MyMark again. Its content is each line's text after its first bar:

with a second line

+ one
| + one point one
+ two

which the same grammar reads as:

doc
| line
| | label
| | | word 'with'
| | text 'a second line'
| blank '\n'
| line
| | label
| | | lead '+'
| | text 'one'
| line
| | label
| | | lead '|'
| | text '+ one point one'
| line
| | label
| | | lead '+'
| | text 'two'

and so on down: the + one item gathers its own bar line, whose content is + one point one. Nesting is recursion through content, and the bar never needs to be counted.

The formal block, and elision

Written out in full, with nothing left out:

¶ A paragraph's first line,
| and its second, after a hard line break,
: and this carried on, after a soft one.
List:
| + first
| + second
Tree:
| date
| | day '2'
| | month '3'

The everyday forms leave a label out, and what is left out is always a label; the empty label stands in its place. What the empty label means depends on where it stands, by the boot sheet's definitions:

Where the empty label standsIt meansSo the everyday form
on a line that starts a blocka paragraph, ¶a bare line is a paragraph
on a line gathered by a block above, with no bara new line of that block, as a bar line would be (a sheet may make it a fold, for hard-wrapped Markdown)a paragraph's second line needs no bar
around a run of list itemsthe list block, List:a list needs no label
at the root of a block of bar linesan empty root, which std makes a table, if it is onea Markdown table needs no label
on a tree's root, moved up to its label's linethe box is left out, and the outer bars are the tree's spineTree: date, then the tree's lines

So these two are the same paragraph, the first with nothing left out:

¶ A paragraph's first line,
| and its second.
A paragraph's first line,
and its second.

From lines to blocks

The example from Lines, with a Markdown table added at its end:

## A heading

A paragraph is a line of text, or several. A blank line ends it.

- a bullet
- another bullet

| x | y |
|---|---|
| 1 | 2 |

is read by the grammar as lines:

doc
| line
| | label
| | | lead '##'
| | text 'A heading'
| blank '\n'
| line
| | label
| | | word 'A'
| | text 'paragraph is a line of text, or several. A blank line ends it.'
| blank '\n'
| line
| | label
| | | lead '-'
| | text 'a bullet'
| line
| | label
| | | lead '-'
| | text 'another bullet'
| blank '\n'
| line
| | label
| | | lead '|'
| | text 'x | y |'
| line
| | label
| | | lead '|---|---|'
| line
| | label
| | | lead '|'
| | text '1 | 2 |'

and the definitions make blocks of them (drawn by hand, as this stage is not built; trunk 3):

doc
| heading
| | text 'A heading'
| paragraph
| | text 'A paragraph is a line of text, or several. A blank line ends it.'
| list
| | item: text 'a bullet'
| | item: text 'another bullet'
| table
| | row
| | | cell 'x'
| | | cell 'y'
| | row
| | | cell '1'
| | | cell '2'

Inline content

An inline element never crosses a line end; but a fold is not a line end. In a block's content a fold is a space, so an element can run across it. The inline grammar reads the block's own span of the input, a fold's line end and its colon read as a space, so every inline node keeps a true span. A bar line, a new line of its block, is always a hard line end, and an element cannot run across one.

Trees in documents

A tree diagram in a document is a Tree: block: nested, its content read by the tree-line grammar, its leaves verbatim. The author may write the root inside the box, as above, or move it up to the label's line:

Tree:  date
| day '2'
| month '3'

Text after the label means the root has moved up, and the outer bars are the tree's spine; no text means the bars only mark the block. The diagram may be drawn tall or fat, as pPEGpy's stree(trunk) lays it out.

Layers: the core, boot, std

Markdown within the model

Markdown's notations are std's definitions, not the model's:

What Markdown lacks is the tree. It nests only lists, by indentation, and quotes, by >, each with rules of its own; MyMark's blocks nest anywhere, by one convention, the bar, and a tree is the shape of every MyMark document. Trees are central to MyMark's design, and quite absent from Markdown's.

What is built

MyMark's translator was rebuilt to this model on 26 October 2026, in five stages (reports/build-plan.md), each keeping every test and page as it was, or changing it on purpose:

  1. Each block function declares how its label gathers lines (gathers: alone, lines, nested, closer, run) and how its content is read (reads: inline, mym, verbatim, or a grammar's name).
  2. The boot sheet, boot.mym, is read first: ¶ (what the empty label means), the bar, the fold, sheets: and definitions:.
  3. A document's block definitions count from their own line down; inline definitions and references, for the whole document.
  4. The grammar, block.peg, reads only lines; the lines grammar shown above is a simpler cousin of it. A fence is found by its definition's kind, closer, in the same pass down the document as the definitions, and a reference by its shape.
  5. List items and footnotes run across blank lines; List: and Tree: are std's, Tree: read by the tree-line grammar, treeline.peg.

Not yet, or not quite as the model has it:

Still open