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 (Section 2.4) scalar values.

\[\begin{split}\begin{array}{llll} \def\mathdef3318#1{{}}\mathdef3318{source} & \href{../text/lexical.html#text-source}{\mathtt{source}} &::=& \href{../text/lexical.html#text-char}{\mathtt{char}}^\ast \\ \def\mathdef3318#1{{}}\mathdef3318{character} & \href{../text/lexical.html#text-char}{\mathtt{char}} &::=& \def\mathdef3357#1{\mathrm{U{+}#1}}\mathdef3357{00} ~|~ \dots ~|~ \def\mathdef3358#1{\mathrm{U{+}#1}}\mathdef3358{D7FF} ~|~ \def\mathdef3359#1{\mathrm{U{+}#1}}\mathdef3359{E000} ~|~ \dots ~|~ \def\mathdef3360#1{\mathrm{U{+}#1}}\mathdef3360{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\mathdef3318#1{{}}\mathdef3318{token} & \href{../text/lexical.html#text-token}{\mathtt{token}} &::=& \href{../text/lexical.html#text-keyword}{\mathtt{keyword}} ~|~ \href{../text/values.html#text-int}{\def\mathdef3339#1{{\mathtt{u}#1}}\mathdef3339{N}} ~|~ \href{../text/values.html#text-int}{\def\mathdef3345#1{{\mathtt{s}#1}}\mathdef3345{N}} ~|~ \href{../text/values.html#text-float}{\def\mathdef3353#1{{\mathtt{f}#1}}\mathdef3353{N}} ~|~ \href{../text/values.html#text-string}{\mathtt{string}} ~|~ \href{../text/values.html#text-id}{\mathtt{id}} ~|~ \def\mathdef3361#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3361{(} ~|~ \def\mathdef3362#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3362{)} ~|~ \href{../text/lexical.html#text-reserved}{\mathtt{reserved}} \\ \def\mathdef3318#1{{}}\mathdef3318{keyword} & \href{../text/lexical.html#text-keyword}{\mathtt{keyword}} &::=& (\def\mathdef3363#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3363{a} ~|~ \dots ~|~ \def\mathdef3364#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3364{z})~\href{../text/values.html#text-idchar}{\mathtt{idchar}}^\ast \qquad (\mathrel{\mbox{if}}~\mbox{occurring as a literal terminal in the grammar}) \\ \def\mathdef3318#1{{}}\mathdef3318{reserved} & \href{../text/lexical.html#text-reserved}{\mathtt{reserved}} &::=& (\href{../text/values.html#text-idchar}{\mathtt{idchar}} ~|~ \href{../text/values.html#text-string}{\mathtt{string}})^+ \\ \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.

Keyword tokens are defined either implicitly by an occurrence of a terminal symbol in literal form, such as \(\def\mathdef3365#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3365{keyword}\), in a syntactic production of this chapter, or explicitly where they arise in 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, white space, or comments. For example, \(\def\mathdef3366#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3366{0\$x}\) is a single reserved token, as is \(\def\mathdef3367#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3367{"a""b"}\). Consequently, they are not recognized as two separate tokens \(\def\mathdef3368#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3368{0}\) and \(\def\mathdef3369#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3369{\$x}\), or \("a"\) and \("b"\), respectively, 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\mathdef3370#1{\mathrm{U{+}#1}}\mathdef3370{09}\)), line feed (\(\def\mathdef3371#1{\mathrm{U{+}#1}}\mathdef3371{0A}\)), and carriage return (\(\def\mathdef3372#1{\mathrm{U{+}#1}}\mathdef3372{0D}\)).

\[\begin{split}\begin{array}{llclll@{\qquad\qquad}l} \def\mathdef3318#1{{}}\mathdef3318{white space} & \href{../text/lexical.html#text-space}{\mathtt{space}} &::=& (\def\mathdef3373#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3373{~~} ~|~ \href{../text/lexical.html#text-format}{\mathtt{format}} ~|~ \href{../text/lexical.html#text-comment}{\mathtt{comment}})^\ast \\ \def\mathdef3318#1{{}}\mathdef3318{format} & \href{../text/lexical.html#text-format}{\mathtt{format}} &::=& \href{../text/lexical.html#text-newline}{\mathtt{newline}} ~|~ \def\mathdef3374#1{\mathrm{U{+}#1}}\mathdef3374{09} \\ \def\mathdef3318#1{{}}\mathdef3318{newline} & \href{../text/lexical.html#text-newline}{\mathtt{newline}} &::=& \def\mathdef3375#1{\mathrm{U{+}#1}}\mathdef3375{0A} ~|~ \def\mathdef3376#1{\mathrm{U{+}#1}}\mathdef3376{0D} ~|~ \def\mathdef3377#1{\mathrm{U{+}#1}}\mathdef3377{0D}~\def\mathdef3378#1{\mathrm{U{+}#1}}\mathdef3378{0A} \\ \end{array}\end{split}\]

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

Comments

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

\[\begin{split}\begin{array}{llclll@{\qquad\qquad}l} \def\mathdef3318#1{{}}\mathdef3318{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\mathdef3318#1{{}}\mathdef3318{line comment} & \href{../text/lexical.html#text-comment}{\mathtt{linecomment}} &::=& \def\mathdef3338#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3338{{;}{;}}~~\href{../text/lexical.html#text-comment}{\mathtt{linechar}}^\ast~~(\href{../text/lexical.html#text-newline}{\mathtt{newline}} ~|~ \mathtt{eof}) \\ \def\mathdef3318#1{{}}\mathdef3318{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\mathdef3379#1{\mathrm{U{+}#1}}\mathdef3379{0A} \land c \neq \def\mathdef3380#1{\mathrm{U{+}#1}}\mathdef3380{0D}) \\ \def\mathdef3318#1{{}}\mathdef3318{block comment} & \href{../text/lexical.html#text-comment}{\mathtt{blockcomment}} &::=& \def\mathdef3336#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3336{{(}{;}}~~\href{../text/lexical.html#text-comment}{\mathtt{blockchar}}^\ast~~\def\mathdef3337#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3337{{;}{)}} \\ \def\mathdef3318#1{{}}\mathdef3318{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\mathdef3381#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3381{;} \land c \neq \def\mathdef3382#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3382{(}) \\ &&|& \def\mathdef3383#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3383{;} & (\mathrel{\mbox{if}}~\mbox{the next character is not}~\def\mathdef3384#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3384{)}) \\ &&|& \def\mathdef3385#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3385{(} & (\mathrel{\mbox{if}}~\mbox{the next character is not}~\def\mathdef3386#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3386{;}) \\ &&|& \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.