Skip to content

Commit

Permalink
support throw as expression in match statement
Browse files Browse the repository at this point in the history
  • Loading branch information
cseufert committed Mar 29, 2021
1 parent 6e91308 commit d27e051
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/parser/expr.js
Expand Up @@ -346,6 +346,14 @@ module.exports = {
case this.tok.T_UNSET_CAST:
return this.read_expr_cast("unset");

case this.tok.T_THROW: {
if (this.version < 800) {
this.raiseError("PHP 8+ is required to use throw as an expression");
}
const result = this.node("throw");
const expr = this.next().read_expr();
return result(expr);
}
case this.tok.T_EXIT: {
const useDie = this.lexer.yytext.toLowerCase() === "die";
result = this.node("exit");
Expand Down
25 changes: 19 additions & 6 deletions test/snapshot/__snapshots__/match.test.js.snap
Expand Up @@ -258,12 +258,25 @@ Program {
"kind": "matcharm",
},
MatchArm {
"body": String {
"isDoubleQuote": false,
"kind": "string",
"raw": "'Nope!'",
"unicode": false,
"value": "Nope!",
"body": Throw {
"kind": "throw",
"what": New {
"arguments": Array [
String {
"isDoubleQuote": false,
"kind": "string",
"raw": "'Nope'",
"unicode": false,
"value": "Nope",
},
],
"kind": "new",
"what": Name {
"kind": "name",
"name": "Exception",
"resolution": "uqn",
},
},
},
"conds": Array [
Boolean {
Expand Down
2 changes: 1 addition & 1 deletion test/snapshot/match.test.js
Expand Up @@ -35,7 +35,7 @@ describe("match", () => {
const ast = parser.parseEval(`
$test = match($test) {
true, => 'ok',
false => 'Nope!',
false => throw new Exception('Nope'),
};
`);
expect(ast).toMatchSnapshot();
Expand Down

0 comments on commit d27e051

Please sign in to comment.