Grammar
The whole syntax of jihoo, as the parser (compiler/parse.jh) reads
it. Syntax explains it with examples; this page is
the precise version, for checking what parses.
Notation
x y x, then y
x | y x or y
[ x ] x, or nothing
{ x } x any number of times, maybe none
"fn" the token itself
name an identifier: a letter or `_`, then letters, digits and `_`
sep a `,`, a `;`, or a newline before the next element
Tokens
-
Whitespace is spaces, tabs,
\rand newlines. The lexer records whether a newline came before each token; that is all the parser knows of lines. -
// ...to the end of the line;/* ... */nests. -
Integers:
123,1_000,0xff,0b1010,0o17. Floats:1.5,2e10,1.5e-3(decimal only;1.and.5are not floats). After a., digits are always an integer, sot.0.1is two tuple fields. -
'a'is one byte (u8);'가'is an error, since it is more than one byte."..."is a string, on one line. Escapes in both:\n \t \r \0 \\ \' \" \xNN. -
The keywords:
as break comptime const continue else enum export extern false fn for if impl import in inout let loop match move pub ref return sink static struct trait true type unsafe var whileself,Self,null,fnptr,int,never,inline(beforefororif),dyn(before a name orfn) and the primitive types are ordinary names that the compiler gives a meaning to;_is a name too, except in patterns. -
..=is two tokens:..then=.
Files
module = { "#![" name "]" } { item | ";" }
item = { attr } [ "pub" ] ( import | fn | struct | enum | impl | trait
| const | static | alias | family )
attr = "#[" name [ "(" name { "," name } ")" ] "]"
import = "import" name { "." name } [ "as" name ]
fn = [ "unsafe" ] [ "extern" | "export" ] "fn" name params
[ "->" [ "ref" | "inout" ] type ] [ block ]
params = "(" [ param { "," param } [ "," ] ] ")"
param = "..." (last; `extern fn` only)
| [ "inout" | "sink" | "comptime" ] name [ ":" type { "+" type } ]
struct = "struct" name [ params ]
"{" { [ "pub" ] name ":" type sep } "}"
enum = "enum" name [ params ] "{" { name [ "(" type { "," type } ")" ] sep } "}"
impl = "impl" [ params ] type [ "for" type ] "{" { item | ";" } "}"
trait = "trait" name [ params ] [ ":" type { "+" type } ] "{" { item | ";" } "}"
const = "const" name [ ":" type ] "=" expr
static = "static" name [ ":" type ] "=" expr
alias = "type" name [ "=" type ]
family = "type" name "{" equation { sep equation } [ sep ] "}"
equation = type_pat "=>" type
type_pat = "_" | "let" name | path [ "(" [ type_pat { "," type_pat } [ "," ] ] ")" ]
| "(" [ type_pat { "," type_pat } [ "," ] ] ")"
| "[" type_pat [ ";" type_pat ] "]" | ( "*" | "ref" | "inout" ) type_pat
| [ "-" ] integer | "true" | "false" | "type"
#![...]attributes come first in the file.#![freestanding]and#![uefi]select runtime profiles. Item attributes are#[view],#[derive(...)](derive),#[interior](unsafe code) and, on functions,#[literal_check(param, check)](compile-time evaluation)#[test](tests),#[panic_handler](panic handlers),#[no_alloc]and#[no_panic](effect attributes), and#[reversible(inverse_name)](reversible functions). The reversible attribute takes one identifier and declares an inverse of that name in the containing module, with the same parameter modes and result type as the forward function. A reversible function takes at least oneinoutparameter and can pair onesink Twith a by-value result-> Tand a finalmoveof that slot.#[erasure(budget, record_name, undo_name)]takes an integer literal or an integer constant’s identifier as its first argument and two function names. It marks a safe nongeneric free function with one by-valueu8oru16parameter and abool,u8, oru16value result. Its module importsstd.erasure. Both companions inherit the function’s visibility. Exact erasure budgets- A parameter’s type may be left out only for
self. A function without a block is a declaration: anextern fn, or a trait function without a default body. Items do not nest inside function bodies. - The parameters of a
struct,enumortraitare compile-time ones whether or not they saycomptime:struct Pair(T: type). Those of animplare written the same way:impl(T: Ord) Ord for Vec(T). type Itemwithout= Tdeclares an associated type in a trait. Type aliases take no parameters.- A family is a module item.
Name(T)applies its equations to one concrete type. A tuple supplies several inputs as one type. A single parenthesized pattern groups its contents. Closed type families
Types
type = path [ "(" expr { sep expr } ")" ] Vec(i64), std.map.Map(K, V)
| "*" type raw pointer
| "[" type "]" slice
| "[" type ";" expr "]" array
| "(" ")" | "(" type ")" unit; parentheses
| "(" type "," [ type { "," type } ] ")" tuple
| "fn" fn_ty | "fnptr" fnptr_ty closure; function pointer
| "dyn" path | "dyn" "fn" fn_ty owned trait value; owning closure
| "impl" path | "impl" "fn" fn_ty a type known while compiling
| ( "ref" | "inout" ) type a borrow, as a type argument
| "type" the type of types
path = name { "." name }
fn_ty = "(" [ fn_ty_param { "," fn_ty_param } ] ")" [ "->" type ]
fn_ty_param = [ "inout" | "sink" ] type
fnptr_ty = "(" [ [ "inout" | "sink" ] type { "," [ "inout" | "sink" ] type }
[ "," "..." ] | "..." ] ")" [ "->" type ]
... in a function pointer requires a C-compatible value signature.
A function type lists types, not names: fn(i64, i64) -> i64.
world is contextual. The binder expression must occupy the entire body
of a named, safe, nongeneric free function. In a world-local helper,
the first parameter is comptime W: world. The type applications
WorldId(W) and WorldArena(T, W) resolve W in the world namespace.
Closed monotone arena worlds
Type arguments are expressions, so a number can be one ([T; N],
Matrix(f64, 3)). The ( of type arguments, and the -> of a function
type, must be on the same line as what comes before.
Blocks and statements
block = "{" { stmt end } [ expr ] "}"
end = ";" | newline | before "}"
stmt = ( "let" | "var" | "inout" ) pattern [ ":" type ] "=" expr [ "else" block ]
| expr "=" expr
| expr opassign expr
| expr
opassign = "+=" | "-=" | "*=" | "/=" | "%=" | "&=" | "|=" | "^=" | "<<=" | ">>="
| "+%=" | "-%=" | "*%="
A block whose last statement is an expression with no ; after it has that
expression’s value. The = or op= of an assignment must be on the same
line as its left side, and the else of a let on the same line as the
end of its value.
Expressions
expr = range | binary
range = [ binary ] ".." [ binary ] only as a whole expression
| binary ".." "=" binary
binary = unary { binop unary } by precedence, below
| binary "as" type
unary = ( "-" | "!" | "*" | "&" | "move" ) unary
| primary { postfix }
postfix = "(" [ expr { sep expr } ] ")" call
| "[" expr "]" index or slice
| "." name | "." integer field, method, tuple field
| "?" early return on None / Err
| "{" [ field { sep field } ] "}" struct literal, after a path
field = name [ ":" expr ]
primary = literal | name | "(" ")" | "(" expr ")"
| "(" expr "," [ expr { "," expr } ] ")" tuple
| "[" [ expr { sep expr } ] "]" array
| "[" expr ";" expr "]" array of copies
| block | "unsafe" block | "comptime" unary
| "world" name "(" name ":" type ")" block
| "if" cond block [ "else" ( if | block ) ]
| "match" cond "{" { pattern [ "if" expr ] "=>" stmt sep } "}"
| "while" cond block
| "loop" block
| "for" [ name "," ] pattern "in" cond block
| "inline" "for" name "in" cond block unrolled at compile time
| "inline" "if" cond block [ "else" ( "if" cond block ... | block ) ]
| "fn" "(" [ cparam { "," cparam } ] ")" [ "->" type ] block closure
| "fn" fn_ty | "fnptr" fn_ty | "dyn" type | ( "ref" | "inout" ) type a type
| "return" [ expr ] | "break" | "continue"
cparam = name [ ":" type ]
cond = expr, where a struct literal cannot start (write `(P { x: 1 })`)
In an integer for or inline for, lo..hi excludes hi and lo..=hi
includes it. Both loop forms require both bounds. A slice uses only ..,
with either bound optional.
Precedence
From loosest to tightest. Binary operators are left-associative.
| level | operators |
|---|---|
| 0 | a..b, a..=b (a whole expression only) |
| 1 | || |
| 2 | && |
| 3 | == != < <= > >= (comparisons do not chain; use &&) |
| 4 | | |
| 5 | ^ |
| 6 | & |
| 7 | << >> |
| 8 | + - +% -% |
| 9 | * / % *% |
| 10 | e as T, which applies to the operand just before it |
| 11 | prefix - ! * & move |
| 12 | postfix f(x) a[i] a.b t.0 e? P { ... } |
So c as i64 * 2 is (c as i64) * 2 and -x as u64 is (-x) as u64.
Unlike C, the bitwise operators are tighter than comparisons, as in Rust:
a & mask == 0 is (a & mask) == 0. Shifts are looser than +:
1 << 2 + 1 is 1 << 3.
Lines
A statement ends at a newline, unless the expression cannot end there. The rules:
- A binary operator continues the expression only at the end of a line,
not at the start of the next one.
a\n+ bisa, then the statement+ b, which is an error. - A
.continues the expression from either line, so method chains may start each line with.method(). (,[and?at the start of a line do not apply to what came before:f\n(x)isf, then(x).as,..,..=,=andop=must be on the line of their left side. Inlo..=hi, the upper bound must start on that line too.returntakes a value only on its own line:return\nxreturns nothing.- Inside
( ),[ ]and{ }lists (arguments, arrays, struct fields, enum variants,matcharms), a newline separates elements like a comma.
Closures and types
fn(...) starts a closure when a block follows, and is a type otherwise:
fn(x, y: i64) { ... } is a closure (parameter types are optional),
fn(i64) -> bool is a type, written where a value is expected only as a
compile-time argument. move fn(x) { ... } is a closure that owns what
it captures. impl fn(A) -> R and impl Trait are types only as the bound
of a compile-time type parameter (comptime F: impl fn(i64) -> i64) and
as a function’s result (-> impl Iterator).
Struct literals
After a path (P, m.P, Pair(i64)), a { on the same line starts a
struct literal if it is followed by }, by name:, by name, or by
name }. In the head of if, while, match and for it never does, so
the { there is the body; put a struct literal there in parentheses.
Patterns
pattern = single { "|" single }
single = "_"
| [ "-" ] integer | char a literal
| lit ".." [ "=" ] lit a range, both ends included
| "true" | "false" | string
| name binds, or a variant without payload
| "var" name a binding that can change
| "(" pattern { "," pattern } ")" tuple
| path "(" [ pattern { "," pattern } ] ")" variant with payload
| path "{" { name [ ":" pattern ] sep } [ ".." ] "}" struct
lit = [ "-" ] integer | char
- A range pattern includes both ends whichever way it is written:
0..9and0..=9both match 9. (In aforloop, only0..=9visits 9.) - A single name is a variant if the matched type has a variant of that name
without payload (
Stop,None); otherwise it binds. - The alternatives of
A | Bbind the same names, with the same types andvar-ness; the first that matches binds them. A|may also appear inside other patterns:(Some(x), None) | (None, Some(x)).