Grammar
The formal grammar is grammar.ebnf. It covers the lexical layer and the surface syntax of v2.
(* =====================================================================
Lingua Tungga — Grammar v2.1
Scope: v2 — `class` removed, replaced by `package` + `struct` + receiver
functions. See docs/v2-decisions.md for the rationale of each decision.
Import v3 amendment (target 0.7.0): exactly one package per file,
extension-less specifiers, and `export import`. See docs/import-v3.md.
Notation: EBNF, `,` = sequence, `[ ]` = optional, `{ }` = zero or more,
`|` = alternative.
===================================================================== *)
(* ---------- Program ---------- *)
Program = { Import } , PackageDecl , EOF ;
(* Import points to a PACKAGE, not a file. Local specifiers start with `./` or
`../` without an extension; external specifiers have the form `<key>/<path>`
with `key` from `[dependencies]` in lt.toml. The package name is stated
explicitly and verified to exist in the target file.
Entries may stand alone or be grouped in one `( ... )` block, one entry per
line without commas. `export import` stands alone only, must have an item
list, and may not appear inside a group. *)
Import = "import" , ( ImportEntry | ImportGroup )
| "export" , "import" , ExportEntry ;
ImportGroup = "(" , { ImportEntry } , ")" ;
ImportEntry = Identifier , [ "as" , Identifier ] ,
[ "{" , ImportList , "}" ] , StringLit ;
(* Explicit re-export: the qualifier stays locally bound, but only the items
inside `{ ... }` enter the package API. No wildcards. *)
ExportEntry = Identifier , [ "as" , Identifier ] ,
"{" , ImportList , "}" , StringLit ;
ImportList = ImportItem , { "," , ImportItem } ;
ImportItem = Identifier , [ "as" , Identifier ] ;
(* Must wrap every declaration. No bare declarations at top level, no nested
packages. Exactly one per file. *)
PackageDecl = "package" , Identifier , "{" , { PackageItem } , "}" ;
PackageItem = StructDecl | EnumDecl | FnDecl | Binding | TestDecl ;
(* ---------- Declarations ---------- *)
StructDecl = [ "export" ] , "struct" , Identifier , [ Generics ] ,
"{" , { FieldDecl } , "}" ;
(* `embed` is a contextual marker before the field name. A field actually named
`embed` is written `embed: Type` and is not treated as a marker. *)
FieldDecl = [ "embed" ] , Identifier , ":" , Type ;
EnumDecl = [ "export" ] , "enum" , Identifier , [ Generics ] ,
"{" , { Variant } , "}" ;
Variant = Identifier , [ "(" , Type , { "," , Type } , ")" ] ;
(* No methods inside types. Receiver functions are ordinary functions whose
first parameter is of struct/enum type, and must live in the same package as
that type. *)
FnDecl = [ "export" ] , "fn" , Identifier ,
"(" , [ ParamList ] , ")" , [ "->" , Type ] , Block ;
ParamList = Param , { "," , Param } ;
Param = Identifier , ":" , Type ;
(* Package-level binding: immutable, eager, without initialization cycles. *)
Binding = [ "export" ] , "let" , Identifier , [ ":" , Type ] , "=" , Expr ;
(* Test titles are string literals, unique per package. *)
TestDecl = "test" , StringLit , Block ;
(* ---------- Types ---------- *)
Type = TypePath , [ Generics ] , [ "?" ]
| "fn" , "(" , [ Type , { "," , Type } ] , ")" , [ "->" , Type ] ;
(* At most 3 segments: package.Type , package.Type.Variant , package.fn.
This limit is enforced semantically, not syntactically. *)
TypePath = Identifier , { "." , Identifier } ;
Generics = "<" , Type , { "," , Type } , ">" ;
(* ---------- Statement ---------- *)
Block = "{" , { Stmt } , "}" ;
Stmt = LetStmt | AssignStmt | ReturnStmt
| IfStmt | ForStmt | WhileStmt | BreakStmt | ContinueStmt | ExprStmt ;
(* Statements are separated by newlines inside a Block. *)
BreakStmt = "break" ;
ContinueStmt = "continue" ;
LetStmt = "let" , [ "mut" ] , Identifier , [ ":" , Type ] , "=" , Expr ;
AssignStmt = Identifier , "=" , Expr ;
ReturnStmt = "return" , [ Expr ] ;
IfStmt = "if" , Expr , Block , [ "else" , ( IfStmt | Block ) ] ;
(* Iterable: List (elements), Map (key), Dict (key). Set is not supported yet. *)
ForStmt = "for" , Identifier , "in" , Expr , Block ;
WhileStmt = "while" , Expr , Block ;
ExprStmt = Expr ;
(* ---------- Expressions ----------
`if` and `match` are expressions; as statements they may be followed without
a semicolon because statements are separated by newlines. *)
Expr = Lambda | OrExpr ;
Lambda = "fn" , "(" , [ LambdaParams ] , ")" , "=>" , ( Expr | Block ) ;
LambdaParams = LambdaParam , { "," , LambdaParam } ;
LambdaParam = Identifier , [ ":" , Type ] ; (* the type may be inferred from context *)
OrExpr = AndExpr , { "||" , AndExpr } ;
AndExpr = EqExpr , { "&&" , EqExpr } ;
EqExpr = RelExpr , [ ( "==" | "!=" ) , RelExpr ] ;
RelExpr = AddExpr , [ ( "<" | "<=" | ">" | ">=" ) , AddExpr ] ;
AddExpr = MulExpr , { ( "+" | "-" ) , MulExpr } ;
MulExpr = UnaryExpr , { ( "*" | "/" | "%" ) , UnaryExpr } ;
UnaryExpr = [ ( "!" | "-" ) ] , PostfixExpr ;
PostfixExpr = Primary , { PostfixOp } ;
(* `p.f()` on a struct-typed value whose package has a receiver function `f`
is sugar for `f(p)`. There is no dynamic dispatch. *)
PostfixOp = "." , Identifier
| "(" , [ ArgList ] , ")"
| "[" , Expr , "]" (* partial lookup: Option<T> *)
| "?" ;
ArgList = NamedArg , { "," , NamedArg }
| Expr , { "," , Expr } ; (* must not be mixed *)
NamedArg = Identifier , ":" , Expr ; (* only valid in struct construction / JSX *)
Primary = IntLit | FloatLit | StringLit | BoolLit
| "None"
| Identifier , [ Generics ] (* variable / type reference *)
| ListLit
| DictLit
| Template
| TaggedTemplate
| Jsx
| IfExpr
| MatchExpr
| "(" , Expr , ")" ;
IfExpr = "if" , Expr , Block , [ "else" , ( IfExpr | Block ) ] ;
MatchExpr = "match" , Expr , "{" , { MatchArm } , "}" ;
MatchArm = Pattern , { "|" , Pattern } , [ "if" , Expr ] , "=>" , ( Expr | Block ) ;
Pattern = "_"
| "None"
| TypePath , [ "(" , [ Pattern , { "," , Pattern } ] , ")" ] ;
ListLit = "[" , [ Expr , { "," , Expr } ] , "]" ;
(* Dict: keys are always string literals; an empty one is written "[:]". *)
DictLit = "[" , ( ":" | StringLit , ":" , Expr , { "," , StringLit , ":" , Expr } ) , "]" ;
(* ---------- Template ---------- *)
Template = "`" , { TemplatePart } , "`" ;
TemplatePart = Text , [ "${" , Expr , "}" ] ;
(* A tag is a path to a package-level function, not a method. *)
TaggedTemplate = TypePath , Template ;
(* ---------- JSX ---------- *)
(* Tag name = a struct name visible in scope. Attribute = field name.
Lowercase = compile error (type names must be PascalCase). *)
Jsx = "<" , TypePath , { JsxAttr } , ( "/>" | ">" , { JsxChild } , "</" , TypePath , ">" ) ;
JsxAttr = Identifier , "=" , ( StringLit | "{" , Expr , "}" ) ;
JsxChild = Jsx | JsxText | "{" , Expr , "}" ;
JsxText = { Character - ( "<" | "{" ) } ;
(* ---------- Lexical ---------- *)
Identifier = letter , { letter | digit | "_" } ;
IntLit = digit , { digit | "_" } ;
FloatLit = digit , { digit | "_" } , "." , digit , { digit | "_" } ,
[ ( "e" | "E" ) , [ "+" | "-" ] , digit , { digit } ] ;
BoolLit = "true" | "false" ;
StringLit = '"' , { StringChar } , '"' ;
StringChar = ordinary_char | Escape ;
Escape = "\\" , ( "n" | "t" | "r" | "\\" | '"' | "`" | "$" )
| "\\u{" , hex , { hex } , "}" ;
Comment = "//" , { any - newline } , newline
| "/*" , { any - "*/" } , "*/" ; (* not nested *)
(* ---------- Keywords (reserved) ---------- *)
(* package export struct enum fn test let mut return if else
for in while match import from as true false None _ *)
(* v1 keywords that were removed STAY reserved so the errors are clear:
class pub static self *)
(* `embed` is not a full keyword: it is only a marker when followed by a name
and `:` inside a struct body; `embed` can still be a field name. *)
(* Soft keywords (see spec §7). Positions that are definitely names accept
keywords: after ".", and before ":" (field, label, parameter, JSX attribute).
Function/binding/package names and import items only accept from, as, in. *)
NameAfterDot = Identifier | Keyword ; (* b.from, b.match *)
SoftName = Identifier | "from" | "as" | "in" ;
FieldName = NameAfterDot ; (* always followed by ":" *)
AttrName = NameAfterDot ; (* always followed by "=" *)
The grammar describes syntax only. Type checking, naming rules, exhaustiveness, visibility, and import resolution are specified in the language specification.