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

Commit e364f7c

Browse files
authored
[#34] Rest parameters (#35)
1 parent 1a91a89 commit e364f7c

File tree

3 files changed

+33
-30
lines changed

3 files changed

+33
-30
lines changed

grammar.js

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,20 @@ module.exports = grammar({
114114
)),
115115
parameters: $ => seq(
116116
'(',
117-
commaSep1($.parameter),
117+
choice(
118+
seq(
119+
commaSep1($.parameter),
120+
optional(seq(',', $.rest_parameter))
121+
),
122+
$.rest_parameter
123+
),
118124
')'
119125
),
126+
rest_parameter: $ => seq(
127+
'...',
128+
field('name', alias($.identifier, $.identifier_pattern)),
129+
optional(seq('=', field('value', $._expression)))
130+
),
120131

121132
argument: $ => choice('?', field('value', $._expression)),
122133
arguments: $ => commaSep1($.argument),
@@ -138,45 +149,35 @@ module.exports = grammar({
138149
prec(PREC.REGEX, $.regex)
139150
),
140151
map_pattern: $ => prec(PREC.PATTERN, seq(
141-
// '{',
142-
// commaSep1(choice(
143-
// $.pattern_pair,
144-
// alias($.identifier, $.shorthand_pair_identifier)
145-
// )),
146-
// optional($.rest),
147-
// '}'
148152
'{',
149153
commaSep1(choice(
150154
$.pattern_pair,
151-
alias($.identifier, $.shorthand_pair_identifier_pattern),
152-
$.rest
155+
alias($.identifier, $.shorthand_pair_identifier_pattern)
153156
)),
157+
optional(seq(',', $.rest_pattern)),
154158
'}'
155159
)),
156160
tuple_pattern: $ => prec(PREC.PATTERN, choice(
157-
// seq('(', $._pattern, $.rest, ')'),
158-
// seq(
159-
// '(',
160-
// commaSep2($._pattern),
161-
// optional($.rest),
162-
// ')'
163-
// )
164-
seq('(', $._pattern, $.rest, ')'),
165-
seq('(', commaSep2(choice($._pattern, $.rest)), ')')
161+
seq('(', $._pattern, ',', $.rest_pattern, ')'),
162+
seq(
163+
'(',
164+
commaSep2($._pattern),
165+
optional(seq(',', $.rest_pattern)),
166+
')'
167+
)
166168
)),
167169
list_pattern: $ => prec(PREC.PATTERN, seq(
168-
// '[',
169-
// commaSep1($._pattern),
170-
// optional($.rest),
171-
// ']'
172-
'[', commaSep1(choice($._pattern, $.rest)), ']'
170+
'[',
171+
commaSep1($._pattern),
172+
optional(seq(',', $.rest_pattern)),
173+
']'
173174
)),
174175
pattern_pair: $ => seq(
175176
field('left', $.string_pattern),
176177
'->',
177178
field('right', $._pattern)
178179
),
179-
rest: $ => seq(
180+
rest_pattern: $ => seq(
180181
'...',
181182
field('name', alias($.identifier, $.identifier_pattern))
182183
),

test/corpus/expressions.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ abstraction
33
==================
44

55
(a, b) => a
6-
(a = 1, b, 1, {c}) => a
6+
(a = 1, b, 1, {c}, ...d) => a
77

88
fib := (0) =>
99
0,
@@ -35,7 +35,9 @@ fib := (0) =>
3535
value: (number))
3636
(parameter
3737
pattern: (map_pattern
38-
(shorthand_pair_identifier_pattern))))
38+
(shorthand_pair_identifier_pattern)))
39+
(rest_parameter
40+
name: (identifier_pattern)))
3941
body: (identifier)))
4042
(assignment
4143
left: (identifier_pattern)

test/corpus/patterns.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ map pattern
2020
left: (string_pattern)
2121
right: (identifier_pattern))
2222
(shorthand_pair_identifier_pattern)
23-
(rest
23+
(rest_pattern
2424
name: (identifier_pattern)))
2525
right: (identifier)))
2626

@@ -42,7 +42,7 @@ tuple pattern
4242
(assignment
4343
left: (tuple_pattern
4444
(identifier_pattern)
45-
(rest
45+
(rest_pattern
4646
name: (identifier_pattern)))
4747
right: (identifier)))
4848

@@ -65,6 +65,6 @@ list pattern
6565
left: (list_pattern
6666
(identifier_pattern)
6767
(identifier_pattern)
68-
(rest
68+
(rest_pattern
6969
name: (identifier_pattern)))
7070
right: (identifier)))

0 commit comments

Comments
 (0)