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.
- Lines: the grammar reads the document as lines, each with perhaps a label, and its text. It knows nothing else.
- 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.
- 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:
- A document is lines. A line may start with a label:
#,+,!tip,Note:. The rest is its text. - What a label means comes from a definition, one line, in a notation sheet or in the document:
!idea ... => callout idea 🌱. - The everyday forms leave labels out. A line whose first word is not a defined label has the empty label, and the empty label's default definition makes it a paragraph, as if it had a
¶label. -
A label's definition says how its line gathers lines into a block:
- a paragraph gathers the lines with empty labels after it, up to a blank line or a line with a defined label;
- a heading usually has only its own line, but it may gather lines with empty labels in the same way;
- a fence gathers every line up to a line with the matching fence label;
- a list item gathers its own lines, and items with the same label, across blank lines, are gathered into a list block, which holds them;
- a block that holds blocks, such as a tree, a callout or a list item, gathers nested blocks, which may hold blocks of their own; by convention MyMark marks each line of a nested level with a bar label,
|.
- Inline notations in a block's text are definitions too:
*...* -> emphasis. -
A block's label also says which grammar reads its content:
- paragraphs, headings and most other blocks read their content with the inline grammar;
- a fenced block of plain text, and a leaf of a tree, take their content as it is, verbatim;
- a label may define a grammar of its own.
- Definitions come in layers: a boot sheet says which sheets to read and what left-out labels mean; then the standard sheet, std.mym, with notations that mimic Markdown and MyMark's own; then any sheet of your own on top.
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'
- The bar is a label like any other. In
| | gammathe label is the first bar, and the text is| gamma, which has a bar of its own; and a bar alone is a line with that label and no text. - A bar must be followed by a space to be a label alone.
|x|is one label, of three parts, and means nothing unless it is defined. - A line that starts with a space has no label: only text.
- A definition is a line too: its label,
.hi(...), then the definition's shape.
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:
- 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. - 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.
- 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:
| Kind | What the line gathers | For |
|---|---|---|
| alone | nothing: the line is the block | a ruler line (---) |
| lines | the lines with the empty label after it, up to a blank line or a line with a defined label | a paragraph, a heading, most blocks |
| a run | blocks with the same meaning after it, across blank lines, into one block that holds them | list items, into a list |
| to a closer | every line up to its matching closing line | a fence |
| nested | the bar lines after it, at the next level: each line's text after its first bar is a line of the content | a 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:
- inline: MyMark's inline grammar, units and the elements the inline definitions find; for paragraphs, headings, list items' text and most blocks;
- MyMark: the content is read again as a document of its own, lines, labels, blocks, by the same rules; for a block that holds blocks, so nesting is this, recursion through content;
- verbatim: taken as it is, not read; for code, and for a tree's leaves;
- its own: a grammar the definition names; for a tree, the tree-line grammar, or a diagram's.
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 stands | It means | So the everyday form |
|---|---|---|
| on a line that starts a block | a paragraph, ¶ | a bare line is a paragraph |
| on a line gathered by a block above, with no bar | a 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 items | the list block, List: | a list needs no label |
| at the root of a block of bar lines | an empty root, which std makes a table, if it is one | a Markdown table needs no label |
| on a tree's root, moved up to its label's line | the box is left out, and the outer bars are the tree's spine | Tree: 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'
- The paragraph's first word,
A, is not a defined label, so its line has the empty label: a paragraph. - The two
-items are a run, gathered into one list. - The table is Markdown's, and the most irregular thing here. Its first row is a bar line with no block above it, so a block with nothing at its root; its row of dashes,
|---|---|, is a label of its own; and its last row is that line's bar line. std's table function reads the pattern as it falls, and makes one table of it.
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
- The core, built into the translator, and not a sheet: the line grammar; the ways of gathering lines (alone, lines, to a closer, a run, nested); the ways of reading content (inline, MyMark, verbatim, a named grammar); the forms of definition; and reading the sheets.
- The boot sheet,
boot.mym, read first, definitions like any others: the empty label's meanings, where elision leaves it; the bar label (a line of the content of the block above); the fold;sheets:anddefinitions:. A MyMark that does not mimic Markdown at all would be the core and boot alone. - The standard sheet,
std.mym: Markdown's notations and MyMark's own, the fence, lists,List:andTree:, and what a block of bar lines with nothing at its root means (a Markdown table, if it is one). - An author's sheets, on top of std, or of boot.
Markdown within the model
Markdown's notations are std's definitions, not the model's:
- Its lists are runs: items with the same label, across blank lines.
- Its fence is a label that gathers lines to a closer, with verbatim content. Because block definitions count from the next line on, and std's from the first, a definition-shaped line inside a fence is never read as one.
- Its table is a block of bar lines with nothing at its root; its row of dashes,
|---|---|, is a line with a label of its own, and its rows after it that line's bar lines. std's table function reads the pattern as it falls and makes one table of it.
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:
- 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). - The boot sheet,
boot.mym, is read first:¶(what the empty label means), the bar, the fold,sheets:anddefinitions:. - A document's block definitions count from their own line down; inline definitions and references, for the whole document.
- 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. - List items and footnotes run across blank lines;
List:andTree:are std's,Tree:read by the tree-line grammar,treeline.peg.
Not yet, or not quite as the model has it:
- A fold in the inline grammar. A fold joins its line to the one before, but each line's text is still read on its own, so an inline element cannot yet run across a fold.
- True spans. A block's content is read again from its lines' text put together, not from spans of the input; the array tree, which would keep them, is pPEG's document first.
- Hard-wrapped Markdown. A sheet that makes a bare continuation line a fold, not a new line, is not written.
- Quotes. Consecutive
>lines are one quote, and a blank line ends it: quotes are nested blocks, not runs, as in Markdown.
Still open
- The array tree's own design: a name, depth against size, a description independent of Python.
- Elsewhere in the plan: the tag wildcard
.?(...), and what.(...)should mean. - Later:
¶and§with tags (¶14,§2), for numbered paragraphs and sections in formal documents.