aboutsummaryrefslogtreecommitdiff
path: root/doc/grammar.txt
blob: c78e2a6d39ac6c922255e004d992cb29ddddab94 (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
root := toplevel*
toplevel := import* ( externfunc | function | constant )

import := "import" IDENT [ NAME ] ";"

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

externfunc := "extern" "func" NAME "(" ( type ), ")" type ";"

constant := [ "pub" ] "const" NAME "=" ( type | bool | INTEGER | FLOAT | STRING* ) ";"

block := "{" body* "}"

body := block | control | statement

control := if | for

statement := ( return
		| break
		| continue
		| declaration
		| assignment
		| call
		| defer ) ";"

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

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

return := "return" expression

break := "break" ":" NAME
continue := "continue" ":" NAME

declaration := "let" [ "mut" ] NAME "=" expression ";"

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

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

defer := "defer" call

type := integer | float | array | slice | struct | enum | union
		| "*" type | "?" type | "!" type | IDENT
integer := "i8" | "u8"
		| "i16" | "u16"
		| "i32" | "u32"
		| "i64" | "u64"
		| "isize" | "usize"
float := "f32" | "f64"
array := "[" INTEGER "]" type
slice := "[]" type
struct := "struct" "{" fields "}"
enum := "enum" [ expression ] "{" ( NAME ), "}"
union := "union" [ type ] "{" fields "}"
fields := ( NAME type ),

expression := equality | comparison
equality := comparison ( "==" | "!=" ) comparison
comparison := term [ ( "<" | "<=" | ">" | ">=" ) term ]
term := numeral [ ( "<<" | ">>" ) numeral ]
numeral := factor ( ( "+" | "-" ) factor )*
factor := unaryprefix ( ( "*" | "/" | "%" ) unaryprefix )*
unaryprefix := [ "-" | "!" | "~" ] unarypostfix
unarypostfix := primary [ "*" | "?" | "!" ]
primary := grouping | literal | call | IDENT
grouping := "(" expression ")"
literal := string | number | arrayelems | sliceelems
string := STRING*
number := ( integer INTEGER ) | ( float FLOAT )
arrayelems := array "{" ( expression ), "}"
sliceelems := slice "{" ( expression ), "}"