On this page (7 sections)
§1 Lexical Structure
1.1 Source encoding & whitespace
- Source files are UTF-8.
- Line breaks:
U+000A(LF),U+000D(CR), orCRLF. - Whitespace separates tokens. Horizontal whitespace is otherwise insignificant; a line break may terminate a statement when its optional semicolon is omitted (§6).
- Indentation is not semantically significant.
1.2 Comments
// single-line comment until end of line
/* block comment, may /* nest */ */
/// doc comment for the following declaration
Doc comments (///) are attached to the next declaration and available to tooling.
1.3 Identifiers
identifier ::= ident_head ident_tail*
ident_head ::= 'a'..'z' | 'A'..'Z' | '_'
ident_tail ::= ident_head | '0'..'9'
Identifiers are case-sensitive. The lone _ is the wildcard and is not an identifier.
1.4 Keywords (full reserved list)
| Category | Keywords |
|---|---|
| Bindings | let, var |
| Control flow | if, else, while, for, in, match, return, break, continue, yield |
| Declarations | func, struct, enum, extension, protocol, subscript, init, deinit, import, as, indirect |
| Access conventions | inout, borrowing, consuming, initializing, consume, mutating |
| Visibility | public, internal, private |
| Types & literals | true, false, nil, self, Self |
| Match guard | where |
| Reserved ⏳ | async, await, actor, throws, try, catch, defer, is, class, trait, macro, invariant, result, unsafe, package, Any |
match does not require a separate "Pattern" lexer feature. The lexer emits ordinary identifier/literal/punctuation tokens plus match, where, indirect, and =>; the parser builds pattern nodes and the type checker performs binding and exhaustiveness checks (§7).
classis reserved ⏳ and rejected by the current compiler; usestructfor aggregates. The other reserved words are also rejected in v0.1 (Appendix A).
asintroduces a numeric conversion (x as T,x as? T,x &as T, §5.2.1). It is not accepted in the module/import grammar (§12); import aliases remain reserved. Theistype-test operator is also deferred (Appendix A).
1.5 Literals
integer_literal ::= decimal_lit | hex_lit | binary_lit | octal_lit
decimal_lit ::= digit ( '_'? digit )*
hex_lit ::= ('0x'|'0X') hex_digit ( '_'? hex_digit )*
binary_lit ::= ('0b'|'0B') ('0'|'1') ( '_'? ('0'|'1') )*
octal_lit ::= ('0o'|'0O') ('0'..'7') ( '_'? ('0'..'7') )*
float_literal ::= decimal_lit '.' decimal_lit [ exponent ]
| decimal_lit exponent
exponent ::= ('e'|'E') ['+'|'-'] decimal_lit
bool_literal ::= 'true' | 'false'
nil_literal ::= 'nil'
char_literal ::= "'" ( escape | <any unicode except "'" or '\'> ) "'" // Char (32-bit scalar)
byte_literal ::= "b'" ( escape | <any ASCII except "'" or '\'> ) "'" // UInt8 (single byte)
string_literal ::= '"' string_item* '"'
string_interpolation ::= '"' string_item* ( '\(' expression ')' string_item* )+ '"'
string_item ::= escape | <any unicode except '"', '\', LF, CR>
escape ::= '\\' ( 'n' | 't' | 'r' | '"' | '\\' | '0' | 'u{' hex+ '}' )
array_literal ::= '[' [ expression , ... ] ']'
dict_literal ::= '[' ':' ']' | '[' dict_entry , ... ']'
dict_entry ::= expression ':' expression
tuple_literal ::= '(' expression , expression , ... ')'
Underscores in numeric literals are visual separators only: 1_000_000.
A char_literal such as 'a' has type Char (a 32-bit Unicode scalar). A byte_literal such as b'a' has type UInt8 and may only contain a single ASCII character; it is the natural element of a UTF-8 String or a [UInt8] buffer (§2.1.1). For example b'"' is 0x22 and b'\n' is 0x0A.
1.6 Operators & punctuation
| Class | Tokens |
|---|---|
| Arithmetic | + - * / % |
| Comparison | == != < <= > >= |
| Logical | && || ! |
| Coalescing | ?? (nil / .Err coalescing; right operand may be a diverging expression, §8.5) |
| Bitwise | & | ^ ~ << >> |
| Assignment | = += -= *= /= %= &= |= ^= <<= >>= |
| Range | ..< ... |
| Postfix | ? (Result / Optional propagation) ! (force-unwrap) |
| Member / call | . ( ) [ ] { } , ; : => _ |
| Memory marker | & (call-site inout marker) |
| Declaration annotation | @ (§11, §19) |
& is both bitwise-AND (binary infix) and the inout call-site marker (prefix on argument). Disambiguated by syntactic position.
@ starts a declaration annotation. Annotation names such as c_extern and c_export, and argument labels such as symbol, are identifiers, not new keywords. The annotation grammar is specified in Appendix C, the C annotations' semantics in §19, @platform in §11.5, and parameter-level @collect in §3.2.8. Implementation status: @, the restricted C annotation subset, @platform, and parameter-level @collect are accepted; the proposed @spec/@property shorthand and DSL remain unsupported.
1.7 String interpolation
A string containing one or more \( sequences is a string interpolation expression (string_interpolation, §5.13, decision 0046), not a plain string_literal:
let n = 42
print(value: "answer is \(n), squared is \(n * n)")
Lexical rules. On encountering \( inside a string literal, the lexer pushes an interpolation frame and lexes an ordinary expression until the matching ) at parenthesis depth zero, then resumes scanning literal string text. Inside \(...), balanced parentheses, horizontal whitespace, line breaks, line comments (// ..., terminated by a line break, not by )), nested block comments (/* ... */), nested string literals, and nested string interpolations are all permitted. Unescaped line breaks (LF, CR) remain forbidden in literal string text segments (lexer.unterminated-string), and escape sequences scan left to right so "\\(x)" is a plain string_literal (\(x)) while "\\\(x)" begins an interpolation after a literal \. Inside byte literals b'...' (and future char literals '...'), \( is not interpolation (lexer.invalid-byte-escape). Empty interpolation "\()" is parser.expected-expression. Each \(...) counts as one level toward the syntax nesting limit (Appendix C) and acts as a delimiter permitting trailing @collect blocks inside control-flow headers (§5.6.2).
Plain string literals without \( remain string_literal tokens and are required wherever a literal token is specified: match literal patterns (§7), C annotations (§19), joyeer_intrinsic_literal_address / joyeer_intrinsic_symbol_address (§19.4.5), and top-level let constant initializers (§3.1.1).
A user-extensible Display protocol remains deferred to a future version (§20.10).