@bablr/helpers 0.17.0 → 0.18.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/lib/enhancers.js CHANGED
@@ -60,7 +60,7 @@ export const mapProductions = (fn, Grammar) => {
60
60
  };
61
61
 
62
62
  export const debugEnhancers = {
63
- agast: (strategy) => logEmitted(strategy, '<<< '),
63
+ // agast: (strategy) => logEmitted(strategy, '<<< '),
64
64
  createBablrStrategy: (strategy) => logStrategy(strategy, ' >>> '),
65
65
  bablrProduction: createProductionLogger('>>> '),
66
66
  };
package/lib/grammar.js CHANGED
@@ -1,12 +1,21 @@
1
1
  import every from 'iter-tools-es/methods/every';
2
2
  import isString from 'iter-tools-es/methods/is-string';
3
- import { objectEntries, getPrototypeOf } from './object.js';
3
+ import objectEntries from 'iter-tools-es/methods/object-entries';
4
+ import { getPrototypeOf } from './object.js';
4
5
 
5
6
  const { isArray } = Array;
6
7
  const isSymbol = (value) => typeof value === 'symbol';
7
8
  const isType = (value) => isString(value) || isSymbol(value);
8
9
 
9
- export const resolveLanguage = (language, path) => {
10
+ export const resolveLanguage = (context, language, path) => {
11
+ if (isString(path)) {
12
+ if (language.canonicalURL === path) {
13
+ return language;
14
+ } else {
15
+ throw new Error('absolute path resolution not implemented');
16
+ }
17
+ }
18
+
10
19
  let l = language;
11
20
 
12
21
  if (!l) {
@@ -28,6 +37,20 @@ export const resolveLanguage = (language, path) => {
28
37
  return l;
29
38
  };
30
39
 
40
+ export const unresolveLanguage = (context, baseLanguage, absoluteLanguage) => {
41
+ if (absoluteLanguage == null || absoluteLanguage === baseLanguage.canonicalURL) {
42
+ return null;
43
+ }
44
+
45
+ for (const { 0: key, 1: value } of objectEntries(baseLanguage.dependencies)) {
46
+ if (value.canonicalURL === absoluteLanguage) {
47
+ return [key];
48
+ }
49
+ }
50
+
51
+ throw new Error('Cannot currently unresolve nested deps');
52
+ };
53
+
31
54
  export const explodeSubtypes = (aliases, exploded, types) => {
32
55
  for (const type of types) {
33
56
  const explodedTypes = aliases.get(type);
package/lib/stream.js ADDED
@@ -0,0 +1,85 @@
1
+ import { Coroutine } from '@bablr/coroutine';
2
+ import {
3
+ generatePrettyCSTMLStrategy as generatePrettyCSTML,
4
+ generateCSTMLStrategy as generateCSTML,
5
+ stringFromStream,
6
+ generateStandardOutput,
7
+ getStreamIterator,
8
+ StreamIterable,
9
+ } from '@bablr/agast-helpers/stream';
10
+ import isString from 'iter-tools-es/methods/is-string';
11
+ import emptyStack from '@iter-tools/imm-stack';
12
+
13
+ import { unresolveLanguage } from './grammar.js';
14
+
15
+ // bad: wrecks tree by breaking weak linkages
16
+ function* __resolveTerminals(ctx, terminals) {
17
+ const co = new Coroutine(getStreamIterator(terminals));
18
+ let languages = emptyStack;
19
+
20
+ for (;;) {
21
+ co.advance();
22
+
23
+ if (co.current instanceof Promise) {
24
+ co.current = yield co.current;
25
+ }
26
+
27
+ if (co.done) break;
28
+
29
+ const terminal = co.value;
30
+
31
+ if (terminal.type === 'DoctypeTag') {
32
+ languages = languages.push(ctx.languages.get(terminal.value.attributes['bablr-language']));
33
+ }
34
+
35
+ if (terminal.type === 'CloseNodeTag') {
36
+ languages = languages.pop();
37
+ }
38
+
39
+ if (terminal.type === 'OpenNodeTag') {
40
+ if (isString(terminal.value.language)) {
41
+ const currentLanguage = languages.value;
42
+ const nodeLanguageURL = terminal.value.language;
43
+ const nodeLanguage = ctx.languages.get(nodeLanguageURL);
44
+
45
+ languages = languages.push(nodeLanguage);
46
+
47
+ yield {
48
+ type: 'OpenNodeTag',
49
+ value: {
50
+ ...terminal.value,
51
+ language: unresolveLanguage(ctx, currentLanguage, nodeLanguageURL),
52
+ },
53
+ };
54
+ } else {
55
+ yield terminal;
56
+ }
57
+ } else {
58
+ yield terminal;
59
+ }
60
+ }
61
+ }
62
+
63
+ export const resolveTerminals = (ctx, terminals) => {
64
+ return new StreamIterable(__resolveTerminals(ctx, terminals));
65
+ };
66
+
67
+ export const generatePrettyCSTMLStrategy = (terminals, options = {}) => {
68
+ const { ctx } = options;
69
+
70
+ return generatePrettyCSTML(ctx ? resolveTerminals(ctx, terminals) : terminals, options);
71
+ };
72
+
73
+ export const printPrettyCSTML = (terminals, options = {}) => {
74
+ return stringFromStream(generateStandardOutput(generatePrettyCSTMLStrategy(terminals, options)));
75
+ };
76
+
77
+ export const generateCSTMLStrategy = (terminals, options = {}) => {
78
+ const { ctx } = options;
79
+
80
+ return generateCSTML(ctx ? resolveTerminals(ctx, terminals) : terminals, options);
81
+ };
82
+
83
+ export const printCSTML = (terminals, options = {}) => {
84
+ return stringFromStream(generateStandardOutput(generateCSTMLStrategy(terminals, options)));
85
+ };
package/lib/tree.js ADDED
@@ -0,0 +1,8 @@
1
+ import { streamFromTree } from '@bablr/agast-helpers/tree';
2
+
3
+ import { printPrettyCSTML as printPrettyCSTMLFromStream } from './stream.js';
4
+
5
+ export const printPrettyCSTML = (rootNode, options = {}) => {
6
+ // i need a context-aware streamFromTree here to build a stream with linked terminals...?
7
+ return printPrettyCSTMLFromStream(streamFromTree(rootNode), options);
8
+ };
package/lib/trivia.js CHANGED
@@ -7,7 +7,7 @@ const lookbehind = (context, s) => {
7
7
  while (
8
8
  token &&
9
9
  ['OpenNodeTag', 'CloseNodeTag', 'Reference'].includes(token.type) &&
10
- (token.type !== 'OpenNodeTag' || !token.value.intrinsicValue)
10
+ (token.type !== 'OpenNodeTag' || !(token.value.flags.intrinsic && token.value.flags.token))
11
11
  ) {
12
12
  const prevToken = context.getPreviousTerminal(token);
13
13
  if (!prevToken) break;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@bablr/helpers",
3
3
  "description": "Command helpers for use in writing BABLR grammars",
4
- "version": "0.17.0",
4
+ "version": "0.18.0",
5
5
  "author": "Conrad Buck<conartist6@gmail.com>",
6
6
  "type": "module",
7
7
  "files": [
@@ -16,7 +16,9 @@
16
16
  "./productions": "./lib/productions.js",
17
17
  "./shorthand": "./lib/shorthand.js",
18
18
  "./source": "./lib/source.js",
19
+ "./stream": "./lib/stream.js",
19
20
  "./symbols": "./lib/symbols.js",
21
+ "./tree": "./lib/tree.js",
20
22
  "./trivia": "./lib/trivia.js"
21
23
  },
22
24
  "sideEffects": false,
@@ -26,17 +28,18 @@
26
28
  "clean": "macrome clean"
27
29
  },
28
30
  "dependencies": {
29
- "@bablr/language_enhancer-debug-log": "0.4.0",
30
- "@bablr/strategy_enhancer-debug-log": "0.3.0",
31
- "@bablr/agast-helpers": "0.2.0",
32
- "@bablr/agast-vm-helpers": "0.2.0",
31
+ "@bablr/language_enhancer-debug-log": "0.5.0",
32
+ "@bablr/strategy_enhancer-debug-log": "0.4.0",
33
+ "@bablr/agast-helpers": "0.3.1",
34
+ "@bablr/agast-vm-helpers": "0.3.1",
33
35
  "@bablr/coroutine": "0.1.0",
36
+ "@iter-tools/imm-stack": "1.1.0",
34
37
  "iter-tools-es": "^7.5.3"
35
38
  },
36
39
  "devDependencies": {
37
- "@bablr/boot": "0.3.0",
40
+ "@bablr/boot": "0.4.0",
38
41
  "@bablr/eslint-config-base": "github:bablr-lang/eslint-config-base#d834ccc52795d6c3b96ecc6c419960fceed221a6",
39
- "@bablr/macrome": "0.1.1",
42
+ "@bablr/macrome": "0.1.3",
40
43
  "@bablr/macrome-generator-bablr": "0.3.1",
41
44
  "enhanced-resolve": "^5.12.0",
42
45
  "eslint": "^7.32.0",