/** One token of TypeScript or JavaScript source. Comments and whitespace are dropped, except the doc comment kept on the next token. */ export interface Token { readonly kind: "name" | "punct" | "string" | "number" | "template" | "regex"; readonly text: string; readonly line: number; /** The text of the comment right before this token, when there is one. */ readonly comment?: string; } const NAME_START = /[A-Za-z_$#@]/; const NAME_PART = /[\w$]/; const PUNCT_3 = new Set(["===", "!==", "...", "**=", "<<=", ">>=", ">>>", "&&=", "||=", "??="]); const PUNCT_2 = new Set(["=>", "==", "!=", "<=", ">=", "&&", "||", "??", "?.", "++", "--", "+=", "-=", "*=", "/=", "%=", "&=", "|=", "^=", "**", "<<", ">>"]); /** After these, a slash starts a regular expression, not a division. */ const REGEX_AFTER_PUNCT = new Set(["(", ",", "=", ":", "[", "!", "&", "|", "?", "{", "}", ";", "&&", "||", "??", "=>", "==", "===", "!=", "!==", "+", "-", "*", "%", "<", ">", "~", "^", "return"]); const REGEX_AFTER_WORD = new Set(["return", "typeof", "instanceof", "in", "of", "new", "delete", "void", "throw", "case", "do", "else", "yield", "await"]); /** * Splits source into tokens. It is a lexer for outlines, not a compiler: it knows strings, template literals with * their `${}` code, comments, numbers and regular expressions well enough to find declarations and names. */ export function tokenize(source: string): Token[] { const tokens: Token[] = []; const text = source.replace(/\r\n?/g, "\n"); let i = 0; let line = 1; let comment: string | undefined; const templates: number[] = []; let braces = 0; const push = (kind: Token["kind"], value: string, at: number) => { tokens.push(comment === undefined ? { kind, text: value, line: at } : { kind, text: value, line: at, comment }); comment = undefined; }; const regexAllowed = () => { const last = tokens[tokens.length - 1]; if (!last) return true; if (last.kind === "punct") return REGEX_AFTER_PUNCT.has(last.text); return last.kind === "name" && REGEX_AFTER_WORD.has(last.text); }; const readTemplate = (from: number): number => { let j = from; while (j < text.length) { const c = text[j]; if (c === "\\") { j += 2; continue; } if (c === "`") return j + 1; if (c === "$" && text[j + 1] === "{") { templates.push(braces); return j + 2; } if (c === "\n") line++; j++; } return j; }; while (i < text.length) { const c = text[i]; if (c === "\n") { line++; i++; continue; } if (c === " " || c === "\t" || c === "\f" || c === "\v" || c === " " || c === "") { i++; continue; } if (c === "/" && text[i + 1] === "/") { const end = text.indexOf("\n", i); const stop = end < 0 ? text.length : end; comment = text.slice(i + 2, stop).trim(); i = stop; continue; } if (c === "/" && text[i + 1] === "*") { const end = text.indexOf("*/", i + 2); const stop = end < 0 ? text.length : end + 2; const body = text.slice(i + 2, stop - 2); for (const ch of body) if (ch === "\n") line++; comment = body.replace(/^\*+/, "").split("\n").map((part) => part.replace(/^\s*\*\s?/, "").trim()).filter(Boolean).join(" "); i = stop; continue; } const start = line; if (c === "'" || c === '"') { let j = i + 1; while (j < text.length && text[j] !== c && text[j] !== "\n") j += text[j] === "\\" ? 2 : 1; push("string", text.slice(i, j + 1), start); i = j + 1; continue; } if (c === "`") { const end = readTemplate(i + 1); push("template", text.slice(i, end), start); i = end; continue; } if (c === "}" && templates.length && templates[templates.length - 1] === braces) { templates.pop(); const end = readTemplate(i + 1); push("template", text.slice(i, end), start); i = end; continue; } if (/[0-9]/.test(c) || (c === "." && /[0-9]/.test(text[i + 1] ?? ""))) { let j = i + 1; while (j < text.length && /[\w.]/.test(text[j])) j++; push("number", text.slice(i, j), start); i = j; continue; } if (NAME_START.test(c)) { let j = i + 1; while (j < text.length && NAME_PART.test(text[j])) j++; push("name", text.slice(i, j), start); i = j; continue; } if (c === "/" && text[i + 1] !== ">" && tokens[tokens.length - 1]?.text !== "<" && regexAllowed()) { let j = i + 1; let inClass = false; while (j < text.length && text[j] !== "\n") { if (text[j] === "\\") j += 2; else if (text[j] === "[") (inClass = true), j++; else if (text[j] === "]") (inClass = false), j++; else if (text[j] === "/" && !inClass) break; else j++; } j++; while (j < text.length && /[a-z]/i.test(text[j])) j++; push("regex", text.slice(i, j), start); i = j; continue; } const three = text.slice(i, i + 3); const two = text.slice(i, i + 2); const punct = PUNCT_3.has(three) ? three : PUNCT_2.has(two) ? two : c; if (punct === "{") braces++; if (punct === "}") braces--; push("punct", punct, start); i += punct.length; } return tokens; }