2. Lexical structure
- Source is UTF-8. Identifiers are ASCII:
[A-Za-z_][A-Za-z0-9_]*.
- Keywords:
fn return if else while for break continue switch var const
true false and or struct enum union try catch orelse error inout sink comptime uses
allow extern link owned null defer errdefer.
- Integer literals are decimal digits, optionally separated by
_
(1_000_000), at most 18446744073709551615; 0x starts a hex one
(0xff, 0xffff_0000) and 0b a binary one (0b1010). A literal’s type comes from
context (section 7); it must fit that type (E003). -5 is unary minus
applied to 5, but checked as one literal, so -128 fits i8.
- String literals are
"..." on one line, with escapes \n \r \t \\ \" \0,
\xNN (any byte, two hex digits) and \u{N} (a Unicode code point,
1 to 6 hex digits, written as its UTF-8 bytes). A str is bytes, so
"\xff\xfe" need not be UTF-8; "caf\u{e9}" is the same 5 bytes as
"café". A surrogate (\u{d800}) or a code point above 10ffff is E001.
- Character literals are one byte in single quotes,
'a', with escapes
\n \r \t \\ \' \" \0 and \xNN (two hex digits). One is an integer
literal, 'a' is 97, so its type comes from the context like any
other: s[i] == 'a' compares a byte, and const c: u8 = 'z' is a u8.
A character of several UTF-8 bytes ('é'), '', 'ab' and '\u{e9}'
(use a string for a code point) are E001.
// expect: 5 true
// expect: 4 240 128
// expect: 3 255 65
uses io
fn main(io: Io) void {
const s = "caf\u{e9}"
io.print(@len(s), " ", s == "café")
const smile = "\u{1f600}"
io.print(@len(smile), " ", smile[0], " ", smile[3])
const raw = "\xff\xfe\x41" // not UTF-8
io.print(@len(raw), " ", raw[0], " ", raw[2])
}
// starts a comment that runs to the end of the line.
- Newlines end statements. A newline may also appear after a binary
operator, after
( [ { or ,, before ) ] }, and before else.
Anywhere else inside an expression, a newline is a syntax error (E002).
- A capitalised type name followed by
{ is a type literal: a struct
literal (Point{ .x = 1 }) or a union literal (Shape{ .circle = 2 }).
This is why type names start with a capital letter: if (ok) { stays a
block.
// expect: 6
// expect: 3
uses io
fn Pair(comptime T: type) type {
return struct {
a: T
b: T
}
}
fn sum(p: Pair(
i64
)) i64 {
return p.a + p.b
}
fn main(io: Io) void {
const total = 1 +
2 +
3
io.print(total)
io.print(sum(
Pair(i64){ .a = 1, .b = 2 }
))
}