Xisp S-Expression Grammar

^ Up

The grammar is available in several different formats:

XBNF notation

The XBNF grammar specification uses a BNF-like notation with PEG-like semantics:

The last point makes the notation simple to translate to code.

Limitations outside the grammar

The following limits are not represented in the grammar:

At-quoted strings

The mechanism of at-quoted strings is not represented in any of the grammars, since it essentially has 256 variants. Representing it sanely in a grammar requires the ability to save and reference variables.

Stream-parsing strategy

The parser consumes one Unit from the input stream every time it's called; it returns the Datum therein if found, or else it returns the Xisp EOF token.

Since a Datum is not self-terminating, the parser must read beyond it to realize that it has ended (if not followed by the EOF). Thus, it will consume one more Blank following the Unit that it parsed. If this Blank is a comment, it will be consumed entirely, ensuring that parsing resumes properly on a subsequent parser call on the same input stream, without needing to store any state in between.

Since comments of type SkipUnit are likewise not self-terminating, an arbitrary number of chained SkipUnit comments may need to be consumed before the parser is finally allowed to return.

The following illustration shows the positions at which the parser will stop consuming input when called repeatedly on the same input stream. The dots represent the extent of each Unit being parsed, while the caret points at the last byte the parser will consume in that parse cycle.

foo (bar)[baz] foo;~bar foo;~bar;~baz;~bat foobar
...^..........^...     ^...               ^......^

Notice how, in the fourth cycle, the parser is forced to consume all commented-out units before it can return, since it would otherwise leave the stream in an inappropriate state.