# 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\mathdef3309#1{{}}\mathdef3309{source} & \href{../text/lexical.html#text-source}{\mathtt{source}} &::=& \href{../text/lexical.html#text-char}{\mathtt{char}}^\ast \\ \def\mathdef3309#1{{}}\mathdef3309{character} & \href{../text/lexical.html#text-char}{\mathtt{char}} &::=& \def\mathdef3348#1{\mathrm{U{+}#1}}\mathdef3348{00} ~|~ \dots ~|~ \def\mathdef3349#1{\mathrm{U{+}#1}}\mathdef3349{D7FF} ~|~ \def\mathdef3350#1{\mathrm{U{+}#1}}\mathdef3350{E000} ~|~ \dots ~|~ \def\mathdef3351#1{\mathrm{U{+}#1}}\mathdef3351{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\mathdef3309#1{{}}\mathdef3309{token} & \href{../text/lexical.html#text-token}{\mathtt{token}} &::=& \href{../text/lexical.html#text-keyword}{\mathtt{keyword}} ~|~ \href{../text/values.html#text-int}{\def\mathdef3330#1{{\mathtt{u}#1}}\mathdef3330{N}} ~|~ \href{../text/values.html#text-int}{\def\mathdef3336#1{{\mathtt{s}#1}}\mathdef3336{N}} ~|~ \href{../text/values.html#text-float}{\def\mathdef3344#1{{\mathtt{f}#1}}\mathdef3344{N}} ~|~ \href{../text/values.html#text-string}{\mathtt{string}} ~|~ \href{../text/values.html#text-id}{\mathtt{id}} ~|~ \def\mathdef3352#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3352{(} ~|~ \def\mathdef3353#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3353{)} ~|~ \href{../text/lexical.html#text-reserved}{\mathtt{reserved}} \\ \def\mathdef3309#1{{}}\mathdef3309{keyword} & \href{../text/lexical.html#text-keyword}{\mathtt{keyword}} &::=& (\def\mathdef3354#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3354{a} ~|~ \dots ~|~ \def\mathdef3355#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3355{z})~\href{../text/values.html#text-idchar}{\mathtt{idchar}}^\ast \qquad (\mbox{if occurring as a literal terminal in the grammar}) \\ \def\mathdef3309#1{{}}\mathdef3309{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.

The set of keyword tokens is defined implicitly, by all occurrences of a terminal symbol in literal form, such as $$\def\mathdef3356#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3356{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, white space, or comments. For example, $$\def\mathdef3357#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3357{0\x}$$ is a single reserved token, as is $$\def\mathdef3358#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3358{"a""b"}$$. Consequently, they are not recognized as two separate tokens $$\def\mathdef3359#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3359{0}$$ and $$\def\mathdef3360#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3360{\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\mathdef3361#1{\mathrm{U{+}#1}}\mathdef3361{09}$$), line feed ($$\def\mathdef3362#1{\mathrm{U{+}#1}}\mathdef3362{0A}$$), and carriage return ($$\def\mathdef3363#1{\mathrm{U{+}#1}}\mathdef3363{0D}$$).

$\begin{split}\begin{array}{llclll@{\qquad\qquad}l} \def\mathdef3309#1{{}}\mathdef3309{white space} & \href{../text/lexical.html#text-space}{\mathtt{space}} &::=& (\def\mathdef3364#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3364{~~} ~|~ \href{../text/lexical.html#text-format}{\mathtt{format}} ~|~ \href{../text/lexical.html#text-comment}{\mathtt{comment}})^\ast \\ \def\mathdef3309#1{{}}\mathdef3309{format} & \href{../text/lexical.html#text-format}{\mathtt{format}} &::=& \def\mathdef3365#1{\mathrm{U{+}#1}}\mathdef3365{09} ~|~ \def\mathdef3366#1{\mathrm{U{+}#1}}\mathdef3366{0A} ~|~ \def\mathdef3367#1{\mathrm{U{+}#1}}\mathdef3367{0D} \\ \end{array}\end{split}$

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

A comment can either be a line comment, started with a double semicolon $$\def\mathdef3329#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3329{{;}{;}}$$ and extending to the end of the line, or a block comment, enclosed in delimiters $$\def\mathdef3327#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3327{{(}{;}} \dots \def\mathdef3328#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3328{{;}{)}}$$. Block comments can be nested.
$\begin{split}\begin{array}{llclll@{\qquad\qquad}l} \def\mathdef3309#1{{}}\mathdef3309{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\mathdef3309#1{{}}\mathdef3309{line comment} & \href{../text/lexical.html#text-comment}{\mathtt{linecomment}} &::=& \def\mathdef3329#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3329{{;}{;}}~~\href{../text/lexical.html#text-comment}{\mathtt{linechar}}^\ast~~(\def\mathdef3368#1{\mathrm{U{+}#1}}\mathdef3368{0A} ~|~ \mathtt{eof}) \\ \def\mathdef3309#1{{}}\mathdef3309{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\mathdef3369#1{\mathrm{U{+}#1}}\mathdef3369{0A}) \\ \def\mathdef3309#1{{}}\mathdef3309{block comment} & \href{../text/lexical.html#text-comment}{\mathtt{blockcomment}} &::=& \def\mathdef3327#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3327{{(}{;}}~~\href{../text/lexical.html#text-comment}{\mathtt{blockchar}}^\ast~~\def\mathdef3328#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3328{{;}{)}} \\ \def\mathdef3309#1{{}}\mathdef3309{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\mathdef3370#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3370{;} \wedge c \neq \def\mathdef3371#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3371{(}) \\ &&|& \def\mathdef3372#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3372{;} & (\mathrel{\mbox{if}}~\mbox{the next character is not}~\def\mathdef3373#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3373{)}) \\ &&|& \def\mathdef3374#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3374{(} & (\mathrel{\mbox{if}}~\mbox{the next character is not}~\def\mathdef3375#1{\mbox{‘}\mathtt{#1}\mbox{’}}\mathdef3375{;}) \\ &&|& \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.