@bablr/agast-vm 0.8.0 → 0.9.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/README.md +14 -49
- package/lib/evaluate.js +116 -147
- package/lib/index.js +2 -1
- package/lib/node.js +176 -0
- package/lib/path.js +160 -1
- package/lib/state.js +256 -146
- package/package.json +3 -6
- package/lib/context.js +0 -88
- package/lib/utils/array.js +0 -7
- package/lib/utils/iterable.js +0 -13
- package/lib/utils/skip.js +0 -39
package/lib/path.js
CHANGED
|
@@ -1,3 +1,162 @@
|
|
|
1
|
-
|
|
1
|
+
import { Property } from '@bablr/agast-helpers/symbols';
|
|
2
|
+
import { NodeFacade, nodes } from './node.js';
|
|
3
|
+
import { Path, TagPath } from '@bablr/agast-helpers/path';
|
|
4
|
+
import { buildChild, buildFacadeProperty, buildProperty } from '@bablr/agast-helpers/builders';
|
|
5
|
+
|
|
6
|
+
export { Path, TagPath } from '@bablr/agast-helpers/path';
|
|
2
7
|
|
|
3
8
|
export { allTagPathsFor, ownTagPathsFor } from '@bablr/agast-helpers/path';
|
|
9
|
+
|
|
10
|
+
let { freeze } = Object;
|
|
11
|
+
|
|
12
|
+
let paths = new WeakMap();
|
|
13
|
+
|
|
14
|
+
export class TagPathFacade {
|
|
15
|
+
static from(path, childrenIndex) {
|
|
16
|
+
return path && new TagPathFacade(TagPath.from(paths.get(path), childrenIndex));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
static fromNode(node, childrenIndex) {
|
|
20
|
+
let path = node && TagPath.fromNode(nodes.get(node), childrenIndex);
|
|
21
|
+
return path && new TagPathFacade(path);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
static wrap(tagPath) {
|
|
25
|
+
return tagPath && new TagPathFacade(tagPath);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
constructor(tagPath) {
|
|
29
|
+
if (!(tagPath instanceof TagPath)) throw new Error();
|
|
30
|
+
paths.set(this, tagPath);
|
|
31
|
+
|
|
32
|
+
let { tag } = tagPath;
|
|
33
|
+
|
|
34
|
+
this.path = new PathFacade(tagPath.path);
|
|
35
|
+
this.node = new NodeFacade(tagPath.node);
|
|
36
|
+
this.tag =
|
|
37
|
+
tag.type === Property
|
|
38
|
+
? buildChild(
|
|
39
|
+
Property,
|
|
40
|
+
buildFacadeProperty(
|
|
41
|
+
tag.value.reference,
|
|
42
|
+
tag.value.binding,
|
|
43
|
+
new NodeFacade(tag.value.node),
|
|
44
|
+
),
|
|
45
|
+
)
|
|
46
|
+
: tag;
|
|
47
|
+
this.childrenIndex = tagPath.childrenIndex;
|
|
48
|
+
|
|
49
|
+
freeze(this);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
get child() {
|
|
53
|
+
return this.tag;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
siblingAt(index) {
|
|
57
|
+
return TagPathFacade.wrap(paths.get(this).siblingAt(index));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
get nextSibling() {
|
|
61
|
+
return TagPathFacade.wrap(paths.get(this).nextSibling);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
get next() {
|
|
65
|
+
return TagPathFacade.wrap(paths.get(this).next);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
get nextUnshifted() {
|
|
69
|
+
return TagPathFacade.wrap(paths.get(this).nextUnshifted);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
get previousSibling() {
|
|
73
|
+
return TagPathFacade.wrap(paths.get(this).previousSibling);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
get previous() {
|
|
77
|
+
return TagPathFacade.wrap(paths.get(this).previous);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
get previousUnshifted() {
|
|
81
|
+
return TagPathFacade.wrap(paths.get(this).previousUnshifted);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
get inner() {
|
|
85
|
+
return PathFacade.wrap(paths.get(this).inner);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
equalTo(tagPath) {
|
|
89
|
+
if (tagPath == null) return false;
|
|
90
|
+
|
|
91
|
+
let { path, childrenIndex } = paths.get(this);
|
|
92
|
+
let tagPath_ = paths.get(tagPath);
|
|
93
|
+
return path.node === tagPath_.path.node && childrenIndex === tagPath_.childrenIndex;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export class PathFacade {
|
|
98
|
+
static from(node) {
|
|
99
|
+
return node && new PathFacade(Path.from(nodes.get(node)));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
static wrap(path) {
|
|
103
|
+
return path && new PathFacade(path);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
constructor(path) {
|
|
107
|
+
if (!(path instanceof Path)) throw new Error();
|
|
108
|
+
this.depth = path.depth;
|
|
109
|
+
|
|
110
|
+
this.node = new NodeFacade(path.node);
|
|
111
|
+
this.referenceIndex = path.referenceIndex;
|
|
112
|
+
|
|
113
|
+
paths.set(this, path);
|
|
114
|
+
|
|
115
|
+
freeze(this);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
get parent() {
|
|
119
|
+
// not a stable reference. can/should it be?
|
|
120
|
+
return PathFacade.wrap(paths.get(this).parent);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
get openTagPath() {
|
|
124
|
+
return TagPathFacade.wrap(paths.get(this).openTagPath);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
get openTag() {
|
|
128
|
+
return paths.get(this).openTag;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
get closeTagPath() {
|
|
132
|
+
return TagPathFacade.wrap(paths.get(this).closeTagPath);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
get closeTag() {
|
|
136
|
+
return paths.get(this).closeTag;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
get reference() {
|
|
140
|
+
return paths.get(this).reference;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
get referenceTagPath() {
|
|
144
|
+
return TagPathFacade.wrap(paths.get(this).referenceTagPath);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
get(path, shiftIndex = null) {
|
|
148
|
+
return new PathFacade(paths.get(this).get(path, shiftIndex));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
tagPathAt(childrenIndex) {
|
|
152
|
+
return TagPathFacade.wrap(TagPath.from(paths.get(this), childrenIndex));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
tagAt(childrenIndex) {
|
|
156
|
+
return this.tagPathAt(childrenIndex).tag;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
atDepth(depth) {
|
|
160
|
+
return new PathFacade(paths.get(this).atDepth(depth));
|
|
161
|
+
}
|
|
162
|
+
}
|