Skip to content

Commit

Permalink
refactor: reset state instead of try / catch
Browse files Browse the repository at this point in the history
  • Loading branch information
czosel committed Mar 27, 2021
1 parent 6b2a654 commit 39b527c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/parser/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,20 +290,20 @@ module.exports = {
if (this.token === this.tok.T_ELLIPSIS) {
return this.node("variadic")(this.next().read_expr());
}
const name = this.text();
let res;
try {
res = this.read_expr();
} catch (e) {
// happens if named argument name equals keyword name, e.g.
// foo(array: $a) - `array` is not a valid expr
this.expect(":") && this.next();
return this.node("namedargument")(name, this.read_expr());
}
if (this.token === ":") {
return this.node("namedargument")(name, this.next().read_expr());
if (
this.token === this.tok.T_STRING ||
Object.values(this.lexer.keywords).includes(this.token)
) {
const backup = [this.token, this.lexer.getState()];
const name = this.text();
this.next();
if (this.token === ":") {
return this.node("namedargument")(name, this.next().read_expr());
}
this.lexer.tokens.push(backup);
this.next();
}
return res;
return this.read_expr();
},
/**
* read type hinting
Expand Down
14 changes: 14 additions & 0 deletions test/snapshot/__snapshots__/call.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ Program {
},
],
"kind": "array",
"leadingComments": Array [
CommentLine {
"kind": "commentline",
"offset": 10,
"value": "// comment
",
},
],
"shortForm": false,
},
],
Expand All @@ -153,6 +161,12 @@ Program {
"kind": "commentline",
"offset": 10,
"value": "// comment
",
},
CommentLine {
"kind": "commentline",
"offset": 10,
"value": "// comment
",
},
],
Expand Down

0 comments on commit 39b527c

Please sign in to comment.