Skip to content

Commit 425c4dc

Browse files
authored
Merge pull request glayzzle#755 from Eldair/php8
PHP8 non-capturing exception
2 parents bb89fff + 7f99d0d commit 425c4dc

File tree

4 files changed

+143
-3
lines changed

4 files changed

+143
-3
lines changed

src/ast/catch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const KIND = "catch";
1313
* @constructor Catch
1414
* @extends {Statement}
1515
* @property {Identifier[]} what
16-
* @property {Variable} variable
16+
* @property {Variable|null} variable
1717
* @property {Statement} body
1818
* @see http://php.net/manual/en/language.exceptions.php
1919
*/

src/parser/try.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = {
1010
* ```ebnf
1111
* try ::= T_TRY '{' inner_statement* '}'
1212
* (
13-
* T_CATCH '(' namespace_name variable ')' '{' inner_statement* '}'
13+
* T_CATCH '(' namespace_name (variable)? ')' '{' inner_statement* '}'
1414
* )*
1515
* (T_FINALLY '{' inner_statement* '}')?
1616
* ```
@@ -28,7 +28,10 @@ module.exports = {
2828
const item = this.node("catch");
2929
this.next().expect("(") && this.next();
3030
const what = this.read_list(this.read_namespace_name, "|", false);
31-
const variable = this.read_variable(true, false);
31+
let variable = null;
32+
if (this.token === this.tok.T_VARIABLE) {
33+
variable = this.read_variable(true, false);
34+
}
3235
this.expect(")");
3336
catches.push(item(this.next().read_statement(), what, variable));
3437
}

test/snapshot/__snapshots__/try.test.js.snap

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,71 @@ Program {
361361
}
362362
`;
363363

364+
exports[`boolean multiple catch without variable 1`] = `
365+
Program {
366+
"children": Array [
367+
Try {
368+
"always": null,
369+
"body": Block {
370+
"children": Array [
371+
ExpressionStatement {
372+
"expression": Call {
373+
"arguments": Array [],
374+
"kind": "call",
375+
"what": Name {
376+
"kind": "name",
377+
"name": "call",
378+
"resolution": "uqn",
379+
},
380+
},
381+
"kind": "expressionstatement",
382+
},
383+
],
384+
"kind": "block",
385+
},
386+
"catches": Array [
387+
Catch {
388+
"body": Block {
389+
"children": Array [
390+
ExpressionStatement {
391+
"expression": Call {
392+
"arguments": Array [],
393+
"kind": "call",
394+
"what": Name {
395+
"kind": "name",
396+
"name": "do_something",
397+
"resolution": "uqn",
398+
},
399+
},
400+
"kind": "expressionstatement",
401+
},
402+
],
403+
"kind": "block",
404+
},
405+
"kind": "catch",
406+
"variable": null,
407+
"what": Array [
408+
Name {
409+
"kind": "name",
410+
"name": "MyException",
411+
"resolution": "uqn",
412+
},
413+
Name {
414+
"kind": "name",
415+
"name": "MyOtherException",
416+
"resolution": "uqn",
417+
},
418+
],
419+
},
420+
],
421+
"kind": "try",
422+
},
423+
],
424+
"errors": Array [],
425+
"kind": "program",
426+
}
427+
`;
428+
364429
exports[`boolean qualified name 1`] = `
365430
Program {
366431
"children": Array [
@@ -552,3 +617,63 @@ Program {
552617
"kind": "program",
553618
}
554619
`;
620+
621+
exports[`boolean without variable 1`] = `
622+
Program {
623+
"children": Array [
624+
Try {
625+
"always": null,
626+
"body": Block {
627+
"children": Array [
628+
ExpressionStatement {
629+
"expression": Call {
630+
"arguments": Array [],
631+
"kind": "call",
632+
"what": Name {
633+
"kind": "name",
634+
"name": "call",
635+
"resolution": "uqn",
636+
},
637+
},
638+
"kind": "expressionstatement",
639+
},
640+
],
641+
"kind": "block",
642+
},
643+
"catches": Array [
644+
Catch {
645+
"body": Block {
646+
"children": Array [
647+
ExpressionStatement {
648+
"expression": Call {
649+
"arguments": Array [],
650+
"kind": "call",
651+
"what": Name {
652+
"kind": "name",
653+
"name": "do_something",
654+
"resolution": "uqn",
655+
},
656+
},
657+
"kind": "expressionstatement",
658+
},
659+
],
660+
"kind": "block",
661+
},
662+
"kind": "catch",
663+
"variable": null,
664+
"what": Array [
665+
Name {
666+
"kind": "name",
667+
"name": "Exception",
668+
"resolution": "uqn",
669+
},
670+
],
671+
},
672+
],
673+
"kind": "try",
674+
},
675+
],
676+
"errors": Array [],
677+
"kind": "program",
678+
}
679+
`;

test/snapshot/try.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ describe("boolean", () => {
88
)
99
).toMatchSnapshot();
1010
});
11+
it("without variable", () => {
12+
expect(
13+
parser.parseEval("try { call(); } catch (Exception) { do_something(); }")
14+
).toMatchSnapshot();
15+
});
1116
it("qualified name", () => {
1217
expect(
1318
parser.parseEval(
@@ -51,6 +56,13 @@ describe("boolean", () => {
5156
)
5257
).toMatchSnapshot();
5358
});
59+
it("multiple catch without variable", () => {
60+
expect(
61+
parser.parseEval(
62+
"try { call(); } catch (MyException | MyOtherException) { do_something(); }"
63+
)
64+
).toMatchSnapshot();
65+
});
5466
it("multiple catch #2", () => {
5567
expect(
5668
parser.parseEval(

0 commit comments

Comments
 (0)