clarity-pattern-parser 11.3.8 → 11.3.10

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.
@@ -1257,12 +1257,10 @@
1257
1257
  }
1258
1258
  }
1259
1259
  }
1260
- if (this._trimDivider && this._hasDivider) {
1261
- const isDividerLastMatch = this.children.length > 1 && nodes.length > 1 && nodes[nodes.length - 1].name === this.children[1].name;
1262
- if (isDividerLastMatch) {
1263
- const node = nodes.pop();
1264
- cursor.moveTo(node.firstIndex);
1265
- }
1260
+ const endedOnDivider = this._hasDivider && nodes.length % modulo === 0;
1261
+ if (this._trimDivider && endedOnDivider) {
1262
+ const node = nodes.pop();
1263
+ cursor.moveTo(node.firstIndex);
1266
1264
  }
1267
1265
  if (matchCount < this._min) {
1268
1266
  const lastIndex = cursor.index;
@@ -1398,6 +1396,7 @@
1398
1396
  this._firstIndex = 0;
1399
1397
  this._nodes = [];
1400
1398
  this._trimDivider = options.trimDivider == null ? false : options.trimDivider;
1399
+ this._patterns = [];
1401
1400
  }
1402
1401
  _assignChildrenToParent(children) {
1403
1402
  for (const child of children) {
@@ -1413,6 +1412,7 @@
1413
1412
  parse(cursor) {
1414
1413
  this._firstIndex = cursor.index;
1415
1414
  this._nodes = [];
1415
+ this._patterns = [];
1416
1416
  const passed = this._tryToParse(cursor);
1417
1417
  if (passed) {
1418
1418
  cursor.resolveError();
@@ -1464,6 +1464,7 @@
1464
1464
  }
1465
1465
  if (repeatNode != null) {
1466
1466
  this._nodes.push(repeatNode);
1467
+ this._patterns.push(this._pattern);
1467
1468
  if (!cursor.hasNext()) {
1468
1469
  passed = true;
1469
1470
  break;
@@ -1488,6 +1489,7 @@
1488
1489
  }
1489
1490
  else {
1490
1491
  this._nodes.push(dividerNode);
1492
+ this._patterns.push(this._divider);
1491
1493
  if (!cursor.hasNext()) {
1492
1494
  passed = true;
1493
1495
  break;
@@ -1510,11 +1512,11 @@
1510
1512
  return passed;
1511
1513
  }
1512
1514
  _createNode(cursor) {
1513
- var _a;
1514
1515
  const hasDivider = this._divider != null;
1516
+ const lastPattern = this._patterns[this._patterns.length - 1];
1515
1517
  if (hasDivider &&
1516
1518
  this._trimDivider &&
1517
- this._nodes[this._nodes.length - 1].name === ((_a = this._divider) === null || _a === void 0 ? void 0 : _a.name)) {
1519
+ lastPattern === this._divider) {
1518
1520
  const dividerNode = this._nodes.pop();
1519
1521
  cursor.moveTo(dividerNode.firstIndex);
1520
1522
  }
@@ -4062,7 +4064,7 @@
4062
4064
  const furthestError = cursor.furthestError;
4063
4065
  const furthestMatch = cursor.allMatchedNodes[cursor.allMatchedNodes.length - 1];
4064
4066
  if (furthestError && furthestMatch) {
4065
- if (furthestError.lastIndex > furthestMatch.endIndex) {
4067
+ if (furthestMatch.endIndex > furthestError.lastIndex) {
4066
4068
  return furthestMatch.endIndex;
4067
4069
  }
4068
4070
  else {
@@ -4117,7 +4119,7 @@
4117
4119
  }
4118
4120
  _createSuggestionsFromRoot() {
4119
4121
  const suggestions = [];
4120
- const tokens = this._pattern.getTokens();
4122
+ const tokens = [...this._pattern.getTokens(), ...this._getTokensForPattern(this._pattern)];
4121
4123
  for (const token of tokens) {
4122
4124
  if (suggestions.findIndex(s => s.text === token) === -1) {
4123
4125
  suggestions.push(this._createSuggestion("", token));
@@ -4129,15 +4131,29 @@
4129
4131
  if (match.pattern == null) {
4130
4132
  return this._createSuggestions(-1, this._getTokensForPattern(this._pattern));
4131
4133
  }
4132
- const leafPattern = match.pattern;
4133
- const parent = match.pattern.parent;
4134
- if (parent !== null && match.node != null) {
4135
- const patterns = leafPattern.getNextPatterns();
4136
- const tokens = patterns.reduce((acc, pattern) => {
4137
- acc.push(...this._getTokensForPattern(pattern));
4134
+ if (match.node != null) {
4135
+ const textStartingMatch = this._text.slice(match.node.startIndex, match.node.endIndex);
4136
+ const currentPatternsTokens = this._getTokensForPattern(match.pattern);
4137
+ /**
4138
+ * Compares tokens to current text and extracts remainder tokens
4139
+ * - IE. **currentText:** *abc*, **baseToken:** *abcdef*, **trailingToken:** *def*
4140
+ */
4141
+ const trailingTokens = currentPatternsTokens.reduce((acc, token) => {
4142
+ if (token.startsWith(textStartingMatch)) {
4143
+ const sliced = token.slice(textStartingMatch.length);
4144
+ if (sliced !== '') {
4145
+ acc.push(sliced);
4146
+ }
4147
+ }
4138
4148
  return acc;
4139
4149
  }, []);
4140
- return this._createSuggestions(match.node.lastIndex, tokens);
4150
+ const leafPatterns = match.pattern.getNextPatterns();
4151
+ const leafTokens = leafPatterns.reduce((acc, leafPattern) => {
4152
+ acc.push(...this._getTokensForPattern(leafPattern));
4153
+ return acc;
4154
+ }, []);
4155
+ const allTokens = [...trailingTokens, ...leafTokens];
4156
+ return this._createSuggestions(match.node.lastIndex, allTokens);
4141
4157
  }
4142
4158
  else {
4143
4159
  return [];
@@ -4147,44 +4163,54 @@
4147
4163
  const augmentedTokens = this._getAugmentedTokens(pattern);
4148
4164
  if (this._options.greedyPatternNames != null && this._options.greedyPatternNames.includes(pattern.name)) {
4149
4165
  const nextPatterns = pattern.getNextPatterns();
4150
- const tokens = [];
4151
4166
  const nextPatternTokens = nextPatterns.reduce((acc, pattern) => {
4152
4167
  acc.push(...this._getTokensForPattern(pattern));
4153
4168
  return acc;
4154
4169
  }, []);
4155
- for (let token of augmentedTokens) {
4156
- for (let nextPatternToken of nextPatternTokens) {
4157
- tokens.push(token + nextPatternToken);
4170
+ // using set to prevent duplicates
4171
+ const tokens = new Set();
4172
+ for (const token of augmentedTokens) {
4173
+ for (const nextPatternToken of nextPatternTokens) {
4174
+ tokens.add(token + nextPatternToken);
4158
4175
  }
4159
4176
  }
4160
- return tokens;
4177
+ return [...tokens];
4161
4178
  }
4162
4179
  else {
4163
4180
  return augmentedTokens;
4164
4181
  }
4165
4182
  }
4166
4183
  _getAugmentedTokens(pattern) {
4184
+ var _a, _b;
4167
4185
  const customTokensMap = this._options.customTokens || {};
4168
4186
  const leafPatterns = pattern.getPatterns();
4169
- const tokens = customTokensMap[pattern.name] || [];
4170
- leafPatterns.forEach(p => {
4171
- const augmentedTokens = customTokensMap[p.name] || [];
4172
- tokens.push(...p.getTokens(), ...augmentedTokens);
4173
- });
4174
- return tokens;
4187
+ /** Using Set to
4188
+ * - prevent duplicates
4189
+ * - prevent mutation of original customTokensMap
4190
+ */
4191
+ const customTokensForPattern = new Set((_a = customTokensMap[pattern.name]) !== null && _a !== void 0 ? _a : []);
4192
+ for (const lp of leafPatterns) {
4193
+ const augmentedTokens = (_b = customTokensMap[lp.name]) !== null && _b !== void 0 ? _b : [];
4194
+ const lpsCombinedTokens = [...lp.getTokens(), ...augmentedTokens];
4195
+ for (const token of lpsCombinedTokens) {
4196
+ customTokensForPattern.add(token);
4197
+ }
4198
+ }
4199
+ return [...customTokensForPattern];
4175
4200
  }
4176
4201
  _createSuggestions(lastIndex, tokens) {
4177
- let substring = lastIndex === -1 ? "" : this._cursor.getChars(0, lastIndex);
4202
+ let textToIndex = lastIndex === -1 ? "" : this._cursor.getChars(0, lastIndex);
4178
4203
  const suggestionStrings = [];
4179
4204
  const options = [];
4180
4205
  for (const token of tokens) {
4181
- const suggestion = substring + token;
4182
- const startsWith = suggestion.startsWith(substring);
4206
+ // concatenated for start index identification inside createSuggestion
4207
+ const suggestion = textToIndex + token;
4183
4208
  const alreadyExist = suggestionStrings.includes(suggestion);
4184
4209
  const isSameAsText = suggestion === this._text;
4185
- if (startsWith && !alreadyExist && !isSameAsText) {
4210
+ if (!alreadyExist && !isSameAsText) {
4186
4211
  suggestionStrings.push(suggestion);
4187
- options.push(this._createSuggestion(this._cursor.text, suggestion));
4212
+ const suggestionOption = this._createSuggestion(this._cursor.text, suggestion);
4213
+ options.push(suggestionOption);
4188
4214
  }
4189
4215
  }
4190
4216
  const reducedOptions = getFurthestOptions(options);
@@ -4194,10 +4220,11 @@
4194
4220
  _createSuggestion(fullText, suggestion) {
4195
4221
  const furthestMatch = findMatchIndex(suggestion, fullText);
4196
4222
  const text = suggestion.slice(furthestMatch);
4197
- return {
4223
+ const option = {
4198
4224
  text: text,
4199
4225
  startIndex: furthestMatch,
4200
4226
  };
4227
+ return option;
4201
4228
  }
4202
4229
  static suggestFor(text, pattern, options) {
4203
4230
  return new AutoComplete(pattern, options).suggestFor(text);