blob: e47844a638dfc2a87d869d86500a22c449863ecf (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
import { TokenType, Lex, Token } from "../../ts-lang";
const WHITESPACE_TOKENS = [
TokenType.SPACE,
TokenType.COMMA,
TokenType.SEMICOLON,
] as string[];
const _lex = (raw: string): Token[] => {
let _raw: string = raw;
let nameCollection = "";
const tokens: Token[] = [];
while (_raw) {
const head = _raw[0];
_raw = _raw.slice(1);
const processNameCollection = () => {
if (nameCollection) {
tokens.push({ type: TokenType.NAME, name: nameCollection });
nameCollection = "";
}
};
if (WHITESPACE_TOKENS.includes(head)) {
processNameCollection();
continue;
}
if (head === TokenType.OPEN_PAREN) {
processNameCollection();
tokens.push({ type: TokenType.OPEN_PAREN, name: "" });
continue;
}
if (head === TokenType.CLOSE_PAREN) {
processNameCollection();
tokens.push({ type: TokenType.CLOSE_PAREN, name: "" });
continue;
}
nameCollection += head;
}
return tokens;
};
export const lex = <const Raw extends string>(raw: Raw): Lex<Raw> =>
_lex(`${raw};`) as Lex<Raw>;
|