@bablr/agast-helpers 0.10.10 → 0.11.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/tree.js CHANGED
@@ -1,22 +1,22 @@
1
1
  import {
2
- buildReferenceTag,
3
2
  buildOpenNodeTag,
4
- buildLiteralTag,
5
3
  buildCloseNodeTag,
6
4
  tokenFlags,
7
5
  buildChild,
8
6
  buildGapTag,
9
7
  buildOpenFragmentTag,
8
+ buildReference,
9
+ parseTagType,
10
+ parseTag,
10
11
  } from './builders.js';
11
12
  import {
12
13
  printPrettyCSTML as printPrettyCSTMLFromStream,
13
14
  printCSTML as printCSTMLFromStream,
14
15
  printSource as printSourceFromStream,
15
16
  treeFromStream,
16
- treeFromStreamSync,
17
- treeFromStreamAsync,
18
- evaluateReturnSync,
19
- evaluateReturnAsync,
17
+ evaluateReturn,
18
+ printOpenNodeTag,
19
+ streamFromString,
20
20
  } from './stream.js';
21
21
  import {
22
22
  OpenNodeTag,
@@ -45,11 +45,10 @@ import {
45
45
  isNullNode,
46
46
  isGapNode,
47
47
  getOpenTag,
48
- getCloseTag,
48
+ getCloseNodeTag,
49
49
  getRoot,
50
- getTags,
51
50
  } from './path.js';
52
- import { freeze, isPlainObject } from './object.js';
51
+ import { freeze, freezeRecord, isPlainObject } from './object.js';
53
52
 
54
53
  export {
55
54
  get,
@@ -60,26 +59,22 @@ export {
60
59
  isNullNode,
61
60
  isGapNode,
62
61
  getOpenTag,
63
- getCloseTag,
62
+ getCloseNodeTag,
64
63
  getRoot,
65
64
  treeFromStream,
66
- treeFromStreamSync,
67
- treeFromStreamAsync,
68
- evaluateReturnSync,
69
- evaluateReturnAsync,
65
+ evaluateReturn,
70
66
  };
71
67
 
72
- export const buildToken = (name, value, attributes = {}) => {
73
- return treeFromStreamSync([
74
- buildOpenNodeTag(tokenFlags, name, null, attributes),
75
- buildLiteralTag(value),
76
- buildCloseNodeTag(),
68
+ export const buildToken = (name, value, attributes = freezeRecord({})) => {
69
+ return treeFromStream([
70
+ printOpenNodeTag(buildOpenNodeTag(tokenFlags, name, value, attributes, true)),
77
71
  ]);
78
72
  };
79
73
 
80
- const isString = (str) => typeof str === 'string';
74
+ let isString = (str) => typeof str === 'string';
81
75
 
82
- const { isArray } = Array;
76
+ let { isArray } = Array;
77
+ let { getTags } = Tags;
83
78
 
84
79
  export const mergeReferences = (outer, inner) => {
85
80
  let { type, name, index, flags: { array, expression, intrinsic, hasGap } = {} } = outer;
@@ -98,8 +93,7 @@ export const mergeReferences = (outer, inner) => {
98
93
  name = type === '.' ? inner.name : name;
99
94
  type = type === '.' ? inner.type : type;
100
95
 
101
- return buildReferenceTag(type, name, freeze({ array, expression, intrinsic, hasGap }), index)
102
- .value;
96
+ return buildReference(type, name, freeze({ array, expression, intrinsic, hasGap }), index);
103
97
  };
104
98
 
105
99
  export const mergeReferenceTags = (outer, inner) => {
@@ -120,12 +114,12 @@ export const isEmpty = (node) => {
120
114
  if (node == null) return true;
121
115
 
122
116
  for (const tag of Tags.traverse(getTags(node))) {
123
- switch (tag.type) {
117
+ switch (parseTagType(tag)) {
124
118
  case Property: {
125
- if (tag.value.property.reference.type === '@') {
119
+ if (tag.value.reference.type === '@') {
126
120
  return false;
127
121
  } else {
128
- const { property } = tag.value;
122
+ const property = tag.value;
129
123
 
130
124
  if (!isNullNode(property.node)) {
131
125
  return false;
@@ -142,7 +136,7 @@ export const isEmpty = (node) => {
142
136
  return true;
143
137
  };
144
138
 
145
- export const streamFromTree = (tree, options = {}) => {
139
+ export const streamFromTree = (tree, options = freeze({})) => {
146
140
  if (tree && !isPlainObject(tree)) throw new Error();
147
141
 
148
142
  return __streamFromTree(null, tree, options);
@@ -163,22 +157,22 @@ function* __streamFromTree(doctypeTag, rootNode, options) {
163
157
  do {
164
158
  ({ tagPath, count } = stack.pop());
165
159
  do {
166
- if (tagPath.tag.type === OpenNodeTag && !tagPath.tag.value.selfClosing) count++;
167
- if (tagPath.tag.type === CloseNodeTag) count--;
160
+ if (tagPath.type === OpenNodeTag && !tagPath.value.selfClosing) count++;
161
+ if (tagPath.type === CloseNodeTag) count--;
168
162
 
169
163
  let gapNode;
170
- if (getGapNode && tagPath.tag.type === GapTag && (gapNode = getGapNode(tagPath.path.node))) {
164
+ if (
165
+ getGapNode &&
166
+ tagPath.type === GapTag &&
167
+ (gapNode = getGapNode(tagPath.path.node)) &&
168
+ gapNode !== tagPath.path.node
169
+ ) {
171
170
  stack.push({ tagPath: unshift ? tagPath.nextUnshifted : tagPath.next, count });
172
171
  tagPath = TagPath.fromNode(gapNode, 0);
173
172
  count = 0;
174
173
  }
175
174
 
176
- if (
177
- !(
178
- tagPath.tag.type === AttributeDefinition ||
179
- (tagPath.tag.type === BindingTag && !tagPath.tag.value.segments.length)
180
- )
181
- ) {
175
+ if (!(tagPath.type === AttributeDefinition)) {
182
176
  yield tagPath.tag;
183
177
  }
184
178
  } while ((tagPath = unshift ? tagPath.nextUnshifted : tagPath.next));
@@ -230,9 +224,12 @@ function* __vcsStreamFromTree(rootNode) {
230
224
  } else {
231
225
  let wrappedTag = child;
232
226
 
233
- if (wrappedTag.type === Property) {
227
+ let wrappedType = parseTagType(wrappedTag);
228
+
229
+ if (wrappedType === Property) {
234
230
  for (let tag of wrappedTag.value.tags) {
235
- switch (tag.type) {
231
+ let tagType = parseTagType(tag);
232
+ switch (tagType) {
236
233
  case TreeNode:
237
234
  case NullNode:
238
235
  case GapNode: {
@@ -250,7 +247,7 @@ function* __vcsStreamFromTree(rootNode) {
250
247
 
251
248
  depth++;
252
249
  i = 0;
253
- agastNode = tag.value;
250
+ agastNode = tag;
254
251
  nodeShifted = wrappedTag.value.tags[0].type === ShiftTag;
255
252
  seenFirstProperty = false;
256
253
  continue outer;
@@ -260,7 +257,7 @@ function* __vcsStreamFromTree(rootNode) {
260
257
  default: {
261
258
  yield tag;
262
259
 
263
- if (tag.type === OpenNodeTag && tag.value.literalValue) {
260
+ if (tagType === OpenNodeTag && parseTag(tag).value.literalValue) {
264
261
  ({ stack, agastNode, node, depth, i, nodeShifted } = stack);
265
262
  seenFirstProperty = true;
266
263
  }
@@ -272,7 +269,7 @@ function* __vcsStreamFromTree(rootNode) {
272
269
  } else {
273
270
  yield wrappedTag;
274
271
 
275
- if (wrappedTag.type === OpenNodeTag && wrappedTag.value.literalValue) {
272
+ if (wrappedType === OpenNodeTag && wrappedTag.value.literalValue) {
276
273
  ({ stack, agastNode, node, depth, i, nodeShifted } = stack);
277
274
  seenFirstProperty = true;
278
275
  }
@@ -292,11 +289,12 @@ export const getCooked = (cookable) => {
292
289
  let cooked = '';
293
290
 
294
291
  // const openTag = getOpenTag(cookable);
295
- // const closeTag = getCloseTag(cookable);
292
+ // const closeTag = getCloseNodeTag(cookable);
296
293
 
297
294
  let referenceTag = null;
298
295
 
299
- for (let tag of Tags.traverse(tags)) {
296
+ for (let tag_ of Tags.traverse(tags)) {
297
+ let tag = parseTag(tag_);
300
298
  switch (tag.type) {
301
299
  case ReferenceTag: {
302
300
  let { type } = tag.value;
@@ -338,6 +336,9 @@ export const getCooked = (cookable) => {
338
336
  }
339
337
 
340
338
  case OpenNodeTag: {
339
+ if (tag.value.literalValue) {
340
+ cooked += tag.value.literalValue;
341
+ }
341
342
  break;
342
343
  }
343
344
 
@@ -354,11 +355,15 @@ export const getCooked = (cookable) => {
354
355
  return cooked;
355
356
  };
356
357
 
358
+ export const treeFromString = (input) => {
359
+ return treeFromStream(streamFromString(input));
360
+ };
361
+
357
362
  export const printCSTML = (tree) => {
358
363
  return printCSTMLFromStream(streamFromTree(tree));
359
364
  };
360
365
 
361
- export const printPrettyCSTML = (tree, options = {}) => {
366
+ export const printPrettyCSTML = (tree, options = freeze({})) => {
362
367
  return printPrettyCSTMLFromStream(streamFromTree(tree), options);
363
368
  };
364
369
 
package/package.json CHANGED
@@ -1,21 +1,26 @@
1
1
  {
2
2
  "name": "@bablr/agast-helpers",
3
3
  "description": "Helper functions for working with agAST trees",
4
- "version": "0.10.10",
4
+ "version": "0.11.0",
5
5
  "author": "Conrad Buck<conartist6@gmail.com>",
6
6
  "type": "module",
7
7
  "files": [
8
8
  "lib"
9
9
  ],
10
10
  "exports": {
11
- "./btree": "./lib/btree.js",
11
+ "./b-list-keyed": "./lib/b-list-keyed.js",
12
+ "./b-list": "./lib/b-list.js",
13
+ "./b-map": "./lib/b-map.js",
14
+ "./b-set": "./lib/b-set.js",
12
15
  "./builders": "./lib/builders.js",
16
+ "./debug": "./lib/debug.js",
17
+ "./debug/register": "./lib/debug.register.js",
18
+ "./iterable": "./lib/iterable.js",
13
19
  "./object": "./lib/object.js",
14
- "./path-facade": "./lib/path-facade.js",
20
+ "./parse": "./lib/parse.js",
15
21
  "./path": "./lib/path.js",
16
22
  "./print": "./lib/print.js",
17
23
  "./shorthand": "./lib/shorthand.js",
18
- "./spans": "./lib/spans.js",
19
24
  "./stream": "./lib/stream.js",
20
25
  "./symbols": "./lib/symbols.js",
21
26
  "./tags": "./lib/tags.js",
@@ -27,13 +32,14 @@
27
32
  "test": "mocha test/*.test.js"
28
33
  },
29
34
  "dependencies": {
30
- "@bablr/btree": "0.4.5",
31
- "@bablr/coroutine": "0.1.0",
32
- "@bablr/stream-iterator": "2.0.0",
35
+ "@bablr/btree": "1.0.0",
36
+ "@bablr/coroutine": "1.0.1",
37
+ "@bablr/stream-iterator": "3.0.0",
33
38
  "@iter-tools/imm-stack": "1.2.0"
34
39
  },
35
40
  "devDependencies": {
36
- "@bablr/eslint-config-base": "github:bablr-lang/eslint-config-base#c97bfa4b3663f8378e9b3e42bb5a41e685406cf9",
41
+ "@bablr/record": "1.0.0",
42
+ "@bablr/eslint-config-base": "github:bablr-lang/eslint-config-base#23b94fb3bdfdbc3ac7ed9cb58d7979d8f07dce2b",
37
43
  "@qnighy/dedent": "0.1.1",
38
44
  "enhanced-resolve": "^5.12.0",
39
45
  "eslint": "^8.32.0",
package/lib/btree.js DELETED
@@ -1 +0,0 @@
1
- export * from '@bablr/btree';