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\mathdef3220#1{{}}\mathdef3220{source} & \href{../text/lexical.html#text-source}{\mathtt{source}} &::=&
\href{../text/lexical.html#text-char}{\mathtt{char}}^\ast \\
\def\mathdef3220#1{{}}\mathdef3220{character} & \href{../text/lexical.html#text-char}{\mathtt{char}} &::=&
\def\mathdef3259#1{\mathrm{U{+}#1}}\mathdef3259{00} ~|~ \dots ~|~ \def\mathdef3260#1{\mathrm{U{+}#1}}\mathdef3260{D7FF} ~|~ \def\mathdef3261#1{\mathrm{U{+}#1}}\mathdef3261{E000} ~|~ \dots ~|~ \def\mathdef3262#1{\mathrm{U{+}#1}}\mathdef3262{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\mathdef3220#1{{}}\mathdef3220{token} & \href{../text/lexical.html#text-token}{\mathtt{token}} &::=&
\href{../text/lexical.html#text-keyword}{\mathtt{keyword}} ~|~ \href{../text/values.html#text-int}{\def\mathdef3241#1{{\mathtt{u}#1}}\mathdef3241{N}} ~|~ \href{../text/values.html#text-int}{\def\mathdef3247#1{{\mathtt{s}#1}}\mathdef3247{N}} ~|~ \href{../text/values.html#text-float}{\def\mathdef3255#1{{\mathtt{f}#1}}\mathdef3255{N}} ~|~ \href{../text/values.html#text-string}{\mathtt{string}} ~|~ \href{../text/values.html#text-id}{\mathtt{id}} ~|~
\def\mathdef3263#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3263{(} ~|~ \def\mathdef3264#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3264{)} ~|~ \href{../text/lexical.html#text-reserved}{\mathtt{reserved}} \\
\def\mathdef3220#1{{}}\mathdef3220{keyword} & \href{../text/lexical.html#text-keyword}{\mathtt{keyword}} &::=&
(\def\mathdef3265#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3265{a} ~|~ \dots ~|~ \def\mathdef3266#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3266{z})~\href{../text/values.html#text-idchar}{\mathtt{idchar}}^\ast
\qquad (\mbox{if occurring as a literal terminal in the grammar}) \\
\def\mathdef3220#1{{}}\mathdef3220{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, such as \(\def\mathdef3267#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3267{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\mathdef3268#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3268{0\$x}\) is a single reserved token.
Consequently, it is not recognized as two separate tokens \(\def\mathdef3269#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3269{0}\) and \(\def\mathdef3270#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3270{\$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\mathdef3271#1{\mathrm{U{+}#1}}\mathdef3271{09}\)), line feed (\(\def\mathdef3272#1{\mathrm{U{+}#1}}\mathdef3272{0A}\)), and carriage return (\(\def\mathdef3273#1{\mathrm{U{+}#1}}\mathdef3273{0D}\)).
\[\begin{split}\begin{array}{llclll@{\qquad\qquad}l}
\def\mathdef3220#1{{}}\mathdef3220{white space} & \href{../text/lexical.html#text-space}{\mathtt{space}} &::=&
(\def\mathdef3274#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3274{~~} ~|~ \href{../text/lexical.html#text-format}{\mathtt{format}} ~|~ \href{../text/lexical.html#text-comment}{\mathtt{comment}})^\ast \\
\def\mathdef3220#1{{}}\mathdef3220{format} & \href{../text/lexical.html#text-format}{\mathtt{format}} &::=&
\def\mathdef3275#1{\mathrm{U{+}#1}}\mathdef3275{09} ~|~ \def\mathdef3276#1{\mathrm{U{+}#1}}\mathdef3276{0A} ~|~ \def\mathdef3277#1{\mathrm{U{+}#1}}\mathdef3277{0D} \\
\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\mathdef3240#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3240{{;}{;}}\) and extending to the end of the line, or a block comment, enclosed in delimiters \(\def\mathdef3238#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3238{{(}{;}} \dots \def\mathdef3239#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3239{{;}{)}}\). Block comments can be nested.
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.