Skip to content
This repository was archived by the owner on Jun 1, 2022. It is now read-only.

Commit 511ffe7

Browse files
authored
[#41] Case (#42)
1 parent 64b032d commit 511ffe7

File tree

2 files changed

+94
-33
lines changed

2 files changed

+94
-33
lines changed

grammar.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,11 @@ module.exports = grammar({
112112
alias($.compound_abstraction, $.abstraction),
113113
alias($.compound_assignment, $.assignment),
114114
alias($.compound_if, $.if),
115+
alias($.compound_case, $.case)
115116
),
116117

117118
block: $ => choice(
118-
seq($._simple_expression, $._newline),
119+
seq(optional('then'), $._simple_expression, $._newline),
119120
seq($._newline, $._indent, repeat1($._expression), $._dedent)
120121
),
121122

@@ -205,13 +206,8 @@ module.exports = grammar({
205206
'=>',
206207
field('body', $._simple_expression)
207208
)),
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-
)
209+
compound_abstraction: $ => prec.left(repeat1(
210+
alias($.compound_abstraction_branch, $.abstraction_branch)
215211
)),
216212
compound_abstraction_branch: $ => prec.left(seq(
217213
field('parameters', $.parameters),
@@ -283,7 +279,14 @@ module.exports = grammar({
283279
$._destructuring_pattern
284280
)),
285281
':=',
286-
field('right', $._compound_expression)
282+
choice(
283+
field('right', $._compound_expression),
284+
seq(
285+
$._indent,
286+
field('right', $._compound_expression),
287+
$._dedent
288+
)
289+
)
287290
),
288291

289292
return: $ => prec.right(seq(
@@ -301,18 +304,29 @@ module.exports = grammar({
301304
compound_if: $ => prec.right(seq(
302305
'if',
303306
field('condition', $._simple_expression),
304-
'then',
305307
field('consequence', $.block),
306308
field('alternatives', alias(repeat($.else_if_clause), $.else_if_clauses)),
307309
optional(seq('else', field('alternative', $.block)))
308310
)),
309311
else_if_clause: $ => seq(
310312
'else if',
311313
field('condition', $._simple_expression),
312-
'then',
313314
field('consequence', $.block)
314315
),
315316

317+
compound_case: $ => seq(
318+
'case',
319+
field('value', $._simple_expression),
320+
field('branches', alias(repeat1($.when_clause), $.when_clauses)),
321+
optional(seq('else', field('default', $.block)))
322+
),
323+
when_clause: $ => seq(
324+
'when',
325+
field('values', $.expression_list),
326+
field('consequence', $.block)
327+
),
328+
expression_list: $ => commaSep1($._simple_expression),
329+
316330
map: $ => seq(
317331
'{',
318332
commaSep(choice(

test/corpus/expressions.txt

Lines changed: 69 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ abstraction
77

88
fib :=
99
(0) =>
10-
0
10+
0
1111
(1) => 1
1212
(n = 2) =>
13-
fib(n - 1) + fib(n - 2)
13+
fib(n - 1) + fib(n - 2)
1414

1515
(a) =>
1616
b := (c) =>
@@ -418,17 +418,18 @@ return (a) =>
418418
if
419419
==================
420420

421-
if false then
421+
if false
422422
1
423-
if true then
423+
if true
424424
1
425425
else
426426
1
427-
if true then
428-
1
429-
else if false then 1
430-
else if true then
431-
1
427+
a :=
428+
if true
429+
1
430+
else if false then 1
431+
else if true
432+
1
432433

433434
if false then 1
434435
if true then 1 else 1
@@ -446,19 +447,21 @@ if true then 1 else 1
446447
(number))
447448
alternative: (block
448449
(number)))
449-
(if
450-
condition: (boolean)
451-
consequence: (block
452-
(number))
453-
alternatives: (else_if_clauses
454-
(else_if_clause
455-
condition: (boolean)
456-
consequence: (block
457-
(number)))
458-
(else_if_clause
459-
condition: (boolean)
460-
consequence: (block
461-
(number)))))
450+
(assignment
451+
left: (identifier_pattern)
452+
right: (if
453+
condition: (boolean)
454+
consequence: (block
455+
(number))
456+
alternatives: (else_if_clauses
457+
(else_if_clause
458+
condition: (boolean)
459+
consequence: (block
460+
(number)))
461+
(else_if_clause
462+
condition: (boolean)
463+
consequence: (block
464+
(number))))))
462465
(if
463466
condition: (boolean)
464467
consequence: (block
@@ -467,3 +470,47 @@ if true then 1 else 1
467470
condition: (boolean)
468471
consequence: (number)
469472
alternative: (number)))
473+
474+
==================
475+
case
476+
==================
477+
478+
a := case a
479+
when 1
480+
1
481+
482+
case a
483+
when 1
484+
1
485+
when 1, 1 then 1
486+
else 1
487+
488+
---
489+
490+
(program
491+
(assignment
492+
left: (identifier_pattern)
493+
right: (case
494+
value: (identifier)
495+
branches: (when_clauses
496+
(when_clause
497+
values: (expression_list
498+
(number))
499+
consequence: (block
500+
(number))))))
501+
(case
502+
value: (identifier)
503+
branches: (when_clauses
504+
(when_clause
505+
values: (expression_list
506+
(number))
507+
consequence: (block
508+
(number)))
509+
(when_clause
510+
values: (expression_list
511+
(number)
512+
(number))
513+
consequence: (block
514+
(number))))
515+
default: (block
516+
(number))))

0 commit comments

Comments
 (0)