@bablr/cli 0.1.10 → 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 CHANGED
@@ -9,10 +9,10 @@ import {
9
9
  sourceFromReadStream,
10
10
  stripTrailingNewline,
11
11
  } from '@bablr/helpers/source';
12
- import { buildDebugEnhancers } from '@bablr/helpers/enhancers';
12
+ import { debugEnhancers } from '@bablr/helpers/enhancers';
13
13
  import colorSupport from 'color-support';
14
- import { generateCSTML } from '../lib/syntax.js';
15
- import { writeLinesToWritableStream } from '../lib/utils/node.js';
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')
@@ -47,20 +47,12 @@ const logStderr = (...args) => {
47
47
  process.stderr.write(args.join(' ') + '\n');
48
48
  };
49
49
 
50
- if (options.verbose && options.color) {
51
- // ANSI formatting escapes don't keep separate spanning stacks for stdin and sterr
52
- // this means you NEED a parser that ingests a combination of log and output if you
53
- // want to be able to highlight the complete output without breakage
54
- // It can be built, but I haven't yet
55
- throw new Error('Unsupported combination of options: verbose and color');
56
- }
57
-
58
- const enhancers = options.verbose ? { ...buildDebugEnhancers(logStderr), agastStrategy: null } : {};
50
+ const enhancers = options.verbose ? { ...debugEnhancers, agast: null } : {};
59
51
 
60
52
  const rawStream = process.stdin.setEncoding('utf-8');
61
53
 
62
- await writeLinesToWritableStream(
63
- generateCSTML(
54
+ await evaluateIO(
55
+ createPrintCSTMLStrategy(
64
56
  streamParse(
65
57
  language,
66
58
  options.production,
@@ -68,9 +60,8 @@ await writeLinesToWritableStream(
68
60
  ? embeddedSourceFromReadStream(rawStream)
69
61
  : stripTrailingNewline(sourceFromReadStream(rawStream)),
70
62
  {},
71
- enhancers,
72
- ),
63
+ { enhancers, emitEffects: true },
64
+ ).tokens,
73
65
  options,
74
66
  ),
75
- process.stdout,
76
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
- generatePrettyCSTML as generatePrettyCSTMLStream,
11
- generateCSTML as generateCSTMLStream,
10
+ generateCSTMLStrategy as generatePlainCSTMLStrategy,
11
+ generatePrettyCSTMLStrategy,
12
+ generateAllOutput,
12
13
  } from '@bablr/agast-helpers/stream';
13
- import { evaluate } from '@bablr/ansi-vm';
14
- import { buildString } from '@bablr/agast-vm-helpers';
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
- if (
37
- currentType === 'Literal' ||
38
- (token.value.type === 'String' && currentRef.name === 'intrinsicValue')
39
- ) {
40
- yield i`push(bold green)`;
41
- } else if (token.value.type === 'Identifier') {
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 i`push(bold gray)`;
54
+ yield buildAnsiPushEffect('bold gray');
55
+ } else if (currentType === 'Call') {
56
+ yield buildAnsiPushEffect('magenta bold');
44
57
  } else {
45
- yield i`push()`;
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 i`push()`;
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 i`pop()`;
80
+ yield buildAnsiPopEffect();
59
81
  }
60
82
 
61
83
  if (token.type === 'Literal') {
62
- yield i`write(${buildString(token.value)})`;
84
+ yield buildWriteEffect(token.value);
63
85
  } else if (token.type === 'OpenNodeTag' && token.value.intrinsicValue) {
64
- yield i`write(${buildString(token.value.intrinsicValue)})`;
65
- yield i`pop()`;
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 () => new StreamIterable(__higlightStrategy(tokens));
94
+ export const higlightStrategy = (context, tokens) => {
95
+ return new StreamIterable(__higlightStrategy(context, tokens));
74
96
  };
75
97
 
76
- const generateColorfulCSTML = (tokens) => evaluate(higlightStrategy(tokens));
77
-
78
- export const generateCSTML = (tokens, options) => {
79
- if (options.format && options.color) {
80
- return generateColorfulCSTML(tokens);
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
- if (options.format && !options.color) {
84
- return generatePrettyCSTMLStream(tokens);
85
- }
106
+ if (options.color) {
107
+ const input = generateAllOutput(outputInstructions);
86
108
 
87
- if (!options.format && !options.color) {
88
- return generateCSTMLStream(tokens);
89
- }
109
+ const { context, tokens } = options.verbose
110
+ ? streamParse(verboseOutput, 'Output', input)
111
+ : streamParse(cstml, 'Document', input);
90
112
 
91
- if (!options.format && options.color) {
92
- throw new Error("I don't know how to do this yet");
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.1.10",
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.5",
21
- "@bablr/agast-vm-helpers": "0.1.3",
22
- "@bablr/ansi-vm": "0.1.1",
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.15.4",
26
- "@bablr/language-cstml": "0.2.4",
27
- "bablr": "0.1.3",
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
- };