clarity-pattern-parser 10.1.0 → 10.1.2

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
@@ -839,9 +839,23 @@ class Reference {
839
839
  return this._pattern;
840
840
  }
841
841
  _findPattern() {
842
+ let pattern = this._parent;
843
+ while (pattern != null) {
844
+ if (pattern.type !== "context") {
845
+ pattern = pattern.parent;
846
+ continue;
847
+ }
848
+ const foundPattern = findPattern(pattern, (pattern) => {
849
+ return pattern.name === this._name && pattern.type !== "reference" && pattern.type !== "context";
850
+ });
851
+ if (foundPattern != null) {
852
+ return foundPattern;
853
+ }
854
+ pattern = pattern.parent;
855
+ }
842
856
  const root = this._getRoot();
843
857
  return findPattern(root, (pattern) => {
844
- return pattern.name === this._name && pattern.type !== "reference";
858
+ return pattern.name === this._name && pattern.type !== "reference" && pattern.type !== "context";
845
859
  });
846
860
  }
847
861
  _getRoot() {
@@ -2506,6 +2520,77 @@ function getFurthestOptions(options) {
2506
2520
  return furthestOptions;
2507
2521
  }
2508
2522
 
2523
+ let contextId = 0;
2524
+ class Context {
2525
+ get id() {
2526
+ return this._id;
2527
+ }
2528
+ get type() {
2529
+ return this._type;
2530
+ }
2531
+ get name() {
2532
+ return this._name;
2533
+ }
2534
+ get parent() {
2535
+ return this._parent;
2536
+ }
2537
+ set parent(pattern) {
2538
+ this._parent = pattern;
2539
+ }
2540
+ get children() {
2541
+ return this._children;
2542
+ }
2543
+ constructor(name, pattern, context = []) {
2544
+ this._id = `context-${contextId++}`;
2545
+ this._type = "context";
2546
+ this._name = name;
2547
+ this._parent = null;
2548
+ const clonedContext = context.map(p => p.clone());
2549
+ const clonedPattern = pattern.clone();
2550
+ clonedContext.forEach(p => p.parent = this);
2551
+ clonedPattern.parent = this;
2552
+ this._pattern = clonedPattern;
2553
+ this._children = [...clonedContext, clonedPattern];
2554
+ }
2555
+ parse(cursor) {
2556
+ return this._pattern.parse(cursor);
2557
+ }
2558
+ exec(text, record) {
2559
+ return this._pattern.exec(text, record);
2560
+ }
2561
+ test(text, record) {
2562
+ return this._pattern.test(text, record);
2563
+ }
2564
+ clone(name = this._name) {
2565
+ const clone = new Context(name, this._pattern, this._children.slice(0, -1));
2566
+ return clone;
2567
+ }
2568
+ getTokens() {
2569
+ return this._pattern.getTokens();
2570
+ }
2571
+ getTokensAfter(childReference) {
2572
+ return this._pattern.getTokensAfter(childReference);
2573
+ }
2574
+ getNextTokens() {
2575
+ return this._pattern.getNextTokens();
2576
+ }
2577
+ getPatterns() {
2578
+ return this._pattern.getPatterns();
2579
+ }
2580
+ getPatternsAfter(childReference) {
2581
+ return this._pattern.getPatternsAfter(childReference);
2582
+ }
2583
+ getNextPatterns() {
2584
+ return this._pattern.getNextPatterns();
2585
+ }
2586
+ find(predicate) {
2587
+ return this._pattern.find(predicate);
2588
+ }
2589
+ isEqual(pattern) {
2590
+ return pattern.type === this.type && this.children.every((c, index) => c.isEqual(pattern.children[index]));
2591
+ }
2592
+ }
2593
+
2509
2594
  let anonymousIndexId = 0;
2510
2595
  const patternNodes = {
2511
2596
  "literal": true,
@@ -2560,9 +2645,17 @@ class Grammar {
2560
2645
  const ast = this._tryToParse(expression);
2561
2646
  yield this._resolveImports(ast);
2562
2647
  this._buildPatterns(ast);
2563
- return Object.fromEntries(this._parseContext.patternsByName);
2648
+ return this._buildPatternRecord();
2564
2649
  });
2565
2650
  }
2651
+ _buildPatternRecord() {
2652
+ const patterns = {};
2653
+ const allPatterns = Array.from(this._parseContext.patternsByName.values());
2654
+ allPatterns.forEach(p => {
2655
+ patterns[p.name] = new Context(p.name, p, allPatterns.filter(o => o !== p));
2656
+ });
2657
+ return patterns;
2658
+ }
2566
2659
  parseString(expression) {
2567
2660
  this._parseContext = new ParseContext(this._params);
2568
2661
  const ast = this._tryToParse(expression);
@@ -2570,7 +2663,7 @@ class Grammar {
2570
2663
  throw new Error("Cannot use imports on parseString, use parse instead.");
2571
2664
  }
2572
2665
  this._buildPatterns(ast);
2573
- return Object.fromEntries(this._parseContext.patternsByName);
2666
+ return this._buildPatternRecord();
2574
2667
  }
2575
2668
  _tryToParse(expression) {
2576
2669
  const { ast, cursor, options, isComplete } = this._autoComplete.suggestFor(expression);
@@ -2931,6 +3024,7 @@ function patterns(strings, ...values) {
2931
3024
  }
2932
3025
 
2933
3026
  exports.AutoComplete = AutoComplete;
3027
+ exports.Context = Context;
2934
3028
  exports.Cursor = Cursor;
2935
3029
  exports.CursorHistory = CursorHistory;
2936
3030
  exports.Grammar = Grammar;