Skip to content

Commit a19aa8b

Browse files
committed
Match statements
commit a6224c4 Author: William C. Johnson <[email protected]> Date: Wed May 17 01:00:30 2017 -0400 Revert "1.0.3" This reverts commit 084045a. commit 084045a Author: William C. Johnson <[email protected]> Date: Wed May 17 00:58:45 2017 -0400 1.0.3 commit f2572dd Author: William C. Johnson <[email protected]> Date: Tue May 16 22:34:24 2017 -0400 Parse consequent bindings commit 1a73a60 Author: William C. Johnson <[email protected]> Date: Tue May 16 21:51:45 2017 -0400 Update fixtures commit 586bf27 Author: William C. Johnson <[email protected]> Date: Tue May 16 21:50:12 2017 -0400 Enable parsing for `MatchStatement`s
1 parent 88f826b commit a19aa8b

File tree

32 files changed

+5276
-5093
lines changed

32 files changed

+5276
-5093
lines changed

src/parser/expression.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ pp.parseExprAtom = function (refShorthandDefaultPos) {
730730

731731
case tt._match:
732732
if (this.hasPlugin("lightscript")) {
733-
return this.parseMatch();
733+
return this.parseMatchExpression();
734734
}
735735

736736
case tt.arrow:

src/parser/statement.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ pp.parseStatement = function (declaration, topLevel) {
116116
}
117117
return starttype === tt._import ? this.parseImport(node) : this.parseExport(node);
118118

119+
case tt._match:
120+
if (this.hasPlugin("lightscript")) return this.parseMatchStatement(node);
121+
119122
case tt.name:
120123
if (this.state.value === "async") {
121124
// peek ahead and see if next token is a function

src/plugins/lightscript.js

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -620,8 +620,15 @@ pp.existentialToParameter = function(node) {
620620
return node.argument;
621621
};
622622

623-
pp.parseMatch = function () {
624-
const node = this.startNode();
623+
pp.parseMatchStatement = function(node) {
624+
return this.parseMatch(node);
625+
};
626+
627+
pp.parseMatchExpression = function() {
628+
return this.parseMatch(this.startNode(), true);
629+
};
630+
631+
pp.parseMatch = function (node, isExpression) {
625632
this.expect(tt._match);
626633
node.discriminant = this.parseParenExpression();
627634

@@ -649,34 +656,57 @@ pp.parseMatch = function () {
649656
node.cases.push(matchCase);
650657
}
651658

652-
return this.finishNode(node, "MatchExpression");
659+
return this.finishNode(node, isExpression ? "MatchExpression" : "MatchStatement");
660+
};
661+
662+
pp.parseMatchCaseConsequent = function(matchNode) {
663+
// c/p parseIf
664+
if (this.match(tt.braceL)) {
665+
matchNode.consequent = this.parseBlock(false, true);
666+
} else {
667+
matchNode.consequent = this.parseWhiteBlock(true);
668+
}
669+
};
670+
671+
pp.parseMatchCaseWithConsequent = function(matchNode) {
672+
// with (x) -> ..., with async, with ->
673+
if (this.match(tt.parenL) || this.match(tt.arrow) || this.isContextual("async")) {
674+
const consequent = this.parseMaybeAssign();
675+
if (consequent.type !== "ArrowFunctionExpression") {
676+
this.unexpected(consequent.start, tt.arrow);
677+
}
678+
matchNode.consequent = consequent;
679+
matchNode.functional = true;
680+
return;
681+
}
682+
683+
// with <Identifier> -> ...
684+
// with <BindingAtom>: ...
685+
const node = this.startNode();
686+
const bindingAtom = this.parseBindingAtom();
687+
if (this.match(tt.arrow)) {
688+
matchNode.consequent = this.parseArrowExpression(node, bindingAtom ? [bindingAtom] : [], false);
689+
matchNode.functional = true;
690+
return;
691+
}
692+
693+
matchNode.binding = bindingAtom;
694+
this.parseMatchCaseConsequent(matchNode);
653695
};
654696

655697
pp.parseMatchCase = function () {
656698
const node = this.startNode();
657699

658700
node.test = this.parseMatchCaseTest();
659701

702+
const oldInMatchCaseConsequent = this.state.inMatchCaseConsequent;
703+
this.state.inMatchCaseConsequent = true;
660704
if (this.eat(tt._with)) {
661-
const oldInMatchCaseConsequent = this.state.inMatchCaseConsequent;
662-
this.state.inMatchCaseConsequent = true;
663-
node.consequent = this.parseMaybeAssign();
664-
this.state.inMatchCaseConsequent = oldInMatchCaseConsequent;
665-
if (node.consequent.type !== "ArrowFunctionExpression") {
666-
this.unexpected(node.consequent.start, tt.arrow);
667-
}
668-
node.functional = true;
705+
this.parseMatchCaseWithConsequent(node);
669706
} else {
670-
const oldInMatchCaseConsequent = this.state.inMatchCaseConsequent;
671-
this.state.inMatchCaseConsequent = true;
672-
// c/p parseIf
673-
if (this.match(tt.braceL)) {
674-
node.consequent = this.parseBlock(false, true);
675-
} else {
676-
node.consequent = this.parseWhiteBlock(true);
677-
}
678-
this.state.inMatchCaseConsequent = oldInMatchCaseConsequent;
707+
this.parseMatchCaseConsequent(node);
679708
}
709+
this.state.inMatchCaseConsequent = oldInMatchCaseConsequent;
680710

681711
return this.finishNode(node, "MatchCase");
682712
};

0 commit comments

Comments
 (0)