-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcli.js
598 lines (563 loc) · 20.6 KB
/
cli.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
/**
* Usage
* node cli [options]
*
* Description
* The command line interface for Aang.
*
* Contains the following built-in programs:
* • `[query]` - Parses the provided query and outputs the k-best parse trees.
*
* • `.test` - Parses the suite of test queries and checks output conforms to
* the test's specifications.
*
* • `.benchmark` - Benchmarks the duration of parsing the queries in the test
* suite.
*
* • `.buildGrammar` - Generates and outputs the grammar containing the grammar
* rules, semantics, entities, and deletables, for use with the parser.
*
* • `.ambiguityCheck` - Finds and prints instances of ambiguity in the grammar.
*
* • `.stateTable` - Prints the state table generated from the grammar.
*
* • `.archive*` - Saves output of program *. Includes: `.archiveTest`,
* `.archiveTestSmall`, `.archiveTestQuiet`, `.archiveGrammar`,
* `.archiveAmbigCheck`, `.archiveStateTable`, `.archiveAll`.
*
* • `.restoreGrammar` - Copies the last output of `.archiveGrammar` to the
* `.buildGrammar` output path.
*
* • `.diff*` - Compares last archived output of program * to current output of
* the same program. Includes: `.diffTest`, `.diffTestSmall`, `.diffTestQuiet`,
* `.diffGrammar`, `.diffAmbigCheck`, `.diffStateTable`.
*
* Enables configuration of CLI environment variables which are passed as options
* when executing the above programs.
*
* Each program is spawn as a child process. This automatically enables any
* changes to modules outside the CLI, allows the user to kill any process (with
* `^C`) without exiting the CLI, and improves benchmark result consistency by
* mitigating the impact of process caches.
*
* Options
* -h, --help Display this screen. [boolean]
*/
var util = require('../util/util')
var yargs = require('yargs')
var argv = yargs
.usage([
util.colors.bold('Usage'),
' node $0 [options]',
'',
util.colors.bold('Description'),
' The command line interface for Aang.',
'',
' Contains the following built-in programs:',
' • `[query]` - Parses the provided query and outputs the k-best parse trees.',
'',
' • `.test` - Parses the suite of test queries and checks output conforms to the test\'s specifications.',
'',
' • `.benchmark` - Benchmarks the duration of parsing the queries in the test suite.',
'',
' • `.buildGrammar` - Generates and outputs the grammar containing the grammar rules, semantics, entities, and deletables, for use with the parser.',
'',
' • `.ambiguityCheck` - Finds and prints instances of ambiguity in the grammar.',
'',
' • `.stateTable` - Prints the state table generated from the grammar.',
'',
' • `.archive*` - Saves output of program *. Includes: `.archiveTest`, `.archiveTestSmall`, `.archiveTestQuiet`, `.archiveGrammar`, `.archiveAmbigCheck`, `.archiveStateTable`, `.archiveAll`.',
'',
' • `.restoreGrammar` - Copies the last output of `.archiveGrammar` to the `.buildGrammar` output path.',
'',
' • `.diff*` - Compares last archived output of program * to current output of the same program. Includes: `.diffTest`, `.diffTestSmall`, `.diffTestQuiet`, `.diffGrammar`, `.diffAmbigCheck`, `.diffStateTable`.',
'',
' Enables configuration of CLI environment variables which are passed as options when executing the above programs.',
'',
' Each program is spawn as a child process. This automatically enables any changes to modules outside the CLI, allows the user to kill any process (with `^C`) without exiting the CLI, and improves benchmark result consistency by mitigating the impact of process caches.',
].join('\n'))
.updateStrings({
'Options:': util.colors.bold('Options'),
})
.help('h', 'Display this screen.').alias('h', 'help')
// Fail on unrecognized arguments.
.strict()
.argv
var childProcess = require('child_process')
var fs = require('fs')
var path = require('path')
// Instantiates a readline `Interface`.
var rl = require('./readlineAsync')
// File paths.
var paths = (function () {
var outDir = './out/'
return {
parse: './lib/parse/parse.js',
outDir: outDir,
test: './test/test.js',
testOut: outDir + 'test',
testSmallOut: outDir + 'test_small',
testQuietOut: outDir + 'test_quiet',
benchmark: './benchmark/benchmark.js',
buildGrammar: './lib/grammar/buildGrammar.js',
grammar: './lib/grammar.json',
grammarOld: outDir + 'grammar_old.json',
ambigCheck: './lib/ambig/ambiguityCheck.js',
ambigCheckOut: outDir + 'ambig',
printStateTable: './lib/parse/printStateTable.js',
stateTableOut: outDir + 'st',
}
}())
// Parse settings.
var parseOpts = {
k: 7,
quiet: false,
benchmark: false,
costs: false,
ambiguity: false,
trees: false,
treeNodeCosts: false,
treeTokenRanges: false,
semantics: true,
objectSemantics: false,
parseStack: false,
forest: false,
forestGraph: false,
}
// Send input not recognized as a command to `parse`.
rl.onLine(function (query) {
this.spawnAsyncProcess('node', [
paths.parse,
// Enclose with quotes to support `parse` invocation within a `zsh` shell.
'"' + query + '"',
'--k=' + parseOpts.k,
'--quiet=' + parseOpts.quiet,
'--benchmark=' + parseOpts.benchmark,
'--costs=' + parseOpts.costs,
'--ambiguity=' + parseOpts.ambiguity,
'--trees=' + parseOpts.trees,
'--tree-node-costs=' + parseOpts.treeNodeCosts,
'--tree-token-ranges=' + parseOpts.treeTokenRanges,
'--semantics=' + parseOpts.semantics,
'--object-semantics=' + parseOpts.objectSemantics,
'--parse-stack=' + parseOpts.parseStack,
'--parse-forest=' + parseOpts.forest,
'--parse-forest-graph=' + parseOpts.forestGraph,
])
})
// Color RLI prompt character green if there are unarchived grammar changes.
updateRLIPrompt(rl)
// Assign commands to the RLI, executed via `.command`.
// All arguments after `.command` are passed to the corresponding functions and programs as options.
rl.addCommands({
name: 'test',
description: 'Parse the suite of test queries.',
func: function test() {
// Pass all arguments to the command line program.
rl.spawnAsyncProcess('node', [ paths.test ].concat(Array.prototype.slice.call(arguments)))
},
}, {
name: 'archiveTest',
argNames: [ '[<filename>]' ],
description: 'Archive test output to \'out\' directory [with <filename>].',
func: function archiveTest(filename, callback) {
// Check arity.
if (typeof filename === 'function') {
callback = filename
filename = undefined
} else if (typeof callback !== 'function') {
callback = undefined
}
var outputPath = util.expandHomeDir(filename ? paths.outDir + filename : paths.testOut)
archiveOuput([ paths.test ], outputPath, callback)
},
}, {
name: 'diffTest',
description: 'Compare last archived test output to current test output.',
func: function diffTest() {
diffOutput(paths.testOut, [ paths.test ])
},
}, {
name: 'archiveTestSmall',
argNames: [ '[<filename>]' ],
description: 'Archive test output with k=5 to \'out\' directory [with <filename>].',
func: function archiveTest(filename, callback) {
// Check arity.
if (typeof filename === 'function') {
callback = filename
filename = undefined
} else if (typeof callback !== 'function') {
callback = undefined
}
var outputPath = util.expandHomeDir(filename ? paths.outDir + filename : paths.testSmallOut)
archiveOuput([ paths.test, '-k=5' ], outputPath, callback)
},
}, {
name: 'diffTestSmall',
description: 'Compare last archived test-small output to current test-small output.',
func: function diffTest() {
diffOutput(paths.testSmallOut, [ paths.test, '-k=5' ])
},
}, {
name: 'archiveTestQuiet',
argNames: [ '[<filename>]' ],
description: 'Archive test-quiet output to \'out\' directory [with <filename>].',
func: function archiveTestQuiet(filename, callback) {
// Check arity.
if (typeof filename === 'function') {
callback = filename
filename = undefined
} else if (typeof callback !== 'function') {
callback = undefined
}
var outputPath = util.expandHomeDir(filename ? paths.outDir + filename : paths.testQuietOut)
archiveOuput([ paths.test, '--quiet', '-k=1' ], outputPath, callback)
},
}, {
name: 'diffTestQuiet',
description: 'Compare last archived test-quiet output to current test-quiet output.',
func: function diffTestQuiet() {
diffOutput(paths.testQuietOut, [ paths.test, '--quiet', '-k=1' ])
},
}, {
name: 'benchmark',
description: 'Benchmark the duration of parsing the test suite.',
func: function benchmark() {
// Pass all arguments to the command line program.
rl.spawnAsyncProcess('node', [ paths.benchmark ].concat(Array.prototype.slice.call(arguments)))
},
}, {
name: 'buildGrammar',
description: 'Rebuild the grammar for use with the parser.',
func: function buildGrammar(callback) {
// Check arity.
var commandArgs = Array.from(arguments)
if (typeof callback === 'function') {
commandArgs.splice(commandArgs.indexOf(callback), 1)
} else {
callback = undefined
}
rl.spawnAsyncProcess('node', [
paths.buildGrammar,
'--output=' + paths.grammar,
// Pass all arguments to the command line program.
].concat(Array.prototype.slice.call(commandArgs)), function () {
if (callback) {
callback()
}
// Color RLI prompt character green if there are unarchived grammar changes. Invoke after `callback()`.
updateRLIPrompt(rl)
})
},
}, {
name: 'archiveGrammar',
argNames: [ '[<filename>]' ],
description: 'Copy the last output of `.buildGrammar` to \'out\' directory [with <filename>].',
func: function archiveGrammar(filename, callback) {
// Check arity.
if (typeof filename === 'function') {
callback = filename
filename = undefined
} else if (typeof callback !== 'function') {
callback = undefined
}
// Copy the last grammar output by `.buildGrammar` instead of rebuilding the grammar.
rl.spawnAsyncProcess('cp', [
'-v',
paths.grammar,
filename ? paths.outDir + filename : paths.grammarOld,
], function () {
if (callback) {
callback()
}
// Reset RLI prompt character after copying grammar to archive. Invoke after `callback()` should the function change 'grammar.json'.
updateRLIPrompt(rl)
})
},
}, {
name: 'restoreGrammar',
description: 'Copy the last output of `.archiveGrammar` to the `.buildGrammar` output path.',
func: function restoreGrammar() {
rl.spawnAsyncProcess('cp', [
'-v',
paths.grammarOld,
paths.grammar,
], function () {
// Reset RLI prompt character after restoring 'grammar.json' from the archive.
updateRLIPrompt(rl)
})
}
}, {
name: 'diffGrammar',
description: 'Compare last archived grammar to current grammar.',
func: function diffGrammar() {
// Invoke `vimdiff` to compare the last archived grammar to the current grammar.
diffFiles(paths.grammarOld, paths.grammar)
},
}, {
name: 'ambigCheck',
description: 'Check for ambiguity in the grammar.',
func: function ambigCheck() {
// Pass all arguments to the command line program.
rl.spawnAsyncProcess('node', [ paths.ambigCheck ].concat(Array.prototype.slice.call(arguments)))
},
}, {
name: 'archiveAmbigCheck',
argNames: [ '[<filename>]' ],
description: 'Archive output of the ambiguity check to \'out\' directory [with <filename>].',
func: function archiveAmbigCheck(filename, callback) {
// Check arity.
if (typeof filename === 'function') {
callback = filename
filename = undefined
} else if (typeof callback !== 'function') {
callback = undefined
}
var outputPath = util.expandHomeDir(filename ? paths.outDir + filename : paths.ambigCheckOut)
archiveOuput([ paths.ambigCheck ], outputPath, callback)
},
}, {
name: 'diffAmbigCheck',
description: 'Compare last archived ambiguity check output to current ambiguity check output.',
func: function diffAmbigCheck() {
diffOutput(paths.ambigCheckOut, [ paths.ambigCheck ])
},
}, {
name: 'stateTable',
description: 'Print the state table.',
func: function stateTable() {
rl.spawnAsyncProcess('node', [
paths.printStateTable,
paths.grammar,
// Pass all arguments to the command line program.
].concat(Array.prototype.slice.call(arguments)))
},
}, {
name: 'archiveStateTable',
argNames: [ '[<filename>]' ],
description: 'Archive state table equational representation to \'out\' directory [with <filename>].',
func: function archiveStateTable(filename, callback) {
// Check arity.
if (typeof filename === 'function') {
callback = filename
filename = undefined
} else if (typeof callback !== 'function') {
callback = undefined
}
var outputPath = util.expandHomeDir(filename ? paths.outDir + filename : paths.stateTableOut)
archiveOuput([ paths.printStateTable, paths.grammar, '--suppress-state-indexes' ], outputPath, callback)
},
}, {
name: 'diffStateTable',
description: 'Compare last archived state table to current state table.',
func: function diffStateTable() {
diffOutput(paths.stateTableOut, [ paths.printStateTable, paths.grammar, '--suppress-state-indexes' ])
},
}, {
name: 'archiveAll',
description: 'Archives grammar, test, test-small, and test-quiet.',
func: function archiveAll() {
// Do not bother rewriting as asynchronous because the change does not shorten processing time enough for the user to not background this process as this (slower) synchronous implementation requires.
// Invoke `buildGrammar` first to update grammar for use in tests.
rl.commands['.buildGrammar'](function () {
rl.commands['.archiveGrammar'](function () {
util.log()
rl.commands['.archiveTest'](function () {
util.log()
rl.commands['.archiveTestSmall'](function () {
util.log()
rl.commands['.archiveTestQuiet']()
})
})
})
}, '--quiet')
},
}, {
name: 'k',
argNames: [ '<n>' ],
description: 'Set the maximum number of parse trees to find to <n>.',
func: function k(k) {
if (!isNaN(k)) parseOpts.k = Number(k)
util.log('k:', parseOpts.k)
},
}, {
name: 'quiet',
description: 'Toggle suppressing parse results from output',
func: function quiet() {
util.log('Suppress parse results:', parseOpts.quiet = !parseOpts.quiet)
},
}, {
name: 'time',
description: 'Toggle benchmarking duration of parse and parse forest search.',
func: function time() {
util.log('Print parse time:', parseOpts.benchmark = !parseOpts.benchmark)
},
}, {
name: 'costs',
description: 'Toggle printing parse costs.',
func: function costs() {
util.log('Print parse costs:', parseOpts.costs = !parseOpts.costs)
},
}, {
name: 'ambiguity',
description: 'Toggle printing instances of semantic ambiguity.',
func: function ambiguity() {
util.log('Print ambiguity:', parseOpts.ambiguity = !parseOpts.ambiguity)
},
}, {
name: 'trees',
description: 'Toggle printing parse trees.',
func: function trees() {
util.log('Print parse trees:', parseOpts.trees = !parseOpts.trees)
},
}, {
name: 'nodeCosts',
description: 'Toggle including in parse trees each node\'s path cost.',
func: function nodeCosts() {
util.log('Include in parse trees each node\'s path cost:', parseOpts.treeNodeCosts = !parseOpts.treeNodeCosts)
},
}, {
name: 'tokenRanges',
description: 'Toggle including in parse trees each node\'s token range.',
func: function tokenRanges() {
util.log('Include in parse trees each node\'s token range:', parseOpts.treeTokenRanges = !parseOpts.treeTokenRanges)
},
}, {
name: 'semantics',
description: 'Toggle printing semantics.',
func: function semantics() {
util.log('Print semantics:', parseOpts.semantics = !parseOpts.semantics)
},
}, {
name: 'objectSemantics',
description: 'Toggle printing object representations of semantics.',
func: function objectSemantics() {
util.log('Print object representations of semantics:', parseOpts.objectSemantics = !parseOpts.objectSemantics)
},
}, {
name: 'stack',
description: 'Toggle printing the parse stack.',
func: function stack() {
util.log('Print parse stack:', parseOpts.parseStack = !parseOpts.parseStack)
},
}, {
name: 'forest',
description: 'Toggle printing an equational representation of the parse forest.',
func: function forest() {
util.log('Print parse forest:', parseOpts.forest = !parseOpts.forest)
},
}, {
name: 'graph',
description: 'Toggle printing a graph representation of the parse forest.',
func: function graph() {
util.log('Print parse forest graph:', parseOpts.forestGraph = !parseOpts.forestGraph)
},
})
/**
* Invokes `node` with `nodeArgs` and saves output to `outputPath`. `diffOutput()` uses this output for comparisons.
*
* @private
* @static
* @param {string[]} nodeArgs The arguments `node` invokes to generate the output to archive.
* @param {string} outputPath The file-path of where to write output.
* @param {Function} [callback] The function to invoke after `nodeArgs` completes and its output has been saved.
*/
function archiveOuput(nodeArgs, outputPath, callback) {
// Log program name and arguments to execute.
var programName = path.basename(nodeArgs[0])
util.log('Processing: node', programName, nodeArgs.slice(1).join(' '))
// Create file if does not exist, truncate file to zero length if it does exist, or throw an exception if `outputPath` is a directory.
var outFD = fs.openSync(outputPath, 'w')
// Spawn program as a child process, outputting `outputPath`.
rl.spawnAsyncProcess('node', nodeArgs, [ 'ignore', outFD, process.stderr ], function () {
util.log(programName, 'output saved:', fs.realpathSync(outputPath))
if (callback) {
callback()
}
})
}
/**
* Invokes `node` with `nodeArgs` to generate output, and opens `vimdiff` to compare the output to the previous output for the same program at `oldOutputPath`.
*
* @private
* @static
* @param {string} oldOutputPath The file-path of previous `nodeArgs` output to compare.
* @param {string[]} nodeArgs The arguments `node` invokes to generate the output to compare.
*/
function diffOutput(oldOutputPath, nodeArgs) {
// Log program name and arguments to execute.
util.log('Processing: node', path.basename(nodeArgs[0]), nodeArgs.slice(1).join(' '))
// Temporarily save output for `vimdiff`.
var tempPath = oldOutputPath + '_temp'
var outFD = fs.openSync(tempPath, 'w')
// Execute program and save output to temporary file-path.
rl.spawnAsyncProcess('node', nodeArgs, [ 'ignore', outFD, outFD ], function () {
// Open `vimdiff` to compare current output to archived previous output.
diffFiles(oldOutputPath, tempPath)
// Remove temporary file.
fs.unlinkSync(tempPath)
})
}
/**
* Opens `vimdiff` with the provided files to compare their differences.
*
* @private
* @static
* @param {string} fileA The file-path to compare.
* @param {string} fileB The other file-path to compare.
*/
function diffFiles(fileA, fileB) {
if (filesDiff(fileA, fileB)) {
// Use `childProcess.spawnSync()` to enable user input (unlike `rl.spawnAsyncProcess()`).
childProcess.spawnSync('vimdiff', [ fileA, fileB ], { stdio: 'inherit', shell: process.env.shell })
} else {
util.log('No changes.')
}
}
/**
* Checks if `fileA` and `fileB` have different contents.
*
* @private
* @static
* @param {string} fileA The file-path to compare.
* @param {string} fileB The other file-path to compare.
* @returns {boolean} Returns `true` if the files differ, else `false`.
*/
function filesDiff(fileA, fileB) {
return !!childProcess.spawnSync('cmp', [ '--quiet', fileA, fileB ], { stdio: 'ignore' }).status
}
/**
* Colors the RLI prompt character green if there are unarchived grammar changes, otherwise removes its color.
*
* @private
* @static
* @param {Interface} rl The RLI of which to color the prompt character.
*/
function updateRLIPrompt(rl) {
rl.setPrompt(util.colors[ filesDiff(paths.grammar, paths.grammarOld) ? 'green' : 'stripColor' ](rl._prompt))
// Re-draw the current line and prompt character if `rl` is not currently running an asynchronous process (during which the prompt character is not displayed and will be updated upon process completion).
if (!rl.paused) {
rl._refreshLine()
}
}
/**
* Invokes `node` with `nodeArgs` and saves output to the system pasteboard.
*
* @deprecated No longer in use because `rl.spawnAsyncProcess()` now invokes its child process inside a shell, enabling `.test | pbcopy` in the RLI.
* @private
* @static
* @param {string[]} nodeArgs The arguments `node` invokes to generate the output to archive.
*/
function copyOutput(nodeArgs) {
// Log program name and arguments to execute.
var programName = path.basename(nodeArgs[0])
util.log('Processing: node', programName, nodeArgs.slice(1).join(' '))
// Spawn `pbcopy` process to receive output.
var pbcopy = childProcess.spawn('pbcopy')
// Pipe process `stdout` to `pbcopy` (excludes `stderr`).
rl.spawnAsyncProcess('node', nodeArgs, [ 'ignore', pbcopy.stdin, process.stderr ], function () {
// Close `pbcopy` write stream.
pbcopy.stdin.end()
util.log(programName, 'output copied to pasteboard.')
})
}