Skip to content

Commit 7de81ca

Browse files
authored
Merge pull request glayzzle#662 from Selion05/issue-658-null-safe-operator
add php8 nullsafe operator
2 parents 5c6fe91 + 4ca49ba commit 7de81ca

16 files changed

+905
-125
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: node_js
22
node_js:
3-
- '6'
3+
- '10'
44
cache:
55
bundler: true
66
directories:

src/ast.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ AST.prototype.checkNodes = function () {
522522
require("./ast/noop"),
523523
require("./ast/nowdoc"),
524524
require("./ast/nullkeyword"),
525+
require("./ast/nullsafepropertylookup"),
525526
require("./ast/number"),
526527
require("./ast/offsetlookup"),
527528
require("./ast/operation"),

src/ast/nullsafepropertylookup.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Copyright (C) 2018 Glayzzle (BSD3 License)
3+
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
4+
* @url http://glayzzle.com
5+
*/
6+
"use strict";
7+
8+
const Lookup = require("./lookup");
9+
const KIND = "nullsafepropertylookup";
10+
11+
/**
12+
* Lookup to an object property
13+
* @constructor PropertyLookup
14+
* @extends {Lookup}
15+
*/
16+
module.exports = Lookup.extends(KIND, function PropertyLookup(
17+
what,
18+
offset,
19+
docs,
20+
location
21+
) {
22+
Lookup.apply(this, [KIND, what, offset, docs, location]);
23+
});

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ const engine = function (options) {
9292
} else if (typeof options.parser.version !== "number") {
9393
throw new Error("Expecting a number for version");
9494
}
95-
if (options.parser.version < 500 || options.parser.version > 704) {
96-
throw new Error("Can only handle versions between 5.x to 7.x");
95+
if (options.parser.version < 500 || options.parser.version > 900) {
96+
throw new Error("Can only handle versions between 5.x to 8.x");
9797
}
9898
}
9999
}

src/lexer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const lexer = function (engine) {
2929
this.mode_eval = false;
3030
this.asp_tags = false;
3131
this.short_tags = false;
32-
this.version = 704;
32+
this.version = 800;
3333
this.yyprevcol = 0;
3434
this.keywords = {
3535
__class__: this.tok.T_CLASS_C,

src/lexer/tokens.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@ module.exports = {
162162
return this.tok.T_COALESCE;
163163
}
164164
}
165+
if (
166+
this.version >= 800 &&
167+
this._input[this.offset] === "-" &&
168+
this._input[this.offset + 1] === ">"
169+
) {
170+
this.consume(2);
171+
return this.tok.T_NULLSAFE_OBJECT_OPERATOR;
172+
}
165173
return "?";
166174
},
167175
"<": function () {

src/parser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const parser = function (lexer, ast) {
3333
this.token = null;
3434
this.prev = null;
3535
this.debug = false;
36-
this.version = 704;
36+
this.version = 800;
3737
this.extractDoc = false;
3838
this.extractTokens = false;
3939
this.suppressErrors = false;

src/parser/variable.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,11 @@ module.exports = {
228228
result = node(result, this.read_what());
229229
break;
230230
}
231+
case this.tok.T_NULLSAFE_OBJECT_OPERATOR: {
232+
node = this.node("nullsafepropertylookup");
233+
result = node(result, this.read_what());
234+
break;
235+
}
231236
default:
232237
break recursive_scan_loop;
233238
}

src/tokens.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ module.exports = {
146146
233: "T_SPACESHIP",
147147
234: "T_COALESCE_EQUAL",
148148
235: "T_FN",
149+
236: "T_NULLSAFE_OBJECT_OPERATOR",
149150
},
150151
names: {
151152
T_HALT_COMPILER: 101,
@@ -283,5 +284,6 @@ module.exports = {
283284
T_SPACESHIP: 233,
284285
T_COALESCE_EQUAL: 234,
285286
T_FN: 235,
287+
T_NULLSAFE_OBJECT_OPERATOR: 236,
286288
},
287289
};

test/snapshot/__snapshots__/call.test.js.snap

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,69 @@ Program {
793793
}
794794
`;
795795

796+
exports[`Test call nullsafepropertylookup (2) 1`] = `
797+
Program {
798+
"children": Array [
799+
ExpressionStatement {
800+
"expression": Call {
801+
"arguments": Array [],
802+
"kind": "call",
803+
"what": PropertyLookup {
804+
"kind": "nullsafepropertylookup",
805+
"offset": Identifier {
806+
"kind": "identifier",
807+
"name": "call",
808+
},
809+
"what": PropertyLookup {
810+
"kind": "nullsafepropertylookup",
811+
"offset": Identifier {
812+
"kind": "identifier",
813+
"name": "property",
814+
},
815+
"what": Variable {
816+
"curly": false,
817+
"kind": "variable",
818+
"name": "obj",
819+
},
820+
},
821+
},
822+
},
823+
"kind": "expressionstatement",
824+
},
825+
],
826+
"errors": Array [],
827+
"kind": "program",
828+
}
829+
`;
830+
831+
exports[`Test call nullsafepropertylookup 1`] = `
832+
Program {
833+
"children": Array [
834+
ExpressionStatement {
835+
"expression": Call {
836+
"arguments": Array [],
837+
"kind": "call",
838+
"what": PropertyLookup {
839+
"kind": "nullsafepropertylookup",
840+
"offset": Identifier {
841+
"kind": "identifier",
842+
"name": "call",
843+
},
844+
"what": Variable {
845+
"curly": false,
846+
"kind": "variable",
847+
"name": "obj",
848+
},
849+
},
850+
},
851+
"kind": "expressionstatement",
852+
},
853+
],
854+
"errors": Array [],
855+
"kind": "program",
856+
}
857+
`;
858+
796859
exports[`Test call offsetlookup 1`] = `
797860
Program {
798861
"children": Array [

0 commit comments

Comments
 (0)