-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbuild.js
68 lines (61 loc) · 1.6 KB
/
build.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
var fs = require('fs')
var pegjs = require('pegjs')
var overrideAction = require('pegjs-override-action')
var input = __dirname + '/grammar.pegjs'
var output = __dirname + '/parser.js'
var overrides = __dirname + '/overrides.js'
if (require.main === module) {
if (process.argv[2] == '-w') {
watch()
} else {
console.log(getSource())
}
}
function getSource () {
delete require.cache[require.resolve('./overrides')]
var grammar = fs.readFileSync(input, 'utf8')
var parserSource = pegjs.buildParser(grammar, {
output: "source",
allowedStartRules: [
'script',
'command',
'argument',
'continuationStart'
],
plugins: [overrideAction],
overrideActionPlugin: require('./overrides')
})
return 'module.exports = parse\n\n' +
parse + '\n' +
'var parser=' + parserSource + '\n' +
'module.exports.SyntaxError = parser.SyntaxError\n';
}
function watch () {
fs.watchFile(input, onChange)
fs.watchFile(overrides, onChange)
function onChange (curr, prev) {
if (curr.mtime > prev.mtime) {
try {
var source = getSource()
fs.writeFileSync(output, source + '\n')
console.error("Wrote " + output)
} catch (err) {
console.error(err.message)
}
}
}
}
/**
* This isn't called directly, but stringified into the resulting source
*/
function parse (input, opts) {
// Wrap parser.parse to allow specifying the start rule
// as a shorthand option
if (!opts) {
opts = {}
}
else if (typeof opts == 'string') {
opts = { startRule: opts }
}
return parser.parse(input, opts)
}