@bablr/cli 0.1.11 → 0.2.0
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 +9 -14
- package/lib/syntax.js +65 -42
- package/package.json +8 -8
- package/lib/utils/node.js +0 -31
package/bin/index.js
CHANGED
|
@@ -9,10 +9,10 @@ import {
|
|
|
9
9
|
sourceFromReadStream,
|
|
10
10
|
stripTrailingNewline,
|
|
11
11
|
} from '@bablr/helpers/source';
|
|
12
|
-
import {
|
|
12
|
+
import { debugEnhancers } from '@bablr/helpers/enhancers';
|
|
13
13
|
import colorSupport from 'color-support';
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
14
|
+
import { evaluateIO } from '@bablr/io-vm-node';
|
|
15
|
+
import { createPrintCSTMLStrategy } from '../lib/syntax.js';
|
|
16
16
|
|
|
17
17
|
program
|
|
18
18
|
.option('-l, --language [URL]', 'The URL of the top BABLR language')
|
|
@@ -37,7 +37,7 @@ if (programOpts.color && !['auto', 'always', 'never'].includes(programOpts.color
|
|
|
37
37
|
const options = {
|
|
38
38
|
...programOpts,
|
|
39
39
|
color:
|
|
40
|
-
(programOpts.color.toLowerCase() === 'auto' && colorSupport.hasBasic
|
|
40
|
+
(programOpts.color.toLowerCase() === 'auto' && colorSupport.hasBasic) ||
|
|
41
41
|
programOpts.color.toLowerCase() === 'always',
|
|
42
42
|
};
|
|
43
43
|
|
|
@@ -47,16 +47,12 @@ const logStderr = (...args) => {
|
|
|
47
47
|
process.stderr.write(args.join(' ') + '\n');
|
|
48
48
|
};
|
|
49
49
|
|
|
50
|
-
|
|
51
|
-
throw new Error('TODO support this in a better way than turning off color');
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const enhancers = options.verbose ? { ...buildDebugEnhancers(logStderr), agastStrategy: null } : {};
|
|
50
|
+
const enhancers = options.verbose ? { ...debugEnhancers, agast: null } : {};
|
|
55
51
|
|
|
56
52
|
const rawStream = process.stdin.setEncoding('utf-8');
|
|
57
53
|
|
|
58
|
-
await
|
|
59
|
-
|
|
54
|
+
await evaluateIO(
|
|
55
|
+
createPrintCSTMLStrategy(
|
|
60
56
|
streamParse(
|
|
61
57
|
language,
|
|
62
58
|
options.production,
|
|
@@ -64,9 +60,8 @@ await writeLinesToWritableStream(
|
|
|
64
60
|
? embeddedSourceFromReadStream(rawStream)
|
|
65
61
|
: stripTrailingNewline(sourceFromReadStream(rawStream)),
|
|
66
62
|
{},
|
|
67
|
-
enhancers,
|
|
68
|
-
),
|
|
63
|
+
{ enhancers, emitEffects: true },
|
|
64
|
+
).tokens,
|
|
69
65
|
options,
|
|
70
66
|
),
|
|
71
|
-
process.stdout,
|
|
72
67
|
);
|
package/lib/syntax.js
CHANGED
|
@@ -1,24 +1,25 @@
|
|
|
1
1
|
/* global process Promise */
|
|
2
2
|
|
|
3
|
-
import { i } from '@bablr/boot';
|
|
4
3
|
import * as cstml from '@bablr/language-cstml';
|
|
4
|
+
import * as verboseOutput from '@bablr/language-bablr-cli-verbose-output';
|
|
5
5
|
import { streamParse } from 'bablr/enhanceable';
|
|
6
6
|
import { Coroutine } from '@bablr/coroutine';
|
|
7
7
|
import {
|
|
8
8
|
StreamIterable,
|
|
9
9
|
getStreamIterator,
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
generateCSTMLStrategy as generatePlainCSTMLStrategy,
|
|
11
|
+
generatePrettyCSTMLStrategy,
|
|
12
|
+
generateAllOutput,
|
|
12
13
|
} from '@bablr/agast-helpers/stream';
|
|
13
|
-
import {
|
|
14
|
-
|
|
14
|
+
import {
|
|
15
|
+
buildWriteEffect,
|
|
16
|
+
buildAnsiPushEffect,
|
|
17
|
+
buildAnsiPopEffect,
|
|
18
|
+
} from '@bablr/agast-helpers/builders';
|
|
15
19
|
|
|
16
|
-
function* __higlightStrategy(tokens) {
|
|
17
|
-
const co = new Coroutine(
|
|
18
|
-
getStreamIterator(streamParse(cstml, 'Document', generatePrettyCSTMLStream(tokens))),
|
|
19
|
-
);
|
|
20
|
+
function* __higlightStrategy(context, tokens) {
|
|
21
|
+
const co = new Coroutine(getStreamIterator(tokens));
|
|
20
22
|
|
|
21
|
-
let currentType;
|
|
22
23
|
let currentRef;
|
|
23
24
|
|
|
24
25
|
co.advance();
|
|
@@ -33,21 +34,42 @@ function* __higlightStrategy(tokens) {
|
|
|
33
34
|
const token = co.value;
|
|
34
35
|
|
|
35
36
|
if (token.type === 'OpenNodeTag') {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
) {
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
const tagType = token.value.type;
|
|
38
|
+
const currentType = context.agast.nodeForTag(token).parent?.type;
|
|
39
|
+
|
|
40
|
+
if (tagType === 'Literal' || (tagType === 'String' && currentRef.name === 'intrinsicValue')) {
|
|
41
|
+
if (tagType === 'Literal' || currentType === 'OpenNodeTag') {
|
|
42
|
+
yield buildAnsiPushEffect('bold green');
|
|
43
|
+
} else if (currentType === 'NodeMatcher') {
|
|
44
|
+
yield buildAnsiPushEffect('bold orange');
|
|
45
|
+
} else {
|
|
46
|
+
yield buildAnsiPushEffect();
|
|
47
|
+
}
|
|
48
|
+
} else if (tagType === 'Pattern') {
|
|
49
|
+
yield buildAnsiPushEffect('bold orange');
|
|
50
|
+
} else if (tagType === 'EscapeSequence') {
|
|
51
|
+
yield buildAnsiPushEffect('bold cyan');
|
|
52
|
+
} else if (tagType === 'Identifier') {
|
|
42
53
|
if (currentType === 'Reference') {
|
|
43
|
-
yield
|
|
54
|
+
yield buildAnsiPushEffect('bold gray');
|
|
55
|
+
} else if (currentType === 'Call') {
|
|
56
|
+
yield buildAnsiPushEffect('magenta bold');
|
|
44
57
|
} else {
|
|
45
|
-
yield
|
|
58
|
+
yield buildAnsiPushEffect();
|
|
46
59
|
}
|
|
60
|
+
} else if (tagType === 'EnterProductionLine' || tagType === 'LeaveProductionLine') {
|
|
61
|
+
yield buildAnsiPushEffect('blue bold');
|
|
62
|
+
} else if (
|
|
63
|
+
(currentRef?.name === 'sigilToken' &&
|
|
64
|
+
(currentType === 'ExecSpamexInstructionLine' ||
|
|
65
|
+
currentType === 'ExecCSTMLInstructionLine')) ||
|
|
66
|
+
(currentType === 'Tuple' &&
|
|
67
|
+
(currentRef.name === 'openToken' || currentRef.name === 'closeToken'))
|
|
68
|
+
) {
|
|
69
|
+
yield buildAnsiPushEffect('magenta bold');
|
|
47
70
|
} else {
|
|
48
|
-
yield
|
|
71
|
+
yield buildAnsiPushEffect();
|
|
49
72
|
}
|
|
50
|
-
currentType = token.value.type;
|
|
51
73
|
}
|
|
52
74
|
|
|
53
75
|
if (token.type === 'Reference') {
|
|
@@ -55,40 +77,41 @@ function* __higlightStrategy(tokens) {
|
|
|
55
77
|
}
|
|
56
78
|
|
|
57
79
|
if (token.type === 'CloseNodeTag') {
|
|
58
|
-
yield
|
|
80
|
+
yield buildAnsiPopEffect();
|
|
59
81
|
}
|
|
60
82
|
|
|
61
83
|
if (token.type === 'Literal') {
|
|
62
|
-
yield
|
|
84
|
+
yield buildWriteEffect(token.value);
|
|
63
85
|
} else if (token.type === 'OpenNodeTag' && token.value.intrinsicValue) {
|
|
64
|
-
yield
|
|
65
|
-
yield
|
|
86
|
+
yield buildWriteEffect(token.value.intrinsicValue);
|
|
87
|
+
yield buildAnsiPopEffect();
|
|
66
88
|
}
|
|
67
89
|
|
|
68
90
|
co.advance();
|
|
69
91
|
}
|
|
70
92
|
}
|
|
71
93
|
|
|
72
|
-
export const higlightStrategy = (tokens) => {
|
|
73
|
-
return
|
|
94
|
+
export const higlightStrategy = (context, tokens) => {
|
|
95
|
+
return new StreamIterable(__higlightStrategy(context, tokens));
|
|
74
96
|
};
|
|
75
97
|
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
98
|
+
export const createPrintCSTMLStrategy =
|
|
99
|
+
(tokens, options = {}) =>
|
|
100
|
+
() => {
|
|
101
|
+
const agastOptions = { verbose: options.verbose, inline: false };
|
|
102
|
+
const outputInstructions = options.format
|
|
103
|
+
? generatePrettyCSTMLStrategy(tokens, agastOptions)
|
|
104
|
+
: generatePlainCSTMLStrategy(tokens, agastOptions);
|
|
82
105
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
106
|
+
if (options.color) {
|
|
107
|
+
const input = generateAllOutput(outputInstructions);
|
|
86
108
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
109
|
+
const { context, tokens } = options.verbose
|
|
110
|
+
? streamParse(verboseOutput, 'Output', input)
|
|
111
|
+
: streamParse(cstml, 'Document', input);
|
|
90
112
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}
|
|
113
|
+
return higlightStrategy(context, tokens);
|
|
114
|
+
} else {
|
|
115
|
+
return outputInstructions;
|
|
116
|
+
}
|
|
117
|
+
};
|
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.
|
|
4
|
+
"version": "0.2.0",
|
|
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/
|
|
22
|
-
"@bablr/
|
|
23
|
-
"@bablr/boot": "0.2.3",
|
|
20
|
+
"@bablr/agast-helpers": "0.1.6",
|
|
21
|
+
"@bablr/io-vm-node": "0.2.0",
|
|
22
|
+
"@bablr/boot": "0.2.4",
|
|
24
23
|
"@bablr/coroutine": "0.1.0",
|
|
25
|
-
"@bablr/helpers": "0.
|
|
26
|
-
"@bablr/language-cstml": "0.
|
|
27
|
-
"bablr": "0.1.
|
|
24
|
+
"@bablr/helpers": "0.16.0",
|
|
25
|
+
"@bablr/language-cstml": "0.3.0",
|
|
26
|
+
"@bablr/language-bablr-cli-verbose-output": "0.1.0",
|
|
27
|
+
"bablr": "0.2.1",
|
|
28
28
|
"color-support": "1.1.3",
|
|
29
29
|
"commander": "12.0.0"
|
|
30
30
|
},
|
package/lib/utils/node.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
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
|
-
};
|