+3
@@ -0,0 +1,3 @@
|
||||
export { RegExpParser } from "./regexp-parser.js";
|
||||
export { BaseRegExpVisitor } from "./base-regexp-visitor.js";
|
||||
//# sourceMappingURL=api.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"api.js","sourceRoot":"","sources":["../../src/api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC"}
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
export class BaseRegExpVisitor {
|
||||
visitChildren(node) {
|
||||
for (const key in node) {
|
||||
const child = node[key];
|
||||
/* istanbul ignore else */
|
||||
if (node.hasOwnProperty(key)) {
|
||||
if (child.type !== undefined) {
|
||||
this.visit(child);
|
||||
}
|
||||
else if (Array.isArray(child)) {
|
||||
child.forEach((subChild) => {
|
||||
this.visit(subChild);
|
||||
}, this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
visit(node) {
|
||||
switch (node.type) {
|
||||
case "Pattern":
|
||||
this.visitPattern(node);
|
||||
break;
|
||||
case "Flags":
|
||||
this.visitFlags(node);
|
||||
break;
|
||||
case "Disjunction":
|
||||
this.visitDisjunction(node);
|
||||
break;
|
||||
case "Alternative":
|
||||
this.visitAlternative(node);
|
||||
break;
|
||||
case "StartAnchor":
|
||||
this.visitStartAnchor(node);
|
||||
break;
|
||||
case "EndAnchor":
|
||||
this.visitEndAnchor(node);
|
||||
break;
|
||||
case "WordBoundary":
|
||||
this.visitWordBoundary(node);
|
||||
break;
|
||||
case "NonWordBoundary":
|
||||
this.visitNonWordBoundary(node);
|
||||
break;
|
||||
case "Lookahead":
|
||||
this.visitLookahead(node);
|
||||
break;
|
||||
case "NegativeLookahead":
|
||||
this.visitNegativeLookahead(node);
|
||||
break;
|
||||
case "Lookbehind":
|
||||
this.visitLookbehind(node);
|
||||
break;
|
||||
case "NegativeLookbehind":
|
||||
this.visitNegativeLookbehind(node);
|
||||
break;
|
||||
case "Character":
|
||||
this.visitCharacter(node);
|
||||
break;
|
||||
case "Set":
|
||||
this.visitSet(node);
|
||||
break;
|
||||
case "Group":
|
||||
this.visitGroup(node);
|
||||
break;
|
||||
case "GroupBackReference":
|
||||
this.visitGroupBackReference(node);
|
||||
break;
|
||||
case "Quantifier":
|
||||
this.visitQuantifier(node);
|
||||
break;
|
||||
}
|
||||
this.visitChildren(node);
|
||||
}
|
||||
visitPattern(node) { }
|
||||
visitFlags(node) { }
|
||||
visitDisjunction(node) { }
|
||||
visitAlternative(node) { }
|
||||
// Assertion
|
||||
visitStartAnchor(node) { }
|
||||
visitEndAnchor(node) { }
|
||||
visitWordBoundary(node) { }
|
||||
visitNonWordBoundary(node) { }
|
||||
visitLookahead(node) { }
|
||||
visitNegativeLookahead(node) { }
|
||||
visitLookbehind(node) { }
|
||||
visitNegativeLookbehind(node) { }
|
||||
// atoms
|
||||
visitCharacter(node) { }
|
||||
visitSet(node) { }
|
||||
visitGroup(node) { }
|
||||
visitGroupBackReference(node) { }
|
||||
visitQuantifier(node) { }
|
||||
}
|
||||
//# sourceMappingURL=base-regexp-visitor.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"base-regexp-visitor.js","sourceRoot":"","sources":["../../src/base-regexp-visitor.ts"],"names":[],"mappings":"AAeA,MAAM,OAAO,iBAAiB;IACrB,aAAa,CAAC,IAAgB;QACnC,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;YACvB,MAAM,KAAK,GAAI,IAAY,CAAC,GAAG,CAAC,CAAC;YACjC,0BAA0B;YAC1B,IAAI,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,EAAE,CAAC;gBAC7B,IAAI,KAAK,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;oBAC7B,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;gBACpB,CAAC;qBAAM,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,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.js";
|
||||
export const digitsCharCodes = [];
|
||||
for (let i = cc("0"); i <= cc("9"); i++) {
|
||||
digitsCharCodes.push(i);
|
||||
}
|
||||
export const wordCharCodes = [cc("_")].concat(digitsCharCodes);
|
||||
for (let i = cc("a"); i <= cc("z"); i++) {
|
||||
wordCharCodes.push(i);
|
||||
}
|
||||
for (let i = cc("A"); i <= cc("Z"); i++) {
|
||||
wordCharCodes.push(i);
|
||||
}
|
||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#character-classes
|
||||
export const whitespaceCodes = [
|
||||
cc(" "),
|
||||
cc("\f"),
|
||||
cc("\n"),
|
||||
cc("\r"),
|
||||
cc("\t"),
|
||||
cc("\v"),
|
||||
cc("\t"),
|
||||
cc("\u00a0"),
|
||||
cc("\u1680"),
|
||||
cc("\u2000"),
|
||||
cc("\u2001"),
|
||||
cc("\u2002"),
|
||||
cc("\u2003"),
|
||||
cc("\u2004"),
|
||||
cc("\u2005"),
|
||||
cc("\u2006"),
|
||||
cc("\u2007"),
|
||||
cc("\u2008"),
|
||||
cc("\u2009"),
|
||||
cc("\u200a"),
|
||||
cc("\u2028"),
|
||||
cc("\u2029"),
|
||||
cc("\u202f"),
|
||||
cc("\u205f"),
|
||||
cc("\u3000"),
|
||||
cc("\ufeff"),
|
||||
];
|
||||
//# sourceMappingURL=character-classes.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"character-classes.js","sourceRoot":"","sources":["../../src/character-classes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,EAAE,EAAE,MAAM,YAAY,CAAC;AAEhC,MAAM,CAAC,MAAM,eAAe,GAAa,EAAE,CAAC;AAC5C,KAAK,IAAI,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;IACxC,eAAe,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAC1B,CAA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C;IACZ,EAAE,CAAC,QAAQ,CAAC;IACZ,EAAE,CAAC,QAAQ,CAAC;IACZ,EAAE,CAAC,QAAQ,CAAC;CACb,CAAC"}
|
||||
+744
@@ -0,0 +1,744 @@
|
||||
import { addFlag, ASSERT_EXISTS, ASSERT_NEVER_REACH_HERE, cc, insertToSet, isCharacter, } from "./utils.js";
|
||||
import { digitsCharCodes, whitespaceCodes, wordCharCodes, } from "./character-classes.js";
|
||||
// consts and utilities
|
||||
const hexDigitPattern = /[0-9a-fA-F]/;
|
||||
const decimalPattern = /[0-9]/;
|
||||
const decimalPatternNoZero = /[1-9]/;
|
||||
// https://hackernoon.com/the-madness-of-parsing-real-world-javascript-regexps-d9ee336df983
|
||||
// https://www.ecma-international.org/ecma-262/8.0/index.html#prod-Pattern
|
||||
export class RegExpParser {
|
||||
constructor() {
|
||||
this.idx = 0;
|
||||
this.input = "";
|
||||
this.groupIdx = 0;
|
||||
}
|
||||
saveState() {
|
||||
return {
|
||||
idx: this.idx,
|
||||
input: this.input,
|
||||
groupIdx: this.groupIdx,
|
||||
};
|
||||
}
|
||||
restoreState(newState) {
|
||||
this.idx = newState.idx;
|
||||
this.input = newState.input;
|
||||
this.groupIdx = newState.groupIdx;
|
||||
}
|
||||
pattern(input) {
|
||||
// parser state
|
||||
this.idx = 0;
|
||||
this.input = input;
|
||||
this.groupIdx = 0;
|
||||
this.consumeChar("/");
|
||||
const value = this.disjunction();
|
||||
this.consumeChar("/");
|
||||
const flags = {
|
||||
type: "Flags",
|
||||
loc: { begin: this.idx, end: input.length },
|
||||
global: false,
|
||||
ignoreCase: false,
|
||||
multiLine: false,
|
||||
unicode: false,
|
||||
sticky: false,
|
||||
};
|
||||
while (this.isRegExpFlag()) {
|
||||
switch (this.popChar()) {
|
||||
case "g":
|
||||
addFlag(flags, "global");
|
||||
break;
|
||||
case "i":
|
||||
addFlag(flags, "ignoreCase");
|
||||
break;
|
||||
case "m":
|
||||
addFlag(flags, "multiLine");
|
||||
break;
|
||||
case "u":
|
||||
addFlag(flags, "unicode");
|
||||
break;
|
||||
case "y":
|
||||
addFlag(flags, "sticky");
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (this.idx !== this.input.length) {
|
||||
throw Error("Redundant input: " + this.input.substring(this.idx));
|
||||
}
|
||||
return {
|
||||
type: "Pattern",
|
||||
flags: flags,
|
||||
value: value,
|
||||
loc: this.loc(0),
|
||||
};
|
||||
}
|
||||
disjunction() {
|
||||
const alts = [];
|
||||
const begin = this.idx;
|
||||
alts.push(this.alternative());
|
||||
while (this.peekChar() === "|") {
|
||||
this.consumeChar("|");
|
||||
alts.push(this.alternative());
|
||||
}
|
||||
return { type: "Disjunction", value: alts, loc: this.loc(begin) };
|
||||
}
|
||||
alternative() {
|
||||
const terms = [];
|
||||
const begin = this.idx;
|
||||
while (this.isTerm()) {
|
||||
terms.push(this.term());
|
||||
}
|
||||
return { type: "Alternative", value: terms, loc: this.loc(begin) };
|
||||
}
|
||||
term() {
|
||||
if (this.isAssertion()) {
|
||||
return this.assertion();
|
||||
}
|
||||
else {
|
||||
return this.atom();
|
||||
}
|
||||
}
|
||||
assertion() {
|
||||
const begin = this.idx;
|
||||
switch (this.popChar()) {
|
||||
case "^":
|
||||
return {
|
||||
type: "StartAnchor",
|
||||
loc: this.loc(begin),
|
||||
};
|
||||
case "$":
|
||||
return { type: "EndAnchor", loc: this.loc(begin) };
|
||||
// '\b' or '\B'
|
||||
case "\\":
|
||||
switch (this.popChar()) {
|
||||
case "b":
|
||||
return {
|
||||
type: "WordBoundary",
|
||||
loc: this.loc(begin),
|
||||
};
|
||||
case "B":
|
||||
return {
|
||||
type: "NonWordBoundary",
|
||||
loc: this.loc(begin),
|
||||
};
|
||||
}
|
||||
/* c8 ignore next */
|
||||
throw Error("Invalid Assertion Escape");
|
||||
// '(?=' or '(?!'
|
||||
case "(":
|
||||
this.consumeChar("?");
|
||||
let type;
|
||||
switch (this.popChar()) {
|
||||
case "=":
|
||||
type = "Lookahead";
|
||||
break;
|
||||
case "!":
|
||||
type = "NegativeLookahead";
|
||||
break;
|
||||
case "<": {
|
||||
switch (this.popChar()) {
|
||||
case "=":
|
||||
type = "Lookbehind";
|
||||
break;
|
||||
case "!":
|
||||
type = "NegativeLookbehind";
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
ASSERT_EXISTS(type);
|
||||
const disjunction = this.disjunction();
|
||||
this.consumeChar(")");
|
||||
return {
|
||||
type: type,
|
||||
value: disjunction,
|
||||
loc: this.loc(begin),
|
||||
};
|
||||
}
|
||||
// istanbul ignore next
|
||||
return ASSERT_NEVER_REACH_HERE();
|
||||
}
|
||||
quantifier(isBacktracking = false) {
|
||||
let range = undefined;
|
||||
const begin = this.idx;
|
||||
switch (this.popChar()) {
|
||||
case "*":
|
||||
range = {
|
||||
atLeast: 0,
|
||||
atMost: Infinity,
|
||||
};
|
||||
break;
|
||||
case "+":
|
||||
range = {
|
||||
atLeast: 1,
|
||||
atMost: Infinity,
|
||||
};
|
||||
break;
|
||||
case "?":
|
||||
range = {
|
||||
atLeast: 0,
|
||||
atMost: 1,
|
||||
};
|
||||
break;
|
||||
case "{":
|
||||
const atLeast = this.integerIncludingZero();
|
||||
switch (this.popChar()) {
|
||||
case "}":
|
||||
range = {
|
||||
atLeast: atLeast,
|
||||
atMost: atLeast,
|
||||
};
|
||||
break;
|
||||
case ",":
|
||||
let atMost;
|
||||
if (this.isDigit()) {
|
||||
atMost = this.integerIncludingZero();
|
||||
range = {
|
||||
atLeast: atLeast,
|
||||
atMost: atMost,
|
||||
};
|
||||
}
|
||||
else {
|
||||
range = {
|
||||
atLeast: atLeast,
|
||||
atMost: Infinity,
|
||||
};
|
||||
}
|
||||
this.consumeChar("}");
|
||||
break;
|
||||
}
|
||||
// throwing exceptions from "ASSERT_EXISTS" during backtracking
|
||||
// causes severe performance degradations
|
||||
if (isBacktracking === true && range === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
ASSERT_EXISTS(range);
|
||||
break;
|
||||
}
|
||||
// throwing exceptions from "ASSERT_EXISTS" during backtracking
|
||||
// causes severe performance degradations
|
||||
if (isBacktracking === true && range === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
// istanbul ignore else
|
||||
if (ASSERT_EXISTS(range)) {
|
||||
if (this.peekChar(0) === "?") {
|
||||
this.consumeChar("?");
|
||||
range.greedy = false;
|
||||
}
|
||||
else {
|
||||
range.greedy = true;
|
||||
}
|
||||
range.type = "Quantifier";
|
||||
range.loc = this.loc(begin);
|
||||
return range;
|
||||
}
|
||||
}
|
||||
atom() {
|
||||
let atom;
|
||||
const begin = this.idx;
|
||||
switch (this.peekChar()) {
|
||||
case ".":
|
||||
atom = this.dotAll();
|
||||
break;
|
||||
case "\\":
|
||||
atom = this.atomEscape();
|
||||
break;
|
||||
case "[":
|
||||
atom = this.characterClass();
|
||||
break;
|
||||
case "(":
|
||||
atom = this.group();
|
||||
break;
|
||||
}
|
||||
if (atom === undefined && this.isPatternCharacter()) {
|
||||
atom = this.patternCharacter();
|
||||
}
|
||||
// istanbul ignore else
|
||||
if (ASSERT_EXISTS(atom)) {
|
||||
atom.loc = this.loc(begin);
|
||||
if (this.isQuantifier()) {
|
||||
atom.quantifier = this.quantifier();
|
||||
}
|
||||
return atom;
|
||||
}
|
||||
// istanbul ignore next
|
||||
return ASSERT_NEVER_REACH_HERE();
|
||||
}
|
||||
dotAll() {
|
||||
this.consumeChar(".");
|
||||
return {
|
||||
type: "Set",
|
||||
complement: true,
|
||||
value: [cc("\n"), cc("\r"), cc("\u2028"), cc("\u2029")],
|
||||
};
|
||||
}
|
||||
atomEscape() {
|
||||
this.consumeChar("\\");
|
||||
switch (this.peekChar()) {
|
||||
case "1":
|
||||
case "2":
|
||||
case "3":
|
||||
case "4":
|
||||
case "5":
|
||||
case "6":
|
||||
case "7":
|
||||
case "8":
|
||||
case "9":
|
||||
return this.decimalEscapeAtom();
|
||||
case "d":
|
||||
case "D":
|
||||
case "s":
|
||||
case "S":
|
||||
case "w":
|
||||
case "W":
|
||||
return this.characterClassEscape();
|
||||
case "f":
|
||||
case "n":
|
||||
case "r":
|
||||
case "t":
|
||||
case "v":
|
||||
return this.controlEscapeAtom();
|
||||
case "c":
|
||||
return this.controlLetterEscapeAtom();
|
||||
case "0":
|
||||
return this.nulCharacterAtom();
|
||||
case "x":
|
||||
return this.hexEscapeSequenceAtom();
|
||||
case "u":
|
||||
return this.regExpUnicodeEscapeSequenceAtom();
|
||||
default:
|
||||
return this.identityEscapeAtom();
|
||||
}
|
||||
}
|
||||
decimalEscapeAtom() {
|
||||
const value = this.positiveInteger();
|
||||
return { type: "GroupBackReference", value: value };
|
||||
}
|
||||
characterClassEscape() {
|
||||
let set;
|
||||
let complement = false;
|
||||
switch (this.popChar()) {
|
||||
case "d":
|
||||
set = digitsCharCodes;
|
||||
break;
|
||||
case "D":
|
||||
set = digitsCharCodes;
|
||||
complement = true;
|
||||
break;
|
||||
case "s":
|
||||
set = whitespaceCodes;
|
||||
break;
|
||||
case "S":
|
||||
set = whitespaceCodes;
|
||||
complement = true;
|
||||
break;
|
||||
case "w":
|
||||
set = wordCharCodes;
|
||||
break;
|
||||
case "W":
|
||||
set = wordCharCodes;
|
||||
complement = true;
|
||||
break;
|
||||
}
|
||||
// istanbul ignore else
|
||||
if (ASSERT_EXISTS(set)) {
|
||||
return { type: "Set", value: set, complement: complement };
|
||||
}
|
||||
// istanbul ignore next
|
||||
return ASSERT_NEVER_REACH_HERE();
|
||||
}
|
||||
controlEscapeAtom() {
|
||||
let escapeCode;
|
||||
switch (this.popChar()) {
|
||||
case "f":
|
||||
escapeCode = cc("\f");
|
||||
break;
|
||||
case "n":
|
||||
escapeCode = cc("\n");
|
||||
break;
|
||||
case "r":
|
||||
escapeCode = cc("\r");
|
||||
break;
|
||||
case "t":
|
||||
escapeCode = cc("\t");
|
||||
break;
|
||||
case "v":
|
||||
escapeCode = cc("\v");
|
||||
break;
|
||||
}
|
||||
// istanbul ignore else
|
||||
if (ASSERT_EXISTS(escapeCode)) {
|
||||
return { type: "Character", value: escapeCode };
|
||||
}
|
||||
// istanbul ignore next
|
||||
return ASSERT_NEVER_REACH_HERE();
|
||||
}
|
||||
controlLetterEscapeAtom() {
|
||||
this.consumeChar("c");
|
||||
const letter = this.popChar();
|
||||
if (/[a-zA-Z]/.test(letter) === false) {
|
||||
throw Error("Invalid ");
|
||||
}
|
||||
const letterCode = letter.toUpperCase().charCodeAt(0) - 64;
|
||||
return { type: "Character", value: letterCode };
|
||||
}
|
||||
nulCharacterAtom() {
|
||||
// TODO implement '[lookahead ∉ DecimalDigit]'
|
||||
// TODO: for the deprecated octal escape sequence
|
||||
this.consumeChar("0");
|
||||
return { type: "Character", value: cc("\0") };
|
||||
}
|
||||
hexEscapeSequenceAtom() {
|
||||
this.consumeChar("x");
|
||||
return this.parseHexDigits(2);
|
||||
}
|
||||
regExpUnicodeEscapeSequenceAtom() {
|
||||
this.consumeChar("u");
|
||||
return this.parseHexDigits(4);
|
||||
}
|
||||
identityEscapeAtom() {
|
||||
// TODO: implement "SourceCharacter but not UnicodeIDContinue"
|
||||
// // http://unicode.org/reports/tr31/#Specific_Character_Adjustments
|
||||
const escapedChar = this.popChar();
|
||||
return { type: "Character", value: cc(escapedChar) };
|
||||
}
|
||||
classPatternCharacterAtom() {
|
||||
switch (this.peekChar()) {
|
||||
// istanbul ignore next
|
||||
case "\n":
|
||||
// istanbul ignore next
|
||||
case "\r":
|
||||
// istanbul ignore next
|
||||
case "\u2028":
|
||||
// istanbul ignore next
|
||||
case "\u2029":
|
||||
// istanbul ignore next
|
||||
case "\\":
|
||||
// istanbul ignore next
|
||||
case "]":
|
||||
throw Error("TBD");
|
||||
default:
|
||||
const nextChar = this.popChar();
|
||||
return { type: "Character", value: cc(nextChar) };
|
||||
}
|
||||
}
|
||||
characterClass() {
|
||||
const set = [];
|
||||
let complement = false;
|
||||
this.consumeChar("[");
|
||||
if (this.peekChar(0) === "^") {
|
||||
this.consumeChar("^");
|
||||
complement = true;
|
||||
}
|
||||
while (this.isClassAtom()) {
|
||||
const from = this.classAtom();
|
||||
const isFromSingleChar = from.type === "Character";
|
||||
if (isCharacter(from) && this.isRangeDash()) {
|
||||
this.consumeChar("-");
|
||||
const to = this.classAtom();
|
||||
const isToSingleChar = to.type === "Character";
|
||||
// a range can only be used when both sides are single characters
|
||||
if (isCharacter(to)) {
|
||||
if (to.value < from.value) {
|
||||
throw Error("Range out of order in character class");
|
||||
}
|
||||
set.push({ from: from.value, to: to.value });
|
||||
}
|
||||
else {
|
||||
// literal dash
|
||||
insertToSet(from.value, set);
|
||||
set.push(cc("-"));
|
||||
insertToSet(to.value, set);
|
||||
}
|
||||
}
|
||||
else {
|
||||
insertToSet(from.value, set);
|
||||
}
|
||||
}
|
||||
this.consumeChar("]");
|
||||
return { type: "Set", complement: complement, value: set };
|
||||
}
|
||||
classAtom() {
|
||||
switch (this.peekChar()) {
|
||||
// istanbul ignore next
|
||||
case "]":
|
||||
// istanbul ignore next
|
||||
case "\n":
|
||||
// istanbul ignore next
|
||||
case "\r":
|
||||
// istanbul ignore next
|
||||
case "\u2028":
|
||||
// istanbul ignore next
|
||||
case "\u2029":
|
||||
throw Error("TBD");
|
||||
case "\\":
|
||||
return this.classEscape();
|
||||
default:
|
||||
return this.classPatternCharacterAtom();
|
||||
}
|
||||
}
|
||||
classEscape() {
|
||||
this.consumeChar("\\");
|
||||
switch (this.peekChar()) {
|
||||
// Matches a backspace.
|
||||
// (Not to be confused with \b word boundary outside characterClass)
|
||||
case "b":
|
||||
this.consumeChar("b");
|
||||
return { type: "Character", value: cc("\u0008") };
|
||||
case "d":
|
||||
case "D":
|
||||
case "s":
|
||||
case "S":
|
||||
case "w":
|
||||
case "W":
|
||||
return this.characterClassEscape();
|
||||
case "f":
|
||||
case "n":
|
||||
case "r":
|
||||
case "t":
|
||||
case "v":
|
||||
return this.controlEscapeAtom();
|
||||
case "c":
|
||||
return this.controlLetterEscapeAtom();
|
||||
case "0":
|
||||
return this.nulCharacterAtom();
|
||||
case "x":
|
||||
return this.hexEscapeSequenceAtom();
|
||||
case "u":
|
||||
return this.regExpUnicodeEscapeSequenceAtom();
|
||||
default:
|
||||
return this.identityEscapeAtom();
|
||||
}
|
||||
}
|
||||
group() {
|
||||
let capturing = true;
|
||||
this.consumeChar("(");
|
||||
switch (this.peekChar(0)) {
|
||||
case "?":
|
||||
this.consumeChar("?");
|
||||
this.consumeChar(":");
|
||||
capturing = false;
|
||||
break;
|
||||
default:
|
||||
this.groupIdx++;
|
||||
break;
|
||||
}
|
||||
const value = this.disjunction();
|
||||
this.consumeChar(")");
|
||||
const groupAst = {
|
||||
type: "Group",
|
||||
capturing: capturing,
|
||||
value: value,
|
||||
};
|
||||
if (capturing) {
|
||||
groupAst["idx"] = this.groupIdx;
|
||||
}
|
||||
return groupAst;
|
||||
}
|
||||
positiveInteger() {
|
||||
let number = this.popChar();
|
||||
// istanbul ignore next - can't ever get here due to previous lookahead checks
|
||||
// still implementing this error checking in case this ever changes.
|
||||
if (decimalPatternNoZero.test(number) === false) {
|
||||
throw Error("Expecting a positive integer");
|
||||
}
|
||||
while (decimalPattern.test(this.peekChar(0))) {
|
||||
number += this.popChar();
|
||||
}
|
||||
return parseInt(number, 10);
|
||||
}
|
||||
integerIncludingZero() {
|
||||
let number = this.popChar();
|
||||
if (decimalPattern.test(number) === false) {
|
||||
throw Error("Expecting an integer");
|
||||
}
|
||||
while (decimalPattern.test(this.peekChar(0))) {
|
||||
number += this.popChar();
|
||||
}
|
||||
return parseInt(number, 10);
|
||||
}
|
||||
patternCharacter() {
|
||||
const nextChar = this.popChar();
|
||||
switch (nextChar) {
|
||||
// istanbul ignore next
|
||||
case "\n":
|
||||
// istanbul ignore next
|
||||
case "\r":
|
||||
// istanbul ignore next
|
||||
case "\u2028":
|
||||
// istanbul ignore next
|
||||
case "\u2029":
|
||||
// istanbul ignore next
|
||||
case "^":
|
||||
// istanbul ignore next
|
||||
case "$":
|
||||
// istanbul ignore next
|
||||
case "\\":
|
||||
// istanbul ignore next
|
||||
case ".":
|
||||
// istanbul ignore next
|
||||
case "*":
|
||||
// istanbul ignore next
|
||||
case "+":
|
||||
// istanbul ignore next
|
||||
case "?":
|
||||
// istanbul ignore next
|
||||
case "(":
|
||||
// istanbul ignore next
|
||||
case ")":
|
||||
// istanbul ignore next
|
||||
case "[":
|
||||
// istanbul ignore next
|
||||
case "|":
|
||||
// istanbul ignore next
|
||||
throw Error("TBD");
|
||||
default:
|
||||
return { type: "Character", value: cc(nextChar) };
|
||||
}
|
||||
}
|
||||
isRegExpFlag() {
|
||||
switch (this.peekChar(0)) {
|
||||
case "g":
|
||||
case "i":
|
||||
case "m":
|
||||
case "u":
|
||||
case "y":
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
isRangeDash() {
|
||||
return this.peekChar() === "-" && this.isClassAtom(1);
|
||||
}
|
||||
isDigit() {
|
||||
return decimalPattern.test(this.peekChar(0));
|
||||
}
|
||||
isClassAtom(howMuch = 0) {
|
||||
switch (this.peekChar(howMuch)) {
|
||||
case "]":
|
||||
case "\n":
|
||||
case "\r":
|
||||
case "\u2028":
|
||||
case "\u2029":
|
||||
return false;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
isTerm() {
|
||||
return this.isAtom() || this.isAssertion();
|
||||
}
|
||||
isAtom() {
|
||||
if (this.isPatternCharacter()) {
|
||||
return true;
|
||||
}
|
||||
switch (this.peekChar(0)) {
|
||||
case ".":
|
||||
case "\\": // atomEscape
|
||||
case "[": // characterClass
|
||||
// TODO: isAtom must be called before isAssertion - disambiguate
|
||||
case "(": // group
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
isAssertion() {
|
||||
switch (this.peekChar(0)) {
|
||||
case "^":
|
||||
case "$":
|
||||
return true;
|
||||
// '\b' or '\B'
|
||||
case "\\":
|
||||
switch (this.peekChar(1)) {
|
||||
case "b":
|
||||
case "B":
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
// '(?=' or '(?!' or `(?<=` or `(?<!`
|
||||
case "(":
|
||||
return (this.peekChar(1) === "?" &&
|
||||
(this.peekChar(2) === "=" ||
|
||||
this.peekChar(2) === "!" ||
|
||||
(this.peekChar(2) === "<" &&
|
||||
(this.peekChar(3) === "=" || this.peekChar(3) === "!"))));
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
isQuantifier() {
|
||||
const prevState = this.saveState();
|
||||
try {
|
||||
return this.quantifier(true) !== undefined;
|
||||
}
|
||||
catch (e) {
|
||||
return false;
|
||||
}
|
||||
finally {
|
||||
this.restoreState(prevState);
|
||||
}
|
||||
}
|
||||
isPatternCharacter() {
|
||||
switch (this.peekChar()) {
|
||||
case "^":
|
||||
case "$":
|
||||
case "\\":
|
||||
case ".":
|
||||
case "*":
|
||||
case "+":
|
||||
case "?":
|
||||
case "(":
|
||||
case ")":
|
||||
case "[":
|
||||
case "|":
|
||||
case "/":
|
||||
case "\n":
|
||||
case "\r":
|
||||
case "\u2028":
|
||||
case "\u2029":
|
||||
return false;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
parseHexDigits(howMany) {
|
||||
let hexString = "";
|
||||
for (let i = 0; i < howMany; i++) {
|
||||
const hexChar = this.popChar();
|
||||
if (hexDigitPattern.test(hexChar) === false) {
|
||||
throw Error("Expecting a HexDecimal digits");
|
||||
}
|
||||
hexString += hexChar;
|
||||
}
|
||||
const charCode = parseInt(hexString, 16);
|
||||
return { type: "Character", value: charCode };
|
||||
}
|
||||
peekChar(howMuch = 0) {
|
||||
return this.input[this.idx + howMuch];
|
||||
}
|
||||
popChar() {
|
||||
const nextChar = this.peekChar(0);
|
||||
this.consumeChar(undefined);
|
||||
return nextChar;
|
||||
}
|
||||
consumeChar(char) {
|
||||
if (char !== undefined && this.input[this.idx] !== char) {
|
||||
throw Error("Expected: '" +
|
||||
char +
|
||||
"' but found: '" +
|
||||
this.input[this.idx] +
|
||||
"' at offset: " +
|
||||
this.idx);
|
||||
}
|
||||
if (this.idx >= this.input.length) {
|
||||
throw Error("Unexpected end of input");
|
||||
}
|
||||
this.idx++;
|
||||
}
|
||||
loc(begin) {
|
||||
return { begin: begin, end: this.idx };
|
||||
}
|
||||
}
|
||||
//# sourceMappingURL=regexp-parser.js.map
|
||||
+1
File diff suppressed because one or more lines are too long
+35
@@ -0,0 +1,35 @@
|
||||
export function cc(char) {
|
||||
return char.charCodeAt(0);
|
||||
}
|
||||
export function insertToSet(item, set) {
|
||||
if (Array.isArray(item)) {
|
||||
item.forEach(function (subItem) {
|
||||
set.push(subItem);
|
||||
});
|
||||
}
|
||||
else {
|
||||
set.push(item);
|
||||
}
|
||||
}
|
||||
export function addFlag(flagObj, flagKey) {
|
||||
if (flagObj[flagKey] === true) {
|
||||
throw "duplicate flag " + flagKey;
|
||||
}
|
||||
const x = flagObj[flagKey];
|
||||
flagObj[flagKey] = true;
|
||||
}
|
||||
export function ASSERT_EXISTS(obj) {
|
||||
// istanbul ignore next
|
||||
if (obj === undefined) {
|
||||
throw Error("Internal Error - Should never get here!");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// istanbul ignore next
|
||||
export function ASSERT_NEVER_REACH_HERE() {
|
||||
throw Error("Internal Error - Should never get here!");
|
||||
}
|
||||
export function isCharacter(obj) {
|
||||
return obj["type"] === "Character";
|
||||
}
|
||||
//# sourceMappingURL=utils.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,EAAE,CAAC,IAAY;IAC7B,OAAO,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;AAC5B,CAAC;AAED,MAAM,UAAU,WAAW,CAAI,IAAa,EAAE,GAAQ;IACpD,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;QACxB,IAAI,CAAC,OAAO,CAAC,UAAU,OAAO;YAC5B,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACpB,CAAC,CAAC,CAAC;IACL,CAAC;SAAM,CAAC;QACN,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACjB,CAAC;AACH,CAAC;AAED,MAAM,UAAU,OAAO,CACrB,OAAoB,EACpB,OAAkD;IAElD,IAAI,OAAO,CAAC,OAAO,CAAC,KAAK,IAAI,EAAE,CAAC;QAC9B,MAAM,iBAAiB,GAAG,OAAO,CAAC;IACpC,CAAC;IAED,MAAM,CAAC,GAAY,OAAO,CAAC,OAAO,CAAC,CAAC;IACpC,OAAO,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC;AAC1B,CAAC;AAED,MAAM,UAAU,aAAa,CAAa,GAAQ;IAChD,uBAAuB;IACvB,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;QACtB,MAAM,KAAK,CAAC,yCAAyC,CAAC,CAAC;IACzD,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED,uBAAuB;AACvB,MAAM,UAAU,uBAAuB;IACrC,MAAM,KAAK,CAAC,yCAAyC,CAAC,CAAC;AACzD,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,GAAqB;IAC/C,OAAO,GAAG,CAAC,MAAM,CAAC,KAAK,WAAW,CAAC;AACrC,CAAC"}
|
||||
Reference in New Issue
Block a user