clarity-pattern-parser 11.2.2 → 11.3.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.
- package/TODO.md +1 -3
- package/dist/ast/Node.d.ts +3 -0
- package/dist/index.browser.js +34 -18
- package/dist/index.browser.js.map +1 -1
- package/dist/index.esm.js +34 -18
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +34 -18
- package/dist/index.js.map +1 -1
- package/dist/patterns/Cursor.d.ts +2 -2
- package/dist/patterns/CursorHistory.d.ts +2 -6
- package/dist/patterns/HistoryRecord.d.ts +8 -0
- package/package.json +1 -1
- package/src/ast/Node.test.ts +5 -2
- package/src/ast/Node.ts +20 -3
- package/src/patterns/Cursor.ts +2 -2
- package/src/patterns/CursorHistory.ts +19 -20
- package/src/patterns/FiniteRepeat.test.ts +12 -12
- package/src/patterns/HistoryRecord.ts +9 -0
- package/src/patterns/InfiniteRepeat.test.ts +6 -6
- package/src/patterns/Literal.test.ts +3 -3
- package/src/patterns/Not.test.ts +1 -1
- package/src/patterns/Options.test.ts +3 -3
- package/src/patterns/PrecedenceTree.test.ts +9 -9
- package/src/patterns/Reference.test.ts +1 -1
- package/src/patterns/Regex.test.ts +1 -1
- package/src/patterns/Repeat.test.ts +8 -8
- package/src/patterns/Sequence.test.ts +6 -6
- package/src/patterns/TakeUntil.ts +5 -2
|
@@ -19,7 +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("./
|
|
22
|
+
get records(): import("./HistoryRecord").HistoryRecord[];
|
|
23
23
|
get index(): number;
|
|
24
24
|
get length(): number;
|
|
25
25
|
get hasError(): boolean;
|
|
@@ -35,7 +35,7 @@ export declare class Cursor {
|
|
|
35
35
|
getLastIndex(): number;
|
|
36
36
|
getChars(first: number, last: number): string;
|
|
37
37
|
recordMatch(pattern: Pattern, node: Node): void;
|
|
38
|
-
recordErrorAt(startIndex: number,
|
|
38
|
+
recordErrorAt(startIndex: number, lastIndex: number, onPattern: Pattern): void;
|
|
39
39
|
resolveError(): void;
|
|
40
40
|
startRecording(): void;
|
|
41
41
|
stopRecording(): void;
|
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
import { Node } from "../ast/Node";
|
|
2
2
|
import { ParseError } from "./ParseError";
|
|
3
3
|
import { Pattern } from "./Pattern";
|
|
4
|
+
import { HistoryRecord } from "./HistoryRecord";
|
|
4
5
|
export interface Match {
|
|
5
6
|
pattern: Pattern | null;
|
|
6
7
|
node: Node | null;
|
|
7
8
|
}
|
|
8
|
-
export interface HistoryRecord {
|
|
9
|
-
pattern: Pattern;
|
|
10
|
-
error: ParseError | null;
|
|
11
|
-
ast: Node | null;
|
|
12
|
-
}
|
|
13
9
|
export declare class CursorHistory {
|
|
14
10
|
private _isRecording;
|
|
15
11
|
private _leafMatches;
|
|
@@ -32,7 +28,7 @@ export declare class CursorHistory {
|
|
|
32
28
|
get patterns(): Pattern[];
|
|
33
29
|
recordMatch(pattern: Pattern, node: Node): void;
|
|
34
30
|
recordErrorAt(startIndex: number, lastIndex: number, pattern: Pattern): void;
|
|
31
|
+
resolveError(): void;
|
|
35
32
|
startRecording(): void;
|
|
36
33
|
stopRecording(): void;
|
|
37
|
-
resolveError(): void;
|
|
38
34
|
}
|
package/package.json
CHANGED
package/src/ast/Node.test.ts
CHANGED
|
@@ -295,7 +295,7 @@ describe("Node", () => {
|
|
|
295
295
|
const parent = new Node("parent", "parent", 0, 0, [a, b]);
|
|
296
296
|
const clone = parent.clone();
|
|
297
297
|
|
|
298
|
-
expect(clone
|
|
298
|
+
expect(clone.isEqual(parent)).toBeTruthy();
|
|
299
299
|
});
|
|
300
300
|
|
|
301
301
|
test("Turn Into JSON", () => {
|
|
@@ -304,12 +304,14 @@ describe("Node", () => {
|
|
|
304
304
|
const parent = new Node("parent", "parent", 0, 0, [a, b]);
|
|
305
305
|
const result = parent.toJson();
|
|
306
306
|
const expected = JSON.stringify({
|
|
307
|
+
id: parent.id,
|
|
307
308
|
type: "parent",
|
|
308
309
|
name: "parent",
|
|
309
310
|
value: "AB",
|
|
310
311
|
startIndex: 0,
|
|
311
312
|
endIndex: 1,
|
|
312
313
|
children: [{
|
|
314
|
+
id: parent.children[0].id,
|
|
313
315
|
type: "a",
|
|
314
316
|
name: "a",
|
|
315
317
|
value: "A",
|
|
@@ -317,6 +319,7 @@ describe("Node", () => {
|
|
|
317
319
|
endIndex: 1,
|
|
318
320
|
children: [],
|
|
319
321
|
}, {
|
|
322
|
+
id: parent.children[1].id,
|
|
320
323
|
type: "b",
|
|
321
324
|
name: "b",
|
|
322
325
|
value: "B",
|
|
@@ -471,7 +474,7 @@ describe("Node", () => {
|
|
|
471
474
|
Node.createValueNode("family", "aunt", "aunt")
|
|
472
475
|
]);
|
|
473
476
|
|
|
474
|
-
expect(result.
|
|
477
|
+
expect(result.isEqual(expected)).toBeTruthy();
|
|
475
478
|
});
|
|
476
479
|
|
|
477
480
|
});
|
package/src/ast/Node.ts
CHANGED
|
@@ -2,7 +2,10 @@ function defaultVisitor(node: Node) {
|
|
|
2
2
|
return node;
|
|
3
3
|
}
|
|
4
4
|
|
|
5
|
+
let idIndex = 0;
|
|
6
|
+
|
|
5
7
|
export interface CycleFreeNode {
|
|
8
|
+
id: string;
|
|
6
9
|
type: string;
|
|
7
10
|
name: string;
|
|
8
11
|
startIndex: number;
|
|
@@ -12,6 +15,7 @@ export interface CycleFreeNode {
|
|
|
12
15
|
}
|
|
13
16
|
|
|
14
17
|
export class Node {
|
|
18
|
+
private _id: string;
|
|
15
19
|
private _type: string;
|
|
16
20
|
private _name: string;
|
|
17
21
|
private _firstIndex: number;
|
|
@@ -20,6 +24,10 @@ export class Node {
|
|
|
20
24
|
private _children: Node[];
|
|
21
25
|
private _value: string;
|
|
22
26
|
|
|
27
|
+
get id() {
|
|
28
|
+
return this._id;
|
|
29
|
+
}
|
|
30
|
+
|
|
23
31
|
get type() {
|
|
24
32
|
return this._type;
|
|
25
33
|
}
|
|
@@ -72,6 +80,7 @@ export class Node {
|
|
|
72
80
|
children: Node[] = [],
|
|
73
81
|
value = ""
|
|
74
82
|
) {
|
|
83
|
+
this._id = String(idIndex++);
|
|
75
84
|
this._type = type;
|
|
76
85
|
this._name = name;
|
|
77
86
|
this._firstIndex = firstIndex;
|
|
@@ -280,7 +289,7 @@ export class Node {
|
|
|
280
289
|
}
|
|
281
290
|
|
|
282
291
|
clone(): Node {
|
|
283
|
-
|
|
292
|
+
const node = new Node(
|
|
284
293
|
this._type,
|
|
285
294
|
this._name,
|
|
286
295
|
this._firstIndex,
|
|
@@ -288,6 +297,8 @@ export class Node {
|
|
|
288
297
|
this._children.map((c) => c.clone()),
|
|
289
298
|
this._value
|
|
290
299
|
);
|
|
300
|
+
|
|
301
|
+
return node;
|
|
291
302
|
}
|
|
292
303
|
|
|
293
304
|
normalize(startIndex = this._firstIndex): number {
|
|
@@ -320,6 +331,7 @@ export class Node {
|
|
|
320
331
|
|
|
321
332
|
toCycleFreeObject(): CycleFreeNode {
|
|
322
333
|
return {
|
|
334
|
+
id: this._id,
|
|
323
335
|
type: this._type,
|
|
324
336
|
name: this._name,
|
|
325
337
|
value: this.toString(),
|
|
@@ -333,8 +345,13 @@ export class Node {
|
|
|
333
345
|
return JSON.stringify(this.toCycleFreeObject(), null, space);
|
|
334
346
|
}
|
|
335
347
|
|
|
336
|
-
isEqual(node: Node) {
|
|
337
|
-
return node.
|
|
348
|
+
isEqual(node: Node): boolean {
|
|
349
|
+
return node._type === this._type &&
|
|
350
|
+
node._name === this._name &&
|
|
351
|
+
node._firstIndex === this._firstIndex &&
|
|
352
|
+
node._lastIndex === this._lastIndex &&
|
|
353
|
+
node._value === this._value &&
|
|
354
|
+
this._children.every((child, index) => child.isEqual(node._children[index]));
|
|
338
355
|
}
|
|
339
356
|
|
|
340
357
|
static createValueNode(type: string, name: string, value = "") {
|
package/src/patterns/Cursor.ts
CHANGED
|
@@ -130,8 +130,8 @@ export class Cursor {
|
|
|
130
130
|
this._history.recordMatch(pattern, node);
|
|
131
131
|
}
|
|
132
132
|
|
|
133
|
-
recordErrorAt(startIndex: number,
|
|
134
|
-
this._history.recordErrorAt(startIndex,
|
|
133
|
+
recordErrorAt(startIndex: number, lastIndex: number, onPattern: Pattern): void {
|
|
134
|
+
this._history.recordErrorAt(startIndex, lastIndex, onPattern);
|
|
135
135
|
}
|
|
136
136
|
|
|
137
137
|
resolveError(): void {
|
|
@@ -1,18 +1,13 @@
|
|
|
1
1
|
import { Node } from "../ast/Node";
|
|
2
2
|
import { ParseError } from "./ParseError";
|
|
3
3
|
import { Pattern } from "./Pattern";
|
|
4
|
+
import { HistoryRecord } from "./HistoryRecord";
|
|
4
5
|
|
|
5
6
|
export interface Match {
|
|
6
7
|
pattern: Pattern | null;
|
|
7
8
|
node: Node | null;
|
|
8
9
|
}
|
|
9
10
|
|
|
10
|
-
export interface HistoryRecord {
|
|
11
|
-
pattern: Pattern;
|
|
12
|
-
error: ParseError | null;
|
|
13
|
-
ast: Node | null;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
11
|
export class CursorHistory {
|
|
17
12
|
private _isRecording = false;
|
|
18
13
|
private _leafMatches: Match[] = [{ pattern: null, node: null }];
|
|
@@ -65,14 +60,16 @@ export class CursorHistory {
|
|
|
65
60
|
}
|
|
66
61
|
|
|
67
62
|
recordMatch(pattern: Pattern, node: Node): void {
|
|
63
|
+
const record: HistoryRecord = {
|
|
64
|
+
pattern,
|
|
65
|
+
ast: node,
|
|
66
|
+
error: null
|
|
67
|
+
};
|
|
68
|
+
|
|
68
69
|
if (this._isRecording) {
|
|
69
70
|
this._patterns.push(pattern);
|
|
70
71
|
this._nodes.push(node);
|
|
71
|
-
this._records.push(
|
|
72
|
-
pattern,
|
|
73
|
-
ast: node,
|
|
74
|
-
error: null
|
|
75
|
-
});
|
|
72
|
+
this._records.push(record);
|
|
76
73
|
}
|
|
77
74
|
|
|
78
75
|
this._rootMatch.pattern = pattern;
|
|
@@ -114,6 +111,12 @@ export class CursorHistory {
|
|
|
114
111
|
|
|
115
112
|
recordErrorAt(startIndex: number, lastIndex: number, pattern: Pattern): void {
|
|
116
113
|
const error = new ParseError(startIndex, lastIndex, pattern);
|
|
114
|
+
const record: HistoryRecord = {
|
|
115
|
+
pattern,
|
|
116
|
+
ast: null,
|
|
117
|
+
error
|
|
118
|
+
};
|
|
119
|
+
|
|
117
120
|
this._currentError = error;
|
|
118
121
|
|
|
119
122
|
if (this._furthestError === null || lastIndex > this._furthestError.lastIndex) {
|
|
@@ -122,14 +125,14 @@ export class CursorHistory {
|
|
|
122
125
|
|
|
123
126
|
if (this._isRecording) {
|
|
124
127
|
this._errors.push(error);
|
|
125
|
-
this.records.push(
|
|
126
|
-
pattern,
|
|
127
|
-
ast: null,
|
|
128
|
-
error
|
|
129
|
-
});
|
|
128
|
+
this.records.push(record);
|
|
130
129
|
}
|
|
131
130
|
}
|
|
132
131
|
|
|
132
|
+
resolveError() {
|
|
133
|
+
this._currentError = null;
|
|
134
|
+
}
|
|
135
|
+
|
|
133
136
|
startRecording(): void {
|
|
134
137
|
this._isRecording = true;
|
|
135
138
|
}
|
|
@@ -138,8 +141,4 @@ export class CursorHistory {
|
|
|
138
141
|
this._isRecording = false;
|
|
139
142
|
}
|
|
140
143
|
|
|
141
|
-
resolveError() {
|
|
142
|
-
this._currentError = null;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
144
|
}
|
|
@@ -26,7 +26,7 @@ describe("BoundedRepeat", () => {
|
|
|
26
26
|
new Node("regex", "number", 1, 1, [], "2"),
|
|
27
27
|
]);
|
|
28
28
|
|
|
29
|
-
expect(result).
|
|
29
|
+
expect(result?.isEqual(expected)).toBeTruthy();
|
|
30
30
|
expect(cursor.hasError).toBeFalsy();
|
|
31
31
|
|
|
32
32
|
cursor = new Cursor("123");
|
|
@@ -37,7 +37,7 @@ describe("BoundedRepeat", () => {
|
|
|
37
37
|
new Node("regex", "number", 2, 2, [], "3"),
|
|
38
38
|
]);
|
|
39
39
|
|
|
40
|
-
expect(result).
|
|
40
|
+
expect(result?.isEqual(expected)).toBeTruthy();
|
|
41
41
|
expect(cursor.hasError).toBeFalsy();
|
|
42
42
|
|
|
43
43
|
cursor = new Cursor("1234");
|
|
@@ -48,7 +48,7 @@ describe("BoundedRepeat", () => {
|
|
|
48
48
|
new Node("regex", "number", 2, 2, [], "3"),
|
|
49
49
|
]);
|
|
50
50
|
|
|
51
|
-
expect(result).
|
|
51
|
+
expect(result?.isEqual(expected)).toBeTruthy();
|
|
52
52
|
expect(cursor.hasError).toBeFalsy();
|
|
53
53
|
expect(cursor.index).toBe(2);
|
|
54
54
|
|
|
@@ -59,7 +59,7 @@ describe("BoundedRepeat", () => {
|
|
|
59
59
|
new Node("regex", "number", 1, 1, [], "2"),
|
|
60
60
|
]);
|
|
61
61
|
|
|
62
|
-
expect(result).
|
|
62
|
+
expect(result?.isEqual(expected)).toBeTruthy();
|
|
63
63
|
expect(cursor.hasError).toBeFalsy();
|
|
64
64
|
});
|
|
65
65
|
|
|
@@ -87,7 +87,7 @@ describe("BoundedRepeat", () => {
|
|
|
87
87
|
new Node("regex", "number", 2, 2, [], "3"),
|
|
88
88
|
]);
|
|
89
89
|
|
|
90
|
-
expect(result).
|
|
90
|
+
expect(result?.isEqual(expected)).toBeTruthy();
|
|
91
91
|
expect(cursor.hasError).toBeFalsy();
|
|
92
92
|
|
|
93
93
|
cursor = new Cursor("1234");
|
|
@@ -98,7 +98,7 @@ describe("BoundedRepeat", () => {
|
|
|
98
98
|
new Node("regex", "number", 2, 2, [], "3"),
|
|
99
99
|
]);
|
|
100
100
|
|
|
101
|
-
expect(result).
|
|
101
|
+
expect(result?.isEqual(expected)).toBeTruthy();
|
|
102
102
|
expect(cursor.hasError).toBeFalsy();
|
|
103
103
|
expect(cursor.index).toBe(2);
|
|
104
104
|
|
|
@@ -122,7 +122,7 @@ describe("BoundedRepeat", () => {
|
|
|
122
122
|
new Node("regex", "number", 2, 2, [], "2"),
|
|
123
123
|
]);
|
|
124
124
|
|
|
125
|
-
expect(result).
|
|
125
|
+
expect(result?.isEqual(expected)).toBeTruthy();
|
|
126
126
|
expect(cursor.hasError).toBeFalsy();
|
|
127
127
|
|
|
128
128
|
cursor = new Cursor("1,2,3,");
|
|
@@ -135,7 +135,7 @@ describe("BoundedRepeat", () => {
|
|
|
135
135
|
new Node("regex", "number", 4, 4, [], "3"),
|
|
136
136
|
]);
|
|
137
137
|
|
|
138
|
-
expect(result).
|
|
138
|
+
expect(result?.isEqual(expected)).toBeTruthy();
|
|
139
139
|
expect(cursor.hasError).toBeFalsy();
|
|
140
140
|
|
|
141
141
|
cursor = new Cursor("1,2,3,4");
|
|
@@ -148,7 +148,7 @@ describe("BoundedRepeat", () => {
|
|
|
148
148
|
new Node("regex", "number", 4, 4, [], "3"),
|
|
149
149
|
]);
|
|
150
150
|
|
|
151
|
-
expect(result).
|
|
151
|
+
expect(result?.isEqual(expected)).toBeTruthy();
|
|
152
152
|
expect(cursor.hasError).toBeFalsy();
|
|
153
153
|
});
|
|
154
154
|
|
|
@@ -171,7 +171,7 @@ describe("BoundedRepeat", () => {
|
|
|
171
171
|
new Node("regex", "divider", 4, 4, [], "\n"),
|
|
172
172
|
]);
|
|
173
173
|
|
|
174
|
-
expect(result).
|
|
174
|
+
expect(result?.isEqual(expected)).toBeTruthy();
|
|
175
175
|
expect(cursor.hasError).toBeFalsy();
|
|
176
176
|
});
|
|
177
177
|
|
|
@@ -202,7 +202,7 @@ describe("BoundedRepeat", () => {
|
|
|
202
202
|
new Node("regex", "number", 4, 4, [], "3"),
|
|
203
203
|
]);
|
|
204
204
|
|
|
205
|
-
expect(result).
|
|
205
|
+
expect(result?.isEqual(expected)).toBeTruthy();
|
|
206
206
|
expect(cursor.hasError).toBeFalsy();
|
|
207
207
|
|
|
208
208
|
cursor = new Cursor("1,2,3,4");
|
|
@@ -215,7 +215,7 @@ describe("BoundedRepeat", () => {
|
|
|
215
215
|
new Node("regex", "number", 4, 4, [], "3"),
|
|
216
216
|
]);
|
|
217
217
|
|
|
218
|
-
expect(result).
|
|
218
|
+
expect(result?.isEqual(expected)).toBeTruthy();
|
|
219
219
|
expect(cursor.hasError).toBeFalsy();
|
|
220
220
|
});
|
|
221
221
|
|
|
@@ -20,7 +20,7 @@ describe("InfiniteRepeat", () => {
|
|
|
20
20
|
new Node("regex", "digit", 2, 2, [], "7"),
|
|
21
21
|
]);
|
|
22
22
|
|
|
23
|
-
expect(result).
|
|
23
|
+
expect(result?.isEqual(expected)).toBeTruthy();
|
|
24
24
|
expect(cursor.hasError).toBeFalsy();
|
|
25
25
|
});
|
|
26
26
|
|
|
@@ -32,7 +32,7 @@ describe("InfiniteRepeat", () => {
|
|
|
32
32
|
let result = integer.parse(cursor);
|
|
33
33
|
let expected: Node | null = null;
|
|
34
34
|
|
|
35
|
-
expect(result).
|
|
35
|
+
expect(result).toBeNull();
|
|
36
36
|
expect(cursor.hasError).toBeTruthy();
|
|
37
37
|
|
|
38
38
|
cursor = new Cursor("33");
|
|
@@ -42,7 +42,7 @@ describe("InfiniteRepeat", () => {
|
|
|
42
42
|
new Node("regex", "digit", 1, 1, [], "3")
|
|
43
43
|
]);
|
|
44
44
|
|
|
45
|
-
expect(result).
|
|
45
|
+
expect(result?.isEqual(expected)).toBeTruthy();
|
|
46
46
|
expect(cursor.hasError).toBeFalsy();
|
|
47
47
|
});
|
|
48
48
|
|
|
@@ -70,7 +70,7 @@ describe("InfiniteRepeat", () => {
|
|
|
70
70
|
new Node("regex", "digit", 4, 4, [], "7"),
|
|
71
71
|
]);
|
|
72
72
|
|
|
73
|
-
expect(result).
|
|
73
|
+
expect(result?.isEqual(expected)).toBeTruthy();
|
|
74
74
|
expect(cursor.hasError).toBeFalsy();
|
|
75
75
|
});
|
|
76
76
|
|
|
@@ -88,7 +88,7 @@ describe("InfiniteRepeat", () => {
|
|
|
88
88
|
new Node("regex", "digit", 4, 4, [], "7"),
|
|
89
89
|
]);
|
|
90
90
|
|
|
91
|
-
expect(result).
|
|
91
|
+
expect(result?.isEqual(expected)).toBeTruthy();
|
|
92
92
|
expect(cursor.hasError).toBeFalsy();
|
|
93
93
|
});
|
|
94
94
|
|
|
@@ -106,7 +106,7 @@ describe("InfiniteRepeat", () => {
|
|
|
106
106
|
new Node("regex", "digit", 4, 4, [], "7"),
|
|
107
107
|
]);
|
|
108
108
|
|
|
109
|
-
expect(result).
|
|
109
|
+
expect(result?.isEqual(expected)).toBeTruthy();
|
|
110
110
|
expect(cursor.hasError).toBeFalsy();
|
|
111
111
|
});
|
|
112
112
|
|
|
@@ -17,12 +17,12 @@ describe("Literal", () => {
|
|
|
17
17
|
const result = literal.parse(cursor);
|
|
18
18
|
const expected = new Node("literal", "greeting", 0, 11, [], "Hello World!");
|
|
19
19
|
|
|
20
|
-
expect(result).
|
|
20
|
+
expect(result?.isEqual(expected)).toBeTruthy();
|
|
21
21
|
expect(cursor.index).toBe(11);
|
|
22
22
|
expect(cursor.error).toBeNull();
|
|
23
|
-
expect(cursor.leafMatch.node).
|
|
23
|
+
expect(cursor.leafMatch.node?.isEqual(expected)).toBeTruthy();
|
|
24
24
|
expect(cursor.leafMatch.pattern).toBe(literal);
|
|
25
|
-
expect(cursor.rootMatch.node).
|
|
25
|
+
expect(cursor.rootMatch.node?.isEqual(expected)).toBeTruthy();
|
|
26
26
|
expect(cursor.rootMatch.pattern).toBe(literal);
|
|
27
27
|
});
|
|
28
28
|
|
package/src/patterns/Not.test.ts
CHANGED
|
@@ -22,7 +22,7 @@ describe("Options", () => {
|
|
|
22
22
|
const result = a.parse(cursor);
|
|
23
23
|
const expected = new Node("literal", "a", 0, 0, [], "A")
|
|
24
24
|
|
|
25
|
-
expect(result).
|
|
25
|
+
expect(result?.isEqual(expected)).toBeTruthy();
|
|
26
26
|
});
|
|
27
27
|
|
|
28
28
|
test("One Option Failed", () => {
|
|
@@ -39,9 +39,9 @@ describe("Options", () => {
|
|
|
39
39
|
const a = new Options("a-b", [new Literal("a", "A"), new Literal("b", "B")]);
|
|
40
40
|
const cursor = new Cursor("AB");
|
|
41
41
|
let result = a.parse(cursor);
|
|
42
|
-
let expected = new Node("literal", "a", 0, 0, [], "A")
|
|
42
|
+
let expected = new Node("literal", "a", 0, 0, [], "A");
|
|
43
43
|
|
|
44
|
-
expect(result).
|
|
44
|
+
expect(result?.isEqual(expected)).toBeTruthy();
|
|
45
45
|
|
|
46
46
|
cursor.next();
|
|
47
47
|
|
|
@@ -40,7 +40,7 @@ describe("Precedence Tree", () => {
|
|
|
40
40
|
|
|
41
41
|
|
|
42
42
|
expect(result?.toString()).toBe("a+b*c||d+e");
|
|
43
|
-
expect(result?.
|
|
43
|
+
expect(result?.isEqual(expected));
|
|
44
44
|
});
|
|
45
45
|
|
|
46
46
|
test("add Prefix", () => {
|
|
@@ -64,7 +64,7 @@ describe("Precedence Tree", () => {
|
|
|
64
64
|
]);
|
|
65
65
|
|
|
66
66
|
expect(result?.toString()).toBe("!+++a");
|
|
67
|
-
expect(result?.
|
|
67
|
+
expect(result?.isEqual(expected));
|
|
68
68
|
});
|
|
69
69
|
|
|
70
70
|
test("add Postfix", () => {
|
|
@@ -84,7 +84,7 @@ describe("Precedence Tree", () => {
|
|
|
84
84
|
|
|
85
85
|
const result = tree.commit();
|
|
86
86
|
expect(result?.toString()).toBe("a--++");
|
|
87
|
-
expect(result?.
|
|
87
|
+
expect(result?.isEqual(expected));
|
|
88
88
|
});
|
|
89
89
|
|
|
90
90
|
test("all", () => {
|
|
@@ -120,7 +120,7 @@ describe("Precedence Tree", () => {
|
|
|
120
120
|
]);
|
|
121
121
|
|
|
122
122
|
expect(result?.toString()).toBe("!a++*b+c");
|
|
123
|
-
expect(result?.
|
|
123
|
+
expect(result?.isEqual(expected));
|
|
124
124
|
});
|
|
125
125
|
|
|
126
126
|
test("Incomplete", () => {
|
|
@@ -157,7 +157,7 @@ describe("Precedence Tree", () => {
|
|
|
157
157
|
]);
|
|
158
158
|
|
|
159
159
|
expect(result?.toString()).toBe("!a++*b+c");
|
|
160
|
-
expect(result?.
|
|
160
|
+
expect(result?.isEqual(expected));
|
|
161
161
|
});
|
|
162
162
|
|
|
163
163
|
test("add Partial Binary With Lower Precedence", () => {
|
|
@@ -187,7 +187,7 @@ describe("Precedence Tree", () => {
|
|
|
187
187
|
|
|
188
188
|
|
|
189
189
|
expect(result?.toString()).toBe("a+b*c");
|
|
190
|
-
expect(result?.
|
|
190
|
+
expect(result?.isEqual(expected));
|
|
191
191
|
});
|
|
192
192
|
|
|
193
193
|
test("add Partial Binary With Equal Precedence", () => {
|
|
@@ -216,7 +216,7 @@ describe("Precedence Tree", () => {
|
|
|
216
216
|
]);
|
|
217
217
|
|
|
218
218
|
expect(result?.toString()).toBe("a+b*c");
|
|
219
|
-
expect(result?.
|
|
219
|
+
expect(result?.isEqual(expected));
|
|
220
220
|
});
|
|
221
221
|
|
|
222
222
|
test("add Partial Binary With Equal Precedence And Right Associated", () => {
|
|
@@ -245,7 +245,7 @@ describe("Precedence Tree", () => {
|
|
|
245
245
|
]);
|
|
246
246
|
|
|
247
247
|
expect(result?.toString()).toBe("a+b*c");
|
|
248
|
-
expect(result?.
|
|
248
|
+
expect(result?.isEqual(expected));
|
|
249
249
|
});
|
|
250
250
|
|
|
251
251
|
test("mul Partial Binary With Greater Precedence", () => {
|
|
@@ -269,6 +269,6 @@ describe("Precedence Tree", () => {
|
|
|
269
269
|
]);
|
|
270
270
|
|
|
271
271
|
expect(result?.toString()).toBe("a+b");
|
|
272
|
-
expect(result?.
|
|
272
|
+
expect(result?.isEqual(expected));
|
|
273
273
|
});
|
|
274
274
|
});
|
|
@@ -17,7 +17,7 @@ describe("Repeat", () => {
|
|
|
17
17
|
let result = finiteRepeat.parse(cursor);
|
|
18
18
|
let expected: Node | null = null;
|
|
19
19
|
|
|
20
|
-
expect(result).
|
|
20
|
+
expect(result).toBeNull();
|
|
21
21
|
expect(cursor.hasError).toBeTruthy();
|
|
22
22
|
|
|
23
23
|
cursor = new Cursor("1");
|
|
@@ -26,7 +26,7 @@ describe("Repeat", () => {
|
|
|
26
26
|
new Node("regex", "number", 0, 0, [], "1")
|
|
27
27
|
]);
|
|
28
28
|
|
|
29
|
-
expect(result?.
|
|
29
|
+
expect(result?.isEqual(expected));
|
|
30
30
|
expect(cursor.hasError).toBeFalsy();
|
|
31
31
|
|
|
32
32
|
cursor = new Cursor("12");
|
|
@@ -36,7 +36,7 @@ describe("Repeat", () => {
|
|
|
36
36
|
new Node("regex", "number", 1, 1, [], "2")
|
|
37
37
|
]);
|
|
38
38
|
|
|
39
|
-
expect(result?.
|
|
39
|
+
expect(result?.isEqual(expected));
|
|
40
40
|
expect(cursor.hasError).toBeFalsy();
|
|
41
41
|
|
|
42
42
|
cursor = new Cursor("123");
|
|
@@ -46,7 +46,7 @@ describe("Repeat", () => {
|
|
|
46
46
|
new Node("regex", "number", 1, 1, [], "2")
|
|
47
47
|
]);
|
|
48
48
|
|
|
49
|
-
expect(result?.
|
|
49
|
+
expect(result?.isEqual(expected));
|
|
50
50
|
expect(cursor.hasError).toBeFalsy();
|
|
51
51
|
expect(cursor.index).toBe(1);
|
|
52
52
|
});
|
|
@@ -59,14 +59,14 @@ describe("Repeat", () => {
|
|
|
59
59
|
let result = finiteRepeat.parse(cursor);
|
|
60
60
|
let expected: Node | null = null;
|
|
61
61
|
|
|
62
|
-
expect(result).
|
|
62
|
+
expect(result).toBeNull();
|
|
63
63
|
expect(cursor.hasError).toBeTruthy();
|
|
64
64
|
|
|
65
65
|
cursor = new Cursor("1");
|
|
66
66
|
result = finiteRepeat.parse(cursor);
|
|
67
67
|
expected = null;
|
|
68
68
|
|
|
69
|
-
expect(result).
|
|
69
|
+
expect(result).toBeNull();
|
|
70
70
|
expect(cursor.hasError).toBeTruthy();
|
|
71
71
|
|
|
72
72
|
cursor = new Cursor("12");
|
|
@@ -76,7 +76,7 @@ describe("Repeat", () => {
|
|
|
76
76
|
new Node("regex", "number", 1, 1, [], "2")
|
|
77
77
|
]);
|
|
78
78
|
|
|
79
|
-
expect(result?.
|
|
79
|
+
expect(result?.isEqual(expected));
|
|
80
80
|
expect(cursor.hasError).toBeFalsy();
|
|
81
81
|
|
|
82
82
|
cursor = new Cursor("123");
|
|
@@ -86,7 +86,7 @@ describe("Repeat", () => {
|
|
|
86
86
|
new Node("regex", "number", 1, 1, [], "2")
|
|
87
87
|
]);
|
|
88
88
|
|
|
89
|
-
expect(result?.
|
|
89
|
+
expect(result?.isEqual(expected));
|
|
90
90
|
expect(cursor.hasError).toBeFalsy();
|
|
91
91
|
expect(cursor.index).toBe(1);
|
|
92
92
|
});
|