-
Notifications
You must be signed in to change notification settings - Fork 3
/
nearley-moo.js
74 lines (58 loc) · 1.73 KB
/
nearley-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
const curry = require('curry')
// intended to be called by the grammar itself
function nm(tokens) {
// XXX: until nearley supports `.` in token identifiers this will
// pollute the global scope :(
const g = global || window
for (const key in tokens) {
let value = tokens[key]
if (value instanceof String)
value = [ value ]
if (value instanceof Array || value.value instanceof Array) {
// we've got ourselves a keyword array!
const keywords = value instanceof Array ? value : value.value
for (let keyword of keywords) {
// add tester functions for each
g[key + '_' + keyword] = { test: tok =>
tok.type === key && tok.value === keyword }
}
}
// add tester function for `key`
g[key] = { test: tok => tok.type === key }
}
}
// intended to be called by the parent js file
nm.parser = curry((nearley, grammar, lexer) => {
const parser = new nearley.Parser(grammar.ParserRules, grammar.ParserStart)
lexer.reset()
const self = {
results: [],
parser, lexer,
ignore: type => {
self.ignoredTokens.push(...(type instanceof Array ? type : [ type ]))
return self
},
feed: (str, fn) => {
let token
// feed to moo
lexer.reset(str)
while(token = lexer.next()) {
// ignore tokens if asked!
if (self.ignoredTokens.includes(token.type))
continue
if (fn) {
// pass to fn for magic to happen?
fn(token, self)
} else {
// feed to nearley
parser.feed([ token ])
}
}
self.results = parser.results
return self
}
}
Object.defineProperty(self, 'ignoredTokens', { value: [] })
return self
})
module.exports = nm