45 lines
1006 B
TypeScript
45 lines
1006 B
TypeScript
/**
|
|
* tokenize a command argument string, honoring double and single quotes so
|
|
* args with spaces can come through as a single token.
|
|
*
|
|
* parse('set "Solarized Dark"') -> ['set', 'Solarized Dark']
|
|
*
|
|
* an unterminated quote just runs to end-of-input (lenient, so someone
|
|
* mid-typing never hits a hard error from the tokenizer itself).
|
|
*/
|
|
export function tokenize(input: string): string[] {
|
|
const tokens: string[] = [];
|
|
let current = '';
|
|
let quote: '"' | "'" | null = null;
|
|
let hasToken = false;
|
|
|
|
for (let i = 0; i < input.length; i++) {
|
|
const ch = input[i];
|
|
if (quote) {
|
|
if (ch === quote) {
|
|
quote = null;
|
|
} else {
|
|
current += ch;
|
|
}
|
|
continue;
|
|
}
|
|
if (ch === '"' || ch === "'") {
|
|
quote = ch;
|
|
hasToken = true;
|
|
continue;
|
|
}
|
|
if (ch === ' ' || ch === '\t') {
|
|
if (hasToken) {
|
|
tokens.push(current);
|
|
current = '';
|
|
hasToken = false;
|
|
}
|
|
continue;
|
|
}
|
|
current += ch;
|
|
hasToken = true;
|
|
}
|
|
if (hasToken) tokens.push(current);
|
|
return tokens;
|
|
}
|