clarity-pattern-parser 10.0.8 → 10.1.0

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.
@@ -19,6 +19,7 @@ export declare class Cursor {
19
19
  get furthestError(): ParseError | null;
20
20
  get error(): ParseError | null;
21
21
  get errors(): ParseError[];
22
+ get records(): import("./CursorHistory").HistoryRecord[];
22
23
  get index(): number;
23
24
  get length(): number;
24
25
  get hasError(): boolean;
@@ -5,6 +5,11 @@ export interface Match {
5
5
  pattern: Pattern | null;
6
6
  node: Node | null;
7
7
  }
8
+ export interface HistoryRecord {
9
+ pattern: Pattern;
10
+ error: ParseError | null;
11
+ ast: Node | null;
12
+ }
8
13
  export declare class CursorHistory {
9
14
  private _isRecording;
10
15
  private _leafMatches;
@@ -14,6 +19,7 @@ export declare class CursorHistory {
14
19
  private _patterns;
15
20
  private _nodes;
16
21
  private _errors;
22
+ private _records;
17
23
  get isRecording(): boolean;
18
24
  get rootMatch(): Match;
19
25
  get leafMatch(): Match;
@@ -21,6 +27,7 @@ export declare class CursorHistory {
21
27
  get furthestError(): ParseError | null;
22
28
  get errors(): ParseError[];
23
29
  get error(): ParseError | null;
30
+ get records(): HistoryRecord[];
24
31
  get nodes(): Node[];
25
32
  get patterns(): Pattern[];
26
33
  recordMatch(pattern: Pattern, node: Node): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clarity-pattern-parser",
3
- "version": "10.0.8",
3
+ "version": "10.1.0",
4
4
  "description": "Parsing Library for Typescript and Javascript.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.esm.js",
@@ -1,6 +1,21 @@
1
1
  import { Cursor } from "./Cursor";
2
2
  import { Literal } from "./Literal";
3
- import { Node } from "../ast/Node"
3
+ import { Node } from "../ast/Node";
4
+ import { patterns } from "../grammar/patterns";
5
+
6
+ const { name } = patterns`
7
+ john = "John"
8
+ jane = "Jane"
9
+ first-names = john | jane
10
+
11
+ doe = "Doe"
12
+ smith = "Smith"
13
+ last-names = doe | smith
14
+
15
+ space = " "
16
+
17
+ name = first-names + space + last-names
18
+ `;
4
19
 
5
20
  describe("Cursor", () => {
6
21
  test("Empty Text", () => {
@@ -20,7 +35,7 @@ describe("Cursor", () => {
20
35
  const cursor = new Cursor("Hello World!");
21
36
  cursor.moveTo(6);
22
37
 
23
- expect(cursor.currentChar).toBe("W")
38
+ expect(cursor.currentChar).toBe("W");
24
39
 
25
40
  cursor.moveToFirstChar();
26
41
  cursor.previous();
@@ -35,7 +50,7 @@ describe("Cursor", () => {
35
50
  cursor.moveToLastChar();
36
51
  cursor.next();
37
52
 
38
- expect(cursor.isOnLast).toBeTruthy()
53
+ expect(cursor.isOnLast).toBeTruthy();
39
54
  expect(cursor.currentChar).toBe("!");
40
55
 
41
56
  cursor.previous();
@@ -69,7 +84,7 @@ describe("Cursor", () => {
69
84
 
70
85
  const cursor = new Cursor("Hello World!");
71
86
 
72
- cursor.recordMatch(pattern, node)
87
+ cursor.recordMatch(pattern, node);
73
88
 
74
89
  expect(cursor.leafMatch.node).toBe(node);
75
90
  expect(cursor.leafMatch.pattern).toBe(pattern);
@@ -96,7 +111,46 @@ describe("Cursor", () => {
96
111
 
97
112
  expect(hello).toBe("Hello");
98
113
  expect(cursor.length).toBe(12);
99
- expect(cursor.text).toBe("Hello World!")
114
+ expect(cursor.text).toBe("Hello World!");
100
115
  expect(cursor.index).toBe(0);
101
116
  });
117
+
118
+ test("Records All matches", () => {
119
+ const { ast, cursor } = name.exec("John Doe", true);
120
+ const records = cursor.records;
121
+
122
+ expect(ast?.toString()).toBe("John Doe");
123
+ expect(records[0].ast?.toString()).toBe("John");
124
+ expect(records[1].ast?.toString()).toBe(" ");
125
+ expect(records[2].ast?.toString()).toBe("Doe");
126
+ expect(records[3].ast?.toString()).toBe("John Doe");
127
+ });
128
+
129
+ test("Records Some Error Some Matches", () => {
130
+ const { ast, cursor } = name.exec("John Smith", true);
131
+ const records = cursor.records;
132
+
133
+ expect(ast?.toString()).toBe("John Smith");
134
+ expect(records[0].ast?.toString()).toBe("John");
135
+ expect(records[1].ast?.toString()).toBe(" ");
136
+ expect(records[2].error?.pattern.name).toBe("doe");
137
+ expect(records[2].error?.startIndex).toBe(5);
138
+ expect(records[2].error?.endIndex).toBe(5);
139
+ expect(records[3].ast?.toString()).toBe("Smith");
140
+ expect(records[4].ast?.toString()).toBe("John Smith");
141
+ });
142
+
143
+ test("Records All Errors", () => {
144
+ const { ast, cursor } = name.exec("Jack Smith", true);
145
+ const records = cursor.records;
146
+
147
+ expect(ast).toBeNull();
148
+ expect(records[0].error).not.toBeNull();
149
+ expect(records[0].pattern.name).toBe("john");
150
+ expect(records[1].error).not.toBeNull();
151
+ expect(records[1].pattern.name).toBe("jane");
152
+ expect(records[2].error).not.toBeNull();
153
+ expect(records[2].pattern.name).toBe("first-names");
154
+ });
155
+
102
156
  });
@@ -57,6 +57,10 @@ export class Cursor {
57
57
  return this._history.errors;
58
58
  }
59
59
 
60
+ get records() {
61
+ return this._history.records;
62
+ }
63
+
60
64
  get index(): number {
61
65
  return this._index;
62
66
  }
@@ -7,6 +7,12 @@ export interface Match {
7
7
  node: Node | null;
8
8
  }
9
9
 
10
+ export interface HistoryRecord {
11
+ pattern: Pattern;
12
+ error: ParseError | null;
13
+ ast: Node | null;
14
+ }
15
+
10
16
  export class CursorHistory {
11
17
  private _isRecording = false;
12
18
  private _leafMatches: Match[] = [{ pattern: null, node: null }];
@@ -16,6 +22,7 @@ export class CursorHistory {
16
22
  private _patterns: Pattern[] = [];
17
23
  private _nodes: Node[] = [];
18
24
  private _errors: ParseError[] = [];
25
+ private _records: HistoryRecord[] = [];
19
26
 
20
27
  get isRecording(): boolean {
21
28
  return this._isRecording;
@@ -45,6 +52,10 @@ export class CursorHistory {
45
52
  return this._currentError;
46
53
  }
47
54
 
55
+ get records(): HistoryRecord[] {
56
+ return this._records;
57
+ }
58
+
48
59
  get nodes(): Node[] {
49
60
  return this._nodes;
50
61
  }
@@ -57,6 +68,11 @@ export class CursorHistory {
57
68
  if (this._isRecording) {
58
69
  this._patterns.push(pattern);
59
70
  this._nodes.push(node);
71
+ this._records.push({
72
+ pattern,
73
+ ast: node,
74
+ error: null
75
+ });
60
76
  }
61
77
 
62
78
  this._rootMatch.pattern = pattern;
@@ -106,6 +122,11 @@ export class CursorHistory {
106
122
 
107
123
  if (this._isRecording) {
108
124
  this._errors.push(error);
125
+ this.records.push({
126
+ pattern,
127
+ ast: null,
128
+ error
129
+ });
109
130
  }
110
131
  }
111
132