Lex
Menu
On this page

Language Reference

Lex is one typed language served by one parser and one bytecode evaluator in two closed evaluation modes. Pure expressions produce the exact same value in both modes, and every refusal reports a line, column, and failure kind.

Evaluation modes

PropertyConsole modeDeclaration mode
StrategyForces each line whole, left to right, including every let bindingLazy thunk graph; forces only the requested field path and its dependencies
EffectsPermitted through host-provided commandsRefused at the border with Effect_Refused before arguments run
Input formSingle line up to 4096 bytes; supports top-level name = exprMulti-line .lex file up to 16 MiB; supports trailing commas; refuses top-level assignment
Cross-file loadingHost-definedimport("path.lex") and declaration(blake3:...) read from the caller-supplied loader

Complete grammar

The parser reads |> and _ directly into compact 16-byte syntax nodes without text rewriting.

line          = name "=" pipeline | pipeline
pipeline      = logical_or { "|>" stage }
stage         = name | name "(" [ arguments ] ")"
logical_or    = logical_and { "||" logical_and }
logical_and   = comparison { "&&" comparison }
comparison    = coalesce [ ("=" | "!=" | "<" | "<=" | ">" | ">=") coalesce ]
coalesce      = with_expr [ "??" coalesce ]
with_expr     = sum { "with" sum }
sum           = term { ("+" | "-" | "++") term }
term          = set { ("*" | "/" | "%") set }
set           = factor { "|" factor }
factor        = ("-" | "!") factor
              | "if" pipeline "then" pipeline "else" pipeline
              | "let" binding { "," binding } "in" pipeline
              | "match" pipeline "{" match_arm { "," match_arm } [ "," ] "}"
              | postfix
postfix       = primary { "." field_name }
primary       = integer [ unit_name ]
              | "true" | "false"
              | text
              | hash
              | "." name
              | name "(" [ arguments ] ")"
              | name record_lit
              | name
              | "(" [ param { "," param } ] ")" "=>" pipeline
              | "(" pipeline ")"
              | record_comp
              | record_lit
              | "[" [ pipeline { "," pipeline } [ "," ] ] "]"
param         = name [ ":" kind_annot ] [ "=" pipeline ] | record_pat
kind_annot    = kind_name | "Integer" "[" unit_name "]"
binding       = (name | record_pat) "=" pipeline
record_pat    = [ name "@" ] "{" [ pat_field { "," pat_field } [ "," ] ] [ "..." [ "," ] ] "}"
pat_field     = name [ ":" kind_annot ] [ "=" pipeline ]
record_lit    = "{" [ field_init { "," field_init } [ "," ] ] "}"
field_init    = field_path "=" pipeline | name
field_path    = name { "." name }
record_comp   = "{" "for" (name | record_pat) "in" pipeline [ "if" pipeline ] ":" coalesce "=" pipeline "}"
match_arm     = match_pat [ "if" pipeline ] "=>" pipeline
match_pat     = "_" | name | literal | "." name | match_rec_pat
match_rec_pat = [ name "@" ] "{" [ name [ "=" match_pat ] { "," name [ "=" match_pat ] } [ "," ] ] [ "..." [ "," ] ] "}"
arguments     = argument { "," argument }
argument      = [ name "=" ] (pipeline | "_")
hash          = "blake3:" 64 * lower_hex_digit
comment       = "//" { byte except line end }

Names, reserved words, and comments

Values and kinds

KindLiteral formRules and bounds
Integer0, 42, -7, 64 MiBSigned 64-bit integer with overflow checks on +, -, *, and /. No operation wraps.
Booleantrue, falseRequired by if, &&, ||, !, and predicates. Integers never coerce to booleans.
Text"hello ${name}"Valid UTF-8 with \\, \", \$, \u{XXXXXX} escapes and ${expr} interpolation.
Hashblake3:<64 lower hex>Exact 32-byte BLAKE3 digest. Uppercase hex or wrong length refuses with Hash_Not_Valid.
Member.french, .ReadClosed enum member literal.
Flag_Set.Read | .WriteUnordered set of unique enum members. Duplicate members refuse with Set_Member_Twice.
Record{ x = 0, width = 10 }Lazy key-value record with canonical sorted field shape.
List[1, 2, 3], []Ordered sequence of values; concatenated with ++.
Function(x, y = 1) => x + yUp to 12 parameters; captures enclosing bindings by thunk reference; supports kind annotations and defaults.

Bindings, destructuring, and lambdas

Bindings in one let block can reference earlier or later sibling bindings. Record destructuring is strict by default and refuses extra fields unless ... is present.

let
  width = 6,
  cfg = { host = "local", scale = 3, extra = 99 },
  area = ({ w: Integer, h: Integer = 1 }) => w * h
in
  let { scale, offset = 0, ... } = cfg in area({ w = width, h = scale }) + offset
=>18

Strict overrides with with

base with override checks that every field path in override already exists in base before forcing field values. Nested dotted paths update deep fields lazily while preserving sibling thunks.

let
  defaults = { net = { host = "localhost", port = 8080 }, retries = 3 }
in
  defaults with { net.host = "cache.modus.systems", net.port = 5010 }
=>{ net = { host = "cache.modus.systems", port = 5010 }, retries = 3 }

To add new fields that do not exist in base, call extend(base, extra) or merge_deep(base, extra).

Record comprehensions

Record comprehensions construct canonical sorted records in one pass with lazy values in declaration mode.

{ for item in [{ k = "b", v = 2 }, { k = "a", v = 1 }]: item.k = item.v * 10 }
=>{ a = 10, b = 20 }

Pattern matching with match

match tests literals, enum members, nested record patterns with optional name @ and ..., variable bindings, wildcards _, and boolean if guards. Uninspected record fields stay lazy, and unmatched values refuse with No_Matching_Arm.

let
  cfg = { mode = .Verify, net = { port = 5010 }, unused = 1 / 0 }
in
  match cfg {
    { mode = .Verify, net = { port = p }, ... } if p > 1024 => p,
    { mode = .Fast, ... } => 80,
    _ => 8080,
  }
=>5010

Safe field fallback with ??

Lex has no null value. Right-associative ?? evaluates its fallback lazily only when a field along the left selection chain is absent. Errors inside present fields propagate instead of being masked.

let
  cfg = { net = { host = "localhost" } }
in
  cfg.net.port ?? 8080
=>8080

Pipelines |> and placeholder _

The pipe operator |> passes its left value as the first argument of the right call, or into the explicit _ placeholder position.

[1, 2, 3, 4]
  |> filter((x) => x % 2 = 0)
  |> map((x) => x * 4)
  |> fold(0, (acc, x) => acc + x)
=>24

Resource bounds and refusals

Every evaluation enforces bounded execution without ambient I/O: