-
Notifications
You must be signed in to change notification settings - Fork 774
Expand file tree
/
Copy pathtokenizer.ts
More file actions
194 lines (166 loc) · 6.51 KB
/
tokenizer.ts
File metadata and controls
194 lines (166 loc) · 6.51 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { Error, ErrorHandler } from './error-handler';
import { Comment, Scanner, SourceLocation } from './scanner';
import { Token, TokenName } from './token';
export type ReaderEntry = string | null;
export interface BufferEntry {
type: string;
value: string;
regex?: {
pattern: string;
flags: string;
};
range?: [number, number];
loc?: SourceLocation;
}
export class Reader {
readonly values: ReaderEntry[];
curly: number;
paren: number;
constructor() {
this.values = [];
this.curly = this.paren = -1;
}
// A function following one of those tokens is an expression.
beforeFunctionExpression(t: string): boolean {
return ['(', '{', '[', 'in', 'typeof', 'instanceof', 'new',
'return', 'case', 'delete', 'throw', 'void',
// assignment operators
'=', '+=', '-=', '*=', '**=', '/=', '%=', '<<=', '>>=', '>>>=',
'&=', '|=', '^=', ',',
// binary/unary operators
'+', '-', '*', '**', '/', '%', '++', '--', '<<', '>>', '>>>', '&',
'|', '^', '!', '~', '&&', '||', '?', ':', '===', '==', '>=',
'<=', '<', '>', '!=', '!=='].indexOf(t) >= 0;
}
// Determine if forward slash (/) is an operator or part of a regular expression
// https://github.com/mozilla/sweet.js/wiki/design
isRegexStart() {
const previous = this.values[this.values.length - 1];
let regex = (previous !== null);
switch (previous) {
case 'this':
case ']':
regex = false;
break;
case ')':
const keyword = this.values[this.paren - 1];
regex = (keyword === 'if' || keyword === 'while' || keyword === 'for' || keyword === 'with');
break;
case '}':
// Dividing a function by anything makes little sense,
// but we have to check for that.
regex = false;
if (this.values[this.curly - 3] === 'function') {
// Anonymous function, e.g. function(){} /42
const check = this.values[this.curly - 4];
regex = check ? !this.beforeFunctionExpression(check) : false;
} else if (this.values[this.curly - 4] === 'function') {
// Named function, e.g. function f(){} /42/
const check = this.values[this.curly - 5];
regex = check ? !this.beforeFunctionExpression(check) : true;
}
break;
default:
break;
}
return regex;
}
push(token): void {
if (token.type === Token.Punctuator || token.type === Token.Keyword) {
if (token.value === '{') {
this.curly = this.values.length;
} else if (token.value === '(') {
this.paren = this.values.length;
}
this.values.push(token.value);
} else {
this.values.push(null);
}
}
}
/* tslint:disable:max-classes-per-file */
export interface Config {
tolerant?: boolean;
comment?: boolean;
range?: boolean;
loc?: boolean;
}
export class Tokenizer {
readonly errorHandler: ErrorHandler;
scanner: Scanner;
readonly trackRange: boolean;
readonly trackLoc: boolean;
readonly buffer: BufferEntry[];
readonly reader: Reader;
constructor(code: string, config: Config) {
this.errorHandler = new ErrorHandler();
this.errorHandler.tolerant = config ? (typeof config.tolerant === 'boolean' && config.tolerant) : false;
this.scanner = new Scanner(code, this.errorHandler);
this.scanner.trackComment = config ? (typeof config.comment === 'boolean' && config.comment) : false;
this.trackRange = config ? (typeof config.range === 'boolean' && config.range) : false;
this.trackLoc = config ? (typeof config.loc === 'boolean' && config.loc) : false;
this.buffer = [];
this.reader = new Reader();
}
errors(): Error[] {
return this.errorHandler.errors;
}
getNextToken() {
if (this.buffer.length === 0) {
const comments: Comment[] = this.scanner.scanComments();
if (this.scanner.trackComment) {
for (let i = 0; i < comments.length; ++i) {
const e: Comment = comments[i];
const value = this.scanner.source.slice(e.slice[0], e.slice[1]);
const comment: BufferEntry = {
type: e.multiLine ? 'BlockComment' : 'LineComment',
value: value
};
if (this.trackRange) {
comment.range = e.range;
}
if (this.trackLoc) {
comment.loc = e.loc;
}
this.buffer.push(comment);
}
}
if (!this.scanner.eof()) {
let loc;
if (this.trackLoc) {
loc = {
start: {
line: this.scanner.lineNumber,
column: this.scanner.index - this.scanner.lineStart
},
end: {}
};
}
const startRegex = (this.scanner.source[this.scanner.index] === '/') && this.reader.isRegexStart();
const token = startRegex ? this.scanner.scanRegExp() : this.scanner.lex();
this.reader.push(token);
const entry: BufferEntry = {
type: TokenName[token.type],
value: this.scanner.source.slice(token.start, token.end)
};
if (this.trackRange) {
entry.range = [token.start, token.end];
}
if (this.trackLoc) {
loc.end = {
line: this.scanner.lineNumber,
column: this.scanner.index - this.scanner.lineStart
};
entry.loc = loc;
}
if (token.type === Token.RegularExpression) {
const pattern = token.pattern as string;
const flags = token.flags as string;
entry.regex = { pattern, flags };
}
this.buffer.push(entry);
}
}
return this.buffer.shift();
}
}