Skip to content

Commit

Permalink
fix: ast for offsetlookup in encapsed
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Oct 3, 2019
1 parent 74ee26f commit 7c09272
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 60 deletions.
13 changes: 6 additions & 7 deletions src/parser/scalar.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ module.exports = {
text
);
} else if (this.token === this.tok.T_DOLLAR_OPEN_CURLY_BRACES) {
syntax = 'simple';
syntax = "simple";
curly = true;
// dynamic variable name
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1239
Expand All @@ -237,24 +237,23 @@ module.exports = {
node = this.node("offsetlookup");
offset = this.next().read_expr();
this.expect("]") && this.next();
name = node(name, offset);
result = node(name, offset);
} else {
name = varName;
result = name(varName, false);
}
} else {
name = this.read_expr();
result = result("variable", this.read_expr(), false);
}
this.expect("}") && this.next();
result = result("variable", name);
} else if (this.token === this.tok.T_CURLY_OPEN) {
// expression
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1246
syntax = 'complex';
syntax = "complex";
result.destroy();
result = this.next().read_variable(false, false);
this.expect("}") && this.next();
} else if (this.token === this.tok.T_VARIABLE) {
syntax = 'simple';
syntax = "simple";
// plain variable
// https://github.com/php/php-src/blob/master/Zend/zend_language_parser.y#L1231
result.destroy();
Expand Down
2 changes: 1 addition & 1 deletion src/parser/variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ module.exports = {
what = this.node("encapsedpart");
name = this.next().read_expr();
this.expect("}") && this.next();
what = what(name, true);
what = what(name, "complex", false);
break;
default:
this.error([this.tok.T_STRING, this.tok.T_VARIABLE, "$", "{"]);
Expand Down
100 changes: 83 additions & 17 deletions test/snapshot/__snapshots__/encapsed.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,11 @@ Program {
"expression": Variable {
"curly": false,
"kind": "variable",
"name": "juice",
"name": Variable {
"curly": false,
"kind": "variable",
"name": "juice",
},
},
"kind": "encapsedpart",
"syntax": "simple",
Expand Down Expand Up @@ -244,12 +248,16 @@ Program {
EncapsedPart {
"curly": true,
"expression": Variable {
"curly": true,
"curly": false,
"kind": "variable",
"name": Variable {
"curly": false,
"curly": true,
"kind": "variable",
"name": "juice",
"name": Variable {
"curly": false,
"kind": "variable",
"name": "juice",
},
},
},
"kind": "encapsedpart",
Expand Down Expand Up @@ -300,13 +308,17 @@ Program {
},
EncapsedPart {
"curly": true,
"expression": Call {
"arguments": Array [],
"kind": "call",
"what": ClassReference {
"kind": "classreference",
"name": "call",
"resolution": "uqn",
"expression": Variable {
"curly": false,
"kind": "variable",
"name": Call {
"arguments": Array [],
"kind": "call",
"what": ClassReference {
"kind": "classreference",
"name": "call",
"resolution": "uqn",
},
},
},
"kind": "encapsedpart",
Expand Down Expand Up @@ -604,9 +616,9 @@ Program {
EncapsedPart {
"curly": true,
"expression": Variable {
"curly": "juice",
"curly": false,
"kind": "variable",
"name": "variable",
"name": "juice",
},
"kind": "encapsedpart",
"syntax": "simple",
Expand Down Expand Up @@ -1447,6 +1459,60 @@ Program {
}
`;

exports[`encapsed propertylookup 1`] = `
Program {
"children": Array [
ExpressionStatement {
"expression": PropertyLookup {
"kind": "propertylookup",
"offset": EncapsedPart {
"curly": false,
"expression": Encapsed {
"kind": "encapsed",
"raw": "\\"set{$type}\\"",
"type": "string",
"value": Array [
EncapsedPart {
"curly": false,
"expression": String {
"isDoubleQuote": false,
"kind": "string",
"raw": "set",
"unicode": false,
"value": "set",
},
"kind": "encapsedpart",
"syntax": null,
},
EncapsedPart {
"curly": false,
"expression": Variable {
"curly": false,
"kind": "variable",
"name": "type",
},
"kind": "encapsedpart",
"syntax": "complex",
},
],
},
"kind": "encapsedpart",
"syntax": "complex",
},
"what": Variable {
"curly": false,
"kind": "variable",
"name": "this",
},
},
"kind": "expressionstatement",
},
],
"errors": Array [],
"kind": "program",
}
`;

exports[`encapsed propertylookup and offsetlookup (complex syntax) 1`] = `
Program {
"children": Array [
Expand Down Expand Up @@ -1547,7 +1613,7 @@ Program {
"expression": PropertyLookup {
"kind": "propertylookup",
"offset": EncapsedPart {
"curly": null,
"curly": false,
"expression": OffsetLookup {
"kind": "offsetlookup",
"offset": Number {
Expand All @@ -1561,7 +1627,7 @@ Program {
},
},
"kind": "encapsedpart",
"syntax": true,
"syntax": "complex",
},
"what": Variable {
"curly": false,
Expand Down Expand Up @@ -2854,9 +2920,9 @@ Program {
EncapsedPart {
"curly": true,
"expression": Variable {
"curly": "var",
"curly": false,
"kind": "variable",
"name": "variable",
"name": "var",
},
"kind": "encapsedpart",
"syntax": "simple",
Expand Down
4 changes: 2 additions & 2 deletions test/snapshot/__snapshots__/propertylookup.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ Program {
"expression": PropertyLookup {
"kind": "propertylookup",
"offset": EncapsedPart {
"curly": null,
"curly": false,
"expression": ClassReference {
"kind": "classreference",
"name": "foo",
"resolution": "uqn",
},
"kind": "encapsedpart",
"syntax": true,
"syntax": "complex",
},
"what": Variable {
"curly": false,
Expand Down
54 changes: 23 additions & 31 deletions test/snapshot/__snapshots__/string.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1662,23 +1662,19 @@ Program {
},
EncapsedPart {
"curly": true,
"expression": Variable {
"curly": false,
"kind": "variable",
"name": OffsetLookup {
"kind": "offsetlookup",
"offset": String {
"isDoubleQuote": false,
"kind": "string",
"raw": "'FOO'",
"unicode": false,
"value": "FOO",
},
"what": Variable {
"curly": false,
"kind": "variable",
"name": "juices",
},
"expression": OffsetLookup {
"kind": "offsetlookup",
"offset": String {
"isDoubleQuote": false,
"kind": "string",
"raw": "'FOO'",
"unicode": false,
"value": "FOO",
},
"what": Variable {
"curly": false,
"kind": "variable",
"name": "juices",
},
},
"kind": "encapsedpart",
Expand Down Expand Up @@ -2866,20 +2862,16 @@ EOFX",
},
EncapsedPart {
"curly": true,
"expression": Variable {
"curly": false,
"kind": "variable",
"name": OffsetLookup {
"kind": "offsetlookup",
"offset": Number {
"kind": "number",
"value": "1",
},
"what": Variable {
"curly": false,
"kind": "variable",
"name": "targetDirs",
},
"expression": OffsetLookup {
"kind": "offsetlookup",
"offset": Number {
"kind": "number",
"value": "1",
},
"what": Variable {
"curly": false,
"kind": "variable",
"name": "targetDirs",
},
},
"kind": "encapsedpart",
Expand Down
4 changes: 2 additions & 2 deletions test/snapshot/__snapshots__/variable.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,7 @@ Program {
"expression": PropertyLookup {
"kind": "propertylookup",
"offset": EncapsedPart {
"curly": null,
"curly": false,
"expression": PropertyLookup {
"kind": "propertylookup",
"offset": Identifier {
Expand All @@ -1249,7 +1249,7 @@ Program {
},
},
"kind": "encapsedpart",
"syntax": true,
"syntax": "complex",
},
"what": Variable {
"curly": false,
Expand Down
3 changes: 3 additions & 0 deletions test/snapshot/encapsed.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,7 @@ describe("encapsed", function() {
it("no curly", () => {
expect(parser.parseEval('"string $$juice string";')).toMatchSnapshot();
});
it("propertylookup", () => {
expect(parser.parseEval('$this->{"set{$type}"};')).toMatchSnapshot();
});
});

0 comments on commit 7c09272

Please sign in to comment.