Compact Grammar

Compact language version 0.11.0.

Notational note: In the grammar productions below, ellipses are used to specify repetition. The notation X ... X, where X is a grammar symbol, represents zero or more occurrences of X. The notation X s ... s X, where X is a grammar symbol and s is a separator such as a comma or or semicolon, represents zero or more occurrences of X separated by s. In either case, when the ellipsis is marked with the superscript 1, the notation represents a sequence containing at least one X. For example, id … id represents zero or more ids, and expr , …¹ , expr represents one or more comma-separated exprs.

identifier (id, module-name, function-name, struct-name, enum-name, contract-name, tvar-name)

identifiers have the same syntax as Typescript identifiers

adt-identifier (adt-name)

identifier that names one of the Ledger ADTs

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 or nat.nat or nat.nat.nat, e.g., 1.2 or 1.2.3, representing major, minor, and bugfix versions

Compact (program)

program   peltpelt

Program-element (pelt)

pelt   pragma
   incld
   mdefn
   idecl
   xdecl
   ldecl
   lconstructor
   cdefn
   edecl
   wdecl
   ecdecl
   struct
   enumdef

Pragma (pragma)

pragma   pragma  id  version-expr  ;

Version-expression (version-expr)

The productions on each line of this group have lower precedence than those on the lines that follow.

version-expr   version-expr || version-term
  version-expr && version-term
  version-term

Version-Term (version-term)

version-term   version-atom
   !  version-term
   <  version-atom
   <=  version-atom
   >=  version-atom
   >  version-atom
   (  version-expr  )

Version-Atom (version-atom)

version-atom   nat
   version

Include (incld)

incld   include  file  ;

Module-definition (mdefn)

mdefn   exportopt  module  module-name  tparamsopt  {  peltpelt  }

Type-params (tparams)

tparams   <  type-param ,, type-param  >

Type-param (type-param)

type-param   #  tvar-name
   tvar-name

Import-declaration (idecl)

idecl   import  module-name  targsopt  prefixopt  ;

Type-args (targs)

targs   <  targ ,, targ  >

Import-prefix (prefix)

prefix   prefix  id

Export (export)

export   export

Sealed (sealed)

sealed   sealed

Pure (pure)

pure   pure

Export-declaration (xdecl)

xdecl   export  {  id ,, id  }  ;opt

Ledger-Declaration (ldecl)

ldecl   exportopt  sealedopt  ledger  id  :  ledger-adt  ;

Ledger-Constructor (lconstructor)

lconstructor   constructor  (  arg ,, arg  )  block  ;opt

Circuit-definition (cdefn)

cdefn   exportopt  pureopt  circuit  function-name  tparamsopt  (  arg ,, arg  )  :  return-type  block

External-declaration (edecl)

edecl   exportopt  circuit  id  tparamsopt  (  arg ,, arg  )  :  return-type  ;

Witness-declaration (wdecl)

wdecl   exportopt  witness  id  tparamsopt  (  arg ,, arg  )  :  return-type  ;

External-contract-declaration (ecdecl)

ecdecl   exportopt  contract  contract-name  {  ecdecl-circuitecdecl-circuit  }  ;opt

External-contract-circuit (ecdecl-circuit)

ecdecl-circuit   pureopt  circuit  id  (  arg ,, arg  )  :  return-type  ;

Structure-definition (struct)

struct   exportopt  struct  struct-name  tparamsopt  {  arg ;; arg  ;opt  }
   exportopt  struct  struct-name  tparamsopt  {  arg ,, arg  ,opt  }

Enum-definition (enumdef)

enumdef   exportopt  enum  enum-name  {  id , …¹ , id  ,opt  }

Ledger-adt (ledger-adt)

Midnight ledger ADTs are described here.

ledger-adt   adt-name  adt-argsopt

Ledger-adt-args (adt-args)

adt-args   <  ledger-adt-arg , …¹ , ledger-adt-arg  >

Ledger-ADT-Arg (ledger-adt-arg)

ledger-adt-arg   nat
   adt-type

Ledger-ADT-Type (adt-type)

adt-type   type
   ledger-adt

Argument (arg)

arg   id  :  type

Return-Type (return-type)

return-type   Void
   type

Type (type)

type   tref
   Boolean
   Field
   Uint  <  tsize  >
   Uint  <  tsize  ..  tsize  >
   Bytes  <  tsize  >
   Opaque  <  str  >
   Vector  <  tsize  ,  type  >

Type-ref (tref)

tref   id  targsopt

Type-size (tsize)

tsize   nat
   id

Type-argument (targ)

targ   nat
   type

Block (block)

block   {  stmtstmt  }

Statement (stmt)

stmt   expr  =  expr  ;
   expr  +=  expr  ;
   expr  -=  expr  ;
   expr  ;
   return  expr  ;
   return  ;
   if  (  expr  )  stmt  else  stmt
   if  (  expr  )  stmt
   for  id  =  nat  to  nat  do  stmt
   for  id  in  expr  do  stmt
   assert  expr  str  ;
   const  id  =  expr  ;
   const  id  :  adt-type  =  expr  ;
   block

Expression (expr)

expr   expr0  ?  expr  :  expr
   expr0

Function (fun)

fun   id  targsopt
   circuit  (  arg ,, arg  )  :  return-type  block
   (  fun  )

Expression0 (expr0)

The productions on each line of this group have lower precedence than those on the lines that follow.

expr0   expr0 || expr1
  expr0 && expr1
  expr0 == expr1  |  expr0 != expr1
  expr1 < expr1  |  expr1 > expr1  |  expr1 <= expr1  |  expr1 >= expr1
  expr1

Expression1 (expr1)

expr1   expr2  tail-expr1

Expression1-tail (tail-expr1)

tail-expr1   (empty)
   as  type  tail-expr1

Expression2 (expr2)

The productions on each line of this group have lower precedence than those on the lines that follow.

expr2   expr2 + expr3  |  expr2 - expr3
  expr2 * expr3
  expr3

Expression3 (expr3)

expr3   !  expr3
   expr4

Expression4 (expr4)

expr4   term  tail-expr4

Expression4-tail (tail-expr4)

tail-expr4   (empty)
   [  nat  ]  tail-expr4
   .  id  tail-expr4
   .  id  (  expr ,, expr  )  tail-expr4

Term (term)

term   lit
   id
   default  <  type  >
   default  <  ledger-adt  >
   map  (  fun  ,  expr , …¹ , expr  )
   fold  (  fun  ,  expr  ,  expr , …¹ , expr  )
   fun  (  expr ,, expr  )
   new  tref  (  expr ,, expr  )
   [  expr ,, expr  ,opt  ]
   (  expr  )
   (  expr , …¹ , expr  ,  expr  )

Literal (lit)

lit   true
   false
   nat
   str
   pad  (  nat  ,  str  )