Skip to content

Commit

Permalink
feat: parse pin-operator
Browse files Browse the repository at this point in the history
  • Loading branch information
fedeci committed Sep 3, 2021
1 parent 04cb052 commit bc04533
Show file tree
Hide file tree
Showing 8 changed files with 377 additions and 7 deletions.
26 changes: 23 additions & 3 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -3015,9 +3015,13 @@ export default class ExpressionParser extends LValParser {
parseWhenMatchClause(): N.MatchClause {
const node = this.startNode<N.MatchClause>();
this.next(); // skip "when"
this.expect(tt.parenL);
node.test = this.parseMaybeBinaryMatchPattern();
this.expect(tt.parenR);
if (this.match(tt.bitwiseXOR)) {
node.test = this.parseExpressionMatchPattern();
} else {
this.expect(tt.parenL);
node.test = this.parseMaybeBinaryMatchPattern();
this.expect(tt.parenR);
}

if (this.isLineTerminator()) {
this.unexpected(
Expand Down Expand Up @@ -3055,11 +3059,27 @@ export default class ExpressionParser extends LValParser {
return this.parseArrayMatchPattern();
case tt.plusMin:
return this.parseSimpleUnaryExpression();
case tt.bitwiseXOR:
return this.parseExpressionMatchPattern();
default:
throw this.unexpected();
}
}

parseExpressionMatchPattern(): N.ExpressionMatchPattern {
const node = this.startNode<N.ExpressionMatchPattern>();
this.next(); // skip ^
let expression: N.Expression;
if (this.match(tt.parenL)) {
expression = this.parseParenAndDistinguishExpression(false);
} else {
expression = this.parseExprSubscripts();
}

node.expression = expression;
return this.finishNode(node, "ExpressionMatchPattern");
}

parseSimpleUnaryExpression(): N.UnaryExpression {
const node = this.startNode<N.UnaryExpression>();
node.operator = this.state.value;
Expand Down

This file was deleted.

@@ -0,0 +1,3 @@
match (foo) {
when ^foo + 1 {}
}
Expand Up @@ -4,5 +4,5 @@
"patternMatching"
]
],
"throws": "Unexpected token (2:8)"
"throws": "Unexpected token (2:16)"
}
@@ -0,0 +1,4 @@
match (foo) {
when ^(foo + 1) {}
when (^(LF) | ^(CR)) {}
}
@@ -0,0 +1,106 @@
{
"type": "File",
"start":0,"end":62,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"program": {
"type": "Program",
"start":0,"end":62,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":62,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"expression": {
"type": "MatchExpression",
"start":0,"end":62,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"discriminant": {
"type": "Identifier",
"start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10},"identifierName":"foo"},
"name": "foo"
},
"clauses": [
{
"type": "MatchClause",
"start":16,"end":34,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":20}},
"test": {
"type": "ExpressionMatchPattern",
"start":21,"end":31,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":17}},
"expression": {
"type": "BinaryExpression",
"start":23,"end":30,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":16}},
"extra": {
"parenthesized": true,
"parenStart": 22
},
"left": {
"type": "Identifier",
"start":23,"end":26,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":12},"identifierName":"foo"},
"name": "foo"
},
"operator": "+",
"right": {
"type": "NumericLiteral",
"start":29,"end":30,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":16}},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
}
},
"consequent": {
"type": "BlockStatement",
"start":32,"end":34,"loc":{"start":{"line":2,"column":18},"end":{"line":2,"column":20}},
"body": [],
"directives": []
}
},
{
"type": "MatchClause",
"start":37,"end":60,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":25}},
"test": {
"type": "BinaryMatchPattern",
"start":43,"end":56,"loc":{"start":{"line":3,"column":8},"end":{"line":3,"column":21}},
"left": {
"type": "ExpressionMatchPattern",
"start":43,"end":48,"loc":{"start":{"line":3,"column":8},"end":{"line":3,"column":13}},
"expression": {
"type": "Identifier",
"start":45,"end":47,"loc":{"start":{"line":3,"column":10},"end":{"line":3,"column":12},"identifierName":"LF"},
"extra": {
"parenthesized": true,
"parenStart": 44
},
"name": "LF"
}
},
"operator": "or",
"right": {
"type": "ExpressionMatchPattern",
"start":51,"end":56,"loc":{"start":{"line":3,"column":16},"end":{"line":3,"column":21}},
"expression": {
"type": "Identifier",
"start":53,"end":55,"loc":{"start":{"line":3,"column":18},"end":{"line":3,"column":20},"identifierName":"CR"},
"extra": {
"parenthesized": true,
"parenStart": 52
},
"name": "CR"
}
}
},
"consequent": {
"type": "BlockStatement",
"start":58,"end":60,"loc":{"start":{"line":3,"column":23},"end":{"line":3,"column":25}},
"body": [],
"directives": []
}
}
]
}
}
],
"directives": []
}
}
@@ -0,0 +1,8 @@
match (token) {
when ^foo {}
when ^foo.bar.zoo {}
when ^baz.bar() {}
when ^baz?.bar() {}
when ^baz["foo"].bar {}
when (^LF | ^CR) {}
}

0 comments on commit bc04533

Please sign in to comment.