@bablr/bablr-vm 0.13.0 → 0.13.2

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/evaluate.js CHANGED
@@ -1,11 +1,12 @@
1
1
  import { Coroutine } from '@bablr/coroutine';
2
- import { reifyExpression } from '@bablr/agast-vm-helpers';
2
+ import { buildCall, buildNull, buildReference, reifyExpression } from '@bablr/agast-vm-helpers';
3
3
  import { StreamGenerator } from '@bablr/agast-helpers/stream';
4
4
  import { resolveLanguage } from '@bablr/helpers/grammar';
5
5
  import { buildTokens } from './utils/token.js';
6
6
  import { formatType } from './utils/format.js';
7
7
  import { facades } from './facades.js';
8
8
  import { State } from './state.js';
9
+ import { updateSpans } from './spans.js';
9
10
 
10
11
  const nodeTopType = Symbol.for('@bablr/node');
11
12
 
@@ -23,103 +24,6 @@ export const evaluate = (ctx, rootLanguage, rootSource, strategy) => {
23
24
 
24
25
  const resolvedLanguages = new WeakMap();
25
26
 
26
- function updateSpans(ctx, s, node, phase) {
27
- switch (phase) {
28
- case 'open': {
29
- const { attributes, flags } = node;
30
- const { span: innerSpan, balanced, balancedSpan, balancer, openSpan } = attributes || {};
31
-
32
- if (!flags.intrinsic && (balancer || balanced)) {
33
- throw new Error('not implemented');
34
- }
35
-
36
- if (flags.intrinsic) {
37
- if (s.path) {
38
- if (balancedSpan) {
39
- if (!balanced) throw new Error();
40
-
41
- s.spans = s.spans.push({
42
- type: 'Lexical',
43
- name: balancedSpan,
44
- path: s.path,
45
- guard: balanced,
46
- });
47
- }
48
-
49
- if (innerSpan) {
50
- throw new Error();
51
- }
52
- }
53
- }
54
-
55
- if (openSpan) {
56
- s.spans = s.spans.push({
57
- type: 'Explicit',
58
- name: openSpan,
59
- path: s.path,
60
- guard: null,
61
- });
62
- }
63
-
64
- if (balancer) {
65
- const balancedNode = s.balanced.value;
66
-
67
- if (!s.balanced.size) throw new Error();
68
-
69
- s.balanced = s.balanced.pop();
70
-
71
- if (balancer && balancedNode.openTag.value.attributes.balancedSpan) {
72
- s.spans = s.spans.pop();
73
- }
74
- }
75
-
76
- if (balanced) {
77
- s.balanced = s.balanced.push(ctx.agast.nodeForTag(s.result));
78
- }
79
-
80
- if (innerSpan) {
81
- s.spans = s.spans.push({
82
- type: 'Inner',
83
- name: innerSpan,
84
- path: s.path,
85
- guard: null,
86
- });
87
- }
88
-
89
- break;
90
- }
91
-
92
- case 'close': {
93
- const { openTag, flags } = node;
94
- const { attributes } = openTag.value;
95
- const { balancedSpan, span: innerSpan, closeSpan, balanced } = attributes || {};
96
-
97
- if (balancedSpan && !flags.intrinsic) {
98
- if (!balanced) throw new Error();
99
-
100
- s.spans = s.spans.push({
101
- type: 'Lexical',
102
- name: balancedSpan,
103
- path: s.path,
104
- guard: balanced,
105
- });
106
- }
107
-
108
- if (closeSpan) {
109
- if (s.spans.value.type !== 'Explicit') throw new Error();
110
- s.spans = s.spans.pop();
111
- }
112
-
113
- if (innerSpan) {
114
- s.spans = s.spans.pop();
115
- }
116
- break;
117
- }
118
- default:
119
- throw new Error();
120
- }
121
- }
122
-
123
27
  const __evaluate = function* bablrStrategy(ctx, rootLanguage, rootSource, agastState, strategy) {
124
28
  let s = State.from(ctx, rootSource, agastState);
125
29
 
@@ -381,6 +285,26 @@ const __evaluate = function* bablrStrategy(ctx, rootLanguage, rootSource, agastS
381
285
 
382
286
  s = s.parent;
383
287
 
288
+ if (rejectedState.path.depth > s.path.depth) {
289
+ const lowPath = rejectedState.path.at(
290
+ Math.min(s.path.depth + 1, rejectedState.path.depth),
291
+ );
292
+
293
+ if (s.path.depth) {
294
+ const { name, isArray } = lowPath.reference?.value || {};
295
+
296
+ if (s.node.depth === s.path.depth && !s.node.resolver.counters.has(name)) {
297
+ if (!lowPath.openTag?.value.flags.trivia && !lowPath.openTag?.value.flags.escape) {
298
+ yield buildCall('advance', buildReference(name, isArray));
299
+ }
300
+
301
+ if (s.result.type === 'Reference') {
302
+ yield buildCall('advance', buildNull());
303
+ }
304
+ }
305
+ }
306
+ }
307
+
384
308
  if (!s) throw new Error('rejected root state');
385
309
 
386
310
  rejectedState.source.reject();
package/lib/spans.js ADDED
@@ -0,0 +1,92 @@
1
+ export function updateSpans(ctx, s, node, phase) {
2
+ switch (phase) {
3
+ case 'open': {
4
+ const { attributes, flags } = node;
5
+ const { span: innerSpan, balanced, balancedSpan, balancer, openSpan } = attributes || {};
6
+
7
+ if (!flags.intrinsic && (balancer || balanced)) {
8
+ throw new Error('not implemented');
9
+ }
10
+
11
+ if (flags.intrinsic) {
12
+ if (s.path && balanced) {
13
+ s.spans = s.spans.push({
14
+ type: 'Lexical',
15
+ name: balancedSpan || s.span.name,
16
+ path: s.path,
17
+ guard: balanced,
18
+ });
19
+
20
+ if (innerSpan) {
21
+ throw new Error();
22
+ }
23
+ }
24
+ }
25
+
26
+ if (openSpan) {
27
+ s.spans = s.spans.push({
28
+ type: 'Explicit',
29
+ name: openSpan,
30
+ path: s.path,
31
+ guard: null,
32
+ });
33
+ }
34
+
35
+ if (balancer) {
36
+ const balancedNode = s.balanced.value;
37
+
38
+ if (!s.balanced.size) throw new Error();
39
+
40
+ if (!balancedNode.openTag.value.attributes.balanced) {
41
+ throw new Error();
42
+ }
43
+
44
+ s.balanced = s.balanced.pop();
45
+
46
+ s.spans = s.spans.pop();
47
+ }
48
+
49
+ if (balanced) {
50
+ s.balanced = s.balanced.push(ctx.agast.nodeForTag(s.result));
51
+ }
52
+
53
+ if (innerSpan) {
54
+ s.spans = s.spans.push({
55
+ type: 'Inner',
56
+ name: innerSpan,
57
+ path: s.path,
58
+ guard: null,
59
+ });
60
+ }
61
+
62
+ break;
63
+ }
64
+
65
+ case 'close': {
66
+ const { openTag, flags } = node;
67
+ const { attributes } = openTag.value;
68
+ const { balancedSpan, span: innerSpan, closeSpan, balanced } = attributes || {};
69
+
70
+ if (balanced && !flags.intrinsic) {
71
+ s.spans = s.spans.push({
72
+ type: 'Lexical',
73
+ name: balancedSpan || s.span.name,
74
+ path: s.path,
75
+ guard: balanced,
76
+ });
77
+ }
78
+
79
+ if (closeSpan) {
80
+ if (s.spans.value.type !== 'Explicit') throw new Error();
81
+ s.spans = s.spans.pop();
82
+ }
83
+
84
+ if (innerSpan) {
85
+ s.spans = s.spans.pop();
86
+ }
87
+ break;
88
+ }
89
+ default:
90
+ throw new Error();
91
+ }
92
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@bablr/bablr-vm",
3
3
  "description": "A VM for parsing using BABLR languages",
4
- "version": "0.13.0",
4
+ "version": "0.13.2",
5
5
  "author": "Conrad Buck<conartist6@gmail.com>",
6
6
  "type": "module",
7
7
  "files": [
@@ -12,10 +12,10 @@
12
12
  },
13
13
  "sideEffects": false,
14
14
  "dependencies": {
15
- "@bablr/agast-helpers": "0.1.0",
16
- "@bablr/agast-vm-helpers": "0.1.0",
15
+ "@bablr/agast-helpers": "0.1.1",
16
+ "@bablr/agast-vm-helpers": "0.1.1",
17
17
  "@bablr/coroutine": "0.1.0",
18
- "@bablr/helpers": "0.15.0",
18
+ "@bablr/helpers": "0.15.1",
19
19
  "@bablr/regex-vm": "0.5.0",
20
20
  "@bablr/weak-stack": "0.1.0",
21
21
  "@iter-tools/imm-stack": "1.1.0",