-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
362 lines (325 loc) · 8.58 KB
/
index.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
'use strict';
/**
* Create a new Scanner with the given `str` and optional `rules`.
*
* ```js
* const Scanner = require('snapdragon-scanner');
* const scanner = new Scanner('var foo = "bar";', {
* rules: {
* space: /^ +/,
* tab: /^\t+/,
* newline: /^\n+/,
* text: /^\w+/,
* equal: /^=/,
* quote: /^["']/,
* semi: /^;/,
* dot: /^\./
* }
* });
* ```
* @name Scanner
* @param {String} `input` Input string to scan.
* @param {Object} `options` (optional) Pass an object of regex patterns on `options.rules`, or use `.addRules()` or `.addRule()` after instantiating.
* @api public
*/
class Scanner {
constructor(str, options = {}) {
assert(typeof str === 'string', 'expected a string');
this.key = Symbol('scanner');
this.options = options;
this.rules = new Map();
this.state = {
consumed: '',
position: 0,
string: str,
input: str,
queue: []
};
if (options.rules) {
this.addRules(options.rules);
}
}
/**
* Add a rule to the scanner.
*
* ```js
* console.log(scanner.token('text', ['foo']);
* //=> { rule: 'text', value: 'foo', match: [foo] };
* ```
* @name .addRule
* @param {String} `rule`
* @param {RegExp} `match` Match array from `RegExp.exec()`.
* @api public
*/
token(type, match) {
if (this.options.Token) {
return new this.options.Token(type, match);
}
return { type, value: match[1] || match[0], match };
}
/**
* Add a rule to the scanner.
*
* ```js
* scanner.addRule(rule, regex);
* // example
* scanner.addRule('text', /^\w+/);
* ```
* @name .addRule
* @param {String} `rule`
* @param {RegExp} `regex` Regular expression to use when [scanning](#scan).
* @api public
*/
addRule(rule, regex) {
if (rule && typeof rule === 'object') {
this.addRule(rule.type, rule.regex);
return this;
}
this.rules.set(rule, [].concat(regex));
return this;
}
/**
* Add an object of rules to the scanner.
*
* ```js
* scanner.addRules({
* text: /^\w+/,
* slash: /^\//,
* dot: /^\./
* });
* ```
* @name .addRules
* @param {Object} `rules`
* @api public
*/
addRules(rules) {
if (Array.isArray(rules)) {
rules.forEach(rule => this.addRule(rule));
return this;
}
for (let type of Object.keys(rules)) {
this.addRule(type, rules[type]);
}
return this;
}
/**
* Attempts to match `scanner.string` with the given regex. Also validates
* the regex to ensure that it starts with `^` since matching should always be
* against the beginning of the string, and throws if the regex matches an empty
* string, to avoid catastrophic backtracking.
*
* ```js
* const scanner = new Scanner('foo/bar', { text: /^\w+/ });
* const match = scanner.match(scanner.rules.get('text'));
* console.log(match);
* //=> [ 'foo', index: 0, input: 'foo/bar', groups: undefined ]
* ```
* @name .match
* @param {RegExp} `regex` (required)
* @return {Array|null} Returns the match array or null from `RegExp.exec`.
* @api public
*/
match(regex) {
if (this.eos()) return null;
assert(regex instanceof RegExp, 'expected a regular expression');
if (regex[this.key] !== true) {
assert(regex.source[0] === '^', 'expected regex to start with "^"');
Reflect.defineProperty(regex, this.key, { value: true });
}
let match = regex.exec(this.state.string);
if (match) {
assert(match[0] !== '', 'unsafe regex: regex should not match an empty string');
match.index = this.state.position;
return match;
}
}
/**
* Remove the given length of substring from `scanner.string`.
*
* ```js
* scanner.consume(1);
* scanner.consume(1, '*');
* ```
* @name .consume
* @param {Number} `len`
* @param {String} `value` Optionally pass the value being consumed for minor performance improvement.
* @return {String} Returns the consumed value
* @api public
*/
consume(len, value = this.state.string.slice(0, len)) {
this.state.consumed += value;
this.state.position += len;
this.state.string = this.state.string.slice(len);
return value;
}
/**
* Push a token onto the `scanner.queue` array.
*
* ```js
* console.log(scanner.queue.length); // 0
* scanner.enqueue({ rule: 'foo' });
* console.log(scanner.queue.length); // 1
* ```
* @name .enqueue
* @param {object} `token`
* @return {Object} Returns the token.
* @api public
*/
enqueue(token) {
if (token) this.state.queue.push(token);
return token;
}
/**
* Shift a token from `scanner.queue`.
*
* ```js
* console.log(scanner.queue.length); // 0
* scanner.enqueue({ rule: 'foo' });
* console.log(scanner.queue.length); // 1
* scanner.dequeue();
* console.log(scanner.queue.length); // 0
* ```
* @name .dequeue
* @return {Object} Returns the first token in the `scanner.queue`.
* @api public
*/
dequeue() {
return this.state.queue.shift();
}
/**
* Iterates over the registered regex patterns until a match is found,
* then returns a token from the match and regex `rule`.
*
* ```js
* const token = scanner.advance();
* console.log(token) // { rule: 'text', value: 'foo' }
* ```
* @name .advance
* @return {Object} Returns a token with `rule`, `value` and `match` properties.
* @api public
*/
advance() {
if (this.eos()) return;
for (let [rule, patterns] of this.rules) {
for (let regex of patterns) {
try {
let pos = this.state.position;
let match = this.match(regex);
if (match) {
let token = this.token(rule, match);
token.range = [pos, pos + token.match[0].length];
return token;
}
} catch (err) {
err.rule = rule;
err.string = this.state.string.slice(0, 20);
throw err;
}
}
}
}
/**
* Lookahead `n` tokens and return the last token. Pushes any
* intermediate tokens onto `scanner.tokens.` To lookahead a single
* token, use [.peek()](#peek).
*
* ```js
* const token = scanner.lookahead(2);
* ```
* @name .lookahead
* @param {number} `n`
* @return {Object}
* @api public
*/
lookahead(n) {
assert(typeof n === 'number', 'expected a number');
let fetch = n - this.state.queue.length;
while (fetch-- > 0 && this.enqueue(this.advance()));
return this.state.queue[--n];
}
/**
* Returns a token representing the next match, but without consuming the
* matched substring (e.g. the cursor position is not advanced).
*
* ```js
* const token = scanner.peek();
* ```
* @name .peek
* @return {Object|undefined} Returns a token, or undefined if no match was found.
* @api public
*/
peek() {
return this.lookahead(1);
}
/**
* Returns a token representing the next match, but without consuming the
* matched substring (e.g. the cursor position is not advanced).
*
* ```js
* const token = scanner.peek();
* ```
* @name .peek
* @return {Object|undefined} Returns a token, or undefined if no match was found.
* @api public
*/
next() {
return this.state.queue.shift() || this.advance();
}
/**
* Returns the next token and advances the cursor position.
*
* ```js
* const token = scanner.scan();
* ```
* @name .scan
* @return {Object|undefined} Returns a token, or undefined if no match was found.
* @api public
*/
scan() {
let token = this.next();
if (token) {
this.consume(token.match[0].length, token.match[0]);
return token;
}
}
/**
* Scan until the given `fn` does not return true.
*
* ```js
* scanner.scanWhile(tok => tok.rule !== 'space');
* ```
* @name .scanWhile
* @param {Function} `fn` Must return true to continue scanning.
* @returns {Array} Returns an array if scanned tokens.
* @api public
*/
scanWhile(fn = (() => !this.eos())) {
const scanned = [];
while (fn.call(this, this.peek())) scanned.push(this.scan());
return scanned;
}
/**
* Returns true if the scanner has not consumed any of the input string.
*
* @name .bos
* @return {Boolean}
* @api public
*/
bos() {
return !this.state.consumed;
}
/**
* Returns true if `scanner.string` and `scanner.queue` are empty.
*
* @name .eos
* @return {Boolean}
* @api public
*/
eos() {
return this.state.string === '' && this.state.queue.length === 0;
}
}
function assert(val, msg) {
if (!val) throw new Error(msg);
}
module.exports = Scanner;