Skip to content

Commit 349a34d

Browse files
committed
Implement argument parsing with escaping.
1 parent ccb1e86 commit 349a34d

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

src/parser.ts

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,52 @@ function rstripSpace(input: string) {
262262

263263
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
264264
function parseEscapedArgs(input: string, index: number, count: number): [string[], number, string | undefined] {
265-
// TODO
266-
return [[], index, 'Internal error: escaped arguments unsupported'];
265+
const result: string[] = [];
266+
let parameter_count = count;
267+
const escapeOrComma = /\\(.)| *(,) */g;
268+
while (parameter_count > 1) {
269+
parameter_count -= 1;
270+
const value: string[] = [];
271+
/* eslint-disable-next-line no-constant-condition */
272+
while (true) {
273+
escapeOrComma.lastIndex = index;
274+
const match = escapeOrComma.exec(input);
275+
if (!match) {
276+
result.push(value.join(''));
277+
return [result, index, `Cannot find comma separating parameter ${count - parameter_count} from the next one`];
278+
}
279+
if (match.index > index) {
280+
value.push(input.substring(index, match.index));
281+
}
282+
index = match.index + match[0].length;
283+
if (match[1] === undefined) {
284+
break;
285+
}
286+
value.push(match[1]);
287+
}
288+
result.push(value.join(''));
289+
}
290+
const escapeOrClosing = /\\(.)|([)])/g;
291+
const value: string[] = [];
292+
/* eslint-disable-next-line no-constant-condition */
293+
while (true) {
294+
escapeOrClosing.lastIndex = index;
295+
const match = escapeOrClosing.exec(input);
296+
if (!match) {
297+
result.push(value.join(''));
298+
return [result, index, 'Cannot find ")" closing after the last parameter'];
299+
}
300+
if (match.index > index) {
301+
value.push(input.substring(index, match.index));
302+
}
303+
index = match.index + match[0].length;
304+
if (match[1] === undefined) {
305+
break;
306+
}
307+
value.push(match[1]);
308+
}
309+
result.push(value.join(''));
310+
return [result, index, undefined];
267311
}
268312

269313
function parseUnescapedArgs(input: string, index: number, count: number): [string[], number, string | undefined] {

0 commit comments

Comments
 (0)