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/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;
@@ -285,6 +285,7 @@
285
285
  this._patterns = [];
286
286
  this._nodes = [];
287
287
  this._errors = [];
288
+ this._records = [];
288
289
  }
289
290
  get isRecording() {
290
291
  return this._isRecording;
@@ -307,6 +308,9 @@
307
308
  get error() {
308
309
  return this._currentError;
309
310
  }
311
+ get records() {
312
+ return this._records;
313
+ }
310
314
  get nodes() {
311
315
  return this._nodes;
312
316
  }
@@ -317,6 +321,11 @@
317
321
  if (this._isRecording) {
318
322
  this._patterns.push(pattern);
319
323
  this._nodes.push(node);
324
+ this._records.push({
325
+ pattern,
326
+ ast: node,
327
+ error: null
328
+ });
320
329
  }
321
330
  this._rootMatch.pattern = pattern;
322
331
  this._rootMatch.node = node;
@@ -356,6 +365,11 @@
356
365
  }
357
366
  if (this._isRecording) {
358
367
  this._errors.push(error);
368
+ this.records.push({
369
+ pattern,
370
+ ast: null,
371
+ error
372
+ });
359
373
  }
360
374
  }
361
375
  startRecording() {
@@ -406,6 +420,9 @@
406
420
  get errors() {
407
421
  return this._history.errors;
408
422
  }
423
+ get records() {
424
+ return this._history.records;
425
+ }
409
426
  get index() {
410
427
  return this._index;
411
428
  }
@@ -824,9 +841,23 @@
824
841
  return this._pattern;
825
842
  }
826
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
+ }
827
858
  const root = this._getRoot();
828
859
  return findPattern(root, (pattern) => {
829
- return pattern.name === this._name && pattern.type !== "reference";
860
+ return pattern.name === this._name && pattern.type !== "reference" && pattern.type !== "context";
830
861
  });
831
862
  }
832
863
  _getRoot() {
@@ -2491,6 +2522,77 @@
2491
2522
  return furthestOptions;
2492
2523
  }
2493
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
+
2494
2596
  let anonymousIndexId = 0;
2495
2597
  const patternNodes = {
2496
2598
  "literal": true,
@@ -2545,9 +2647,17 @@
2545
2647
  const ast = this._tryToParse(expression);
2546
2648
  yield this._resolveImports(ast);
2547
2649
  this._buildPatterns(ast);
2548
- return Object.fromEntries(this._parseContext.patternsByName);
2650
+ return this._buildPatternRecord();
2549
2651
  });
2550
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
+ }
2551
2661
  parseString(expression) {
2552
2662
  this._parseContext = new ParseContext(this._params);
2553
2663
  const ast = this._tryToParse(expression);
@@ -2555,7 +2665,7 @@
2555
2665
  throw new Error("Cannot use imports on parseString, use parse instead.");
2556
2666
  }
2557
2667
  this._buildPatterns(ast);
2558
- return Object.fromEntries(this._parseContext.patternsByName);
2668
+ return this._buildPatternRecord();
2559
2669
  }
2560
2670
  _tryToParse(expression) {
2561
2671
  const { ast, cursor, options, isComplete } = this._autoComplete.suggestFor(expression);
@@ -2916,6 +3026,7 @@
2916
3026
  }
2917
3027
 
2918
3028
  exports.AutoComplete = AutoComplete;
3029
+ exports.Context = Context;
2919
3030
  exports.Cursor = Cursor;
2920
3031
  exports.CursorHistory = CursorHistory;
2921
3032
  exports.Grammar = Grammar;