php-parser
Version:
Parse PHP code from JS and returns its AST
97 lines (93 loc) • 2.61 kB
JavaScript
/**
* Copyright (C) 2018 Glayzzle (BSD3 License)
* @authors https://github.com/glayzzle/php-parser/graphs/contributors
* @url http://glayzzle.com
*/
"use strict";
module.exports = {
matchST_LOOKING_FOR_PROPERTY: function () {
let ch = this.input();
if (ch === "-") {
ch = this.input();
if (ch === ">") {
// https://github.com/php/php-src/blob/master/Zend/zend_language_scanner.l#L1296
return this.tok.T_OBJECT_OPERATOR;
}
if (ch) this.unput(1);
} else if (this.is_WHITESPACE()) {
return this.tok.T_WHITESPACE;
} else if (this.is_LABEL_START()) {
// https://github.com/php/php-src/blob/master/Zend/zend_language_scanner.l#L1300
this.consume_LABEL();
this.popState();
return this.tok.T_STRING;
}
// https://github.com/php/php-src/blob/master/Zend/zend_language_scanner.l#L1306
this.popState();
if (ch) this.unput(1);
return false;
},
matchST_LOOKING_FOR_VARNAME: function () {
let ch = this.input();
// SHIFT STATE
this.popState();
this.begin("ST_IN_SCRIPTING");
if (this.is_LABEL_START()) {
this.consume_LABEL();
ch = this.input();
if (ch === "[" || ch === "}") {
this.unput(1);
return this.tok.T_STRING_VARNAME;
} else {
// any char (that's started with a label sequence)
this.unput(this.yytext.length);
}
} else {
// any char (thats not a label start sequence)
if (ch) this.unput(1);
}
// stops looking for a varname and starts the scripting mode
return false;
},
matchST_VAR_OFFSET: function () {
const ch = this.input();
if (this.is_NUM_START()) {
this.consume_NUM();
return this.tok.T_NUM_STRING;
} else if (ch === "]") {
this.popState();
return "]";
} else if (ch === "$") {
this.input();
if (this.is_LABEL_START()) {
this.consume_LABEL();
return this.tok.T_VARIABLE;
} else {
/* istanbul ignore next */
throw new Error("Unexpected terminal");
}
} else if (this.is_LABEL_START()) {
this.consume_LABEL();
return this.tok.T_STRING;
} else if (
this.is_WHITESPACE() ||
ch === "\\" ||
ch === "'" ||
ch === "#"
) {
return this.tok.T_ENCAPSED_AND_WHITESPACE;
} else if (
ch === "[" ||
ch === "{" ||
ch === "}" ||
ch === '"' ||
ch === "`" ||
this.is_TOKEN()
) {
return ch;
} else {
/* istanbul ignore next */
throw new Error("Unexpected terminal");
}
},
};