(source: string)
| 18 | ) |
| 19 | |
| 20 | export function tokenize(source: string): Array<Token> { |
| 21 | const tokens: Array<Token> = [] |
| 22 | for (const match of source.matchAll(pattern)) { |
| 23 | if (match[1] === undefined) { |
| 24 | tokens.push({ |
| 25 | text: match[0], |
| 26 | start: match.index, |
| 27 | end: match.index + match[0].length, |
| 28 | kind: match[2] !== undefined |
| 29 | ? "identifier" |
| 30 | : match[3] !== undefined |
| 31 | ? "string" |
| 32 | : match[4] !== undefined |
| 33 | ? "number" |
| 34 | : "punctuation" |
| 35 | }) |
| 36 | } |
| 37 | } |
| 38 | return tokens |
| 39 | } |