Skip to content

Commit d27e051

Browse files
committed
support throw as expression in match statement
1 parent 6e91308 commit d27e051

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

src/parser/expr.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,14 @@ module.exports = {
346346
case this.tok.T_UNSET_CAST:
347347
return this.read_expr_cast("unset");
348348

349+
case this.tok.T_THROW: {
350+
if (this.version < 800) {
351+
this.raiseError("PHP 8+ is required to use throw as an expression");
352+
}
353+
const result = this.node("throw");
354+
const expr = this.next().read_expr();
355+
return result(expr);
356+
}
349357
case this.tok.T_EXIT: {
350358
const useDie = this.lexer.yytext.toLowerCase() === "die";
351359
result = this.node("exit");

test/snapshot/__snapshots__/match.test.js.snap

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,25 @@ Program {
258258
"kind": "matcharm",
259259
},
260260
MatchArm {
261-
"body": String {
262-
"isDoubleQuote": false,
263-
"kind": "string",
264-
"raw": "'Nope!'",
265-
"unicode": false,
266-
"value": "Nope!",
261+
"body": Throw {
262+
"kind": "throw",
263+
"what": New {
264+
"arguments": Array [
265+
String {
266+
"isDoubleQuote": false,
267+
"kind": "string",
268+
"raw": "'Nope'",
269+
"unicode": false,
270+
"value": "Nope",
271+
},
272+
],
273+
"kind": "new",
274+
"what": Name {
275+
"kind": "name",
276+
"name": "Exception",
277+
"resolution": "uqn",
278+
},
279+
},
267280
},
268281
"conds": Array [
269282
Boolean {

test/snapshot/match.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe("match", () => {
3535
const ast = parser.parseEval(`
3636
$test = match($test) {
3737
true, => 'ok',
38-
false => 'Nope!',
38+
false => throw new Exception('Nope'),
3939
};
4040
`);
4141
expect(ast).toMatchSnapshot();

0 commit comments

Comments
 (0)