Zane Lexical Rules

This document specifies Zane’s lexical layer: how source text is tokenised, how identifiers are formed, and how literals are written. Identifier casing distinguishes type names from value names during parsing, including where <> type expressions meet comparison syntax.

See also: syntax.md for the surface grammar built on these tokens. generics.md §4.3 for how casing disambiguates <> type expressions. types.md §5 for type and alias declarations.


1. Overview

Zane is case-sensitive. The first letter of an identifier selects its lexical class, so the parser can distinguish type names from value names without consulting their declarations.

  • Case-sensitive. Vector and vector are distinct identifiers. Casing is never normalised.
  • Casing determines kind. An uppercase-initial name is a type, including a type parameter such as T; a lowercase-initial name is a value, binding, field, or any other parameter. Using a lowercase name as a type, or giving a binding, a field, or a parameter other than a type parameter an uppercase name, is a compile-time error.
  • Digits are ordinary identifier characters. A digit may appear in an identifier except as its first character, so names such as Vec2 and Tensor3 are ordinary names.
  • Casing keeps the grammar unambiguous. Because only a type may precede < in a type expression, the parser distinguishes Vector<Int> from a < b by casing alone.
  • The bracket picks the separator. A { } body terminates each entry with ; (always trailing), except a match arm whose body is a block, which its brace ends; a [ ], ( ), or < > list separates its entries with , (never trailing). A { } holding statements is a code block, where a ; terminates each statement too — except a statement ending in }, which takes none. A package-scope declaration ends the same way.
  • Newlines are never structural. Every entry, statement, and declaration has its own end mark — a ;, or for a statement, a declaration or a block-bodied match arm ending in }, that brace — so line breaks are free everywhere.
  • String structure is language-defined. Double quotes delimit a string; quote and backslash escapes and interpolation are recognized by the compiler. Other backslash sequences remain text for the consumer (§7.1).
  • The spelling picks the numeric concept. A numeric literal written without a . is an integer literal; one written with a . is a float literal. The two carry different concept types. A ' groups the digits before the . and changes neither (§7).

2. Case Sensitivity

Identifiers are compared by exact byte sequence. Two names that differ only in case are two distinct identifiers and never refer to the same entity.

Vector   // a type
vector   // a value-level name

The compiler never folds case. There is no implicit conversion between an identifier and its differently-cased spelling.


3. Casing Determines Kind

The first letter of an identifier selects its lexical class.

First letter Class Examples
Uppercase Type Int, Vector, Vec2, Matrix
Lowercase Value, binding, or parameter x, count, n, transform
Lowercase Package name math, json, httpClient

Package names share lowercase-initial casing with value names but are syntactically distinct: they appear only after the package keyword, after import, after the as of a whole-package import alias, or as the left operand of $. The surrounding syntax, not casing, disambiguates them from ordinary value names.

3.1 Types must be uppercase

A name used in a type position MUST be uppercase-initial. A lowercase name in a type position is a compile-time error.

vec Vector(Int(2), Int(3));  // legal: Vector and Int are types
vec vector(int(2));          // ILLEGAL: lowercase names are not types

3.2 Values must be lowercase

The name of a binding, a field, or a parameter other than a type parameter is lowercase-initial; a type parameter names a type and is uppercase-initial (§3.1). This is why a number parameter such as n is known to be a number and not a type: its casing places it in the value class even when it appears inside a <> slot.

type Buffer<T Type, n @concepts$Int> = struct {   // T is a type parameter, n is a number parameter
    data Array<T, n>;
}

A type parameter such as T is uppercase because it names a type; a number parameter such as n is lowercase because it names a compile-time integer. A parameter is introduced by a type’s or a package’s <> header or inline in a verb’s signature, and referenced by its bare name. See generics.md §3.

Story: stories/lexical.md — “Casing separates types from values”.


4. Identifiers

4.1 Identifier characters

An identifier is a letter followed by zero or more letters or digits. A digit MUST NOT be the first character.

Vec2       // legal
Tensor3    // legal
2Vec       // ILLEGAL: an identifier cannot start with a digit

Digits carry no special meaning inside an identifier; Vec2 is an ordinary type name, not a parameterised form. Parameters are supplied only through <> (see generics.md §4).

4.2 Leading underscore

A leading _ marks an identifier as private without changing the lexical class set by the first letter.

  • A field beginning with _ is private to the package that declares its type. See types.md §2.3.
  • A named package-scope declaration beginning with _ is private to its package. This applies to every named declaration, including types; operators are named by operator tokens and remain public. See packages.md §4.
  • A top-level library package whose name begins with _ is private to its project. See packages.md §4.4.

