clarity-pattern-parser 4.0.3 → 6.0.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/README.md +466 -1
- package/TODO.md +76 -2
- package/dist/ast/Node.d.ts +49 -11
- package/dist/ast/Visitor.d.ts +31 -31
- package/dist/index.browser.js +1513 -1495
- package/dist/index.browser.js.map +1 -1
- package/dist/index.d.ts +17 -17
- package/dist/index.esm.js +1480 -1459
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +1481 -1463
- package/dist/index.js.map +1 -1
- package/dist/intellisense/AutoComplete.d.ts +28 -0
- package/dist/intellisense/Suggestion.d.ts +11 -0
- package/dist/intellisense/SuggestionOption.d.ts +4 -0
- package/dist/patterns/And.d.ts +37 -24
- package/dist/patterns/Cursor.d.ts +37 -0
- package/dist/patterns/CursorHistory.d.ts +30 -0
- package/dist/patterns/Literal.d.ts +35 -19
- package/dist/patterns/Not.d.ts +29 -11
- package/dist/patterns/Or.d.ts +33 -22
- package/dist/patterns/ParseError.d.ts +6 -8
- package/dist/patterns/ParseResult.d.ts +6 -0
- package/dist/patterns/Pattern.d.ts +20 -26
- package/dist/patterns/Reference.d.ts +34 -12
- package/dist/patterns/Regex.d.ts +41 -21
- package/dist/patterns/Repeat.d.ts +38 -20
- package/dist/patterns/clonePatterns.d.ts +2 -0
- package/dist/patterns/filterOutNull.d.ts +2 -0
- package/dist/patterns/findPattern.d.ts +2 -0
- package/dist/patterns/getNextPattern.d.ts +2 -0
- package/jest.config.js +2 -1
- package/package.json +4 -5
- package/rollup.config.js +1 -1
- package/src/ast/Node.test.ts +364 -0
- package/src/ast/Node.ts +237 -23
- package/src/index.ts +25 -27
- package/src/intellisense/AutoComplete.test.ts +150 -0
- package/src/intellisense/AutoComplete.ts +200 -0
- package/src/intellisense/Suggestion.ts +12 -0
- package/src/intellisense/SuggestionOption.ts +4 -0
- package/src/{tests/cssPatterns → intellisense/css}/cssValue.ts +1 -1
- package/src/{tests/cssPatterns → intellisense/css}/divider.ts +2 -1
- package/src/intellisense/css/hex.ts +6 -0
- package/src/{tests/cssPatterns → intellisense/css}/method.ts +8 -9
- package/src/intellisense/css/name.ts +5 -0
- package/src/{tests/javascriptPatterns → intellisense/css}/number.ts +3 -3
- package/src/intellisense/css/spaces.ts +6 -0
- package/src/intellisense/css/unit.ts +10 -0
- package/src/{tests/cssPatterns → intellisense/css}/value.ts +1 -1
- package/src/{tests/cssPatterns → intellisense/css}/values.ts +1 -1
- package/src/intellisense/javascript/Javascript.test.ts +203 -0
- package/src/intellisense/javascript/arrayLiteral.ts +25 -0
- package/src/intellisense/javascript/deleteStatement.ts +14 -0
- package/src/intellisense/javascript/escapedCharacter.ts +49 -0
- package/src/intellisense/javascript/exponent.ts +24 -0
- package/src/intellisense/javascript/expression.ts +87 -0
- package/src/intellisense/javascript/expressionStatement.ts +29 -0
- package/src/intellisense/javascript/fraction.ts +11 -0
- package/src/intellisense/javascript/infixOperator.ts +36 -0
- package/src/intellisense/javascript/integer.ts +7 -0
- package/src/intellisense/javascript/invocation.ts +28 -0
- package/src/intellisense/javascript/literal.ts +14 -0
- package/src/intellisense/javascript/name.ts +3 -0
- package/src/intellisense/javascript/numberLiteral.ts +10 -0
- package/src/intellisense/javascript/objectLiteral.ts +30 -0
- package/src/intellisense/javascript/optionalSpaces.ts +3 -0
- package/src/intellisense/javascript/parameters.ts +20 -0
- package/src/intellisense/javascript/prefixOperator.ts +13 -0
- package/src/intellisense/javascript/propertyAccess.ts +23 -0
- package/src/intellisense/javascript/stringLiteral.ts +28 -0
- package/src/patterns/And.test.ts +310 -0
- package/src/patterns/And.ts +244 -119
- package/src/patterns/Cursor.test.ts +93 -0
- package/src/patterns/Cursor.ts +133 -0
- package/src/patterns/CursorHistory.test.ts +54 -0
- package/src/patterns/CursorHistory.ts +95 -0
- package/src/patterns/Literal.test.ts +166 -0
- package/src/patterns/Literal.ts +141 -62
- package/src/patterns/Not.test.ts +168 -0
- package/src/patterns/Not.ts +113 -32
- package/src/patterns/Or.test.ts +209 -0
- package/src/patterns/Or.ts +128 -97
- package/src/patterns/ParseError.ts +3 -7
- package/src/patterns/ParseResult.ts +7 -0
- package/src/patterns/Pattern.ts +21 -150
- package/src/patterns/Reference.test.ts +193 -0
- package/src/patterns/Reference.ts +114 -88
- package/src/patterns/Regex.test.ts +133 -0
- package/src/patterns/Regex.ts +117 -60
- package/src/patterns/Repeat.test.ts +218 -0
- package/src/patterns/Repeat.ts +220 -103
- package/src/patterns/clonePatterns.ts +5 -0
- package/src/patterns/filterOutNull.ts +13 -0
- package/src/patterns/findPattern.ts +25 -0
- package/src/Cursor.ts +0 -141
- package/src/CursorHistory.ts +0 -146
- package/src/TextSuggester.ts +0 -317
- package/src/ast/Visitor.ts +0 -271
- package/src/patterns/LookAhead.ts +0 -32
- package/src/patterns/Recursive.ts +0 -92
- package/src/tests/And.test.ts +0 -180
- package/src/tests/ComplexExamples.test.ts +0 -86
- package/src/tests/CssPatterns.test.ts +0 -90
- package/src/tests/CursorHistory.test.ts +0 -107
- package/src/tests/Cusor.test.ts +0 -174
- package/src/tests/HtmlPatterns.test.ts +0 -34
- package/src/tests/Literal.test.ts +0 -79
- package/src/tests/LookAhead.test.ts +0 -44
- package/src/tests/Not.test.ts +0 -51
- package/src/tests/Or.test.ts +0 -113
- package/src/tests/Pattern.test.ts +0 -290
- package/src/tests/Recursive.test.ts +0 -64
- package/src/tests/Reference.test.ts +0 -16
- package/src/tests/Repeat.test.ts +0 -75
- package/src/tests/SpeedTest.test.ts +0 -31
- package/src/tests/TextSuggester.test.ts +0 -297
- package/src/tests/Visitor.test.ts +0 -331
- package/src/tests/cssPatterns/hex.ts +0 -5
- package/src/tests/cssPatterns/name.ts +0 -5
- package/src/tests/cssPatterns/number.ts +0 -8
- package/src/tests/cssPatterns/spaces.ts +0 -5
- package/src/tests/cssPatterns/unit.ts +0 -8
- package/src/tests/htmlPatterns/element.ts +0 -49
- package/src/tests/javascriptPatterns/boolean.ts +0 -10
- package/src/tests/javascriptPatterns/json.ts +0 -67
- package/src/tests/javascriptPatterns/name.ts +0 -5
- package/src/tests/javascriptPatterns/objectLiteral.ts +0 -40
- package/src/tests/javascriptPatterns/string.ts +0 -84
- package/src/tests/javascriptPatterns/unit.ts +0 -8
- package/src/tests/javascriptPatterns/whitespace.ts +0 -44
- package/src/tests/naturalLanguage/filter.ts +0 -37
- package/src/tests/patterns/sentence.ts +0 -37
- /package/src/{tests/cssPatterns → intellisense/css}/optionalSpaces.ts +0 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { And } from "./And";
|
|
2
|
+
import { Cursor } from "./Cursor";
|
|
3
|
+
import { Literal } from "./Literal";
|
|
4
|
+
import { Not } from "./Not";
|
|
5
|
+
import { Pattern } from "./Pattern";
|
|
6
|
+
|
|
7
|
+
describe("Not", () => {
|
|
8
|
+
test("Parse Successfully", () => {
|
|
9
|
+
const a = new Literal("a", "A");
|
|
10
|
+
const notA = new Not("not-a", a);
|
|
11
|
+
const cursor = new Cursor("B");
|
|
12
|
+
const result = notA.parse(cursor);
|
|
13
|
+
|
|
14
|
+
expect(result).toBeNull();
|
|
15
|
+
expect(cursor.hasError).toBeFalsy();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("Parse Failed", () => {
|
|
19
|
+
const a = new Literal("a", "A");
|
|
20
|
+
const notA = new Not("not-a", a);
|
|
21
|
+
const cursor = new Cursor("A");
|
|
22
|
+
const result = notA.parse(cursor);
|
|
23
|
+
|
|
24
|
+
expect(result).toBeNull();
|
|
25
|
+
expect(cursor.hasError).toBeTruthy();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test("Clone", () => {
|
|
29
|
+
const a = new Literal("a", "A");
|
|
30
|
+
const notA = new Not("not-a", a);
|
|
31
|
+
const clone = notA.clone();
|
|
32
|
+
|
|
33
|
+
expect(clone.name).toBe("not-a");
|
|
34
|
+
expect(clone).not.toBe(notA)
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("Tokens", () => {
|
|
38
|
+
const a = new Literal("a", "A");
|
|
39
|
+
const notA = new Not("not-a", a);
|
|
40
|
+
const tokens = notA.getTokens();
|
|
41
|
+
const nextTokens = notA.getTokensAfter(new Literal("bogus", "bogus"))
|
|
42
|
+
const emptyArray: string[] = [];
|
|
43
|
+
|
|
44
|
+
expect(tokens).toEqual(emptyArray);
|
|
45
|
+
expect(nextTokens).toEqual(emptyArray);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("Properties", () => {
|
|
49
|
+
const a = new Literal("a", "A");
|
|
50
|
+
const notA = new Not("not-a", a);
|
|
51
|
+
|
|
52
|
+
expect(notA.type).toBe("not");
|
|
53
|
+
expect(notA.name).toBe("not-a");
|
|
54
|
+
expect(notA.parent).toBeNull();
|
|
55
|
+
expect(notA.children[0].name).toBe("a");
|
|
56
|
+
expect(notA.isOptional).toBeFalsy();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("Not A Not", () => {
|
|
60
|
+
const a = new Literal("a", "A");
|
|
61
|
+
const notA = new Not("not-a", a);
|
|
62
|
+
const notnotA = new Not("not-not-a", notA);
|
|
63
|
+
|
|
64
|
+
const cursor = new Cursor("A");
|
|
65
|
+
const result = notnotA.parse(cursor);
|
|
66
|
+
|
|
67
|
+
expect(result).toBeNull();
|
|
68
|
+
expect(cursor.hasError).toBeFalsy();
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("Exec", () => {
|
|
72
|
+
const a = new Literal("a", "A");
|
|
73
|
+
const notA = new Not("not-a", a);
|
|
74
|
+
const { ast: result } = notA.exec("A");
|
|
75
|
+
|
|
76
|
+
expect(result).toBeNull();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("Test", () => {
|
|
80
|
+
const a = new Literal("a", "A");
|
|
81
|
+
const notA = new Not("not-a", a);
|
|
82
|
+
const result = notA.test("A");
|
|
83
|
+
|
|
84
|
+
expect(result).toBeFalsy();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("Get Next Tokens", () => {
|
|
88
|
+
const notAboutUs = new Not("not-about-us", new Literal("about-us", "About Us"));
|
|
89
|
+
const sequence = new And("sequence", [notAboutUs, new Literal("about-them", "About Them")]);
|
|
90
|
+
|
|
91
|
+
const cloneNotAboutUs = sequence.findPattern(p => p.name === "not-about-us") as Pattern;
|
|
92
|
+
const nextTokens = cloneNotAboutUs.getNextTokens() || [];
|
|
93
|
+
|
|
94
|
+
expect(nextTokens[0]).toBe("About Them");
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test("Get Next Tokens With No Parent", () => {
|
|
98
|
+
const notAboutUs = new Not("not-about-us", new Literal("about-us", "About Us"));
|
|
99
|
+
const nextTokens = notAboutUs.getNextTokens() || [];
|
|
100
|
+
|
|
101
|
+
expect(nextTokens.length).toBe(0);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
test("Get Tokens", () => {
|
|
105
|
+
const notAboutUs = new Not("not-about-us", new Literal("about-us", "About Us"));
|
|
106
|
+
const sequence = new And("sequence", [notAboutUs, new Literal("about-them", "About Them")]);
|
|
107
|
+
|
|
108
|
+
const cloneNotAboutUs = sequence.findPattern(p => p.name === "not-about-us") as Pattern;
|
|
109
|
+
const nextTokens = cloneNotAboutUs.getTokens() || [];
|
|
110
|
+
|
|
111
|
+
expect(nextTokens[0]).toBe("About Them");
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test("Get Tokens After", () => {
|
|
115
|
+
const notAboutUs = new Not("not-about-us", new Literal("about-us", "About Us"));
|
|
116
|
+
const sequence = new And("sequence", [notAboutUs, new Literal("about-them", "About Them")]);
|
|
117
|
+
const notAboutUsClone = sequence.findPattern(p => p.name === "not-about-us") as Pattern;
|
|
118
|
+
const aboutUsClone = sequence.findPattern(p => p.name === "about-us") as Pattern;
|
|
119
|
+
const nextTokens = notAboutUsClone.getTokensAfter(aboutUsClone) || [];
|
|
120
|
+
|
|
121
|
+
expect(nextTokens[0]).toBe("About Them");
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test("Find Pattern", () => {
|
|
125
|
+
const notAboutUs = new Not("not-about-us", new Literal("about-us", "About Us"));
|
|
126
|
+
const child = notAboutUs.findPattern(p => p.name === "about-us")
|
|
127
|
+
|
|
128
|
+
expect(child).not.toBeNull();
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test("Get Next Patterns", () => {
|
|
132
|
+
const notAboutUs = new Not("not-about-us", new Literal("about-us", "About Us"));
|
|
133
|
+
const sequence = new And("sequence", [notAboutUs, new Literal("about-them", "About Them")]);
|
|
134
|
+
|
|
135
|
+
const cloneNotAboutUs = sequence.findPattern(p => p.name === "not-about-us") as Pattern;
|
|
136
|
+
const patterns = cloneNotAboutUs.getNextPatterns() || [];
|
|
137
|
+
|
|
138
|
+
expect(patterns.length).toBe(1);
|
|
139
|
+
expect(patterns[0].name).toBe("about-them");
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("Get Next Patterns With No Parent", () => {
|
|
143
|
+
const notAboutUs = new Not("not-about-us", new Literal("about-us", "About Us"));
|
|
144
|
+
const patterns = notAboutUs.getNextPatterns() || [];
|
|
145
|
+
|
|
146
|
+
expect(patterns.length).toBe(0);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test("Get Patterns After", () => {
|
|
150
|
+
const notAboutUs = new Not("not-about-us", new Literal("about-us", "About Us"));
|
|
151
|
+
const sequence = new And("sequence", [notAboutUs, new Literal("about-them", "About Them")]);
|
|
152
|
+
const notAboutUsClone = sequence.findPattern(p => p.name === "not-about-us") as Pattern;
|
|
153
|
+
const aboutUsClone = sequence.findPattern(p => p.name === "about-us") as Pattern;
|
|
154
|
+
const patterns = notAboutUsClone.getPatternsAfter(aboutUsClone) || [];
|
|
155
|
+
|
|
156
|
+
expect(patterns.length).toBe(1);
|
|
157
|
+
expect(patterns[0].name).toBe("about-them");
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test("Get Patterns After With Null Parent", () => {
|
|
161
|
+
const notAboutUs = new Not("not-about-us", new Literal("about-us", "About Us"));
|
|
162
|
+
const aboutUsClone = notAboutUs.findPattern(p => p.name === "about-us") as Pattern;
|
|
163
|
+
const patterns = notAboutUs.getPatternsAfter(aboutUsClone) || [];
|
|
164
|
+
|
|
165
|
+
expect(patterns.length).toBe(0);
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
});
|
package/src/patterns/Not.ts
CHANGED
|
@@ -1,50 +1,131 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
1
|
+
import { Node } from "../ast/Node";
|
|
2
|
+
import { Cursor } from "./Cursor";
|
|
3
|
+
import { Pattern } from "./Pattern";
|
|
4
4
|
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
export class Not implements Pattern {
|
|
6
|
+
private _type: string;
|
|
7
|
+
private _name: string;
|
|
8
|
+
private _parent: Pattern | null;
|
|
9
|
+
private _children: Pattern[];
|
|
8
10
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
this._isOptional = true;
|
|
11
|
+
get type(): string {
|
|
12
|
+
return this._type;
|
|
12
13
|
}
|
|
13
14
|
|
|
14
|
-
|
|
15
|
-
this.
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
get name(): string {
|
|
16
|
+
return this._name;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
get parent(): Pattern | null {
|
|
20
|
+
return this._parent;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
set parent(pattern: Pattern | null) {
|
|
24
|
+
this._parent = pattern;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
get children(): Pattern[] {
|
|
28
|
+
return this._children;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
get isOptional(): boolean {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
constructor(name: string, pattern: Pattern) {
|
|
36
|
+
this._type = "not";
|
|
37
|
+
this._name = name;
|
|
38
|
+
this._parent = null;
|
|
39
|
+
this._children = [pattern.clone(pattern.name, false)];
|
|
40
|
+
this._children[0].parent = this;
|
|
19
41
|
}
|
|
20
42
|
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
this.
|
|
43
|
+
test(text: string) {
|
|
44
|
+
const cursor = new Cursor(text);
|
|
45
|
+
this.parse(cursor);
|
|
24
46
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
47
|
+
return !cursor.hasError;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
exec(text: string) {
|
|
51
|
+
const cursor = new Cursor(text);
|
|
52
|
+
const ast = this.parse(cursor);
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
ast,
|
|
56
|
+
cursor
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
parse(cursor: Cursor): Node | null {
|
|
61
|
+
const firstIndex = cursor.index;
|
|
62
|
+
this._children[0].parse(cursor);
|
|
63
|
+
|
|
64
|
+
if (cursor.hasError) {
|
|
65
|
+
cursor.resolveError();
|
|
66
|
+
cursor.moveTo(firstIndex);
|
|
28
67
|
} else {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
this.mark,
|
|
33
|
-
this
|
|
34
|
-
);
|
|
35
|
-
this.cursor.throwError(parseError);
|
|
68
|
+
cursor.moveTo(firstIndex);
|
|
69
|
+
cursor.resolveError();
|
|
70
|
+
cursor.recordErrorAt(firstIndex, this);
|
|
36
71
|
}
|
|
72
|
+
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
clone(name = this._name): Pattern {
|
|
77
|
+
const not = new Not(name, this._children[0]);
|
|
78
|
+
return not;
|
|
37
79
|
}
|
|
38
80
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
81
|
+
getTokens(): string[] {
|
|
82
|
+
const parent = this._parent;
|
|
83
|
+
|
|
84
|
+
if (parent != null) {
|
|
85
|
+
return parent.getTokensAfter(this);
|
|
42
86
|
}
|
|
43
87
|
|
|
44
|
-
return
|
|
88
|
+
return [];
|
|
45
89
|
}
|
|
46
90
|
|
|
47
|
-
|
|
91
|
+
getTokensAfter(_childReference: Pattern): string[] {
|
|
92
|
+
const parent = this._parent;
|
|
93
|
+
|
|
94
|
+
if (parent != null) {
|
|
95
|
+
return parent.getTokensAfter(this);
|
|
96
|
+
}
|
|
97
|
+
|
|
48
98
|
return [];
|
|
49
99
|
}
|
|
100
|
+
|
|
101
|
+
getNextTokens(): string[] {
|
|
102
|
+
if (this.parent == null) {
|
|
103
|
+
return []
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return this.parent.getTokensAfter(this);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
getPatternsAfter(_childReference: Pattern): Pattern[] {
|
|
110
|
+
const parent = this._parent;
|
|
111
|
+
|
|
112
|
+
if (parent != null) {
|
|
113
|
+
return parent.getPatternsAfter(this);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return []
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
getNextPatterns(): Pattern[] {
|
|
120
|
+
if (this.parent == null) {
|
|
121
|
+
return [];
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return this.parent.getPatternsAfter(this)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
findPattern(predicate: (p: Pattern) => boolean): Pattern | null {
|
|
128
|
+
return predicate(this._children[0]) ? this._children[0] : null;
|
|
129
|
+
}
|
|
130
|
+
|
|
50
131
|
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { Cursor } from "./Cursor";
|
|
2
|
+
import { Node } from "../ast/Node";
|
|
3
|
+
import { Literal } from "./Literal";
|
|
4
|
+
import { Or } from "./Or";
|
|
5
|
+
import { And } from "./And";
|
|
6
|
+
import { Pattern } from "./Pattern";
|
|
7
|
+
|
|
8
|
+
describe("Or", () => {
|
|
9
|
+
test("Empty Options", () => {
|
|
10
|
+
expect(() => {
|
|
11
|
+
new Or("bad", []);
|
|
12
|
+
}).toThrowError();
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test("One Option Successful", () => {
|
|
16
|
+
const a = new Or("a", [new Literal("a", "A")]);
|
|
17
|
+
const cursor = new Cursor("A");
|
|
18
|
+
const result = a.parse(cursor);
|
|
19
|
+
const expected = new Node("literal", "a", 0, 0, [], "A")
|
|
20
|
+
|
|
21
|
+
expect(result).toEqual(expected);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test("One Option Failed", () => {
|
|
25
|
+
const a = new Or("a", [new Literal("a", "A")]);
|
|
26
|
+
const cursor = new Cursor("B");
|
|
27
|
+
const result = a.parse(cursor);
|
|
28
|
+
|
|
29
|
+
expect(result).toEqual(null);
|
|
30
|
+
expect(cursor.index).toBe(0);
|
|
31
|
+
expect(cursor.hasError).toBeTruthy();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("Two Option", () => {
|
|
35
|
+
const a = new Or("a-b", [new Literal("a", "A"), new Literal("b", "B")]);
|
|
36
|
+
const cursor = new Cursor("AB");
|
|
37
|
+
let result = a.parse(cursor);
|
|
38
|
+
let expected = new Node("literal", "a", 0, 0, [], "A")
|
|
39
|
+
|
|
40
|
+
expect(result).toEqual(expected);
|
|
41
|
+
|
|
42
|
+
cursor.next();
|
|
43
|
+
|
|
44
|
+
result = a.parse(cursor);
|
|
45
|
+
expected = new Node("or", "a-b", 0, 0, [
|
|
46
|
+
new Node("literal", "b", 0, 0, [], "B")
|
|
47
|
+
], "B");
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("Failed (Optional)", () => {
|
|
51
|
+
const a = new Or("a", [new Literal("a", "A")], true);
|
|
52
|
+
const cursor = new Cursor("B");
|
|
53
|
+
const result = a.parse(cursor);
|
|
54
|
+
|
|
55
|
+
expect(result).toBeNull();
|
|
56
|
+
expect(cursor.hasError).toBeFalsy();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("Get Tokens", () => {
|
|
60
|
+
const aOrB = new Or("a-b", [new Literal("a", "A"), new Literal("b", "B")]);
|
|
61
|
+
const tokens = aOrB.getTokens();
|
|
62
|
+
const expected = ["A", "B"];
|
|
63
|
+
|
|
64
|
+
expect(tokens).toEqual(expected);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("Get Tokens After", () => {
|
|
68
|
+
const a = new Or("a", [new Literal("a", "A")]);
|
|
69
|
+
const parent = new And("parent", [a, new Literal("b", "B")]);
|
|
70
|
+
const tokens = parent.children[0].getTokensAfter(parent.children[0].children[0]);
|
|
71
|
+
const expected = ["B"];
|
|
72
|
+
|
|
73
|
+
expect(tokens).toEqual(expected);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("Get Tokens After Without A Parent", () => {
|
|
77
|
+
const a = new Or("a", [new Literal("a", "A")]);
|
|
78
|
+
const tokens = a.getTokensAfter(a.children[0]);
|
|
79
|
+
const expected: string[] = [];
|
|
80
|
+
|
|
81
|
+
expect(tokens).toEqual(expected);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("Properties", () => {
|
|
85
|
+
const a = new Or("a", [new Literal("a", "A")]);
|
|
86
|
+
|
|
87
|
+
expect(a.type).toBe("or");
|
|
88
|
+
expect(a.name).toBe("a");
|
|
89
|
+
expect(a.isOptional).toBeFalsy();
|
|
90
|
+
expect(a.parent).toBeNull();
|
|
91
|
+
expect(a.children[0].name).toBe("a");
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test("Exec", () => {
|
|
95
|
+
const a = new Or("a", [new Literal("a", "A")]);
|
|
96
|
+
const { ast: result } = a.exec("B");
|
|
97
|
+
expect(result).toBeNull();
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("Test No Match", () => {
|
|
101
|
+
const a = new Or("a", [new Literal("a", "A")]);
|
|
102
|
+
const result = a.test("B");
|
|
103
|
+
expect(result).toBeFalsy();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test("Test With Match", () => {
|
|
107
|
+
const a = new Or("a", [new Literal("a", "A")]);
|
|
108
|
+
const result = a.test("A");
|
|
109
|
+
expect(result).toBeTruthy();
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("Get Next Tokens", () => {
|
|
113
|
+
const sequence = new And("sequence", [
|
|
114
|
+
new Or("a-or-b", [
|
|
115
|
+
new Literal("a", "A"),
|
|
116
|
+
new Literal("b", "B")
|
|
117
|
+
]),
|
|
118
|
+
new Literal("c", "C")
|
|
119
|
+
]);
|
|
120
|
+
|
|
121
|
+
const orClone = sequence.findPattern(p => p.name === "a-or-b") as Pattern;
|
|
122
|
+
const tokens = orClone.getNextTokens();
|
|
123
|
+
|
|
124
|
+
expect(tokens.length).toBe(1);
|
|
125
|
+
expect(tokens[0]).toBe("C");
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test("Get Next Tokens With Null Parent", () => {
|
|
129
|
+
const or = new Or("a-or-b", [
|
|
130
|
+
new Literal("a", "A"),
|
|
131
|
+
new Literal("b", "B")
|
|
132
|
+
])
|
|
133
|
+
|
|
134
|
+
const tokens = or.getNextTokens();
|
|
135
|
+
expect(tokens.length).toBe(0);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
test("Get Tokens After", () => {
|
|
140
|
+
const sequence = new And("sequence", [
|
|
141
|
+
new Or("a-or-b", [
|
|
142
|
+
new Literal("a", "A"),
|
|
143
|
+
new Literal("b", "B")
|
|
144
|
+
]),
|
|
145
|
+
new Literal("c", "C")
|
|
146
|
+
]);
|
|
147
|
+
|
|
148
|
+
const aClone = sequence.findPattern(p => p.name === "a") as Pattern;
|
|
149
|
+
const orClone = sequence.findPattern(p => p.name === "a-or-b") as Pattern;
|
|
150
|
+
const tokens = orClone.getTokensAfter(aClone);
|
|
151
|
+
|
|
152
|
+
expect(tokens.length).toBe(1);
|
|
153
|
+
expect(tokens[0]).toBe("C");
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
test("Get Patterns After", () => {
|
|
157
|
+
const sequence = new And("sequence", [
|
|
158
|
+
new Or("a-or-b", [
|
|
159
|
+
new Literal("a", "A"),
|
|
160
|
+
new Literal("b", "B")
|
|
161
|
+
]),
|
|
162
|
+
new Literal("c", "C")
|
|
163
|
+
]);
|
|
164
|
+
|
|
165
|
+
const aClone = sequence.findPattern(p => p.name === "a") as Pattern;
|
|
166
|
+
const orClone = sequence.findPattern(p => p.name === "a-or-b") as Pattern;
|
|
167
|
+
const patterns = orClone.getPatternsAfter(aClone);
|
|
168
|
+
|
|
169
|
+
expect(patterns.length).toBe(1);
|
|
170
|
+
expect(patterns[0].name).toBe("c");
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
test("Get Patterns After With Null Parent", () => {
|
|
174
|
+
const or = new Or("a-or-b", [
|
|
175
|
+
new Literal("a", "A"),
|
|
176
|
+
new Literal("b", "B")
|
|
177
|
+
])
|
|
178
|
+
const aClone = or.findPattern(p => p.name === "a") as Pattern;
|
|
179
|
+
const patterns = or.getPatternsAfter(aClone);
|
|
180
|
+
|
|
181
|
+
expect(patterns.length).toBe(0);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
test("Get Next Patterns", () => {
|
|
185
|
+
const sequence = new And("sequence", [
|
|
186
|
+
new Or("a-or-b", [
|
|
187
|
+
new Literal("a", "A"),
|
|
188
|
+
new Literal("b", "B")
|
|
189
|
+
]),
|
|
190
|
+
new Literal("c", "C")
|
|
191
|
+
]);
|
|
192
|
+
|
|
193
|
+
const orClone = sequence.findPattern(p => p.name === "a-or-b") as Pattern;
|
|
194
|
+
const patterns = orClone.getNextPatterns();
|
|
195
|
+
|
|
196
|
+
expect(patterns.length).toBe(1);
|
|
197
|
+
expect(patterns[0].name).toBe("c");
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
test("Get Next Patterns With Null Parent", () => {
|
|
201
|
+
const or = new Or("a-or-b", [
|
|
202
|
+
new Literal("a", "A"),
|
|
203
|
+
new Literal("b", "B")
|
|
204
|
+
])
|
|
205
|
+
const patterns = or.getNextPatterns();
|
|
206
|
+
|
|
207
|
+
expect(patterns.length).toBe(0);
|
|
208
|
+
});
|
|
209
|
+
});
|