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 by ochre fmt.

Reserved Keywords

CategoryKeywords
Declarationsfn, let, var, struct, enum, impl, import
Control Flowif, else, match, while, for, in, return, break, continue
Concurrencyspawn
Literals & Constructorstrue, false, Some, None, Ok, Err

Literals

KindSyntax Examples
Integer0, 42, 1_000_000
Float3.14, 0.5, 1_000.25
Booleantrue, 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 LiteralPoint { x: 1, y: 2 }
Enum VariantColor.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 nested a[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)

PatternMatches
_Wildcard (matches anything, binds nothing)
identCatch-all binding (binds matched value to ident)
42, true, "ok"Literal Int, Bool, or Str
Some(x) / NoneOption variants
Ok(v) / Err(e)Result variants
Enum.Variant / Enum.Variant(a, b)Enum variants and payload bindings