clarity-pattern-parser 10.0.8 → 10.1.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.d.ts CHANGED
@@ -18,4 +18,5 @@ import { CursorHistory, Match } from "./patterns/CursorHistory";
18
18
  import { ParseResult } from "./patterns/ParseResult";
19
19
  import { grammar } from "./grammar/patterns/grammar";
20
20
  import { patterns } from "./grammar/patterns";
21
- export { Node, Grammar, AutoComplete, AutoCompleteOptions, Suggestion, SuggestionOption, Sequence, Cursor, CursorHistory, Match, Literal, Not, Options, Optional, ParseError, ParseResult, Pattern, Reference, Regex, Repeat, grammar, patterns, };
21
+ import { Context } from "./patterns/Context";
22
+ export { Node, Grammar, AutoComplete, AutoCompleteOptions, Suggestion, SuggestionOption, Sequence, Cursor, CursorHistory, Match, Context, Literal, Not, Options, Optional, ParseError, ParseResult, Pattern, Reference, Regex, Repeat, grammar, patterns, };
package/dist/index.esm.js CHANGED
@@ -279,6 +279,7 @@ class CursorHistory {
279
279
  this._patterns = [];
280
280
  this._nodes = [];
281
281
  this._errors = [];
282
+ this._records = [];
282
283
  }
