This repository has been archived by the owner on Feb 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
cli.js
executable file
·125 lines (111 loc) · 4.17 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
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const minimist = require("minimist");
const {execFileSync} = require('child_process');
let args = process.argv.slice(2);
let subcommand = args.shift();
let needsHelp = false;
if (subcommand === 'help') {
needsHelp = true;
subcommand = args.shift();
}
let argv = minimist(args);
if (argv.h || argv.help) {
needsHelp = true;
}
switch (subcommand) {
case "generate":
if (needsHelp)
usage("generate", [
"Read a `grammar.js` file in the current working directory, and create/update the following files:",
"",
" src/grammar.json - the grammar in JSON format",
" src/parser.c - the parser",
" src/binding.cc - the C++ node.js binding for the parser",
" binding.gyp - the build configuration file for the node.js binding",
"",
"Arguments",
" --debug, -d - Log to stderr",
" --profile, -P - Render a flame graph of the parse performance (requires sudo)",
]);
require("./lib/cli/generate")({
profile: argv.profile || argv.P,
debug: argv.debug || argv.d,
properties: argv.properties
}, process.exit);
break;
case "build":
if (needsHelp)
usage("build", [
"Generate and recompile the parser. See the `generate` command.",
]);
require("./lib/cli/generate")({}, () => {
const nodeGyp = require.resolve('node-gyp/bin/node-gyp.js');
if (!fs.existsSync(path.join('build', 'config.gypi'))) {
execFileSync(nodeGyp, ['configure'], {stdio: 'inherit'});
}
execFileSync(nodeGyp, ['build', ...args], {stdio: 'inherit'});
process.exit(0)
});
break;
case "test":
if (needsHelp)
usage("test", [
"Run the tests for the parser in the current working directory.",
"",
"Arguments",
" --focus, -f <string> - Only run tests whose name contain the given string",
" --debug, -d - Output debugging information during parsing"
]);
require("./lib/cli/test")({
focus: argv.focus || argv.f,
debug: argv.debug || argv.d,
debugGraph: argv['debug-graph'] || argv.D
}, process.exit);
break;
case "parse":
const codePaths = argv._;
if (needsHelp || codePaths.length === 0)
usage("parse <code-file>", [
"Parse the given file using the parser in the current working directory and print the syntax tree.",
"",
"Arguments",
" code-path - The file to parse",
" --quiet, -q - Parse, but don't print any output",
" --time, -t - Print the time it took to parse",
" --debug, -d - Print a log of parse actions",
" --debug-graph, -D - Render a sequence of diagrams showing the changing parse stack",
" --profile, -P - Render a flame graph of the parse performance (requires sudo)",
" --heap, -H - Report heap allocation breakdown (requires google perf tools)",
" --repeat <count> - Parse the file the given number of times (useful for profiling)",
" --edit <edits> - Reparse the file after performing the given edit.",
" For example, pass '5,3,\"x\"' to delete three characters and",
" insert an 'x' at index 5. You can repeat this flag multiple times",
" to perform a series of edits."
]);
require("./lib/cli/parse")({
codePaths: codePaths,
debugGraph: argv['debug-graph'] || argv.D,
debug: argv.debug || argv.d,
quiet: argv.quiet || argv.q,
time: argv.time || argv.t,
profile: argv.profile || argv.P,
heapProfile: argv.heap || argv.H,
repeat: argv.repeat,
edits: argv.edit,
properties: argv.properties
}, process.exit);
break;
default:
usage("<generate|test|parse|build> [flags]", [
"Run `tree-sitter <command> --help` for more information about a particular command."
])
break;
}
function usage(command, lines) {
console.log("Usage: tree-sitter " + command + "\n");
if (lines)
console.log(lines.join('\n') + '\n')
process.exit(0);
}