-
Notifications
You must be signed in to change notification settings - Fork 4
/
laythe.bnf
108 lines (91 loc) · 3.21 KB
/
laythe.bnf
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
106
107
Module ::= Decl* ;
Decl ::= Symbol | Export | Stmt | Trait | TypeDecl;
Export ::= "export" Symbol ;
Symbol ::= Class | Fn | Let ;
Class ::= "class" IDENTIFIER ( "<" IDENTIFIER )?
"{" ( TypeMember ";" | Function)* "}" ;
Fn ::= "fn" Function ;
Let ::= "let" IDENTIFIER ( TypeAnnotation )? ( "=" Expr )? ";" ;
Trait ::= "trait" IDENTIFIER TypeParams? "{" TypeMember ("," TypeMember)* "}"
TraitMember ::= TypeMember | TypeMethod ;
TypeMethod ::= IDENTIFIER CallSignature ;
TypeMember ::= IDENTIFIER TypeAnnotation ";" ;
TypeDecl ::= "type" IDENTIFIER TypeParams? = Type ";" ;
Stmt ::= ExprStmt
| Import
| For
| If
| Return
| While
| Launch
| Try
| Raise
| AssignBlock ;
Function ::= IDENTIFIER CallSignature Block ;
Params ::= IDENTIFIER ( "," IDENTIFIER )* ;
Args ::= Expr ( "," Expr )* ;
ExprStmt ::= Expr ";" ;
Import ::= "import" Imports "from" STRING ;
Imports ::= ( IDENTIFIER | "{" ImportSymbol ( "," ImportSymbol )* "}" ) ;
ImportSymbol ::= ( IDENTIFIER (":" IDENTIFIER)? )
For ::= "for" IDENTIFIER "in" expr Block ;
If ::= "if" Expr Block ( "else" Block | If )? ;
Return ::= "return" Expr? ";" ;
While ::= "while" Expr Block ;
Launch ::= "launch" Expr ";"
Try ::= "try" Block "catch" Block ;
Raise ::= "raise" Expr ";" ;
AssignBlock ::= ":" Block ;
Block ::= "{" Decl* "}" ;
Expr ::= Assign ;
Assign ::= ( Call "." )? IDENTIFIER ( "=" | "<-" | "+=" | "-=" | "*=" | "/=" ) Assign | LogicOr;
LogicOr ::= LogicAnd ( "or" LogicAnd )* ;
LogicAnd ::= Equality ( "and" Equality )* ;
Equality ::= Comparison ( ( "!=" | "==" ) Comparison )* ;
Comparison ::= Addition ( ( ">" | ">=" | "<" | "<=" ) Addition )* ;
Addition ::= Multiplication ( ( "-" | "+" ) Multiplication )* ;
Multiplication ::= Unary ( ( "/" | "*" ) Unary )* ;
Unary ::= ( "<-" | "!" | "-" ) Unary | Call | Map | List ;
List ::= "[" ( Expr ( "," Expr )* ","? )? "]"
Map ::= "{" ( MapEntry ( "," MapEntry )* ","? )? "}"
MapEntry ::= Expr ":" Expr
Call ::= Primary ( "(" Args? ")" | "[" Expr "]" | "." IDENTIFIER )* ;
Primary ::= "true" | "false" | "nil" | "self"
| NUMBER | STRING | IDENTIFIER | LAMBDA
| "chan" "(" Expr ")"
| "(" Expr ")" | "[" Args? "]"
| "super" "." IDENTIFIER ;
NUMBER ::= DIGIT+ ( "." DIGIT+ )? ;
STRING ::= '"' <any char except '"'>* '"' | "'" <any char except "'">* "'";
LAMBDA ::= '|' ParameterList? '|' (Block | Expr)
IDENTIFIER ::= ALPHA ( ALPHA | DIGIT )* ;
ALPHA ::= 'a' ... 'z' | 'A' ... 'Z' | '_' ;
DIGIT ::= '0' ... '9' ;
# placeholder name
TypeParams ::= "<" TypeParam ("," TypeParam)* ">";
TypeParam ::= IDENTIFIER (":" Type)?
Type ::= TypeLiteral
| TypeReference
| TypeUnion
| PREDEFINED_TYPE ;
TypeReference ::= IDENTIFIER TypeArgs? ;
TypeArgs ::= '<' TypeArgsList '>' ;
TypeArgsList ::= TypeArg (',' TypeArg)* ;
TypeArg ::= Type ;
TypeUnion ::= TypeUnion '|' TypeIntersection
TypeIntersection ::= TypeIntersection '&' Type
TypeLiteral ::= FunctionType
| ListType
FunctionType ::= CallSignature;
CallSignature ::= TypeParams? '(' ParameterList? ')' '->' Type ;
ListType ::= Type '[' ']'
ParameterList ::= RequiredParams ;
RequiredParams ::= RequiredParam (',' RequiredParam)* ;
RequiredParam ::= IDENTIFIER TypeAnnotation? ;
TypeAnnotation ::= ':' Type ;
PREDEFINED_TYPE = 'nil'
| 'chan'
| 'number'
| 'bool'
| 'string'
| 'any'