Skip to content

Syntax

A document is data. A text is one way to write it. The syntax kit of the core gives one parser and one printer for each text syntax. A profile tells them the syntax: the operators with their precedence and grouping, the literals, the references and the call forms.

parse(profile, text) gives a document, or the issues of the text. Each issue has an offset, a length and a message. print(profile, doc) writes the document with the fewest parentheses that keep its tree.

The diagram reads the text with the selected profile. It shows the tree of the document, the printed text, and the operators of the profile. It then reads the printed text again, to check that the round trip gives the same document.

Profile
Examples

STD_SYNTAX of the core. ^ groups to the right and binds more tightly than a minus sign. % is the remainder.

The profile Standard reads the text as one document. The tree has 4 nodes.

The document

  • ops.neg
  • 0: ops.pow
  • 0: ref names.x
  • 1: 2
The printed text-x ^ 2✓ The printed text reads back to the same document.
The operators of the profile, from the loosest to the tightest
TextBindsPlaceDocument
or1between terms, one flat chainor
and2between terms, one flat chainand
not3before its termops.not
==4between two terms, no chainops.eq
!=4between two terms, no chainops.ne
<4between two terms, no chainops.lt
<=4between two terms, no chainops.le
>4between two terms, no chainops.gt
>=4between two terms, no chainops.ge
+5between terms, groups to the leftops.add
-5between terms, groups to the leftops.sub
*6between terms, groups to the leftops.mul
/6between terms, groups to the leftops.div
%6between terms, groups to the leftops.mod
▸ -7before its termops.neg
▸ ^8between terms, groups to the rightops.pow
One parser and one printer read each profile as data. Select a profile, and the same text gives another tree or an issue. A wavy red mark with a number is the span of an issue, and a red bar is an issue between two characters. A row of the table with ▸ is an operator that the text uses.

The text -x ^ 2 is the same in each profile. The tree is different.

  • In the standard profile, ^ binds more tightly than a minus sign. Thus the text is -(x ^ 2).
  • In the spreadsheet profile, a minus sign binds more tightly than ^. Thus the text is (-x) ^ 2.
  • The world profile has no minus sign before a term. Thus the text gives an issue.

A profile is data, thus a new text syntax needs no new parser. A language of a host program becomes one more profile, and the same tests examine it.

A bad text gives no document. It gives each issue with its place, in the order of the offsets. An issue with no length marks a place between two characters, for example a term that is missing. parse gives at most 20 issues for one text.