@bablr/language_enhancer-debug-log 0.2.1 → 0.4.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 +28 -16
  2. package/package.json +3 -2
package/lib/index.js CHANGED
@@ -1,9 +1,14 @@
1
1
  /* global console */
2
2
 
3
3
  import { printSource, getCooked } from '@bablr/agast-helpers/tree';
4
- import { printType } from '@bablr/agast-helpers/print';
4
+ import { buildCall, buildNumber, buildObject, buildString } from '@bablr/agast-vm-helpers';
5
+ import { printType as printType_ } from '@bablr/agast-helpers/print';
5
6
 
6
- const { getOwnPropertyNames, hasOwn } = Object;
7
+ const { getOwnPropertyNames, getOwnPropertySymbols, hasOwn } = Object;
8
+
9
+ const printType = (type) => {
10
+ return type == null ? '[null]' : printType_(type);
11
+ };
7
12
 
8
13
  // TODO this lives in bablr-helpers but I can't use it there
9
14
  // it would create a circular dep at the module level
@@ -14,8 +19,8 @@ const mapProductions = (fn, Grammar) => {
14
19
 
15
20
  const mapped = MappedGrammar.prototype;
16
21
 
17
- while (prototype) {
18
- for (const key of getOwnPropertyNames(prototype)) {
22
+ while (prototype && prototype !== Object.prototype) {
23
+ for (const key of [...getOwnPropertyNames(prototype), ...getOwnPropertySymbols(prototype)]) {
19
24
  if (!hasOwn(mapped, key)) {
20
25
  mapped[key] = fn(prototype[key], key);
21
26
  }
@@ -26,17 +31,22 @@ const mapProductions = (fn, Grammar) => {
26
31
  return MappedGrammar;
27
32
  };
28
33
 
29
- const mapProduction = (production, type, indentation, log) => {
30
- return function* (...args) {
34
+ const writeError = (text) => {
35
+ return buildCall('write', buildString(text), buildObject({ stream: buildNumber(2) }));
36
+ };
37
+
38
+ const mapProduction = (production, type, indentation) => {
39
+ return function* mappedProduction(props) {
40
+ const { grammar } = props;
31
41
  const indent = (strings, ...args) => {
32
42
  return `${indentation}${String.raw(strings, ...args)}`;
33
43
  };
34
44
 
35
- log(`--> ${printType(type)}`);
45
+ yield writeError(`--> ${printType(type)}`);
36
46
 
37
47
  let earlyReturn = true;
38
48
  try {
39
- const generator = production(...args);
49
+ const generator = production(props);
40
50
  let current = generator.next();
41
51
 
42
52
  let anyResult = false;
@@ -44,7 +54,7 @@ const mapProduction = (production, type, indentation, log) => {
44
54
  while (!current.done) {
45
55
  const instr = current.value;
46
56
 
47
- log(indent`${printSource(instr)}`);
57
+ yield writeError(`>>> ` + indent`${printSource(instr)}`.slice(indentation.length));
48
58
 
49
59
  const eats =
50
60
  instr.type === 'Call' && ['eat', 'eatMatch'].includes(getCooked(instr.properties.verb));
@@ -56,21 +66,23 @@ const mapProduction = (production, type, indentation, log) => {
56
66
  current = generator.next(result);
57
67
  }
58
68
 
59
- if (anyResult) {
60
- log(`<-- ${printType(type)}`);
69
+ const allowEmpty = grammar.emptyables?.has(type);
70
+
71
+ if ((anyResult || allowEmpty) && props.s.status === 'active') {
72
+ yield writeError(`<-- ${printType(type)}`);
61
73
  } else {
62
- log(`x-- ${printType(type)}`);
74
+ yield writeError(`x-- ${printType(type)}`);
63
75
  }
64
76
  earlyReturn = false;
65
77
 
66
78
  if (current.value) {
67
- log(indent`${printSource(current.value)}`);
79
+ yield writeError(indent`${printSource(current.value)}`);
68
80
  }
69
81
 
70
82
  return current.value;
71
83
  } finally {
72
84
  if (earlyReturn) {
73
- log(`x-- ${printType(type)}`);
85
+ yield writeError(`x-- ${printType(type)}`);
74
86
  }
75
87
  }
76
88
  };
@@ -89,9 +101,9 @@ export const enhanceLanguageWithDebugLogging = (language, indentation = '') => {
89
101
 
90
102
  export const enhanceWithDebugLogging = enhanceLanguageWithDebugLogging;
91
103
 
92
- export const enhanceProductionWithDebugLogging = (indentation = '', log = console.log) => {
104
+ export const enhanceProductionWithDebugLogging = (indentation = '') => {
93
105
  return (production, type) => {
94
- return mapProduction(production, type, indentation, log);
106
+ return mapProduction(production, type, indentation);
95
107
  };
96
108
  };
97
109
 
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.1",
4
+ "version": "0.4.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.1"
15
+ "@bablr/agast-helpers": "0.2.0",
16
+ "@bablr/agast-vm-helpers": "0.2.0"
16
17
  },
17
18
  "devDependencies": {
18
19
  "@bablr/eslint-config-base": "github:bablr-lang/eslint-config-base#49f5952efed27f94ee9b94340eb1563c440bf64e",