Lexical Conventions#
Warning
This page is a work in progress and is currently incomplete.
This page specifies MoonBit lexical forms. Runtime representation, APIs, and literal overloading are covered in Fundamentals.
In the productions, { symbol } means zero or more repetitions,
{ symbol }+ means one or more repetitions, and x ... y denotes an inclusive
range.
Common Lexical Classes#
hex-digit ::= 09AFaf octal-digit ::= 07 unicode-scalar-value ::= U+0000U+D7FFU+E000U+10FFFF newline ::= LFCRCRLFU+2028U+2029 whitespace ::= U+0009U+000BU+000CU+0020U+00A0U+1680 U+2000U+200AU+202FU+205FU+3000U+FEFF
String Literals#
string-literal ::= "string-character" string-character ::= regular-string-character simple-escape-sequence unicode-escape-sequence interpolation regular-string-character ::= unicode-scalar-value"\CRLF simple-escape-sequence ::= \\"'ntbrf/ unicode-escape-sequence ::= \uhex-digithex-digithex-digithex-digit \u{hex-digit}
The simple escape sequences have the following meanings:
Sequence |
Character |
|---|---|
|
Backslash (U+005C) |
|
Double quote (U+0022) |
|
Single quote (U+0027) |
|
Forward slash (U+002F) |
|
Line feed (U+000A) |
|
Carriage return (U+000D) |
|
Horizontal tab (U+0009) |
|
Backspace (U+0008) |
|
Form feed (U+000C) |
A Unicode escape must denote a Unicode scalar value. A CR or LF before the closing quote reports an unterminated string literal.
Interpolation#
interpolation ::= \{whitespaceexpressionwhitespace}
The expression must be nonempty and end at the matching }. Braces inside
nested literals do not affect matching; nested interpolations are recognized
recursively. CR, LF, // comments, attributes, and multiline string literals
are not permitted.
Multiline String Literals#
multiline-string-literal ::= raw-multiline-string-literal interpolated-multiline-string-literal raw-multiline-string-literal ::= raw-multiline-string-line newlineraw-multiline-string-line interpolated-multiline-string-literal ::= interpolated-multiline-string-line newlineinterpolated-multiline-string-line raw-multiline-string-line ::= #|multiline-regular-character interpolated-multiline-string-line ::= $|multiline-regular-characterinterpolation multiline-regular-character ::= unicode-scalar-valueCRLF
The prefixes are omitted from the result, and lines are joined with U+000A. A
final empty prefixed line adds a trailing line feed. A #| line is literal; in
a $| line, only \{ begins interpolation. Multiline strings are not permitted
inside interpolation expressions.
Bytes Literals#
bytes-literal ::= b"bytes-character" bytes-character ::= regular-string-character simple-escape-sequence byte-escape-sequence interpolation byte-escape-sequence ::= \xhex-digithex-digit \o03octal-digitoctal-digit
A CR or LF before the closing quote reports an unterminated literal. Non-ASCII
source characters contribute their UTF-8 encoding; \xHH and \oDDD each
contribute one byte with a value from 0 to 255. Interpolation follows the
string-literal rules. There is no multiline bytes-literal form.
Character Literals#
character-literal ::= 'regular-character' 'character-escape-sequence' regular-character ::= unicode-scalar-value'\CRLF character-escape-sequence ::= simple-escape-sequence unicode-escape-sequence
A character literal contains exactly one Unicode scalar value or escape sequence.
Byte Literals#
byte-literal ::= b'regular-byte-character' b'byte-character-escape-sequence' regular-byte-character ::= U+0000U+007F'\CRLF byte-character-escape-sequence ::= simple-escape-sequence byte-escape-sequence
An unescaped byte is ASCII. Unicode escapes are invalid in byte literals.