clarity-pattern-parser 10.1.2 → 10.1.4

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.
@@ -9,12 +9,17 @@ export declare class Context implements Pattern {
9
9
  private _parent;
10
10
  private _children;
11
11
  private _pattern;
12
+ private _patterns;
12
13
  get id(): string;
13
14
  get type(): string;
14
15
  get name(): string;
15
16
  get parent(): Pattern | null;
16
17
  set parent(pattern: Pattern | null);
17
18
  get children(): Pattern[];
19
+ getPatternWithinContext(name: string): Pattern | null;
20
+ getPatternsWithinContext(): {
21
+ [x: string]: Pattern;
22
+ };
18
23
  constructor(name: string, pattern: Pattern, context?: Pattern[]);
19
24
  parse(cursor: Cursor): Node | null;
20
25
  exec(text: string, record?: boolean | undefined): ParseResult;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clarity-pattern-parser",
3
- "version": "10.1.2",
3
+ "version": "10.1.4",
4
4
  "description": "Parsing Library for Typescript and Javascript.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.esm.js",
@@ -12,6 +12,7 @@ export class Context implements Pattern {
12
12
  private _parent: Pattern | null;
13
13
  private _children: Pattern[];
14
14
  private _pattern: Pattern;
15
+ private _patterns: Record<string, Pattern>;
15
16
 
16
17
  get id(): string {
17
18
  return this._id;
@@ -37,20 +38,28 @@ export class Context implements Pattern {
37
38
  return this._children;
38
39
  }
39
40
 
41
+ getPatternWithinContext(name: string): Pattern | null {
42
+ return this._patterns[name] || null;
43
+ }
44
+
45
+ getPatternsWithinContext() {
46
+ return { ...this._patterns };
47
+ }
48
+
40
49
  constructor(name: string, pattern: Pattern, context: Pattern[] = []) {
41
50
  this._id = `context-${contextId++}`;
42
51
  this._type = "context";
43
52
  this._name = name;
44
53
  this._parent = null;
54
+ this._patterns = {};
45
55
 
46
- const clonedContext = context.map(p => p.clone());
47
- const clonedPattern = pattern.clone();
48
56
 
49
- clonedContext.forEach(p => p.parent = this);
57
+ const clonedPattern = pattern.clone();
58
+ context.forEach(p => this._patterns[p.name] = p);
50
59
  clonedPattern.parent = this;
51
60
 
52
61
  this._pattern = clonedPattern;
53
- this._children = [...clonedContext, clonedPattern];
62
+ this._children = [clonedPattern];
54
63
  }
55
64
 
56
65
  parse(cursor: Cursor): Node | null {
@@ -66,7 +75,7 @@ export class Context implements Pattern {
66
75
  }
67
76
 
68
77
  clone(name = this._name): Pattern {
69
- const clone = new Context(name, this._pattern, this._children.slice(0, -1));
78
+ const clone = new Context(name, this._pattern, Object.values(this._patterns));
70
79
  return clone;
71
80
  }
72
81
 
@@ -3,6 +3,7 @@ import { Cursor } from "./Cursor";
3
3
  import { Pattern } from "./Pattern";
4
4
  import { findPattern } from "./findPattern";
5
5
  import { ParseResult } from "./ParseResult";
6
+ import { Context } from "./Context";
6
7
 
7
8
  let idIndex = 0;
8
9
 
@@ -105,9 +106,7 @@ export class Reference implements Pattern {
105
106
  continue;
106
107
  }
107
108
 
108
- const foundPattern = findPattern(pattern, (pattern: Pattern) => {
109
- return pattern.name === this._name && pattern.type !== "reference" && pattern.type !== "context";
110
- });
109
+ const foundPattern = (pattern as Context).getPatternWithinContext(this.name);
111
110
 
112
111
  if (foundPattern != null) {
113
112
  return foundPattern;