Story: stories/lexical.md — “Privacy lives in the name”. Story: stories/lexical.md — “Field privacy moves to the package”.

4.3 Reserved sigils

Certain leading characters are reserved and are not ordinary identifier starts:

Sigil Meaning Canonical home
& Reference type (&Node) memory.md §2
^ Roaming owner type (^Node) memory.md §2.1
@ Intrinsic namespace (@primitives$, @concepts$, @operators$, @math$, @controlflow$, @runtime$, @program$) syntax.md §2.7
$ Package-member separator (packageName$member) packages.md §1

4.4 Operator words

and and or are operator tokens (operators.md §2.4). They are reserved, and MUST NOT be used as an identifier, a member name after $ included:

ready Bool = left and right;   // legal: `and` in operator position
and Bool = Bool(true);          // ILLEGAL: `and` is reserved

5. How Casing Disambiguates the Grammar

The <> type-expression syntax shares its characters with the < and > comparison operators. Casing resolves the overlap without lookahead: the operand on the left of < in a type expression is always a type, and a type is always uppercase-initial.

type Holder = struct {
    data Array<Int, 9>;   // type expression: Array is uppercase, so < opens a type argument list
}

ok Bool = a < b;         // comparison: a is lowercase, so < is the comparison operator

A comparison never has a type on its immediate left, and a type expression never has a value on its immediate left. The parser therefore distinguishes the two by inspecting the first letter of a single identifier token. A value on the left that is not an identifier, such as the literal 1 in 1 < count, has no first letter, and the < after it is the comparison operator.

See also: generics.md §4.3 for the type-expression rule and operators.md for comparison-operator semantics. Story: stories/lexical.md — “Casing separates types from values”.


6. Delimiters and Brackets

The bracket picks the separator. A { } body terminates each entry with ;, except a match arm whose body is a block, which its brace ends (§6.1); a [ ], ( ), or < > list separates its entries with ,. The rule depends only on the bracket, not on the meaning of the construct, so a separator character never has two meanings in one context.

6.1 ; terminates an entry inside { }

A ; terminates every entry of a { } body: the members of a struct or variant type-definition body, marked or unmarked with #; the => expr arms of a match block; the fields of an init{ }; the entries of a field-constructor header or call site; the entries of an enum-map declaration (adt.md §6); and the entries of a map literal (§6.4). It is always trailing: every entry ends with a ;, inline or multiline, single-entry or many, because newlines are insignificant inside these bodies. The last entry carries a ; exactly like every other, so the form is uniform.

A match arm whose body is a { } block is the one entry that takes no ;: its closing brace ends the arm, as the brace that closes a statement ends the statement (§6.3). An => expr arm keeps its ;, including one whose expression ends in a }.

name String = match (color) {
    red => "Red";
    green {
        console!print("green");
        return "Green";
    }
    blue => "Blue";
}

Story: stories/adt.md — “A match parenthesizes its scrutinees, and a block arm ends on its brace”.

type Node = #struct {
    _id Int;
    scale Float;
    label String;
}

type Color = struct { r Int; g Int; b Int; }   // inline body, every entry ends in ';'

Vec2(x Float, y Float) => init{x; y;}

A , may still appear inside an entry, where it separates a nested list under §6.2 — a map entry’s key, value pair, a match arm’s per-scrutinee selectors, or the arguments of a call in a field’s initializer. The ; terminates the entry; a , separates parts within one.

6.2 , separates an entry inside [ ], ( ), and < >

A , separates the entries of a [ ], ( ), or < > list: array literals, an enum body, a match case group, a function-type parameter list, call and constructor arguments, parameter lists, and generic arguments and headers. It is never trailing: a , appears only between entries, never after the last entry. A list written with no bracket at all separates with , on the same terms: a match arm’s per-scrutinee selectors (syntax.md §4.7), and the key, value pair of a map entry (§2.10 there).

arr Array([Int(1), Int(2), Int(3)]);
type Colors = enum [ red, green, blue ];
Node(id Int, scale Float, label String)

6.3 ; terminates a statement and a declaration

A ; terminates every statement in a code block — a function body, a lambda body, or a block argument — and every package-scope declaration. Newlines are insignificant here as they are inside a { } body (§6.1), so two statements may share a line and one statement may span several.

Unit main() {
    x Int(5);
    console!print(x);
    return Unit();
}

