Skip to main content

Compact grammar

Compact language version 0.22.0.

Notational note: In the grammar below, keywords and punctuation are in monospaced font. Terminal and nonterminal names are in emphasized font. Alternation is indicated by a vertical bar (|). Optional items are indicated by the superscript opt. Repetition is specified by ellipses. The notation XX, where X is a grammar symbol, represents zero or more occurrences of X. The notation X ,, X, where X is a grammar symbol and , is a literal comma, represents zero or more occurrences of X separated by commas. In either case, when the ellipsis is marked with the superscript 1, the notation represents a sequence containing at least one X. When such a sequence is followed by ,opt, an optional trailing comma is allowed, but only if there is at least one X. For example, idid represents zero or more ids, and expr , ⋯¹, expr ,opt represents one or more comma-separated exprs possibly followed by an extra comma. The rules involving commas apply equally to semicolons, i.e., apply when , is replaced by ;.

identifier

id, module-name, function-name, struct-name, enum-name, contract-name, tvar-name, type-name

Identifiers have the same syntax as Typescript identifiers.

field-literal

nat

A field literal is 0 or a natural number formed from a sequence of digits starting with 1-9, e.g. 723, whose value does not exceed the maximum field value.

string-literal

str, file

A string literal has the same syntax as a Typescript string.

version-literal

version

A version literal takes the form nat.nat representing major and minor versions or nat.nat.nat representing major, minor, and bugfix versions. Where version literals are allowed, a plain nat representing just the major version is also allowed.

Compact

Program-element

Pragma

pragma-formpragma id version-expr ;

Version-expression

Version-expression0

Version-Term

Version-atom

version-atomnat
|version

Include

include-forminclude file ;

Module-definition

module-definitionexportopt module module-name gparamsopt { program-elementprogram-element }

Generic-parameter-list

gparams< generic-param ,, generic-param ,opt >

Generic-parameter

generic-param# tvar-name
|tvar-name

Import-declaration

import-formimport import-selectionopt import-name gargsopt import-prefixopt ;

Import-selection

import-selection{ import-element ,, import-element ,opt } from

Import-element

import-elementid
|id as id

Import-name

import-nameid
|file

Import-prefix

import-prefixprefix id

Generic-argument-list

gargs< garg ,, garg ,opt >

Generic-argument

gargnat
|type

Export-declaration

export-formexport { id ,, id ,opt } ;opt

Ledger-declaration

ledger-declarationexportopt sealedopt ledger id : type ;

Witness-declaration

witness-declarationexportopt witness id gparamsopt simple-parameter-list : type ;

Constructor

constructor-definitionconstructor pattern-parameter-list block

Circuit-definition

circuit-definitionexportopt pureopt circuit function-name gparamsopt pattern-parameter-list : type block

Structure-declaration

struct-declarationexportopt struct struct-name gparamsopt { typed-id ;; typed-id ;opt } ;opt
|exportopt struct struct-name gparamsopt { typed-id ,, typed-id ,opt } ;opt

Enum-declaration

enum-declarationexportopt enum enum-name { id , ⋯¹ , id ,opt } ;opt

External-contract-declaration

contract-declarationexportopt contract contract-name { circuit-declaration ;; circuit-declaration ;opt } ;opt
|exportopt contract contract-name { circuit-declaration ,, circuit-declaration ,opt } ;opt

External-contract-circuit

circuit-declarationpureopt circuit id simple-parameter-list : type

Type-declaration

type-alias-declarationexportopt newopt type type-name gparamsopt = type ;

Typed-identifier

typed-idid : type

Simple-parameter-list

simple-parameter-list( typed-id ,, typed-id ,opt )

Typed-pattern

typed-patternpattern : type

Pattern-parameter-list

pattern-parameter-list( typed-pattern ,, typed-pattern ,opt )

Type

typetref
|Boolean
|Field
|Uint < tsize >
|Uint < tsize .. tsize >
|Bytes < tsize >
|Opaque < str >
|Vector < tsize , type >
|[ type ,, type ,opt ]

Type-reference

trefid gargsopt

Type-size

tsizenat
|id

Block

block{ stmtstmt }

Statement

stmtif ( expr-seq ) stmt
|stmt0

Statement0

stmt0expr-seq ;
|const cbinding , ⋯¹ , cbinding ;
|if ( expr-seq ) stmt0 else stmt
|for ( const id of nat .. nat ) stmt
|for ( const id of expr-seq ) stmt
|return expr-seq ;
|return ;
|block

Pattern

patternid
|[ patternopt ,, patternopt ,opt ]
|{ pattern-struct-elt ,, pattern-struct-elt ,opt }

Pattern-struct-element

pattern-struct-eltid
|id : pattern

Expression-sequence

expr-seqexpr
|expr , ⋯¹ , expr , expr

Expression

Expression0

expr0expr0 || expr1
|expr1

Expression1

expr1expr1 && expr2
|expr2

Expression2

expr2expr2 == expr3
|expr2 != expr3
|expr3

Expression3

Expression4

expr4expr4 as type
|expr5

Expression5

Expression6

expr6expr6 * expr7
|expr7

Expression7

expr7! expr7
|expr8

Expression8

expr8expr8 [ expr ]
|expr8 . id
|expr8 . id ( expr ,, expr ,opt )
|expr9

Expression9

expr9fun ( expr ,, expr ,opt )
|map ( fun , expr , ⋯¹ , expr ,opt )
|fold ( fun , expr , expr , ⋯¹ , expr ,opt )
|slice < tsize > ( expr , expr )
|[ tuple-arg ,, tuple-arg ,opt ]
|Bytes [ bytes-arg ,, bytes-arg ,opt ]
|tref { struct-arg ,, struct-arg ,opt }
|assert ( expr , str )
|disclose ( expr )
|term

Term

termid
|true
|false
|nat
|str
|pad ( nat , str )
|default < type >
|( expr-seq )

Tuple-argument

tuple-argexpr
|... expr
bytes-argtuple-arg

Structure-argument

struct-argexpr
|id : expr
|... expr

Function

Return-type

return-type: type

Optionally-typed-pattern

optionally-typed-patternpattern
|typed-pattern

Const-Binding

Arrow-parameter-list

arrow-parameter-list( optionally-typed-pattern ,, optionally-typed-pattern ,opt )