283
284
  get isRecording() {
284
285
  return this._isRecording;
@@ -301,6 +302,9 @@ class CursorHistory {
301
302
  get error() {
302
303
  return this._currentError;
303
304
  }
305
+ get records() {
306
+ return this._records;
307
+ }
304
308
  get nodes() {
305
309
  return this._nodes;
306
310
  }
@@ -311,6 +315,11 @@ class CursorHistory {
311
315
  if (this._isRecording) {
312
316
  this._patterns.push(pattern);
313
317
  this._nodes.push(node);
318
+ this._records.push({
319
+ pattern,
320
+ ast: node,
321
+ error: null
322
+ });
314
323
  }
315
324
  this._rootMatch.pattern = pattern;
316
325
  this._rootMatch.node = node;
@@ -350,6 +359,11 @@ class CursorHistory {
350
359
  }
351
360
  if (this._isRecording) {
352
361
  this._errors.push(error);
362
+ this.records.push({
363
+ pattern,
364
+ ast: null,
365
+ error
366
+ });
353
367
  }
354
368
  }
355
369
  startRecording() {
@@ -400,6 +414,9 @@ class Cursor {
400
414
  get errors() {
401
415
  return this._history.errors;
402
416
  }
417
+ get records() {
418
+ return this._history.records;
419
+ }
403
420
  get index() {
404
421
  return this._index;
405
422
  }
@@ -818,9 +835,23 @@ class Reference {
818
835
  return this._pattern;
819
836
  }
820
837
  _findPattern() {
838
+ let pattern = this._parent;
839
+ while (pattern != null) {
840
+ if (pattern.type !== "context") {
841
+ pattern = pattern.parent;
842
+ continue;
843
+ }
844
+ const foundPattern = findPattern(pattern, (pattern) => {
845
+ return pattern.name === this._name && pattern.type !== "reference" && pattern.type !== "context";
846
+ });
847
+ if (foundPattern != null) {
848
+ return foundPattern;
849
+ }
850
+ pattern = pattern.parent;
851
+ }
821
852
  const root = this._getRoot();
822
853
  return findPattern(root, (pattern) => {
823
- return pattern.name === this._name && pattern.type !== "reference";
854
+ return pattern.name === this._name && pattern.type !== "reference" && pattern.type !== "context";
824
855
  });
825
856
  }
826
857
  _getRoot() {
@@ -2485,6 +2516,77 @@ function getFurthestOptions(options) {
2485
2516
  return furthestOptions;
2486
2517
  }
2487
2518
 
2519
+ let contextId = 0;
2520
+ class Context {
2521
+ get id() {
2522
+ return this._id;
2523
+ }
2524
+ get type() {
2525
+ return this._type;
2526
+ }
2527
+ get name() {
2528
+ return this._name;
2529
+ }
2530
+ get parent() {
2531
+ return this._parent;
2532
+ }
2533
+ set parent(pattern) {
2534
+ this._parent = pattern;
2535
+ }
2536
+ get children() {
2537
+ return this._children;
2538
+ }
2539
+ constructor(name, pattern, context = []) {
2540
+ this._id = `context-${contextId++}`;
2541
+ this._type = "context";
2542
+ this._name = name;
2543
+ this._parent = null;
2544
+ const clonedContext = context.map(p => p.clone());
2545
+ const clonedPattern = pattern.clone();
2546
+ clonedContext.forEach(p => p.parent = this);
2547
+ clonedPattern.parent = this;
2548
+ this._pattern = clonedPattern;
2549
+ this._children = [...clonedContext, clonedPattern];
2550
+ }
2551
+ parse(cursor) {
2552
+ return this._pattern.parse(cursor);
2553
+ }
2554
+ exec(text, record) {
2555
+ return this._pattern.exec(text, record);
2556
+ }
2557
+ test(text, record) {
2558
+ return this._pattern.test(text, record);
2559
+ }
2560
+ clone(name = this._name) {
2561
+ const clone = new Context(name, this._pattern, this._children.slice(-1));
2562
+ return clone;
2563
+ }
2564
+ getTokens() {
2565
+ return this._pattern.getTokens();
2566
+ }
2567
+ getTokensAfter(childReference) {
2568
+ return this._pattern.getTokensAfter(childReference);
2569
+ }
2570
+ getNextTokens() {
2571
+ return this._pattern.getNextTokens();
2572
+ }
2573
+ getPatterns() {
2574
+ return this._pattern.getPatterns();
2575
+ }
2576
+ getPatternsAfter(childReference) {
2577
+ return this._pattern.getPatternsAfter(childReference);
2578
+ }
2579
+ getNextPatterns() {
2580
+ return this._pattern.getNextPatterns();
2581
+ }
2582
+ find(predicate) {
2583
+ return this._pattern.find(predicate);
2584
+ }
2585
+ isEqual(pattern) {
2586
+ return pattern.type === this.type && this.children.every((c, index) => c.isEqual(pattern.children[index]));
2587
+ }
2588
+ }
2589
+
2488
2590
  let anonymousIndexId = 0;
2489
2591
  const patternNodes = {
2490
2592
  "literal": true,
@@ -2539,9 +2641,17 @@ class Grammar {
2539
2641
  const ast = this._tryToParse(expression);
2540
2642
  yield this._resolveImports(ast);
2541
2643
  this._buildPatterns(ast);
2542
- return Object.fromEntries(this._parseContext.patternsByName);
2644
+ return this._buildPatternRecord();
2543
2645
  });
2544
2646
  }
2647
+ _buildPatternRecord() {
2648
+ const patterns = {};
2649
+ const allPatterns = Array.from(this._parseContext.patternsByName.values());
2650
+ allPatterns.forEach(p => {
2651
+ patterns[p.name] = new Context(p.name, p, allPatterns.filter(o => o !== p));
2652
+ });
2653
+ return patterns;
2654
+ }
2545
2655
  parseString(expression) {
2546
2656
  this._parseContext = new ParseContext(this._params);
2547
2657
  const ast = this._tryToParse(expression);
@@ -2549,7 +2659,7 @@ class Grammar {
2549
2659
  throw new Error("Cannot use imports on parseString, use parse instead.");
2550
2660
  }
2551
2661
  this._buildPatterns(ast);
2552
- return Object.fromEntries(this._parseContext.patternsByName);
2662
+ return this._buildPatternRecord();
2553
2663
  }
2554
2664
  _tryToParse(expression) {
2555
2665
  const { ast, cursor, options, isComplete } = this._autoComplete.suggestFor(expression);
@@ -2909,5 +3019,5 @@ function patterns(strings, ...values) {
2909
3019
  return result;
2910
3020
  }
2911
3021
 
2912
- export { AutoComplete, Cursor, CursorHistory, Grammar, Literal, Node, Not, Optional, Options, ParseError, Reference, Regex, Repeat, Sequence, grammar, patterns };
3022
+ export { AutoComplete, Context, Cursor, CursorHistory, Grammar, Literal, Node, Not, Optional, Options, ParseError, Reference, Regex, Repeat, Sequence, grammar, patterns };
2913
3023
  //# sourceMappingURL=index.esm.js.map