Grammar
The shape of every construct, in one place.
Reading it
The grammar holds Adam's productions in one section per part of the language, in the order of Adam's grammar specification: programs and modules, directives, declarations, types, statements, expressions, intrinsics, unit testing, the lexical rules and generics. It is derived from that specification at every docs build, its design notes left out.
The productions are written in ISO 14977 EBNF: = defines a rule and ; ends it, , puts parts in sequence, | separates alternatives, [ ... ] is optional, { ... } repeats zero or more times, ( ... ) groups, a quoted string is written as it stands, - takes a part out and ? ... ? describes in words what no production spells. What each construct means is on its Language page (Modules, Declarations, Statements and the rest).
Program and Modules
program = module ;
module = "module" , module kind , module name , ";" ,
{ directive } ,
[ import clause ] ,
{ declaration } ,
[ initialize section ] ,
[ finalize section ] ,
[ module body ] ,
"end" , "." ,
{ test block } ;
module kind = "exe" | "dll" | "lib" | "unit" ;
module name = identifier , { "." , identifier } ;
import clause = "import" , module name , { "," , module name } , ";" ;
initialize section
= "initialize" , routine body ;
finalize section
= "finalize" , routine body ;
module body = "begin" , { statement } ;
Directives
directive = conditional directive | build directive ;
conditional directive
= "@define" , identifier
| "@undef" , identifier
| "@ifdef" , identifier
| "@ifndef" , identifier
| "@elseif" , identifier
| "@else"
| "@endif" ;
build directive = "@optimize" , identifier , ";"
| "@subsystem" , identifier , ";"
| "@target" , target spec , ";"
| "@addverinfo" , identifier , ";"
| "@unitTestMode" , identifier , ";"
| "@vimajor" , ( integer | qualified ident ) , ";"
| "@viminor" , ( integer | qualified ident ) , ";"
| "@vipatch" , ( integer | qualified ident ) , ";"
| "@viproductname" , ( cstring | qualified ident ) , ";"
| "@videscription" , ( cstring | qualified ident ) , ";"
| "@vifilename" , ( cstring | qualified ident ) , ";"
| "@vicompanyname" , ( cstring | qualified ident ) , ";"
| "@vicopyright" , ( cstring | qualified ident ) , ";"
| "@exeicon" , ( cstring | qualified ident ) , ";"
| "@copydll" , cstring , ";"
| "@linklibrary" , cstring , ";"
| "@addlibrarypath" , cstring , ";"
| "@addmodulepath" , cstring , ";"
| "@breakpoint" , ";"
| "@message" , severity , cstring , ";"
| "@outputpath" , cstring , ";"
| "@addresdata" , cstring , cstring , ";" ;
target spec = cstring ;
severity = "hint" | "warn" | "error" | "fatal" ;
qualified ident = identifier , { "." , identifier } ;
Declarations
declaration = var block | const block | type block | threadstatic block
| routine decl | forward decl | extend decl | public decl ;
public decl = "public" , declaration ;
forward decl = "forward" , ( "func" , [ linkage ] , identifier , [ generic params ] ,
[ param list ] , [ ":" , type expr ] , ";" ,
[ "noreturn" , ";" ] ,
[ "inline" , ";" ]
| "type" , identifier , [ generic params ] , ";" ) ;
var block = "var" , { var decl } ;
var decl = identifier , ":" , type expr ,
[ "(" , [ arg list ] , ")" | "=" , expression ] , ";" ,
[ external clause ] ;
threadstatic block = "threadstatic" , { var decl } ;
const block = "const" , { const decl } ;
const decl = identifier , ":" , type expr , "=" , expression , ";" ;
routine decl = "func" , [ linkage ] , identifier , [ generic params ] ,
[ param list ] , [ ":" , type expr ] , ";" ,
[ "noreturn" , ";" ] ,
[ "inline" , ";" ] ,
( external clause | routine body ) ;
routine body = { var block | const block | type block } , block , ";" ;
method decl = ( "static"
| [ "virtual" | "override" ] , [ "final" ] ,
[ "const" ] ) ,
"method" , identifier , [ generic params ] , [ param list ] ,
[ ":" , ( type expr | "self" ) ] , ";" ,
( "abstract" , ";" | { var block } , block , ";" ) ;
linkage = "clink" | "cpplink" ;
external clause = "external" , [ cstring | identifier ] ,
[ "name" , cstring ] , ";" ;
extend decl = "extend" , qualified type , [ type args ] ,
{ method decl | operator decl } , "end" , ";" ,
[ external clause ] ;
param list = "(" , [ params ] , ")" ;
params = "..." | param , { ";" , param } , [ ";" , "..." ] ;
param = [ "var" | "const" | "literal" ] , identifier , ":" , type expr ;
Types
type block = "type" , { type decl } ;
type decl = identifier , [ generic params ] , "=" , type def , ";" ,
[ external clause ] ;
type def = record type | [ "final" ] , object type | interface type
| overlay type | choices type
| array type | pointer type | set type | routine type
| alias type ;
alias type = type expr ;
record type = "record" , [ "(" , qualified type , ")" ] ,
[ packing ] , [ "align" , "(" , expression , ")" ] ,
{ field decl | anon overlay | operator decl } , "end" ;
packing = "packed" , [ "(" , expression , ")" ] ;
object type = [ "ref" ] , "object" , [ "(" , qualified type , ")" ] ,
[ "implements" , qualified type , { "," , qualified type } ] ,
{ [ "hidden" ] , ( field decl | embed decl | method decl | property decl )
| init decl | copy decl | done decl | operator decl } ,
"end" ;
operator decl = "operator" , operator symbol , param list , ":" , type expr , ";" ,
{ var block } , block , ";" ;
operator symbol = "+" | "-" | "*" | "/" | "div" | "mod" | "=" | "<>" | "<" | "<=" | ">" | ">=" | "in"
| "hash" ;
property decl = [ "virtual" | "override" ] , [ "final" ] ,
"property" , identifier , [ "[" , [ "const" ] , identifier , ":" , type expr , "]" ] ,
":" , type expr ,
"read" , ( identifier , ";" | "abstract" , ";" | { var block } , block , ";" ) ,
[ [ "hidden" ] , "write" , ( identifier , ";" | "abstract" , ";"
| "(" , identifier , ")" , { var block } , block , ";" ) ] ,
[ "default" , ";" ] ;
interface property = "property" , identifier , [ "[" , [ "const" ] , identifier , ":" , type expr , "]" ] ,
":" , type expr , "read" , ";" , [ "write" , ";" ] ;
embed decl = "embed" , identifier , ":" , type expr , ";" ;
interface type = "interface" , [ "(" , qualified type , ")" ] ,
{ [ "const" ] , "method" , identifier , [ param list ] , [ ":" , type expr ] , ";"
| interface property } , "end" ;
init decl = "init" , param list , ";" , { var block } , block , ";" ;
copy decl = "copy" , param list , ";" , { var block } , block , ";" ;
done decl = "done" , ";" , { var block } , block , ";" ;
overlay type = "overlay" , { field decl | anon record } , "end" ;
anon record = "record" , [ packing ] ,
{ field decl | anon overlay } , "end" , ";" ;
anon overlay = "overlay" , { field decl | anon record } , "end" , ";" ;
field decl = identifier , ":" , type expr , [ ":" , expression ] , ";"
| identifier , ":" , ( inline record | inline overlay ) , ";" ;
inline record = "record" , [ packing ] , { field decl | anon overlay } , "end" ;
inline overlay = "overlay" , { field decl | anon record } , "end" ;
choices type = "choices" , "(" , choice value ,
{ "," , choice value } , ")" ;
choice value = identifier , [ "=" , expression ] ;
array type = "array" , [ "[" , [ expression , ".." , [ expression ] ] , "]" ] ,
"of" , type expr ;
pointer type = "ptr" , [ "to" , ( "const" , [ type expr ] | type expr ) ] ;
set type = "set" , "of" , ( expression , ".." , expression | type expr ) ;
routine type = "func" , [ linkage ] , param list ,
[ ":" , type expr ] ;
closure type = "ref" , "func" , param list , [ ":" , type expr ] ;
type expr = builtin type | named type | pointer type | array type
| routine type
| closure type
| owner type
| task type
| sequence type ;
task type = "task" , [ type expr ] ;
sequence type = "sequence" , "of" , type expr ;
owner type = ( "own" | "ref" | "weak" ) , type expr ;
qualified type = identifier , { "." , identifier } ;
named type = qualified type , [ type args ] ;
generic params = "<" , identifier , { "," , identifier } , ">" ;
type args = "<" , type expr , { "," , type expr } , ">" ;
builtin type = "int8" | "int16" | "int32" | "int64"
| "uint8" | "uint16" | "uint32" | "uint64"
| "clong" | "culong"
| "float32" | "float64"
| "bool" | "char" | "wchar"
| "string" | "wstring"
| "ptr"
| "mutex"
| "varargs" ;
Statements
block = "begin" , { statement } , "end" ;
statement = block , ";"
| if stmt | while stmt | for stmt | repeat stmt | match stmt
| guard stmt
| return stmt | leave stmt | skip stmt
| directive stmt
| memory stmt | exception stmt | io stmt | assert stmt
| lock stmt | trylock stmt
| defer stmt
| yield stmt | for in stmt
| var block | const block | type block
| expression statement ;
lock stmt = "lock" , "(" , expression , ")" , "do" , { statement } , "end" , ";" ;
trylock stmt = "trylock" , "(" , expression , ")" , [ "wait" , expression ] , "do" ,
{ statement } , [ "else" , { statement } ] , "end" , ";" ;
defer stmt = "defer" , statement ;
yield stmt = "yield" , [ "from" ] , expression , ";" ;
if stmt = "if" , expression , "then" , { statement } ,
[ "else" , { statement } ] , "end" , ";" ;
while stmt = "while" , expression , "do" , { statement } , "end" , ";" ;
for stmt = "for" , [ "parallel" , [ "(" , expression , ")" ] ] ,
identifier , ":=" , expression ,
( "to" | "downto" ) , expression , "do" ,
{ statement } , "end" , ";" ;
for in stmt = "for" , identifier , "in" , expression , "do" ,
{ statement } , "end" , ";" ;
repeat stmt = "repeat" , { statement } , "until" , expression , ";" ;
match stmt = "match" , expression , "of" , { match arm } ,
[ "else" , { statement } ] , "end" , ";" ;
match arm = match label , { "," , match label } , ":" , { statement } ;
match label = expression , [ ".." , expression ] ;
guard stmt = "guard" , { statement } ,
[ "except" , { statement } ] ,
[ "finally" , { statement } ] ,
"end" , ";" ;
return stmt = "return" , [ expression ] , ";" ;
leave stmt = "leave" , ";" ;
skip stmt = "skip" , ";" ;
directive stmt = "@breakpoint" , ";"
| "@message" , severity , cstring , ";" ;
expression statement
= expression , ";" ;
Expressions
expression = assign expr ;
assign expr = cond expr , [ assign op , assign expr ] ;
assign op = ":=" | "+=" | "-=" | "*=" | "/="
| "%=" | "&=" | "|=" | "<<=" | ">>=" ;
cond expr = or expr , [ "?" , expression , ":" , cond expr ] ;
or expr = and expr , { ( "or" | "||" ) , and expr } ;
and expr = rel expr , { ( "and" | "&&" ) , rel expr } ;
rel expr = add expr , [ rel op , add expr | "is" , type expr ] ;
rel op = "=" | "<>" | "<" | ">" | "<=" | ">=" | "in"
| "<=>" ;
add expr = shift expr , { ( "+" | "-" | "|" | "xor" ) , shift expr } ;
shift expr = mul expr , { ( "shl" | "shr" | "<<" | ">>" ) , mul expr } ;
mul expr = unary expr ,
{ ( "*" | "/" | "div" | "mod" | "%" | "&" ) , unary expr
| "as" , type expr } ;
unary expr = prefix op , unary expr
| "async" , postfix expr
| "async" , block
| "await" , unary expr
| postfix expr ;
prefix op = "not" | "-" | "+" | "addr" , "of"
| "!" | "~" | "*" | "&" | "++" | "--" ;
postfix expr = primary expr , { postfix op } ;
postfix op = "(" , [ arg list ] , ")"
| "." , "next" , "(" , type arg , ")"
| "." , "get" , "(" , expression , "," , type arg , ")"
| "[" , expression , "]"
| "." , member name
| "->" , member name
| "^"
| "++" | "--" ;
arg list = expression , { "," , expression } ;
type arg = type expr | expression ;
member name = identifier ;
owner new = ( "own" | "ref" ) , type expr , [ "(" , [ arg list ] , ")" ] ;
primary expr = integer | float | cstring | cchar
| "true" | "false" | "nil"
| "self" | "parent"
| owner new
| identifier , [ type args ]
| varargs expr
| type cast
| pointer cast
| array literal
| set literal
| record literal
| intrinsic call
| func literal
| "(" , expression , ")" ;
func literal = "func" , param list , [ ":" , type expr ] ,
{ var block | const block | type block } , block ;
type cast = builtin type , "(" , expression , ")" ;
pointer cast = "ptr" , "to" , [ "const" ] , type expr ,
"(" , expression , ")" ;
set literal = "[" , [ set element , { "," , set element } ] , "]" ;
set element = expression , [ ".." , expression ] ;
array literal = "[" , [ expression , { "," , expression } , [ "," ] ] , "]" ;
record literal = named type , "(" , field init , { "," , field init } , ")" ;
field init = identifier , ":" , expression ;
varargs expr = "varargs" , "." , ( "count"
| "next" , "(" , type expr , ")"
| "get" , "(" , expression , "," , type expr , ")"
| "reset" , "(" , ")"
| "copy" , "(" , ")"
| "kind" , "(" , expression , ")"
| "clist" , "(" , ")" ) ;
Intrinsics
intrinsic call = "len" , "(" , expression , ")"
| "size" , "(" , type expr , ")"
| "utf8" , "(" , expression , ")"
| "paramcount" , "(" , ")"
| "paramstr" , "(" , expression , ")"
| "getmem" , "(" , expression , ")"
| "resizemem" , "(" , expression , "," , expression , ")"
| "exccode" , "(" , ")"
| "excmsg" , "(" , ")"
| "alive" , "(" , expression , ")"
| "lock" , "(" , expression , ")"
| "ready" , "(" , expression , ")"
| "atomicadd" , "(" , expression , "," , expression , ")"
| "atomicswap" , "(" , expression , "," , expression , ")"
| "atomiccas" , "(" , expression , "," , expression , "," , expression , ")"
| "atomicload" , "(" , expression , ")"
| "workerindex" , "(" , ")"
| "workercount" , "(" , ")"
| "cancel" , "(" , expression , ")"
| "cancelled" , "(" , [ expression ] , ")"
| "pull" , "(" , expression , "," , expression , ")"
| "ProjectPassword" , "(" , ")"
| "FormatStr" , "(" , arg list , ")"
| "byteswap" , "(" , expression , ")"
| "leadingzeros" , "(" , expression , ")"
| "trailingzeros" , "(" , expression , ")"
| "popcount" , "(" , expression , ")"
| "hash" , "(" , expression , ")"
| "nameof" , "(" , ( expression | type expr ) , ")"
| "sourcehash" , "(" , ")" ;
memory stmt = "create" , "(" , expression , ")" , ";"
| "destroy" , "(" , expression , ")" , ";"
| "getmem" , "(" , expression , ")" , ";"
| "freemem" , "(" , expression , ")" , ";"
| "resizemem" , "(" , expression , "," , expression , ")" , ";"
| "setlength" , "(" , expression , "," , expression , ")" , ";" ;
exception stmt = "raise" , "(" , expression , ")" , ";"
| "raisecode" , "(" , expression , "," , expression , ")" , ";" ;
io stmt = ( "print" | "println" ) , "(" , [ arg list ] , ")" , ";" ;
atomic stmt = "atomicstore" , "(" , expression , "," , expression , ")" , ";" ;
Unit Testing
test block = "test" , cstring , [ var block ] , block , ";" ;
assert stmt = assert1 , "(" , expression , ")" , ";"
| assert2 , "(" , expression , "," , expression , ")" , ";" ;
assert1 = "testAssert" | "testAssertTrue" | "testAssertFalse"
| "testAssertNil" | "testAssertNotNil" | "testFail" ;
assert2 = "testAssertEqualInt" | "testAssertEqualUInt"
| "testAssertEqualFloat" | "testAssertEqualStr"
| "testAssertEqualBool" | "testAssertEqualPtr" ;
Lexical
identifier = letter , { letter | digit | "_" } ;
letter = "A" .. "Z" | "a" .. "z" | "_" ;
digit = "0" .. "9" ;
hex digit = digit | "A" .. "F" | "a" .. "f" ;
integer = ( decimal | hexadecimal | binary ) ,
[ int suffix ] ;
decimal = "0" | nonzero digit , { [ "'" ] , digit } ;
nonzero digit = "1" .. "9" ;
hexadecimal = ( "0x" | "0X" ) , hex digit , { [ "'" ] , hex digit } ;
binary = ( "0b" | "0B" ) , ( "0" | "1" ) , { [ "'" ] , ( "0" | "1" ) } ;
int suffix = ( "u" | "U" ) , [ "l" | "L" | "ll" | "LL" | "z" | "Z" ]
| ( "l" | "L" | "ll" | "LL" | "z" | "Z" ) , [ "u" | "U" ] ;
float = ( decimal float | hex float ) , [ float suffix ] ;
decimal float = digits , "." , [ digits ] , [ exponent ]
| "." , digits , [ exponent ]
| digits , exponent ;
digits = digit , { [ "'" ] , digit } ;
exponent = ( "e" | "E" ) , [ "+" | "-" ] , digits ;
hex float = ( "0x" | "0X" ) , hex digit , { hex digit } , [ "." , { hex digit } ] ,
( "p" | "P" ) , [ "+" | "-" ] , digits ;
float suffix = "f" | "F" ;
cstring = ( '"' , { character | escape } , '"'
| raw string ) ;
raw string = 'R"' , delimiter , "(" , { character } , ")" ,
delimiter , '"' ;
delimiter = { character } ;
cchar = "'" , { character | escape } , "'" ;
escape = "\" , character ;
comment = line comment | block comment | doc comment ;
line comment = "//" , { character } , newline ;
doc comment = "///" , [ space ] , doc text , newline ;
doc text = { character } ;
doc tag = "@param" , identifier | "@returns" | "@return" | "@raises" | "@see"
| "@deprecated" | "@since" | "@example" ;
block comment = "/*" , { character } , "*/" ;
newline = LF | CR , LF | CR ;
line splice = "\" , { space | tab | FF | VT } , newline ;
character = ? any source character ? ;
LF = ? line feed, U+000A ? ;
CR = ? carriage return, U+000D ? ;
space = " " ;
tab = ? horizontal tab, U+0009 ? ;
FF = ? form feed, U+000C ? ;
VT = ? vertical tab, U+000B ? ;