Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/parser/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ module.exports = {
const result = cb();
if (result) {
this.ast.swapLocations(result, byref, result, this);
result.byref = true;
result.byref = true;
}
return result;
},
Expand Down
40 changes: 10 additions & 30 deletions src/parser/variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,33 +126,6 @@ module.exports = {
if (is_static_lookup && this.token === this.tok.T_OBJECT_OPERATOR) {
this.error();
}

if (this.token === this.tok.T_VARIABLE) {
const inner = this.node("variable");
name = this.text().substring(1);
this.next();
what = this.node("encapsed")(
[what, inner(name, false)],
null,
"offset"
);
if (what.loc && what.value[0].loc) {
what.loc.start = what.value[0].loc.start;
}
} else if (this.token === "{") {
// EncapsedPart
const part = this.node("encapsedpart");
const expr = this.next().read_expr();
this.expect("}") && this.next();
what = this.node("encapsed")(
[what, part(expr, true)],
null,
"offset"
);
if (what.loc && what.value[0].loc) {
what.loc.start = what.value[0].loc.start;
}
}
break;
case this.tok.T_VARIABLE:
what = this.node("variable");
Expand Down Expand Up @@ -209,23 +182,30 @@ module.exports = {
}
break;
case "[":
case "{": {
const backet = this.token;
const isSquareBracket = backet === "[";
node = this.node("offsetlookup");
this.next();
offset = false;
if (encapsed) {
offset = this.read_encaps_var_offset();
this.expect("]") && this.next();
this.expect(isSquareBracket ? "]" : "}") && this.next();
} else {
const isCallableVariable = isSquareBracket
? this.token !== "]"
: this.token !== "}";
// callable_variable : https://github.com/php/php-src/blob/493524454d66adde84e00d249d607ecd540de99f/Zend/zend_language_parser.y#L1122
if (this.token !== "]") {
if (isCallableVariable) {
offset = this.read_expr();
this.expect("]") && this.next();
this.expect(isSquareBracket ? "]" : "}") && this.next();
} else {
this.next();
}
}
result = node(result, offset);
break;
}
case this.tok.T_DOUBLE_COLON:
// @see https://github.com/glayzzle/php-parser/issues/107#issuecomment-354104574
if (
Expand Down
20 changes: 10 additions & 10 deletions test/snapshot/__snapshots__/break.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ Program {
}
`;

exports[`break with parens 1`] = `
exports[`break with expression 1`] = `
Program {
"children": Array [
Break {
"kind": "break",
"level": Number {
"kind": "number",
"parenthesizedExpression": true,
"value": "1",
"level": Variable {
"curly": false,
"kind": "variable",
"name": "var",
},
},
],
Expand All @@ -78,15 +78,15 @@ Program {
}
`;

exports[`break with var 1`] = `
exports[`break with parens 1`] = `
Program {
"children": Array [
Break {
"kind": "break",
"level": Variable {
"curly": false,
"kind": "variable",
"name": "var",
"level": Number {
"kind": "number",
"parenthesizedExpression": true,
"value": "1",
},
},
],
Expand Down
63 changes: 28 additions & 35 deletions test/snapshot/__snapshots__/expr.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -114,44 +114,37 @@ Program {
"kind": "identifier",
"name": "foo",
},
"what": PropertyLookup {
"kind": "propertylookup",
"offset": Encapsed {
"kind": "encapsed",
"type": "offset",
"value": Array [
Identifier {
"kind": "identifier",
"name": "bar",
},
EncapsedPart {
"curly": true,
"expression": Variable {
"what": OffsetLookup {
"kind": "offsetlookup",
"offset": Variable {
"curly": false,
"kind": "variable",
"name": "baz",
},
"what": PropertyLookup {
"kind": "propertylookup",
"offset": Identifier {
"kind": "identifier",
"name": "bar",
},
"what": Call {
"arguments": Array [
Variable {
"curly": false,
"kind": "variable",
"name": "baz",
"name": "foo",
},
],
"kind": "call",
"what": Post {
"kind": "post",
"parenthesizedExpression": true,
"type": "+",
"what": Variable {
"curly": false,
"kind": "variable",
"name": "a",
},
"kind": "encapsedpart",
},
],
},
"what": Call {
"arguments": Array [
Variable {
"curly": false,
"kind": "variable",
"name": "foo",
},
],
"kind": "call",
"what": Post {
"kind": "post",
"parenthesizedExpression": true,
"type": "+",
"what": Variable {
"curly": false,
"kind": "variable",
"name": "a",
},
},
},
Expand Down
102 changes: 102 additions & 0 deletions test/snapshot/__snapshots__/graceful.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,108 @@ Program {
}
`;

exports[`Test graceful mode to suppress errors should fail with '[' and '}' 1`] = `
Program {
"children": Array [
ExpressionStatement {
"expression": OffsetLookup {
"kind": "offsetlookup",
"offset": Variable {
"curly": false,
"kind": "variable",
"name": "bar",
},
"what": Variable {
"curly": false,
"kind": "variable",
"name": "obj",
},
},
"kind": "expressionstatement",
},
ExpressionStatement {
"expression": undefined,
"kind": "expressionstatement",
},
],
"errors": Array [
Error {
"expected": "]",
"kind": "error",
"line": 1,
"message": "Parse Error : syntax error, unexpected '}', expecting ']' on line 1",
"token": "'}'",
},
Error {
"expected": ";",
"kind": "error",
"line": 1,
"message": "Parse Error : syntax error, unexpected '}', expecting ';' on line 1",
"token": "'}'",
},
Error {
"expected": "EXPR",
"kind": "error",
"line": 1,
"message": "Parse Error : syntax error, unexpected '}' on line 1",
"token": "'}'",
},
],
"kind": "program",
}
`;

exports[`Test graceful mode to suppress errors should fail with '{' and ']' 1`] = `
Program {
"children": Array [
ExpressionStatement {
"expression": OffsetLookup {
"kind": "offsetlookup",
"offset": Variable {
"curly": false,
"kind": "variable",
"name": "foo",
},
"what": Variable {
"curly": false,
"kind": "variable",
"name": "obj",
},
},
"kind": "expressionstatement",
},
ExpressionStatement {
"expression": undefined,
"kind": "expressionstatement",
},
],
"errors": Array [
Error {
"expected": "}",
"kind": "error",
"line": 1,
"message": "Parse Error : syntax error, unexpected ']', expecting '}' on line 1",
"token": "']'",
},
Error {
"expected": ";",
"kind": "error",
"line": 1,
"message": "Parse Error : syntax error, unexpected ']', expecting ';' on line 1",
"token": "']'",
},
Error {
"expected": "EXPR",
"kind": "error",
"line": 1,
"message": "Parse Error : syntax error, unexpected ']' on line 1",
"token": "']'",
},
],
"kind": "program",
}
`;

exports[`Test graceful mode to suppress errors staticlookup 1`] = `
Program {
"children": Array [
Expand Down
Loading