Skip to content

Commit 9f22d76

Browse files
committed
glayzzle#386 - introduce a new version property
1 parent 628977c commit 9f22d76

File tree

10 files changed

+39
-40
lines changed

10 files changed

+39
-40
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(options.parser.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.0 to 7.4");
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: 3 additions & 3 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]);
@@ -417,7 +417,7 @@ module.exports = {
417417
if (this.next().token == "&") {
418418
this.next();
419419
if (this.token === this.tok.T_NEW) {
420-
if (this.php7) {
420+
if (this.version >= 700) {
421421
this.error();
422422
}
423423
right = this.read_new_expr();
@@ -540,7 +540,7 @@ module.exports = {
540540
return this.read_function(true, flags);
541541
}
542542
// introduced in PHP 7.4
543-
if (!this.php74) {
543+
if (!this.version >= 704) {
544544
this.raiseError("Arrow Functions are not allowed");
545545
}
546546
// 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
@@ -31,15 +31,15 @@ module.exports = {
3131
read_function_list: function(item, separator) {
3232
const result = [];
3333
do {
34-
if (this.token == separator && this.php73 && result.length > 0) {
34+
if (this.token == separator && this.version >= 703 && result.length > 0) {
3535
result.push(this.node("noop")());
3636
break;
3737
}
3838
result.push(item.apply(this, []));
3939
if (this.token != separator) {
4040
break;
4141
}
42-
if (this.next().token == ")" && this.php73) {
42+
if (this.next().token == ")" && this.version >= 703) {
4343
break;
4444
}
4545
} 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();

0 commit comments

Comments
 (0)