php-parser
Version:
Parse PHP code from JS and returns its AST
115 lines (112 loc) • 3.23 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_IN_SCRIPTING: function () {
let ch = this.input();
switch (ch) {
case " ":
case "\t":
case "\n":
case "\r":
case "\r\n":
return this.T_WHITESPACE();
case "#":
if (this.version >= 800 && this._input[this.offset] === "[") {
this.input();
this.attributeListDepth[++this.attributeIndex] = 0;
this.begin("ST_ATTRIBUTE");
return this.tok.T_ATTRIBUTE;
}
return this.T_COMMENT();
case "/":
if (this._input[this.offset] === "/") {
return this.T_COMMENT();
} else if (this._input[this.offset] === "*") {
this.input();
return this.T_DOC_COMMENT();
}
return this.consume_TOKEN();
case "'":
return this.T_CONSTANT_ENCAPSED_STRING();
case '"':
return this.ST_DOUBLE_QUOTES();
case "`":
this.begin("ST_BACKQUOTE");
return "`";
case "?":
if (!this.aspTagMode && this.tryMatch(">")) {
this.input();
const nextCH = this._input[this.offset];
if (nextCH === "\n" || nextCH === "\r") this.input();
if (this.conditionStack.length > 1) {
this.begin("INITIAL");
}
return this.tok.T_CLOSE_TAG;
}
return this.consume_TOKEN();
case "%":
if (this.aspTagMode && this._input[this.offset] === ">") {
this.input(); // consume the '>'
ch = this._input[this.offset]; // read next
if (ch === "\n" || ch === "\r") {
this.input(); // consume the newline
}
this.aspTagMode = false;
if (this.conditionStack.length > 1) {
this.begin("INITIAL");
}
return this.tok.T_CLOSE_TAG;
}
return this.consume_TOKEN();
case "{":
this.begin("ST_IN_SCRIPTING");
return "{";
case "}":
if (this.conditionStack.length > 2) {
// Return to HEREDOC/ST_DOUBLE_QUOTES mode
this.popState();
}
return "}";
default:
if (ch === ".") {
ch = this.input();
if (this.is_NUM_START()) {
return this.consume_NUM();
} else {
if (ch) this.unput(1);
}
}
if (this.is_NUM_START()) {
return this.consume_NUM();
} else if (this.is_LABEL_START()) {
return this.consume_LABEL().T_STRING();
} else if (this.is_TOKEN()) {
return this.consume_TOKEN();
}
}
throw new Error(
'Bad terminal sequence "' +
ch +
'" at line ' +
this.yylineno +
" (offset " +
this.offset +
")",
);
},
T_WHITESPACE: function () {
while (this.offset < this.size) {
const ch = this.input();
if (ch === " " || ch === "\t" || ch === "\n" || ch === "\r") {
continue;
}
if (ch) this.unput(1);
break;
}
return this.tok.T_WHITESPACE;
},
};