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.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) {
@@ -1188,7 +1184,6 @@ class FiniteRepeat {
1188
1184
  }
1189
1185
  parse(cursor) {
1190
1186
  this._firstIndex = cursor.index;
1191
- const startIndex = cursor.index;
1192
1187
  const nodes = [];
1193
1188
  const modulo = this._hasDivider ? 2 : 1;
1194
1189
  let matchCount = 0;
@@ -1224,12 +1219,12 @@ class FiniteRepeat {
1224
1219
  }
1225
1220
  if (matchCount < this._min) {
1226
1221
  const lastIndex = cursor.index;
1227
- cursor.moveTo(startIndex);
1228
- cursor.recordErrorAt(startIndex, lastIndex, this);
1222
+ cursor.moveTo(this._firstIndex);
1223
+ cursor.recordErrorAt(this._firstIndex, lastIndex, this);
1229
1224
  return null;
1230
1225
  }
1231
1226
  if (nodes.length === 0 && !cursor.hasError) {
1232
- cursor.moveTo(startIndex);
1227
+ cursor.moveTo(this._firstIndex);
1233
1228
  return null;
1234
1229
  }
1235
1230
  const firstIndex = nodes[0].firstIndex;
@@ -1353,7 +1348,7 @@ class InfiniteRepeat {
1353
1348
  this._children = children;
1354
1349
  this._pattern = children[0];
1355
1350
  this._divider = children[1];
1356
- this._firstIndex = -1;
1351
+ this._firstIndex = 0;
1357
1352
  this._nodes = [];
1358
1353
  this._trimDivider = options.trimDivider == null ? false : options.trimDivider;
1359
1354
  }
@@ -1533,7 +1528,7 @@ class InfiniteRepeat {
1533
1528
  patterns.push(this._children[0]);
1534
1529
  }
1535
1530
  // If there is no divider then suggest the repeating pattern and the next pattern after.
1536
- if (index === 0 && !this._divider && this._parent) {
1531
+ if (index === 0 && this._divider == null && this._parent) {
1537
1532
  patterns.push(this._children[0]);
1538
1533
  patterns.push(...this._parent.getPatternsAfter(this));
1539
1534
  }
@@ -2126,13 +2121,13 @@ const repeatLiteral = new Sequence("repeat-literal", [
2126
2121
  const optionalNot = new Optional("optional-not", new Literal("not", "!"));
2127
2122
  const optionalIsOptional$1 = new Optional("optional-is-optional", new Literal("is-optional", "?"));
2128
2123
  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", [
2124
+ const patterns$2 = new Options("sequence-patterns", [patternName$1, anonymousPattern]);
2125
+ const pattern$1 = new Sequence("sequence-child-pattern", [
2131
2126
  optionalNot,
2132
2127
  patterns$2,
2133
2128
  optionalIsOptional$1,
2134
2129
  ]);
2135
- const divider$1 = new Regex("and-divider", "\\s*[+]\\s*");
2130
+ const divider$1 = new Regex("sequence-divider", "\\s*[+]\\s*");
2136
2131
  divider$1.setTokens([" + "]);
2137
2132
  const sequenceLiteral = new Repeat("sequence-literal", pattern$1, { divider: divider$1, min: 2, trimDivider: true });
2138
2133
 
@@ -2874,7 +2869,7 @@ class PrecedenceTree {
2874
2869
  }
2875
2870
 
2876
2871
  let indexId$1 = 0;
2877
- class ExpressionPattern {
2872
+ class Expression {
2878
2873
  get id() {
2879
2874
  return this._id;
2880
2875
  }
@@ -2916,7 +2911,7 @@ class ExpressionPattern {
2916
2911
  this._type = "expression";
2917
2912
  this._name = name;
2918
2913
  this._parent = null;
2919
- this._firstIndex = -1;
2914
+ this._firstIndex = 0;
2920
2915
  this._atomPatterns = [];
2921
2916
  this._prefixPatterns = [];
2922
2917
  this._prefixNames = [];
@@ -2961,10 +2956,10 @@ class ExpressionPattern {
2961
2956
  }
2962
2957
  else if (this._isBinary(pattern)) {
2963
2958
  const name = this._extractName(pattern);
2964
- const clone = this._extractBinary(pattern);
2965
- clone.parent = this;
2959
+ const binary = this._extractBinary(pattern);
2960
+ binary.parent = this;
2966
2961
  this._precedenceMap[name] = this._binaryPatterns.length;
2967
- this._binaryPatterns.push(clone);
2962
+ this._binaryPatterns.push(binary);
2968
2963
  this._binaryNames.push(name);
2969
2964
  if (pattern.type === "right-associated") {
2970
2965
  this._associationMap[name] = Association.right;
@@ -2972,7 +2967,7 @@ class ExpressionPattern {
2972
2967
  else {
2973
2968
  this._associationMap[name] = Association.left;
2974
2969
  }
2975
- finalPatterns.push(clone);
2970
+ finalPatterns.push(binary);
2976
2971
  }
2977
2972
  });
2978
2973
  return finalPatterns;
@@ -3213,7 +3208,9 @@ class ExpressionPattern {
3213
3208
  return execPattern(this, text, record);
3214
3209
  }
3215
3210
  getTokens() {
3216
- return this.atomPatterns.map(p => p.getTokens()).flat();
3211
+ const atomTokens = this._atomPatterns.map(p => p.getTokens()).flat();
3212
+ const prefixTokens = this.prefixPatterns.map(p => p.getTokens()).flat();
3213
+ return [...prefixTokens, ...atomTokens];
3217
3214
  }
3218
3215
  getTokensAfter(childReference) {
3219
3216
  if (this._prefixPatterns.includes(childReference) || this._binaryPatterns.includes(childReference)) {
@@ -3242,7 +3239,9 @@ class ExpressionPattern {
3242
3239
  return this._parent.getTokensAfter(this);
3243
3240
  }
3244
3241
  getPatterns() {
3245
- return this.atomPatterns.map(p => p.getPatterns()).flat();
3242
+ const atomPatterns = this._atomPatterns.map(p => p.getPatterns()).flat();
3243
+ const prefixPatterns = this.prefixPatterns.map(p => p.getPatterns()).flat();
3244
+ return [...prefixPatterns, ...atomPatterns];
3246
3245
  }
3247
3246
  getPatternsAfter(childReference) {
3248
3247
  if (this._prefixPatterns.includes(childReference) || this._binaryPatterns.includes(childReference)) {
@@ -3274,7 +3273,7 @@ class ExpressionPattern {
3274
3273
  return findPattern(this, predicate);
3275
3274
  }
3276
3275
  clone(name = this._name) {
3277
- const clone = new ExpressionPattern(name, this._originalPatterns);
3276
+ const clone = new Expression(name, this._originalPatterns);
3278
3277
  clone._id = this._id;
3279
3278
  return clone;
3280
3279
  }
@@ -3284,7 +3283,7 @@ class ExpressionPattern {
3284
3283
  }
3285
3284
 
3286
3285
  let indexId = 0;
3287
- class RightAssociatedPattern {
3286
+ class RightAssociated {
3288
3287
  get id() {
3289
3288
  return this._id;
3290
3289
  }
@@ -3323,7 +3322,7 @@ class RightAssociatedPattern {
3323
3322
  return this.children[0].test(text, record);
3324
3323
  }
3325
3324
  clone(_name) {
3326
- const clone = new RightAssociatedPattern(this.children[0]);
3325
+ const clone = new RightAssociated(this.children[0]);
3327
3326
  clone._id = this._id;
3328
3327
  return clone;
3329
3328
  }
@@ -3416,14 +3415,6 @@ class Grammar {
3416
3415
  return this._buildPatternRecord();
3417
3416
  });
3418
3417
  }
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
3418
  parseString(expression) {
3428
3419
  this._parseContext = new ParseContext(this._params);
3429
3420
  const ast = this._tryToParse(expression);
@@ -3433,6 +3424,14 @@ class Grammar {
3433
3424
  this._buildPatterns(ast);
3434
3425
  return this._buildPatternRecord();
3435
3426
  }
3427
+ _buildPatternRecord() {
3428
+ const patterns = {};
3429
+ const allPatterns = Array.from(this._parseContext.patternsByName.values());
3430
+ allPatterns.forEach(p => {
3431
+ patterns[p.name] = new Context(p.name, p, allPatterns.filter(o => o !== p));
3432
+ });
3433
+ return patterns;
3434
+ }
3436
3435
  _tryToParse(expression) {
3437
3436
  const { ast, cursor, options, isComplete } = this._autoComplete.suggestFor(expression);
3438
3437
  if (!isComplete) {
@@ -3549,7 +3548,7 @@ class Grammar {
3549
3548
  const patterns = patternNodes.map(n => {
3550
3549
  const rightAssociated = n.find(n => n.name === "right-associated");
3551
3550
  if (rightAssociated != null) {
3552
- return new RightAssociatedPattern(this._buildPattern(n.children[0]));
3551
+ return new RightAssociated(this._buildPattern(n.children[0]));
3553
3552
  }
3554
3553
  else {
3555
3554
  return this._buildPattern(n.children[0]);
@@ -3558,13 +3557,13 @@ class Grammar {
3558
3557
  const hasRecursivePattern = patterns.some(p => this._isRecursive(name, p));
3559
3558
  if (hasRecursivePattern && !isGreedy) {
3560
3559
  try {
3561
- const expression = new ExpressionPattern(name, patterns);
3560
+ const expression = new Expression(name, patterns);
3562
3561
  return expression;
3563
3562
  }
3564
3563
  catch (_a) { }
3565
3564
  }
3566
- const or = new Options(name, patterns, isGreedy);
3567
- return or;
3565
+ const options = new Options(name, patterns, isGreedy);
3566
+ return options;
3568
3567
  }
3569
3568
  _isRecursive(name, pattern) {
3570
3569
  if (pattern.type === "right-associated" && this._isRecursivePattern(name, pattern.children[0])) {
@@ -3619,7 +3618,7 @@ class Grammar {
3619
3618
  this._parseContext.patternsByName.set(name, sequence);
3620
3619
  }
3621
3620
  _buildSequence(name, node) {
3622
- const patternNodes = node.children.filter(n => n.name !== "and-divider");
3621
+ const patternNodes = node.children.filter(n => n.name !== "sequence-divider");
3623
3622
  const patterns = patternNodes.map(n => {
3624
3623
  const patternNode = n.children[0].name === "not" ? n.children[1] : n.children[0];
3625
3624
  const isNot = n.find(n => n.name === "not") != null;
@@ -3824,5 +3823,5 @@ function patterns(strings, ...values) {
3824
3823
  return result;
3825
3824
  }
3826
3825
 
3827
- export { AutoComplete, Context, Cursor, CursorHistory, ExpressionPattern, Grammar, Literal, Node, Not, Optional, Options, ParseError, Reference, Regex, Repeat, Sequence, compact, grammar, patterns, remove };
3826
+ export { AutoComplete, Context, Cursor, CursorHistory, Expression, Grammar, Literal, Node, Not, Optional, Options, ParseError, Reference, Regex, Repeat, RightAssociated, Sequence, compact, grammar, patterns, remove };
3828
3827
  //# sourceMappingURL=index.esm.js.map