Skip to content

Commit 85ed983

Browse files
authored
Merge pull request glayzzle#479 from glayzzle/php_version
Php version
2 parents 12faf38 + fdd8c15 commit 85ed983

18 files changed

+124
-65
lines changed

src/index.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function combine(src, to) {
4545
* parser: {
4646
* extractDoc: true,
4747
* suppressErrors: true,
48-
* php7: true
48+
* version: 704 // or '7.4'
4949
* },
5050
* ast: {
5151
* withPositions: true
@@ -80,24 +80,27 @@ const engine = function(options) {
8080
if (!options.lexer) {
8181
options.lexer = {};
8282
}
83-
if (options.parser.php7 === false) {
84-
options.parser.php73 = false;
85-
options.parser.php74 = false;
86-
}
87-
if (options.parser.php73 === true) {
88-
options.parser.php7 = true;
89-
}
90-
if (options.parser.php74 === true) {
91-
options.parser.php7 = true;
92-
options.parser.php73 = true;
83+
if (options.parser.version) {
84+
if (typeof options.parser.version === "string") {
85+
let version = options.parser.version.split(".");
86+
version = parseInt(version[0]) * 100 + parseInt(version[1]);
87+
if (isNaN(version)) {
88+
throw new Error("Bad version number : " + options.parser.version);
89+
} else {
90+
options.parser.version = version;
91+
}
92+
} else if (typeof options.parser.version !== "number") {
93+
throw new Error("Expecting a number for version");
94+
}
95+
if (options.parser.version < 500 || options.parser.version > 704) {
96+
throw new Error("Can only handle versions between 5.x to 7.x");
97+
}
9398
}
9499
}
95100
combine(options, this);
96101

97102
// same version flags based on parser options
98-
this.lexer.php7 = this.parser.php7;
99-
this.lexer.php73 = this.parser.php73;
100-
this.lexer.php74 = this.parser.php74;
103+
this.lexer.version = this.parser.version;
101104
}
102105
};
103106

src/lexer.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ const lexer = function(engine) {
2929
this.mode_eval = false;
3030
this.asp_tags = false;
3131
this.short_tags = false;
32-
this.php7 = true;
33-
this.php73 = true;
34-
this.php74 = true;
32+
this.version = 704;
3533
this.yyprevcol = 0;
3634
this.keywords = {
3735
__class__: this.tok.T_CLASS_C,
@@ -146,7 +144,7 @@ lexer.prototype.setInput = function(input) {
146144
last_column: 0
147145
};
148146
this.tokens = [];
149-
if (this.php74) {
147+
if (this.version > 703) {
150148
this.keywords.fn = this.tok.T_FN;
151149
} else {
152150
delete this.keywords.fn;

src/lexer/tokens.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = {
1111
let id = this.keywords[token];
1212
if (typeof id !== "number") {
1313
if (token === "yield") {
14-
if (this.php7 && this.tryMatch(" from")) {
14+
if (this.version >= 700 && this.tryMatch(" from")) {
1515
this.consume(5);
1616
id = this.tok.T_YIELD_FROM;
1717
} else {
@@ -153,8 +153,8 @@ module.exports = {
153153
return "!";
154154
},
155155
"?": function() {
156-
if (this.php7 && this._input[this.offset] === "?") {
157-
if (this.php74 && this._input[this.offset + 1] === "=") {
156+
if (this.version >= 700 && this._input[this.offset] === "?") {
157+
if (this.version >= 704 && this._input[this.offset + 1] === "=") {
158158
this.consume(2);
159159
return this.tok.T_COALESCE_EQUAL;
160160
} else {
@@ -180,7 +180,7 @@ module.exports = {
180180
return this.tok.T_SL;
181181
} else if (nchar === "=") {
182182
this.input();
183-
if (this.php7 && this._input[this.offset] === ">") {
183+
if (this.version >= 700 && this._input[this.offset] === ">") {
184184
this.input();
185185
return this.tok.T_SPACESHIP;
186186
} else {

src/parser.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ const parser = function(lexer, ast) {
3333
this.token = null;
3434
this.prev = null;
3535
this.debug = false;
36-
this.php7 = true;
37-
this.php73 = true;
38-
this.php74 = true;
36+
this.version = 704;
3937
this.extractDoc = false;
4038
this.extractTokens = false;
4139
this.suppressErrors = false;

src/parser/array.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ module.exports = {
8282
this.next();
8383
byRef = true;
8484
value = this.read_variable(true, false);
85-
} else if (this.token === this.tok.T_ELLIPSIS && this.php74) {
85+
} else if (this.token === this.tok.T_ELLIPSIS && this.version >= 704) {
8686
this.next();
8787
if (this.token === "&") {
8888
this.error();

src/parser/class.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ module.exports = {
103103
} else if (
104104
this.token === this.tok.T_VARIABLE ||
105105
// support https://wiki.php.net/rfc/typed_properties_v2
106-
(this.php74 &&
106+
(this.version >= 704 &&
107107
(this.token === "?" ||
108108
this.token === this.tok.T_CALLABLE ||
109109
this.token === this.tok.T_ARRAY ||
@@ -197,7 +197,7 @@ module.exports = {
197197
let value = null;
198198
if (
199199
this.token === this.tok.T_STRING ||
200-
(this.php7 && this.is("IDENTIFIER"))
200+
(this.version >= 700 && this.is("IDENTIFIER"))
201201
) {
202202
constName = this.node("identifier");
203203
const name = this.text();
@@ -486,7 +486,7 @@ module.exports = {
486486
this.next();
487487
if (
488488
this.token === this.tok.T_STRING ||
489-
(this.php7 && this.is("IDENTIFIER"))
489+
(this.version >= 700 && this.is("IDENTIFIER"))
490490
) {
491491
trait = method;
492492
method = this.node("identifier");
@@ -520,7 +520,7 @@ module.exports = {
520520

521521
if (
522522
this.token === this.tok.T_STRING ||
523-
(this.php7 && this.is("IDENTIFIER"))
523+
(this.version >= 700 && this.is("IDENTIFIER"))
524524
) {
525525
alias = this.node("identifier");
526526
const name = this.text();

src/parser/expr.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ module.exports = {
386386
this.next();
387387
if (
388388
this.token === this.tok.T_FUNCTION ||
389-
(this.php74 && this.token === this.tok.T_FN)
389+
(this.version >= 704 && this.token === this.tok.T_FN)
390390
) {
391391
// handles static function
392392
return this.read_inline_function([0, 1, 0]);
@@ -413,22 +413,10 @@ module.exports = {
413413
switch (this.token) {
414414
case "=": {
415415
if (isConst) this.error("VARIABLE");
416-
let right;
417416
if (this.next().token == "&") {
418-
this.next();
419-
if (this.token === this.tok.T_NEW) {
420-
if (this.php7) {
421-
this.error();
422-
}
423-
right = this.read_new_expr();
424-
} else {
425-
right = this.read_variable(false, false);
426-
}
427-
428-
return result("assignref", expr, right);
417+
return this.read_assignref(result, expr);
429418
}
430-
right = this.read_expr();
431-
return result("assign", expr, right, "=");
419+
return result("assign", expr, this.read_expr(), "=");
432420
}
433421

434422
// operations :
@@ -520,6 +508,25 @@ module.exports = {
520508
return expr;
521509
},
522510

511+
/**
512+
* Reads assignment
513+
* @param {*} left
514+
*/
515+
read_assignref: function(result, left) {
516+
this.next();
517+
let right;
518+
if (this.token === this.tok.T_NEW) {
519+
if (this.version >= 700) {
520+
this.error();
521+
}
522+
right = this.read_new_expr();
523+
} else {
524+
right = this.read_variable(false, false);
525+
}
526+
527+
return result("assignref", left, right);
528+
},
529+
523530
/**
524531
*
525532
* inline_function:
@@ -540,7 +547,7 @@ module.exports = {
540547
return this.read_function(true, flags);
541548
}
542549
// introduced in PHP 7.4
543-
if (!this.php74) {
550+
if (!this.version >= 704) {
544551
this.raiseError("Arrow Functions are not allowed");
545552
}
546553
// as an arrowfunc

src/parser/function.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ module.exports = {
8282
if (type !== 1) {
8383
const nameNode = this.node("identifier");
8484
if (type === 2) {
85-
if (this.php7) {
85+
if (this.version >= 700) {
8686
if (this.token === this.tok.T_STRING || this.is("IDENTIFIER")) {
8787
name = this.text();
8888
this.next();
89-
} else if (!this.php74) {
89+
} else if (this.version < 704) {
9090
this.error("IDENTIFIER");
9191
}
9292
} else if (this.token === this.tok.T_STRING) {
@@ -96,11 +96,11 @@ module.exports = {
9696
this.error("IDENTIFIER");
9797
}
9898
} else {
99-
if (this.php7) {
99+
if (this.version >= 700) {
100100
if (this.token === this.tok.T_STRING) {
101101
name = this.text();
102102
this.next();
103-
} else if (this.php74) {
103+
} else if (this.version >= 704) {
104104
if (!this.expect("(")) {
105105
this.next();
106106
}

src/parser/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ module.exports = {
3838
read_function_list: function(item, separator) {
3939
const result = [];
4040
do {
41-
if (this.token == separator && this.php73 && result.length > 0) {
41+
if (this.token == separator && this.version >= 703 && result.length > 0) {
4242
result.push(this.node("noop")());
4343
break;
4444
}
4545
result.push(item.apply(this, []));
4646
if (this.token != separator) {
4747
break;
4848
}
49-
if (this.next().token == ")" && this.php73) {
49+
if (this.next().token == ")" && this.version >= 703) {
5050
break;
5151
}
5252
} while (this.token != this.EOF);

src/parser/variable.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ module.exports = {
9292
} else if (
9393
this.token === this.tok.T_STRING ||
9494
this.token === this.tok.T_CLASS ||
95-
(this.php7 && this.is("IDENTIFIER"))
95+
(this.version >= 700 && this.is("IDENTIFIER"))
9696
) {
9797
offset = this.node("identifier");
9898
name = this.text();

test/snapshot/arrowfunc.test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ describe("arrow function", () => {
1616
expect(
1717
parser.parseEval(`function fn($arg) { return $arg; }`, {
1818
parser: {
19-
php73: true,
20-
php74: false // disable the php 7.4 support
19+
version: "7.3" // disable the php 7.4 support
2120
}
2221
})
2322
).toMatchSnapshot();
@@ -26,7 +25,7 @@ describe("arrow function", () => {
2625
expect(
2726
parser.parseEval(`function fn($arg) { return $arg; }`, {
2827
parser: {
29-
php74: true,
28+
version: "7.4", // enable the php 7.4 support
3029
suppressErrors: true
3130
}
3231
})

test/snapshot/assign.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe("assign", () => {
5555
it("??= (php < 7)", function() {
5656
const astErr = parser.parseEval(`$var ??= $var;`, {
5757
parser: {
58-
php7: false,
58+
version: "5.6",
5959
suppressErrors: true
6060
}
6161
});

test/snapshot/bin.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ describe("bin", () => {
109109
it("?? (php < 7)", function() {
110110
const astErr = parser.parseEval(`$var ?? $var;`, {
111111
parser: {
112-
php7: false,
112+
version: "5.6",
113113
suppressErrors: true
114114
}
115115
});

test/snapshot/byref.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe("byref", () => {
1414
expect(
1515
parser.parseEval("$a =& new foo();", {
1616
parser: {
17-
php7: false
17+
version: "5.6"
1818
}
1919
})
2020
).toMatchSnapshot();

test/snapshot/function.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe("Function tests", function() {
5252
it("test reserved word for function name error", function() {
5353
const astErr = parser.parseEval(`function list() {}`, {
5454
parser: {
55-
php7: false,
55+
version: "5.6",
5656
suppressErrors: true
5757
}
5858
});
@@ -67,7 +67,7 @@ describe("Function tests", function() {
6767
it("test arrow function php 7.4", function() {
6868
const astErr = parser.parseEval(`function () {}`, {
6969
parser: {
70-
php74: true,
70+
version: "7.4",
7171
suppressErrors: true
7272
}
7373
});

test/snapshot/php5.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ describe("Test syntax parsing without PHP7 support", function() {
44
it("special keywords should fail", function() {
55
const ast = parser.parseEval("class foo { function list() { } }", {
66
parser: {
7-
php7: false,
7+
version: "5.6",
88
suppressErrors: true
99
}
1010
});

test/snapshot/php73.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ describe("Test syntax parsing with PHP 73 support", function() {
66
"[$a, &$b] = $array; list($a, &$b) = $array;",
77
{
88
parser: {
9-
php73: true
9+
version: "7.3"
1010
}
1111
}
1212
);
@@ -42,7 +42,7 @@ describe("Test syntax parsing with PHP 73 support", function() {
4242
`,
4343
{
4444
parser: {
45-
php73: true
45+
version: "7.3"
4646
}
4747
}
4848
);
@@ -66,7 +66,7 @@ describe("Test syntax parsing with PHP 73 support", function() {
6666
`,
6767
{
6868
parser: {
69-
php73: true,
69+
version: "7.3",
7070
suppressErrors: true
7171
}
7272
}

0 commit comments

Comments
 (0)