@bufbuild/re2 0.0.1-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +30 -0
- package/dist/cjs/CharClass.d.ts +30 -0
- package/dist/cjs/CharClass.js +284 -0
- package/dist/cjs/CharGroup.d.ts +8 -0
- package/dist/cjs/CharGroup.js +83 -0
- package/dist/cjs/Codepoint.d.ts +3 -0
- package/dist/cjs/Codepoint.js +62 -0
- package/dist/cjs/Compiler.d.ts +40 -0
- package/dist/cjs/Compiler.js +262 -0
- package/dist/cjs/DFA.d.ts +36 -0
- package/dist/cjs/DFA.js +350 -0
- package/dist/cjs/Inst.d.ts +26 -0
- package/dist/cjs/Inst.js +86 -0
- package/dist/cjs/MachineInput.d.ts +17 -0
- package/dist/cjs/MachineInput.js +72 -0
- package/dist/cjs/Parser.d.ts +111 -0
- package/dist/cjs/Parser.js +1538 -0
- package/dist/cjs/Prefilter.d.ts +19 -0
- package/dist/cjs/Prefilter.js +163 -0
- package/dist/cjs/Prog.d.ts +39 -0
- package/dist/cjs/Prog.js +154 -0
- package/dist/cjs/RE2.d.ts +27 -0
- package/dist/cjs/RE2.js +221 -0
- package/dist/cjs/RE2Flags.d.ts +16 -0
- package/dist/cjs/RE2Flags.js +58 -0
- package/dist/cjs/Regexp.d.ts +43 -0
- package/dist/cjs/Regexp.js +98 -0
- package/dist/cjs/Simplify.d.ts +3 -0
- package/dist/cjs/Simplify.js +230 -0
- package/dist/cjs/Unicode.d.ts +17 -0
- package/dist/cjs/Unicode.js +165 -0
- package/dist/cjs/UnicodeRangeTable.d.ts +12 -0
- package/dist/cjs/UnicodeRangeTable.js +31 -0
- package/dist/cjs/UnicodeTables.d.ts +29 -0
- package/dist/cjs/UnicodeTables.js +571 -0
- package/dist/cjs/Utils.d.ts +22 -0
- package/dist/cjs/Utils.js +119 -0
- package/dist/cjs/__fixtures__/find.d.ts +9 -0
- package/dist/cjs/__fixtures__/find.js +115 -0
- package/dist/cjs/chars.d.ts +2 -0
- package/dist/cjs/chars.js +19 -0
- package/dist/cjs/exceptions.d.ts +55 -0
- package/dist/cjs/exceptions.js +94 -0
- package/dist/cjs/index.d.ts +102 -0
- package/dist/cjs/index.js +173 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/testParser.d.ts +3 -0
- package/dist/cjs/testParser.js +143 -0
- package/dist/esm/CharClass.d.ts +30 -0
- package/dist/esm/CharClass.js +281 -0
- package/dist/esm/CharGroup.d.ts +8 -0
- package/dist/esm/CharGroup.js +78 -0
- package/dist/esm/Codepoint.d.ts +3 -0
- package/dist/esm/Codepoint.js +59 -0
- package/dist/esm/Compiler.d.ts +40 -0
- package/dist/esm/Compiler.js +259 -0
- package/dist/esm/DFA.d.ts +36 -0
- package/dist/esm/DFA.js +347 -0
- package/dist/esm/Inst.d.ts +26 -0
- package/dist/esm/Inst.js +83 -0
- package/dist/esm/MachineInput.d.ts +17 -0
- package/dist/esm/MachineInput.js +68 -0
- package/dist/esm/Parser.d.ts +111 -0
- package/dist/esm/Parser.js +1535 -0
- package/dist/esm/Prefilter.d.ts +19 -0
- package/dist/esm/Prefilter.js +159 -0
- package/dist/esm/Prog.d.ts +39 -0
- package/dist/esm/Prog.js +150 -0
- package/dist/esm/RE2.d.ts +27 -0
- package/dist/esm/RE2.js +218 -0
- package/dist/esm/RE2Flags.d.ts +16 -0
- package/dist/esm/RE2Flags.js +41 -0
- package/dist/esm/Regexp.d.ts +43 -0
- package/dist/esm/Regexp.js +94 -0
- package/dist/esm/Simplify.d.ts +3 -0
- package/dist/esm/Simplify.js +228 -0
- package/dist/esm/Unicode.d.ts +17 -0
- package/dist/esm/Unicode.js +150 -0
- package/dist/esm/UnicodeRangeTable.d.ts +12 -0
- package/dist/esm/UnicodeRangeTable.js +28 -0
- package/dist/esm/UnicodeTables.d.ts +29 -0
- package/dist/esm/UnicodeTables.js +568 -0
- package/dist/esm/Utils.d.ts +22 -0
- package/dist/esm/Utils.js +103 -0
- package/dist/esm/__fixtures__/find.d.ts +9 -0
- package/dist/esm/__fixtures__/find.js +112 -0
- package/dist/esm/chars.d.ts +2 -0
- package/dist/esm/chars.js +14 -0
- package/dist/esm/exceptions.d.ts +55 -0
- package/dist/esm/exceptions.js +86 -0
- package/dist/esm/index.d.ts +102 -0
- package/dist/esm/index.js +163 -0
- package/dist/esm/testParser.d.ts +3 -0
- package/dist/esm/testParser.js +138 -0
- package/package.json +49 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EMPTY_NO_WORD_BOUNDARY = exports.EMPTY_END_TEXT = exports.EMPTY_BEGIN_TEXT = exports.EMPTY_WORD_BOUNDARY = exports.EMPTY_END_LINE = exports.EMPTY_BEGIN_LINE = void 0;
|
|
4
|
+
exports.emptyInts = emptyInts;
|
|
5
|
+
exports.runeToString = runeToString;
|
|
6
|
+
exports.emptyOpContext = emptyOpContext;
|
|
7
|
+
exports.charCount = charCount;
|
|
8
|
+
exports.stringToRunes = stringToRunes;
|
|
9
|
+
exports.isalnum = isalnum;
|
|
10
|
+
exports.unhex = unhex;
|
|
11
|
+
exports.quoteMeta = quoteMeta;
|
|
12
|
+
const Unicode_js_1 = require("./Unicode.js");
|
|
13
|
+
const chars_js_1 = require("./chars.js");
|
|
14
|
+
/**
|
|
15
|
+
* Various constants and helper utilities.
|
|
16
|
+
*/
|
|
17
|
+
const METACHARACTERS = "\\.+*?()|[]{}^$";
|
|
18
|
+
//// EMPTY_* flags
|
|
19
|
+
const EMPTY_BEGIN_LINE = 0x01;
|
|
20
|
+
exports.EMPTY_BEGIN_LINE = EMPTY_BEGIN_LINE;
|
|
21
|
+
const EMPTY_END_LINE = 0x02;
|
|
22
|
+
exports.EMPTY_END_LINE = EMPTY_END_LINE;
|
|
23
|
+
const EMPTY_BEGIN_TEXT = 0x04;
|
|
24
|
+
exports.EMPTY_BEGIN_TEXT = EMPTY_BEGIN_TEXT;
|
|
25
|
+
const EMPTY_END_TEXT = 0x08;
|
|
26
|
+
exports.EMPTY_END_TEXT = EMPTY_END_TEXT;
|
|
27
|
+
const EMPTY_WORD_BOUNDARY = 0x10;
|
|
28
|
+
exports.EMPTY_WORD_BOUNDARY = EMPTY_WORD_BOUNDARY;
|
|
29
|
+
const EMPTY_NO_WORD_BOUNDARY = 0x20;
|
|
30
|
+
exports.EMPTY_NO_WORD_BOUNDARY = EMPTY_NO_WORD_BOUNDARY;
|
|
31
|
+
function emptyInts() {
|
|
32
|
+
return [];
|
|
33
|
+
}
|
|
34
|
+
// Returns true iff |c| is an ASCII letter or decimal digit.
|
|
35
|
+
function isalnum(c) {
|
|
36
|
+
return ((0x30 <= c && c <= 0x39) ||
|
|
37
|
+
(0x61 <= c && c <= 0x7a) ||
|
|
38
|
+
(0x41 <= c && c <= 0x5a));
|
|
39
|
+
}
|
|
40
|
+
// If |c| is an ASCII hex digit, returns its value, otherwise -1.
|
|
41
|
+
function unhex(c) {
|
|
42
|
+
if (0x30 <= c && c <= 0x39) {
|
|
43
|
+
return c - 0x30;
|
|
44
|
+
}
|
|
45
|
+
if (0x61 <= c && c <= 0x66) {
|
|
46
|
+
return c - 0x61 + 10;
|
|
47
|
+
}
|
|
48
|
+
if (0x41 <= c && c <= 0x46) {
|
|
49
|
+
return c - 0x41 + 10;
|
|
50
|
+
}
|
|
51
|
+
return -1;
|
|
52
|
+
}
|
|
53
|
+
// Returns the array of runes in the specified UTF-16 string.
|
|
54
|
+
function stringToRunes(str) {
|
|
55
|
+
return Array.from(String(str)).map((s) => (0, chars_js_1.codePointAtOrThrow)(s, 0));
|
|
56
|
+
}
|
|
57
|
+
// Returns the Java UTF-16 string containing the single rune |r|.
|
|
58
|
+
function runeToString(r) {
|
|
59
|
+
return String.fromCodePoint(r);
|
|
60
|
+
}
|
|
61
|
+
// isWordRune reports whether r is consider a ``word character''
|
|
62
|
+
// during the evaluation of the \b and \B zero-width assertions.
|
|
63
|
+
// These assertions are ASCII-only: the word characters are [A-Za-z0-9_].
|
|
64
|
+
function isWordRune(r) {
|
|
65
|
+
return ((0x61 <= r && r <= 0x7a) ||
|
|
66
|
+
(0x41 <= r && r <= 0x5a) ||
|
|
67
|
+
(0x30 <= r && r <= 0x39) ||
|
|
68
|
+
r === 0x5f);
|
|
69
|
+
}
|
|
70
|
+
// emptyOpContext returns the zero-width assertions satisfied at the position
|
|
71
|
+
// between the runes r1 and r2, a bitmask of EMPTY_* flags.
|
|
72
|
+
// Passing r1 == -1 indicates that the position is at the beginning of the
|
|
73
|
+
// text.
|
|
74
|
+
// Passing r2 == -1 indicates that the position is at the end of the text.
|
|
75
|
+
// eslint-disable-next-line no-warning-comments
|
|
76
|
+
// TODO(adonovan): move to Machine.
|
|
77
|
+
function emptyOpContext(r1, r2) {
|
|
78
|
+
let op = 0;
|
|
79
|
+
if (r1 < 0) {
|
|
80
|
+
op |= EMPTY_BEGIN_TEXT | EMPTY_BEGIN_LINE;
|
|
81
|
+
}
|
|
82
|
+
if (r1 === 0x0a) {
|
|
83
|
+
op |= EMPTY_BEGIN_LINE;
|
|
84
|
+
}
|
|
85
|
+
if (r2 < 0) {
|
|
86
|
+
op |= EMPTY_END_TEXT | EMPTY_END_LINE;
|
|
87
|
+
}
|
|
88
|
+
if (r2 === 0x0a) {
|
|
89
|
+
op |= EMPTY_END_LINE;
|
|
90
|
+
}
|
|
91
|
+
if (isWordRune(r1) !== isWordRune(r2)) {
|
|
92
|
+
op |= EMPTY_WORD_BOUNDARY;
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
op |= EMPTY_NO_WORD_BOUNDARY;
|
|
96
|
+
}
|
|
97
|
+
return op;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Returns a string that quotes all regular expression metacharacters inside the argument text;
|
|
101
|
+
* the returned string is a regular expression matching the literal text. For example,
|
|
102
|
+
* {@code quoteMeta("[foo]").equals("\\[foo\\]")}.
|
|
103
|
+
* @param {string} str
|
|
104
|
+
* @returns {string}
|
|
105
|
+
*/
|
|
106
|
+
function quoteMeta(str) {
|
|
107
|
+
return str
|
|
108
|
+
.split("") // A char loop is correct because all metacharacters fit in one UTF-16 code.
|
|
109
|
+
.map((s) => {
|
|
110
|
+
if (METACHARACTERS.indexOf(s) >= 0) {
|
|
111
|
+
return `\\${s}`;
|
|
112
|
+
}
|
|
113
|
+
return s;
|
|
114
|
+
})
|
|
115
|
+
.join("");
|
|
116
|
+
}
|
|
117
|
+
function charCount(codePoint) {
|
|
118
|
+
return codePoint > Unicode_js_1.MAX_BMP ? 2 : 1;
|
|
119
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FIND_TESTS = void 0;
|
|
4
|
+
class Test {
|
|
5
|
+
pat;
|
|
6
|
+
text;
|
|
7
|
+
matches;
|
|
8
|
+
constructor(pat, text, n, ...x) {
|
|
9
|
+
this.pat = pat;
|
|
10
|
+
this.text = text;
|
|
11
|
+
this.matches = [];
|
|
12
|
+
if (n > 0) {
|
|
13
|
+
const runLength = Math.floor(x.length / n);
|
|
14
|
+
for (let i = 0, j = 0; i < n; i++) {
|
|
15
|
+
this.matches[i] = x.slice(j, j + runLength);
|
|
16
|
+
j += runLength;
|
|
17
|
+
if (j > x.length) {
|
|
18
|
+
throw new Error("invalid build entry");
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
toString() {
|
|
24
|
+
return `pat=${this.pat} text=${this.text} len=${this.matches.length} matches=${JSON.stringify(this.matches)}`;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.FIND_TESTS = [
|
|
28
|
+
new Test("", "", 1, 0, 0),
|
|
29
|
+
new Test("^abcdefg", "abcdefg", 1, 0, 7),
|
|
30
|
+
new Test("a+", "baaab", 1, 1, 4),
|
|
31
|
+
new Test("abcd..", "abcdef", 1, 0, 6),
|
|
32
|
+
new Test("a", "a", 1, 0, 1),
|
|
33
|
+
new Test("x", "y", 0),
|
|
34
|
+
new Test("b", "abc", 1, 1, 2),
|
|
35
|
+
new Test(".", "a", 1, 0, 1),
|
|
36
|
+
new Test(".*", "abcdef", 1, 0, 6),
|
|
37
|
+
new Test("^", "abcde", 1, 0, 0),
|
|
38
|
+
new Test("$", "abcde", 1, 5, 5),
|
|
39
|
+
new Test("^abcd$", "abcd", 1, 0, 4),
|
|
40
|
+
new Test("^bcd'", "abcdef", 0),
|
|
41
|
+
new Test("^abcd$", "abcde", 0),
|
|
42
|
+
new Test("h.*od?", "hello\ngoodbye\n", 1, 0, 5),
|
|
43
|
+
new Test("a{1,5}", "baaac", 1, 1, 4),
|
|
44
|
+
new Test("ac{1,25}", "bbaaaccccdd", 1, 4, 9),
|
|
45
|
+
new Test("a+", "baaab", 1, 1, 4),
|
|
46
|
+
new Test("a*", "baaab", 3, 0, 0, 1, 4, 5, 5),
|
|
47
|
+
new Test("[a-z]+", "abcd", 1, 0, 4),
|
|
48
|
+
new Test("[^a-z]+", "ab1234cd", 1, 2, 6),
|
|
49
|
+
new Test("[a\\-\\]z]+", "az]-bcz", 2, 0, 4, 6, 7),
|
|
50
|
+
new Test("[^\\n]+", "abcd\n", 1, 0, 4),
|
|
51
|
+
new Test("[日本語]+", "日本語日本語", 1, 0, 18),
|
|
52
|
+
new Test("日本語+", "日本語", 1, 0, 9),
|
|
53
|
+
new Test("日本語+", "日本語語語語", 1, 0, 18),
|
|
54
|
+
new Test("()", "", 1, 0, 0, 0, 0),
|
|
55
|
+
new Test("(a)", "a", 1, 0, 1, 0, 1),
|
|
56
|
+
new Test("(.)(.)", "日a", 1, 0, 4, 0, 3, 3, 4),
|
|
57
|
+
new Test("(.*)", "", 1, 0, 0, 0, 0),
|
|
58
|
+
new Test("(.*)", "abcd", 1, 0, 4, 0, 4),
|
|
59
|
+
new Test("(..)(..)", "abcd", 1, 0, 4, 0, 2, 2, 4),
|
|
60
|
+
new Test("(([^xyz]*)(d))", "abcd", 1, 0, 4, 0, 4, 0, 3, 3, 4),
|
|
61
|
+
new Test("((a|b|c)*(d))", "abcd", 1, 0, 4, 0, 4, 2, 3, 3, 4),
|
|
62
|
+
new Test("(((a|b|c)*)(d))", "abcd", 1, 0, 4, 0, 4, 0, 3, 2, 3, 3, 4),
|
|
63
|
+
new Test("\\a\\f\\n\\r\\t\\v", "\x07\f\n\r\t\v", 1, 0, 6),
|
|
64
|
+
new Test("[\\a\\f\\n\\r\\t\\v]+", "\x07\f\n\r\t\v", 1, 0, 6),
|
|
65
|
+
new Test("a*(|(b))c*", "aacc", 1, 0, 4, 2, 2, -1, -1),
|
|
66
|
+
new Test("(.*).*", "ab", 1, 0, 2, 0, 2),
|
|
67
|
+
new Test("[.]", ".", 1, 0, 1),
|
|
68
|
+
new Test("/$", "/abc/", 1, 4, 5),
|
|
69
|
+
new Test("/$", "/abc", 0),
|
|
70
|
+
// multiple matches
|
|
71
|
+
new Test(".", "abc", 3, 0, 1, 1, 2, 2, 3),
|
|
72
|
+
new Test("(.)", "abc", 3, 0, 1, 0, 1, 1, 2, 1, 2, 2, 3, 2, 3),
|
|
73
|
+
new Test(".(.)", "abcd", 2, 0, 2, 1, 2, 2, 4, 3, 4),
|
|
74
|
+
new Test("ab*", "abbaab", 3, 0, 3, 3, 4, 4, 6),
|
|
75
|
+
new Test("a(b*)", "abbaab", 3, 0, 3, 1, 3, 3, 4, 4, 4, 4, 6, 5, 6),
|
|
76
|
+
// fixed bugs
|
|
77
|
+
new Test("ab$", "cab", 1, 1, 3),
|
|
78
|
+
new Test("axxb$", "axxcb", 0),
|
|
79
|
+
new Test("data", "daXY data", 1, 5, 9),
|
|
80
|
+
new Test("da(.)a$", "daXY data", 1, 5, 9, 7, 8),
|
|
81
|
+
new Test("zx+", "zzx", 1, 1, 3),
|
|
82
|
+
new Test("ab$", "abcab", 1, 3, 5),
|
|
83
|
+
new Test("(aa)*$", "a", 1, 1, 1, -1, -1),
|
|
84
|
+
new Test("(?:.|(?:.a))", "", 0),
|
|
85
|
+
new Test("(?:A(?:A|a))", "Aa", 1, 0, 2),
|
|
86
|
+
new Test("(?:A|(?:A|a))", "a", 1, 0, 1),
|
|
87
|
+
new Test("(a){0}", "", 1, 0, 0, -1, -1),
|
|
88
|
+
new Test("(?-s)(?:(?:^).)", "\n", 0),
|
|
89
|
+
new Test("(?s)(?:(?:^).)", "\n", 1, 0, 1),
|
|
90
|
+
new Test("(?:(?:^).)", "\n", 0),
|
|
91
|
+
new Test("\\b", "x", 2, 0, 0, 1, 1),
|
|
92
|
+
new Test("\\b", "xx", 2, 0, 0, 2, 2),
|
|
93
|
+
new Test("\\b", "x y", 4, 0, 0, 1, 1, 2, 2, 3, 3),
|
|
94
|
+
new Test("\\b", "xx yy", 4, 0, 0, 2, 2, 3, 3, 5, 5),
|
|
95
|
+
new Test("\\B", "x", 0),
|
|
96
|
+
new Test("\\B", "xx", 1, 1, 1),
|
|
97
|
+
new Test("\\B", "x y", 0),
|
|
98
|
+
new Test("\\B", "xx yy", 2, 1, 1, 4, 4),
|
|
99
|
+
// RE2 tests
|
|
100
|
+
new Test("[^\\S\\s]", "abcd", 0),
|
|
101
|
+
new Test("[^\\S[:space:]]", "abcd", 0),
|
|
102
|
+
new Test("[^\\D\\d]", "abcd", 0),
|
|
103
|
+
new Test("[^\\D[:digit:]]", "abcd", 0),
|
|
104
|
+
new Test("(?i)\\W", "x", 0),
|
|
105
|
+
new Test("(?i)\\W", "k", 0),
|
|
106
|
+
new Test("(?i)\\W", "s", 0),
|
|
107
|
+
// can backslash-escape any punctuation
|
|
108
|
+
new Test("\\!\\\"\\#\\$\\%\\&\\'\\(\\)\\*\\+\\,\\-\\.\\/\\:\\;\\<\\=\\>\\?\\@\\[\\\\\\]\\^\\_\\{\\|\\}\\~", "!\"#$%&'()*+,-./:;<=>?@[\\]^_{|}~", 1, 0, 31),
|
|
109
|
+
new Test("[\\!\\\"\\#\\$\\%\\&\\'\\(\\)\\*\\+\\,\\-\\.\\/\\:\\;\\<\\=\\>\\?\\@\\[\\\\\\]\\^\\_\\{\\|\\}\\~]+", "!\"#$%&'()*+,-./:;<=>?@[\\]^_{|}~", 1, 0, 31),
|
|
110
|
+
new Test("\\`", "`", 1, 0, 1),
|
|
111
|
+
new Test("[\\`]+", "`", 1, 0, 1),
|
|
112
|
+
// long set of matches
|
|
113
|
+
new Test(".", "qwertyuiopasdfghjklzxcvbnm1234567890", 36, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30, 30, 31, 31, 32, 32, 33, 33, 34, 34, 35, 35, 36),
|
|
114
|
+
new Test("(|a)*", "aa", 3, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2),
|
|
115
|
+
];
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.codePointAtOrThrow = exports.codePoint = void 0;
|
|
4
|
+
const codePoint = (v) => {
|
|
5
|
+
const cp = v.codePointAt(0);
|
|
6
|
+
if (cp === undefined) {
|
|
7
|
+
throw new Error("codePoint: empty string");
|
|
8
|
+
}
|
|
9
|
+
return cp;
|
|
10
|
+
};
|
|
11
|
+
exports.codePoint = codePoint;
|
|
12
|
+
const codePointAtOrThrow = (s, i) => {
|
|
13
|
+
const cp = s.codePointAt(i);
|
|
14
|
+
if (cp === undefined) {
|
|
15
|
+
throw new Error(`codePointAt(${i}) returned undefined for ${JSON.stringify(s)}`);
|
|
16
|
+
}
|
|
17
|
+
return cp;
|
|
18
|
+
};
|
|
19
|
+
exports.codePointAtOrThrow = codePointAtOrThrow;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
declare class RE2JSException extends Error {
|
|
2
|
+
/** @param {string} message */
|
|
3
|
+
constructor(message: string);
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* An exception thrown by the parser if the pattern was invalid.
|
|
7
|
+
*/
|
|
8
|
+
declare class RE2JSSyntaxException extends RE2JSException {
|
|
9
|
+
error: string;
|
|
10
|
+
input: string | null;
|
|
11
|
+
/**
|
|
12
|
+
* @param {string} error
|
|
13
|
+
* @param {string|null} [input=null]
|
|
14
|
+
*/
|
|
15
|
+
constructor(error: string, input?: string | null);
|
|
16
|
+
/**
|
|
17
|
+
* Retrieves the description of the error.
|
|
18
|
+
* @returns {string}
|
|
19
|
+
*/
|
|
20
|
+
getDescription(): string;
|
|
21
|
+
/**
|
|
22
|
+
* Retrieves the erroneous regular-expression pattern.
|
|
23
|
+
* @returns {string|null}
|
|
24
|
+
*/
|
|
25
|
+
getPattern(): string | null;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* An exception thrown by the compiler
|
|
29
|
+
*/
|
|
30
|
+
declare class RE2JSCompileException extends RE2JSException {
|
|
31
|
+
/** @param {string} message */
|
|
32
|
+
constructor(message: string);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* An exception thrown by using groups
|
|
36
|
+
*/
|
|
37
|
+
declare class RE2JSGroupException extends RE2JSException {
|
|
38
|
+
/** @param {string} message */
|
|
39
|
+
constructor(message: string);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* An exception thrown by flags
|
|
43
|
+
*/
|
|
44
|
+
declare class RE2JSFlagsException extends RE2JSException {
|
|
45
|
+
/** @param {string} message */
|
|
46
|
+
constructor(message: string);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* An exception thrown for internal engine errors, such as corrupted bytecodes.
|
|
50
|
+
*/
|
|
51
|
+
declare class RE2JSInternalException extends RE2JSException {
|
|
52
|
+
/** @param {string} message */
|
|
53
|
+
constructor(message: string);
|
|
54
|
+
}
|
|
55
|
+
export { RE2JSException, RE2JSSyntaxException, RE2JSCompileException, RE2JSGroupException, RE2JSFlagsException, RE2JSInternalException, };
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RE2JSInternalException = exports.RE2JSFlagsException = exports.RE2JSGroupException = exports.RE2JSCompileException = exports.RE2JSSyntaxException = exports.RE2JSException = void 0;
|
|
4
|
+
class RE2JSException extends Error {
|
|
5
|
+
/** @param {string} message */
|
|
6
|
+
constructor(message) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "RE2JSException";
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
exports.RE2JSException = RE2JSException;
|
|
12
|
+
/**
|
|
13
|
+
* An exception thrown by the parser if the pattern was invalid.
|
|
14
|
+
*/
|
|
15
|
+
class RE2JSSyntaxException extends RE2JSException {
|
|
16
|
+
error;
|
|
17
|
+
input;
|
|
18
|
+
/**
|
|
19
|
+
* @param {string} error
|
|
20
|
+
* @param {string|null} [input=null]
|
|
21
|
+
*/
|
|
22
|
+
constructor(error, input = null) {
|
|
23
|
+
let message = `error parsing regexp: ${error}`;
|
|
24
|
+
if (input) {
|
|
25
|
+
message += `: \`${input}\``;
|
|
26
|
+
}
|
|
27
|
+
super(message);
|
|
28
|
+
this.name = "RE2JSSyntaxException";
|
|
29
|
+
this.message = message;
|
|
30
|
+
/** @type {string} */
|
|
31
|
+
this.error = error;
|
|
32
|
+
/** @type {string|null} */
|
|
33
|
+
this.input = input;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Retrieves the description of the error.
|
|
37
|
+
* @returns {string}
|
|
38
|
+
*/
|
|
39
|
+
getDescription() {
|
|
40
|
+
return this.error;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Retrieves the erroneous regular-expression pattern.
|
|
44
|
+
* @returns {string|null}
|
|
45
|
+
*/
|
|
46
|
+
getPattern() {
|
|
47
|
+
return this.input;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.RE2JSSyntaxException = RE2JSSyntaxException;
|
|
51
|
+
/**
|
|
52
|
+
* An exception thrown by the compiler
|
|
53
|
+
*/
|
|
54
|
+
class RE2JSCompileException extends RE2JSException {
|
|
55
|
+
/** @param {string} message */
|
|
56
|
+
constructor(message) {
|
|
57
|
+
super(message);
|
|
58
|
+
this.name = "RE2JSCompileException";
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.RE2JSCompileException = RE2JSCompileException;
|
|
62
|
+
/**
|
|
63
|
+
* An exception thrown by using groups
|
|
64
|
+
*/
|
|
65
|
+
class RE2JSGroupException extends RE2JSException {
|
|
66
|
+
/** @param {string} message */
|
|
67
|
+
constructor(message) {
|
|
68
|
+
super(message);
|
|
69
|
+
this.name = "RE2JSGroupException";
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
exports.RE2JSGroupException = RE2JSGroupException;
|
|
73
|
+
/**
|
|
74
|
+
* An exception thrown by flags
|
|
75
|
+
*/
|
|
76
|
+
class RE2JSFlagsException extends RE2JSException {
|
|
77
|
+
/** @param {string} message */
|
|
78
|
+
constructor(message) {
|
|
79
|
+
super(message);
|
|
80
|
+
this.name = "RE2JSFlagsException";
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
exports.RE2JSFlagsException = RE2JSFlagsException;
|
|
84
|
+
/**
|
|
85
|
+
* An exception thrown for internal engine errors, such as corrupted bytecodes.
|
|
86
|
+
*/
|
|
87
|
+
class RE2JSInternalException extends RE2JSException {
|
|
88
|
+
/** @param {string} message */
|
|
89
|
+
constructor(message) {
|
|
90
|
+
super(message);
|
|
91
|
+
this.name = "RE2JSInternalException";
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
exports.RE2JSInternalException = RE2JSInternalException;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { RE2 } from "./RE2.js";
|
|
2
|
+
export { RE2JSCompileException, RE2JSException, RE2JSFlagsException, RE2JSGroupException, RE2JSInternalException, RE2JSSyntaxException, } from "./exceptions.js";
|
|
3
|
+
/**
|
|
4
|
+
* A compiled representation of an RE2 regular expression
|
|
5
|
+
*/
|
|
6
|
+
export declare class RE2JS {
|
|
7
|
+
patternInput: string;
|
|
8
|
+
flagsInput: number;
|
|
9
|
+
re2Input: RE2;
|
|
10
|
+
/**
|
|
11
|
+
* Flag: case insensitive matching.
|
|
12
|
+
*/
|
|
13
|
+
static CASE_INSENSITIVE: number;
|
|
14
|
+
/**
|
|
15
|
+
* Flag: dot ({@code .}) matches all characters, including newline.
|
|
16
|
+
*/
|
|
17
|
+
static DOTALL: number;
|
|
18
|
+
/**
|
|
19
|
+
* Flag: multiline matching: {@code ^} and {@code $} match at beginning and end of line, not just
|
|
20
|
+
* beginning and end of input.
|
|
21
|
+
*/
|
|
22
|
+
static MULTILINE: number;
|
|
23
|
+
/**
|
|
24
|
+
* Flag: Unicode groups (e.g. {@code \p{Greek}} ) will be syntax errors.
|
|
25
|
+
*/
|
|
26
|
+
static DISABLE_UNICODE_GROUPS: number;
|
|
27
|
+
/**
|
|
28
|
+
* Returns a literal pattern string for the specified string.
|
|
29
|
+
*
|
|
30
|
+
* @param {string} str The string to be literalized
|
|
31
|
+
* @returns {string} A literal string replacement
|
|
32
|
+
*/
|
|
33
|
+
static quote(str: string): string;
|
|
34
|
+
/**
|
|
35
|
+
* Helper: create new RE2JS with given regex and flags.
|
|
36
|
+
* @param {string} regex
|
|
37
|
+
* @param {number} [flags=0]
|
|
38
|
+
* @returns {RE2JS}
|
|
39
|
+
*/
|
|
40
|
+
static compile(regex: string, flags?: number): RE2JS;
|
|
41
|
+
static validateFlags(flags: number): void;
|
|
42
|
+
static buildRegexWithFlags(regex: string, flags?: number): string;
|
|
43
|
+
/**
|
|
44
|
+
* Matches a string against a regular expression.
|
|
45
|
+
*
|
|
46
|
+
* @param {string} regex the regular expression
|
|
47
|
+
* @param {string} input the input
|
|
48
|
+
* @returns {boolean} true if the regular expression matches the entire input
|
|
49
|
+
* @throws RE2JSSyntaxException if the regular expression is malformed
|
|
50
|
+
*/
|
|
51
|
+
static matches(regex: string, input: string): boolean;
|
|
52
|
+
/**
|
|
53
|
+
* @param {string} pattern
|
|
54
|
+
* @param {number} flags
|
|
55
|
+
*/
|
|
56
|
+
constructor(pattern: string, flags?: number);
|
|
57
|
+
/**
|
|
58
|
+
* Releases memory used by internal caches associated with this pattern.
|
|
59
|
+
*/
|
|
60
|
+
reset(): void;
|
|
61
|
+
/**
|
|
62
|
+
* Returns the flags used in the constructor.
|
|
63
|
+
* @returns {number}
|
|
64
|
+
*/
|
|
65
|
+
flags(): number;
|
|
66
|
+
/**
|
|
67
|
+
* Returns the pattern used in the constructor.
|
|
68
|
+
* @returns {string}
|
|
69
|
+
*/
|
|
70
|
+
pattern(): string;
|
|
71
|
+
re2(): RE2;
|
|
72
|
+
/**
|
|
73
|
+
* Matches a string against a regular expression.
|
|
74
|
+
*
|
|
75
|
+
* @param {string} input the input
|
|
76
|
+
* @returns {boolean} true if the regular expression matches the entire input
|
|
77
|
+
*/
|
|
78
|
+
matches(input: string): boolean;
|
|
79
|
+
/**
|
|
80
|
+
* Tests whether the regular expression matches any part of the input string.
|
|
81
|
+
*
|
|
82
|
+
* @param {string} input - The input string to test against.
|
|
83
|
+
* @returns {boolean} `true` if the pattern is found anywhere in the input, `false` otherwise.
|
|
84
|
+
*/
|
|
85
|
+
test(input: string): boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Tests whether the regular expression matches the ENTIRE input string.
|
|
88
|
+
*
|
|
89
|
+
* @param {string} input - The input string to test against.
|
|
90
|
+
* @returns {boolean} `true` if the exact input string fully matches the pattern, `false` otherwise.
|
|
91
|
+
*/
|
|
92
|
+
testExact(input: string): boolean;
|
|
93
|
+
toString(): string;
|
|
94
|
+
/**
|
|
95
|
+
* Returns the number of capturing groups in this matcher's pattern.
|
|
96
|
+
*/
|
|
97
|
+
groupCount(): number;
|
|
98
|
+
/**
|
|
99
|
+
* Return a map of the capturing groups in this matcher's pattern.
|
|
100
|
+
*/
|
|
101
|
+
namedGroups(): Map<string, number>;
|
|
102
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RE2JS = exports.RE2JSSyntaxException = exports.RE2JSInternalException = exports.RE2JSGroupException = exports.RE2JSFlagsException = exports.RE2JSException = exports.RE2JSCompileException = void 0;
|
|
4
|
+
const RE2Flags_js_1 = require("./RE2Flags.js");
|
|
5
|
+
const MachineInput_js_1 = require("./MachineInput.js");
|
|
6
|
+
const RE2_js_1 = require("./RE2.js");
|
|
7
|
+
const Utils_js_1 = require("./Utils.js");
|
|
8
|
+
const exceptions_js_1 = require("./exceptions.js");
|
|
9
|
+
var exceptions_js_2 = require("./exceptions.js");
|
|
10
|
+
Object.defineProperty(exports, "RE2JSCompileException", { enumerable: true, get: function () { return exceptions_js_2.RE2JSCompileException; } });
|
|
11
|
+
Object.defineProperty(exports, "RE2JSException", { enumerable: true, get: function () { return exceptions_js_2.RE2JSException; } });
|
|
12
|
+
Object.defineProperty(exports, "RE2JSFlagsException", { enumerable: true, get: function () { return exceptions_js_2.RE2JSFlagsException; } });
|
|
13
|
+
Object.defineProperty(exports, "RE2JSGroupException", { enumerable: true, get: function () { return exceptions_js_2.RE2JSGroupException; } });
|
|
14
|
+
Object.defineProperty(exports, "RE2JSInternalException", { enumerable: true, get: function () { return exceptions_js_2.RE2JSInternalException; } });
|
|
15
|
+
Object.defineProperty(exports, "RE2JSSyntaxException", { enumerable: true, get: function () { return exceptions_js_2.RE2JSSyntaxException; } });
|
|
16
|
+
/**
|
|
17
|
+
* A compiled representation of an RE2 regular expression
|
|
18
|
+
*/
|
|
19
|
+
class RE2JS {
|
|
20
|
+
patternInput;
|
|
21
|
+
flagsInput;
|
|
22
|
+
re2Input;
|
|
23
|
+
/**
|
|
24
|
+
* Flag: case insensitive matching.
|
|
25
|
+
*/
|
|
26
|
+
static CASE_INSENSITIVE = 1;
|
|
27
|
+
/**
|
|
28
|
+
* Flag: dot ({@code .}) matches all characters, including newline.
|
|
29
|
+
*/
|
|
30
|
+
static DOTALL = 2;
|
|
31
|
+
/**
|
|
32
|
+
* Flag: multiline matching: {@code ^} and {@code $} match at beginning and end of line, not just
|
|
33
|
+
* beginning and end of input.
|
|
34
|
+
*/
|
|
35
|
+
static MULTILINE = 4;
|
|
36
|
+
/**
|
|
37
|
+
* Flag: Unicode groups (e.g. {@code \p{Greek}} ) will be syntax errors.
|
|
38
|
+
*/
|
|
39
|
+
static DISABLE_UNICODE_GROUPS = 8;
|
|
40
|
+
/**
|
|
41
|
+
* Returns a literal pattern string for the specified string.
|
|
42
|
+
*
|
|
43
|
+
* @param {string} str The string to be literalized
|
|
44
|
+
* @returns {string} A literal string replacement
|
|
45
|
+
*/
|
|
46
|
+
static quote(str) {
|
|
47
|
+
return (0, Utils_js_1.quoteMeta)(str);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Helper: create new RE2JS with given regex and flags.
|
|
51
|
+
* @param {string} regex
|
|
52
|
+
* @param {number} [flags=0]
|
|
53
|
+
* @returns {RE2JS}
|
|
54
|
+
*/
|
|
55
|
+
static compile(regex, flags = 0) {
|
|
56
|
+
return new RE2JS(regex, flags);
|
|
57
|
+
}
|
|
58
|
+
static validateFlags(flags) {
|
|
59
|
+
if ((flags &
|
|
60
|
+
~(RE2JS.MULTILINE |
|
|
61
|
+
RE2JS.DOTALL |
|
|
62
|
+
RE2JS.CASE_INSENSITIVE |
|
|
63
|
+
RE2JS.DISABLE_UNICODE_GROUPS)) !==
|
|
64
|
+
0) {
|
|
65
|
+
throw new exceptions_js_1.RE2JSFlagsException("Flags should only be a combination of MULTILINE, DOTALL, CASE_INSENSITIVE, DISABLE_UNICODE_GROUPS");
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
static buildRegexWithFlags(regex, flags = 0) {
|
|
69
|
+
let fregex = regex;
|
|
70
|
+
if ((flags & RE2JS.CASE_INSENSITIVE) !== 0) {
|
|
71
|
+
fregex = `(?i)${fregex}`;
|
|
72
|
+
}
|
|
73
|
+
if ((flags & RE2JS.DOTALL) !== 0) {
|
|
74
|
+
fregex = `(?s)${fregex}`;
|
|
75
|
+
}
|
|
76
|
+
if ((flags & RE2JS.MULTILINE) !== 0) {
|
|
77
|
+
fregex = `(?m)${fregex}`;
|
|
78
|
+
}
|
|
79
|
+
return fregex;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Matches a string against a regular expression.
|
|
83
|
+
*
|
|
84
|
+
* @param {string} regex the regular expression
|
|
85
|
+
* @param {string} input the input
|
|
86
|
+
* @returns {boolean} true if the regular expression matches the entire input
|
|
87
|
+
* @throws RE2JSSyntaxException if the regular expression is malformed
|
|
88
|
+
*/
|
|
89
|
+
static matches(regex, input) {
|
|
90
|
+
return RE2JS.compile(regex).testExact(input);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* @param {string} pattern
|
|
94
|
+
* @param {number} flags
|
|
95
|
+
*/
|
|
96
|
+
constructor(pattern, flags = 0) {
|
|
97
|
+
let re2Flags = RE2Flags_js_1.PERL;
|
|
98
|
+
if ((flags & RE2JS.DISABLE_UNICODE_GROUPS) !== 0) {
|
|
99
|
+
re2Flags &= ~RE2Flags_js_1.UNICODE_GROUPS;
|
|
100
|
+
}
|
|
101
|
+
RE2JS.validateFlags(flags);
|
|
102
|
+
const fregex = RE2JS.buildRegexWithFlags(pattern, flags);
|
|
103
|
+
this.patternInput = pattern;
|
|
104
|
+
this.flagsInput = flags;
|
|
105
|
+
this.re2Input = RE2_js_1.RE2.compileImpl(fregex, re2Flags);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Releases memory used by internal caches associated with this pattern.
|
|
109
|
+
*/
|
|
110
|
+
reset() {
|
|
111
|
+
this.re2Input.reset();
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Returns the flags used in the constructor.
|
|
115
|
+
* @returns {number}
|
|
116
|
+
*/
|
|
117
|
+
flags() {
|
|
118
|
+
return this.flagsInput;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Returns the pattern used in the constructor.
|
|
122
|
+
* @returns {string}
|
|
123
|
+
*/
|
|
124
|
+
pattern() {
|
|
125
|
+
return this.patternInput;
|
|
126
|
+
}
|
|
127
|
+
re2() {
|
|
128
|
+
return this.re2Input;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Matches a string against a regular expression.
|
|
132
|
+
*
|
|
133
|
+
* @param {string} input the input
|
|
134
|
+
* @returns {boolean} true if the regular expression matches the entire input
|
|
135
|
+
*/
|
|
136
|
+
matches(input) {
|
|
137
|
+
return this.testExact(input);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Tests whether the regular expression matches any part of the input string.
|
|
141
|
+
*
|
|
142
|
+
* @param {string} input - The input string to test against.
|
|
143
|
+
* @returns {boolean} `true` if the pattern is found anywhere in the input, `false` otherwise.
|
|
144
|
+
*/
|
|
145
|
+
test(input) {
|
|
146
|
+
return this.re2Input.match(input);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Tests whether the regular expression matches the ENTIRE input string.
|
|
150
|
+
*
|
|
151
|
+
* @param {string} input - The input string to test against.
|
|
152
|
+
* @returns {boolean} `true` if the exact input string fully matches the pattern, `false` otherwise.
|
|
153
|
+
*/
|
|
154
|
+
testExact(input) {
|
|
155
|
+
return (this.re2Input.executeEngine((0, MachineInput_js_1.fromUTF16)(input), 0, RE2Flags_js_1.ANCHOR_BOTH, 0) !== null);
|
|
156
|
+
}
|
|
157
|
+
toString() {
|
|
158
|
+
return this.patternInput;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Returns the number of capturing groups in this matcher's pattern.
|
|
162
|
+
*/
|
|
163
|
+
groupCount() {
|
|
164
|
+
return this.re2Input.numberOfCapturingGroups();
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Return a map of the capturing groups in this matcher's pattern.
|
|
168
|
+
*/
|
|
169
|
+
namedGroups() {
|
|
170
|
+
return this.re2Input.namedGroups;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
exports.RE2JS = RE2JS;
|