@bablr/agast-helpers 0.5.2 → 0.6.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/print.js CHANGED
@@ -2,45 +2,77 @@ import {
2
2
  DoctypeTag,
3
3
  OpenNodeTag,
4
4
  CloseNodeTag,
5
- OpenFragmentTag,
6
- CloseFragmentTag,
7
5
  ReferenceTag,
8
6
  ShiftTag,
9
7
  GapTag,
10
8
  NullTag,
11
- ArrayTag,
9
+ ArrayInitializerTag,
12
10
  LiteralTag,
13
11
  EmbeddedNode,
14
12
  EmbeddedTag,
15
- EmbeddedExpression,
13
+ EmbeddedObject,
14
+ EmbeddedMatcher,
15
+ EmbeddedRegex,
16
16
  } from './symbols.js';
17
+ import { isEmptyReference, isGapNode, isNullNode, printSource, referenceFlags } from './tree.js';
17
18
 
18
- const { isInteger, isFinite } = Number;
19
- const { isArray } = Array;
20
- const isString = (val) => typeof val === 'string';
21
- const isNumber = (val) => typeof val === 'number';
22
- const isObject = (val) => val && typeof val === 'object' && !isArray(val);
23
- const isFunction = (val) => typeof val === 'function';
19
+ let { isInteger, isFinite } = Number;
20
+ let { isArray } = Array;
21
+ let isString = (val) => typeof val === 'string';
22
+ let isNumber = (val) => typeof val === 'number';
23
+ let isObject = (val) => val && typeof val === 'object' && !isArray(val);
24
+ let isFunction = (val) => typeof val === 'function';
24
25
 
25
- const when = (condition, value) =>
26
+ let when = (condition, value) =>
26
27
  condition ? (isFunction(value) ? value() : value) : { *[Symbol.iterator]() {} };
27
28
 
28
29
  export const printCall = (call) => {
29
- const { verb, arguments: args } = call;
30
- return `${verb}${printTuple(args)}`;
30
+ let { verb, arguments: args } = call;
31
+ return `${verb}${`(${args.map((v) => printExpression(v)).join(' ')})`}`;
31
32
  };
32
33
 
33
- export const printArray = (arr) => `[${arr.map((v) => printExpression(v)).join(' ')}]`;
34
-
35
- export const printTuple = (tup) => `(${tup.map((v) => printExpression(v)).join(' ')})`;
34
+ export const printArray = (arr) => `[${arr.map((v) => printExpression(v)).join(', ')}]`;
36
35
 
37
36
  export const printObject = (obj) => {
38
- const entries = Object.entries(obj);
37
+ let entries = Object.entries(obj);
39
38
  return entries.length
40
- ? `{ ${entries.map(([k, v]) => `${k}: ${printExpression(v)}`).join(' ')} }`
39
+ ? `{ ${entries.map(([k, v]) => `${k}: ${printExpression(v)}`).join(', ')} }`
41
40
  : '{}';
42
41
  };
43
42
 
