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.
package/dist/index.js CHANGED
@@ -128,16 +128,42 @@ class Node {
128
128
  }
129
129
  return null;
130
130
  }
131
- find(predicate) {
132
- return this.findAll(predicate)[0] || null;
131
+ find(predicate, breadthFirst = false) {
132
+ let match = null;
133
+ if (breadthFirst) {
134
+ this.walkBreadthFirst(n => {
135
+ if (predicate(n)) {
136
+ match = n;
137
+ return false;
138
+ }
139
+ });
140
+ }
141
+ else {
142
+ this.walkUp(n => {
143
+ if (predicate(n)) {
144
+ match = n;
145
+ return false;
146
+ }
147
+ });
148
+ }
149
+ return match;
133
150
  }
134
- findAll(predicate) {
151
+ findAll(predicate, breadthFirst = false) {
135
152
  const matches = [];
136
- this.walkUp(n => {
137
- if (predicate(n)) {
138
- matches.push(n);
139
- }
140
- });
153
+ if (breadthFirst) {
154
+ this.walkBreadthFirst(n => {
155
+ if (predicate(n)) {
156
+ matches.push(n);
157
+ }
158
+ });
159
+ }
160
+ else {
161
+ this.walkUp(n => {
162
+ if (predicate(n)) {
163
+ matches.push(n);
164
+ }
165
+ });
166
+ }
141
167
  return matches;
142
168
  }
143
169
  findRoot() {
@@ -160,22 +186,26 @@ class Node {
160
186
  return null;
161
187
  }
162
188
  walkUp(callback) {
189
+ var _a;
163
190
  const childrenCopy = this._children.slice();
164
- childrenCopy.forEach(c => c.walkUp(callback));
165
- callback(this);
191
+ const result = childrenCopy.every(c => c.walkUp(callback));
192
+ return ((_a = callback(this)) !== null && _a !== void 0 ? _a : true) && result;
166
193
  }
167
194
  walkDown(callback) {
195
+ var _a;
168
196
  const childrenCopy = this._children.slice();
169
- callback(this);
170
- childrenCopy.forEach(c => c.walkDown(callback));
197
+ return ((_a = callback(this)) !== null && _a !== void 0 ? _a : true) && childrenCopy.every(c => c.walkDown(callback));
171
198
  }
172
199
  walkBreadthFirst(callback) {
173
200
  const queue = [this];
174
201
  while (queue.length > 0) {
175
202
  const current = queue.shift();
176
- callback(current);
203
+ if (callback(current) === false) {
204
+ return false;
205
+ }
177
206
  queue.push(...current.children);
178
207
  }
208
+ return true;
179
209
  }
180
210
  transform(visitors) {
181
211
  const childrenCopy = this._children.slice();
@@ -2871,6 +2901,7 @@ class Expression {
2871
2901
  this._name = name;
2872
2902
  this._originalName = name;
2873
2903
  this._parent = null;
2904
+ this._cachedParent = null;
2874
2905
  this._firstIndex = 0;
2875
2906
  this._atomPatterns = [];
2876
2907
  this._prefixPatterns = [];
@@ -3021,7 +3052,8 @@ class Expression {
3021
3052
  return pattern.name === this._originalName;
3022
3053
  }
3023
3054
  build() {
3024
- if (!this._hasOrganized) {
3055
+ if (!this._hasOrganized || this._cachedParent !== this.parent) {
3056
+ this._cachedParent = this.parent;
3025
3057
  this._hasOrganized = true;
3026
3058
  this._organizePatterns(this._originalPatterns);
3027
3059
  this._cacheAncestors();
@@ -3192,11 +3224,13 @@ class Expression {
3192
3224
  return execPattern(this, text, record);
3193
3225
  }
3194
3226
  getTokens() {
3227
+ this.build();
3195
3228
  const atomTokens = this._atomPatterns.map(p => p.getTokens()).flat();
3196
3229
  const prefixTokens = this.prefixPatterns.map(p => p.getTokens()).flat();
3197
3230
  return [...prefixTokens, ...atomTokens];
3198
3231
  }
3199
3232
  getTokensAfter(childReference) {
3233
+ this.build();
3200
3234
  if (this._prefixPatterns.includes(childReference) || this._binaryPatterns.includes(childReference)) {
3201
3235
  const atomTokens = this._atomPatterns.map(p => p.getTokens()).flat();
3202
3236
  const prefixTokens = this.prefixPatterns.map(p => p.getTokens()).flat();
@@ -3223,11 +3257,13 @@ class Expression {
3223
3257
  return this._parent.getTokensAfter(this);
3224
3258
  }
3225
3259
  getPatterns() {
3260
+ this.build();
3226
3261
  const atomPatterns = this._atomPatterns.map(p => p.getPatterns()).flat();
3227
3262
  const prefixPatterns = this.prefixPatterns.map(p => p.getPatterns()).flat();
3228
3263
  return [...prefixPatterns, ...atomPatterns];
3229
3264
  }
3230
3265
  getPatternsAfter(childReference) {
3266
+ this.build();
3231
3267
  if (this._prefixPatterns.includes(childReference) || this._binaryPatterns.includes(childReference)) {
3232
3268
  const atomPatterns = this._atomPatterns.map(p => p.getPatterns()).flat();
3233
3269
  const prefixPatterns = this.prefixPatterns.map(p => p.getPatterns()).flat();