Reference · 01
Complete Language Syntax
Formal syntax reference for Ochre v0.1.0-alpha.4 — keywords, comments, literals, declarations, statements, expressions, and patterns.
This page documents the exact lexical and syntactic grammar accepted by ochre-syntax in Ochre v0.1.0-alpha.4.
Comments & Whitespace
- Single-line comments:
// comment to end of line - Block comments (nesting supported):
/ outer / inner / outer / - Semicolons (
;) are optional; newlines terminate statements cleanly. Both//and/ /comments are preserved byochre fmt.
Reserved Keywords
| Category | Keywords |
|---|---|
| Declarations | fn, let, var, struct, enum, impl, import |
| Control Flow | if, else, match, while, for, in, return, break, continue |
| Concurrency | spawn |
| Literals & Constructors | true, false, Some, None, Ok, Err |
Literals
| Kind | Syntax Examples |
|---|---|
| Integer | 0, 42, 1_000_000 |
| Float | 3.14, 0.5, 1_000.25 |
| Boolean | true, false |
| Interpolated String | "hello {name}", "escaped \{brace\} \n \t \\ \"" |
| Raw String | '{"a": 1}', 'C:\path\no\escapes' |
| List | [], [1, 2, 3] |
| Map | #{}, #{ "k": 1, "m": 2 } |
| Struct Literal | Point { x: 1, y: 2 } |
| Enum Variant | Color.Red, Role.Member(3) |
Top-Level Items
import "<relative-path.och>"
struct Name {
field: Type,
}
enum Name {
UnitVariant,
TupleVariant(Type1, Type2),
}
impl StructName {
fn method(self, param: Type) -> ReturnType { ... }
}
fn name(param: Type) -> ReturnType { ... }
Statements & Expressions
- Variable bindings:
let x = expr,let x: Type = expr,var x = expr - Assignments:
x = v,x += v,x -= v,x *= v,x /= v(also nesteda[i].b[j] = v) - Loops:
while cond { ... },for x in iter { ... },break,continue - Closures:
() => expr,(x) => expr,(x: Int, y: Int) -> Int => { ... } - Ranges:
start..end(exclusive upper bound) - Branching:
if cond { ... } else if cond2 { ... } else { ... } - Pattern matching:
match scrutinee { Pattern => expr, ... } - Concurrency:
spawn { ... }
Patterns (in match)
| Pattern | Matches |
|---|---|
_ | Wildcard (matches anything, binds nothing) |
ident | Catch-all binding (binds matched value to ident) |
42, true, "ok" | Literal Int, Bool, or Str |
Some(x) / None | Option variants |
Ok(v) / Err(e) | Result variants |
Enum.Variant / Enum.Variant(a, b) | Enum variants and payload bindings |