A statement that ends with a } is the exception, and it takes no terminator: the brace itself ends the statement. A ; after it would mark nothing. Whatever comes after the brace is the next statement, so a ( or a [ written there opens a statement of its own rather than calling or subscripting the expression that the brace closed (syntax.md §4.8). A value closed by its own brace may still go on through a token that opens no statement — a ., :, !, ?, ??, or an operator, though a map literal takes the first three only once parenthesized — and the statement then ends where that longer expression does (syntax.md §4.9).

Unit main() {
    if(ready) {
        start();
    }
    console!print(done);
}

This applies to statements and to a match arm whose body is a block (§6.1). Every other entry of a { } body carries its ; unconditionally, including an entry whose value ends in a }, and that does not bend for the last entry or for any particular value shape.

init{
    handler = Unit() { fire(); };
}

A package-scope declaration follows the statement rule: it ends with a ;, unless it ends in a }, which ends it instead. A declaration is therefore written the same at package scope and inside a body.

package geometry;

import core$;

type Meters = Int;
type Point = struct { x Int; y Int; }

Int double(value Int) => value * 2;
Int triple(value Int) {
    return value * 3;
}

@primitives$I64 crc32(crc @primitives$I64, data @primitives$String) foreign "zane_crc32";

6.3.1 A { } may not open a statement

A code block is never written on its own. A { } in statement position is a compile-time error, so the only braces a reader meets at the start of a statement are braces belonging to a declaration. Scoping a run of work is an ordinary call taking a block argument — core’s do(block @concepts$Block) — which reads the same and needs no rule of its own.

do() {
    tmp Int(9);
    use(tmp);
}

6.4 Brackets and their separators

Each bracket takes exactly one separator, so the bracket predicts both the mark and its trailing behaviour:

Bracket Encloses Separator
{ } a body of entries: struct, variant, and their # forms; a match block of arms; an init{ }; a field-constructor header or call site; an enum-map declaration; a map literal ;, always trailing, except after a block-bodied match arm (§6.1)
{ } a code block: a function body, a lambda body, or a block argument ;, terminating each statement (§6.3)
[ ] a flat list: an array, an enum body, a match case group, a function-type parameter list ,, never trailing
( ) a parameter list or an argument list ,, never trailing
< > a generic header or a generic argument list ,, never trailing

A { } is the one bracket with two readings. Most are introduced by a token that shows which reading applies — a mould keyword, match, init, a type name, or a verb’s signature. A map literal and a block argument are the two readings with no such token, and both hold ;-terminated entries or statements, so the ; alone does not distinguish them. In argument position, where both can appear, the parser distinguishes them by the mark after the first entry: it parses one expression, then reads the next mark. A , opens a map entry’s value; a ; ends a statement. A map literal is never empty, so a bare {} is a block (syntax.md §2.10).

f({ key, value; });  // a map literal: ',' follows the first expression
f({ doWork(); });    // a block argument: ';' follows the first expression

A match written as that first expression keeps its own commas inside its brackets: its scrutinees sit in its ( ) and its arms’ selectors in its { }, so neither is read as the , that opens a map entry’s value.

See also: syntax.md §1 for declaration forms and adt.md for how these delimiters apply across enum, variant, and match. Story: stories/lexical.md — “Declarations end the way statements do” explains why package-scope declarations took the statement rule. Story: stories/lexical.md — “Statements end in ; so a trailing brace can end one” explains why statements gained a ; and what it bought. Story: stories/lexical.md — “The enum map takes braces” explains why the enum map changed brackets to match. Story: stories/lexical.md — “Telling a body brace from a code block by its content” explains why a map literal carries no leading token and why no literal may be empty. Story: stories/lexical.md — “The bracket picks the separator”. Story: stories/lexical.md — “A delimiter for each separated construct” explains where the two marks and the trailing asymmetry came from.


7. Literals

A numeric literal is written in decimal digits, in one of two forms, and the form determines its concept type (syntax.md §2.8):

  • An integer literal is one or more digits. It carries @concepts$Int.
  • A float literal is one or more digits, a ., and one or more digits. It carries @concepts$Float.

A numeric literal consists of digits, at most one ., which has at least one digit on each side, and any number of ' digit separators. A separator stands between two digits before the ., and it only groups them: it changes neither the literal’s value nor its concept type. The digits after a float literal’s . take no separator.

