clarity-pattern-parser 4.0.2 → 5.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 +176 -1
- package/TODO.md +22 -2
- package/dist/ast/Node.d.ts +43 -11
- package/dist/ast/Visitor.d.ts +31 -31
- package/dist/index.browser.js +1248 -1495
- package/dist/index.browser.js.map +1 -1
- package/dist/index.d.ts +12 -16
- package/dist/index.esm.js +1218 -1460
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +1217 -1464
- package/dist/index.js.map +1 -1
- package/dist/patterns/And.d.ts +37 -24
- package/dist/patterns/Cursor.d.ts +35 -0
- package/dist/patterns/CursorHistory.d.ts +30 -0
- package/dist/patterns/Literal.d.ts +36 -19
- package/dist/patterns/Not.d.ts +26 -11
- package/dist/patterns/Or.d.ts +31 -22
- package/dist/patterns/ParseError.d.ts +6 -8
- package/dist/patterns/ParseResult.d.ts +6 -0
- package/dist/patterns/Pattern.d.ts +17 -26
- package/dist/patterns/Reference.d.ts +31 -12
- package/dist/patterns/Regex.d.ts +42 -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 +254 -0
- package/src/ast/Node.ts +171 -23
- package/src/index.ts +11 -19
- package/src/intellisense/AutoComplete.test.ts +72 -0
- package/src/intellisense/AutoComplete.ts +146 -0
- package/src/intellisense/Suggestion.ts +13 -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 +50 -0
- package/src/intellisense/javascript/exponent.ts +26 -0
- package/src/intellisense/javascript/expression.ts +87 -0
- package/src/intellisense/javascript/expressionStatement.ts +29 -0
- package/src/intellisense/javascript/fraction.ts +13 -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 +299 -0
- package/src/patterns/And.ts +222 -119
- package/src/patterns/Cursor.test.ts +93 -0
- package/src/patterns/Cursor.ts +130 -0
- package/src/patterns/CursorHistory.test.ts +54 -0
- package/src/patterns/CursorHistory.ts +95 -0
- package/src/patterns/Literal.test.ts +134 -0
- package/src/patterns/Literal.ts +151 -61
- package/src/patterns/Not.test.ts +88 -0
- package/src/patterns/Not.ts +74 -33
- package/src/patterns/Or.test.ts +105 -0
- package/src/patterns/Or.ts +106 -98
- package/src/patterns/ParseError.ts +3 -7
- package/src/patterns/ParseResult.ts +7 -0
- package/src/patterns/Pattern.ts +18 -150
- package/src/patterns/Reference.test.ts +104 -0
- package/src/patterns/Reference.ts +94 -94
- package/src/patterns/Regex.test.ts +101 -0
- package/src/patterns/Regex.ts +129 -60
- package/src/patterns/Repeat.test.ts +196 -0
- package/src/patterns/Repeat.ts +208 -104
- package/src/patterns/clonePatterns.ts +5 -0
- package/src/patterns/filterOutNull.ts +13 -0
- package/src/patterns/findPattern.ts +25 -0
- package/src/patterns/getNextPattern.test.ts +39 -0
- package/src/patterns/getNextPattern.ts +18 -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
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
/** @jest-environment node */
|
|
2
|
-
import cssValue from "./cssPatterns/cssValue";
|
|
3
|
-
import { Cursor } from "../index";
|
|
4
|
-
|
|
5
|
-
describe("Css", () => {
|
|
6
|
-
test("Css: unit", () => {
|
|
7
|
-
const cursor = new Cursor("10% 10%");
|
|
8
|
-
const result = cssValue.parse(cursor);
|
|
9
|
-
|
|
10
|
-
expect(result?.endIndex).toBe(cursor.text.length - 1);
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
test("Css: All known unit values spaced", () => {
|
|
14
|
-
const cursor = new Cursor(
|
|
15
|
-
"10 linear-gradient(to left, #333, #333 50%, #eee 75%, #333 75%) rgba(0,0,0,1) #333 #555555 0px 0% 0deg 1em radial-gradient(at 40% 40%, rgba(187,202,218,1) 0%, rgba(187,202,218,1) 20%, rgba(187,202,218,1) 100%)"
|
|
16
|
-
);
|
|
17
|
-
const result = cssValue.parse(cursor);
|
|
18
|
-
expect(result?.endIndex).toBe(cursor.text.length - 1);
|
|
19
|
-
expect(cursor.text).toBe(result?.toString());
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
test("Css: radial-gradient", () => {
|
|
23
|
-
const cursor = new Cursor(
|
|
24
|
-
"radial-gradient(at 40% 40%, rgba(187,202,218,1) 0%, rgba(187,202,218,1) 20%, rgba(187,202,218,1) 100%)"
|
|
25
|
-
);
|
|
26
|
-
const result = cssValue.parse(cursor);
|
|
27
|
-
expect(result?.endIndex).toBe(cursor.text.length - 1);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test("Css: rgba", () => {
|
|
31
|
-
const cursor = new Cursor("rgba(0,0,0,0)");
|
|
32
|
-
const result = cssValue.parse(cursor);
|
|
33
|
-
expect(result?.endIndex).toBe(cursor.text.length - 1);
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
test("Css: method mutliple values", () => {
|
|
37
|
-
const cursor = new Cursor("method-one(first 0px third, arg2)");
|
|
38
|
-
const result = cssValue.parse(cursor);
|
|
39
|
-
expect(result?.endIndex).toBe(cursor.text.length - 1);
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
test("Css: nested methods", () => {
|
|
43
|
-
const cursor = new Cursor("outer-method(inner-method(0), 0)");
|
|
44
|
-
const result = cssValue.parse(cursor);
|
|
45
|
-
expect(result?.endIndex).toBe(cursor.text.length - 1);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
test("Css: nested methods with trailing value", () => {
|
|
49
|
-
const cursor = new Cursor("outer-method(inner-method(0,0), 0)");
|
|
50
|
-
const result = cssValue.parse(cursor);
|
|
51
|
-
expect(result?.endIndex).toBe(cursor.text.length - 1);
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
test("Css: spaced values in a method", () => {
|
|
55
|
-
const cursor = new Cursor("method(arg1 0%, arg2, 10%) 0%");
|
|
56
|
-
const result = cssValue.parse(cursor);
|
|
57
|
-
expect(result?.endIndex).toBe(cursor.text.length - 1);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
test("Css: spaced values within method", () => {
|
|
61
|
-
const cursor = new Cursor("method(inner-method(0,0,0,0) 0%, arg2, 10%)");
|
|
62
|
-
const result = cssValue.parse(cursor);
|
|
63
|
-
expect(result?.endIndex).toBe(cursor.text.length - 1);
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
test("Css: spaced values", () => {
|
|
67
|
-
const cursor = new Cursor(
|
|
68
|
-
"inner-method(0,0,0,0) inner-method(0,0,0,0) 0px 10px 0%"
|
|
69
|
-
);
|
|
70
|
-
const result = cssValue.parse(cursor);
|
|
71
|
-
expect(result?.endIndex).toBe(cursor.text.length - 1);
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
test("Css: complex spaced values", () => {
|
|
75
|
-
const cursor = new Cursor(
|
|
76
|
-
"#222 linear-gradient(to left, #333, #333 50%, #eee 75%, #333 75%) linear-gradient(to bottom, #555, #555 50%, #eee 75%, #555 75%)"
|
|
77
|
-
);
|
|
78
|
-
const result = cssValue.parse(cursor);
|
|
79
|
-
expect(result?.endIndex).toBe(cursor.text.length - 1);
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
test("Css: multiple linear gradients", () => {
|
|
83
|
-
const cursor = new Cursor(
|
|
84
|
-
"linear-gradient(to left, #333, #333 50%, #eee 75%, #333 75%), linear-gradient(to bottom, #555, #555 50%, #eee 75%, #555 75%)"
|
|
85
|
-
);
|
|
86
|
-
const result = cssValue.parse(cursor);
|
|
87
|
-
expect(result?.endIndex).toBe(cursor.text.length - 1);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
});
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
/** @jest-environment node */
|
|
2
|
-
import CursorHistory from "../CursorHistory";
|
|
3
|
-
import { Literal, Node, Cursor, ParseError } from "../index";
|
|
4
|
-
import sentence from "./patterns/sentence";
|
|
5
|
-
describe("CursorHistory", () => {
|
|
6
|
-
test("addMatch", () => {
|
|
7
|
-
const cursorHistory = new CursorHistory();
|
|
8
|
-
const pattern = new Literal("T", "T");
|
|
9
|
-
const node = new Node("T", "T", 0, 1, [], "T");
|
|
10
|
-
|
|
11
|
-
cursorHistory.addMatch(pattern, node);
|
|
12
|
-
expect(cursorHistory.getFurthestMatch().pattern).toBe(pattern);
|
|
13
|
-
expect(cursorHistory.getFurthestMatch().astNode).toBe(node);
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
test("addMatch with Recording", () => {
|
|
17
|
-
const cursorHistory = new CursorHistory();
|
|
18
|
-
const pattern = new Literal("T", "T");
|
|
19
|
-
const node = new Node("T", "T", 0, 1, [], "T");
|
|
20
|
-
|
|
21
|
-
cursorHistory.startRecording();
|
|
22
|
-
cursorHistory.addMatch(pattern, node);
|
|
23
|
-
|
|
24
|
-
expect(cursorHistory.getFurthestMatch().pattern).toBe(pattern);
|
|
25
|
-
expect(cursorHistory.getFurthestMatch().astNode).toBe(node);
|
|
26
|
-
expect(cursorHistory.getLastMatch().pattern).toBe(pattern);
|
|
27
|
-
expect(cursorHistory.getLastMatch().astNode).toBe(node);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test("addError", () => {
|
|
31
|
-
const cursorHistory = new CursorHistory();
|
|
32
|
-
const error = new ParseError(
|
|
33
|
-
"Expected something else.",
|
|
34
|
-
0,
|
|
35
|
-
new Literal("T", "T")
|
|
36
|
-
);
|
|
37
|
-
|
|
38
|
-
cursorHistory.addError(error);
|
|
39
|
-
|
|
40
|
-
expect(cursorHistory.getFurthestError()).toBe(error);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
test("addError with recording", () => {
|
|
44
|
-
const cursorHistory = new CursorHistory();
|
|
45
|
-
const error = new ParseError(
|
|
46
|
-
"Expected something else.",
|
|
47
|
-
0,
|
|
48
|
-
new Literal("T", "T")
|
|
49
|
-
);
|
|
50
|
-
|
|
51
|
-
cursorHistory.startRecording();
|
|
52
|
-
cursorHistory.addError(error);
|
|
53
|
-
|
|
54
|
-
expect(cursorHistory.getFurthestError()).toBe(error);
|
|
55
|
-
expect(cursorHistory.getLastError()).toBe(error);
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
test("getLastError without any.", () => {
|
|
59
|
-
const cursorHistory = new CursorHistory();
|
|
60
|
-
expect(cursorHistory.getLastError()).toBe(null);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
test("getFurthestMatch without an matches.", () => {
|
|
64
|
-
const cursorHistory = new CursorHistory();
|
|
65
|
-
|
|
66
|
-
expect(cursorHistory.getLastMatch().pattern).toBe(null);
|
|
67
|
-
expect(cursorHistory.getLastMatch().astNode).toBe(null);
|
|
68
|
-
expect(cursorHistory.getFurthestMatch().pattern).toBe(null);
|
|
69
|
-
expect(cursorHistory.getFurthestMatch().astNode).toBe(null);
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
test("getFurthestMatch without an matches while recording.", () => {
|
|
73
|
-
const cursorHistory = new CursorHistory();
|
|
74
|
-
cursorHistory.startRecording();
|
|
75
|
-
|
|
76
|
-
expect(cursorHistory.getLastMatch().pattern).toBe(null);
|
|
77
|
-
expect(cursorHistory.getLastMatch().astNode).toBe(null);
|
|
78
|
-
expect(cursorHistory.getFurthestMatch().pattern).toBe(null);
|
|
79
|
-
expect(cursorHistory.getFurthestMatch().astNode).toBe(null);
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
test("getAllParseStacks.", () => {
|
|
83
|
-
const text = "Pat went to the";
|
|
84
|
-
const cursor = new Cursor(text);
|
|
85
|
-
cursor.startRecording();
|
|
86
|
-
|
|
87
|
-
sentence.parse(cursor);
|
|
88
|
-
cursor.history.getAllParseStacks();
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
test("getLastParseStack.", () => {
|
|
92
|
-
const text = "Pat went to the";
|
|
93
|
-
const cursor = new Cursor(text);
|
|
94
|
-
cursor.startRecording();
|
|
95
|
-
|
|
96
|
-
sentence.parse(cursor);
|
|
97
|
-
|
|
98
|
-
const stack = cursor.history.getLastParseStack();
|
|
99
|
-
|
|
100
|
-
expect(stack.length).toBe(5);
|
|
101
|
-
expect(stack[0].name).toBe("noun");
|
|
102
|
-
expect(stack[1].name).toBe("space");
|
|
103
|
-
expect(stack[2].name).toBe("verb");
|
|
104
|
-
expect(stack[3].name).toBe("space");
|
|
105
|
-
expect(stack[4].name).toBe("article");
|
|
106
|
-
});
|
|
107
|
-
});
|
package/src/tests/Cusor.test.ts
DELETED
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
/** @jest-environment node */
|
|
2
|
-
import { Cursor, Literal, ParseError, Node } from "../index";
|
|
3
|
-
|
|
4
|
-
describe("Cursor", () => {
|
|
5
|
-
test("addMatch.", () => {
|
|
6
|
-
const cursor = new Cursor("Text");
|
|
7
|
-
const node = new Node("text-node", "Text", 0, 3, [], "Text");
|
|
8
|
-
const pattern = new Literal("text", "Text");
|
|
9
|
-
|
|
10
|
-
cursor.setIndex(3);
|
|
11
|
-
cursor.addMatch(pattern, node);
|
|
12
|
-
|
|
13
|
-
expect(cursor.history.furthestMatch.astNode).toBe(node);
|
|
14
|
-
expect(cursor.history.furthestMatch.pattern).toBe(pattern);
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
test("addMatch that isn't as far.", () => {
|
|
18
|
-
const cursor = new Cursor("Text");
|
|
19
|
-
const node = new Node("text-node", "Text", 0, 3, [], "Text");
|
|
20
|
-
const pattern = new Literal("text", "Text Node");
|
|
21
|
-
|
|
22
|
-
const shorterNode = new Node("tex-node", "Tex", 0, 2, [], "Tex2");
|
|
23
|
-
const shorterPattern = new Literal("tex", "Tex");
|
|
24
|
-
|
|
25
|
-
cursor.setIndex(3);
|
|
26
|
-
cursor.addMatch(pattern, node);
|
|
27
|
-
|
|
28
|
-
expect(cursor.history.furthestMatch.astNode).toBe(node);
|
|
29
|
-
expect(cursor.history.furthestMatch.pattern).toBe(pattern);
|
|
30
|
-
|
|
31
|
-
cursor.setIndex(2);
|
|
32
|
-
cursor.addMatch(shorterPattern, shorterNode);
|
|
33
|
-
|
|
34
|
-
expect(cursor.history.furthestMatch.astNode).toBe(node);
|
|
35
|
-
expect(cursor.history.furthestMatch.pattern).toBe(pattern);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
test("didSuccessfullyParse.", () => {
|
|
39
|
-
const cursor = new Cursor("Text");
|
|
40
|
-
cursor.setIndex(3);
|
|
41
|
-
expect(cursor.didSuccessfullyParse()).toBe(true);
|
|
42
|
-
|
|
43
|
-
cursor.throwError(
|
|
44
|
-
new ParseError("Failed To Parse.", 0, new Literal("T", "T"))
|
|
45
|
-
);
|
|
46
|
-
|
|
47
|
-
expect(cursor.didSuccessfullyParse()).toBe(false);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
test("getChar.", () => {
|
|
51
|
-
const cursor = new Cursor("Text");
|
|
52
|
-
expect(cursor.getChar()).toBe("T");
|
|
53
|
-
cursor.setIndex(3);
|
|
54
|
-
expect(cursor.getChar()).toBe("t");
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
test("getIndex.", () => {
|
|
58
|
-
const cursor = new Cursor("Text");
|
|
59
|
-
expect(cursor.getIndex()).toBe(0);
|
|
60
|
-
cursor.setIndex(3);
|
|
61
|
-
expect(cursor.getIndex()).toBe(3);
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
test("hasNext.", () => {
|
|
65
|
-
const cursor = new Cursor("Text");
|
|
66
|
-
expect(cursor.hasNext()).toBe(true);
|
|
67
|
-
cursor.setIndex(3);
|
|
68
|
-
expect(cursor.hasNext()).toBe(false);
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
test("hasPrevious.", () => {
|
|
72
|
-
const cursor = new Cursor("Text");
|
|
73
|
-
expect(cursor.hasPrevious()).toBe(false);
|
|
74
|
-
cursor.setIndex(3);
|
|
75
|
-
expect(cursor.hasPrevious()).toBe(true);
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
test("recording history.", () => {
|
|
79
|
-
const cursor = new Cursor("Text");
|
|
80
|
-
const tNode = new Node("T", "T", 0, 0, [], "T");
|
|
81
|
-
const tPattern = new Literal("T", "T");
|
|
82
|
-
|
|
83
|
-
const eNode = new Node("e", "e", 1, 1, [], "e");
|
|
84
|
-
const ePattern = new Literal("e", "e");
|
|
85
|
-
|
|
86
|
-
const xNode = new Node("x", "x", 2, 2, [], "x");
|
|
87
|
-
const xPattern = new Literal("x", "x");
|
|
88
|
-
|
|
89
|
-
cursor.startRecording();
|
|
90
|
-
|
|
91
|
-
cursor.addMatch(tPattern, tNode);
|
|
92
|
-
cursor.addMatch(ePattern, eNode);
|
|
93
|
-
|
|
94
|
-
expect(cursor.history.patterns.length).toBe(2);
|
|
95
|
-
expect(cursor.history.astNodes.length).toBe(2);
|
|
96
|
-
expect(cursor.history.getFurthestMatch().astNode).toBe(eNode);
|
|
97
|
-
expect(cursor.history.getFurthestMatch().pattern).toBe(ePattern);
|
|
98
|
-
expect(cursor.history.getLastMatch().astNode).toBe(eNode);
|
|
99
|
-
expect(cursor.history.getLastMatch().pattern).toBe(ePattern);
|
|
100
|
-
|
|
101
|
-
cursor.stopRecording();
|
|
102
|
-
|
|
103
|
-
cursor.addMatch(xPattern, xNode);
|
|
104
|
-
|
|
105
|
-
expect(cursor.history.patterns.length).toBe(0);
|
|
106
|
-
expect(cursor.history.astNodes.length).toBe(0);
|
|
107
|
-
expect(cursor.history.getFurthestMatch().astNode).toBe(xNode);
|
|
108
|
-
expect(cursor.history.getFurthestMatch().pattern).toBe(xPattern);
|
|
109
|
-
expect(cursor.history.getLastMatch().astNode).toBe(xNode);
|
|
110
|
-
expect(cursor.history.getLastMatch().pattern).toBe(xPattern);
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
test("moveToBeginning.", () => {
|
|
114
|
-
const cursor = new Cursor("Text");
|
|
115
|
-
cursor.setIndex(3);
|
|
116
|
-
cursor.moveToBeginning();
|
|
117
|
-
|
|
118
|
-
expect(cursor.getIndex()).toBe(0);
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
test("moveToEnd.", () => {
|
|
122
|
-
const cursor = new Cursor("Text");
|
|
123
|
-
cursor.moveToEnd();
|
|
124
|
-
|
|
125
|
-
expect(cursor.getIndex()).toBe(3);
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
test("setIndex outside of bounds.", () => {
|
|
129
|
-
const cursor = new Cursor("Text");
|
|
130
|
-
expect(() => {
|
|
131
|
-
cursor.setIndex(4);
|
|
132
|
-
}).toThrow();
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
test("next out of bounds.", () => {
|
|
136
|
-
const cursor = new Cursor("Text");
|
|
137
|
-
cursor.setIndex(3);
|
|
138
|
-
|
|
139
|
-
expect(() => {
|
|
140
|
-
cursor.next();
|
|
141
|
-
}).toThrow();
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
test("previous out of bounds.", () => {
|
|
145
|
-
const cursor = new Cursor("Text");
|
|
146
|
-
|
|
147
|
-
expect(() => {
|
|
148
|
-
cursor.previous();
|
|
149
|
-
}).toThrow();
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
test("previous.", () => {
|
|
153
|
-
const cursor = new Cursor("Text");
|
|
154
|
-
cursor.setIndex(1);
|
|
155
|
-
cursor.previous();
|
|
156
|
-
|
|
157
|
-
expect(cursor.getIndex()).toBe(0);
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
test("isAtBeginning.", () => {
|
|
161
|
-
const cursor = new Cursor("Text");
|
|
162
|
-
cursor.isAtBeginning();
|
|
163
|
-
|
|
164
|
-
expect(cursor.getIndex()).toBe(0);
|
|
165
|
-
expect(cursor.isAtBeginning()).toBe(true);
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
test("setIndex invalid number.", () => {
|
|
169
|
-
const cursor = new Cursor("Text");
|
|
170
|
-
expect(() => {
|
|
171
|
-
cursor.setIndex(-1);
|
|
172
|
-
});
|
|
173
|
-
});
|
|
174
|
-
});
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
/** @jest-environment node */
|
|
2
|
-
import element, { attribute } from "./htmlPatterns/element";
|
|
3
|
-
import { Cursor } from "../index";
|
|
4
|
-
|
|
5
|
-
describe("HTML", () => {
|
|
6
|
-
test("Html: element", () => {
|
|
7
|
-
const cursor = new Cursor("<div></div>");
|
|
8
|
-
const result = element.parse(cursor);
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
test("Html: element nested", () => {
|
|
12
|
-
const cursor = new Cursor("<div><p><span></span></p></div>");
|
|
13
|
-
const result = element.parse(cursor);
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
test("Html: element with attributes", () => {
|
|
17
|
-
const cursor = new Cursor('<div prop="value"></div>');
|
|
18
|
-
const result = element.parse(cursor);
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
test("Html: multiple children", () => {
|
|
22
|
-
const cursor = new Cursor(`<div prop="value">
|
|
23
|
-
Text
|
|
24
|
-
<span>Hello</span>
|
|
25
|
-
<span>World!</span>
|
|
26
|
-
</div>`);
|
|
27
|
-
const result = element.parse(cursor);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test("Attributes: ", () => {
|
|
31
|
-
const cursor = new Cursor('prop="boo"');
|
|
32
|
-
const result = attribute.parse(cursor);
|
|
33
|
-
});
|
|
34
|
-
});
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
/** @jest-environment node */
|
|
2
|
-
import { Literal, Cursor } from "../index";
|
|
3
|
-
|
|
4
|
-
describe("Literal", () => {
|
|
5
|
-
test("Empty literal.", () => {
|
|
6
|
-
expect(() => {
|
|
7
|
-
new Literal("literal", "");
|
|
8
|
-
}).toThrow();
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
test("exec.", () => {
|
|
12
|
-
const john = new Literal("john", "John");
|
|
13
|
-
|
|
14
|
-
const result = john.exec("John");
|
|
15
|
-
const result2 = john.exec("Jane");
|
|
16
|
-
|
|
17
|
-
const expectedValue = {
|
|
18
|
-
type: "literal",
|
|
19
|
-
name: "john",
|
|
20
|
-
startIndex: 0,
|
|
21
|
-
endIndex: 3,
|
|
22
|
-
children: [],
|
|
23
|
-
value: "John",
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
expect(JSON.stringify(result)).toBe(JSON.stringify(expectedValue));
|
|
27
|
-
expect(result2).toBe(null);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test("Match.", () => {
|
|
31
|
-
const variable = new Literal("variable", "var");
|
|
32
|
-
const cursor = new Cursor("var foo = 'Hello World';");
|
|
33
|
-
const node = variable.parse(cursor);
|
|
34
|
-
|
|
35
|
-
expect(node?.name).toBe("variable");
|
|
36
|
-
expect(node?.value).toBe("var");
|
|
37
|
-
expect(cursor.getIndex()).toBe(2);
|
|
38
|
-
expect(cursor.getChar()).toBe("r");
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
test("Match at end.", () => {
|
|
42
|
-
const variable = new Literal("variable", "var");
|
|
43
|
-
const cursor = new Cursor("var");
|
|
44
|
-
const node = variable.parse(cursor);
|
|
45
|
-
|
|
46
|
-
expect(node?.name).toBe("variable");
|
|
47
|
-
expect(node?.value).toBe("var");
|
|
48
|
-
expect(cursor.getIndex()).toBe(2);
|
|
49
|
-
expect(cursor.getChar()).toBe("r");
|
|
50
|
-
expect(cursor.isAtEnd()).toBe(true);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
test("No match.", () => {
|
|
54
|
-
const variable = new Literal("variable", "var");
|
|
55
|
-
const cursor = new Cursor("vax");
|
|
56
|
-
|
|
57
|
-
variable.parse(cursor);
|
|
58
|
-
|
|
59
|
-
expect(cursor.hasUnresolvedError()).toBe(true);
|
|
60
|
-
expect(cursor.getIndex()).toBe(0);
|
|
61
|
-
expect(cursor.getChar()).toBe("v");
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
test("Bad cursor.", () => {
|
|
65
|
-
const variable = new Literal("variable", "var");
|
|
66
|
-
|
|
67
|
-
expect(() => {
|
|
68
|
-
(variable as any).parse();
|
|
69
|
-
}).toThrow();
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
test("Pattern methods.", () => {
|
|
73
|
-
const variable = new Literal("variable", "var");
|
|
74
|
-
const clone = variable.clone();
|
|
75
|
-
|
|
76
|
-
expect(variable.name).toBe(clone.name);
|
|
77
|
-
expect(variable.children.length).toBe(clone.children.length);
|
|
78
|
-
});
|
|
79
|
-
});
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
/** @jest-environment node */
|
|
2
|
-
import { Literal, Cursor, LookAhead, Not } from "../index";
|
|
3
|
-
|
|
4
|
-
describe("LookAheadValue", () => {
|
|
5
|
-
test("Look for pattern.", () => {
|
|
6
|
-
const john = new Literal("john", "John");
|
|
7
|
-
const lookAheadValue = new LookAhead(john);
|
|
8
|
-
const cursor = new Cursor("John");
|
|
9
|
-
const node = lookAheadValue.parse(cursor);
|
|
10
|
-
|
|
11
|
-
expect(node).toBe(null);
|
|
12
|
-
expect(cursor.hasUnresolvedError()).toBe(false);
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
test("Look for a not pattern.", () => {
|
|
16
|
-
const john = new Literal("john", "John");
|
|
17
|
-
const lookAheadValue = new Not(new LookAhead(john));
|
|
18
|
-
const cursor = new Cursor("Joel");
|
|
19
|
-
const node = lookAheadValue.parse(cursor);
|
|
20
|
-
|
|
21
|
-
expect(node).toBe(null);
|
|
22
|
-
expect(cursor.hasUnresolvedError()).toBe(false);
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
test("Fail looking for a pattern.", () => {
|
|
26
|
-
const john = new Literal("john", "John");
|
|
27
|
-
const lookAheadValue = new LookAhead(john);
|
|
28
|
-
const cursor = new Cursor("Joel");
|
|
29
|
-
const node = lookAheadValue.parse(cursor);
|
|
30
|
-
|
|
31
|
-
expect(node).toBe(null);
|
|
32
|
-
expect(cursor.hasUnresolvedError()).toBe(true);
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
test("And a look ahead together.", () => {
|
|
36
|
-
const john = new Literal("john", "John");
|
|
37
|
-
const lookAheadValue = new LookAhead(john);
|
|
38
|
-
const cursor = new Cursor("Joel");
|
|
39
|
-
const node = lookAheadValue.parse(cursor);
|
|
40
|
-
|
|
41
|
-
expect(node).toBe(null);
|
|
42
|
-
expect(cursor.hasUnresolvedError()).toBe(true);
|
|
43
|
-
});
|
|
44
|
-
});
|
package/src/tests/Not.test.ts
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import Cursor from "../Cursor";
|
|
2
|
-
import And from "../patterns/And";
|
|
3
|
-
import Literal from "../patterns/Literal";
|
|
4
|
-
import Not from "../patterns/Not";
|
|
5
|
-
import Or from "../patterns/Or";
|
|
6
|
-
import Regex from "../patterns/Regex";
|
|
7
|
-
|
|
8
|
-
describe("Not", () => {
|
|
9
|
-
test("Negate pattern with literal.", () => {
|
|
10
|
-
const greaterThan = new Literal("greater-than", ">");
|
|
11
|
-
const lessThan = new Literal("less-than", "<");
|
|
12
|
-
const from = new Literal("from", "FROM");
|
|
13
|
-
const table = new Literal("table", "Table");
|
|
14
|
-
const operator = new Or("operator", [lessThan, greaterThan]);
|
|
15
|
-
const keywords = new And("keywords-with-space", [
|
|
16
|
-
new Or("keywords", [from, table]),
|
|
17
|
-
new Literal("space", " "),
|
|
18
|
-
]);
|
|
19
|
-
const identFirstPart = new Regex(
|
|
20
|
-
"ident-first-part",
|
|
21
|
-
"[a-zA-Z_$][a-zA-Z0-9_]*"
|
|
22
|
-
);
|
|
23
|
-
const identity = new And("identity", [
|
|
24
|
-
new Not(keywords),
|
|
25
|
-
new Not(operator),
|
|
26
|
-
identFirstPart,
|
|
27
|
-
]);
|
|
28
|
-
|
|
29
|
-
const result = identity.parse(new Cursor("_goodName"));
|
|
30
|
-
|
|
31
|
-
expect(result).not.toBeNull();
|
|
32
|
-
|
|
33
|
-
const cursor1 = new Cursor("<badName");
|
|
34
|
-
const result1 = identity.parse(cursor1);
|
|
35
|
-
|
|
36
|
-
expect(result1).toBe(null);
|
|
37
|
-
expect(cursor1.hasUnresolvedError()).toBe(true);
|
|
38
|
-
|
|
39
|
-
const cursor2 = new Cursor("FROM_IS_OKAY");
|
|
40
|
-
const result2 = identity.parse(cursor2);
|
|
41
|
-
|
|
42
|
-
expect(result2).not.toBe(null);
|
|
43
|
-
expect(cursor2.hasUnresolvedError()).toBe(false);
|
|
44
|
-
|
|
45
|
-
const cursor3 = new Cursor("FROM ISBAD");
|
|
46
|
-
const result3 = identity.parse(cursor3);
|
|
47
|
-
|
|
48
|
-
expect(result3).toBe(null);
|
|
49
|
-
expect(cursor3.hasUnresolvedError()).toBe(true);
|
|
50
|
-
});
|
|
51
|
-
});
|
package/src/tests/Or.test.ts
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
/** @jest-environment node */
|
|
2
|
-
import Or from "../patterns/Or";
|
|
3
|
-
import Literal from "../patterns/Literal";
|
|
4
|
-
import Cursor from "../Cursor";
|
|
5
|
-
|
|
6
|
-
describe("Or", () => {
|
|
7
|
-
test("Or: Empty array parser.", () => {
|
|
8
|
-
expect(() => {
|
|
9
|
-
new Or("name", []);
|
|
10
|
-
}).toThrow();
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
test("Or: One Pattern.", () => {
|
|
14
|
-
expect(() => {
|
|
15
|
-
new Or("name", [new Literal("some-value", "")]);
|
|
16
|
-
}).toThrow();
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
test("Or: Name and patterns.", () => {
|
|
20
|
-
const letter = new Literal("a", "a");
|
|
21
|
-
const number = new Literal("1", "1");
|
|
22
|
-
const alphaNumeric = new Or("alpha-numeric", [letter, number]);
|
|
23
|
-
|
|
24
|
-
const letterCursor = new Cursor("a");
|
|
25
|
-
const numberCursor = new Cursor("1");
|
|
26
|
-
|
|
27
|
-
const letterNode = alphaNumeric.parse(letterCursor);
|
|
28
|
-
const numberNode = alphaNumeric.parse(numberCursor);
|
|
29
|
-
|
|
30
|
-
expect(letterNode?.name).toBe("alpha-numeric");
|
|
31
|
-
expect(letterNode?.value).toBe("a");
|
|
32
|
-
|
|
33
|
-
expect(numberNode?.name).toBe("alpha-numeric");
|
|
34
|
-
expect(numberNode?.value).toBe("1");
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
test("Or: Fail.", () => {
|
|
38
|
-
const letter = new Literal("a", "a");
|
|
39
|
-
const number = new Literal("1", "1");
|
|
40
|
-
const alphaNumeric = new Or("alpha-numeric", [letter, number]);
|
|
41
|
-
|
|
42
|
-
const letterCursor = new Cursor("b");
|
|
43
|
-
const numberCursor = new Cursor("2");
|
|
44
|
-
|
|
45
|
-
const letterNode = alphaNumeric.parse(letterCursor);
|
|
46
|
-
const numberNode = alphaNumeric.parse(numberCursor);
|
|
47
|
-
|
|
48
|
-
expect(letterNode).toBe(null);
|
|
49
|
-
expect(numberNode).toBe(null);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
test("Or: Clone.", () => {
|
|
53
|
-
const letter = new Literal("a", "a");
|
|
54
|
-
const number = new Literal("1", "1");
|
|
55
|
-
const alphaNumeric = new Or("alpha-numeric", [letter, number]);
|
|
56
|
-
|
|
57
|
-
const clone = alphaNumeric.clone();
|
|
58
|
-
|
|
59
|
-
expect(alphaNumeric.children.length).toBe(clone.children.length);
|
|
60
|
-
expect(alphaNumeric.name).toBe(clone.name);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
test("Or: Invalid patterns.", () => {
|
|
64
|
-
expect(() => {
|
|
65
|
-
new (Or as any)("some-alpha-numeric", [{}, null]);
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
test("Or: Not enough patterns.", () => {
|
|
70
|
-
expect(() => {
|
|
71
|
-
new (Or as any)("some-alpha-numeric", [{}]);
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
test("Or: Bad name.", () => {
|
|
76
|
-
expect(() => {
|
|
77
|
-
new (Or as any)({}, [new Literal("a", "a"), new Literal("a", "a")]);
|
|
78
|
-
});
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
test("Or: Bad cursor.", () => {
|
|
82
|
-
expect(() => {
|
|
83
|
-
new (Or as any)("A", [
|
|
84
|
-
new Literal("a", "a"),
|
|
85
|
-
new Literal("a", "a"),
|
|
86
|
-
]).parse();
|
|
87
|
-
});
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
test("Or: Furthest Parse Error.", () => {
|
|
91
|
-
const longer = new Literal("longer", "Longer");
|
|
92
|
-
const bang = new Literal("bang", "Bang");
|
|
93
|
-
const orValue = new (Or as any)("test", [longer, bang]);
|
|
94
|
-
const cursor = new Cursor("Longed");
|
|
95
|
-
|
|
96
|
-
orValue.parse(cursor);
|
|
97
|
-
|
|
98
|
-
expect(cursor.getIndex()).toBe(0);
|
|
99
|
-
expect(cursor.hasUnresolvedError()).toBe(true);
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
test("Or: Last pattern matches.", () => {
|
|
103
|
-
const longer = new Literal("longer", "Longer");
|
|
104
|
-
const bang = new Literal("bang", "Bang");
|
|
105
|
-
const orValue = new (Or as any)("test", [longer, bang]);
|
|
106
|
-
const cursor = new Cursor("Bang");
|
|
107
|
-
|
|
108
|
-
const node = orValue.parse(cursor);
|
|
109
|
-
|
|
110
|
-
expect(node.name).toBe("test");
|
|
111
|
-
expect(node.value).toBe("Bang");
|
|
112
|
-
});
|
|
113
|
-
});
|