-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenizer.ts
More file actions
179 lines (151 loc) · 4.26 KB
/
tokenizer.ts
File metadata and controls
179 lines (151 loc) · 4.26 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import type {Dictionary} from "dictionary-types";
export interface Token {
readonly type: "word" | "operator";
readonly text: string;
}
export function tokenize(text: string): readonly Token[] {
let state: State = {
mode: "token-boundary",
tokens: [],
text,
position: 0,
token: ""
};
while (state.mode !== "end") {
state = step(state);
}
return state.tokens;
}
interface State {
readonly mode:
| "token-boundary"
| "in-word"
| "in-operator"
| "in-escape"
| "in-single-quote"
| "in-double-quote"
| "end";
readonly tokens: readonly Token[];
readonly text: string;
readonly position: number;
readonly token: string;
}
function step(state: State): State {
if (state.mode === "end") {
throw new Error("Step past end of character stream");
}
if (state.position === state.text.length) {
return {
...delimit(state),
mode: "end"
};
}
const char = state.text.charAt(state.position);
const token = state.token + char;
if (state.mode === "in-operator") {
if (partialOperatorMap[token] ?? false) {
return consumeChar(state);
} else {
return delimit(state);
}
}
if (!quoting(state)) {
if (char === "\\") {
return consumeChar({...state, mode: "in-escape"});
} else if (char === "'") {
return consumeChar({...state, mode: "in-single-quote"});
} else if (char === '"') {
return consumeChar({...state, mode: "in-double-quote"});
}
}
if (!hardQuoting(state)) {
if (char === "$") {
return consumeDollarExpansion(consumeChar(state));
} else if (char === "`") {
throw new Error("Not implemented");
}
}
if (!quoting(state)) {
if (partialOperatorMap[char] ?? false) {
return consumeChar({...delimit(state), mode: "in-operator"});
}
if (char === " " || char === "\t") {
return discardChar(delimit(state));
}
}
if (state.mode === "in-word") {
return consumeChar(state);
}
if (char === "#") {
return discardComment(state);
}
return consumeChar({...state, mode: "in-word"});
}
function delimit(state: State): State {
const tokenType =
state.mode === "in-word" ? "word" : state.mode === "in-operator" ? "operator" : null;
if (tokenType == null) {
return state;
} else {
return {
...state,
mode: "token-boundary",
tokens: state.tokens.concat([
{
type: tokenType,
text: state.token
}
]),
token: ""
};
}
}
const operators = ["<", ">", ">|", ">>", "<<", "<<-", "<&", ">&", "<>", "|", "&&", "||", ";", "&"];
const partialOperatorMap = operators.sort().reduce<Dictionary<boolean>>((map, operator) => {
void operator.split("").reduce((partial, char) => {
const p = partial + char;
map[p] = true;
return p;
}, "");
return map;
}, {});
function discardChar(state: State): State {
return {
...state,
position: state.position + 1
};
}
function consumeChar(state: State): State {
return {
...state,
position: state.position + 1,
token: state.token + state.text.charAt(state.position)
};
}
function quoting(state: State): boolean {
return (
state.mode === "in-escape" ||
state.mode === "in-single-quote" ||
state.mode === "in-double-quote"
);
}
function hardQuoting(state: State): boolean {
return state.mode === "in-escape" || state.mode === "in-single-quote";
}
function discardComment(state: State): State {
let i = state.position;
while (i < state.text.length && state.text.charAt(i) !== "\n") {
++i;
}
return {...state, position: i};
}
function consumeDollarExpansion(state: State): State {
const char = state.text.charAt(state.position);
if (char === "{" || char === "(") {
throw new Error("Not implemented");
}
return {
...state,
mode: quoting(state) ? state.mode : "in-word"
};
}