@@ -34,26 +34,31 @@ module.exports = grammar({
3434 word : $ => $ . _identifier_without_operators ,
3535 conflicts : $ => [
3636 [ $ . infix_application ] ,
37- [ $ . _expression , $ . pattern ] ,
37+ [ $ . _simple_expression , $ . pattern ] ,
3838 [ $ . _literal , $ . _literal_pattern ] ,
3939 [ $ . string , $ . string_pattern ] ,
40- [ $ . map , $ . map_pattern ] ,
41- [ $ . pattern_pair , $ . _literal ] ,
4240 [ $ . shorthand_pair_identifier_pattern , $ . map ]
4341 ] ,
4442
4543 rules : {
46- program : $ => seq ( optional ( $ . hash_bang_line ) , optional ( $ . _statement_seq ) ) ,
44+ program : $ => seq (
45+ optional ( $ . hash_bang_line ) ,
46+ optional ( repeat1 ( $ . _statement ) ) ,
47+ ) ,
4748 hash_bang_line : $ => / # ! .* / ,
4849
4950 _statement : $ => choice (
51+ seq ( $ . _simple_statement , $ . _newline ) ,
52+ $ . _compound_statement
53+ ) ,
54+ _simple_statement : $ => choice (
5055 $ . import ,
51- $ . export ,
52- $ . _expression
56+ alias ( $ . simple_export , $ . export ) ,
57+ $ . _simple_expression
5358 ) ,
54- _statement_seq : $ => choice (
55- $ . _statement ,
56- seq ( $ . _statement , $ . _newline , optional ( $ . _statement_seq ) ) ,
59+ _compound_statement : $ => choice (
60+ alias ( $ . compound_export , $ . export ) ,
61+ $ . _compound_expression
5762 ) ,
5863
5964 import : $ => seq (
@@ -73,40 +78,53 @@ module.exports = grammar({
7378 field ( 'right' , $ . identifier )
7479 ) ,
7580
76- export : $ => seq (
81+ simple_export : $ => seq (
82+ 'export' ,
83+ field ( 'declaration' , alias ( $ . simple_assignment , $ . assignment ) )
84+ ) ,
85+ compound_export : $ => seq (
7786 'export' ,
78- field ( 'declaration' , $ . assignment )
87+ field ( 'declaration' , alias ( $ . compound_assignment , $ . assignment ) )
7988 ) ,
8089
81- _expression : $ => prec . left ( PREC . EXPRESSION , choice (
90+ _expression : $ => choice (
91+ seq ( $ . _simple_expression , $ . _newline ) ,
92+ $ . _compound_expression
93+ ) ,
94+ _simple_expression : $ => prec . left ( PREC . EXPRESSION , choice (
8295 $ . _group ,
83- $ . abstraction ,
96+ alias ( $ . simple_abstraction , $ . abstraction ) ,
8497 $ . application ,
8598 $ . prefix_application ,
8699 $ . infix_application ,
87100 $ . pipeline ,
88- $ . assignment ,
101+ alias ( $ . simple_assignment , $ . assignment ) ,
89102 $ . return ,
103+ alias ( $ . simple_if , $ . if ) ,
90104 $ . map ,
91105 $ . tuple ,
92106 $ . list ,
93107 $ . list_comprehension ,
94108 $ . identifier ,
95109 $ . _literal
96110 ) ) ,
97- _expression_seq : $ => choice (
98- $ . _expression ,
99- seq ( $ . _expression , $ . _newline , optional ( $ . _expression_seq ) ) ,
111+ _compound_expression : $ => choice (
112+ alias ( $ . compound_abstraction , $ . abstraction ) ,
113+ alias ( $ . compound_assignment , $ . assignment ) ,
114+ alias ( $ . compound_if , $ . if ) ,
100115 ) ,
101116
102- block : $ => seq ( $ . _indent , $ . _expression_seq , $ . _dedent ) ,
117+ block : $ => choice (
118+ seq ( $ . _simple_expression , $ . _newline ) ,
119+ seq ( $ . _newline , $ . _indent , repeat1 ( $ . _expression ) , $ . _dedent )
120+ ) ,
103121
104122 pattern : $ => prec ( PREC . PATTERN , choice (
105123 field ( 'pattern' , $ . _destructuring_pattern ) ,
106124 field ( 'value' , $ . _literal_pattern ) ,
107125 seq (
108126 field ( 'name' , alias ( $ . identifier , $ . identifier_pattern ) ) ,
109- optional ( seq ( '=' , field ( 'value' , $ . _expression ) ) )
127+ optional ( seq ( '=' , field ( 'value' , $ . _simple_expression ) ) )
110128 )
111129 ) ) ,
112130
@@ -143,7 +161,7 @@ module.exports = grammar({
143161 ) ,
144162 shorthand_pair_identifier_pattern : $ => seq (
145163 field ( 'name' , alias ( $ . identifier , $ . identifier_pattern ) ) ,
146- optional ( seq ( '=' , field ( 'value' , $ . _expression ) ) )
164+ optional ( seq ( '=' , field ( 'value' , $ . _simple_expression ) ) )
147165 ) ,
148166 rest : $ => seq (
149167 '...' ,
@@ -174,81 +192,126 @@ module.exports = grammar({
174192 ')'
175193 ) ,
176194
177- argument : $ => choice ( '?' , field ( 'value' , $ . _expression ) ) ,
195+ argument : $ => prec . left ( choice ( '?' , field ( 'value' , $ . _simple_expression ) ) ) ,
178196 arguments : $ => commaSep1 ( $ . argument ) ,
179197
180- _group : $ => seq ( '(' , $ . _expression , ')' ) ,
198+ _group : $ => seq ( '(' , $ . _simple_expression , ')' ) ,
181199
182- abstraction : $ => prec . left ( commaSep1 ( $ . abstraction_branch ) ) ,
183- abstraction_branch : $ => seq (
200+ simple_abstraction : $ => prec . left ( commaSep1 (
201+ alias ( $ . simple_abstraction_branch , $ . abstraction_branch )
202+ ) ) ,
203+ simple_abstraction_branch : $ => prec . left ( seq (
184204 field ( 'parameters' , $ . parameters ) ,
185205 '=>' ,
186- field ( 'body' , $ . _expression )
187- ) ,
206+ field ( 'body' , $ . _simple_expression )
207+ ) ) ,
208+ compound_abstraction : $ => prec . left ( choice (
209+ alias ( $ . compound_abstraction_branch , $ . abstraction_branch ) ,
210+ seq (
211+ $ . _indent ,
212+ repeat1 ( alias ( $ . compound_abstraction_branch , $ . abstraction_branch ) ) ,
213+ $ . _dedent
214+ )
215+ ) ) ,
216+ compound_abstraction_branch : $ => prec . left ( seq (
217+ field ( 'parameters' , $ . parameters ) ,
218+ '=>' ,
219+ field ( 'body' , $ . block )
220+ ) ) ,
188221
189222 application : $ => prec ( PREC . APPLICATION , seq (
190- field ( 'abstraction' , $ . _expression ) ,
223+ field ( 'abstraction' , $ . _simple_expression ) ,
191224 '(' ,
192225 field ( 'arguments' , $ . arguments ) ,
193226 ')'
194227 ) ) ,
195228 prefix_application : $ => prec . right ( PREC . PREFIX , seq (
196229 field ( 'abstraction' , $ . identifier ) ,
197- field ( 'argument' , $ . _expression )
230+ field ( 'argument' , $ . _simple_expression )
198231 ) ) ,
199232 infix_application : $ => choice (
200- prec . left ( PREC . NOT , seq ( field ( 'left' , $ . _expression ) , field ( 'abstraction' , alias ( '!' , $ . infix_application_operator ) ) , field ( 'right' , $ . _expression ) ) ) ,
201- prec . left ( PREC . EXPONENTIATION , seq ( field ( 'left' , $ . _expression ) , field ( 'abstraction' , alias ( '^' , $ . infix_application_operator ) ) , field ( 'right' , $ . _expression ) ) ) ,
202- prec . left ( PREC . PRODUCT , seq ( field ( 'left' , $ . _expression ) , field ( 'abstraction' , alias ( '*' , $ . infix_application_operator ) ) , field ( 'right' , $ . _expression ) ) ) ,
203- prec . left ( PREC . PRODUCT , seq ( field ( 'left' , $ . _expression ) , field ( 'abstraction' , alias ( '/' , $ . infix_application_operator ) ) , field ( 'right' , $ . _expression ) ) ) ,
204- prec . left ( PREC . PRODUCT , seq ( field ( 'left' , $ . _expression ) , field ( 'abstraction' , alias ( '%' , $ . infix_application_operator ) ) , field ( 'right' , $ . _expression ) ) ) ,
205- prec . left ( PREC . SUM , seq ( field ( 'left' , $ . _expression ) , field ( 'abstraction' , alias ( '+' , $ . infix_application_operator ) ) , field ( 'right' , $ . _expression ) ) ) ,
206- prec . left ( PREC . SUM , seq ( field ( 'left' , $ . _expression ) , field ( 'abstraction' , alias ( '-' , $ . infix_application_operator ) ) , field ( 'right' , $ . _expression ) ) ) ,
207- prec . left ( PREC . ORDER , seq ( field ( 'left' , $ . _expression ) , field ( 'abstraction' , alias ( '<' , $ . infix_application_operator ) ) , field ( 'right' , $ . _expression ) ) ) ,
208- prec . left ( PREC . ORDER , seq ( field ( 'left' , $ . _expression ) , field ( 'abstraction' , alias ( '<=' , $ . infix_application_operator ) ) , field ( 'right' , $ . _expression ) ) ) ,
209- prec . left ( PREC . ORDER , seq ( field ( 'left' , $ . _expression ) , field ( 'abstraction' , alias ( '>' , $ . infix_application_operator ) ) , field ( 'right' , $ . _expression ) ) ) ,
210- prec . left ( PREC . ORDER , seq ( field ( 'left' , $ . _expression ) , field ( 'abstraction' , alias ( '>=' , $ . infix_application_operator ) ) , field ( 'right' , $ . _expression ) ) ) ,
211- prec . left ( PREC . EQUALITY , seq ( field ( 'left' , $ . _expression ) , field ( 'abstraction' , alias ( '==' , $ . infix_application_operator ) ) , field ( 'right' , $ . _expression ) ) ) ,
212- prec . left ( PREC . EQUALITY , seq ( field ( 'left' , $ . _expression ) , field ( 'abstraction' , alias ( '!=' , $ . infix_application_operator ) ) , field ( 'right' , $ . _expression ) ) ) ,
213- prec . left ( PREC . EQUALITY , seq ( field ( 'left' , $ . _expression ) , field ( 'abstraction' , alias ( '===' , $ . infix_application_operator ) ) , field ( 'right' , $ . _expression ) ) ) ,
214- prec . left ( PREC . EQUALITY , seq ( field ( 'left' , $ . _expression ) , field ( 'abstraction' , alias ( '!==' , $ . infix_application_operator ) ) , field ( 'right' , $ . _expression ) ) ) ,
215- prec . left ( PREC . AND , seq ( field ( 'left' , $ . _expression ) , field ( 'abstraction' , alias ( '&&' , $ . infix_application_operator ) ) , field ( 'right' , $ . _expression ) ) ) ,
216- prec . left ( PREC . OR , seq ( field ( 'left' , $ . _expression ) , field ( 'abstraction' , alias ( '||' , $ . infix_application_operator ) ) , field ( 'right' , $ . _expression ) ) ) ,
217- prec . left ( PREC . IMPLICATION , seq ( field ( 'left' , $ . _expression ) , field ( 'abstraction' , alias ( '==>' , $ . infix_application_operator ) ) , field ( 'right' , $ . _expression ) ) ) ,
218- prec . left ( PREC . BICONDITIONAL , seq ( field ( 'left' , $ . _expression ) , field ( 'abstraction' , alias ( '<=>' , $ . infix_application_operator ) ) , field ( 'right' , $ . _expression ) ) ) ,
233+ prec . left ( PREC . NOT , seq ( field ( 'left' , $ . _simple_expression ) , field ( 'abstraction' , alias ( '!' , $ . infix_application_operator ) ) , field ( 'right' , $ . _simple_expression ) ) ) ,
234+ prec . left ( PREC . EXPONENTIATION , seq ( field ( 'left' , $ . _simple_expression ) , field ( 'abstraction' , alias ( '^' , $ . infix_application_operator ) ) , field ( 'right' , $ . _simple_expression ) ) ) ,
235+ prec . left ( PREC . PRODUCT , seq ( field ( 'left' , $ . _simple_expression ) , field ( 'abstraction' , alias ( '*' , $ . infix_application_operator ) ) , field ( 'right' , $ . _simple_expression ) ) ) ,
236+ prec . left ( PREC . PRODUCT , seq ( field ( 'left' , $ . _simple_expression ) , field ( 'abstraction' , alias ( '/' , $ . infix_application_operator ) ) , field ( 'right' , $ . _simple_expression ) ) ) ,
237+ prec . left ( PREC . PRODUCT , seq ( field ( 'left' , $ . _simple_expression ) , field ( 'abstraction' , alias ( '%' , $ . infix_application_operator ) ) , field ( 'right' , $ . _simple_expression ) ) ) ,
238+ prec . left ( PREC . SUM , seq ( field ( 'left' , $ . _simple_expression ) , field ( 'abstraction' , alias ( '+' , $ . infix_application_operator ) ) , field ( 'right' , $ . _simple_expression ) ) ) ,
239+ prec . left ( PREC . SUM , seq ( field ( 'left' , $ . _simple_expression ) , field ( 'abstraction' , alias ( '-' , $ . infix_application_operator ) ) , field ( 'right' , $ . _simple_expression ) ) ) ,
240+ prec . left ( PREC . ORDER , seq ( field ( 'left' , $ . _simple_expression ) , field ( 'abstraction' , alias ( '<' , $ . infix_application_operator ) ) , field ( 'right' , $ . _simple_expression ) ) ) ,
241+ prec . left ( PREC . ORDER , seq ( field ( 'left' , $ . _simple_expression ) , field ( 'abstraction' , alias ( '<=' , $ . infix_application_operator ) ) , field ( 'right' , $ . _simple_expression ) ) ) ,
242+ prec . left ( PREC . ORDER , seq ( field ( 'left' , $ . _simple_expression ) , field ( 'abstraction' , alias ( '>' , $ . infix_application_operator ) ) , field ( 'right' , $ . _simple_expression ) ) ) ,
243+ prec . left ( PREC . ORDER , seq ( field ( 'left' , $ . _simple_expression ) , field ( 'abstraction' , alias ( '>=' , $ . infix_application_operator ) ) , field ( 'right' , $ . _simple_expression ) ) ) ,
244+ prec . left ( PREC . EQUALITY , seq ( field ( 'left' , $ . _simple_expression ) , field ( 'abstraction' , alias ( '==' , $ . infix_application_operator ) ) , field ( 'right' , $ . _simple_expression ) ) ) ,
245+ prec . left ( PREC . EQUALITY , seq ( field ( 'left' , $ . _simple_expression ) , field ( 'abstraction' , alias ( '!=' , $ . infix_application_operator ) ) , field ( 'right' , $ . _simple_expression ) ) ) ,
246+ prec . left ( PREC . EQUALITY , seq ( field ( 'left' , $ . _simple_expression ) , field ( 'abstraction' , alias ( '===' , $ . infix_application_operator ) ) , field ( 'right' , $ . _simple_expression ) ) ) ,
247+ prec . left ( PREC . EQUALITY , seq ( field ( 'left' , $ . _simple_expression ) , field ( 'abstraction' , alias ( '!==' , $ . infix_application_operator ) ) , field ( 'right' , $ . _simple_expression ) ) ) ,
248+ prec . left ( PREC . AND , seq ( field ( 'left' , $ . _simple_expression ) , field ( 'abstraction' , alias ( '&&' , $ . infix_application_operator ) ) , field ( 'right' , $ . _simple_expression ) ) ) ,
249+ prec . left ( PREC . OR , seq ( field ( 'left' , $ . _simple_expression ) , field ( 'abstraction' , alias ( '||' , $ . infix_application_operator ) ) , field ( 'right' , $ . _simple_expression ) ) ) ,
250+ prec . left ( PREC . IMPLICATION , seq ( field ( 'left' , $ . _simple_expression ) , field ( 'abstraction' , alias ( '==>' , $ . infix_application_operator ) ) , field ( 'right' , $ . _simple_expression ) ) ) ,
251+ prec . left ( PREC . BICONDITIONAL , seq ( field ( 'left' , $ . _simple_expression ) , field ( 'abstraction' , alias ( '<=>' , $ . infix_application_operator ) ) , field ( 'right' , $ . _simple_expression ) ) ) ,
219252 prec . left ( PREC . OPERATOR_INFIX , seq (
220- field ( 'left' , $ . _expression ) ,
253+ field ( 'left' , $ . _simple_expression ) ,
221254 field ( 'abstraction' , alias ( $ . _operator , $ . infix_application_operator ) ) ,
222- field ( 'right' , $ . _expression ) )
255+ field ( 'right' , $ . _simple_expression ) )
223256 ) ,
224257 prec . left ( PREC . NAMED_INFIX , seq (
225- field ( 'left' , $ . _expression ) ,
258+ field ( 'left' , $ . _simple_expression ) ,
226259 '`' ,
227260 field ( 'abstraction' , alias ( $ . _identifier_without_operators , $ . infix_application_operator ) ) ,
228261 '`' ,
229- field ( 'right' , $ . _expression ) )
262+ field ( 'right' , $ . _simple_expression ) )
230263 )
231264 ) ,
232265
233266 pipeline : $ => prec . left ( PREC . PIPELINE , seq (
234- field ( 'left' , $ . _expression ) ,
267+ field ( 'left' , $ . _simple_expression ) ,
235268 '.' ,
236- field ( 'right' , $ . _expression )
269+ field ( 'right' , $ . _simple_expression )
237270 ) ) ,
238271
239- assignment : $ => seq (
272+ simple_assignment : $ => seq (
273+ field ( 'left' , choice (
274+ alias ( $ . identifier , $ . identifier_pattern ) ,
275+ $ . _destructuring_pattern
276+ ) ) ,
277+ ':=' ,
278+ field ( 'right' , $ . _simple_expression )
279+ ) ,
280+ compound_assignment : $ => seq (
240281 field ( 'left' , choice (
241282 alias ( $ . identifier , $ . identifier_pattern ) ,
242283 $ . _destructuring_pattern
243284 ) ) ,
244285 ':=' ,
245- field ( 'right' , $ . _expression )
286+ field ( 'right' , $ . _compound_expression )
246287 ) ,
247288
248289 return : $ => prec . right ( seq (
249290 'return' ,
250- optional ( field ( 'value' , $ . _expression ) )
291+ optional ( field ( 'value' , $ . _simple_expression ) )
292+ ) ) ,
293+
294+ simple_if : $ => prec . right ( seq (
295+ 'if' ,
296+ field ( 'condition' , $ . _simple_expression ) ,
297+ 'then' ,
298+ field ( 'consequence' , $ . _simple_expression ) ,
299+ seq ( 'else' , field ( 'alternative' , $ . _simple_expression ) )
300+ ) ) ,
301+ compound_if : $ => prec . right ( seq (
302+ 'if' ,
303+ field ( 'condition' , $ . _simple_expression ) ,
304+ 'then' ,
305+ field ( 'consequence' , $ . block ) ,
306+ field ( 'alternatives' , alias ( repeat ( $ . else_if_clause ) , $ . else_if_clauses ) ) ,
307+ optional ( seq ( 'else' , field ( 'alternative' , $ . block ) ) )
251308 ) ) ,
309+ else_if_clause : $ => seq (
310+ 'else if' ,
311+ field ( 'condition' , $ . _simple_expression ) ,
312+ 'then' ,
313+ field ( 'consequence' , $ . block )
314+ ) ,
252315
253316 map : $ => seq (
254317 '{' ,
@@ -259,18 +322,18 @@ module.exports = grammar({
259322 ) ) ,
260323 '}'
261324 ) ,
262- tuple : $ => seq ( '(' , commaSep2 ( choice ( $ . _expression , $ . spread ) ) , ')' ) ,
263- list : $ => seq ( '[' , commaSep ( choice ( $ . _expression , $ . spread ) ) , ']' ) ,
325+ tuple : $ => seq ( '(' , commaSep2 ( choice ( $ . _simple_expression , $ . spread ) ) , ')' ) ,
326+ list : $ => seq ( '[' , commaSep ( choice ( $ . _simple_expression , $ . spread ) ) , ']' ) ,
264327 expression_pair : $ => seq (
265- field ( 'left' , $ . _expression ) ,
328+ field ( 'left' , $ . _simple_expression ) ,
266329 '->' ,
267- field ( 'right' , $ . _expression )
330+ field ( 'right' , $ . _simple_expression )
268331 ) ,
269- spread : $ => seq ( '...' , field ( 'value' , $ . _expression ) ) ,
332+ spread : $ => seq ( '...' , field ( 'value' , $ . _simple_expression ) ) ,
270333
271334 list_comprehension : $ => seq (
272335 '[' ,
273- field ( 'body' , $ . _expression ) ,
336+ field ( 'body' , $ . _simple_expression ) ,
274337 '|' ,
275338 field ( 'generators' , $ . generators ) ,
276339 ']'
@@ -279,10 +342,10 @@ module.exports = grammar({
279342 generator : $ => seq (
280343 field ( 'name' , $ . identifier ) ,
281344 'in' ,
282- field ( 'value' , $ . _expression ) ,
345+ field ( 'value' , $ . _simple_expression ) ,
283346 optional ( field ( 'condition' , $ . generator_condition ) )
284347 ) ,
285- generator_condition : $ => seq ( 'if' , $ . _expression ) ,
348+ generator_condition : $ => seq ( 'if' , $ . _simple_expression ) ,
286349
287350 _identifier_without_operators : $ => / [ a - z _ ] [ a - z 0 - 9 _ ] * \? ? / ,
288351 _operator : $ => choice ( / ( = = | [ ! @ $ % ^ & * | < > ~ * \\ \- + / . ] + ) = * > ? / , '/' ) ,
@@ -321,7 +384,7 @@ module.exports = grammar({
321384 ) ,
322385 interpolation : $ => seq (
323386 '{' ,
324- field ( 'value' , $ . _expression ) ,
387+ field ( 'value' , $ . _simple_expression ) ,
325388 '}'
326389 ) ,
327390 escape_sequence : $ => token . immediate ( seq (
0 commit comments