js-ast-parser
Version:
把javascript代码字符串解析为AST对象
86 lines • 2.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.assertExpectNullStatement = exports.assertUnexpect = exports.getLast = exports.getPriority = exports.isHexCharCode = exports.isOperator = exports.isNumberStart = exports.isNumberCharCode = exports.isIdentifierChar = exports.isIdentifierStart = void 0;
const constants_1 = require("./constants");
const patterns_1 = require("./patterns");
const UnExpectStatement_1 = require("./lib/statement/UnExpectStatement");
function isIdentifierStart(code) {
if (code < 65)
return code === 36;
if (code < 91)
return true;
if (code < 97)
return code === 95;
if (code < 123)
return true;
return false;
}
exports.isIdentifierStart = isIdentifierStart;
function isIdentifierChar(code) {
if (code < 48)
return code === 36;
if (code < 58)
return true;
if (code < 65)
return false;
if (code < 91)
return true;
if (code < 97)
return code === 95;
if (code < 123)
return true;
return false;
}
exports.isIdentifierChar = isIdentifierChar;
function isNumberCharCode(code) {
return code >= 48 && code <= 57;
}
exports.isNumberCharCode = isNumberCharCode;
function isNumberStart(code) {
return code >= 48 && code <= 57;
}
exports.isNumberStart = isNumberStart;
function isOperator(word) {
return patterns_1.Patterns.operators.test(word);
}
exports.isOperator = isOperator;
function isHexCharCode(code) {
return code >= 48 && code <= 57 || code >= 97 && code <= 122 || code >= 65 && code <= 90;
}
exports.isHexCharCode = isHexCharCode;
function getPriority(tokenOrLiteratureStatement) {
if (typeof tokenOrLiteratureStatement === 'string') {
return constants_1.priorityMap[tokenOrLiteratureStatement] || -1;
}
else {
return -1;
}
}
exports.getPriority = getPriority;
function getLast(arr) {
return arr[arr.length - 1];
}
exports.getLast = getLast;
function assertUnexpect(parent, child, getUnexpectMsg) {
if (child && child.type === 'UnExpectStatement') {
parent.unexpectedNodes.push(child);
return true;
}
let msg = getUnexpectMsg ? getUnexpectMsg(child) : null;
if (msg) {
parent.unexpectedNodes.push(new UnExpectStatement_1.UnExpectStatement(child, msg));
return true;
}
}
exports.assertUnexpect = assertUnexpect;
function assertExpectNullStatement(parent, child, expect) {
if (child) {
assertUnexpect(parent, child, () => {
return `expect "${expect}"`;
});
return false;
}
return true;
}
exports.assertExpectNullStatement = assertExpectNullStatement;
//# sourceMappingURL=javasriptAstParserUtil.js.map