-
Notifications
You must be signed in to change notification settings - Fork 9
/
tokenizer.js
169 lines (136 loc) · 5.57 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
self.tsh.Token = function(
file, code, kind, startLine, startLineOffset, startOffset, stopLine, stopLineOffset, stopOffset
) {
this.file = file;
this.code = code;
this.kind = kind;
this.startLine = startLine;
this.startLineOffset = startLineOffset;
this.startOffset = startOffset;
this.stopLine = stopLine;
this.stopLineOffset = stopLineOffset;
this.stopOffset = stopOffset;
};
self.tsh.Token.end = (file, code) => new self.tsh.Token(file, code, "end", 0, 0, 0, 0, 0, 0);
self.tsh.tokenize = (file, code) => {
var tokens = [];
var line = 1;
var lineOffset = 0;
var operatorCharactersString = "!@#$%&/=?+|^~*<>.:-,;";
var operatorCharacters = {};
for(var j = 0; j < operatorCharactersString.length; j++) operatorCharacters[operatorCharactersString[j]] = true;
function token(kind, startOffset, stopOffset) {
return new self.tsh.Token(
file, code, kind, startLine, startLineOffset, startOffset, line, lineOffset, stopOffset
);
}
for(var i = 0; i < code.length;) {
while(code[i] === ' ' || code[i] === '\t' || code[i] === '\r') i++;
var start = i;
var startLine = line;
var startLineOffset = lineOffset;
if(i === lineOffset && code[i] !== "\n" &&
code[i] !== "}" && code[i] !== "]" && code[i] !== ")" && code[i] !== "|" && code[i] !== "/"
) {
tokens.push(token("top", i, i));
}
if(code[i] === '\n') {
i += 1;
line += 1;
lineOffset = i;
} else if(code[i] === '/' && code[i + 1] === '/') {
i += 2;
while(code[i] !== '\n' && i < code.length) i += 1;
} else if(code[i] === '/' && code[i + 1] === '*') {
i += 2;
while(code[i] !== '*' || code[i + 1] !== '/') {
if(i >= code.length) {
throw 'Expected end of comment started on line ' + startLine + ', got end of file.';
}
if(code[i] === '\n') {
line += 1;
lineOffset = i + 1;
}
i += 1;
}
i += 2;
} else if(code[i] === '"') {
i += 1;
while(code[i] !== '"') {
if(code[i] === '\n') {
throw 'Unexpected end of line in string started on line ' + startLine + '.';
}
if(i >= code.length) {
throw 'Expected end of string started on line ' + startLine + ', got end of file.';
}
if(code[i] === '\\' && code[i + 1] !== '\n') i += 1;
i += 1;
}
i += 1;
tokens.push(token("string", start, i));
} else if((code[i] >= 'a' && code[i] <= 'z') || (code[i] >= 'A' && code[i] <= 'Z')) {
var kind = code[i] >= 'a' ? "lower" : "upper";
i += 1;
while(
(code[i] >= 'a' && code[i] <= 'z') ||
(code[i] >= 'A' && code[i] <= 'Z') ||
(code[i] >= '0' && code[i] <= '9')
) i += 1;
tokens.push(token(kind, start, i));
} else if(code[i] >= '0' && code[i] <= '9') {
var dot = false;
var exponent = false;
while(code[i] >= '0' && code[i] <= '9') {
i += 1;
if((code[i] === 'e' || code[i] === 'E') && !exponent) {
i += 1;
dot = true;
exponent = true;
if(code[i] === '+' || code[i] === '-') i += 1;
}
if(code[i] === '.' && !dot && !exponent) {
i += 1;
dot = true;
}
}
tokens.push(token(dot || exponent ? "float" : "int", start, i));
} else if(code[i] === '_') {
i += 1;
tokens.push(token("definition", start, i));
} else if(operatorCharacters[code[i]] === true) {
i += 1;
while(operatorCharacters[code[i]] === true) i += 1;
var op = code.slice(start, i);
var operatorKind =
op === "=" || op === "<-" || op === "->" ||
op === "," || op === ";" ||
op === "." || op === ".?" ||
op === "~" ||
op === "..." || op === ":" || op === "::" || op === "=>" || op === "?" ?
"separator" : "operator";
tokens.push(token(operatorKind, start, i));
} else if(
code[i] === '(' || code[i] === ')' ||
code[i] === '[' || code[i] === ']' ||
code[i] === '{' || code[i] === '}'
) {
i += 1;
tokens.push(token("bracket", start, i));
} else if(i < code.length) {
throw 'Unexpected character: ' + code[i];
}
}
for(var k = 0; k + 1 < tokens.length; k++) {
var previous = k === 0 ? "" : code.slice(tokens[k - 1].startOffset, tokens[k - 1].stopOffset);
var next = code.slice(tokens[k + 1].startOffset, tokens[k + 1].stopOffset);
var isField = previous === "{" || previous === "." || previous === ".?" || previous === ",";
if(tokens[k].kind === "lower" && (next === "=" || next === "<-" || next === "->")) {
tokens[k].kind = "definition";
} else if(tokens[k].kind === "lower" && !isField && next === ":") {
tokens[k].kind = "definition"
} else if(tokens[k].kind === "upper" && next === "@") {
tokens[k].kind = "definition"
}
}
return tokens;
};