Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 184 lines 6.1 KB
Raw
1 const compilesAsUnicode = (pattern: string): boolean => {
2 try {
3 new RegExp(pattern, 'u');
4 return true;
5 } catch {
6 return false;
7 }
8 };
9
10 const REGEX_SYNTAX_CHARACTERS = new Set('^$\\.*+?()[]{}|/');
11 const CHARACTER_CLASS_ESCAPES = new Set('dDwWsS');
12 const CONTROL_ESCAPES = new Set('fnrtv');
13 const HEX_DIGIT = /^[0-9A-Fa-f]$/;
14 const QUANTIFIER = /^\{\d+(?:,\d*)?\}/;
15
16 const hexEscape = (code: number): string => `\\x${code.toString(16).padStart(2, '0')}`;
17
18 /** Capturing group count and whether any is named, as the parser counts them. */
19 const scanGroups = (pattern: string): { count: number; named: boolean } => {
20 let count = 0;
21 let named = false;
22 let inClass = false;
23 for (let index = 0; index < pattern.length; index++) {
24 const char = pattern[index];
25 if (char === '\\') {
26 index++;
27 } else if (inClass) {
28 inClass = char !== ']';
29 } else if (char === '[') {
30 inClass = true;
31 } else if (char === '(') {
32 if (pattern[index + 1] !== '?') {
33 count++;
34 } else if (pattern[index + 2] === '<' && !'=!'.includes(pattern[index + 3] ?? '')) {
35 count++;
36 named = true;
37 }
38 }
39 }
40 return { count, named };
41 };
42
43 /**
44 * A legacy octal escape (a `\` digit sequence that is not a backreference) as
45 * a hex escape. Returns the replacement and how many digits it consumed.
46 */
47 const legacyOctalEscape = (pattern: string, index: number): [string, number] => {
48 const first = pattern[index];
49 if (first === '8' || first === '9') {
50 return [first, 1];
51 }
52 const maxDigits = first <= '3' ? 3 : 2;
53 let digits = first;
54 while (digits.length < maxDigits && /^[0-7]$/.test(pattern[index + digits.length] ?? '')) {
55 digits += pattern[index + digits.length];
56 }
57 return [hexEscape(parseInt(digits, 8)), digits.length];
58 };
59
60 /**
61 * The interpreter compiles every pattern with the `u` flag, while
62 * `compilePattern` and the native parsers deliberately do not. This spells a
63 * pattern written for the legacy (Annex B) grammar in the Unicode grammar with
64 * the same meaning: identity escapes such as `\_`, `\:` or `\k` become the
65 * literal character, incomplete `\x`, `\u` and `\c` escapes and legacy octal
66 * escapes are spelled out, lone `{`, `}` and `]` are escaped, and a hyphen
67 * next to a class escape (`[\w-.]`), which is a literal there, is escaped.
68 *
69 * Returns `undefined` for the few legacy constructs with no Unicode spelling,
70 * such as a quantified lookahead. Matching differs only for astral
71 * characters, which `.` and negated classes treat as one character, not two.
72 */
73 export const toUnicodePattern = (pattern: string): string | undefined => {
74 const { count: groupCount, named: hasNamedGroups } = scanGroups(pattern);
75
76 let rewritten = '';
77 let inClass = false;
78 let previousAtomWasClassEscape = false;
79 let index = 0;
80 while (index < pattern.length) {
81 const char = pattern[index];
82
83 if (char !== '\\' && inClass) {
84 if (char === ']') {
85 inClass = false;
86 rewritten += char;
87 } else if (
88 char === '-' &&
89 (previousAtomWasClassEscape ||
90 (pattern[index + 1] === '\\' && CHARACTER_CLASS_ESCAPES.has(pattern[index + 2] ?? '')))
91 ) {
92 rewritten += '\\-';
93 } else {
94 rewritten += char;
95 }
96 previousAtomWasClassEscape = false;
97 index++;
98 continue;
99 }
100
101 if (char !== '\\') {
102 const quantifier = char === '{' ? QUANTIFIER.exec(pattern.slice(index)) : null;
103 if (char === '[') {
104 inClass = true;
105 const opening = pattern[index + 1] === '^' ? '[^' : '[';
106 rewritten += opening;
107 index += opening.length;
108 } else if (quantifier) {
109 rewritten += quantifier[0];
110 index += quantifier[0].length;
111 } else {
112 rewritten += char === '{' || char === '}' || char === ']' ? `\\${char}` : char;
113 index++;
114 }
115 continue;
116 }
117
118 const escaped = pattern[index + 1];
119 if (escaped === undefined) {
120 return undefined;
121 }
122 previousAtomWasClassEscape = false;
123
124 if (CHARACTER_CLASS_ESCAPES.has(escaped)) {
125 previousAtomWasClassEscape = inClass;
126 rewritten += `\\${escaped}`;
127 index += 2;
128 } else if (
129 CONTROL_ESCAPES.has(escaped) ||
130 REGEX_SYNTAX_CHARACTERS.has(escaped) ||
131 escaped === 'b' ||
132 (escaped === 'B' && !inClass) ||
133 (escaped === '-' && inClass) ||
134 (escaped === 'k' && hasNamedGroups && !inClass)
135 ) {
136 rewritten += `\\${escaped}`;
137 index += 2;
138 } else if (escaped === 'c') {
139 const letter = pattern[index + 2] ?? '';
140 if (/^[A-Za-z]$/.test(letter)) {
141 rewritten += `\\c${letter}`;
142 index += 3;
143 } else if (inClass && /^[0-9_]$/.test(letter)) {
144 rewritten += hexEscape(letter.charCodeAt(0) % 32);
145 index += 3;
146 } else {
147 // Without a control letter, `\c` is a literal backslash and the `c`
148 // that follows is an ordinary character.
149 rewritten += '\\\\';
150 index++;
151 }
152 } else if (escaped === 'x' || escaped === 'u') {
153 const length = escaped === 'x' ? 2 : 4;
154 const digits = pattern.slice(index + 2, index + 2 + length);
155 if (digits.length === length && [...digits].every(digit => HEX_DIGIT.test(digit))) {
156 rewritten += `\\${escaped}${digits}`;
157 index += 2 + length;
158 } else {
159 rewritten += escaped;
160 index += 2;
161 }
162 } else if (/^[0-9]$/.test(escaped)) {
163 const decimal = /^\d+/.exec(pattern.slice(index + 1))?.[0] ?? escaped;
164 if (escaped === '0' && !/^[0-9]$/.test(pattern[index + 2] ?? '')) {
165 rewritten += '\\0';
166 index += 2;
167 } else if (!inClass && escaped !== '0' && Number(decimal) <= groupCount) {
168 rewritten += `\\${decimal}`;
169 index += 1 + decimal.length;
170 } else {
171 const [replacement, consumed] = legacyOctalEscape(pattern, index + 1);
172 rewritten += replacement;
173 index += 1 + consumed;
174 }
175 } else {
176 // Every other escape is an identity escape: the character itself, which
177 // is not special where it stands.
178 rewritten += escaped;
179 index += 2;
180 }
181 }
182
183 return compilesAsUnicode(rewritten) ? rewritten : undefined;
184 };