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.
Read a text with a profile
Section titled “Read a text with a profile”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.
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
-x ^ 2✓ The printed text reads back to the same document.| Text | Binds | Place | Document |
|---|---|---|---|
| or | 1 | between terms, one flat chain | or |
| and | 2 | between terms, one flat chain | and |
| not | 3 | before its term | ops.not |
| == | 4 | between two terms, no chain | ops.eq |
| != | 4 | between two terms, no chain | ops.ne |
| < | 4 | between two terms, no chain | ops.lt |
| <= | 4 | between two terms, no chain | ops.le |
| > | 4 | between two terms, no chain | ops.gt |
| >= | 4 | between two terms, no chain | ops.ge |
| + | 5 | between terms, groups to the left | ops.add |
| - | 5 | between terms, groups to the left | ops.sub |
| * | 6 | between terms, groups to the left | ops.mul |
| / | 6 | between terms, groups to the left | ops.div |
| % | 6 | between terms, groups to the left | ops.mod |
| ▸ - | 7 | before its term | ops.neg |
| ▸ ^ | 8 | between terms, groups to the right | ops.pow |
The profile decides the tree
Section titled “The profile decides the tree”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.
An issue has a span
Section titled “An issue has a span”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.