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.esm.js CHANGED
@@ -124,16 +124,42 @@ class Node {
124
124
  }
125
125
  return null;
126
126
  }
127
- find(predicate) {
128
- return this.findAll(predicate)[0] || null;
127
+ find(predicate, breadthFirst = false) {
128
+ let match = null;
129
+ if (breadthFirst) {
130
+ this.walkBreadthFirst(n => {
131
+ if (predicate(n)) {
132
+ match = n;
133
+ return false;
134
+ }
135
+ });
136
+ }
137
+ else {
138
+ this.walkUp(n => {
139
+ if (predicate(n)) {
140
+ match = n;
141
+ return false;
142
+ }
143
+ });
144
+ }
145
+ return match;
129
146
  }
130
- findAll(predicate) {
147
+ findAll(predicate, breadthFirst = false) {
131
148
  const matches = [];
132
- this.walkUp(n => {
133
- if (predicate(n)) {
134
- matches.push(n);
135
- }
136
- });
149
+ if (breadthFirst) {
150
+ this.walkBreadthFirst(n => {
151
+ if (predicate(n)) {
152
+ matches.push(n);
153
+ }
154
+ });
155
+ }
156
+ else {
157
+ this.walkUp(n => {
158
+ if (predicate(n)) {
159
+ matches.push(n);
160
+ }
161
+ });
162
+ }
137
163
  return matches;
138
164
  }
139
165
  findRoot() {
@@ -156,22 +182,26 @@ class Node {
156
182
  return null;
157
183
  }
158
184
  walkUp(callback) {
185
+ var _a;
159
186
  const childrenCopy = this._children.slice();
160
- childrenCopy.forEach(c => c.walkUp(callback));
161
- callback(this);
187
+ const result = childrenCopy.every(c => c.walkUp(callback));
188
+ return ((_a = callback(this)) !== null && _a !== void 0 ? _a : true) && result;
162
189
  }
163
190
  walkDown(callback) {
191
+ var _a;
164
192
  const childrenCopy = this._children.slice();
165
- callback(this);
166
- childrenCopy.forEach(c => c.walkDown(callback));
193
+ return ((_a = callback(this)) !== null && _a !== void 0 ? _a : true) && childrenCopy.every(c => c.walkDown(callback));
167
194
  }
168
195
  walkBreadthFirst(callback) {
169
196
  const queue = [this];
170
197
  while (queue.length > 0) {
171
198
  const current = queue.shift();
172
- callback(current);
199
+ if (callback(current) === false) {
200
+ return false;
201
+ }
173
202
  queue.push(...current.children);
174
203
  }
204
+ return true;
175
205
  }
176
206
  transform(visitors) {
177
207
  const childrenCopy = this._children.slice();
@@ -2867,6 +2897,7 @@ class Expression {
2867
2897
  this._name = name;
2868
2898
  this._originalName = name;
2869
2899
  this._parent = null;
2900
+ this._cachedParent = null;
2870
2901
  this._firstIndex = 0;
2871
2902
  this._atomPatterns = [];
2872
2903
  this._prefixPatterns = [];
@@ -3017,7 +3048,8 @@ class Expression {
3017
3048
  return pattern.name === this._originalName;
3018
3049
  }
3019
3050
  build() {
3020
- if (!this._hasOrganized) {
3051
+ if (!this._hasOrganized || this._cachedParent !== this.parent) {
3052
+ this._cachedParent = this.parent;
3021
3053
  this._hasOrganized = true;
3022
3054
  this._organizePatterns(this._originalPatterns);
3023
3055
  this._cacheAncestors();
@@ -3188,11 +3220,13 @@ class Expression {
3188
3220
  return execPattern(this, text, record);
3189
3221
  }
3190
3222
  getTokens() {
3223
+ this.build();
3191
3224
  const atomTokens = this._atomPatterns.map(p => p.getTokens()).flat();
3192
3225
  const prefixTokens = this.prefixPatterns.map(p => p.getTokens()).flat();
3193
3226
  return [...prefixTokens, ...atomTokens];
3194
3227
  }
3195
3228
  getTokensAfter(childReference) {
3229
+ this.build();
3196
3230
  if (this._prefixPatterns.includes(childReference) || this._binaryPatterns.includes(childReference)) {
3197
3231
  const atomTokens = this._atomPatterns.map(p => p.getTokens()).flat();
3198
3232
  const prefixTokens = this.prefixPatterns.map(p => p.getTokens()).flat();
@@ -3219,11 +3253,13 @@ class Expression {
3219
3253
  return this._parent.getTokensAfter(this);
3220
3254
  }
3221
3255
  getPatterns() {
3256
+ this.build();
3222
3257
  const atomPatterns = this._atomPatterns.map(p => p.getPatterns()).flat();
3223
3258
  const prefixPatterns = this.prefixPatterns.map(p => p.getPatterns()).flat();
3224
3259
  return [...prefixPatterns, ...atomPatterns];
3225
3260
  }
3226
3261
  getPatternsAfter(childReference) {
3262
+ this.build();
3227
3263
  if (this._prefixPatterns.includes(childReference) || this._binaryPatterns.includes(childReference)) {
3228
3264
  const atomPatterns = this._atomPatterns.map(p => p.getPatterns()).flat();
3229
3265
  const prefixPatterns = this.prefixPatterns.map(p => p.getPatterns()).flat();