clarity-pattern-parser 11.4.2 → 11.5.1

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.
@@ -40,13 +40,13 @@ export declare class Node {
40
40
  append(...nodes: Node[]): void;
41
41
  nextSibling(): Node | null;
42
42
  previousSibling(): Node | null;
43
- find(predicate: (node: Node) => boolean): Node | null;
44
- findAll(predicate: (node: Node) => boolean): Node[];
43
+ find(predicate: (node: Node) => boolean, breadthFirst?: boolean): Node | null;
44
+ findAll(predicate: (node: Node) => boolean, breadthFirst?: boolean): Node[];
45
45
  findRoot(): Node;
46
46
  findAncestor(predicate: (node: Node) => boolean): Node | null;
47
- walkUp(callback: (node: Node) => void): void;
48
- walkDown(callback: (node: Node) => void): void;
49
- walkBreadthFirst(callback: (node: Node) => void): void;
47
+ walkUp(callback: (node: Node) => boolean | void): boolean;
48
+ walkDown(callback: (node: Node) => boolean | void): boolean;
49
+ walkBreadthFirst(callback: (node: Node) => boolean | void): boolean;
50
50
  transform(visitors: Record<string, (node: Node) => Node>): Node;
51
51
  flatten(): Node[];
52
52
  compact(): void;
@@ -130,16 +130,42 @@
130
130
  }
131
131
  return null;
132
132
  }
133
- find(predicate) {
134
- return this.findAll(predicate)[0] || null;
133
+ find(predicate, breadthFirst = false) {
134
+ let match = null;
135
+ if (breadthFirst) {
136
+ this.walkBreadthFirst(n => {
137
+ if (predicate(n)) {
138
+ match = n;
139
+ return false;
140
+ }
141
+ });
142
+ }
143
+ else {
144
+ this.walkUp(n => {
145
+ if (predicate(n)) {
146
+ match = n;
147
+ return false;
148
+ }
149
+ });
150
+ }
151
+ return match;
135
152
  }
136
- findAll(predicate) {
153
+ findAll(predicate, breadthFirst = false) {
137
154
  const matches = [];
138
- this.walkUp(n => {
139
- if (predicate(n)) {
140
- matches.push(n);
141
- }
142
- });
155
+ if (breadthFirst) {
156
+ this.walkBreadthFirst(n => {
157
+ if (predicate(n)) {
158
+ matches.push(n);
159
+ }
160
+ });
161
+ }
162
+ else {
163
+ this.walkUp(n => {
164
+ if (predicate(n)) {
165
+ matches.push(n);
166
+ }
167
+ });
168
+ }
143
169
  return matches;
144
170
  }
145
171
  findRoot() {
@@ -162,22 +188,26 @@
162
188
  return null;
163
189
  }
164
190
  walkUp(callback) {
191
+ var _a;
165
192
  const childrenCopy = this._children.slice();
166
- childrenCopy.forEach(c => c.walkUp(callback));
167
- callback(this);
193
+ const result = childrenCopy.every(c => c.walkUp(callback));
194
+ return ((_a = callback(this)) !== null && _a !== void 0 ? _a : true) && result;
168
195
  }
169
196
  walkDown(callback) {
197
+ var _a;
170
198
  const childrenCopy = this._children.slice();
171
- callback(this);
172
- childrenCopy.forEach(c => c.walkDown(callback));
199
+ return ((_a = callback(this)) !== null && _a !== void 0 ? _a : true) && childrenCopy.every(c => c.walkDown(callback));
173
200
  }
174
201
  walkBreadthFirst(callback) {
175
202
  const queue = [this];
176
203
  while (queue.length > 0) {
177
204
  const current = queue.shift();
178
- callback(current);
205
+ if (callback(current) === false) {
206
+ return false;
207
+ }
179
208
  queue.push(...current.children);
180
209
  }
210
+ return true;
181
211
  }
182
212
  transform(visitors) {
183
213
  const childrenCopy = this._children.slice();
@@ -2873,6 +2903,7 @@
2873
2903
  this._name = name;
2874
2904
  this._originalName = name;
2875
2905
  this._parent = null;
2906
+ this._cachedParent = null;
2876
2907
  this._firstIndex = 0;
2877
2908
  this._atomPatterns = [];
2878
2909
  this._prefixPatterns = [];
@@ -3023,7 +3054,8 @@
3023
3054
  return pattern.name === this._originalName;
3024
3055
  }
3025
3056
  build() {
3026
- if (!this._hasOrganized) {
3057
+ if (!this._hasOrganized || this._cachedParent !== this.parent) {
3058
+ this._cachedParent = this.parent;
3027
3059
  this._hasOrganized = true;
3028
3060
  this._organizePatterns(this._originalPatterns);
3029
3061
  this._cacheAncestors();
@@ -3194,11 +3226,13 @@
3194
3226
  return execPattern(this, text, record);
3195
3227
  }
3196
3228
  getTokens() {
3229
+ this.build();
3197
3230
  const atomTokens = this._atomPatterns.map(p => p.getTokens()).flat();
3198
3231
  const prefixTokens = this.prefixPatterns.map(p => p.getTokens()).flat();
3199
3232
  return [...prefixTokens, ...atomTokens];
3200
3233
  }
3201
3234
  getTokensAfter(childReference) {
3235
+ this.build();
3202
3236
  if (this._prefixPatterns.includes(childReference) || this._binaryPatterns.includes(childReference)) {
3203
3237
  const atomTokens = this._atomPatterns.map(p => p.getTokens()).flat();
3204
3238
  const prefixTokens = this.prefixPatterns.map(p => p.getTokens()).flat();
@@ -3225,11 +3259,13 @@
3225
3259
  return this._parent.getTokensAfter(this);
3226
3260
  }
3227
3261
  getPatterns() {
3262
+ this.build();
3228
3263
  const atomPatterns = this._atomPatterns.map(p => p.getPatterns()).flat();
3229
3264
  const prefixPatterns = this.prefixPatterns.map(p => p.getPatterns()).flat();
3230
3265
  return [...prefixPatterns, ...atomPatterns];
3231
3266
  }
3232
3267
  getPatternsAfter(childReference) {
3268
+ this.build();
3233
3269
  if (this._prefixPatterns.includes(childReference) || this._binaryPatterns.includes(childReference)) {
3234
3270
  const atomPatterns = this._atomPatterns.map(p => p.getPatterns()).flat();
3235
3271
  const prefixPatterns = this.prefixPatterns.map(p => p.getPatterns()).flat();