Skip to content

Commit 62cf815

Browse files
authored
Merge pull request #377 from glayzzle/test-curly-offsetlookup
fix: curly offsetlookup
2 parents 802bb7a + b5a49c5 commit 62cf815

File tree

7 files changed

+610
-88
lines changed

7 files changed

+610
-88
lines changed

src/parser/variable.js

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -126,33 +126,6 @@ module.exports = {
126126
if (is_static_lookup && this.token === this.tok.T_OBJECT_OPERATOR) {
127127
this.error();
128128
}
129-
130-
if (this.token === this.tok.T_VARIABLE) {
131-
const inner = this.node("variable");
132-
name = this.text().substring(1);
133-
this.next();
134-
what = this.node("encapsed")(
135-
[what, inner(name, false)],
136-
null,
137-
"offset"
138-
);
139-
if (what.loc && what.value[0].loc) {
140-
what.loc.start = what.value[0].loc.start;
141-
}
142-
} else if (this.token === "{") {
143-
// EncapsedPart
144-
const part = this.node("encapsedpart");
145-
const expr = this.next().read_expr();
146-
this.expect("}") && this.next();
147-
what = this.node("encapsed")(
148-
[what, part(expr, true)],
149-
null,
150-
"offset"
151-
);
152-
if (what.loc && what.value[0].loc) {
153-
what.loc.start = what.value[0].loc.start;
154-
}
155-
}
156129
break;
157130
case this.tok.T_VARIABLE:
158131
what = this.node("variable");
@@ -209,23 +182,30 @@ module.exports = {
209182
}
210183
break;
211184
case "[":
185+
case "{": {
186+
const backet = this.token;
187+
const isSquareBracket = backet === "[";
212188
node = this.node("offsetlookup");
213189
this.next();
214190
offset = false;
215191
if (encapsed) {
216192
offset = this.read_encaps_var_offset();
217-
this.expect("]") && this.next();
193+
this.expect(isSquareBracket ? "]" : "}") && this.next();
218194
} else {
195+
const isCallableVariable = isSquareBracket
196+
? this.token !== "]"
197+
: this.token !== "}";
219198
// callable_variable : https://github.com/php/php-src/blob/493524454d66adde84e00d249d607ecd540de99f/Zend/zend_language_parser.y#L1122
220-
if (this.token !== "]") {
199+
if (isCallableVariable) {
221200
offset = this.read_expr();
222-
this.expect("]") && this.next();
201+
this.expect(isSquareBracket ? "]" : "}") && this.next();
223202
} else {
224203
this.next();
225204
}
226205
}
227206
result = node(result, offset);
228207
break;
208+
}
229209
case this.tok.T_DOUBLE_COLON:
230210
// @see https://github.com/glayzzle/php-parser/issues/107#issuecomment-354104574
231211
if (

test/snapshot/__snapshots__/expr.test.js.snap

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -114,44 +114,37 @@ Program {
114114
"kind": "identifier",
115115
"name": "foo",
116116
},
117-
"what": PropertyLookup {
118-
"kind": "propertylookup",
119-
"offset": Encapsed {
120-
"kind": "encapsed",
121-
"type": "offset",
122-
"value": Array [
123-
Identifier {
124-
"kind": "identifier",
125-
"name": "bar",
126-
},
127-
EncapsedPart {
128-
"curly": true,
129-
"expression": Variable {
117+
"what": OffsetLookup {
118+
"kind": "offsetlookup",
119+
"offset": Variable {
120+
"curly": false,
121+
"kind": "variable",
122+
"name": "baz",
123+
},
124+
"what": PropertyLookup {
125+
"kind": "propertylookup",
126+
"offset": Identifier {
127+
"kind": "identifier",
128+
"name": "bar",
129+
},
130+
"what": Call {
131+
"arguments": Array [
132+
Variable {
130133
"curly": false,
131134
"kind": "variable",
132-
"name": "baz",
135+
"name": "foo",
136+
},
137+
],
138+
"kind": "call",
139+
"what": Post {
140+
"kind": "post",
141+
"parenthesizedExpression": true,
142+
"type": "+",
143+
"what": Variable {
144+
"curly": false,
145+
"kind": "variable",
146+
"name": "a",
133147
},
134-
"kind": "encapsedpart",
135-
},
136-
],
137-
},
138-
"what": Call {
139-
"arguments": Array [
140-
Variable {
141-
"curly": false,
142-
"kind": "variable",
143-
"name": "foo",
144-
},
145-
],
146-
"kind": "call",
147-
"what": Post {
148-
"kind": "post",
149-
"parenthesizedExpression": true,
150-
"type": "+",
151-
"what": Variable {
152-
"curly": false,
153-
"kind": "variable",
154-
"name": "a",
155148
},
156149
},
157150
},

test/snapshot/__snapshots__/graceful.test.js.snap

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,108 @@ Program {
145145
}
146146
`;
147147

148+
exports[`Test graceful mode to suppress errors should fail with '[' and '}' 1`] = `
149+
Program {
150+
"children": Array [
151+
ExpressionStatement {
152+
"expression": OffsetLookup {
153+
"kind": "offsetlookup",
154+
"offset": Variable {
155+
"curly": false,
156+
"kind": "variable",
157+
"name": "bar",
158+
},
159+
"what": Variable {
160+
"curly": false,
161+
"kind": "variable",
162+
"name": "obj",
163+
},
164+
},
165+
"kind": "expressionstatement",
166+
},
167+
ExpressionStatement {
168+
"expression": undefined,
169+
"kind": "expressionstatement",
170+
},
171+
],
172+
"errors": Array [
173+
Error {
174+
"expected": "]",
175+
"kind": "error",
176+
"line": 1,
177+
"message": "Parse Error : syntax error, unexpected '}', expecting ']' on line 1",
178+
"token": "'}'",
179+
},
180+
Error {
181+
"expected": ";",
182+
"kind": "error",
183+
"line": 1,
184+
"message": "Parse Error : syntax error, unexpected '}', expecting ';' on line 1",
185+
"token": "'}'",
186+
},
187+
Error {
188+
"expected": "EXPR",
189+
"kind": "error",
190+
"line": 1,
191+
"message": "Parse Error : syntax error, unexpected '}' on line 1",
192+
"token": "'}'",
193+
},
194+
],
195+
"kind": "program",
196+
}
197+
`;
198+
199+
exports[`Test graceful mode to suppress errors should fail with '{' and ']' 1`] = `
200+
Program {
201+
"children": Array [
202+
ExpressionStatement {
203+
"expression": OffsetLookup {
204+
"kind": "offsetlookup",
205+
"offset": Variable {
206+
"curly": false,
207+
"kind": "variable",
208+
"name": "foo",
209+
},
210+
"what": Variable {
211+
"curly": false,
212+
"kind": "variable",
213+
"name": "obj",
214+
},
215+
},
216+
"kind": "expressionstatement",
217+
},
218+
ExpressionStatement {
219+
"expression": undefined,
220+
"kind": "expressionstatement",
221+
},
222+
],
223+
"errors": Array [
224+
Error {
225+
"expected": "}",
226+
"kind": "error",
227+
"line": 1,
228+
"message": "Parse Error : syntax error, unexpected ']', expecting '}' on line 1",
229+
"token": "']'",
230+
},
231+
Error {
232+
"expected": ";",
233+
"kind": "error",
234+
"line": 1,
235+
"message": "Parse Error : syntax error, unexpected ']', expecting ';' on line 1",
236+
"token": "']'",
237+
},
238+
Error {
239+
"expected": "EXPR",
240+
"kind": "error",
241+
"line": 1,
242+
"message": "Parse Error : syntax error, unexpected ']' on line 1",
243+
"token": "']'",
244+
},
245+
],
246+
"kind": "program",
247+
}
248+
`;
249+
148250
exports[`Test graceful mode to suppress errors staticlookup 1`] = `
149251
Program {
150252
"children": Array [

0 commit comments

Comments
 (0)