aboutsummaryrefslogtreecommitdiff
path: root/doc/grammar.txt
blob: 11518d4bb09cf0180c493d38bbf6dfcaafcf56e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
root := ( toplevel )\n
toplevel := externfunc | function | constant | typedef

path := NAME

function := [ COMMENT ]
		[ "export" ] "func" NAME "(" ( param ), ")" [ type ] block
param := NAME type

externfunc := "extern" "func" NAME "(" ( type ), ")" [ type ]

constant := [ COMMENT ]
		"const" NAME "=" ( NUMBER | bool | STRING* )

typedef := [ COMMENT ] "type" NAME type

block := [ ":" NAME ] "{" ( command )\n "}"

command := block | statement

control := if | for

statement := ( return
		| break
		| continue
		| declaration
		| defer )

if := "if" expression block elseif* else*
elseif := "else" "if" expression block
else := "else" block

for := "for" [ assignment ";" ] expression [ ";" assignment ] block

return := "return" [ expression ]

break := "break" [ ":" NAME ] [ expression ]
continue := "continue" [ ":" NAME ]

declaration := ( "const" | "var" ) NAME "=" expression

assignment := location "=" expression
location := path ( index | field )* [ "*" ]
index := "[" expression "]"
field := "." NAME

call := location "(" ( expression ), ")"

defer := "defer" call

type := "*" type | "?" type | "!" type
		| bool | integer | float | struct | enum | union
		| array
		| path
numtype := integer | float
integer := "int8" | "uint8"
		| "int16" | "uint16"
		| "int32" | "uint32"
		| "int64" | "uint64"
		| "int" | "uint"
float := "float32" | "float64"
array := "[" [ expression ] "]" type
struct := "struct" "{" fields "}"
enum := "enum" [ "(" integer ")" ] [ "=" expression ] "{" ( NAME ), "}"
union := "union" [ "(" type ")" ] "{" ( type ), "}"
fields := ( NAME type ),

expression := disjunction
disjunction := conjunction ( "||" conjunction )*
conjunction := boolean ( "&&" boolean )*
boolean := equality
equality := comparison [ ( "==" | "!=" ) comparison ]
comparison := term [ ( "<" | "<=" | ">" | ">=" ) term ]
term := numeral [ ( "<<" | ">>" ) numeral ]
numeral := factor ( ( "+" | "-" ) factor )*
factor := binary ( ( "*" | "/" | "%" ) binary  )*
binary := bitsummand ( "|" bitsummand )*
bitsummand := bitfactor ( "^" | bitfactor )*
bitfactor := unaryprefix ( "&" unaryprefix )*
unaryprefix := [ "-" | "!" | "~" ] unarypostfix
unarypostfix := primary [ "*" | "?" | "!" ]
primary := control | grouping | literal | named
named := assignment | call | location
grouping := "(" expression ")"
literal := bool | string | number | arrayelems
bool := "true" | "false"
string := STRING*
number := NUMBER numtype
arrayelems := array "{" ( expression ), "}"