clarity-pattern-parser 10.1.0 → 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/TODO.md CHANGED
@@ -1,9 +1,9 @@
1
- * Anonymous patterns within cpat files.
2
- ```
3
- pattern = ("Name" | "Name") & "Blah" & /foo/
4
- ```
5
- * Aliasing imports.
6
- * Send through params
1
+ * Add a Context Pattern
2
+ This will allow you to store a list of patterns and the pattern you want to start a parse from. This will hold the context of many patterns
3
+ that perhaps are references but are by way of the context. This will be helpful with cpat files.
4
+
5
+ * Optimizations We can add a map to the cursor where we can search through previous matches and if the match for this pattern with this id has already matched in other bracnhes of the parse it can just use the node and clone it and skip the full parse and speed up things tremendously
6
+
7
7
 
8
8
  * Generate typescript files from cpat
9
9
 
@@ -17,6 +17,7 @@ export declare class Grammar {
17
17
  constructor(options?: GrammarOptions);
18
18
  import(path: string): Promise<Record<string, Pattern>>;
19
19
  parse(expression: string): Promise<Record<string, Pattern>>;
20
+ private _buildPatternRecord;
20
21
  parseString(expression: string): Record<string, Pattern>;
21
22
  private _tryToParse;
22
23
  private _hasImports;
@@ -841,9 +841,23 @@
841
841
  return this._pattern;
842
842
  }
843
843
  _findPattern() {
844
+ let pattern = this._parent;
845
+ while (pattern != null) {
846
+ if (pattern.type !== "context") {
847
+ pattern = pattern.parent;
848
+ continue;
849
+ }
850
+ const foundPattern = findPattern(pattern, (pattern) => {
851
+ return pattern.name === this._name && pattern.type !== "reference" && pattern.type !== "context";
852
+ });
853
+ if (foundPattern != null) {
854
+ return foundPattern;
855
+ }
856
+ pattern = pattern.parent;
857
+ }
844
858
  const root = this._getRoot();
845
859
  return findPattern(root, (pattern) => {
846
- return pattern.name === this._name && pattern.type !== "reference";
860
+ return pattern.name === this._name && pattern.type !== "reference" && pattern.type !== "context";
847
861
  });
848
862
  }
849
863
  _getRoot() {
@@ -2508,6 +2522,77 @@
2508
2522
  return furthestOptions;
2509
2523
  }
2510
2524
 
2525
+ let contextId = 0;
2526
+ class Context {
2527
+ get id() {
2528
+ return this._id;
2529
+ }
2530
+ get type() {
2531
+ return this._type;
2532
+ }
2533
+ get name() {
2534
+ return this._name;
2535
+ }
2536
+ get parent() {
2537
+ return this._parent;
2538
+ }
2539
+ set parent(pattern) {
2540
+ this._parent = pattern;
2541
+ }
2542
+ get children() {
2543
+ return this._children;
2544
+ }
2545
+ constructor(name, pattern, context = []) {
2546
+ this._id = `context-${contextId++}`;
2547
+ this._type = "context";
2548
+ this._name = name;
2549
+ this._parent = null;
2550
+ const clonedContext = context.map(p => p.clone());
2551
+ const clonedPattern = pattern.clone();
2552
+ clonedContext.forEach(p => p.parent = this);
2553
+ clonedPattern.parent = this;
2554
+ this._pattern = clonedPattern;
2555
+ this._children = [...clonedContext, clonedPattern];
2556
+ }
2557
+ parse(cursor) {
2558
+ return this._pattern.parse(cursor);
2559
+ }
2560
+ exec(text, record) {
2561
+ return this._pattern.exec(text, record);
2562
+ }
2563
+ test(text, record) {
2564
+ return this._pattern.test(text, record);
2565
+ }
2566
+ clone(name = this._name) {
2567
+ const clone = new Context(name, this._pattern, this._children.slice(-1));
2568
+ return clone;
2569
+ }
2570
+ getTokens() {
2571
+ return this._pattern.getTokens();
2572
+ }
2573
+ getTokensAfter(childReference) {
2574
+ return this._pattern.getTokensAfter(childReference);
2575
+ }
2576
+ getNextTokens() {
2577
+ return this._pattern.getNextTokens();
2578
+ }
2579
+ getPatterns() {
2580
+ return this._pattern.getPatterns();
2581
+ }
2582
+ getPatternsAfter(childReference) {
2583
+ return this._pattern.getPatternsAfter(childReference);
2584
+ }
2585
+ getNextPatterns() {
2586
+ return this._pattern.getNextPatterns();
2587
+ }
2588
+ find(predicate) {
2589
+ return this._pattern.find(predicate);
2590
+ }
2591
+ isEqual(pattern) {
2592
+ return pattern.type === this.type && this.children.every((c, index) => c.isEqual(pattern.children[index]));
2593
+ }
2594
+ }
2595
+
2511
2596
  let anonymousIndexId = 0;
2512
2597
  const patternNodes = {
2513
2598
  "literal": true,
@@ -2562,9 +2647,17 @@
2562
2647
  const ast = this._tryToParse(expression);
2563
2648
  yield this._resolveImports(ast);
2564
2649
  this._buildPatterns(ast);
2565
- return Object.fromEntries(this._parseContext.patternsByName);
2650
+ return this._buildPatternRecord();
2566
2651
  });
2567
2652
  }
2653
+ _buildPatternRecord() {
2654
+ const patterns = {};
2655
+ const allPatterns = Array.from(this._parseContext.patternsByName.values());
2656
+ allPatterns.forEach(p => {
2657
+ patterns[p.name] = new Context(p.name, p, allPatterns.filter(o => o !== p));
2658
+ });
2659
+ return patterns;
2660
+ }
2568
2661
  parseString(expression) {
2569
2662
  this._parseContext = new ParseContext(this._params);
2570
2663
  const ast = this._tryToParse(expression);
@@ -2572,7 +2665,7 @@
2572
2665
  throw new Error("Cannot use imports on parseString, use parse instead.");
2573
2666
  }
2574
2667
  this._buildPatterns(ast);
2575
- return Object.fromEntries(this._parseContext.patternsByName);
2668
+ return this._buildPatternRecord();
2576
2669
  }
2577
2670
  _tryToParse(expression) {
2578
2671
  const { ast, cursor, options, isComplete } = this._autoComplete.suggestFor(expression);
@@ -2933,6 +3026,7 @@
2933
3026
  }
2934
3027
 
2935
3028
  exports.AutoComplete = AutoComplete;
3029
+ exports.Context = Context;
2936
3030
  exports.Cursor = Cursor;
2937
3031
  exports.CursorHistory = CursorHistory;
2938
3032
  exports.Grammar = Grammar;