3          // integer literal: @concepts$Int
3.1        // float literal: @concepts$Float
3.0        // float literal: the `.` makes it a float literal, whatever the value
1'000'000  // integer literal: the same value as 1000000
1'000.25   // float literal: the same value as 1000.25
3.         // ILLEGAL: a digit must follow the `.`
.5         // ILLEGAL: a digit must precede the `.`
1.000'001  // ILLEGAL: a separator after the `.`
1''000     // ILLEGAL: a separator stands between two digits
1'000'     // ILLEGAL: a separator stands between two digits

A numeric literal is written without a sign. ~ before one gives the literal of the opposite sign (operators.md §2.7), so ~3 is a negative integer literal.

Because the spelling determines the concept type, 3 and 3.0 are different arguments: a destination that accepts only integers accepts the first and rejects the second (types.md §2.6).

Story: stories/generics.md — “Splitting integer and float literals”. Story: stories/types.md — “How a literal lowers into primitive storage” explains how the concepts took their names. Story: stories/lexical.md — “Digit groups take the Swiss apostrophe”.

7.1 String literals and structural escapes

A string literal is enclosed in double quotes and may be empty. Only an unescaped double quote closes it. Inside the delimiters the compiler scans left to right:

Source form Literal structure
\" One literal double quote; it does not close the string
\\ One literal backslash; the character following the pair is scanned normally
\%name An interpolation site naming one value identifier
Any other backslash sequence Literal text, with the backslash preserved

After \%, the scanner consumes the longest value identifier allowed by §4, including a leading private underscore where legal. A missing identifier or a type identifier is a compile-time error. Identifier punctuation is not consumed: "\%name.txt" interpolates name and then contains the literal text .txt. A field or call result must first be bound to a value symbol to use it in interpolation. An unclosed string is a lexical error.

"say \"hello\""  // literal fragment: say "hello"
"\\%name"        // literal fragment: \%name; no interpolation
"\w+"           // literal fragment: \w+; compiler preserves the backslash
"\n"            // two literal characters; the consumer interprets them
"\%name!"       // interpolation of name, followed by !

Structural escapes are recognized once. Neither a backslash produced by \\ nor bytes inserted by interpolation are rescanned for another language escape. The consumer interprets the remaining text by its own rules, or reads the text escapes in it with @primitives$String.escaped (types.md §2.7).

See also: syntax.md §2.15 for surface forms. types.md §2.8 for interpolation resolution, copying, and concept typing. Story: stories/lexical.md — “Literal structure and consumer interpretation”.


8. Summary

Concept Rule
Case sensitivity Identifiers compare by exact bytes; case is never folded
Type names Uppercase-initial; a lowercase name in a type position is a compile-time error
Value names Lowercase-initial; bindings, parameters, and fields
Package names camelCase (lowercase-initial); appear only after package, after import, after the as of a whole-package import alias, or as the left operand of $
Number parameter A lowercase name (n) declared n @concepts$Int (in a type’s or a package’s <> header, or inline in a verb); a compile-time integer, not a type
Type parameter An uppercase name (T) declared T Type (in a type’s or a package’s <> header, or inline in a verb); referenced bare
Digits Legal in an identifier except as the first character; carry no special meaning
Leading _ A field is private to its type’s package; a named package-scope declaration is private to its package
Leading & &Node is a reference type, legal in storage, parameter, and return positions; it is the only marker a type may carry, and it is never written on this
<> disambiguation A type (uppercase) on the left marks a type argument list; a value (lowercase) marks comparison
Entry terminator ; terminates every entry of a { } body (struct/variant members marked or unmarked with #, => expr match arms, init{ } fields, field-constructor entries, enum-map entries); always trailing, inline or multiline; a block-bodied match arm is ended by its brace instead; newlines are insignificant there
Entry separator , separates the entries of a [ ], ( ), or < > list (arrays, enum, match case groups, function-type parameter lists, call/constructor args, parameter lists, generic args and headers); never trailing
Statement and declaration terminator ; terminates every statement in a code block and every package-scope declaration, except one ending in } — that brace ends it, so a ( or [ after it opens the next statement; newlines are insignificant
Brackets The bracket picks the separator: { } takes ; as a body and as a code block, [ ]/( )/< > take ,
Integer literal One or more digits, which a ' may group; carries @concepts$Int
Float literal Digits, a ., and digits, with at least one digit on each side; a ' may group the digits before the .; carries @concepts$Float, whatever its value
String literal Double-quoted; \" inserts a quote, \\ inserts a backslash, \%name interpolates; other backslash sequences remain consumer-interpreted text
Statement blocks A { } may not open a statement; a scoped run of work is a call taking a block argument (do(block))