-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenizer.js
287 lines (240 loc) · 6.42 KB
/
tokenizer.js
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
var Token = require("./token");
var Position = require("./position");
// TODO Implement comments
// these characters are treated as separate tokens
// even if not surrounded by whitespace
var punctuationChars = "(){}[].,;:@";
var specialOperators = ["::", "..."];
function tokenize(inputString) {
var tokens = [];
// Add a new line to the start of the input to
// ensure that the token array starts with an indent
var result = {
rest: "\n" + inputString,
position: Position(0, 0),
};
do {
// getNextToken returns an object
// with "token" and "rest" properties
result = getNextToken(result.rest, result.position);
if (result.token) {
tokens.push(result.token);
}
} while (result.rest.length > 0);
// Add 4 end-of-file tokens because some parts of the
// parser may look ahead up to four tokens, and will
// error if the token is null
tokens.push(Token.eof(), Token.eof(), Token.eof(), Token.eof());
// store a link to the token array and each token's position in it
// as a property, so they can navigate relatively between tokens
tokens.forEach(function (token, i) {
token.index = i;
token.tokenArray = tokens;
return token;
});
return tokens;
}
function getNextToken(str, position) {
return (
whiteSpace(str, position) ||
indent(str, position) ||
comment(str, position) ||
specialOperator(str, position) ||
punctuation(str, position) ||
numberLiteral(str, position) ||
regexLiteral(str, position) ||
stringLiteral(str, position) ||
identifier(str, position)
);
}
function whiteSpace(str, position) {
if (isWhiteSpaceChar(str.charAt(0))) {
var taken = takeStringWhile(str, isWhiteSpaceChar);
return {
token: null,
rest: taken.rest,
position: position.moveColumn(taken.head.length),
};
}
else return false;
}
function indent(str, position) {
var taken = false;
while (isNewLineChar(str.charAt(0))) {
position = position.newLine();
taken = takeStringWhile(str.slice(1), isWhiteSpaceChar);
str = taken.rest;
}
if (taken) {
return {
token: Token.indent(taken.head, position),
rest: str,
position: position.moveColumn(taken.head.length),
};
}
else return false;
}
function comment(str, position) {
if (str.charAt(0) === "#") {
var taken = takeStringWhile(str.slice(1), isCommentChar);
if (taken.rest.charAt(0) === "#") return {
token: Token.comment("#" + taken.head + "#", position),
rest: taken.rest.slice(1),
position: position.moveColumn(taken.head.length + 2),
};
else return {
token: Token.comment("#" + taken.head, position),
rest: taken.rest,
position: position.moveColumn(taken.head.length + 1),
};
}
else return false;
}
function specialOperator(str, position) {
return specialOperators.reduce(
function (alreadyFound, operator) {
if (alreadyFound) return alreadyFound;
if (str.slice(0, operator.length) === operator) {
return {
token: Token.identifier(operator, position),
rest: str.slice(operator.length),
position: position.moveColumn(operator.length),
};
}
}, false);
}
function punctuation(str, position) {
if (isPunctuationChar(str.charAt(0))) {
return {
token: Token.punctuation(str.charAt(0), position),
rest: str.slice(1),
position: position.moveColumn(1),
};
}
else return false;
}
function numberLiteral(str, position) {
if (isNumericChar(str.charAt(0))) {
var taken = takeStringWhile(str, isNumericChar);
return {
token: Token.number(taken.head, position),
rest: taken.rest,
position: position.moveColumn(taken.head.length),
};
}
else return false;
}
function stringLiteral(str, position, isRegex) {
var delim = str.charAt(0);
if (isRegex || isStringDelimiterChar(delim)) {
var taken = takeStringWhile(
str.slice(1), isNotDelimiter(delim), "escape");
var wrappedString = delim + taken.head + delim;
return {
token: isRegex ?
Token.regex(wrappedString, position) :
Token.string(wrappedString, position),
rest: taken.rest.slice(1),
position: position.moveColumn(wrappedString.length),
};
}
else return false;
}
function regexLiteral(str, position) {
if (str.charAt(0) === "/") {
// if the opening slash is followed by whitespace,
// it is a division symbol, so fall through to make
// it an identifier
if (/\s/.test(str.charAt(1))) {
return false;
}
// double slash is an invalid regex because it is a comment in JS
else if (str.charAt(1) === "/") {
position.error("Empty regular expression literal");
}
return stringLiteral(str, position, "regex");
}
else return false;
}
function identifier(str, position) {
var token;
var taken = takeStringWhile(str, isIdentifierChar);
var name = taken.head;
if (!isNaN(Number(name))) {
token = Token.number(name, position);
}
else if (name === "true" || name === "false") {
token = Token.boolean(name, position);
}
else {
token = Token.identifier(name, position);
}
return {
token: token,
rest: taken.rest,
position: position.moveColumn(taken.head.length),
};
}
// matches whitespace characters other than new lines
function isWhiteSpaceChar(ch) {
return (/\s/).test(ch) && !isNewLineChar(ch);
}
// matches whitespace characters other than new lines
function isNewLineChar(ch) {
return ch === "\n";
}
function isCommentChar(ch) {
return ch !== "\n" && ch !== "#";
}
// matches punctuation characters defined in the string at the top
function isPunctuationChar(ch) {
return punctuationChars.indexOf(ch) >= 0;
}
function isNumericChar(ch) {
return (/[0-9]/).test(ch);
}
function isStringDelimiterChar(ch) {
return ch === "\"" || ch === "\'";
}
function isIdentifierChar(ch) {
return (!isNewLineChar(ch)) &&
(!isPunctuationChar(ch)) &&
(!isWhiteSpaceChar(ch)) &&
(!isStringDelimiterChar(ch));
}
function isNotDelimiter(delimiter) {
return function (ch, i, str) {
if (ch === delimiter) {
// count the number of backslashes preceding the delimiter
var backslashCount = 0;
while (str.charAt(i - backslashCount - 1) === "\\") {
backslashCount += 1;
}
return isOdd(backslashCount);
}
else return true;
};
}
function isEven(x) {
return x % 2 === 0;
}
function isOdd(x) {
return x % 2 === 1;
}
function not(predicate) {
return function () {
return !predicate.apply(this, arguments);
};
}
function takeStringWhile(str, predicate, escape) {
var i = 0;
var backslashCount = 0;
while (i < str.length && predicate(str.charAt(i), i, str)) {
i += 1;
}
return {
head: str.slice(0, i),
rest: str.slice(i)
};
}
module.exports = tokenize;