-
Notifications
You must be signed in to change notification settings - Fork 10
/
lever-0.9.0.grammar
276 lines (225 loc) · 6.96 KB
/
lever-0.9.0.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# This file is outdated and here for the compiler/
#
# The new file is in the lever-A.grammar
use indentation(indent, dedent, newline)
can_close = [")", "]", "}", ","]
file: sep(cond_statement, newline)
block: indent join(cond_statement, newline) dedent
cond_statement:
block_statement 'if' statement / if(., [1])
block_statement 'while' statement / while(., [1])
block_statement 'for' symbol 'in' subexpr / for(.., [1])
block_statement
block_statement:
statement
'return' statement? / return
'yield' statement / yield
'if' statement block otherwise / if
local_symbol '=' block_statement / local_assign
symbol ':=' block_statement / upvalue_assign
slot op '=' block_statement / op_assign
expr '[' subexpr ']' '=' block_statement / setitem
expr '.' symbol '=' block_statement / setattr
expr '.' symbol '?=' block_statement / setattr_default
'[' sep(symbol, ',') ']' '=' block_statement / unpack_assign
symbol ',' sep(symbol, ',') '=' block_statement / unpack_assign([.] concat ., ..)
'while' statement block / while
'for' symbol 'in' subexpr block / for
'for' '[' sep(symbol, ',') ']' 'in' subexpr block / for_unpack
'for' symbol ',' sep(symbol, ',') 'in' subexpr block / for_unpack([.] concat ., ..)
'import' join(symbol, ',') / import
'from' symbol 'import' join(symbol, ',') / from_import
'from' symbol 'import' '*' / from_import_all
'try' block join(except, newline) / try
'try' block sep(except, newline) finally / try
'raise' statement / raise
'break' / break
'continue' / continue
'class' class_header block / class
'class' class_header / class
'assert' statement ',' statement / assert
'assert' statement block / assert
'assert' statement / assert
class_header:
symbol / class_header
symbol 'extends' expr / class_header
local_symbol:
symbol / str_join
"+" symbol / str_join(1, 2)
"%" string / str_join
slot:
symbol / lookup_slot
expr '.' symbol / attr_slot
expr '[' expr ']' / item_slot
otherwise:
/ done
newline 'elif' statement block otherwise / elif
newline 'else' block / else
except:
'except' expr 'as' symbol block / except
finally:
'finally' block / finally
statement:
expr
subexpr:
expr
expr? ".:" expr? / slice_incr
expr? ".:" expr? "::" expr / slice_incr
expr? ":." expr? / slice_decr
expr? ":." expr? "::" expr / slice_decr
expr:
expr3
expr3 'or' expr / or
expr3:
expr5
expr5 'and' expr3 / and
expr5:
expr8
'not' expr8 / not
expr8:
expr10
expr10 "in" expr10 / in
expr10 "not" "in" expr10 / not_in
expr10 ["<"] expr10 / binary
expr10 [">"] expr10 / binary
expr10 ["=="] expr10 / binary
expr10 ["!="] expr10 / binary
expr10 ["<="] expr10 / binary
expr10 [">="] expr10 / binary
expr10:
expr20
expr10 ["|"] expr20 / binary
expr20:
expr30
# There is plenty of evidence that the ^ as XOR
# operation confuses beginners because they think it is an exponentiation.
#
# I did a temporary measure to disable the ^, the idea is that
# eventually this symbol will take the place of ** and we'll
# figure out an another place for the xor.
#expr20 ["^"] expr30 / binary
#
# The temporary disable ensures that we will get to
# purge the xor '^' out of the code before switching.
expr30:
expr50
expr30 ["&"] expr50 / binary
expr50:
expr100
expr50 ["<<"] expr100 / binary
expr50 [">>"] expr100 / binary
expr100:
expr200
expr100 ["++"] expr200 / binary
expr100 ["+"] expr200 / binary
expr100 ["-"] expr200 / binary
expr200:
prefix
expr200 ["*"] prefix / binary
expr200 ["/"] prefix / binary
expr200 ["//"] prefix / binary
expr200 ["%"] prefix / binary
prefix:
exponent_postfix
["+"] exponent_postfix / prefix
["-"] exponent_postfix / prefix
op:
"|"
"^"
"&"
"<<"
">>"
"++"
"+"
"-"
"%"
"/"
"*"
exponent_postfix:
postfix
postfix "**" exponent_postfix / exponent
postfix:
term
postfix "(" arguments ")" / call
postfix "(" subexpr_last "..." ")" / callv
postfix "[" subexpr "]" / getitem
postfix "." symbol / getattr
postfix ".?" symbol / getattr_or_null
postfix ";" block / scopegrabber # It occured to me that scope grab
# is an useful concept for passing values around.
# so we need some syntax for it.
# The syntax for inlined records is a little bit
# experimental. It may be desirable to handle this a bit
# differently.
arguments:
/ []
record_last
record_last ','
subexpr_last
subexpr_last ','
record_last:
inline_record / [.]
subexpr_last ',' inline_record / (. append .)
subexpr_last:
join(subexpr, ',')
record_last ',' join(subexpr, ',') / (. concat .)
inline_record: join(symbol "=" subexpr, ',') / record
term:
symbol / lookup
int / int
hex / hex
float / float
string / string
"(" subexpr ")"
"(" sep(symbol "=" subexpr, ',') ")" / record
"[" sep(subexpr, ',', ',') "]" / list
"(" bindings ")" ":" block / function
":" block / function(blank_bindings(), .)
"{" pairs "}" / dict
"{" nl_pairs "}" / dict
"{" escaped_keyword "}" / lookup
"%" string / lookup
":" expr block / scopegrabber # DEPRECATED: remove soon.
# The notation to use ':' on the
# left side of expression could be
# argued to be confusing.
terminal int, hex, float, string, symbol
bindings:
/ blank_bindings
optionals
optionals ','
optionals ',' symbol '...' / with_variadic
symbol '...' / only_variadic
optionals:
optional / first_optional
optionals ',' optional / append_optional
mandatorys
optional: symbol '=' expr / optional
mandatorys:
symbol / mandatory
mandatorys ',' symbol / append_mandatory
nl_pairs: indent pairlines dedent
pairlines:
pair / [.]
pairlines newline pair / (. append .)
pair "," / [.]
pairlines newline pair "," / (. append .)
pairs:
/ []
pairs1
pairs1 ','
pairs1:
pair / [.]
pairs1 ',' pair / (. append .)
pair:
expr ":" expr
[symbol / string] "=" expr
# nl_pairs:
# indent join(pair ','? / (1), newline) dedent
#
# pairs: sep(pair, ',', ',')
#
# pair:
# expr ':' expr
# [symbol / string] '=' expr
escaped_keyword: ['import', 'and', 'or', 'not']