Lexical Structure
Source encoding, comments, identifiers, literals, strings, and interpolation.
Contents
Haze source is a sequence of UTF-8 bytes. The lexer is intentionally small: identifiers are ASCII, punctuation is explicit, and most words that look like keywords are recognized by the parser in context. Whitespace separates tokens and, in a few constructs, indentation supplies the body of a control statement.
Comments and Whitespace#
Line comments begin with // and run to the end of the line. Block comments begin with /* and end with */; they do not nest. Whitespace between tokens is insignificant except at line boundaries where the parser uses indentation to find the body of an if or for.
// A line comment.
/* A block comment that may span
several lines. */
fn main() {
print("comments are ignored")
}
Haze does not use semicolons. A statement ends at the end of its syntactic form, and a newline usually marks the next statement. 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; the parser uses capitalization only to decide 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 are recognized as qualified value prefixes and 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 of them remain valid identifiers in other positions, but using them as ordinary names makes code harder to read.
| 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 |
The words interface and import are explicitly rejected as unsupported declarations. while is not a keyword; the equivalent loop is for condition { ... }.
Integer Literals#
Decimal literals use digits and may contain underscores for grouping. Hexadecimal literals start with 0x or 0X and use hexadecimal digits. 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 in the integer and fraction parts.
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. The escape sequences below are recognized inside ordinary strings.
| 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 byte, 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 the 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 the braces is parsed as a normal Haze expression and is converted to text using the same formatting rules as 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 not a general expression; it is a small formatting language.
| 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 |
The integer type x and X produce lower- and upper-case hexadecimal, b produces binary, and o produces octal. The u type 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 format specification, but a specification is rejected for those types. Records and lists cannot be interpolated directly; interpolate their fields or elements instead.
const x = 42
const f = 3.14159
print($"{x:x} {x:08b} {f:.2f} {f:>10.3g}")