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.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
@@ -835,9 +835,23 @@ class Reference {
835
835
  return this._pattern;
836
836
  }
837
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
+ }
838
852
  const root = this._getRoot();
839
853
  return findPattern(root, (pattern) => {
840
- return pattern.name === this._name && pattern.type !== "reference";
854
+ return pattern.name === this._name && pattern.type !== "reference" && pattern.type !== "context";
841
855
  });
842
856
  }
843
857
  _getRoot() {
@@ -2502,6 +2516,77 @@ function getFurthestOptions(options) {
2502
2516
  return furthestOptions;
2503
2517
  }
2504
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(0, -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
+
2505
2590
  let anonymousIndexId = 0;
2506
2591
  const patternNodes = {
2507
2592
  "literal": true,
@@ -2556,9 +2641,17 @@ class Grammar {
2556
2641
  const ast = this._tryToParse(expression);
2557
2642
  yield this._resolveImports(ast);
2558
2643
  this._buildPatterns(ast);
2559
- return Object.fromEntries(this._parseContext.patternsByName);
2644
+ return this._buildPatternRecord();
2560
2645
  });
2561
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
+ }
2562
2655
  parseString(expression) {
2563
2656
  this._parseContext = new ParseContext(this._params);
2564
2657
  const ast = this._tryToParse(expression);
@@ -2566,7 +2659,7 @@ class Grammar {
2566
2659
  throw new Error("Cannot use imports on parseString, use parse instead.");
2567
2660
  }
2568
2661
  this._buildPatterns(ast);
2569
- return Object.fromEntries(this._parseContext.patternsByName);
2662
+ return this._buildPatternRecord();
2570
2663
  }
2571
2664
  _tryToParse(expression) {
2572
2665
  const { ast, cursor, options, isComplete } = this._autoComplete.suggestFor(expression);
@@ -2926,5 +3019,5 @@ function patterns(strings, ...values) {
2926
3019
  return result;
2927
3020
  }
2928
3021
 
2929
- 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 };
2930
3023
  //# sourceMappingURL=index.esm.js.map