Lexical Format

Characters

The text format assigns meaning to source text, which consists of a sequence of characters. Characters are assumed to be represented as valid Unicode code points.

\[\begin{split}\begin{array}{llll} \def\mathdef2484#1{{}}\mathdef2484{character} & \href{../text/lexical.html#text-char}{\mathtt{char}} &::=& \def\mathdef2542#1{\mathrm{U{+}#1}}\mathdef2542{00} ~|~ \dots ~|~ \def\mathdef2543#1{\mathrm{U{+}#1}}\mathdef2543{D7FF} ~|~ \def\mathdef2544#1{\mathrm{U{+}#1}}\mathdef2544{E000} ~|~ \dots ~|~ \def\mathdef2545#1{\mathrm{U{+}#1}}\mathdef2545{10FFFF} \\ \end{array}\end{split}\]

Note

While source text may contain any Unicode character in comments or string literals, the rest of the grammar is formed exclusively from the characters supported by the 7-bit ASCII subset of Unicode.

Tokens

The character stream in the source text is divided, from left to right, into a sequence of tokens, as defined by the following grammar.

\[\begin{split}\begin{array}{llll} \def\mathdef2484#1{{}}\mathdef2484{token} & \href{../text/lexical.html#text-token}{\mathtt{token}} &::=& \href{../text/lexical.html#text-keyword}{\mathtt{keyword}} ~|~ \href{../text/values.html#text-int}{\def\mathdef2526#1{{\mathtt{u}#1}}\mathdef2526{N}} ~|~ \href{../text/values.html#text-int}{\def\mathdef2532#1{{\mathtt{s}#1}}\mathdef2532{N}} ~|~ \href{../text/values.html#text-float}{\def\mathdef2538#1{{\mathtt{f}#1}}\mathdef2538{N}} ~|~ \href{../text/values.html#text-string}{\mathtt{string}} ~|~ \href{../text/values.html#text-id}{\mathtt{id}} ~|~ \def\mathdef2546#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef2546{(} ~|~ \def\mathdef2547#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef2547{)} ~|~ \href{../text/lexical.html#text-reserved}{\mathtt{reserved}} \\ \def\mathdef2484#1{{}}\mathdef2484{keyword} & \href{../text/lexical.html#text-keyword}{\mathtt{keyword}} &::=& (\def\mathdef2548#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef2548{a} ~|~ \dots ~|~ \def\mathdef2549#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef2549{z})~\href{../text/values.html#text-idchar}{\mathtt{idchar}}^\ast \qquad (\mbox{if occurring as a literal terminal in the grammar}) \\ \def\mathdef2484#1{{}}\mathdef2484{reserved} & \href{../text/lexical.html#text-reserved}{\mathtt{reserved}} &::=& \href{../text/values.html#text-idchar}{\mathtt{idchar}}^+ \\ \end{array}\end{split}\]

Tokens are formed from the input character stream according to the longest match rule. That is, the next token always consists of the longest possible sequence of characters that is recognized by the above lexical grammar. Tokens can be separated by white space, but except for strings, they cannot themselves contain whitespace.

The set of keyword tokens is defined implicitly, by all occurrences of a terminal symbol in literal form \(\def\mathdef2550#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef2550{keyword}\) in a syntactic production of this chapter.

Any token that does not fall into any of the other categories is considered reserved, and cannot occur in source text.

Note

The effect of defining the set of reserved tokens is that all tokens must be separated by either parentheses or white space. For example, \(\def\mathdef2551#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef2551{0\$x}\) is a single reserved token. Consequently, it is not recognized as two separate tokens \(\def\mathdef2552#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef2552{0}\) and \(\def\mathdef2553#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef2553{\$x}\), but instead disallowed. This property of tokenization is not affected by the fact that the definition of reserved tokens overlaps with other token classes.

White Space

White space is any sequence of literal space characters, formatting characters, or comments. The allowed formatting characters correspond to a subset of the ASCII format effectors, namely, horizontal tabulation (\(\def\mathdef2554#1{\mathrm{U{+}#1}}\mathdef2554{09}\)), line feed (\(\def\mathdef2555#1{\mathrm{U{+}#1}}\mathdef2555{0A}\)), and carriage return (\(\def\mathdef2556#1{\mathrm{U{+}#1}}\mathdef2556{0D}\)).

\[\begin{split}\begin{array}{llclll@{\qquad\qquad}l} \def\mathdef2484#1{{}}\mathdef2484{white space} & \href{../text/lexical.html#text-space}{\mathtt{space}} &::=& (\def\mathdef2557#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef2557{~~} ~|~ \href{../text/lexical.html#text-format}{\mathtt{format}} ~|~ \href{../text/lexical.html#text-comment}{\mathtt{comment}})^\ast \\ \def\mathdef2484#1{{}}\mathdef2484{format} & \href{../text/lexical.html#text-format}{\mathtt{format}} &::=& \def\mathdef2558#1{\mathrm{U{+}#1}}\mathdef2558{09} ~|~ \def\mathdef2559#1{\mathrm{U{+}#1}}\mathdef2559{0A} ~|~ \def\mathdef2560#1{\mathrm{U{+}#1}}\mathdef2560{0D} \\ \end{array}\end{split}\]

The only relevance of white space is to separate tokens, it is ignored otherwise.

Comments

A comment can either be a line comment, started with a double semicolon \(\def\mathdef2525#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef2525{{;}{;}}\) and extending to the end of the line, or a block comment, enclosed in delimiters \(\def\mathdef2523#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef2523{{(}{;}} \dots \def\mathdef2524#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef2524{{;}{)}}\). Block comments can be nested.

\[\begin{split}\begin{array}{llclll@{\qquad\qquad}l} \def\mathdef2484#1{{}}\mathdef2484{comment} & \href{../text/lexical.html#text-comment}{\mathtt{comment}} &::=& \href{../text/lexical.html#text-comment}{\mathtt{linecomment}} ~|~ \href{../text/lexical.html#text-comment}{\mathtt{blockcomment}} \\ \def\mathdef2484#1{{}}\mathdef2484{line comment} & \href{../text/lexical.html#text-comment}{\mathtt{linecomment}} &::=& \def\mathdef2525#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef2525{{;}{;}}~~\href{../text/lexical.html#text-comment}{\mathtt{linechar}}^\ast~~(\def\mathdef2561#1{\mathrm{U{+}#1}}\mathdef2561{0A} ~|~ \mathtt{eof}) \\ \def\mathdef2484#1{{}}\mathdef2484{line character} & \href{../text/lexical.html#text-comment}{\mathtt{linechar}} &::=& c{:}\href{../text/lexical.html#text-char}{\mathtt{char}} & (\mathrel{\mbox{if}} c \neq \def\mathdef2562#1{\mathrm{U{+}#1}}\mathdef2562{0A}) \\ \def\mathdef2484#1{{}}\mathdef2484{block comment} & \href{../text/lexical.html#text-comment}{\mathtt{blockcomment}} &::=& \def\mathdef2523#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef2523{{(}{;}}~~\href{../text/lexical.html#text-comment}{\mathtt{blockchar}}^\ast~~\def\mathdef2524#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef2524{{;}{)}} \\ \def\mathdef2484#1{{}}\mathdef2484{block character} & \href{../text/lexical.html#text-comment}{\mathtt{blockchar}} &::=& c{:}\href{../text/lexical.html#text-char}{\mathtt{char}} & (\mathrel{\mbox{if}} c \neq \def\mathdef2563#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef2563{;} \wedge c \neq \def\mathdef2564#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef2564{(}) \\ &&|& \def\mathdef2565#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef2565{;} & (\mathrel{\mbox{if}}~\mbox{the next character is not}~\def\mathdef2566#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef2566{)}) \\ &&|& \def\mathdef2567#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef2567{(} & (\mathrel{\mbox{if}}~\mbox{the next character is not}~\def\mathdef2568#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef2568{;}) \\ &&|& \href{../text/lexical.html#text-comment}{\mathtt{blockcomment}} \\ \end{array}\end{split}\]

Here, the pseudo token \(\mathtt{eof}\) indicates the end of the input. The look-ahead restrictions on the productions for \(\href{../text/lexical.html#text-comment}{\mathtt{blockchar}}\) disambiguate the grammar such that only well-bracketed uses of block comment delimiters are allowed.

Note

Any formatting and control characters are allowed inside comments.