clarity-pattern-parser 11.0.6 → 11.0.8

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.d.ts CHANGED
@@ -21,5 +21,6 @@ import { ParseResult } from "./patterns/ParseResult";
21
21
  import { grammar } from "./grammar/patterns/grammar";
22
22
  import { patterns } from "./grammar/patterns";
23
23
  import { Context } from "./patterns/Context";
24
- import { ExpressionPattern } from "./patterns/ExpressionPattern";
25
- export { Node, Grammar, AutoComplete, AutoCompleteOptions, Suggestion, SuggestionOption, Sequence, Cursor, CursorHistory, Match, Context, ExpressionPattern, Literal, Not, Options, Optional, ParseError, ParseResult, Pattern, Reference, Regex, Repeat, grammar, patterns, compact, remove };
24
+ import { Expression } from "./patterns/Expression";
25
+ import { RightAssociated } from "./patterns/RightAssociated";
26
+ export { Node, Grammar, AutoComplete, AutoCompleteOptions, Suggestion, SuggestionOption, Sequence, Cursor, CursorHistory, Match, Context, Expression, Literal, Not, Options, Optional, ParseError, ParseResult, Pattern, Reference, RightAssociated, Regex, Repeat, grammar, patterns, compact, remove };
package/dist/index.esm.js CHANGED
@@ -8,6 +8,9 @@ class Node {
8
8
  get name() {
9
9
  return this._name;
10
10
  }
11
+ get value() {
12
+ return this.toString();
13
+ }
11
14
  get firstIndex() {
12
15
  return this._firstIndex;
13
16
  }
@@ -32,9 +35,6 @@ class Node {
32
35
  get isLeaf() {
33
36
  return !this.hasChildren;
34
37
  }
35
- get value() {
36
- return this.toString();
37
- }
38
38
  constructor(type, name, firstIndex, lastIndex, children = [], value = "") {
39
39
  this._type = type;
40
40
  this._name = name;
@@ -122,15 +122,6 @@ class Node {
122
122
  find(predicate) {
123
123
  return this.findAll(predicate)[0] || null;
124
124
  }
125
- findRoot() {
126
- let pattern = this;
127
- while (true) {
128
- if (pattern.parent == null) {
129
- return pattern;
130
- }
131
- pattern = pattern.parent;
132
- }
133
- }
134
125
  findAll(predicate) {
135
126
  const matches = [];
136
127
  this.walkUp(n => {
@@ -140,6 +131,15 @@ class Node {
140
131
  });
141
132
  return matches;
142
133
  }
134
+ findRoot() {
135
+ let pattern = this;
136
+ while (true) {
137
+ if (pattern.parent == null) {
138
+ return pattern;
139
+ }
140
+ pattern = pattern.parent;
141
+ }
142
+ }
143
143
  findAncestor(predicate) {
144
144
  let parent = this._parent;
145
145
  while (parent != null) {
@@ -185,7 +185,7 @@ class Node {
185
185
  });
186
186
  return nodes;
187
187
  }
188
- reduce() {
188
+ compact() {
189
189
  const value = this.toString();
190
190
  this.removeAllChildren();
191
191
  this._value = value;
@@ -216,10 +216,6 @@ class Node {
216
216
  this._lastIndex = Math.max(startIndex + length - 1, 0);
217
217
  return length;
218
218
  }
219
- compact() {
220
- this._value = this.toString();
221
- this._children.length = 0;
222
- }
223
219
  toString() {
224
220
  if (this._children.length === 0) {
225
221
  return this._value;
@@ -391,10 +387,10 @@ class CursorHistory {
391
387
  }
392
388
  }
393
389
  }
394
- recordErrorAt(startIndex, endIndex, pattern) {
395
- const error = new ParseError(startIndex, endIndex, pattern);
390
+ recordErrorAt(startIndex, lastIndex, pattern) {
391
+ const error = new ParseError(startIndex, lastIndex, pattern);
396
392
  this._currentError = error;
397
- if (this._furthestError === null || endIndex > this._furthestError.lastIndex) {
393
+ if (this._furthestError === null || lastIndex > this._furthestError.lastIndex) {
398
394
  this._furthestError = error;
399
395
  }
400
396
  if (this._isRecording) {
@@ -848,6 +844,8 @@ class Reference {
848
844
  this._cachedPattern = null;
849
845
  this._children = [];
850
846
  this._firstIndex = 0;
847
+ this._cachedAncestors = false;
848
+ this._recursiveAncestors = [];
851
849
  }
852
850
  test(text, record = false) {
853
851
  return testPattern(this, text, record);
@@ -857,8 +855,36 @@ class Reference {
857
855
  }
858
856
  parse(cursor) {
859
857
  this._firstIndex = cursor.index;
858
+ this._cacheAncestors();
859
+ if (this._isBeyondRecursiveAllowance()) {
860
+ cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
861
+ return null;
862
+ }
860
863
  return this.getReferencePatternSafely().parse(cursor);
861
864
  }
865
+ _cacheAncestors() {
866
+ if (!this._cachedAncestors) {
867
+ let pattern = this.parent;
868
+ while (pattern != null) {
869
+ if (pattern.type === this.type && pattern.id === this._id) {
870
+ this._recursiveAncestors.push(pattern);
871
+ }
872
+ pattern = pattern.parent;
873
+ }
874
+ }
875
+ }
876
+ _isBeyondRecursiveAllowance() {
877
+ let depth = 0;
878
+ for (let pattern of this._recursiveAncestors) {
879
+ if (pattern._firstIndex === this._firstIndex) {
880
+ depth++;
881
+ if (depth > 2) {
882
+ return true;
883
+ }
884
+ }
885
+ }
886
+ return false;
887
+ }
862
888
  getReferencePatternSafely() {
863
889
  if (this._pattern === null) {
864
890
  let pattern = null;
@@ -1044,9 +1070,6 @@ class Options {
1044
1070
  return null;
1045
1071
  }
1046
1072
  _tryToParse(cursor) {
1047
- if (this._isBeyondRecursiveAllowance()) {
1048
- return null;
1049
- }
1050
1073
  const results = [];
1051
1074
  for (const pattern of this._children) {
1052
1075
  cursor.moveTo(this._firstIndex);
@@ -1064,20 +1087,6 @@ class Options {
1064
1087
  nonNullResults.sort((a, b) => b.endIndex - a.endIndex);
1065
1088
  return nonNullResults[0] || null;
1066
1089
  }
1067
- _isBeyondRecursiveAllowance() {
1068
- let depth = 0;
1069
- let pattern = this;
1070
- while (pattern != null) {
1071
- if (pattern.id === this.id && pattern.startedOnIndex === this.startedOnIndex) {
1072
- depth++;
1073
- }
1074
- if (depth > 2) {
1075
- return true;
1076
- }
1077
- pattern = pattern.parent;
1078
- }
1079
- return false;
1080
- }
1081
1090
  getTokens() {
1082
1091
  const tokens = [];
1083
1092
  for (const pattern of this._children) {
@@ -1188,7 +1197,6 @@ class FiniteRepeat {
1188
1197
  }
1189
1198
  parse(cursor) {
1190
1199
  this._firstIndex = cursor.index;
1191
- const startIndex = cursor.index;
1192
1200
  const nodes = [];
1193
1201
  const modulo = this._hasDivider ? 2 : 1;
1194
1202
  let matchCount = 0;
@@ -1224,12 +1232,12 @@ class FiniteRepeat {
1224
1232
  }
1225
1233
  if (matchCount < this._min) {
1226
1234
  const lastIndex = cursor.index;
1227
- cursor.moveTo(startIndex);
1228
- cursor.recordErrorAt(startIndex, lastIndex, this);
1235
+ cursor.moveTo(this._firstIndex);
1236
+ cursor.recordErrorAt(this._firstIndex, lastIndex, this);
1229
1237
  return null;
1230
1238
  }
1231
1239
  if (nodes.length === 0 && !cursor.hasError) {
1232
- cursor.moveTo(startIndex);
1240
+ cursor.moveTo(this._firstIndex);
1233
1241
  return null;
1234
1242
  }
1235
1243
  const firstIndex = nodes[0].firstIndex;
@@ -1353,7 +1361,7 @@ class InfiniteRepeat {
1353
1361
  this._children = children;
1354
1362
  this._pattern = children[0];
1355
1363
  this._divider = children[1];
1356
- this._firstIndex = -1;
1364
+ this._firstIndex = 0;
1357
1365
  this._nodes = [];
1358
1366
  this._trimDivider = options.trimDivider == null ? false : options.trimDivider;
1359
1367
  }
@@ -1533,7 +1541,7 @@ class InfiniteRepeat {
1533
1541
  patterns.push(this._children[0]);
1534
1542
  }
1535
1543
  // If there is no divider then suggest the repeating pattern and the next pattern after.
1536
- if (index === 0 && !this._divider && this._parent) {
1544
+ if (index === 0 && this._divider == null && this._parent) {
1537
1545
  patterns.push(this._children[0]);
1538
1546
  patterns.push(...this._parent.getPatternsAfter(this));
1539
1547
  }
@@ -1734,10 +1742,6 @@ class Sequence {
1734
1742
  return null;
1735
1743
  }
1736
1744
  tryToParse(cursor) {
1737
- if (this._isBeyondRecursiveAllowance()) {
1738
- cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
1739
- return false;
1740
- }
1741
1745
  let passed = false;
1742
1746
  for (let i = 0; i < this._children.length; i++) {
1743
1747
  const runningCursorIndex = cursor.index;
@@ -1801,20 +1805,6 @@ class Sequence {
1801
1805
  }
1802
1806
  return nodes[nodes.length - 1];
1803
1807
  }
1804
- _isBeyondRecursiveAllowance() {
1805
- let depth = 0;
1806
- let pattern = this;
1807
- while (pattern != null) {
1808
- if (pattern.id === this.id && pattern.startedOnIndex === this.startedOnIndex) {
1809
- depth++;
1810
- }
1811
- if (depth > 1) {
1812
- return true;
1813
- }
1814
- pattern = pattern.parent;
1815
- }
1816
- return false;
1817
- }
1818
1808
  areRemainingPatternsOptional(fromIndex) {
1819
1809
  const startOnIndex = fromIndex + 1;
1820
1810
  const length = this._children.length;
@@ -2126,13 +2116,13 @@ const repeatLiteral = new Sequence("repeat-literal", [
2126
2116
  const optionalNot = new Optional("optional-not", new Literal("not", "!"));
2127
2117
  const optionalIsOptional$1 = new Optional("optional-is-optional", new Literal("is-optional", "?"));
2128
2118
  const patternName$1 = name$1.clone("pattern-name");
2129
- const patterns$2 = new Options("and-patterns", [patternName$1, anonymousPattern]);
2130
- const pattern$1 = new Sequence("and-child-pattern", [
2119
+ const patterns$2 = new Options("sequence-patterns", [patternName$1, anonymousPattern]);
2120
+ const pattern$1 = new Sequence("sequence-child-pattern", [
2131
2121
  optionalNot,
2132
2122
  patterns$2,
2133
2123
  optionalIsOptional$1,
2134
2124
  ]);
2135
- const divider$1 = new Regex("and-divider", "\\s*[+]\\s*");
2125
+ const divider$1 = new Regex("sequence-divider", "\\s*[+]\\s*");
2136
2126
  divider$1.setTokens([" + "]);
2137
2127
  const sequenceLiteral = new Repeat("sequence-literal", pattern$1, { divider: divider$1, min: 2, trimDivider: true });
2138
2128
 
@@ -2874,7 +2864,7 @@ class PrecedenceTree {
2874
2864
  }
2875
2865
 
2876
2866
  let indexId$1 = 0;
2877
- class ExpressionPattern {
2867
+ class Expression {
2878
2868
  get id() {
2879
2869
  return this._id;
2880
2870
  }
@@ -2916,7 +2906,7 @@ class ExpressionPattern {
2916
2906
  this._type = "expression";
2917
2907
  this._name = name;
2918
2908
  this._parent = null;
2919
- this._firstIndex = -1;
2909
+ this._firstIndex = 0;
2920
2910
  this._atomPatterns = [];
2921
2911
  this._prefixPatterns = [];
2922
2912
  this._prefixNames = [];
@@ -2961,10 +2951,10 @@ class ExpressionPattern {
2961
2951
  }
2962
2952
  else if (this._isBinary(pattern)) {
2963
2953
  const name = this._extractName(pattern);
2964
- const clone = this._extractBinary(pattern);
2965
- clone.parent = this;
2954
+ const binary = this._extractBinary(pattern);
2955
+ binary.parent = this;
2966
2956
  this._precedenceMap[name] = this._binaryPatterns.length;
2967
- this._binaryPatterns.push(clone);
2957
+ this._binaryPatterns.push(binary);
2968
2958
  this._binaryNames.push(name);
2969
2959
  if (pattern.type === "right-associated") {
2970
2960
  this._associationMap[name] = Association.right;
@@ -2972,7 +2962,7 @@ class ExpressionPattern {
2972
2962
  else {
2973
2963
  this._associationMap[name] = Association.left;
2974
2964
  }
2975
- finalPatterns.push(clone);
2965
+ finalPatterns.push(binary);
2976
2966
  }
2977
2967
  });
2978
2968
  return finalPatterns;
@@ -3058,10 +3048,6 @@ class ExpressionPattern {
3058
3048
  return null;
3059
3049
  }
3060
3050
  _tryToParse(cursor) {
3061
- if (this._isBeyondRecursiveAllowance()) {
3062
- cursor.recordErrorAt(this._firstIndex, this._firstIndex, this);
3063
- return null;
3064
- }
3065
3051
  this._shouldStopParsing = false;
3066
3052
  while (true) {
3067
3053
  cursor.resolveError();
@@ -3192,20 +3178,6 @@ class ExpressionPattern {
3192
3178
  this._shouldStopParsing = true;
3193
3179
  }
3194
3180
  }
3195
- _isBeyondRecursiveAllowance() {
3196
- let depth = 0;
3197
- let pattern = this;
3198
- while (pattern != null) {
3199
- if (pattern.id === this.id && pattern.startedOnIndex === this.startedOnIndex) {
3200
- depth++;
3201
- }
3202
- if (depth > 2) {
3203
- return true;
3204
- }
3205
- pattern = pattern.parent;
3206
- }
3207
- return false;
3208
- }
3209
3181
  test(text, record = false) {
3210
3182
  return testPattern(this, text, record);
3211
3183
  }
@@ -3213,7 +3185,9 @@ class ExpressionPattern {
3213
3185
  return execPattern(this, text, record);
3214
3186
  }
3215
3187
  getTokens() {
3216
- return this.atomPatterns.map(p => p.getTokens()).flat();
3188
+ const atomTokens = this._atomPatterns.map(p => p.getTokens()).flat();
3189
+ const prefixTokens = this.prefixPatterns.map(p => p.getTokens()).flat();
3190
+ return [...prefixTokens, ...atomTokens];
3217
3191
  }
3218
3192
  getTokensAfter(childReference) {
3219
3193
  if (this._prefixPatterns.includes(childReference) || this._binaryPatterns.includes(childReference)) {
@@ -3242,7 +3216,9 @@ class ExpressionPattern {
3242
3216
  return this._parent.getTokensAfter(this);
3243
3217
  }
3244
3218
  getPatterns() {
3245
- return this.atomPatterns.map(p => p.getPatterns()).flat();
3219
+ const atomPatterns = this._atomPatterns.map(p => p.getPatterns()).flat();
3220
+ const prefixPatterns = this.prefixPatterns.map(p => p.getPatterns()).flat();
3221
+ return [...prefixPatterns, ...atomPatterns];
3246
3222
  }
3247
3223
  getPatternsAfter(childReference) {
3248
3224
  if (this._prefixPatterns.includes(childReference) || this._binaryPatterns.includes(childReference)) {
@@ -3274,7 +3250,7 @@ class ExpressionPattern {
3274
3250
  return findPattern(this, predicate);
3275
3251
  }
3276
3252
  clone(name = this._name) {
3277
- const clone = new ExpressionPattern(name, this._originalPatterns);
3253
+ const clone = new Expression(name, this._originalPatterns);
3278
3254
  clone._id = this._id;
3279
3255
  return clone;
3280
3256
  }
@@ -3284,7 +3260,7 @@ class ExpressionPattern {
3284
3260
  }
3285
3261
 
3286
3262
  let indexId = 0;
3287
- class RightAssociatedPattern {
3263
+ class RightAssociated {
3288
3264
  get id() {
3289
3265
  return this._id;
3290
3266
  }
@@ -3323,7 +3299,7 @@ class RightAssociatedPattern {
3323
3299
  return this.children[0].test(text, record);
3324
3300
  }
3325
3301
  clone(_name) {
3326
- const clone = new RightAssociatedPattern(this.children[0]);
3302
+ const clone = new RightAssociated(this.children[0]);
3327
3303
  clone._id = this._id;
3328
3304
  return clone;
3329
3305
  }
@@ -3416,14 +3392,6 @@ class Grammar {
3416
3392
  return this._buildPatternRecord();
3417
3393
  });
3418
3394
  }
3419
- _buildPatternRecord() {
3420
- const patterns = {};
3421
- const allPatterns = Array.from(this._parseContext.patternsByName.values());
3422
- allPatterns.forEach(p => {
3423
- patterns[p.name] = new Context(p.name, p, allPatterns.filter(o => o !== p));
3424
- });
3425
- return patterns;
3426
- }
3427
3395
  parseString(expression) {
3428
3396
  this._parseContext = new ParseContext(this._params);
3429
3397
  const ast = this._tryToParse(expression);
@@ -3433,6 +3401,14 @@ class Grammar {
3433
3401
  this._buildPatterns(ast);
3434
3402
  return this._buildPatternRecord();
3435
3403
  }
3404
+ _buildPatternRecord() {
3405
+ const patterns = {};
3406
+ const allPatterns = Array.from(this._parseContext.patternsByName.values());
3407
+ allPatterns.forEach(p => {
3408
+ patterns[p.name] = new Context(p.name, p, allPatterns.filter(o => o !== p));
3409
+ });
3410
+ return patterns;
3411
+ }
3436
3412
  _tryToParse(expression) {
3437
3413
  const { ast, cursor, options, isComplete } = this._autoComplete.suggestFor(expression);
3438
3414
  if (!isComplete) {
@@ -3549,7 +3525,7 @@ class Grammar {
3549
3525
  const patterns = patternNodes.map(n => {
3550
3526
  const rightAssociated = n.find(n => n.name === "right-associated");
3551
3527
  if (rightAssociated != null) {
3552
- return new RightAssociatedPattern(this._buildPattern(n.children[0]));
3528
+ return new RightAssociated(this._buildPattern(n.children[0]));
3553
3529
  }
3554
3530
  else {
3555
3531
  return this._buildPattern(n.children[0]);
@@ -3558,13 +3534,13 @@ class Grammar {
3558
3534
  const hasRecursivePattern = patterns.some(p => this._isRecursive(name, p));
3559
3535
  if (hasRecursivePattern && !isGreedy) {
3560
3536
  try {
3561
- const expression = new ExpressionPattern(name, patterns);
3537
+ const expression = new Expression(name, patterns);
3562
3538
  return expression;
3563
3539
  }
3564
3540
  catch (_a) { }
3565
3541
  }
3566
- const or = new Options(name, patterns, isGreedy);
3567
- return or;
3542
+ const options = new Options(name, patterns, isGreedy);
3543
+ return options;
3568
3544
  }
3569
3545
  _isRecursive(name, pattern) {
3570
3546
  if (pattern.type === "right-associated" && this._isRecursivePattern(name, pattern.children[0])) {
@@ -3619,7 +3595,7 @@ class Grammar {
3619
3595
  this._parseContext.patternsByName.set(name, sequence);
3620
3596
  }
3621
3597
  _buildSequence(name, node) {
3622
- const patternNodes = node.children.filter(n => n.name !== "and-divider");
3598
+ const patternNodes = node.children.filter(n => n.name !== "sequence-divider");
3623
3599
  const patterns = patternNodes.map(n => {
3624
3600
  const patternNode = n.children[0].name === "not" ? n.children[1] : n.children[0];
3625
3601
  const isNot = n.find(n => n.name === "not") != null;
@@ -3824,5 +3800,5 @@ function patterns(strings, ...values) {
3824
3800
  return result;
3825
3801
  }
3826
3802
 
3827
- export { AutoComplete, Context, Cursor, CursorHistory, ExpressionPattern, Grammar, Literal, Node, Not, Optional, Options, ParseError, Reference, Regex, Repeat, Sequence, compact, grammar, patterns, remove };
3803
+ export { AutoComplete, Context, Cursor, CursorHistory, Expression, Grammar, Literal, Node, Not, Optional, Options, ParseError, Reference, Regex, Repeat, RightAssociated, Sequence, compact, grammar, patterns, remove };
3828
3804
  //# sourceMappingURL=index.esm.js.map