Tokens¶
During lexical analysis, source file text is separated into distinctive meaningful tokens.
Each source contains at least one end-of-file token.
Identifiers¶
The primary purpose of identifiers is to give a human-readable name to a specific program part.
Regular identifier¶
A regular identifier begins with a letter or underscore and is followed by any number of letters, digits, underscores, or marks.
identifier_or_keyword = identifier_start { identifier_continue } ;
identifier_start = "_" | letter ;
identifier_continue = "_" | letter | digit ;
letter = <Lu> | <Ll> | <Lt> | <Lm> | <Lo> | <Nl> ; (* (1)! *)
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
- Wikipedia page about Unicode categories: https://en.wikipedia.org/wiki/Unicode_character_property#General_Category
Note
As you can see: identifiers support not only ASCII letters, but the entire Letter Unicode category.
This means you can use your native language for identifiers, for example привет_мир is a valid identifier.
Escaped identifier¶
An escaped identifier is enclosed in backtick characters and may contain any character, including those that are otherwise invalid in identifiers. Escape sequences are supported inside escaped identifiers.
escaped_identifier
= "`" { escaped_identifier_character | identifier_escape_sequence } "`" ;
escaped_identifier_character
= ? any Unicode scalar value except LF, CR, "\", and "`" ? ;
identifier_escape_sequence
= "\\"
| "\`"
| "\a"
| "\b"
| "\f"
| "\n"
| "\r"
| "\t"
| "\v"
| "\0"
| "\u" hex_digit , hex_digit , hex_digit , hex_digit
| "\U" hex_digit , hex_digit , hex_digit , hex_digit ,
hex_digit , hex_digit , hex_digit , hex_digit
;
hex_digit
= digit
| "A" | "B" | "C" | "D" | "E" | "F"
| "a" | "b" | "c" | "d" | "e" | "f"
;
Punctuators and operators¶
| Preview | Name | Description |
|---|---|---|
. |
Dot | |
, |
Comma | |
: |
Colon | |
-> |
Retusa | Defines the return type |
:: |
DoubleColon | Part of the qualified paths |
{ |
OpenBrace | |
} |
CloseBrace | |
( |
OpenParen | |
) |
CloseParen | |
[ |
OpenBracket | |
] |
CloseBracket | |
+ |
Plus | |
+= |
PlusEquals | |
- |
Minus | |
-= |
MinusEquals | |
* |
Asterisk | |
*= |
AsteriskEquals | |
/ |
Slash | |
/= |
SlashEquals | |
% |
Percent | |
%= |
PercentEquals | |
& |
Ampersand | |
&= |
AmpersandEquals | |
&& |
DoubleAmpersand | |
^ |
Caret | |
^= |
CaretEquals | |
| |
Bar | |
|= |
BarEquals | |
|| |
DoubleBar | |
~ |
Tilde | |
~= |
TildeEquals | |
! |
Exclamation | |
.. |
Range | |
..= |
InclusiveRange | |
>> |
RightShift1 | |
>>= |
RightShiftEquals | |
>>> |
UnsignedRightShift1 | |
>>>= |
UnsignedRightShiftEquals | |
<< |
LeftShift | |
<<= |
LeftShiftEquals | |
= |
Equals | |
== |
DoubleEquals | |
!= |
NotEquals | |
< |
Less | |
<= |
LessEquals | |
> |
Greater | |
>= |
GreaterEquals |
Literals¶
Literals are tokens that are used in literal expressions.
Numeric literals¶
numeric_literal = ( float_literal | integer_literal ) , [ numeric_suffix ] ;
integer_literal = decimal_literal | hex_literal | binary_literal ;
decimal_literal = digit , { { separator } , digit } ;
binary_literal = "0b" , { { separator } , binary_digit } ;
hex_literal = "0x" , { { separator } , hex_digit } ;
float_literal = decimal_literal , "." , decimal_literal
| decimal_literal , [ "." , decimal_literal ] , float_exponent;
float_exponent = ( "e" | "E" ) , [ "+" | "-" ] , decimal_literal;
separator = "'" ;
binary_digit = "0" | "1" ;
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
hex_digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
| "A" | "B" | "C" | "D" | "E" | "F"
| "a" | "b" | "c" | "d" | "e" | "f"
;
numeric_suffix
= "u8" | "u16" | "u32" | "u64" | "u128" | "usize"
| "i8" | "i16" | "i32" | "i64" | "i128" | "isize"
| "f16" | "f32" | "f64"
;
Numeric literal examples
| Literal | Description |
|---|---|
42 |
Decimal integer |
1'000'000 |
Decimal with separator |
0xFF |
Hexadecimal |
0b1010'0101 |
Binary with separator |
3.14 |
Float with decimal point |
1.5e10 |
Float with positive exponent |
2.5e-3 |
Float with negative exponent |
42u32 |
Unsigned 32-bit integer |
3.14f64 |
64-bit float |
Character and string literals¶
string_literal = '"' { character - '"' } '"' [ encoding_suffix ] ;
character_literal = "'" ( character - "'" ) "'" [ encoding_suffix ] ;
character = ? any Unicode scalar value except LF, CR, "\" ? | escape_sequence;
escape_sequence
= "\\"
| "\'"
| '\"'
| "\n"
| "\r"
| "\t"
| "\0"
| "\u" hex_digit , hex_digit , hex_digit , hex_digit
| "\U" hex_digit , hex_digit , hex_digit , hex_digit ,
hex_digit , hex_digit , hex_digit , hex_digit
;
hex_digit
= digit
| "A" | "B" | "C" | "D" | "E" | "F"
| "a" | "b" | "c" | "d" | "e" | "f"
;
encoding_suffix = "ascii" | "utf8" | "utf16" | "utf32" ;
Textual literal examples
| Literal | Description |
|---|---|
'a' |
Character literal |
'\n' |
Character with escape sequence |
'\u0041' |
Character with 4-digit Unicode escape |
'\U0001F600' |
Character with 8-digit Unicode escape |
"hello" |
String literal |
"line 1\nline 2" |
String with escape sequence |
"escaped \"quote\"" |
String with escaped quote |
'A'ascii |
Character with encoding suffix |
"hello"utf8 |
String with encoding suffix |
Character escape sequences¶
An escape starts with a Backslash (U+005C) and continues with one of the following forms:
- The backslash escape is the Backslash (U+005C) which must be escaped in order to denote itself.
- The double quote escape is the Double Quote (U+0022). This escape is optional for literals that are not enclosed by double quote characters.
- The apostrophe (a.k.a. single quote) escape is the Apostrophe (U+0027). This escape is optional for literals that are not enclosed by apostrophe characters.
- A whitespace escape is one of the characters:
- Lower n (U+006E) denoting the Line Feed (U+000A).
- Lower r (U+0072) denoting the Carriage Return (U+000D).
- Lower t (U+0074) denoting the Horizontal Tab (U+0009).
- The null escape is the character Zero (U+0030) and denotes the Unicode Null (U+0000) value.
- The any-Unicode-scalar-value escape is the Lower u (U+0075) followed by four hexadecimal digits, or the Capital U (U+0055) followed by eight hexadecimal digits.
Keywords¶
Keywords are much like identifiers, except they cannot be used as such. They are reserved words that the language uses in different contexts.
Strong keywords¶
Strong keywords are a set of words that are always treated as non-identifier tokens. The following list represents all existing strong keywords in the current language version.
i8i16i32i64i128isizeu8u16u32u64u128usizebooltruefalsemoduleuseifelsefordowhileloopcontinuebreak
Contextual keywords¶
Contextual keywords are identifiers that have a special meaning only in certain syntactic contexts. Outside of these contexts, they can be used as regular identifiers and as names for user-defined program parts.
The following list represents all contextual keywords in the current language version.
Information
There are no contextual keywords at this time.
Tip
If a user needs or wants to use a contextual or strong keyword as an identifier, they can use an escaped identifier.
Reserved keywords¶
Reserved keywords are identifiers that are reserved for future use in the language. Using a reserved keyword as an identifier will result in a compilation error.
enummaybe add a link to the discussion?asautonullinterfacevirtconst