@bablr/cli 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/index.js +16 -10
- package/lib/syntax.js +20 -3
- package/lib/utils/node.js +31 -0
- package/package.json +8 -8
package/bin/index.js
CHANGED
|
@@ -11,23 +11,22 @@ import {
|
|
|
11
11
|
} from '@bablr/helpers/source';
|
|
12
12
|
import { buildDebugEnhancers } from '@bablr/helpers/enhancers';
|
|
13
13
|
import colorSupport from 'color-support';
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
14
|
+
import { generateCSTML } from '../lib/syntax.js';
|
|
15
|
+
import { writeLinesToWritableStream } from '../lib/utils/node.js';
|
|
16
16
|
|
|
17
17
|
program
|
|
18
18
|
.option('-l, --language [URL]', 'The URL of the BABLR language to parse with')
|
|
19
19
|
.option('-p, --production [type]', 'The top-level type for the parse')
|
|
20
|
-
.option('-f, --format', 'Pretty-format CSTML output')
|
|
21
20
|
.option('-F, --no-format', 'Produce machine-readable CSTML output')
|
|
21
|
+
.option('-f, --format', 'Pretty-format CSTML output')
|
|
22
22
|
.option('-v, --verbose', 'Prints debugging information to stderr')
|
|
23
23
|
.option('-c, --color', 'Force colored output', true)
|
|
24
24
|
.option('-C, --no-color', 'Do not color output')
|
|
25
25
|
.option('-e, --embedded', 'Requires quoted input but enables gap parsing')
|
|
26
26
|
.parse(process.argv);
|
|
27
27
|
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
const enableAnsi = colorSupport.hasBasic && options.color;
|
|
28
|
+
const programOpts = program.opts();
|
|
29
|
+
const options = { ...programOpts, color: colorSupport.hasBasic && programOpts.color };
|
|
31
30
|
|
|
32
31
|
const language = await import(options.language);
|
|
33
32
|
|
|
@@ -35,14 +34,20 @@ const logStderr = (...args) => {
|
|
|
35
34
|
process.stderr.write(args.join(' ') + '\n');
|
|
36
35
|
};
|
|
37
36
|
|
|
37
|
+
if (options.verbose && options.color) {
|
|
38
|
+
// ANSI formatting escapes don't keep separate spanning stacks for stdin and sterr
|
|
39
|
+
// this means you NEED a parser that ingests a combination of log and output if you
|
|
40
|
+
// want to be able to highlight the complete output without breakage
|
|
41
|
+
// It can be built, but I haven't yet
|
|
42
|
+
throw new Error('Unsupported combination of options: verbose and color');
|
|
43
|
+
}
|
|
44
|
+
|
|
38
45
|
const enhancers = options.verbose ? { ...buildDebugEnhancers(logStderr), agastStrategy: null } : {};
|
|
39
46
|
|
|
40
47
|
const rawStream = process.stdin.setEncoding('utf-8');
|
|
41
48
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
pipeline(
|
|
45
|
-
generateCSTML_(
|
|
49
|
+
await writeLinesToWritableStream(
|
|
50
|
+
generateCSTML(
|
|
46
51
|
streamParse(
|
|
47
52
|
language,
|
|
48
53
|
options.production,
|
|
@@ -52,6 +57,7 @@ pipeline(
|
|
|
52
57
|
{},
|
|
53
58
|
enhancers,
|
|
54
59
|
),
|
|
60
|
+
options,
|
|
55
61
|
),
|
|
56
62
|
process.stdout,
|
|
57
63
|
);
|
package/lib/syntax.js
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
StreamIterable,
|
|
9
9
|
getStreamIterator,
|
|
10
10
|
generatePrettyCSTML as generatePrettyCSTMLStream,
|
|
11
|
+
generateCSTML as generateCSTMLStream,
|
|
11
12
|
} from '@bablr/agast-helpers/stream';
|
|
12
13
|
import { evaluate } from '@bablr/ansi-vm';
|
|
13
14
|
import { buildString } from '@bablr/agast-vm-helpers';
|
|
@@ -63,7 +64,7 @@ function* __higlightStrategy(tokens) {
|
|
|
63
64
|
|
|
64
65
|
if (token.type === 'Literal') {
|
|
65
66
|
yield i`write(${buildString(token.value)})`;
|
|
66
|
-
} else if (token.type === 'OpenNodeTag' && token.value.
|
|
67
|
+
} else if (token.type === 'OpenNodeTag' && token.value.intrinsicValue) {
|
|
67
68
|
yield i`write(${buildString(token.value.intrinsicValue)})`;
|
|
68
69
|
yield i`pop()`;
|
|
69
70
|
}
|
|
@@ -76,6 +77,22 @@ export const higlightStrategy = (tokens) => {
|
|
|
76
77
|
return () => new StreamIterable(__higlightStrategy(tokens));
|
|
77
78
|
};
|
|
78
79
|
|
|
79
|
-
|
|
80
|
+
const generateColorfulCSTML = (tokens) => evaluate(higlightStrategy(tokens));
|
|
80
81
|
|
|
81
|
-
export const generateCSTML = (tokens) =>
|
|
82
|
+
export const generateCSTML = (tokens, options) => {
|
|
83
|
+
if (options.format && options.color) {
|
|
84
|
+
return generateColorfulCSTML(tokens);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (options.format && !options.color) {
|
|
88
|
+
return generatePrettyCSTMLStream(tokens);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (!options.format && !options.color) {
|
|
92
|
+
return generateCSTMLStream(tokens);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (!options.format && options.color) {
|
|
96
|
+
throw new Error("I don't know how to do this yet");
|
|
97
|
+
}
|
|
98
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Coroutine } from '@bablr/coroutine';
|
|
2
|
+
import { getStreamIterator } from '@bablr/agast-helpers/stream';
|
|
3
|
+
|
|
4
|
+
export const writeLinesToWritableStream = async (from, to) => {
|
|
5
|
+
const co = new Coroutine(getStreamIterator(from));
|
|
6
|
+
|
|
7
|
+
let buf = '';
|
|
8
|
+
|
|
9
|
+
for (;;) {
|
|
10
|
+
co.advance();
|
|
11
|
+
|
|
12
|
+
if (co.current instanceof Promise) {
|
|
13
|
+
co.current = await co.current;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if (co.done) break;
|
|
17
|
+
|
|
18
|
+
const chr = co.value;
|
|
19
|
+
|
|
20
|
+
buf += chr;
|
|
21
|
+
|
|
22
|
+
if (chr === '\n') {
|
|
23
|
+
if (!to.write(buf)) {
|
|
24
|
+
await to.once('drain');
|
|
25
|
+
}
|
|
26
|
+
buf = '';
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
to.write(buf);
|
|
31
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bablr/cli",
|
|
3
3
|
"description": "CLI for running BABLR parsers",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.6",
|
|
5
5
|
"author": "Conrad Buck<conartist6@gmail.com>",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"files": [
|
|
@@ -17,14 +17,14 @@
|
|
|
17
17
|
},
|
|
18
18
|
"sideEffects": false,
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@bablr/agast-helpers": "0.1.
|
|
21
|
-
"@bablr/agast-vm-helpers": "0.1.
|
|
22
|
-
"@bablr/ansi-vm": "0.1.
|
|
23
|
-
"@bablr/boot": "0.2.
|
|
20
|
+
"@bablr/agast-helpers": "0.1.5",
|
|
21
|
+
"@bablr/agast-vm-helpers": "0.1.3",
|
|
22
|
+
"@bablr/ansi-vm": "0.1.1",
|
|
23
|
+
"@bablr/boot": "0.2.3",
|
|
24
24
|
"@bablr/coroutine": "0.1.0",
|
|
25
|
-
"@bablr/helpers": "0.15.
|
|
26
|
-
"@bablr/language-cstml": "0.2.
|
|
27
|
-
"bablr": "0.1.
|
|
25
|
+
"@bablr/helpers": "0.15.4",
|
|
26
|
+
"@bablr/language-cstml": "0.2.2",
|
|
27
|
+
"bablr": "0.1.3",
|
|
28
28
|
"color-support": "1.1.3",
|
|
29
29
|
"commander": "12.0.0"
|
|
30
30
|
},
|