Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add php8 nullsafe operator #662

Merged
merged 4 commits into from
Feb 24, 2021
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 .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- '6'
- '10'
cache:
bundler: true
directories:
Expand Down
1 change: 1 addition & 0 deletions src/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ AST.prototype.checkNodes = function () {
require("./ast/noop"),
require("./ast/nowdoc"),
require("./ast/nullkeyword"),
require("./ast/nullsafepropertylookup"),
require("./ast/number"),
require("./ast/offsetlookup"),
require("./ast/operation"),
Expand Down
23 changes: 23 additions & 0 deletions src/ast/nullsafepropertylookup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright (C) 2018 Glayzzle (BSD3 License)
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
* @url http://glayzzle.com
*/
"use strict";

const Lookup = require("./lookup");
const KIND = "nullsafepropertylookup";

/**
* Lookup to an object property
* @constructor PropertyLookup
* @extends {Lookup}
*/
module.exports = Lookup.extends(KIND, function PropertyLookup(
what,
offset,
docs,
location
) {
Lookup.apply(this, [KIND, what, offset, docs, location]);
});
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ const engine = function (options) {
} else if (typeof options.parser.version !== "number") {
throw new Error("Expecting a number for version");
}
if (options.parser.version < 500 || options.parser.version > 704) {
throw new Error("Can only handle versions between 5.x to 7.x");
if (options.parser.version < 500 || options.parser.version > 900) {
throw new Error("Can only handle versions between 5.x to 8.x");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const lexer = function (engine) {
this.mode_eval = false;
this.asp_tags = false;
this.short_tags = false;
this.version = 704;
this.version = 800;
this.yyprevcol = 0;
this.keywords = {
__class__: this.tok.T_CLASS_C,
Expand Down
8 changes: 8 additions & 0 deletions src/lexer/tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ module.exports = {
return this.tok.T_COALESCE;
}
}
if (
this.version >= 800 &&
this._input[this.offset] === "-" &&
this._input[this.offset + 1] === ">"
) {
this.consume(2);
return this.tok.T_NULLSAFE_OBJECT_OPERATOR;
}
return "?";
},
"<": function () {
Expand Down
2 changes: 1 addition & 1 deletion src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const parser = function (lexer, ast) {
this.token = null;
this.prev = null;
this.debug = false;
this.version = 704;
this.version = 800;
this.extractDoc = false;
this.extractTokens = false;
this.suppressErrors = false;
Expand Down
5 changes: 5 additions & 0 deletions src/parser/variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ module.exports = {
result = node(result, this.read_what());
break;
}
case this.tok.T_NULLSAFE_OBJECT_OPERATOR: {
node = this.node("nullsafepropertylookup");
result = node(result, this.read_what());
break;
}
default:
break recursive_scan_loop;
}
Expand Down
2 changes: 2 additions & 0 deletions src/tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ module.exports = {
233: "T_SPACESHIP",
234: "T_COALESCE_EQUAL",
235: "T_FN",
236: "T_NULLSAFE_OBJECT_OPERATOR",
},
names: {
T_HALT_COMPILER: 101,
Expand Down Expand Up @@ -283,5 +284,6 @@ module.exports = {
T_SPACESHIP: 233,
T_COALESCE_EQUAL: 234,
T_FN: 235,
T_NULLSAFE_OBJECT_OPERATOR: 236,
},
};
63 changes: 63 additions & 0 deletions test/snapshot/__snapshots__/call.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,69 @@ Program {
}
`;

exports[`Test call nullsafepropertylookup (2) 1`] = `
Program {
"children": Array [
ExpressionStatement {
"expression": Call {
"arguments": Array [],
"kind": "call",
"what": PropertyLookup {
"kind": "nullsafepropertylookup",
"offset": Identifier {
"kind": "identifier",
"name": "call",
},
"what": PropertyLookup {
"kind": "nullsafepropertylookup",
"offset": Identifier {
"kind": "identifier",
"name": "property",
},
"what": Variable {
"curly": false,
"kind": "variable",
"name": "obj",
},
},
},
},
"kind": "expressionstatement",
},
],
"errors": Array [],
"kind": "program",
}
`;

exports[`Test call nullsafepropertylookup 1`] = `
Program {
"children": Array [
ExpressionStatement {
"expression": Call {
"arguments": Array [],
"kind": "call",
"what": PropertyLookup {
"kind": "nullsafepropertylookup",
"offset": Identifier {
"kind": "identifier",
"name": "call",
},
"what": Variable {
"curly": false,
"kind": "variable",
"name": "obj",
},
},
},
"kind": "expressionstatement",
},
],
"errors": Array [],
"kind": "program",
}
`;

exports[`Test call offsetlookup 1`] = `
Program {
"children": Array [
Expand Down
Loading