chevrotain
Version:
Chevrotain is a high performance fault tolerant javascript parsing DSL for building recursive decent parsers
590 lines • 24.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tokens_public_1 = require("./tokens_public");
var lexer_public_1 = require("./lexer_public");
var utils_1 = require("../utils/utils");
var PATTERN = "PATTERN";
exports.DEFAULT_MODE = "defaultMode";
exports.MODES = "modes";
exports.SUPPORT_STICKY = typeof new RegExp("(?:)").sticky === "boolean";
function disableSticky() {
exports.SUPPORT_STICKY = false;
}
exports.disableSticky = disableSticky;
function enableSticky() {
exports.SUPPORT_STICKY = true;
}
exports.enableSticky = enableSticky;
function analyzeTokenTypes(tokenTypes, useSticky) {
if (useSticky === void 0) { useSticky = exports.SUPPORT_STICKY; }
var onlyRelevantTypes = utils_1.reject(tokenTypes, function (currType) {
return currType[PATTERN] === lexer_public_1.Lexer.NA;
});
var hasCustom = false;
var allTransformedPatterns = utils_1.map(onlyRelevantTypes, function (currType) {
var currPattern = currType[PATTERN];
if (utils_1.isRegExp(currPattern)) {
var regExpSource = currPattern.source;
if (regExpSource.length === 1 &&
// only these regExp meta characters which can appear in a length one regExp
regExpSource !== "^" &&
regExpSource !== "$" &&
regExpSource !== ".") {
return regExpSource;
}
else if (regExpSource.length === 2 &&
regExpSource[0] === "\\" &&
// not a meta character
!utils_1.contains([
"d",
"D",
"s",
"S",
"t",
"r",
"n",
"t",
"0",
"c",
"b",
"B",
"f",
"v",
"w",
"W"
], regExpSource[1])) {
// escaped meta Characters: /\+/ /\[/
// or redundant escaping: /\a/
// without the escaping "\"
return regExpSource[1];
}
else {
return useSticky
? addStickyFlag(currPattern)
: addStartOfInput(currPattern);
}
}
else if (utils_1.isFunction(currPattern)) {
hasCustom = true;
// CustomPatternMatcherFunc - custom patterns do not require any transformations, only wrapping in a RegExp Like object
return { exec: currPattern };
}
else if (utils_1.has(currPattern, "exec")) {
hasCustom = true;
// ICustomPattern
return currPattern;
}
else if (typeof currPattern === "string") {
// IGNORE ABOVE ELSE
if (currPattern.length === 1) {
return currPattern;
}
else {
var escapedRegExpString = currPattern.replace(/[\\^$.*+?()[\]{}|]/g, "\\$&");
var wrappedRegExp = new RegExp(escapedRegExpString);
// TODO: extract the "?" expression, it is duplicated
return useSticky
? addStickyFlag(wrappedRegExp)
: addStartOfInput(wrappedRegExp);
}
}
else {
/* istanbul ignore next */
throw Error("non exhaustive match");
}
});
var patternIdxToType = utils_1.map(onlyRelevantTypes, function (currType) { return currType.tokenTypeIdx; });
var patternIdxToGroup = utils_1.map(onlyRelevantTypes, function (clazz) {
var groupName = clazz.GROUP;
if (groupName === lexer_public_1.Lexer.SKIPPED) {
return undefined;
}
else if (utils_1.isString(groupName)) {
return groupName;
}
else if (utils_1.isUndefined(groupName)) {
return false;
}
else {
/* istanbul ignore next */
throw Error("non exhaustive match");
}
});
var patternIdxToLongerAltIdx = utils_1.map(onlyRelevantTypes, function (clazz) {
var longerAltType = clazz.LONGER_ALT;
if (longerAltType) {
var longerAltIdx = utils_1.indexOf(onlyRelevantTypes, longerAltType);
return longerAltIdx;
}
});
var patternIdxToPushMode = utils_1.map(onlyRelevantTypes, function (clazz) { return clazz.PUSH_MODE; });
var patternIdxToPopMode = utils_1.map(onlyRelevantTypes, function (clazz) {
return utils_1.has(clazz, "POP_MODE");
});
var patternIdxToCanLineTerminator = utils_1.map(onlyRelevantTypes, function (clazz) { return clazz.LINE_BREAKS === true; });
var patternIdxToIsCustom = utils_1.map(onlyRelevantTypes, isCustomPattern);
var patternIdxToShort = utils_1.map(allTransformedPatterns, isShortPattern);
var emptyGroups = utils_1.reduce(onlyRelevantTypes, function (acc, clazz) {
var groupName = clazz.GROUP;
if (utils_1.isString(groupName) && !(groupName === lexer_public_1.Lexer.SKIPPED)) {
acc[groupName] = [];
}
return acc;
}, {});
var patternIdxToConfig = utils_1.map(allTransformedPatterns, function (x, idx) {
return {
pattern: allTransformedPatterns[idx],
longerAlt: patternIdxToLongerAltIdx[idx],
canLineTerminator: patternIdxToCanLineTerminator[idx],
isCustom: patternIdxToIsCustom[idx],
short: patternIdxToShort[idx],
group: patternIdxToGroup[idx],
push: patternIdxToPushMode[idx],
pop: patternIdxToPopMode[idx],
tokenTypeIdx: patternIdxToType[idx],
tokenType: onlyRelevantTypes[idx]
};
});
return {
emptyGroups: emptyGroups,
patternIdxToConfig: patternIdxToConfig,
hasCustom: hasCustom
};
}
exports.analyzeTokenTypes = analyzeTokenTypes;
function validatePatterns(tokenTypes, validModesNames) {
var errors = [];
var missingResult = findMissingPatterns(tokenTypes);
errors = errors.concat(missingResult.errors);
var invalidResult = findInvalidPatterns(missingResult.valid);
var validTokenTypes = invalidResult.valid;
errors = errors.concat(invalidResult.errors);
errors = errors.concat(validateRegExpPattern(validTokenTypes));
errors = errors.concat(findInvalidGroupType(validTokenTypes));
errors = errors.concat(findModesThatDoNotExist(validTokenTypes, validModesNames));
errors = errors.concat(findUnreachablePatterns(validTokenTypes));
return errors;
}
exports.validatePatterns = validatePatterns;
function validateRegExpPattern(tokenTypes) {
var errors = [];
var withRegExpPatterns = utils_1.filter(tokenTypes, function (currTokType) {
return utils_1.isRegExp(currTokType[PATTERN]);
});
errors = errors.concat(findEndOfInputAnchor(withRegExpPatterns));
errors = errors.concat(findStartOfInputAnchor(withRegExpPatterns));
errors = errors.concat(findUnsupportedFlags(withRegExpPatterns));
errors = errors.concat(findDuplicatePatterns(withRegExpPatterns));
errors = errors.concat(findEmptyMatchRegExps(withRegExpPatterns));
return errors;
}
function findMissingPatterns(tokenTypes) {
var tokenTypesWithMissingPattern = utils_1.filter(tokenTypes, function (currType) {
return !utils_1.has(currType, PATTERN);
});
var errors = utils_1.map(tokenTypesWithMissingPattern, function (currType) {
return {
message: "Token Type: ->" +
tokens_public_1.tokenName(currType) +
"<- missing static 'PATTERN' property",
type: lexer_public_1.LexerDefinitionErrorType.MISSING_PATTERN,
tokenTypes: [currType]
};
});
var valid = utils_1.difference(tokenTypes, tokenTypesWithMissingPattern);
return { errors: errors, valid: valid };
}
exports.findMissingPatterns = findMissingPatterns;
function findInvalidPatterns(tokenTypes) {
var tokenTypesWithInvalidPattern = utils_1.filter(tokenTypes, function (currType) {
var pattern = currType[PATTERN];
return (!utils_1.isRegExp(pattern) &&
!utils_1.isFunction(pattern) &&
!utils_1.has(pattern, "exec") &&
!utils_1.isString(pattern));
});
var errors = utils_1.map(tokenTypesWithInvalidPattern, function (currType) {
return {
message: "Token Type: ->" +
tokens_public_1.tokenName(currType) +
"<- static 'PATTERN' can only be a RegExp, a" +
" Function matching the {CustomPatternMatcherFunc} type or an Object matching the {ICustomPattern} interface.",
type: lexer_public_1.LexerDefinitionErrorType.INVALID_PATTERN,
tokenTypes: [currType]
};
});
var valid = utils_1.difference(tokenTypes, tokenTypesWithInvalidPattern);
return { errors: errors, valid: valid };
}
exports.findInvalidPatterns = findInvalidPatterns;
var end_of_input = /[^\\][\$]/;
function findEndOfInputAnchor(tokenTypes) {
var invalidRegex = utils_1.filter(tokenTypes, function (currType) {
var pattern = currType[PATTERN];
return end_of_input.test(pattern.source);
});
var errors = utils_1.map(invalidRegex, function (currType) {
return {
message: "Unexpected RegExp Anchor Error:\n" +
"\tToken Type: ->" +
tokens_public_1.tokenName(currType) +
"<- static 'PATTERN' cannot contain end of input anchor '$'\n" +
"\tSee sap.github.io/chevrotain/website/Building_Grammars/resolving_lexer_errors.html#ANCHORS" +
"\tfor details.",
type: lexer_public_1.LexerDefinitionErrorType.EOI_ANCHOR_FOUND,
tokenTypes: [currType]
};
});
return errors;
}
exports.findEndOfInputAnchor = findEndOfInputAnchor;
function findEmptyMatchRegExps(tokenTypes) {
var matchesEmptyString = utils_1.filter(tokenTypes, function (currType) {
var pattern = currType[PATTERN];
return pattern.test("");
});
var errors = utils_1.map(matchesEmptyString, function (currType) {
return {
message: "Token Type: ->" +
tokens_public_1.tokenName(currType) +
"<- static 'PATTERN' must not match an empty string",
type: lexer_public_1.LexerDefinitionErrorType.EMPTY_MATCH_PATTERN,
tokenTypes: [currType]
};
});
return errors;
}
exports.findEmptyMatchRegExps = findEmptyMatchRegExps;
var start_of_input = /[^\\[][\^]|^\^/;
function findStartOfInputAnchor(tokenTypes) {
var invalidRegex = utils_1.filter(tokenTypes, function (currType) {
var pattern = currType[PATTERN];
return start_of_input.test(pattern.source);
});
var errors = utils_1.map(invalidRegex, function (currType) {
return {
message: "Unexpected RegExp Anchor Error:\n" +
"\tToken Type: ->" +
tokens_public_1.tokenName(currType) +
"<- static 'PATTERN' cannot contain start of input anchor '^'\n" +
"\tSee https://github.com/SAP/chevrotain/blob/master/docs/resolving_lexer_errors.md#ANCHORS\n" +
"\tfor details.",
type: lexer_public_1.LexerDefinitionErrorType.SOI_ANCHOR_FOUND,
tokenTypes: [currType]
};
});
return errors;
}
exports.findStartOfInputAnchor = findStartOfInputAnchor;
function findUnsupportedFlags(tokenTypes) {
var invalidFlags = utils_1.filter(tokenTypes, function (currType) {
var pattern = currType[PATTERN];
return (pattern instanceof RegExp && (pattern.multiline || pattern.global));
});
var errors = utils_1.map(invalidFlags, function (currType) {
return {
message: "Token Type: ->" +
tokens_public_1.tokenName(currType) +
"<- static 'PATTERN' may NOT contain global('g') or multiline('m')",
type: lexer_public_1.LexerDefinitionErrorType.UNSUPPORTED_FLAGS_FOUND,
tokenTypes: [currType]
};
});
return errors;
}
exports.findUnsupportedFlags = findUnsupportedFlags;
// This can only test for identical duplicate RegExps, not semantically equivalent ones.
function findDuplicatePatterns(tokenTypes) {
var found = [];
var identicalPatterns = utils_1.map(tokenTypes, function (outerType) {
return utils_1.reduce(tokenTypes, function (result, innerType) {
if (outerType.PATTERN.source === innerType.PATTERN.source &&
!utils_1.contains(found, innerType) &&
innerType.PATTERN !== lexer_public_1.Lexer.NA) {
// this avoids duplicates in the result, each Token Type may only appear in one "set"
// in essence we are creating Equivalence classes on equality relation.
found.push(innerType);
result.push(innerType);
return result;
}
return result;
}, []);
});
identicalPatterns = utils_1.compact(identicalPatterns);
var duplicatePatterns = utils_1.filter(identicalPatterns, function (currIdenticalSet) {
return currIdenticalSet.length > 1;
});
var errors = utils_1.map(duplicatePatterns, function (setOfIdentical) {
var tokenTypeNames = utils_1.map(setOfIdentical, function (currType) {
return tokens_public_1.tokenName(currType);
});
var dupPatternSrc = utils_1.first(setOfIdentical).PATTERN;
return {
message: "The same RegExp pattern ->" + dupPatternSrc + "<-" +
("has been used in all of the following Token Types: " + tokenTypeNames.join(", ") + " <-"),
type: lexer_public_1.LexerDefinitionErrorType.DUPLICATE_PATTERNS_FOUND,
tokenTypes: setOfIdentical
};
});
return errors;
}
exports.findDuplicatePatterns = findDuplicatePatterns;
function findInvalidGroupType(tokenTypes) {
var invalidTypes = utils_1.filter(tokenTypes, function (clazz) {
if (!utils_1.has(clazz, "GROUP")) {
return false;
}
var group = clazz.GROUP;
return group !== lexer_public_1.Lexer.SKIPPED && group !== lexer_public_1.Lexer.NA && !utils_1.isString(group);
});
var errors = utils_1.map(invalidTypes, function (currType) {
return {
message: "Token Type: ->" +
tokens_public_1.tokenName(currType) +
"<- static 'GROUP' can only be Lexer.SKIPPED/Lexer.NA/A String",
type: lexer_public_1.LexerDefinitionErrorType.INVALID_GROUP_TYPE_FOUND,
tokenTypes: [currType]
};
});
return errors;
}
exports.findInvalidGroupType = findInvalidGroupType;
function findModesThatDoNotExist(tokenTypes, validModes) {
var invalidModes = utils_1.filter(tokenTypes, function (clazz) {
return (clazz.PUSH_MODE !== undefined &&
!utils_1.contains(validModes, clazz.PUSH_MODE));
});
var errors = utils_1.map(invalidModes, function (clazz) {
var msg = "Token Type: ->" + tokens_public_1.tokenName(clazz) + "<- static 'PUSH_MODE' value cannot refer to a Lexer Mode ->" + clazz.PUSH_MODE + "<-" + "which does not exist";
return {
message: msg,
type: lexer_public_1.LexerDefinitionErrorType.PUSH_MODE_DOES_NOT_EXIST,
tokenTypes: [clazz]
};
});
return errors;
}
exports.findModesThatDoNotExist = findModesThatDoNotExist;
function findUnreachablePatterns(tokenTypes) {
var errors = [];
var canBeTested = utils_1.reduce(tokenTypes, function (result, tokType, idx) {
var pattern = tokType.PATTERN;
if (pattern === lexer_public_1.Lexer.NA) {
return result;
}
// a more comprehensive validation for all forms of regExps would require
// deeper regExp analysis capabilities
if (utils_1.isString(pattern)) {
result.push({ str: pattern, idx: idx, tokenType: tokType });
}
else if (utils_1.isRegExp(pattern) && noMetaChar(pattern)) {
result.push({ str: pattern.source, idx: idx, tokenType: tokType });
}
return result;
}, []);
utils_1.forEach(tokenTypes, function (tokType, testIdx) {
utils_1.forEach(canBeTested, function (_a) {
var str = _a.str, idx = _a.idx, tokenType = _a.tokenType;
if (testIdx < idx && testTokenType(str, tokType.PATTERN)) {
var msg = "Token: ->" + tokens_public_1.tokenName(tokenType) + "<- can never be matched.\n" +
("Because it appears AFTER the Token Type ->" + tokens_public_1.tokenName(tokType) + "<-") +
"in the lexer's definition.\n" +
"See http://sap.github.io/chevrotain/website/Building_Grammars/resolving_lexer_errors.html#UNREACHABLE";
errors.push({
message: msg,
type: lexer_public_1.LexerDefinitionErrorType.UNREACHABLE_PATTERN,
tokenTypes: [tokType, tokenType]
});
}
});
});
return errors;
}
exports.findUnreachablePatterns = findUnreachablePatterns;
function testTokenType(str, pattern) {
if (utils_1.isRegExp(pattern)) {
var regExpArray = pattern.exec(str);
return regExpArray !== null && regExpArray.index === 0;
}
else if (utils_1.isFunction(pattern)) {
// maintain the API of custom patterns
return pattern(str, 0, [], {});
}
else if (utils_1.has(pattern, "exec")) {
// maintain the API of custom patterns
return pattern.exec(str, 0, [], {});
}
else if (typeof pattern === "string") {
return pattern === str;
}
else {
/* istanbul ignore next */
throw Error("non exhaustive match");
}
}
function noMetaChar(regExp) {
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
var metaChars = [
".",
"\\",
"[",
"]",
"|",
"^",
"$",
"(",
")",
"?",
"*",
"+",
"{"
];
return (utils_1.find(metaChars, function (char) { return regExp.source.indexOf(char) !== -1; }) ===
undefined);
}
function addStartOfInput(pattern) {
var flags = pattern.ignoreCase ? "i" : "";
// always wrapping in a none capturing group preceded by '^' to make sure matching can only work on start of input.
// duplicate/redundant start of input markers have no meaning (/^^^^A/ === /^A/)
return new RegExp("^(?:" + pattern.source + ")", flags);
}
exports.addStartOfInput = addStartOfInput;
function addStickyFlag(pattern) {
var flags = pattern.ignoreCase ? "iy" : "y";
// always wrapping in a none capturing group preceded by '^' to make sure matching can only work on start of input.
// duplicate/redundant start of input markers have no meaning (/^^^^A/ === /^A/)
return new RegExp("" + pattern.source, flags);
}
exports.addStickyFlag = addStickyFlag;
function performRuntimeChecks(lexerDefinition, trackLines) {
var errors = [];
// some run time checks to help the end users.
if (!utils_1.has(lexerDefinition, exports.DEFAULT_MODE)) {
errors.push({
message: "A MultiMode Lexer cannot be initialized without a <" +
exports.DEFAULT_MODE +
"> property in its definition\n",
type: lexer_public_1.LexerDefinitionErrorType.MULTI_MODE_LEXER_WITHOUT_DEFAULT_MODE
});
}
if (!utils_1.has(lexerDefinition, exports.MODES)) {
errors.push({
message: "A MultiMode Lexer cannot be initialized without a <" +
exports.MODES +
"> property in its definition\n",
type: lexer_public_1.LexerDefinitionErrorType.MULTI_MODE_LEXER_WITHOUT_MODES_PROPERTY
});
}
if (utils_1.has(lexerDefinition, exports.MODES) &&
utils_1.has(lexerDefinition, exports.DEFAULT_MODE) &&
!utils_1.has(lexerDefinition.modes, lexerDefinition.defaultMode)) {
errors.push({
message: "A MultiMode Lexer cannot be initialized with a " + exports.DEFAULT_MODE + ": <" + lexerDefinition.defaultMode + ">" + "which does not exist\n",
type: lexer_public_1.LexerDefinitionErrorType.MULTI_MODE_LEXER_DEFAULT_MODE_VALUE_DOES_NOT_EXIST
});
}
if (utils_1.has(lexerDefinition, exports.MODES)) {
utils_1.forEach(lexerDefinition.modes, function (currModeValue, currModeName) {
utils_1.forEach(currModeValue, function (currTokType, currIdx) {
if (utils_1.isUndefined(currTokType)) {
errors.push({
message: "A Lexer cannot be initialized using an undefined Token Type. Mode:" +
("<" + currModeName + "> at index: <" + currIdx + ">\n"),
type: lexer_public_1.LexerDefinitionErrorType.LEXER_DEFINITION_CANNOT_CONTAIN_UNDEFINED
});
}
});
});
}
var allTokenTypes = utils_1.flatten(utils_1.mapValues(lexerDefinition.modes, function (tokTypes) { return tokTypes; }));
if (trackLines &&
utils_1.find(allTokenTypes, function (currTokType) { return currTokType.LINE_BREAKS; }) === undefined) {
errors.push({
message: "No LINE_BREAKS Error:\n" +
"\tThis Lexer has been defined to track line and column information,\n" +
"\tyet none of the Token definitions contain a LINE_BREAK flag.\n" +
"\tSee http://sap.github.io/chevrotain/website/Building_Grammars/resolving_lexer_errors.html#LINE_BREAKS \n" +
"\tfor details.",
type: lexer_public_1.LexerDefinitionErrorType.NO_LINE_BREAKS_FLAGS
});
}
return errors;
}
exports.performRuntimeChecks = performRuntimeChecks;
function cloneEmptyGroups(emptyGroups) {
var clonedResult = {};
var groupKeys = utils_1.keys(emptyGroups);
utils_1.forEach(groupKeys, function (currKey) {
var currGroupValue = emptyGroups[currKey];
/* istanbul ignore else */
if (utils_1.isArray(currGroupValue)) {
clonedResult[currKey] = [];
}
else {
/* istanbul ignore next */
throw Error("non exhaustive match");
}
});
return clonedResult;
}
exports.cloneEmptyGroups = cloneEmptyGroups;
// TODO: refactor to avoid duplication
function isCustomPattern(tokenType) {
var pattern = tokenType.PATTERN;
if (utils_1.isRegExp(pattern)) {
return false;
}
else if (utils_1.isFunction(pattern)) {
// CustomPatternMatcherFunc - custom patterns do not require any transformations, only wrapping in a RegExp Like object
return true;
}
else if (utils_1.has(pattern, "exec")) {
// ICustomPattern
return true;
}
else if (utils_1.isString(pattern)) {
return false;
}
else {
/* istanbul ignore next */
throw Error("non exhaustive match");
}
}
exports.isCustomPattern = isCustomPattern;
function isShortPattern(pattern) {
if (utils_1.isString(pattern) && pattern.length === 1) {
return pattern.charCodeAt(0);
}
else {
return false;
}
}
exports.isShortPattern = isShortPattern;
/**
* Faster than using a RegExp for default newline detection during lexing.
*/
exports.LineTerminatorOptimizedTester = {
// implements /\n|\r\n?/g.test
test: function (text) {
var len = text.length;
for (var i = this.lastIndex; i < len; i++) {
var c = text.charCodeAt(i);
if (c === 10) {
this.lastIndex = i + 1;
return true;
}
else if (c === 13) {
if (text.charCodeAt(i + 1) === 10) {
this.lastIndex = i + 2;
}
else {
this.lastIndex = i + 1;
}
return true;
}
}
return false;
},
lastIndex: 0
};
//# sourceMappingURL=lexer.js.map