clarity-pattern-parser 10.1.21 → 10.1.22

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.
@@ -20,7 +20,7 @@ export declare class Reference implements Pattern {
20
20
  test(text: string): boolean;
21
21
  exec(text: string, record?: boolean): ParseResult;
22
22
  parse(cursor: Cursor): Node | null;
23
- private _getPatternSafely;
23
+ getReferencePatternSafely(): Pattern;
24
24
  private _findPattern;
25
25
  private _isValidPattern;
26
26
  private _getRoot;
@@ -0,0 +1,2 @@
1
+ import { Pattern } from "./Pattern";
2
+ export declare function isRecursivePattern(pattern: Pattern): boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clarity-pattern-parser",
3
- "version": "10.1.21",
3
+ "version": "10.1.22",
4
4
  "description": "Parsing Library for Typescript and Javascript.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.esm.js",
@@ -5,6 +5,7 @@ import { clonePatterns } from "./clonePatterns";
5
5
  import { findPattern } from "./findPattern";
6
6
  import { ParseResult } from "./ParseResult";
7
7
  import { DepthCache } from './DepthCache';
8
+ import { isRecursivePattern } from "./isRecursivePattern";
8
9
 
9
10
  /*
10
11
  The following is created to reduce the overhead of recursion check.
@@ -143,7 +144,7 @@ export class Options implements Pattern {
143
144
  const tokens: string[] = [];
144
145
 
145
146
  for (const pattern of this._children) {
146
- if (pattern.type === "reference" && pattern.name === this.name) {
147
+ if (isRecursivePattern(pattern)) {
147
148
  continue;
148
149
  }
149
150
  tokens.push(...pattern.getTokens());
@@ -172,7 +173,7 @@ export class Options implements Pattern {
172
173
  const patterns: Pattern[] = [];
173
174
 
174
175
  for (const pattern of this._children) {
175
- if (pattern.type === "reference" && pattern.name === this.name) {
176
+ if (isRecursivePattern(pattern)) {
176
177
  continue;
177
178
  }
178
179
  patterns.push(...pattern.getPatterns());
@@ -70,10 +70,10 @@ export class Reference implements Pattern {
70
70
  }
71
71
 
72
72
  parse(cursor: Cursor): Node | null {
73
- return this._getPatternSafely().parse(cursor);
73
+ return this.getReferencePatternSafely().parse(cursor);
74
74
  }
75
75
 
76
- private _getPatternSafely(): Pattern {
76
+ getReferencePatternSafely(): Pattern {
77
77
  if (this._pattern === null) {
78
78
  let pattern: Pattern | null = null;
79
79
 
@@ -150,7 +150,7 @@ export class Reference implements Pattern {
150
150
  }
151
151
 
152
152
  getTokens(): string[] {
153
- return this._getPatternSafely().getTokens();
153
+ return this.getReferencePatternSafely().getTokens();
154
154
  }
155
155
 
156
156
  getTokensAfter(_lastMatched: Pattern): string[] {
@@ -170,7 +170,7 @@ export class Reference implements Pattern {
170
170
  }
171
171
 
172
172
  getPatterns(): Pattern[] {
173
- return this._getPatternSafely().getPatterns();
173
+ return this.getReferencePatternSafely().getPatterns();
174
174
  }
175
175
 
176
176
  getPatternsAfter(_childReference: Pattern): Pattern[] {
@@ -5,6 +5,7 @@ import { clonePatterns } from "./clonePatterns";
5
5
  import { filterOutNull } from "./filterOutNull";
6
6
  import { findPattern } from "./findPattern";
7
7
  import { DepthCache } from "./DepthCache";
8
+ import { isRecursivePattern } from "./isRecursivePattern";
8
9
 
9
10
  const depthCache = new DepthCache();
10
11
  let idIndex = 0;
@@ -217,7 +218,7 @@ export class Sequence implements Pattern {
217
218
  const tokens: string[] = [];
218
219
 
219
220
  for (const pattern of this._children) {
220
- if (pattern.type === "reference" && pattern.name === this.name && pattern === this.children[0]) {
221
+ if (isRecursivePattern(pattern) && pattern === this._children[0]) {
221
222
  return tokens;
222
223
  }
223
224
 
@@ -251,8 +252,7 @@ export class Sequence implements Pattern {
251
252
  const patterns: Pattern[] = [];
252
253
 
253
254
  for (const pattern of this._children) {
254
-
255
- if (pattern.type === "reference" && pattern.name === this.name && pattern === this.children[0]) {
255
+ if (isRecursivePattern(pattern) && pattern === this._children[0]) {
256
256
  return patterns;
257
257
  }
258
258
 
@@ -0,0 +1,21 @@
1
+ import { Pattern } from "./Pattern";
2
+ import { Reference } from "./Reference";
3
+
4
+ export function isRecursivePattern(pattern: Pattern) {
5
+ let onPattern = pattern.parent;
6
+ let depth = 0;
7
+
8
+ while (onPattern != null) {
9
+ if (onPattern.id === pattern.id) {
10
+ depth++;
11
+ }
12
+
13
+ onPattern = onPattern.parent;
14
+
15
+ if (depth > 1){
16
+ return true;
17
+ }
18
+ }
19
+
20
+ return false;
21
+ }