Skip to content

Commit 06f6d89

Browse files
refactor: read_lexical_vars
1 parent 33578a4 commit 06f6d89

File tree

3 files changed

+153
-4
lines changed

3 files changed

+153
-4
lines changed

src/parser/function.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,8 @@ module.exports = {
120120
if (this.expect("(")) this.next();
121121
const params = this.read_parameter_list();
122122
if (this.expect(")")) this.next();
123-
if (type === 1 && this.token === this.tok.T_USE) {
124-
if (this.next().expect("(")) this.next();
125-
use = this.read_list(this.read_lexical_var, ",");
126-
if (this.expect(")")) this.next();
123+
if (type === 1) {
124+
use = this.read_lexical_vars();
127125
}
128126
if (this.token === ":") {
129127
if (this.next().token === "?") {
@@ -138,6 +136,24 @@ module.exports = {
138136
}
139137
return result(name, params, isRef, returnType, nullable);
140138
},
139+
140+
read_lexical_vars: function() {
141+
let result = [];
142+
143+
if (this.token === this.tok.T_USE) {
144+
this.next();
145+
this.expect("(") && this.next();
146+
result = this.read_lexical_var_list();
147+
this.expect(")") && this.next();
148+
}
149+
150+
return result;
151+
},
152+
153+
read_lexical_var_list: function() {
154+
return this.read_list(this.read_lexical_var, ",");
155+
},
156+
141157
/**
142158
* ```ebnf
143159
* lexical_var ::= '&'? T_VARIABLE

test/snapshot/__snapshots__/closure.test.js.snap

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,63 @@ Program {
409409
}
410410
`;
411411

412+
exports[`closure inside call 1`] = `
413+
Program {
414+
"children": Array [
415+
ExpressionStatement {
416+
"expression": Call {
417+
"arguments": Array [
418+
Closure {
419+
"arguments": Array [
420+
Parameter {
421+
"byref": false,
422+
"kind": "parameter",
423+
"name": Identifier {
424+
"kind": "identifier",
425+
"name": "arg",
426+
},
427+
"nullable": false,
428+
"type": null,
429+
"value": null,
430+
"variadic": false,
431+
},
432+
],
433+
"body": Block {
434+
"children": Array [
435+
Return {
436+
"expr": Variable {
437+
"curly": false,
438+
"kind": "variable",
439+
"name": "arg",
440+
},
441+
"kind": "return",
442+
},
443+
],
444+
"kind": "block",
445+
},
446+
"byref": false,
447+
"isStatic": false,
448+
"kind": "closure",
449+
"nullable": false,
450+
"type": null,
451+
"uses": Array [],
452+
},
453+
],
454+
"kind": "call",
455+
"what": ClassReference {
456+
"kind": "classreference",
457+
"name": "call",
458+
"resolution": "uqn",
459+
},
460+
},
461+
"kind": "expressionstatement",
462+
},
463+
],
464+
"errors": Array [],
465+
"kind": "program",
466+
}
467+
`;
468+
412469
exports[`closure simple 1`] = `
413470
Program {
414471
"children": Array [
@@ -565,3 +622,67 @@ Program {
565622
"kind": "program",
566623
}
567624
`;
625+
626+
exports[`closure use multiple 1`] = `
627+
Program {
628+
"children": Array [
629+
ExpressionStatement {
630+
"expression": Assign {
631+
"kind": "assign",
632+
"left": Variable {
633+
"curly": false,
634+
"kind": "variable",
635+
"name": "var",
636+
},
637+
"operator": "=",
638+
"right": Closure {
639+
"arguments": Array [],
640+
"body": Block {
641+
"children": Array [
642+
Echo {
643+
"expressions": Array [
644+
String {
645+
"isDoubleQuote": true,
646+
"kind": "string",
647+
"raw": "\\"something\\"",
648+
"unicode": false,
649+
"value": "something",
650+
},
651+
],
652+
"kind": "echo",
653+
"shortForm": false,
654+
},
655+
],
656+
"kind": "block",
657+
},
658+
"byref": false,
659+
"isStatic": false,
660+
"kind": "closure",
661+
"nullable": false,
662+
"type": null,
663+
"uses": Array [
664+
Variable {
665+
"curly": false,
666+
"kind": "variable",
667+
"name": "message",
668+
},
669+
Variable {
670+
"curly": false,
671+
"kind": "variable",
672+
"name": "message1",
673+
},
674+
Variable {
675+
"curly": false,
676+
"kind": "variable",
677+
"name": "message2",
678+
},
679+
],
680+
},
681+
},
682+
"kind": "expressionstatement",
683+
},
684+
],
685+
"errors": Array [],
686+
"kind": "program",
687+
}
688+
`;

test/snapshot/closure.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ describe("closure", () => {
3333
)
3434
).toMatchSnapshot();
3535
});
36+
it("use multiple", () => {
37+
expect(
38+
parser.parseEval(
39+
'$var = function() use ($message, $message1, $message2) { echo "something"; };'
40+
)
41+
).toMatchSnapshot();
42+
});
3643
it("use by ref", () => {
3744
expect(
3845
parser.parseEval(
@@ -54,4 +61,9 @@ describe("closure", () => {
5461
)
5562
).toMatchSnapshot();
5663
});
64+
it("inside call", () => {
65+
expect(
66+
parser.parseEval(`call(function ($arg) { return $arg; });`)
67+
).toMatchSnapshot();
68+
});
5769
});

0 commit comments

Comments
 (0)