@bablr/cli 0.7.1 → 0.9.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/README.md +2 -62
- package/bin/index.js +27 -10
- package/lib/syntax.js +15 -17
- package/package.json +10 -10
package/README.md
CHANGED
|
@@ -2,26 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
This is the CLI runner for BABLR. If you have a BABLR grammar for a computer language, this tool allows you to use it to do streaming parsing. Parse results are presented in CSTML.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
This package has no built-in language support, but rather expects you to supply a valid import specifier referring to a BABLR language. This likely means that you will likely need to start by installing both the parser and the language you wish to use.
|
|
8
|
-
|
|
9
|
-
```
|
|
10
|
-
Usage: bablr [options]
|
|
11
|
-
|
|
12
|
-
Options:
|
|
13
|
-
-l, --language [URL] The URL of the top BABLR language
|
|
14
|
-
-p, --production [type] The name of the top production type
|
|
15
|
-
-f, --format Pretty-format CSTML output (default: true)
|
|
16
|
-
-F, --no-format
|
|
17
|
-
-v, --verbose Prints debugging information to stderr
|
|
18
|
-
-c, --color [WHEN] When to use ANSI escape colors
|
|
19
|
-
WHEN: "auto" | "always" | "never" (default: "auto")
|
|
20
|
-
-e, --embedded Requires quoted input but enables gap parsing
|
|
21
|
-
-h, --help display help for command
|
|
22
|
-
```
|
|
5
|
+
For complete information see the [bablr-cli reference documentation](https://docs.bablr.org/reference/bablr-cli)
|
|
23
6
|
|
|
24
|
-
##
|
|
7
|
+
## Usage
|
|
25
8
|
|
|
26
9
|
```bash
|
|
27
10
|
bablr -l @bablr/language-en-json -p Expression -f << 'EOF'
|
|
@@ -32,46 +15,3 @@ bablr -l @bablr/language-en-json -p Expression -f << 'EOF'
|
|
|
32
15
|
]
|
|
33
16
|
EOF
|
|
34
17
|
```
|
|
35
|
-
|
|
36
|
-
Running the above command produces the following output. Note that this is a stream parse so lines of output will appear one by one as fast as the input can be read and parsed.
|
|
37
|
-
|
|
38
|
-
```cstml
|
|
39
|
-
<!0:cstml bablr-language='https://github.com/bablr-lang/language-en-json'>
|
|
40
|
-
<>
|
|
41
|
-
.:
|
|
42
|
-
<Array>
|
|
43
|
-
openToken: <*Punctuator '[' balanced=']' />
|
|
44
|
-
<#*Space:Space '\n ' />
|
|
45
|
-
separators[]: []
|
|
46
|
-
elements[]: []
|
|
47
|
-
elements[]:
|
|
48
|
-
<Number span='Number'>
|
|
49
|
-
wholePart:
|
|
50
|
-
<Integer>
|
|
51
|
-
signToken: null
|
|
52
|
-
value: <*UnsignedInteger '1' />
|
|
53
|
-
</>
|
|
54
|
-
fractionalSeparatorToken: null
|
|
55
|
-
fractionalPart: null
|
|
56
|
-
exponentSeparatorToken: null
|
|
57
|
-
exponentPart: null
|
|
58
|
-
</>
|
|
59
|
-
separators[]: <*Punctuator ',' />
|
|
60
|
-
<#*Space:Space '\n ' />
|
|
61
|
-
elements[]:
|
|
62
|
-
<Boolean>
|
|
63
|
-
sigilToken: <*Keyword 'true' />
|
|
64
|
-
</>
|
|
65
|
-
separators[]: <*Punctuator ',' />
|
|
66
|
-
<#*Space:Space '\n ' />
|
|
67
|
-
elements[]:
|
|
68
|
-
<String>
|
|
69
|
-
openToken: <*Punctuator '"' balanced='"' balancedSpan='String' />
|
|
70
|
-
content: <*StringContent '3' />
|
|
71
|
-
closeToken: <*Punctuator '"' balancer />
|
|
72
|
-
<#*Space:Space '\n' />
|
|
73
|
-
</>
|
|
74
|
-
closeToken: <*Punctuator ']' balancer />
|
|
75
|
-
</>
|
|
76
|
-
</>
|
|
77
|
-
```
|
package/bin/index.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
/* global process */
|
|
4
4
|
|
|
5
5
|
import { program } from 'commander';
|
|
6
|
-
import { streamParse
|
|
6
|
+
import { streamParse } from 'bablr';
|
|
7
7
|
import { embeddedSourceFrom, readFromStream, stripTrailingNewline } from '@bablr/helpers/source';
|
|
8
8
|
import { debugEnhancers } from '@bablr/helpers/enhancers';
|
|
9
9
|
import colorSupport from 'color-support';
|
|
@@ -11,17 +11,23 @@ import { evaluateIO } from '@bablr/io-vm-node';
|
|
|
11
11
|
import { generateCSTML } from '../lib/syntax.js';
|
|
12
12
|
import {
|
|
13
13
|
buildBasicNodeMatcher,
|
|
14
|
+
buildNodeFlags,
|
|
14
15
|
buildOpenNodeMatcher,
|
|
15
16
|
buildPropertyMatcher,
|
|
16
17
|
} from '@bablr/helpers/builders';
|
|
17
|
-
import {
|
|
18
|
+
import { evaluateReturnAsync } from '@bablr/agast-helpers/tree';
|
|
19
|
+
import { buildEmbeddedMatcher } from '@bablr/agast-vm-helpers/builders';
|
|
20
|
+
import { o } from '@bablr/helpers/grammar';
|
|
18
21
|
|
|
19
22
|
program
|
|
20
23
|
.name('bablr')
|
|
21
24
|
.option('-l, --language [URL]', 'The URL of the top BABLR language')
|
|
22
25
|
.option('-p, --production [type]', 'The name of the top production type')
|
|
23
26
|
.option('-f, --format', 'Pretty-format CSTML output', true)
|
|
24
|
-
.option('-g, --gaps', '
|
|
27
|
+
.option('-g, --gaps', 'Sets hasGap flag on root matcher')
|
|
28
|
+
.option('-r, --fragment', 'Sets fragment flag on root matcher')
|
|
29
|
+
.option('-t --token', 'Sets token flag on root matcher')
|
|
30
|
+
.option('-o --cover', 'Sets cover flag on root matcher')
|
|
25
31
|
.option('-F, --no-format')
|
|
26
32
|
.option('-v, --verbose', 'Prints debugging information to stderr')
|
|
27
33
|
.option(
|
|
@@ -49,9 +55,18 @@ const language = await import(options.language);
|
|
|
49
55
|
|
|
50
56
|
const matcher = buildEmbeddedMatcher(
|
|
51
57
|
buildPropertyMatcher(
|
|
58
|
+
null,
|
|
52
59
|
null,
|
|
53
60
|
buildBasicNodeMatcher(
|
|
54
|
-
buildOpenNodeMatcher(
|
|
61
|
+
buildOpenNodeMatcher(
|
|
62
|
+
buildNodeFlags({
|
|
63
|
+
hasGap: options.gaps,
|
|
64
|
+
fragment: options.fragment,
|
|
65
|
+
token: options.token,
|
|
66
|
+
cover: options.cover,
|
|
67
|
+
}),
|
|
68
|
+
options.production,
|
|
69
|
+
),
|
|
55
70
|
),
|
|
56
71
|
),
|
|
57
72
|
);
|
|
@@ -62,23 +77,25 @@ const logStderr = (...args) => {
|
|
|
62
77
|
|
|
63
78
|
const enhancers = options.verbose ? { ...debugEnhancers, agast: null } : {};
|
|
64
79
|
|
|
65
|
-
const ctx = Context.from(language, enhancers.bablrProduction);
|
|
66
|
-
|
|
67
80
|
const rawStream = process.stdin.setEncoding('utf-8');
|
|
68
81
|
|
|
69
82
|
const output = evaluateIO(() =>
|
|
70
83
|
generateCSTML(
|
|
71
84
|
streamParse(
|
|
72
|
-
|
|
85
|
+
language,
|
|
73
86
|
matcher,
|
|
74
87
|
options.embedded
|
|
75
88
|
? embeddedSourceFrom(readFromStream(rawStream))
|
|
76
89
|
: stripTrailingNewline(readFromStream(rawStream)),
|
|
77
|
-
{},
|
|
78
|
-
{
|
|
90
|
+
o({}),
|
|
91
|
+
{
|
|
92
|
+
enhancers,
|
|
93
|
+
emitEffects: true,
|
|
94
|
+
holdShiftedNodes: !options.gaps,
|
|
95
|
+
holdUndefinedAttributes: !options.gaps,
|
|
96
|
+
},
|
|
79
97
|
),
|
|
80
98
|
{
|
|
81
|
-
ctx,
|
|
82
99
|
color: options.color,
|
|
83
100
|
format: options.format,
|
|
84
101
|
emitEffects: true,
|
package/lib/syntax.js
CHANGED
|
@@ -3,27 +3,30 @@
|
|
|
3
3
|
import emptyStack from '@iter-tools/imm-stack';
|
|
4
4
|
import * as cstml from '@bablr/language-en-cstml';
|
|
5
5
|
import * as verboseOutput from '@bablr/language-en-bablr-cli-verbose-output';
|
|
6
|
-
import { streamParse
|
|
6
|
+
import { streamParse } from 'bablr/enhanceable';
|
|
7
7
|
import { Coroutine } from '@bablr/coroutine';
|
|
8
|
+
import { StreamIterable, getStreamIterator } from '@bablr/agast-helpers/stream';
|
|
8
9
|
import {
|
|
9
|
-
StreamIterable,
|
|
10
10
|
generateAllOutput,
|
|
11
|
-
getStreamIterator,
|
|
12
11
|
writeCSTMLStrategy,
|
|
13
12
|
writePrettyCSTMLStrategy,
|
|
14
|
-
} from '@bablr/agast-helpers/stream';
|
|
13
|
+
} from '@bablr/agast-vm-helpers/stream';
|
|
15
14
|
import {
|
|
16
15
|
buildWriteEffect,
|
|
17
16
|
buildAnsiPushEffect,
|
|
18
17
|
buildAnsiPopEffect,
|
|
19
18
|
buildEmbeddedMatcher,
|
|
20
|
-
} from '@bablr/agast-helpers/builders';
|
|
19
|
+
} from '@bablr/agast-vm-helpers/builders';
|
|
21
20
|
import { OpenNodeTag, CloseNodeTag, ReferenceTag, LiteralTag } from '@bablr/agast-helpers/symbols';
|
|
22
21
|
import {
|
|
23
22
|
buildBasicNodeMatcher,
|
|
23
|
+
buildNodeFlags,
|
|
24
24
|
buildOpenNodeMatcher,
|
|
25
25
|
buildPropertyMatcher,
|
|
26
26
|
} from '@bablr/helpers/builders';
|
|
27
|
+
import { getFlagsWithGap, nodeFlags } from '@bablr/agast-helpers/builders';
|
|
28
|
+
|
|
29
|
+
let gapNodeFlags = getFlagsWithGap(nodeFlags);
|
|
27
30
|
|
|
28
31
|
function* __higlightStrategy(tags) {
|
|
29
32
|
const co = new Coroutine(getStreamIterator(tags));
|
|
@@ -60,10 +63,7 @@ function* __higlightStrategy(tags) {
|
|
|
60
63
|
} else {
|
|
61
64
|
yield buildAnsiPushEffect();
|
|
62
65
|
}
|
|
63
|
-
} else if (
|
|
64
|
-
tagType === Symbol.for('Pattern') &&
|
|
65
|
-
tag.value.language !== 'https://bablr.org/languages/core/en/spamex'
|
|
66
|
-
) {
|
|
66
|
+
} else if (tagType === Symbol.for('Pattern')) {
|
|
67
67
|
yield buildAnsiPushEffect('bold orange');
|
|
68
68
|
} else if (tagType === Symbol.for('EscapeSequence')) {
|
|
69
69
|
yield buildAnsiPushEffect('bold cyan');
|
|
@@ -120,30 +120,28 @@ export const higlightStrategy = (tags) => {
|
|
|
120
120
|
};
|
|
121
121
|
|
|
122
122
|
export const generateCSTML = (tags, options = {}) => {
|
|
123
|
-
const strategyOptions = { ctx: options.ctx };
|
|
124
123
|
const outputInstructions = options.format
|
|
125
|
-
? writePrettyCSTMLStrategy(tags
|
|
126
|
-
: writeCSTMLStrategy(tags
|
|
124
|
+
? writePrettyCSTMLStrategy(tags)
|
|
125
|
+
: writeCSTMLStrategy(tags);
|
|
127
126
|
|
|
128
127
|
if (options.color) {
|
|
129
128
|
const input = generateAllOutput(outputInstructions);
|
|
130
129
|
const language = options.emitEffects ? verboseOutput : cstml;
|
|
131
130
|
const type = options.emitEffects ? 'Output' : 'Document';
|
|
132
131
|
|
|
133
|
-
const context = Context.from(language);
|
|
134
|
-
|
|
135
132
|
const tags = streamParse(
|
|
136
|
-
|
|
133
|
+
language,
|
|
137
134
|
buildEmbeddedMatcher(
|
|
138
135
|
buildPropertyMatcher(
|
|
139
136
|
null,
|
|
140
|
-
|
|
137
|
+
null,
|
|
138
|
+
buildBasicNodeMatcher(buildOpenNodeMatcher(buildNodeFlags(gapNodeFlags), type)),
|
|
141
139
|
),
|
|
142
140
|
),
|
|
143
141
|
input,
|
|
144
142
|
);
|
|
145
143
|
|
|
146
|
-
return higlightStrategy(tags
|
|
144
|
+
return higlightStrategy(tags);
|
|
147
145
|
} else {
|
|
148
146
|
return outputInstructions;
|
|
149
147
|
}
|
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.9.0",
|
|
5
5
|
"author": "Conrad Buck<conartist6@gmail.com>",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"files": [
|
|
@@ -17,21 +17,21 @@
|
|
|
17
17
|
},
|
|
18
18
|
"sideEffects": false,
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@bablr/agast-helpers": "0.
|
|
21
|
-
"@bablr/agast-vm-helpers": "0.
|
|
22
|
-
"@bablr/io-vm-node": "0.
|
|
23
|
-
"@bablr/boot": "0.
|
|
20
|
+
"@bablr/agast-helpers": "0.8.0",
|
|
21
|
+
"@bablr/agast-vm-helpers": "0.8.0",
|
|
22
|
+
"@bablr/io-vm-node": "0.7.0",
|
|
23
|
+
"@bablr/boot": "0.9.0",
|
|
24
24
|
"@bablr/coroutine": "0.1.0",
|
|
25
|
-
"@bablr/helpers": "0.
|
|
26
|
-
"@bablr/language-en-cstml": "0.
|
|
27
|
-
"@bablr/language-en-bablr-cli-verbose-output": "0.
|
|
25
|
+
"@bablr/helpers": "0.23.0",
|
|
26
|
+
"@bablr/language-en-cstml": "0.10.0",
|
|
27
|
+
"@bablr/language-en-bablr-cli-verbose-output": "0.8.0",
|
|
28
28
|
"@iter-tools/imm-stack": "1.1.0",
|
|
29
|
-
"bablr": "0.
|
|
29
|
+
"bablr": "0.9.0",
|
|
30
30
|
"color-support": "1.1.3",
|
|
31
31
|
"commander": "^12.0.0"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@bablr/eslint-config-base": "github:bablr-lang/eslint-config-base#
|
|
34
|
+
"@bablr/eslint-config-base": "github:bablr-lang/eslint-config-base#c97bfa4b3663f8378e9b3e42bb5a41e685406cf9",
|
|
35
35
|
"enhanced-resolve": "^5.12.0",
|
|
36
36
|
"eslint": "^8.32.0",
|
|
37
37
|
"eslint-import-resolver-enhanced-resolve": "^1.0.5",
|