diff --git a/src/parser/scalar.js b/src/parser/scalar.js index 97f84babc..d68229df7 100644 --- a/src/parser/scalar.js +++ b/src/parser/scalar.js @@ -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 @@ -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(); diff --git a/src/parser/variable.js b/src/parser/variable.js index c3202f094..4f18cb172 100644 --- a/src/parser/variable.js +++ b/src/parser/variable.js @@ -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, "$", "{"]); diff --git a/test/snapshot/__snapshots__/encapsed.test.js.snap b/test/snapshot/__snapshots__/encapsed.test.js.snap index 37bb63e6f..f05c313bb 100644 --- a/test/snapshot/__snapshots__/encapsed.test.js.snap +++ b/test/snapshot/__snapshots__/encapsed.test.js.snap @@ -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", @@ -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", @@ -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", @@ -604,9 +616,9 @@ Program { EncapsedPart { "curly": true, "expression": Variable { - "curly": "juice", + "curly": false, "kind": "variable", - "name": "variable", + "name": "juice", }, "kind": "encapsedpart", "syntax": "simple", @@ -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 [ @@ -1547,7 +1613,7 @@ Program { "expression": PropertyLookup { "kind": "propertylookup", "offset": EncapsedPart { - "curly": null, + "curly": false, "expression": OffsetLookup { "kind": "offsetlookup", "offset": Number { @@ -1561,7 +1627,7 @@ Program { }, }, "kind": "encapsedpart", - "syntax": true, + "syntax": "complex", }, "what": Variable { "curly": false, @@ -2854,9 +2920,9 @@ Program { EncapsedPart { "curly": true, "expression": Variable { - "curly": "var", + "curly": false, "kind": "variable", - "name": "variable", + "name": "var", }, "kind": "encapsedpart", "syntax": "simple", diff --git a/test/snapshot/__snapshots__/propertylookup.test.js.snap b/test/snapshot/__snapshots__/propertylookup.test.js.snap index c361844b2..9964a63ee 100644 --- a/test/snapshot/__snapshots__/propertylookup.test.js.snap +++ b/test/snapshot/__snapshots__/propertylookup.test.js.snap @@ -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, diff --git a/test/snapshot/__snapshots__/string.test.js.snap b/test/snapshot/__snapshots__/string.test.js.snap index 583eed033..467c2880c 100644 --- a/test/snapshot/__snapshots__/string.test.js.snap +++ b/test/snapshot/__snapshots__/string.test.js.snap @@ -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", @@ -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", diff --git a/test/snapshot/__snapshots__/variable.test.js.snap b/test/snapshot/__snapshots__/variable.test.js.snap index 07e8239b2..8556ee41d 100644 --- a/test/snapshot/__snapshots__/variable.test.js.snap +++ b/test/snapshot/__snapshots__/variable.test.js.snap @@ -1235,7 +1235,7 @@ Program { "expression": PropertyLookup { "kind": "propertylookup", "offset": EncapsedPart { - "curly": null, + "curly": false, "expression": PropertyLookup { "kind": "propertylookup", "offset": Identifier { @@ -1249,7 +1249,7 @@ Program { }, }, "kind": "encapsedpart", - "syntax": true, + "syntax": "complex", }, "what": Variable { "curly": false, diff --git a/test/snapshot/encapsed.test.js b/test/snapshot/encapsed.test.js index 55b00556f..bbbadfb6f 100644 --- a/test/snapshot/encapsed.test.js +++ b/test/snapshot/encapsed.test.js @@ -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(); + }); });