Lexical Structure
Source encoding, comments, identifiers, literals, strings, and interpolation.
Contents
Haze source is a sequence of UTF-8 bytes. Identifiers are ASCII. Whitespace separates tokens, and in some constructs indentation supplies the body of a control statement.
Comments and Whitespace#
Line comments begin with // and run to the end of the line. There are no block comments.
// A line comment.
fn main() {
print("comments are ignored")
}
Whitespace between tokens is insignificant except at line boundaries where the parser uses indentation to find the body of an if or for.
Haze does not use semicolons. A statement ends at the end of its syntactic form. Expressions may continue across lines when the line ends with an operator, a comma, a dot, an opening bracket, or an opening parenthesis.
Identifiers and Names#
An identifier begins with an ASCII letter or an underscore and continues with ASCII letters, digits, or underscores. Upper-case identifiers are not a separate token class; capitalization only decides whether a name followed by { is a record literal.
| Kind | Example | Convention |
|---|---|---|
| Value or function | total, read_file, map |
snake_case |
| Type | Account, Positive, Result |
PascalCase |
| Type parameter | T, K, V, A |
Short and upper-case |
| Module path segment | std/seq, orders, a/b/c |
snake_case |
| Enum or sum variant | Color.Red, Some, Cons |
PascalCase |
Names beginning with two underscores are reserved for the compiler and runtime. The built-in namespaces str, bytes, rune, cstr, and ptr should not be shadowed.
Keywords and Contextual Words#
Haze has no separate keyword token. The parser recognizes the following words in declaration and statement positions. Some remain valid identifiers elsewhere.
| Context | Words |
|---|---|
| Top level | fn, type, const, contract, trait, impl, use, test, extern, pub, pure |
| Function contracts | pre, post, invariant |
| Statements | const, var, if, else, for, in, break, continue, return, defer, assert |
| Expressions | if, else, match, fn, is, true, false, None, Some, Success, Error, cast, trunc, truncate |
| Type declarations | type, enum, extern, of |
| Conditional compilation | @cfg, all, any, not |
interface and import are rejected as unsupported declarations. while is not a keyword; use for condition { ... }.
Integer Literals#
Decimal literals use digits and may contain underscores. Hexadecimal literals start with 0x or 0X. There is no binary or octal integer literal syntax. A decimal literal that fits i32 has type i32; a larger literal that fits i64 has type i64; a literal above the signed 64-bit range has type u64. Unary minus is a separate operator.
const decimal = 1_000_000
const hexadecimal = 0xFF_EC
const negative = -42
const huge = 18446744073709551615
Floating-Point Literals#
Floating-point literals contain a decimal point or an exponent. The only floating-point type is f64. Underscores may group digits.
const simple = 1.5
const exponent = 2.5e-3
const grouped = 1_000.25
Boolean and Absent Values#
true and false are boolean literals. None is the empty Option constructor and requires a contextual type when the element type cannot be inferred. There is no null literal.
const enabled = true
const absent Option[i32] = None
String Literals#
A string literal is enclosed in double quotes.
| Escape | Meaning |
|---|---|
\n |
Line feed |
\t |
Tab |
\r |
Carriage return |
\0 |
Null byte |
\\ |
Backslash |
\" |
Double quote |
\xNN |
One byte written as two hexadecimal digits |
A backslash before a newline removes the newline. Other escaped characters are preserved as the character after the backslash. Strings are immutable sequences of UTF-8 bytes; indexing a string yields a u8, not a rune.
const path = "docs\\guide"
const tabbed = "name\tvalue"
const byte = "\x41" // "A"
Multiline Strings#
A multiline string begins with two backslashes and reads one line at a time. Each continuation line must begin with optional spaces or tabs followed by two backslashes. The leading whitespace before the continuation marker is removed, and lines are joined with newline characters. The string ends at the first line that does not begin with a continuation marker.
const message = \\first line
\\second line
\\third line
The value of message is "first line\nsecond line\nthird line".
Interpolated Strings#
An interpolated string begins with $" and embeds expressions in braces. A literal brace is written as {{ or }}. The expression inside braces is parsed as a normal Haze expression and formatted as by print. Interpolation supports nested strings and nested interpolated strings.
const name = "Haze"
const n = 42
print($"hello {name}, n + 1 = {n + 1}")
print($"literal braces {{n}} and {{}}")
print($"nested {"quoted text"} inside")
An expression may carry a format specification after a colon, as in {value:spec}. The specification is a small formatting language, not a general expression.
| Part | Meaning | Example |
|---|---|---|
| Fill and alignment | Optional fill character followed by <, ^, or > |
*^8 |
| Alignment only | <, ^, or > |
>8 |
| Zero padding | 0 before a width, right-aligned |
05 |
| Width | Decimal digit count | 12 |
| Precision | . followed by digits, for floats |
.3 |
| Integer type | x, X, b, o, or u |
x |
| Float type | f, e, or g |
.2f |
| Text type | s |
s |
x and X produce lower- and upper-case hexadecimal, b binary, and o octal. u prints a rune as its decimal Unicode scalar value. Integer formatting does not accept precision. Text formatting accepts only s. Enums, Option, and Result may be interpolated without a specification; a specification is rejected for those types. Records and lists cannot be interpolated directly.
const x = 42
const f = 3.14159
print($"{x:x} {x:08b} {f:.2f} {f:>10.3g}")