-
Notifications
You must be signed in to change notification settings - Fork 1
/
kore.grammar
105 lines (89 loc) · 4.13 KB
/
kore.grammar
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
(* The grammar for the kore programming language in EBNF
It is loosely based on the EBNF grammar for Go (https://golang.org/ref/spec).
*)
(* Grammar for the top-level source file *)
SourceFile = ModuleDecl { ImportDecl } { TopLevelDecl } .
ModuleName = Identifier .
ModuleDecl = "module" ModuleName .
ModuleList = Identifier { "," Identifier } .
ImportDecl = "import" ModuleName { "." ModuleName } [ "{" ModuleList "}" ] .
TopLevelDecl = Declaration | Function .
(* Statements *)
Block = "{" StatementList "}" .
StatementList = { Statement ";" } .
Statement = Declaration | SimpleStmt | IfStmt | ForStmt | Block | ReturnStmt .
EmptyStmt = .
ReturnStmt = "return" [ ExpressionList ] .
ExpressionStmt = Expression .
IncDecStmt = ( "++" | "--" ) Expression .
Assignment = ExpressionList assign_op ExpressionList .
assign_op = [ add_op | mul_op ] "=" .
IfStmt = "if" [ SimpleStmt ] Block [ "else" ( IfStmt | Block ) ] .
TypeAlias = [ "export" ] "type" Identifier "=" Type .
VarDecl = Identifier [ Type ] '=' Expression .
Declaration = TypeAlias | TypeDecl | VarDecl .
(* Expressions *)
ExpressionList = Expression { "," Expression } .
Expression = UnaryExpr | Expression BinaryOp Expression .
UnaryExpr = SubExpr | UnaryOp UnaryExpr .
SubExpr = Operand | SubExpr Selector | SubExpr Call .
Operand = Literal | OperandName | "(" Expression ")" .
Selector = ( "." | ".?" ) Identifier .
Call = "(" [ ExpressionList ] ")" .
BinaryOp = BoolOp | RelOp | ArithOp | BitOp | OptionalOp .
BoolOp = "or" | "and" | "!" .
RelOp = "==" | "!=" | "<" | "<=" | ">" | ">=" .
ArithOp = "+" | "-" | "*" | "/" | "%" | "rem" .
BitOp = "|" | "&" | "^" | "<<" | "<<=" | ">>" | ">>=" .
UnaryOp = "+" | "-" .
OptionalOp = "??" .
Struct = "struct" "{" { FieldDecl ";" } "}" .
(* Identifiers *)
QualifiedIdentifer = Identifier { "." Identifier } .
IdentifierList = Identifier { "," Identifier } .
(* Arrays *)
ArrayRange = Expression ".." Expression .
Array = "[" [ ExpressionList ] "]" .
ArrayFill = "[" Expression ":" Expression "]" .
ArrayDecl = Array | ArrayRange | ArrayFill .
(* Sets *)
SetRangeLiteral = "{" Expression ".." Expression "}" .
SetLiteral = "{" [ ExpressionList ] "}" .
(* Literals *)
Literal = IntLit | FloatLit | BinLit | HexLit | OctLit | BoolLit | ArrayLit | FuncLit .
IntLit = DecimalLit | BinaryLit | OctalLit | HexLit .
DecimalLit = "0" | ( "1" … "9" ) [ [ "_" ] DecimalDigits ] .
BinaryLit = "0" ( "b" | "B" ) [ "_" ] BinaryDigits .
OctalLit = "0" [ "o" | "O" ] [ "_" ] OctalDigits .
HexLit = "0" ( "x" | "X" ) [ "_" ] HexDigits .
DecimalDigits = DecimalDigit { [ "_" ] DecimalDigit } .
BinaryDigits = BinaryDigit { [ "_" ] BinaryDigit } .
HexDigits = HexDigit { [ "_" ] HexDigit } .
OctalDigits = OctalDigit { [ "_" ] OctalDigit } .
DecimalDigit = "0" … "9" .
BinaryDigit = "0" | "1" .
HexDigit = DecimalDigit | "a" … "f" | "A" … "F" .
OctalDigit = "0" … "7" .
BoolLit = "true" | "false" .
FuncLitArg = "@" ( "args" | { "1" … "9" } ) .
OperandName = Identifier | QualifiedIdentifer | FuncPositionalArg .
FuncPositionalArg = "@" ( FuncArgNum | "args" ) .
FuncArgNum = ( "1" … "9" ) { DecimalDigit } .
letter = "A" | "B" | "C" | "D" | "E" | "F" | "G"
| "H" | "I" | "J" | "K" | "L" | "M" | "N"
| "O" | "P" | "Q" | "R" | "S" | "T" | "U"
| "V" | "W" | "X" | "Y" | "Z" | "a" | "b"
| "c" | "d" | "e" | "f" | "g" | "h" | "i"
| "j" | "k" | "l" | "m" | "n" | "o" | "p"
| "q" | "r" | "s" | "t" | "u" | "v" | "w"
| "x" | "y" | "z" .
digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" .
Identifier = ( "_" | letter ) [ "_" | letter | digit ] .
(* Functions *)
Function = [ "export" ] "func" FunctionName FuncSignature [ FunctionBody ] .
FunctionName = Identifier .
FuncSignature = Parameters [ Type ] .
Parameters = "(" [ ParameterList ] ")" .
ParameterList = ParameterDecl { "," ParameterDecl } .
ParameterDecl = [ IdentifierList ] [ "..." ] Type .
FunctionBody = Block .