forked from no-context/moo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
moo.js
627 lines (530 loc) · 17.8 KB
/
moo.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory) /* global define */
}
else if (typeof module === 'object' && module.exports) {
module.exports = factory()
}
else {
root.moo = factory()
}
}(this, function() {
'use strict';
var hasOwnProperty = Object.prototype.hasOwnProperty
var hasSticky = typeof new RegExp().sticky === 'boolean'
/***************************************************************************/
function toArray(possiblyArray) {
if (!possiblyArray) return []
return Array.isArray(possiblyArray) ? possiblyArray : [possiblyArray]
}
function isRegExp(o) { return o && o.constructor === RegExp }
function isObject(o) { return o && typeof o === 'object' && o.constructor !== RegExp && !Array.isArray(o) }
function reEscape(s) {
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
}
function reGroups(s) {
var re = new RegExp('|' + s)
return re.exec('').length - 1
}
function reCapture(s) {
return '(' + s + ')'
}
function reUnion(regexps) {
var source = regexps.map(function(s) {
return "(?:" + s + ")"
}).join('|')
return "(?:" + source + ")"
}
function regexpOrLiteral(obj) {
if (typeof obj === 'string') {
return '(?:' + reEscape(obj) + ')'
}
else if (isRegExp(obj)) {
// TODO: consider /u support
if (obj.ignoreCase) throw new Error('RegExp /i flag not allowed')
if (obj.global) throw new Error('RegExp /g flag is implied')
if (obj.sticky) throw new Error('RegExp /y flag is implied')
if (obj.multiline) throw new Error('RegExp /m flag is implied')
return obj.source
}
else {
throw new Error('not a pattern: ' + obj)
}
}
function objectToRules(object) {
var keys = Object.getOwnPropertyNames(object)
var result = []
for (var i = 0; i < keys.length; i++) {
var key = keys[i]
var thing = object[key]
var rules = toArray(thing)
var match = []
rules.forEach(function(rule) {
if (isObject(rule)) {
if (match.length) result.push(ruleOptions(key, match))
result.push(ruleOptions(key, rule))
match = []
}
else {
match.push(rule)
}
})
if (match.length) result.push(ruleOptions(key, match))
}
return result
}
function arrayToRules(array) {
var result = []
for (var i = 0; i < array.length; i++) {
var obj = array[i]
if (!obj.name) {
throw new Error('Rule has no name: ' + JSON.stringify(obj))
}
result.push(ruleOptions(obj.name, obj))
}
return result
}
function flattenCategories(categories) {
if (categories.length == 0) return null
else {
var finalCategories = []
for (var j = categories.length - 1; j >= 0; j--) {
var category = categories[j]
finalCategories.push(category.categoryName)
// since the parent categories have already been flattened, this works
if (category.categories) finalCategories.push.apply(finalCategories, category.categories.map((parentCategory) => parentCategory.categoryName))
}
return finalCategories
}
}
function ruleOptions(name, obj) {
if (typeof obj !== 'object' || Array.isArray(obj) || isRegExp(obj)) {
obj = { match: obj }
}
// nb. error implies lineBreaks
var options = {
tokenType: name,
lineBreaks: !!obj.error,
pop: false,
next: null,
push: null,
error: false,
value: null,
getTypeAndCategories: null,
categories: null,
keywords: null,
}
// Avoid Object.assign(), so we support IE9+
for (var key in obj) {
if (hasOwnProperty.call(obj, key)) {
options[key] = obj[key]
}
}
// convert to array
var match = options.match
options.match = Array.isArray(match) ? match : match ? [match] : []
options.match.sort(function(a, b) {
return isRegExp(a) && isRegExp(b) ? 0
: isRegExp(b) ? -1 : isRegExp(a) ? +1 : b.length - a.length
})
function normalizeCategories(optionsObject) {
if (optionsObject.categories) {
var categories = toArray(optionsObject.categories)
validateCategories(categories)
optionsObject.categories = flattenCategories(categories)
}
else optionsObject.categories = null
}
// coerce undefined or empty arrays to null
normalizeCategories(options)
if (options.keywords) {
var tokenTypes = Object.getOwnPropertyNames(options.keywords)
for (var i = tokenTypes.length - 1; i >= 0; i--) {
var tokenType = tokenTypes[i]
var keywords = options.keywords[tokenType]
if (!isObject(keywords)) keywords = { values: toArray(keywords), categories: null }
else {
keywords.values = toArray(keywords.values)
if (keywords.values.length == 0) keywords.values = [null]
normalizeCategories(keywords)
}
if (options.categories) {
if (keywords.categories) keywords.categories = keywords.categories.concat(options.categories)
else keywords.categories = options.categories
}
options.keywords[tokenType] = keywords
}
options.getTypeAndCategories = keywordTransform(options.keywords)
}
return options
}
function keywordTransform(types) {
var reverseMap = Object.create(null)
var byLength = Object.create(null)
var tokenTypes = Object.getOwnPropertyNames(types)
for (var i = tokenTypes.length - 1; i >= 0; i--) {
var tokenType = tokenTypes[i]
var item = types[tokenType]
var keywordList = item.values
var tokenCategories = item.categories
for (var j = keywordList.length - 1; j >= 0; j--) {
var keyword = keywordList[j]
if (typeof keyword !== 'string') {
throw new Error("keyword must be string (in keyword '" + tokenType + "')")
}
(byLength[keyword.length] = byLength[keyword.length] || []).push(keyword)
reverseMap[keyword] = { tokenType: tokenType, categories: tokenCategories && tokenCategories.length != 0 ? tokenCategories: null }
}
}
// fast string lookup
// https://jsperf.com/string-lookups
function str(x) { return JSON.stringify(x) }
var source = ''
source += '(function(value) {\n'
source += 'switch (value.length) {\n'
for (var length in byLength) {
var keywords = byLength[length]
source += 'case ' + length + ':\n'
source += 'switch (value) {\n'
for (var i = keywords.length - 1; i >= 0; i--) {
var keyword = keywords[i]
var tokenTypeAndCategories = reverseMap[keyword]
source += 'case ' + str(keyword) + ': return ' + str(tokenTypeAndCategories) + '\n'
}
source += '}\n'
}
source += '}\n'
source += '})'
return eval(source) // getTypeAndCategories
}
function matchToken(testToken, matchTokenOrCategory) {
if (testToken === undefined) return false
if (matchTokenOrCategory.isCategory) {
if (!testToken.categories) return false
for (var i = testToken.categories.length - 1; i >= 0; i--) {
var categoryName = testToken.categories[i]
if (matchTokenOrCategory.categoryName == categoryName) return true
}
return false
}
else return testToken.type == matchTokenOrCategory.type
}
function matchTokens(testTokens, matchTokensOrCategories) {
if (testTokens.length != matchTokensOrCategories.length) return false
for (var i = testTokens.length - 1; i >= 0; i--) {
var testToken = testTokens[i]
var matchTokenOrCategory = matchTokensOrCategories[i]
if (!matchToken(testToken, matchTokenOrCategory)) return false
}
return true
}
function validateCategories(categoriesArray) {
if (categoriesArray === null) return
for (var i = categoriesArray.length - 1; i >= 0; i--) {
var category = categoriesArray[i]
if (!category.isCategory) {
throw new Error("Categories should only be set to category objects: " + category)
}
}
}
function createCategory(categoryName, parentCategories) {
var finalCategories = []
if (parentCategories) {
parentCategories = toArray(parentCategories)
validateCategories(parentCategories)
for (var i = parentCategories.length - 1; i >= 0; i--) {
var parentCategory = parentCategories[i]
finalCategories.push(parentCategory)
if (parentCategory.categories) finalCategories.push.apply(finalCategories, parentCategory.categories)
}
}
return {
isCategory: true, categoryName: categoryName,
categories: finalCategories.length != 0 ? finalCategories : null
}
}
function compileRules(rules, hasStates) {
rules = Array.isArray(rules) ? arrayToRules(rules) : objectToRules(rules)
var errorRule = null
var groups = []
var parts = []
for (var i = 0; i < rules.length; i++) {
var options = rules[i]
if (options.error) {
if (errorRule) {
throw new Error("Multiple error rules not allowed: (for token '" + options.tokenType + "')")
}
errorRule = options
}
// skip rules with no match
if (options.match.length === 0) {
continue
}
groups.push(options)
// convert to RegExp
var pat = reUnion(options.match.map(regexpOrLiteral))
// validate
var regexp = new RegExp(pat)
if (regexp.test("")) {
throw new Error("RegExp matches empty string: " + regexp)
}
var groupCount = reGroups(pat)
if (groupCount > 0) {
throw new Error("RegExp has capture groups: " + regexp + "\nUse (?: … ) instead")
}
if (!hasStates && (options.pop || options.push || options.next)) {
throw new Error("State-switching options are not allowed in stateless lexers (for token '" + options.tokenType + "')")
}
// try and detect rules matching newlines
if (!options.lineBreaks && regexp.test('\n')) {
throw new Error('Rule should declare lineBreaks: ' + regexp)
}
// store regex
parts.push(reCapture(pat))
}
var suffix = hasSticky ? '' : '|(?:)'
var flags = hasSticky ? 'ym' : 'gm'
var combined = new RegExp(reUnion(parts) + suffix, flags)
return {regexp: combined, groups: groups, error: errorRule}
}
function compile(rules) {
var result = compileRules(rules)
return new Lexer({start: result}, 'start')
}
function compileStates(states, start) {
var keys = Object.getOwnPropertyNames(states)
if (!start) start = keys[0]
var map = Object.create(null)
for (var i = 0; i < keys.length; i++) {
var key = keys[i]
map[key] = compileRules(states[key], true)
}
for (var i = 0; i < keys.length; i++) {
var groups = map[keys[i]].groups
for (var j = 0; j < groups.length; j++) {
var g = groups[j]
var state = g && (g.push || g.next)
if (state && !map[state]) {
throw new Error("Missing state '" + state + "' (in token '" + g.tokenType + "' of state '" + keys[i] + "')")
}
if (g && g.pop && +g.pop !== 1) {
throw new Error("pop must be 1 (in token '" + g.tokenType + "' of state '" + keys[i] + "')")
}
}
}
return new Lexer(map, start)
}
/***************************************************************************/
var Lexer = function(states, state) {
this.startState = state
this.states = states
this.buffer = ''
this.stack = []
this.reset()
}
Lexer.prototype.reset = function(data, info) {
this.buffer = data || ''
this.index = 0
this.line = info ? info.line : 1
this.col = info ? info.col : 1
this.setState(info ? info.state : this.startState)
return this
}
Lexer.prototype.save = function() {
return {
line: this.line,
col: this.col,
state: this.state,
}
}
Lexer.prototype.setState = function(state) {
if (!state || this.state === state) return
this.state = state
var info = this.states[state]
this.groups = info.groups
this.error = info.error || {lineBreaks: true, shouldThrow: true}
this.re = info.regexp
}
Lexer.prototype.popState = function() {
this.setState(this.stack.pop())
}
Lexer.prototype.pushState = function(state) {
this.stack.push(this.state)
this.setState(state)
}
Lexer.prototype._eat = hasSticky ? function(re) { // assume re is /y
return re.exec(this.buffer)
} : function(re) { // assume re is /g
var match = re.exec(this.buffer)
// will always match, since we used the |(?:) trick
if (match[0].length === 0) {
return null
}
return match
}
Lexer.prototype._getGroup = function(match) {
if (match === null) {
return -1
}
var groupCount = this.groups.length
for (var i = 0; i < groupCount; i++) {
if (match[i + 1] !== undefined) {
return i
}
}
throw new Error('oops')
}
function tokenToString() {
return this.value
}
Lexer.prototype.next = function() {
var re = this.re
var buffer = this.buffer
var index = re.lastIndex = this.index
if (index === buffer.length) {
return // EOF
}
var match = this._eat(re)
var i = this._getGroup(match)
var group, text
if (i === -1) {
group = this.error
// consume rest of buffer
text = buffer.slice(index)
}
else {
text = match[0]
group = this.groups[i]
}
// count line breaks
var lineBreaks = 0
if (group.lineBreaks) {
var matchNL = /\n/g
var nl = 1
if (text === '\n') {
lineBreaks = 1
}
else {
while (matchNL.exec(text)) { lineBreaks++; nl = matchNL.lastIndex }
}
}
// we'll have to use this area to inject all token categories into the token
var tokenTypeAndCategories = group.getTypeAndCategories ? group.getTypeAndCategories(text) : undefined
var tokenType = tokenTypeAndCategories ? tokenTypeAndCategories.tokenType : undefined
var tokenCategories = tokenTypeAndCategories ? tokenTypeAndCategories.categories : undefined
var token = {
type: tokenType || group.tokenType,
value: group.value ? group.value(text) : text,
text: text,
toString: tokenToString,
offset: index,
lineBreaks: lineBreaks,
line: this.line,
col: this.col,
categories: tokenCategories || group.categories,
}
// nb. adding more props to token object will make V8 sad!
var size = text.length
this.index += size
this.line += lineBreaks
if (lineBreaks !== 0) {
this.col = size - nl + 1
}
else {
this.col += size
}
// throw, if no rule with {error: true}
if (group.shouldThrow) {
throw new Error(this.formatError(token, "invalid syntax"))
}
if (group.pop) this.popState()
else if (group.push) this.pushState(group.push)
else if (group.next) this.setState(group.next)
if (group.ignore) return this.next()
return token
}
if (typeof Symbol !== 'undefined' && Symbol.iterator) {
var LexerIterator = function(lexer) {
this.lexer = lexer
}
LexerIterator.prototype.next = function() {
var token = this.lexer.next()
return {value: token, done: !token}
}
LexerIterator.prototype[Symbol.iterator] = function() {
return this
}
Lexer.prototype[Symbol.iterator] = function() {
return new LexerIterator(this)
}
}
Lexer.prototype.formatError = function(token, message) {
var value = token.value
var index = token.offset
var endOfLine = token.lineBreaks ? value.indexOf('\n') : value.length
var start = Math.max(0, index - token.col + 1)
var firstLine = this.buffer.substring(start, index + endOfLine)
message += " at line " + token.line + " col " + token.col + ":\n\n"
message += " " + firstLine + "\n"
message += " " + Array(token.col).join(" ") + "^"
return message
}
// Lexer.prototype.formatRangeError = function(beginToken, endToken, message) {
// const startIndex = beginToken.offset
// }
Lexer.prototype.clone = function() {
return new Lexer(this.states, this.state)
}
Lexer.prototype.has = function(tokenType) {
for (var s in this.states) {
var state = this.states[s]
if (state.error && state.error.tokenType === tokenType) return true
var groups = state.groups
for (var i = 0; i < groups.length; i++) {
var group = groups[i]
if (group.tokenType === tokenType) return true
if (group.keywords && hasOwnProperty.call(group.keywords, tokenType)) {
return true
}
}
}
return false
}
Lexer.prototype.tokenLibrary = function() {
var library = {}
for (var stateKey in this.states) {
var state = this.states[stateKey]
for (var i = state.groups.length - 1; i >= 0; i--) {
var group = state.groups[i]
if (group.keywords) {
var keywordTypes = Object.getOwnPropertyNames(group.keywords)
for (var j = keywordTypes.length - 1; j >= 0; j--) {
var type = keywordTypes[j]
var keyword = group.keywords[type]
// var keyword = group.keywords[j]
// var type = keyword.type
var categories = keyword.categories
if (type in library) throw new Error("there are overlapping token names in multiple states: " + type)
library[type] = {
type: type, categories: categories,
}
}
}
var type = group.tokenType
if (type in library) throw new Error("there are overlapping token names in multiple states: " + type)
library[type] = {
type: type, categories: group.categories,
}
}
}
return library
}
return {
compile: compile,
states: compileStates,
error: Object.freeze({error: true}),
matchToken: matchToken,
matchTokens: matchTokens,
createCategory: createCategory,
}
}))