43
+ export const printPropertyMatcher = (matcher) => {
44
+ let { refMatcher, nodeMatcher } = matcher;
45
+ let ref = { type: ReferenceTag, value: refMatcher };
46
+ let refPart = refMatcher && !isEmptyReference(ref) ? `${printReferenceTag(ref)} ` : '';
47
+ let nodePart;
48
+
49
+ if (isArray(nodeMatcher)) {
50
+ if (nodeMatcher.length) throw new Error();
51
+ nodePart = '[]';
52
+ } else if (isGapNode(nodeMatcher)) {
53
+ nodePart = '<//>';
54
+ } else if (isNullNode(nodeMatcher)) {
55
+ nodePart = 'null';
56
+ } else {
57
+ nodePart = printOpenNodeMatcher(nodeMatcher);
58
+ }
59
+ return `${refPart}${nodePart}`;
60
+ };
61
+
62
+ export const printOpenNodeMatcher = (matcher) => {
63
+ let { flags, language: tagLanguage, type, attributes, intrinsicValue } = matcher;
64
+
65
+ let printedAttributes = printAttributes(attributes);
66
+ let attributesFrag = printedAttributes ? ` ${printedAttributes}` : '';
67
+
68
+ let intrinsicFrag = intrinsicValue
69
+ ? ` ${isString(intrinsicValue) ? printExpression(intrinsicValue) : printSource(intrinsicValue)}`
70
+ : '';
71
+ let typeFrag = type !== Symbol.for('@bablr/fragment') ? printTagPath(tagLanguage, type) : ' ';
72
+
73
+ return `<${printNodeFlags(flags)}${typeFrag}${intrinsicFrag}${attributesFrag}/>`;
74
+ };
75
+
44
76
  export const printExpression = (expr) => {
45
77
  if (isString(expr)) {
46
78
  return printString(expr);
@@ -67,15 +99,20 @@ export const printExpression = (expr) => {
67
99
  export const printEmbedded = (value) => {
68
100
  switch (value.type) {
69
101
  case EmbeddedTag:
70
- return printTag(value.value);
102
+ return `t\`${printTag(value.value)}\``;
103
+
104
+ case EmbeddedMatcher:
105
+ return `m\`${printSource(value.value)}\``;
106
+
107
+ case EmbeddedRegex:
108
+ return `re\`${printSource(value.value)}\``;
71
109
 
72
- case EmbeddedExpression: {
110
+ case EmbeddedObject: {
73
111
  return printObject(value.value);
74
112
  }
75
113
 
76
114
  case EmbeddedNode: {
77
- throw new Error('not implemented');
78
- break;
115
+ return printSource(value.value);
79
116
  }
80
117
 
81
118
  default:
@@ -84,9 +121,8 @@ export const printEmbedded = (value) => {
84
121
  };
85
122
 
86
123
  export const printAttributes = (attributes) => {
87
- return Object.entries(attributes)
88
- .map(([k, v]) => (v === true ? k : v === false ? `!${k}` : `${k}=${printExpression(v)}`))
89
- .join(' ');
124
+ const printed = attributes && printObject(attributes);
125
+ return !printed || printed === '{}' ? '' : printed;
90
126
  };
91
127
 
92
128
  export const printLanguage = (language) => {
@@ -104,7 +140,7 @@ export const printTagPath = (language, type) => {
104
140
  ].join(':');
105
141
  };
106
142
 
107
- const escapeReplacer = (esc) => {
143
+ let escapeReplacer = (esc) => {
108
144
  if (esc === '\r') {
109
145
  return '\\r';
110
146
  } else if (esc === '\n') {
@@ -138,8 +174,8 @@ export const printGapTag = (tag) => {
138
174
  return `<//>`;
139
175
  };
140
176
 
141
- export const printArrayTag = (tag) => {
142
- if (tag?.type !== ArrayTag) throw new Error();
177
+ export const printArrayInitializerTag = (tag) => {
178
+ if (tag?.type !== ArrayInitializerTag) throw new Error();
143
179
 
144
180
  return `[]`;
145
181
  };
@@ -153,11 +189,10 @@ export const printShiftTag = (tag) => {
153
189
  export const printReferenceTag = (tag) => {
154
190
  if (tag?.type !== ReferenceTag) throw new Error();
155
191
 
156
- const { name, isArray, hasGap } = tag.value;
157
- const pathBraces = isArray ? '[]' : '';
158
- const gapDollar = hasGap ? '$' : '';
192
+ let { name, isArray, flags, index } = tag.value;
193
+ let pathBraces = isArray ? `[${index || ''}]` : '';
159
194
 
160
- return `${name}${pathBraces}${gapDollar}:`;
195
+ return `${name || ''}${pathBraces}${printReferenceFlags(flags)}:`;
161
196
  };
162
197
 
163
198
  export const printNullTag = (tag) => {
@@ -193,47 +228,45 @@ export const printLiteralTag = (tag) => {
193
228
  return printString(tag.value);
194
229
  };
195
230
 
196
- export const printFlags = (flags) => {
197
- const hash = flags.trivia ? '#' : '';
198
- const star = flags.token ? '*' : '';
199
- const at = flags.escape ? '@' : '';
200
- const plus = flags.expression ? '+' : '';
201
- const dollar = flags.hasGap ? '$' : '';
231
+ export const printReferenceFlags = (flags = referenceFlags) => {
232
+ let plus = flags.expression ? '+' : '';
233
+ let dollar = flags.hasGap ? '$' : '';
202
234
 
203
- if (flags.escape && flags.trivia) throw new Error('Node cannot be escape and trivia');
235
+ return `${plus}${dollar}`;
236
+ };
237
+
238
+ export const printNodeFlags = (flags) => {
239
+ let star = flags.token ? '*' : '';
240
+ let dollar = flags.hasGap ? '$' : '';
204
241
 
205
- return `${hash}${star}${at}${plus}${dollar}`;
242
+ return `${star}${dollar}`;
206
243
  };
207
244
 
208
245
  export const printOpenNodeTag = (tag) => {
209
246
  if (tag?.type !== OpenNodeTag) throw new Error();
210
247
 
211
- const { flags, language: tagLanguage, type, attributes } = tag.value;
212
-
213
- const printedAttributes = attributes && printAttributes(attributes);
214
- const attributesFrag = printedAttributes ? ` ${printedAttributes}` : '';
248
+ let { flags, language: tagLanguage, type, attributes } = tag.value;
215
249
 
216
- return `<${printFlags(flags)}${printTagPath(tagLanguage, type)}${attributesFrag}>`;
217
- };
218
-
219
- export const printOpenFragmentTag = (tag) => {
220
- if (tag?.type !== OpenFragmentTag) throw new Error();
250
+ if (!type) {
251
+ return `<${printNodeFlags(flags)}>`;
252
+ }
221
253
 
222
- const { flags } = tag.value;
254
+ let printedAttributes = printAttributes(attributes);
255
+ let attributesFrag = printedAttributes ? ` ${printedAttributes}` : '';
223
256
 
224
- return `<${printFlags(flags)}>`;
257
+ return `<${printNodeFlags(flags)}${printTagPath(tagLanguage, type)}${attributesFrag}>`;
225
258
  };
226
259
 
227
260
  export const printSelfClosingNodeTag = (tag, intrinsicValue) => {
228
261
  if (tag?.type !== OpenNodeTag) throw new Error();
229
262
 
230
- const { flags, language: tagLanguage, type, attributes } = tag.value;
263
+ let { flags, language: tagLanguage, type, attributes } = tag.value;
231
264
 
232
- const printedAttributes = attributes && printAttributes(attributes);
233
- const attributesFrag = printedAttributes ? ` ${printedAttributes}` : '';
234
- const intrinsicFrag = intrinsicValue ? ` ${printString(intrinsicValue)}` : '';
265
+ let printedAttributes = printAttributes(attributes);
266
+ let attributesFrag = printedAttributes ? ` ${printedAttributes}` : '';
267
+ let intrinsicFrag = intrinsicValue ? ` ${printString(intrinsicValue)}` : '';
235
268
 
236
- return `<${printFlags(flags)}${printTagPath(
269
+ return `<${printNodeFlags(flags)}${printTagPath(
237
270
  tagLanguage,
238
271
  type,
239
272
  )}${intrinsicFrag}${attributesFrag} />`;
@@ -245,12 +278,6 @@ export const printCloseNodeTag = (tag) => {
245
278
  return `</>`;
246
279
  };
247
280
 
248
- export const printCloseFragmentTag = (tag) => {
249
- if (tag?.type !== CloseFragmentTag) throw new Error();
250
-
251
- return `</>`;
252
- };
253
-
254
281
  export const printTag = (tag) => {
255
282
  if (!isObject(tag)) throw new Error();
256
283
 
@@ -261,8 +288,8 @@ export const printTag = (tag) => {
261
288
  case GapTag:
262
289
  return printGapTag(tag);
263
290
 
264
- case ArrayTag:
265
- return printArrayTag(tag);
291
+ case ArrayInitializerTag:
292
+ return printArrayInitializerTag(tag);
266
293
 
267
294
  case ShiftTag:
268
295
  return printShiftTag(tag);
@@ -282,12 +309,6 @@ export const printTag = (tag) => {
282
309
  case CloseNodeTag:
283
310
  return printCloseNodeTag(tag);
284
311
 
285
- case OpenFragmentTag:
286
- return printOpenFragmentTag(tag);
287
-
288
- case CloseFragmentTag:
289
- return printCloseFragmentTag(tag);
290
-
291
312
  default:
292
313
  throw new Error();
293
314
  }
package/lib/shorthand.js CHANGED
@@ -1,22 +1,12 @@
1
1
  import {
2
- buildReferenceTag,
2
+ buildDoctypeTag,
3
3
  buildGapTag,
4
- buildEmbeddedNode,
5
- buildNodeOpenTag,
6
- buildNodeCloseTag,
4
+ buildOpenNodeTag,
5
+ buildCloseNodeTag,
7
6
  buildLiteralTag,
8
- buildNullNode,
9
- buildArrayTag,
10
- buildNode,
11
- buildFragment,
12
- buildGapNode,
13
- buildSyntacticNode,
14
- buildEscapeNode,
15
- buildSyntacticEscapeNode,
16
- buildSyntacticTriviaNode,
17
- buildTriviaNode,
7
+ buildArrayInitializerTag,
18
8
  } from './builders.js';
19
- import { parsePath } from './path.js';
9
+ import { parseReference, treeFromStreamSync } from './tree.js';
20
10
 
21
11
  export * from './builders.js';
22
12
 
@@ -34,24 +24,14 @@ const stripArray = (val) => {
34
24
  };
35
25
 
36
26
  export const ref = (path) => {
37
- const { name, isArray: pathIsArray } = parsePath(isArray(path) ? path[0] : path);
38
-
39
- return buildReferenceTag(name, pathIsArray);
27
+ return parseReference(isArray(path) ? path[0] : path);
40
28
  };
41
29
 
42
30
  export const lit = (str) => buildLiteralTag(stripArray(str));
43
31
 
32
+ export const doctype = buildDoctypeTag;
44
33
  export const gap = buildGapTag;
45
- export const arr = buildArrayTag;
46
- export const embedded = buildEmbeddedNode;
47
- export const nodeOpen = buildNodeOpenTag;
48
- export const nodeClose = buildNodeCloseTag;
49
- export const node = buildNode;
50
- export const frag = buildFragment;
51
- export const g_node = buildGapNode;
52
- export const s_node = buildSyntacticNode;
53
- export const e_node = buildEscapeNode;
54
- export const s_e_node = buildSyntacticEscapeNode;
55
- export const s_t_node = buildSyntacticTriviaNode;
56
- export const t_node = buildTriviaNode;
57
- export const null_node = buildNullNode;
34
+ export const arr = buildArrayInitializerTag;
35
+ export const nodeOpen = buildOpenNodeTag;
36
+ export const nodeClose = buildCloseNodeTag;
37
+ export const tree = (...tags) => treeFromStreamSync(tags);