Skip to content

Commit 8979aa8

Browse files
committed
BinaryPattern => BinaryMatchPattern
1 parent d67b89a commit 8979aa8

File tree

2 files changed

+385
-2
lines changed

2 files changed

+385
-2
lines changed
Lines changed: 373 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,373 @@
1+
# AST examples of [Pattern Matching ESTree Proposal](proposal-pattern-matching-estree)
2+
3+
Note: The `ExpressionStatement` and `Program` is omitted in the examples.
4+
5+
## 1. Object Match Pattern and "else" Match Clause
6+
```js
7+
match (x) {
8+
when ({ colour: "#000000" }) { "yes" }
9+
else {
10+
"no"
11+
}
12+
}
13+
```
14+
15+
```jsonc
16+
{
17+
"type": "MatchExpression",
18+
"discriminant": { "type": "Identifier", "name": "x" },
19+
"id": null,
20+
"clauses": [{
21+
"type": "MatchClause",
22+
"test": {
23+
"type": "ObjectMatchPattern",
24+
"properties": [{
25+
"type": "Properties",
26+
"key": { "type": "StringLiteral", "value": "colour" },
27+
"value": { "type": "StringLiteral", "value": "#000000" }
28+
}]
29+
},
30+
"consequent": {
31+
"type": "BlockStatement",
32+
"body": [{
33+
"type": "ExpressionStatement",
34+
"expression": { "type": "StringLiteral", "value": "yes" }
35+
}]
36+
},
37+
"guard": null,
38+
"id": null
39+
}, {
40+
"type": "MatchClause",
41+
"test": null,
42+
"consequent": {
43+
"type": "BlockStatement",
44+
"body": [{
45+
"type": "ExpressionStatement",
46+
"expression": { "type": "StringLiteral", "value": "no" }
47+
}]
48+
},
49+
"guard": null,
50+
"id": null
51+
}]
52+
}
53+
```
54+
55+
## 2. Matchable `as` RHS, Array Match Pattern and Null Match Pattern
56+
57+
```js
58+
match (f()) as [x, y] {
59+
when ([_, _, 200]) { "foo"; }
60+
else {
61+
process(x, y)
62+
}
63+
}
64+
```
65+
66+
```jsonc
67+
{
68+
"type": "MatchExpression",
69+
"discriminant": {
70+
"type": "CallExpression",
71+
"callee": { "type": "Identifier", "name": "f" },
72+
"arguments": []
73+
},
74+
"id": {
75+
"type": "ArrayPattern",
76+
"elements": [
77+
{ "type": "Identifier", "name": "x" },
78+
{ "type": "Identifier", "name": "y" }
79+
]
80+
},
81+
"clauses": [{
82+
"type": "MatchClause",
83+
"test": {
84+
"type": "ArrayMatchPattern",
85+
"elements": [
86+
{ "type": "NullMatchPattern" },
87+
{ "type": "NullMatchPattern" },
88+
{ "type": "Literal", "value": 20 }
89+
]
90+
},
91+
"consequent": {
92+
"type": "BlockStatement",
93+
"body": [{
94+
"type": "ExpressionStatement",
95+
"expression": { "type": "StringLiteral", "value": "foo" }
96+
}]
97+
},
98+
"guard": null,
99+
"id": null
100+
}, {
101+
"type": "MatchClause",
102+
"test": null,
103+
"consequent": {
104+
"type": "BlockStatement",
105+
"body": [{
106+
"type": "CallExpression",
107+
"callee": { "type": "Identifier", "name": "process" },
108+
"arguments": [
109+
{ "type": "Identifier", "name": "x" },
110+
{ "type": "Identifier", "name": "y" }
111+
]
112+
}]
113+
},
114+
"guard": null,
115+
"id": null
116+
}]
117+
}
118+
```
119+
120+
## 3. Match Clause "as" RHS and guard condition
121+
```js
122+
match (x) {
123+
when (/(?<x>\d+)/) as { groups: { y } } if (isEmpty(y)) {}
124+
}
125+
```
126+
127+
```jsonc
128+
{
129+
"type": "MatchExpression",
130+
"discriminant": { "type": "Identifier", "name": "x" },
131+
"id": null,
132+
"clauses": [{
133+
"type": "MatchClause",
134+
"test": {
135+
"type": "Literal",
136+
"regex": { "pattern": "(?<x>\\d+)", "flags": "" }
137+
},
138+
"consequent": {
139+
"type": "BlockStatement",
140+
"body": []
141+
},
142+
"guard": {
143+
"type": "CallExpression",
144+
"callee": { "type": "Identifier", "name": "isEmpty" },
145+
"arguments": [
146+
{ "type": "Identifier", "name": "y" }
147+
]
148+
},
149+
"id": {
150+
"type": "ObjectPattern",
151+
"properties": [{
152+
"type": "Property",
153+
"key": { "type": "Identifier", "name": "groups" },
154+
"value": {
155+
"type": "ObjectPattern",
156+
"properties": [{
157+
"type": "Property",
158+
"key": { "type": "Identifier", "name": "y" }
159+
}]
160+
}
161+
}]
162+
}
163+
}]
164+
}
165+
```
166+
167+
## 4 "if" Match Clause and Binary Match Pattern
168+
```js
169+
match (x) {
170+
if(isEmpty(x)) {}
171+
when({ status: 200 | 201 }) {}
172+
}
173+
```
174+
175+
```jsonc
176+
{
177+
"type": "MatchExpression",
178+
"discriminant": { "type": "Identifier", "name": "x" },
179+
"id": null,
180+
"clauses": [{
181+
"type": "MatchClause",
182+
"test": {
183+
"type": "Literal",
184+
"regex": { "pattern": "(?<x>\\d+)", "flags": "" }
185+
},
186+
"consequent": {
187+
"type": "BlockStatement",
188+
"body": []
189+
},
190+
"guard": {
191+
"type": "CallExpression",
192+
"callee": { "type": "Identifier", "name": "isEmpty" },
193+
"arguments": [{ "type": "Identifier", "name": "x" }]
194+
},
195+
"id": null,
196+
}, {
197+
"type": "MatchClause",
198+
"test": {
199+
"type": "ObjectMatchPattern",
200+
"properties": [{
201+
"type": "Properties",
202+
"key": { "type": "StringLiteral", "value": "status" },
203+
"value": {
204+
"type": "BinaryMatchPattern",
205+
"operator": "or",
206+
"left": { "type": "Literal", "value": 200 },
207+
"right": { "type": "Literal", "value": 201 }
208+
}
209+
}]
210+
},
211+
"consequent": {
212+
"type": "BlockStatement",
213+
"body": []
214+
},
215+
"guard": null,
216+
"id": null
217+
}]
218+
}
219+
```
220+
221+
## 5 RegExp as Match Pattern, "with" additional Match Pattern and As Match Pattern
222+
```js
223+
match (x) {
224+
when ({
225+
input: (/(?<x>\d+(?<d>[NSEW]))/ with [_, 200, _]) as { groups: { d } }
226+
}) {}
227+
}
228+
```
229+
230+
```jsonc
231+
{
232+
"type": "MatchExpression",
233+
"discriminant": { "type": "Identifier", "name": "x" },
234+
"id": null,
235+
"clauses": [{
236+
"type": "MatchClause",
237+
"test": {
238+
"type": "ObjectMatchPattern",
239+
"properties": [{
240+
"type": "Properties",
241+
"key": { "type": "StringLiteral", "value": "input" },
242+
"value": {
243+
"type": "AsMatchPattern",
244+
"test": {
245+
"type": "BinaryMatchPattern",
246+
"operator": "with",
247+
"left": {
248+
"type": "Literal",
249+
"regex": { "pattern": "(?<x>\\d+(?<d>[NSEW]))", "flags": "" }
250+
},
251+
"right": {
252+
"type": "ArrayMatchPattern",
253+
"elements": [
254+
{ "type": "NullMatchPattern" },
255+
{ "type": "Literal", "value": 20 },
256+
{ "type": "NullMatchPattern" }
257+
]
258+
}
259+
},
260+
"id": {
261+
"type": "ObjectPattern",
262+
"properties": [{
263+
"type": "Property",
264+
"key": { "type": "Identifier", "name": "groups" },
265+
"value": {
266+
"type": "ObjectPattern",
267+
"properties": [{
268+
"type": "Property",
269+
"key": { "type": "Identifier", "name": "d" }
270+
}]
271+
}
272+
}]
273+
}
274+
}
275+
}]
276+
},
277+
"consequent": {
278+
"type": "BlockStatement",
279+
"body": []
280+
},
281+
"guard": null,
282+
"id": null
283+
}]
284+
}
285+
```
286+
287+
## 6 Expression Match Pattern
288+
289+
```js
290+
match (token) {
291+
when (^LF | ^CR) {}
292+
}
293+
```
294+
295+
```jsonc
296+
{
297+
"type": "MatchExpression",
298+
"discriminant": { "type": "Identifier", "name": "x" },
299+
"id": null,
300+
"clauses": [{
301+
"type": "MatchClause",
302+
"test": {
303+
"type": "BinaryMatchPattern",
304+
"operator": "or",
305+
"left": {
306+
"type": "ExpressionMatchPattern",
307+
"expression": { "type": "Identifier", "name": "LF" }
308+
},
309+
"right": {
310+
"type": "ExpressionMatchPattern",
311+
"expression": { "type": "Identifier", "name": "CR" }
312+
}
313+
},
314+
"consequent": {
315+
"type": "BlockStatement",
316+
"body": []
317+
},
318+
"guard": null,
319+
"id": null
320+
}]
321+
}
322+
```
323+
324+
## 7. Expression Match Pattern and computed Object Match Pattern
325+
326+
```js
327+
328+
match (action) {
329+
when (["take", ...{ [computed]: -200 }]) {}
330+
}
331+
```
332+
333+
```jsonc
334+
{
335+
"type": "MatchExpression",
336+
"discriminant": { "type": "Identifier", "name": "action" },
337+
"id": null,
338+
"clauses": [{
339+
"type": "MatchClause",
340+
"test": {
341+
"type": "ArrayMatchPattern",
342+
"elements": [
343+
{ "type": "Literal", "value": "take" },
344+
{
345+
"type": "RestMatchElement",
346+
"argument": {
347+
"type": "ObjectMatchPattern",
348+
"properties": [{
349+
"type": "Property",
350+
"key": { "type": "Identifier", "name": "computed" },
351+
"computed": true,
352+
"value": {
353+
"type": "UnaryExpression",
354+
"operator": "-",
355+
"prefix": true,
356+
"argument": { "type": "Literal", "value": 200 }
357+
}
358+
}]
359+
}
360+
}]
361+
},
362+
"consequent": {
363+
"type": "BlockStatement",
364+
"body": []
365+
},
366+
"guard": null,
367+
"id": null
368+
}]
369+
}
370+
```
371+
372+
373+
[proposal-pattern-matching-estree]: https://github.com/JLHwung/estree/proposal-pattern-matching/

0 commit comments

Comments
 (0)