-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.EBNF
75 lines (66 loc) · 2.67 KB
/
grammar.EBNF
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
program ::= statement* EOF ;
statement ::= var_decl
| fn_decl
| block
| if_stmt
| for_stmt
| while_stmt
| match_stmt
| return
| exprStmt
| "break"
; (* | "continue" *)
block ::= "{" statement* "}" ;
fn_decl ::= "fn" IDENTIFIER "(" params? ")" body ;
var_decl ::= ( "let" | "global" ) ( IDENTIFIER )+ ( "=" expression )? ( "?" )? ";" ;
if_stmt ::= "if" expression block ( else ( if_stmt | block ) )?;
for_stmt ::= ( "for" | "fori" ) IDENTIFIER ("," IDENTIFIER )* "in" ( range | arguments ) block ;
while_stmt ::= "while" expression block;
return ::= "return" arguments? ;
match_stmt ::= "match" expression ( "as" IDENTIFIER )? "{" ( arm )* ( "," rest_arm )? "}" ;
expr_stmt ::= assignment ";" ;
arm ::= expression "=>" block ;
rest_arm ::= "_" "=>" block ;
assignment ::= assignable ( "," assignable )* ( "+=" | "-=" | "*=" | "/=", "**=" | "=" ) expression
| expression
;
expression ::= default_op | lambda_decl ;
default_op ::= logic_or ( "??" logic_or )? ;
logic_or ::= logic_and ( "or" logic_and )* ;
logic_and ::= equality ( "and" equality )* ;
equality ::= comparison ( ( "!=" | "==" ) comparison )* ;
comparison ::= addition ( ( ">" | ">=" | "<" | "<=" ) addition )* ;
addition ::= multiplication ( ( "-" | "+" ) multiplication )* ;
multiplication ::= exponentiation ( ( "/" | "\" | "*" ) exponentiation )* ;
exponentiation ::= unary ( ( "**" ) exponentiation )* ;
unary ::= ( "not" | "-" | "..." ) unary
| "len" "(" expression ")"
| call
;
call ::= primary
( "(" arguments? ")" | "." IDENTIFIER | "[" expression "]" )*
( err_block )*
;
err_block ::= "err" block ;
primary ::= "nil"
| "true"
| "false"
| NUMBER
| FSTRING
| STRING
| IDENTIFIER
| VARIADICS
| "(" expression ")"
| "[" arguments? "]"
| "{" pairs? "}"
| "lua" "{" .* "}"
;
assignable ::= IDENTIFIER | get | get_item ;
(* Allow an optional trailing comma *)
params ::= IDENTIFIER ( "," IDENTIFIER ( "?" )? )* ( "," )? ;
arguments ::= expression ( "," expression)* ( "," )? ;
pairs ::= pair ("," pair )* ( "," )? ;
pair ::= expr "=" expr ;
VARIADICS ::= "@" ;
IDENTIFIER ::= < [a-zA-Z_][a-zA-Z0-9_]* >;
EOF ::= <END OF FILE> ;