clarity-pattern-parser 11.0.6 → 11.0.7

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
@@ -12,6 +12,9 @@ class Node {
12
12
  get name() {
13
13
  return this._name;
14
14
  }
15
+ get value() {
16
+ return this.toString();
17
+ }
15
18
  get firstIndex() {
16
19
  return this._firstIndex;
17
20
  }
@@ -36,9 +39,6 @@ class Node {
36
39
  get isLeaf() {
37
40
  return !this.hasChildren;
38
41
  }
39
- get value() {
40
- return this.toString();
41
- }
42
42
  constructor(type, name, firstIndex, lastIndex, children = [], value = "") {
43
43
  this._type = type;
44
44
  this._name = name;
@@ -126,15 +126,6 @@ class Node {
126
126
  find(predicate) {
127
127
  return this.findAll(predicate)[0] || null;
128
128
  }
129
- findRoot() {
130
- let pattern = this;
131
- while (true) {
132
- if (pattern.parent == null) {
133
- return pattern;
134
- }
135
- pattern = pattern.parent;
136
- }
137
- }
138
129
  findAll(predicate) {
139
130
  const matches = [];
140
131
  this.walkUp(n => {
@@ -144,6 +135,15 @@ class Node {
144
135
  });
145
136
  return matches;
146
137
  }
138
+ findRoot() {
139
+ let pattern = this;
140
+ while (true) {
141
+ if (pattern.parent == null) {
142
+ return pattern;
143
+ }
144
+ pattern = pattern.parent;
145
+ }
146
+ }
147
147
  findAncestor(predicate) {
148
148
  let parent = this._parent;
149
149
  while (parent != null) {
@@ -189,7 +189,7 @@ class Node {
189
189
  });
190
190
  return nodes;
191
191
  }
192
- reduce() {
192
+ compact() {
193
193
  const value = this.toString();
194
194
  this.removeAllChildren();
195
195
  this._value = value;
@@ -220,10 +220,6 @@ class Node {
220
220
  this._lastIndex = Math.max(startIndex + length - 1, 0);
221
221
  return length;
222
222
  }
223
- compact() {
224
- this._value = this.toString();
225
- this._children.length = 0;
226
- }
227
223
  toString() {
228
224
  if (this._children.length === 0) {
229
225
  return this._value;
@@ -395,10 +391,10 @@ class CursorHistory {
395
391
  }
396
392
  }
397
393
  }
398
- recordErrorAt(startIndex, endIndex, pattern) {
399
- const error = new ParseError(startIndex, endIndex, pattern);
394
+ recordErrorAt(startIndex, lastIndex, pattern) {
395
+ const error = new ParseError(startIndex, lastIndex, pattern);
400
396
  this._currentError = error;
401
- if (this._furthestError === null || endIndex > this._furthestError.lastIndex) {
397
+ if (this._furthestError === null || lastIndex > this._furthestError.lastIndex) {
402
398
  this._furthestError = error;
403
399
  }
404
400
  if (this._isRecording) {
@@ -1192,7 +1188,6 @@ class FiniteRepeat {
1192
1188
  }
1193
1189
  parse(cursor) {
1194
1190
  this._firstIndex = cursor.index;
1195
- const startIndex = cursor.index;
1196
1191
  const nodes = [];
1197
1192
  const modulo = this._hasDivider ? 2 : 1;
1198
1193
  let matchCount = 0;
@@ -1228,12 +1223,12 @@ class FiniteRepeat {
1228
1223
  }
1229
1224
  if (matchCount < this._min) {
1230
1225
  const lastIndex = cursor.index;
1231
- cursor.moveTo(startIndex);
1232
- cursor.recordErrorAt(startIndex, lastIndex, this);
1226
+ cursor.moveTo(this._firstIndex);
1227
+ cursor.recordErrorAt(this._firstIndex, lastIndex, this);
1233
1228
  return null;
1234
1229
  }
1235
1230
  if (nodes.length === 0 && !cursor.hasError) {
1236
- cursor.moveTo(startIndex);
1231
+ cursor.moveTo(this._firstIndex);
1237
1232
  return null;
1238
1233
  }
1239
1234
  const firstIndex = nodes[0].firstIndex;
@@ -1357,7 +1352,7 @@ class InfiniteRepeat {
1357
1352
  this._children = children;
1358
1353
  this._pattern = children[0];
1359
1354
  this._divider = children[1];
1360
- this._firstIndex = -1;
1355
+ this._firstIndex = 0;
1361
1356
  this._nodes = [];
1362
1357
  this._trimDivider = options.trimDivider == null ? false : options.trimDivider;
1363
1358
  }
@@ -1537,7 +1532,7 @@ class InfiniteRepeat {
1537
1532
  patterns.push(this._children[0]);
1538
1533
  }
1539
1534
  // If there is no divider then suggest the repeating pattern and the next pattern after.
1540
- if (index === 0 && !this._divider && this._parent) {
1535
+ if (index === 0 && this._divider == null && this._parent) {
1541
1536
  patterns.push(this._children[0]);
1542
1537
  patterns.push(...this._parent.getPatternsAfter(this));
1543
1538
  }
@@ -2130,13 +2125,13 @@ const repeatLiteral = new Sequence("repeat-literal", [
2130
2125
  const optionalNot = new Optional("optional-not", new Literal("not", "!"));
2131
2126
  const optionalIsOptional$1 = new Optional("optional-is-optional", new Literal("is-optional", "?"));
2132
2127
  const patternName$1 = name$1.clone("pattern-name");
2133
- const patterns$2 = new Options("and-patterns", [patternName$1, anonymousPattern]);
2134
- const pattern$1 = new Sequence("and-child-pattern", [
2128
+ const patterns$2 = new Options("sequence-patterns", [patternName$1, anonymousPattern]);
2129
+ const pattern$1 = new Sequence("sequence-child-pattern", [
2135
2130
  optionalNot,
2136
2131
  patterns$2,
2137
2132
  optionalIsOptional$1,
2138
2133
  ]);
2139
- const divider$1 = new Regex("and-divider", "\\s*[+]\\s*");
2134
+ const divider$1 = new Regex("sequence-divider", "\\s*[+]\\s*");
2140
2135
  divider$1.setTokens([" + "]);
2141
2136
  const sequenceLiteral = new Repeat("sequence-literal", pattern$1, { divider: divider$1, min: 2, trimDivider: true });
2142
2137
 
@@ -2878,7 +2873,7 @@ class PrecedenceTree {
2878
2873
  }
2879
2874
 
2880
2875
  let indexId$1 = 0;
2881
- class ExpressionPattern {
2876
+ class Expression {
2882
2877
  get id() {
2883
2878
  return this._id;
2884
2879
  }
@@ -2920,7 +2915,7 @@ class ExpressionPattern {
2920
2915
  this._type = "expression";
2921
2916
  this._name = name;
2922
2917
  this._parent = null;
2923
- this._firstIndex = -1;
2918
+ this._firstIndex = 0;
2924
2919
  this._atomPatterns = [];
2925
2920
  this._prefixPatterns = [];
2926
2921
  this._prefixNames = [];
@@ -2965,10 +2960,10 @@ class ExpressionPattern {
2965
2960
  }
2966
2961
  else if (this._isBinary(pattern)) {
2967
2962
  const name = this._extractName(pattern);
2968
- const clone = this._extractBinary(pattern);
2969
- clone.parent = this;
2963
+ const binary = this._extractBinary(pattern);
2964
+ binary.parent = this;
2970
2965
  this._precedenceMap[name] = this._binaryPatterns.length;
2971
- this._binaryPatterns.push(clone);
2966
+ this._binaryPatterns.push(binary);
2972
2967
  this._binaryNames.push(name);
2973
2968
  if (pattern.type === "right-associated") {
2974
2969
  this._associationMap[name] = Association.right;
@@ -2976,7 +2971,7 @@ class ExpressionPattern {
2976
2971
  else {
2977
2972
  this._associationMap[name] = Association.left;
2978
2973
  }
2979
- finalPatterns.push(clone);
2974
+ finalPatterns.push(binary);
2980
2975
  }
2981
2976
  });
2982
2977
  return finalPatterns;
@@ -3217,7 +3212,9 @@ class ExpressionPattern {
3217
3212
  return execPattern(this, text, record);
3218
3213
  }
3219
3214
  getTokens() {
3220
- return this.atomPatterns.map(p => p.getTokens()).flat();
3215
+ const atomTokens = this._atomPatterns.map(p => p.getTokens()).flat();
3216
+ const prefixTokens = this.prefixPatterns.map(p => p.getTokens()).flat();
3217
+ return [...prefixTokens, ...atomTokens];
3221
3218
  }
3222
3219
  getTokensAfter(childReference) {
3223
3220
  if (this._prefixPatterns.includes(childReference) || this._binaryPatterns.includes(childReference)) {
@@ -3246,7 +3243,9 @@ class ExpressionPattern {
3246
3243
  return this._parent.getTokensAfter(this);
3247
3244
  }
3248
3245
  getPatterns() {
3249
- return this.atomPatterns.map(p => p.getPatterns()).flat();
3246
+ const atomPatterns = this._atomPatterns.map(p => p.getPatterns()).flat();
3247
+ const prefixPatterns = this.prefixPatterns.map(p => p.getPatterns()).flat();
3248
+ return [...prefixPatterns, ...atomPatterns];
3250
3249
  }
3251
3250
  getPatternsAfter(childReference) {
3252
3251
  if (this._prefixPatterns.includes(childReference) || this._binaryPatterns.includes(childReference)) {
@@ -3278,7 +3277,7 @@ class ExpressionPattern {
3278
3277
  return findPattern(this, predicate);
3279
3278
  }
3280
3279
  clone(name = this._name) {
3281
- const clone = new ExpressionPattern(name, this._originalPatterns);
3280
+ const clone = new Expression(name, this._originalPatterns);
3282
3281
  clone._id = this._id;
3283
3282
  return clone;
3284
3283
  }
@@ -3288,7 +3287,7 @@ class ExpressionPattern {
3288
3287
  }
3289
3288
 
3290
3289
  let indexId = 0;
3291
- class RightAssociatedPattern {
3290
+ class RightAssociated {
3292
3291
  get id() {
3293
3292
  return this._id;
3294
3293
  }
@@ -3327,7 +3326,7 @@ class RightAssociatedPattern {
3327
3326
  return this.children[0].test(text, record);
3328
3327
  }
3329
3328
  clone(_name) {
3330
- const clone = new RightAssociatedPattern(this.children[0]);
3329
+ const clone = new RightAssociated(this.children[0]);
3331
3330
  clone._id = this._id;
3332
3331
  return clone;
3333
3332
  }
@@ -3420,14 +3419,6 @@ class Grammar {
3420
3419
  return this._buildPatternRecord();
3421
3420
  });
3422
3421
  }
3423
- _buildPatternRecord() {
3424
- const patterns = {};
3425
- const allPatterns = Array.from(this._parseContext.patternsByName.values());
3426
- allPatterns.forEach(p => {
3427
- patterns[p.name] = new Context(p.name, p, allPatterns.filter(o => o !== p));
3428
- });
3429
- return patterns;
3430
- }
3431
3422
  parseString(expression) {
3432
3423
  this._parseContext = new ParseContext(this._params);
3433
3424
  const ast = this._tryToParse(expression);
@@ -3437,6 +3428,14 @@ class Grammar {
3437
3428
  this._buildPatterns(ast);
3438
3429
  return this._buildPatternRecord();
3439
3430
  }
3431
+ _buildPatternRecord() {
3432
+ const patterns = {};
3433
+ const allPatterns = Array.from(this._parseContext.patternsByName.values());
3434
+ allPatterns.forEach(p => {
3435
+ patterns[p.name] = new Context(p.name, p, allPatterns.filter(o => o !== p));
3436
+ });
3437
+ return patterns;
3438
+ }
3440
3439
  _tryToParse(expression) {
3441
3440
  const { ast, cursor, options, isComplete } = this._autoComplete.suggestFor(expression);
3442
3441
  if (!isComplete) {
@@ -3553,7 +3552,7 @@ class Grammar {
3553
3552
  const patterns = patternNodes.map(n => {
3554
3553
  const rightAssociated = n.find(n => n.name === "right-associated");
3555
3554
  if (rightAssociated != null) {
3556
- return new RightAssociatedPattern(this._buildPattern(n.children[0]));
3555
+ return new RightAssociated(this._buildPattern(n.children[0]));
3557
3556
  }
3558
3557
  else {
3559
3558
  return this._buildPattern(n.children[0]);
@@ -3562,13 +3561,13 @@ class Grammar {
3562
3561
  const hasRecursivePattern = patterns.some(p => this._isRecursive(name, p));
3563
3562
  if (hasRecursivePattern && !isGreedy) {
3564
3563
  try {
3565
- const expression = new ExpressionPattern(name, patterns);
3564
+ const expression = new Expression(name, patterns);
3566
3565
  return expression;
3567
3566
  }
3568
3567
  catch (_a) { }
3569
3568
  }
3570
- const or = new Options(name, patterns, isGreedy);
3571
- return or;
3569
+ const options = new Options(name, patterns, isGreedy);
3570
+ return options;
3572
3571
  }
3573
3572
  _isRecursive(name, pattern) {
3574
3573
  if (pattern.type === "right-associated" && this._isRecursivePattern(name, pattern.children[0])) {
@@ -3623,7 +3622,7 @@ class Grammar {
3623
3622
  this._parseContext.patternsByName.set(name, sequence);
3624
3623
  }
3625
3624
  _buildSequence(name, node) {
3626
- const patternNodes = node.children.filter(n => n.name !== "and-divider");
3625
+ const patternNodes = node.children.filter(n => n.name !== "sequence-divider");
3627
3626
  const patterns = patternNodes.map(n => {
3628
3627
  const patternNode = n.children[0].name === "not" ? n.children[1] : n.children[0];
3629
3628
  const isNot = n.find(n => n.name === "not") != null;
@@ -3832,7 +3831,7 @@ exports.AutoComplete = AutoComplete;
3832
3831
  exports.Context = Context;
3833
3832
  exports.Cursor = Cursor;
3834
3833
  exports.CursorHistory = CursorHistory;
3835
- exports.ExpressionPattern = ExpressionPattern;
3834
+ exports.Expression = Expression;
3836
3835
  exports.Grammar = Grammar;
3837
3836
  exports.Literal = Literal;
3838
3837
  exports.Node = Node;
@@ -3843,6 +3842,7 @@ exports.ParseError = ParseError;
3843
3842
  exports.Reference = Reference;
3844
3843
  exports.Regex = Regex;
3845
3844
  exports.Repeat = Repeat;
3845
+ exports.RightAssociated = RightAssociated;
3846
3846
  exports.Sequence = Sequence;
3847
3847
  exports.compact = compact;
3848
3848
  exports.grammar = grammar;