-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
385 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,373 @@ | ||
# AST examples of [Pattern Matching ESTree Proposal](proposal-pattern-matching-estree) | ||
|
||
Note: The `ExpressionStatement` and `Program` is omitted in the examples. | ||
|
||
## 1. Object Match Pattern and "else" Match Clause | ||
```js | ||
match (x) { | ||
when ({ colour: "#000000" }) { "yes" } | ||
else { | ||
"no" | ||
} | ||
} | ||
``` | ||
|
||
```jsonc | ||
{ | ||
"type": "MatchExpression", | ||
"discriminant": { "type": "Identifier", "name": "x" }, | ||
"id": null, | ||
"clauses": [{ | ||
"type": "MatchClause", | ||
"test": { | ||
"type": "ObjectMatchPattern", | ||
"properties": [{ | ||
"type": "Properties", | ||
"key": { "type": "StringLiteral", "value": "colour" }, | ||
"value": { "type": "StringLiteral", "value": "#000000" } | ||
}] | ||
}, | ||
"consequent": { | ||
"type": "BlockStatement", | ||
"body": [{ | ||
"type": "ExpressionStatement", | ||
"expression": { "type": "StringLiteral", "value": "yes" } | ||
}] | ||
}, | ||
"guard": null, | ||
"id": null | ||
}, { | ||
"type": "MatchClause", | ||
"test": null, | ||
"consequent": { | ||
"type": "BlockStatement", | ||
"body": [{ | ||
"type": "ExpressionStatement", | ||
"expression": { "type": "StringLiteral", "value": "no" } | ||
}] | ||
}, | ||
"guard": null, | ||
"id": null | ||
}] | ||
} | ||
``` | ||
|
||
## 2. Matchable `as` RHS, Array Match Pattern and Null Match Pattern | ||
|
||
```js | ||
match (f()) as [x, y] { | ||
when ([_, _, 200]) { "foo"; } | ||
else { | ||
process(x, y) | ||
} | ||
} | ||
``` | ||
|
||
```jsonc | ||
{ | ||
"type": "MatchExpression", | ||
"discriminant": { | ||
"type": "CallExpression", | ||
"callee": { "type": "Identifier", "name": "f" }, | ||
"arguments": [] | ||
}, | ||
"id": { | ||
"type": "ArrayPattern", | ||
"elements": [ | ||
{ "type": "Identifier", "name": "x" }, | ||
{ "type": "Identifier", "name": "y" } | ||
] | ||
}, | ||
"clauses": [{ | ||
"type": "MatchClause", | ||
"test": { | ||
"type": "ArrayMatchPattern", | ||
"elements": [ | ||
{ "type": "NullMatchPattern" }, | ||
{ "type": "NullMatchPattern" }, | ||
{ "type": "Literal", "value": 20 } | ||
] | ||
}, | ||
"consequent": { | ||
"type": "BlockStatement", | ||
"body": [{ | ||
"type": "ExpressionStatement", | ||
"expression": { "type": "StringLiteral", "value": "foo" } | ||
}] | ||
}, | ||
"guard": null, | ||
"id": null | ||
}, { | ||
"type": "MatchClause", | ||
"test": null, | ||
"consequent": { | ||
"type": "BlockStatement", | ||
"body": [{ | ||
"type": "CallExpression", | ||
"callee": { "type": "Identifier", "name": "process" }, | ||
"arguments": [ | ||
{ "type": "Identifier", "name": "x" }, | ||
{ "type": "Identifier", "name": "y" } | ||
] | ||
}] | ||
}, | ||
"guard": null, | ||
"id": null | ||
}] | ||
} | ||
``` | ||
|
||
## 3. Match Clause "as" RHS and guard condition | ||
```js | ||
match (x) { | ||
when (/(?<x>\d+)/) as { groups: { y } } if (isEmpty(y)) {} | ||
} | ||
``` | ||
|
||
```jsonc | ||
{ | ||
"type": "MatchExpression", | ||
"discriminant": { "type": "Identifier", "name": "x" }, | ||
"id": null, | ||
"clauses": [{ | ||
"type": "MatchClause", | ||
"test": { | ||
"type": "Literal", | ||
"regex": { "pattern": "(?<x>\\d+)", "flags": "" } | ||
}, | ||
"consequent": { | ||
"type": "BlockStatement", | ||
"body": [] | ||
}, | ||
"guard": { | ||
"type": "CallExpression", | ||
"callee": { "type": "Identifier", "name": "isEmpty" }, | ||
"arguments": [ | ||
{ "type": "Identifier", "name": "y" } | ||
] | ||
}, | ||
"id": { | ||
"type": "ObjectPattern", | ||
"properties": [{ | ||
"type": "Property", | ||
"key": { "type": "Identifier", "name": "groups" }, | ||
"value": { | ||
"type": "ObjectPattern", | ||
"properties": [{ | ||
"type": "Property", | ||
"key": { "type": "Identifier", "name": "y" } | ||
}] | ||
} | ||
}] | ||
} | ||
}] | ||
} | ||
``` | ||
|
||
## 4 "if" Match Clause and Binary Match Pattern | ||
```js | ||
match (x) { | ||
if(isEmpty(x)) {} | ||
when({ status: 200 | 201 }) {} | ||
} | ||
``` | ||
|
||
```jsonc | ||
{ | ||
"type": "MatchExpression", | ||
"discriminant": { "type": "Identifier", "name": "x" }, | ||
"id": null, | ||
"clauses": [{ | ||
"type": "MatchClause", | ||
"test": { | ||
"type": "Literal", | ||
"regex": { "pattern": "(?<x>\\d+)", "flags": "" } | ||
}, | ||
"consequent": { | ||
"type": "BlockStatement", | ||
"body": [] | ||
}, | ||
"guard": { | ||
"type": "CallExpression", | ||
"callee": { "type": "Identifier", "name": "isEmpty" }, | ||
"arguments": [{ "type": "Identifier", "name": "x" }] | ||
}, | ||
"id": null, | ||
}, { | ||
"type": "MatchClause", | ||
"test": { | ||
"type": "ObjectMatchPattern", | ||
"properties": [{ | ||
"type": "Properties", | ||
"key": { "type": "StringLiteral", "value": "status" }, | ||
"value": { | ||
"type": "BinaryMatchPattern", | ||
"operator": "or", | ||
"left": { "type": "Literal", "value": 200 }, | ||
"right": { "type": "Literal", "value": 201 } | ||
} | ||
}] | ||
}, | ||
"consequent": { | ||
"type": "BlockStatement", | ||
"body": [] | ||
}, | ||
"guard": null, | ||
"id": null | ||
}] | ||
} | ||
``` | ||
|
||
## 5 RegExp as Match Pattern, "with" additional Match Pattern and As Match Pattern | ||
```js | ||
match (x) { | ||
when ({ | ||
input: (/(?<x>\d+(?<d>[NSEW]))/ with [_, 200, _]) as { groups: { d } } | ||
}) {} | ||
} | ||
``` | ||
|
||
```jsonc | ||
{ | ||
"type": "MatchExpression", | ||
"discriminant": { "type": "Identifier", "name": "x" }, | ||
"id": null, | ||
"clauses": [{ | ||
"type": "MatchClause", | ||
"test": { | ||
"type": "ObjectMatchPattern", | ||
"properties": [{ | ||
"type": "Properties", | ||
"key": { "type": "StringLiteral", "value": "input" }, | ||
"value": { | ||
"type": "AsMatchPattern", | ||
"test": { | ||
"type": "BinaryMatchPattern", | ||
"operator": "with", | ||
"left": { | ||
"type": "Literal", | ||
"regex": { "pattern": "(?<x>\\d+(?<d>[NSEW]))", "flags": "" } | ||
}, | ||
"right": { | ||
"type": "ArrayMatchPattern", | ||
"elements": [ | ||
{ "type": "NullMatchPattern" }, | ||
{ "type": "Literal", "value": 20 }, | ||
{ "type": "NullMatchPattern" } | ||
] | ||
} | ||
}, | ||
"id": { | ||
"type": "ObjectPattern", | ||
"properties": [{ | ||
"type": "Property", | ||
"key": { "type": "Identifier", "name": "groups" }, | ||
"value": { | ||
"type": "ObjectPattern", | ||
"properties": [{ | ||
"type": "Property", | ||
"key": { "type": "Identifier", "name": "d" } | ||
}] | ||
} | ||
}] | ||
} | ||
} | ||
}] | ||
}, | ||
"consequent": { | ||
"type": "BlockStatement", | ||
"body": [] | ||
}, | ||
"guard": null, | ||
"id": null | ||
}] | ||
} | ||
``` | ||
|
||
## 6 Expression Match Pattern | ||
|
||
```js | ||
match (token) { | ||
when (^LF | ^CR) {} | ||
} | ||
``` | ||
|
||
```jsonc | ||
{ | ||
"type": "MatchExpression", | ||
"discriminant": { "type": "Identifier", "name": "x" }, | ||
"id": null, | ||
"clauses": [{ | ||
"type": "MatchClause", | ||
"test": { | ||
"type": "BinaryMatchPattern", | ||
"operator": "or", | ||
"left": { | ||
"type": "ExpressionMatchPattern", | ||
"expression": { "type": "Identifier", "name": "LF" } | ||
}, | ||
"right": { | ||
"type": "ExpressionMatchPattern", | ||
"expression": { "type": "Identifier", "name": "CR" } | ||
} | ||
}, | ||
"consequent": { | ||
"type": "BlockStatement", | ||
"body": [] | ||
}, | ||
"guard": null, | ||
"id": null | ||
}] | ||
} | ||
``` | ||
|
||
## 7. Expression Match Pattern and computed Object Match Pattern | ||
|
||
```js | ||
|
||
match (action) { | ||
when (["take", ...{ [computed]: -200 }]) {} | ||
} | ||
``` | ||
|
||
```jsonc | ||
{ | ||
"type": "MatchExpression", | ||
"discriminant": { "type": "Identifier", "name": "action" }, | ||
"id": null, | ||
"clauses": [{ | ||
"type": "MatchClause", | ||
"test": { | ||
"type": "ArrayMatchPattern", | ||
"elements": [ | ||
{ "type": "Literal", "value": "take" }, | ||
{ | ||
"type": "RestMatchElement", | ||
"argument": { | ||
"type": "ObjectMatchPattern", | ||
"properties": [{ | ||
"type": "Property", | ||
"key": { "type": "Identifier", "name": "computed" }, | ||
"computed": true, | ||
"value": { | ||
"type": "UnaryExpression", | ||
"operator": "-", | ||
"prefix": true, | ||
"argument": { "type": "Literal", "value": 200 } | ||
} | ||
}] | ||
} | ||
}] | ||
}, | ||
"consequent": { | ||
"type": "BlockStatement", | ||
"body": [] | ||
}, | ||
"guard": null, | ||
"id": null | ||
}] | ||
} | ||
``` | ||
|
||
|
||
[proposal-pattern-matching-estree]: https://github.com/JLHwung/estree/proposal-pattern-matching/ |
Oops, something went wrong.