-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrammar.ebnf
129 lines (74 loc) · 3.23 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Entrypoint
program = { statement }
statement = globalStatement
| localStatement
globalStatement = functionDeclaration
| classDeclaration
| localStatement
localStatement = ifStatement
| whileStatement
| forStatement
| variableStatement
| assignmentStatement
| returnStatement
| breakStatement
| continueStatement
| expressionStatement
anyBody = "{" { localStatement } "}"
functionDeclaration = "func" IDENTIFIER "(" paramsList ")" anyBody
paramsList = [ IDENTIFIER { "," IDENTIFIER } ]
classDeclaration = "class" IDENTIFIER "{" constructorDeclaration { methodDeclaration } "}"
constructorDeclaration = "constructor" "(" paramsList ")" anyBody
methodDeclaration = functionDeclaration
ifStatement = "if" "(" expression ")" anyBody [ "else" anyBody ]
whileStatement = "while" "(" expression ")" anyBody
forInit = variableStatement
| assignmentStatement
| expressionStatement
forIncrement = assignmentStatementNoSemicolon
| expression
forStatement = "for" "(" forInit expression ";" forIncrement ")" anyBody
variableStatement = "var" IDENTIFIER "=" expression ";"
assignmentStatement = assignmentStatementNoSemicolon ";"
assignmentStatementNoSemicolon = IDENTIFIER { "." IDENTIFIER } ASSIGNMENT_OP expression
ASSIGNMENT_OP = "=" | "+=" | "-=" | "*=" | "/=" | "\\=" | "%=" | "^=" | "&=" | "|=" | "++=" |
returnStatement = "return" [ expression ] ";"
breakStatement = "break" ";"
continueStatement = "continue" ";"
expressionStatement = expression ";"
expression = literalExpression
| variableExpression
| groupingExpression
| unaryExpression
| binaryExpression
| functionCallExpression
| objectAccessExpression
variableExpression = IDENTIFIER
groupingExpression = "(" expression ")"
literalExpression = NUMBER | STRING | BOOLEAN | NULL
unaryExpression = UN_OP expression
UN_OP = "-" | "!" | "@" | "#"
binaryExpression = expression BIN_OP expression
BIN_OP = "+" | "-" | "*" | "/" | "\\" | "%" | "^" | "&" | "|" | ">" | "<" | "<=" | ">=" | "==" | "!=" | "++"
functionCallExpression = expression "(" callParamsList ")"
callParamsList = [ expression { "," expression } ]
objectAccessExpression = expression "." IDENTIFIER
IDENTIFIER = ID_ALPHA { (ID_ALPHA | DIGIT_10 | ANY_UNICODE_EMOJI) }
ID_ALPHA = LETTER | "_"
NUMBER = NUMBER_2 | NUMBER_10 | NUMBER_16
NUMBER_2 = "0b" DIGIT_2 { DIGIT_2 }
NUMBER_10 = NUMBER_10_INT [ "." NUMBER_10_INT ]
NUMBER_10_INT = DIGIT_10 { DIGIT_10 }
NUMBER_16 = "0x" DIGIT_16 { DIGIT_16 }
DIGIT_2 = "0" | "1"
DIGIT_10 = DIGIT_2 | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
DIGIT_16 = DIGIT_10 | "a" | "b" | "c" | "d" | "e" | "f" | "A" | "B" | "C" | "D" | "E" | "F"
STRING = "\"" STRING_ELEM "\""
STRING_ELEM = ALLOWED_CHARACTER | INTERPOLATION | ESCAPE_SEQUENCE
ALLOWED_CHARACTER = ... # any unicode character except for: \ " { }
INTERPOLATION = "{" expression "}"
ESCAPE_SEQUENCE = "\\" ESCAPE_CONTENT
ESCAPE_CONTENT = "r" | "n" | "\\" | "\"" | "{" | "}"
| ( "u{" ( NUMBER_2 | NUMBER_10_INT | NUMBER_16 ) "}" )
BOOLEAN = "true" | "false"
NULL = null