@bablr/language_enhancer-debug-log 0.2.0 → 0.3.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.
Files changed (2) hide show
  1. package/lib/index.js +20 -12
  2. package/package.json +3 -2
package/lib/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  /* global console */
2
2
 
3
3
  import { printSource, getCooked } from '@bablr/agast-helpers/tree';
4
+ import { buildCall, buildNumber, buildObject, buildString } from '@bablr/agast-vm-helpers';
4
5
  import { printType } from '@bablr/agast-helpers/print';
5
6
 
6
7
  const { getOwnPropertyNames, hasOwn } = Object;
@@ -26,17 +27,22 @@ const mapProductions = (fn, Grammar) => {
26
27
  return MappedGrammar;
27
28
  };
28
29
 
29
- const mapProduction = (production, type, indentation, log) => {
30
- return function* (...args) {
30
+ const writeError = (text) => {
31
+ return buildCall('write', buildString(text), buildObject({ stream: buildNumber(2) }));
32
+ };
33
+
34
+ const mapProduction = (production, type, indentation) => {
35
+ return function* mappedProduction(props) {
36
+ const { grammar } = props;
31
37
  const indent = (strings, ...args) => {
32
38
  return `${indentation}${String.raw(strings, ...args)}`;
33
39
  };
34
40
 
35
- log(`--> ${printType(type)}`);
41
+ yield writeError(`--> ${printType(type)}`);
36
42
 
37
43
  let earlyReturn = true;
38
44
  try {
39
- const generator = production(...args);
45
+ const generator = production(props);
40
46
  let current = generator.next();
41
47
 
42
48
  let anyResult = false;
@@ -44,7 +50,7 @@ const mapProduction = (production, type, indentation, log) => {
44
50
  while (!current.done) {
45
51
  const instr = current.value;
46
52
 
47
- log(indent`${printSource(instr)}`);
53
+ yield writeError(`>>> ` + indent`${printSource(instr)}`.slice(indentation.length));
48
54
 
49
55
  const eats =
50
56
  instr.type === 'Call' && ['eat', 'eatMatch'].includes(getCooked(instr.properties.verb));
@@ -56,21 +62,23 @@ const mapProduction = (production, type, indentation, log) => {
56
62
  current = generator.next(result);
57
63
  }
58
64
 
59
- if (anyResult) {
60
- log(`<-- ${printType(type)}`);
65
+ const allowEmpty = grammar.emptyables?.has(type);
66
+
67
+ if ((anyResult || allowEmpty) && props.s.status === 'active') {
68
+ yield writeError(`<-- ${printType(type)}`);
61
69
  } else {
62
- log(`x-- ${printType(type)}`);
70
+ yield writeError(`x-- ${printType(type)}`);
63
71
  }
64
72
  earlyReturn = false;
65
73
 
66
74
  if (current.value) {
67
- log(indent`${printSource(current.value)}`);
75
+ yield writeError(indent`${printSource(current.value)}`);
68
76
  }
69
77
 
70
78
  return current.value;
71
79
  } finally {
72
80
  if (earlyReturn) {
73
- log(`x-- ${printType(type)}`);
81
+ yield writeError(`x-- ${printType(type)}`);
74
82
  }
75
83
  }
76
84
  };
@@ -89,9 +97,9 @@ export const enhanceLanguageWithDebugLogging = (language, indentation = '') => {
89
97
 
90
98
  export const enhanceWithDebugLogging = enhanceLanguageWithDebugLogging;
91
99
 
92
- export const enhanceProductionWithDebugLogging = (indentation = '', log = console.log) => {
100
+ export const enhanceProductionWithDebugLogging = (indentation = '') => {
93
101
  return (production, type) => {
94
- return mapProduction(production, type, indentation, log);
102
+ return mapProduction(production, type, indentation);
95
103
  };
96
104
  };
97
105
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@bablr/language_enhancer-debug-log",
3
3
  "description": "A BABLR language enhancer that logs production execution",
4
- "version": "0.2.0",
4
+ "version": "0.3.0",
5
5
  "author": "Conrad Buck<conartist6@gmail.com>",
6
6
  "type": "module",
7
7
  "files": [
@@ -12,7 +12,8 @@
12
12
  },
13
13
  "sideEffects": false,
14
14
  "dependencies": {
15
- "@bablr/agast-helpers": "0.1.0"
15
+ "@bablr/agast-helpers": "0.1.6",
16
+ "@bablr/agast-vm-helpers": "0.1.5"
16
17
  },
17
18
  "devDependencies": {
18
19
  "@bablr/eslint-config-base": "github:bablr-lang/eslint-config-base#49f5952efed27f94ee9b94340eb1563c440bf64e",