-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar
97 lines (93 loc) · 3.08 KB
/
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
# this is file is just for my reference
# lexical
Number = [0-9]+(\.[0-9]+)?
LeftParen = (
RightParen = )
LeftBrace = {
RightBrace = }
Comma = ,
Dot = .
DotDot = ..
Colon = :
Semicolon = ;
Plus = +
Minus = -
Star = *
Slash = /
LineComment = // .* \n
Caret = ^
Bang = !
BangEqual = !=
Equal = =
PlusEqual = +=
MinusEqual = -=
StarEqual = *=
SlashEqual = /=
EqualEqual = ==
LessEqual = <=
GreaterEqual = >=
Less = <
Greater = >
Arrow = ->
FatArrow = =>
Identifier = [a-zA-Z_]+[a-zA-Z0-9_]*
Let = "let"
And = "and"
Or = "or"
For = "for"
In = "in"
While = "while"
Print = "print"
Export = "export"
True = "true"
False = "false"
Nil = "nil"
Global = "global"
If = "if"
Else = "else"
Return = "return"
# syntax
script = declaration* exportStatement? EoF ;
declaration = varDeclaration
| globDeclaration
| statement ;
varDeclaration = Let Identifier (Equal expression)? Semicolon ;
fnDeclaration = Fn Identifier LeftParen (Identifier Colon Identifier (Comma Identifier Colon Identifier)*)? RightParen
globDeclaration = Let Global Identifier Equal expression Semicolon ;
statement = exprStatement
| returnStatement
| ifStatement
| forStatement
| printStatement
| blockStatement ;
returnStatement = Return expr ;
ifStatement = If expression blockStatement ( Else blockStatement )? ;
forStatement = For identifier In expression blockStatement ;
blockStatement = LeftBrace declaration* RightBrace ;
exprStatement = expression Semicolon ;
printStatement = Print expression Semicolon ;
exportStatement = Export expression (Comma expression)* Semicolon ;
expression = ifExpression
| range ;
ifExpression = If expression blockExpression ( Else blockExpression | ifExpression )? ;
range = equality DotDot equality ;
equality = comparison ( ( BangEquality | EqualityEquality ) comparison )* ;
comparsion = addition ( ( Less | LessEqual | Greater | GreaterEqual ) addition )* ;
addition = multiplication ( ( Minus | Plus ) multiplication )* ;
multiplication = unary ( ( Slash | Star ) unary )* ;
unary = ( Bang | Minus ) unary | call ;
call = primary ( LeftParen arguments? RightParen )* ;
primary = Number
| String
| True
| False
| Nil
| LeftParen expression RightParen
| Identifier
| Global Identifier
| funcExpression
| blockExpression ;
paramList = LeftParen (Identifier (Comma Identifier)* )? RightParen ;
arguments = expression ( Comma expression )* ;
funcExpression = paramList? Arrow blockExpression ;
blockExpression = LeftBrace declaration* expression? RightBrace ;