clarity-pattern-parser 3.0.16 → 4.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 +0 -191
- package/dist/ast/Node.d.ts +4 -5
- package/dist/index.browser.js +476 -731
- package/dist/index.browser.js.map +1 -1
- package/dist/index.d.ts +10 -18
- package/dist/index.esm.js +469 -716
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +476 -731
- package/dist/index.js.map +1 -1
- package/dist/patterns/And.d.ts +24 -0
- package/dist/patterns/Literal.d.ts +19 -0
- package/dist/patterns/LookAhead.d.ts +8 -0
- package/dist/patterns/Not.d.ts +11 -0
- package/dist/patterns/Or.d.ts +22 -0
- package/dist/patterns/Pattern.d.ts +6 -7
- package/dist/patterns/{RecursivePattern.d.ts → Recursive.d.ts} +4 -4
- package/dist/patterns/Reference.d.ts +14 -0
- package/dist/patterns/Regex.d.ts +21 -0
- package/dist/patterns/Repeat.d.ts +20 -0
- package/package.json +1 -1
- package/src/CursorHistory.ts +1 -1
- package/src/ast/Node.ts +20 -17
- package/src/ast/Visitor.ts +14 -18
- package/src/index.ts +17 -33
- package/src/patterns/And.ts +178 -0
- package/src/patterns/Literal.ts +91 -0
- package/src/patterns/Not.ts +50 -0
- package/src/patterns/Or.ts +132 -0
- package/src/patterns/Pattern.ts +31 -47
- package/src/patterns/Recursive.ts +92 -0
- package/src/patterns/{ReferencePattern.ts → Reference.ts} +32 -28
- package/src/patterns/Regex.ts +123 -0
- package/src/patterns/Repeat.ts +155 -0
- package/src/tests/{AndValue.test.ts → And.test.ts} +31 -41
- package/src/tests/CursorHistory.test.ts +6 -6
- package/src/tests/Cusor.test.ts +7 -10
- package/src/tests/Literal.test.ts +3 -5
- package/src/tests/LookAhead.test.ts +2 -51
- package/src/tests/Not.test.ts +51 -0
- package/src/tests/Or.test.ts +113 -0
- package/src/tests/Pattern.test.ts +40 -139
- package/src/tests/{RecursivePattern.test.ts → Recursive.test.ts} +10 -8
- package/src/tests/Reference.test.ts +16 -0
- package/src/tests/{RepeatValue.test.ts → Repeat.test.ts} +10 -42
- package/src/tests/TextSuggester.test.ts +20 -28
- package/src/tests/{NodeVisitor.test.ts → Visitor.test.ts} +42 -21
- package/src/tests/cssPatterns/cssValue.ts +2 -2
- package/src/tests/cssPatterns/divider.ts +2 -2
- package/src/tests/cssPatterns/hex.ts +2 -2
- package/src/tests/cssPatterns/method.ts +7 -9
- package/src/tests/cssPatterns/name.ts +2 -2
- package/src/tests/cssPatterns/number.ts +2 -2
- package/src/tests/cssPatterns/optionalSpaces.ts +1 -2
- package/src/tests/cssPatterns/spaces.ts +2 -2
- package/src/tests/cssPatterns/unit.ts +3 -3
- package/src/tests/cssPatterns/value.ts +2 -2
- package/src/tests/cssPatterns/values.ts +2 -2
- package/src/tests/htmlPatterns/element.ts +18 -33
- package/src/tests/javascriptPatterns/boolean.ts +2 -3
- package/src/tests/javascriptPatterns/json.ts +14 -26
- package/src/tests/javascriptPatterns/name.ts +3 -20
- package/src/tests/javascriptPatterns/number.ts +2 -2
- package/src/tests/javascriptPatterns/objectLiteral.ts +9 -16
- package/src/tests/javascriptPatterns/string.ts +26 -24
- package/src/tests/javascriptPatterns/unit.ts +3 -6
- package/src/tests/javascriptPatterns/whitespace.ts +8 -12
- package/src/tests/naturalLanguage/filter.ts +16 -33
- package/src/tests/patterns/sentence.ts +8 -8
- package/dist/Cursor.js +0 -105
- package/dist/Cursor.js.map +0 -1
- package/dist/CursorHistory.js +0 -104
- package/dist/CursorHistory.js.map +0 -1
- package/dist/Permutor.d.ts +0 -13
- package/dist/Permutor.js +0 -52
- package/dist/Permutor.js.map +0 -1
- package/dist/TextSuggester.js +0 -244
- package/dist/TextSuggester.js.map +0 -1
- package/dist/ast/CompositeNode.d.ts +0 -6
- package/dist/ast/CompositeNode.js +0 -17
- package/dist/ast/CompositeNode.js.map +0 -1
- package/dist/ast/Node.js +0 -16
- package/dist/ast/Node.js.map +0 -1
- package/dist/ast/NodeVisitor.d.ts +0 -31
- package/dist/ast/ValueNode.d.ts +0 -6
- package/dist/ast/ValueNode.js +0 -14
- package/dist/ast/ValueNode.js.map +0 -1
- package/dist/ast/Visitor.js +0 -209
- package/dist/ast/Visitor.js.map +0 -1
- package/dist/patterns/ParseError.js +0 -9
- package/dist/patterns/ParseError.js.map +0 -1
- package/dist/patterns/Pattern.js +0 -127
- package/dist/patterns/Pattern.js.map +0 -1
- package/dist/patterns/RecursivePattern.js +0 -65
- package/dist/patterns/RecursivePattern.js.map +0 -1
- package/dist/patterns/composite/AndComposite.d.ts +0 -22
- package/dist/patterns/composite/AndComposite.js +0 -117
- package/dist/patterns/composite/AndComposite.js.map +0 -1
- package/dist/patterns/composite/CompositePattern.d.ts +0 -4
- package/dist/patterns/composite/CompositePattern.js +0 -7
- package/dist/patterns/composite/CompositePattern.js.map +0 -1
- package/dist/patterns/composite/OptionalComposite.d.ts +0 -10
- package/dist/patterns/composite/OptionalComposite.js +0 -29
- package/dist/patterns/composite/OptionalComposite.js.map +0 -1
- package/dist/patterns/composite/OrComposite.d.ts +0 -16
- package/dist/patterns/composite/OrComposite.js +0 -69
- package/dist/patterns/composite/OrComposite.js.map +0 -1
- package/dist/patterns/composite/RepeatComposite.d.ts +0 -21
- package/dist/patterns/composite/RepeatComposite.js +0 -88
- package/dist/patterns/composite/RepeatComposite.js.map +0 -1
- package/dist/patterns/value/AndValue.d.ts +0 -21
- package/dist/patterns/value/AndValue.js +0 -118
- package/dist/patterns/value/AndValue.js.map +0 -1
- package/dist/patterns/value/AnyOfThese.d.ts +0 -18
- package/dist/patterns/value/AnyOfThese.js +0 -59
- package/dist/patterns/value/AnyOfThese.js.map +0 -1
- package/dist/patterns/value/Literal.d.ts +0 -20
- package/dist/patterns/value/Literal.js +0 -63
- package/dist/patterns/value/Literal.js.map +0 -1
- package/dist/patterns/value/NotValue.d.ts +0 -17
- package/dist/patterns/value/NotValue.js +0 -70
- package/dist/patterns/value/NotValue.js.map +0 -1
- package/dist/patterns/value/OptionalValue.d.ts +0 -9
- package/dist/patterns/value/OptionalValue.js +0 -32
- package/dist/patterns/value/OptionalValue.js.map +0 -1
- package/dist/patterns/value/OrValue.d.ts +0 -19
- package/dist/patterns/value/OrValue.js +0 -73
- package/dist/patterns/value/OrValue.js.map +0 -1
- package/dist/patterns/value/RegexValue.d.ts +0 -19
- package/dist/patterns/value/RegexValue.js +0 -69
- package/dist/patterns/value/RegexValue.js.map +0 -1
- package/dist/patterns/value/RepeatValue.d.ts +0 -19
- package/dist/patterns/value/RepeatValue.js +0 -89
- package/dist/patterns/value/RepeatValue.js.map +0 -1
- package/dist/patterns/value/ValuePattern.d.ts +0 -5
- package/dist/patterns/value/ValuePattern.js +0 -7
- package/dist/patterns/value/ValuePattern.js.map +0 -1
- package/src/Permutor.ts +0 -64
- package/src/ast/CompositeNode.ts +0 -26
- package/src/ast/ValueNode.ts +0 -28
- package/src/patterns/RecursivePattern.ts +0 -86
- package/src/patterns/composite/AndComposite.ts +0 -159
- package/src/patterns/composite/CompositePattern.ts +0 -7
- package/src/patterns/composite/OptionalComposite.ts +0 -37
- package/src/patterns/composite/OrComposite.ts +0 -96
- package/src/patterns/composite/RepeatComposite.ts +0 -130
- package/src/patterns/value/AndValue.ts +0 -152
- package/src/patterns/value/AnyOfThese.ts +0 -81
- package/src/patterns/value/Literal.ts +0 -92
- package/src/patterns/value/NotValue.ts +0 -95
- package/src/patterns/value/OptionalValue.ts +0 -39
- package/src/patterns/value/OrValue.ts +0 -103
- package/src/patterns/value/RegexValue.ts +0 -103
- package/src/patterns/value/RepeatValue.ts +0 -131
- package/src/patterns/value/ValuePattern.ts +0 -8
- package/src/tests/AndComposite.test.ts +0 -102
- package/src/tests/AnyOfThese.test.ts +0 -74
- package/src/tests/CompositeNode.test.ts +0 -33
- package/src/tests/NotValue.test.ts +0 -76
- package/src/tests/OptionalValue.test.ts +0 -50
- package/src/tests/OrComposite.test.ts +0 -75
- package/src/tests/OrValue.test.ts +0 -171
- package/src/tests/Permutor.test.ts +0 -30
- package/src/tests/ReferencePattern.test.ts +0 -24
- package/src/tests/RegexValue.test.ts +0 -22
- package/src/tests/RepeatComposite.test.ts +0 -58
- package/src/tests/ValueNode.test.ts +0 -24
- package/src/tests/javascriptPatterns/varStatement.ts +0 -0
- package/src/tests/readmeDemo.test.ts +0 -124
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
/** @jest-environment node */
|
|
2
|
-
import AnyOfThese from "../patterns/value/AnyOfThese";
|
|
3
|
-
import Cursor from "../Cursor";
|
|
4
|
-
|
|
5
|
-
describe("AnyOfThese", () => {
|
|
6
|
-
test("Empty string provided as characters.", () => {
|
|
7
|
-
expect(() => {
|
|
8
|
-
new AnyOfThese("no-characters", "");
|
|
9
|
-
}).toThrow();
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
test("Single character.", () => {
|
|
13
|
-
const lowerCaseA = new AnyOfThese("lower-case-a", "a");
|
|
14
|
-
const cursor = new Cursor("a");
|
|
15
|
-
const node = lowerCaseA.parse(cursor);
|
|
16
|
-
|
|
17
|
-
expect(node?.name).toBe("lower-case-a");
|
|
18
|
-
expect(node?.value).toBe("a");
|
|
19
|
-
expect(node?.startIndex).toBe(0);
|
|
20
|
-
expect(node?.endIndex).toBe(0);
|
|
21
|
-
expect(cursor.isAtEnd()).toBe(true);
|
|
22
|
-
expect(cursor.getChar()).toBe("a");
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
test("Uppercase A and lowercase A.", () => {
|
|
26
|
-
const letterA = new AnyOfThese("letter-a", "Aa");
|
|
27
|
-
const lowerCaseCursor = new Cursor("a");
|
|
28
|
-
const upperCaseCursor = new Cursor("A");
|
|
29
|
-
const lowerCaseNode = letterA.parse(lowerCaseCursor);
|
|
30
|
-
const upperCaseNode = letterA.parse(upperCaseCursor);
|
|
31
|
-
|
|
32
|
-
expect(lowerCaseNode?.name).toBe("letter-a");
|
|
33
|
-
expect(lowerCaseNode?.value).toBe("a");
|
|
34
|
-
expect(lowerCaseNode?.startIndex).toBe(0);
|
|
35
|
-
expect(lowerCaseNode?.endIndex).toBe(0);
|
|
36
|
-
|
|
37
|
-
expect(upperCaseNode?.name).toBe("letter-a");
|
|
38
|
-
expect(upperCaseNode?.value).toBe("A");
|
|
39
|
-
expect(upperCaseNode?.startIndex).toBe(0);
|
|
40
|
-
expect(upperCaseNode?.endIndex).toBe(0);
|
|
41
|
-
|
|
42
|
-
expect(upperCaseCursor.getChar()).toBe("A");
|
|
43
|
-
expect(upperCaseCursor.isAtEnd()).toBe(true);
|
|
44
|
-
|
|
45
|
-
expect(lowerCaseCursor.getChar()).toBe("a");
|
|
46
|
-
expect(lowerCaseCursor.isAtEnd()).toBe(true);
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
test("Match with long cursor.", () => {
|
|
50
|
-
const letterA = new AnyOfThese("letter-a", "Aa");
|
|
51
|
-
const cursor = new Cursor("a12345");
|
|
52
|
-
const node = letterA.parse(cursor);
|
|
53
|
-
|
|
54
|
-
expect(node?.name).toBe("letter-a");
|
|
55
|
-
expect(node?.value).toBe("a");
|
|
56
|
-
expect(cursor.getChar()).toBe("a");
|
|
57
|
-
expect(cursor.getIndex()).toBe(0);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
test("No match.", () => {
|
|
61
|
-
const letterA = new AnyOfThese("letter-a", "Aa");
|
|
62
|
-
const cursor = new Cursor("12345");
|
|
63
|
-
const node = letterA.parse(cursor);
|
|
64
|
-
|
|
65
|
-
expect(node).toBe(null);
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
test("Pattern Methods.", () => {
|
|
69
|
-
const letterA = new AnyOfThese("letter-a", "Aa");
|
|
70
|
-
|
|
71
|
-
expect(letterA.name).toBe("letter-a");
|
|
72
|
-
expect(letterA.children.length).toBe(0);
|
|
73
|
-
});
|
|
74
|
-
});
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
/** @jest-environment node */
|
|
2
|
-
import CompositeNode from "../ast/CompositeNode";
|
|
3
|
-
import { ValueNode } from "../index";
|
|
4
|
-
|
|
5
|
-
describe("CompositeNode", () => {
|
|
6
|
-
test("CompositeNode: clone", () => {
|
|
7
|
-
const node = new CompositeNode("type", "name", 0, 0);
|
|
8
|
-
const valueNode = new ValueNode("value-type", "value-name", "t", 0, 0);
|
|
9
|
-
|
|
10
|
-
node.children.push(valueNode);
|
|
11
|
-
|
|
12
|
-
const clone = node.clone();
|
|
13
|
-
|
|
14
|
-
expect(clone.type).toBe("type");
|
|
15
|
-
expect(clone.name).toBe("name");
|
|
16
|
-
expect(clone.children.length).toBe(1);
|
|
17
|
-
expect(clone.children[0].type).toBe("value-type");
|
|
18
|
-
expect(clone.children[0].name).toBe("value-name");
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
test("CompositeNode: constructor without default indexes.", () => {
|
|
22
|
-
const node = new CompositeNode("type", "name");
|
|
23
|
-
const valueNode = new ValueNode("value-type", "value-name", "t", 0, 0);
|
|
24
|
-
|
|
25
|
-
node.children.push(valueNode);
|
|
26
|
-
|
|
27
|
-
expect(node.type).toBe("type");
|
|
28
|
-
expect(node.name).toBe("name");
|
|
29
|
-
expect(node.children.length).toBe(1);
|
|
30
|
-
expect(node.children[0].type).toBe("value-type");
|
|
31
|
-
expect(node.children[0].name).toBe("value-name");
|
|
32
|
-
});
|
|
33
|
-
});
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
/** @jest-environment node */
|
|
2
|
-
import NotValue from "../patterns/value/NotValue";
|
|
3
|
-
import Literal from "../patterns/value/Literal";
|
|
4
|
-
import Cursor from "../Cursor";
|
|
5
|
-
import OrValue from "../patterns/value/OrValue";
|
|
6
|
-
import AndValue from "../patterns/value/AndValue";
|
|
7
|
-
import RepeatValue from "../patterns/value/RepeatValue";
|
|
8
|
-
import RegexValue from "../patterns/value/RegexValue";
|
|
9
|
-
|
|
10
|
-
describe("NotValue", () => {
|
|
11
|
-
test("Empty Constructor.", () => {
|
|
12
|
-
expect(() => {
|
|
13
|
-
new (NotValue as any)();
|
|
14
|
-
}).toThrow();
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
test("Invalid name.", () => {
|
|
18
|
-
expect(() => {
|
|
19
|
-
new (NotValue as any)([], new Literal("blah", "Blah"));
|
|
20
|
-
}).toThrow();
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
test("No patterns", () => {
|
|
24
|
-
expect(() => {
|
|
25
|
-
new (NotValue as any)("and-value");
|
|
26
|
-
}).toThrow();
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
test("Empty patterns", () => {
|
|
30
|
-
expect(() => {
|
|
31
|
-
new (NotValue as any)("and-value", null);
|
|
32
|
-
}).toThrow();
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
test("Invalid patterns", () => {
|
|
36
|
-
expect(() => {
|
|
37
|
-
new (NotValue as any)("and-value", {});
|
|
38
|
-
}).toThrow();
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
test("No Match", () => {
|
|
42
|
-
const john = new Literal("john", "John");
|
|
43
|
-
const notJohn = new NotValue("not-john", john);
|
|
44
|
-
const cursor = new Cursor("John");
|
|
45
|
-
|
|
46
|
-
notJohn.parse(cursor);
|
|
47
|
-
expect(cursor.hasUnresolvedError()).toBe(true);
|
|
48
|
-
expect(cursor.parseError?.message).toBe(
|
|
49
|
-
"Didn't find any characters that didn't match the john pattern."
|
|
50
|
-
);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
test("Success", () => {
|
|
54
|
-
const john = new Literal("john", "John");
|
|
55
|
-
const notJohn = new NotValue("not-john", john);
|
|
56
|
-
const cursor = new Cursor("Jane");
|
|
57
|
-
const node = notJohn.parse(cursor);
|
|
58
|
-
|
|
59
|
-
expect(node?.name).toBe("not-john");
|
|
60
|
-
expect(node?.value).toBe("J");
|
|
61
|
-
expect(node?.startIndex).toBe(0);
|
|
62
|
-
expect(node?.endIndex).toBe(0);
|
|
63
|
-
expect(cursor.getIndex()).toBe(0);
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
test("Clone.", () => {
|
|
67
|
-
const john = new Literal("john", "John");
|
|
68
|
-
const notJohn = new NotValue("not-john", john);
|
|
69
|
-
const clone = notJohn.clone();
|
|
70
|
-
|
|
71
|
-
expect(notJohn.name).toBe(clone.name);
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
});
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
/** @jest-environment node */
|
|
2
|
-
import Literal from "../patterns/value/Literal";
|
|
3
|
-
import OptionalValue from "../patterns/value/OptionalValue";
|
|
4
|
-
import Cursor from "../Cursor";
|
|
5
|
-
|
|
6
|
-
describe("OptionalValue", () => {
|
|
7
|
-
test("Empty constructor.", () => {
|
|
8
|
-
expect(() => {
|
|
9
|
-
new (OptionalValue as any)();
|
|
10
|
-
}).toThrow();
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
test("Empty pattern.", () => {
|
|
14
|
-
expect(() => {
|
|
15
|
-
new (OptionalValue as any)();
|
|
16
|
-
}).toThrow();
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
test("Invalid pattern.", () => {
|
|
20
|
-
expect(() => {
|
|
21
|
-
new (OptionalValue as any)({});
|
|
22
|
-
}).toThrow();
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
test("Match pattern.", () => {
|
|
26
|
-
const john = new Literal("john", "John");
|
|
27
|
-
const optionalValue = new OptionalValue(john);
|
|
28
|
-
const cursor = new Cursor("John");
|
|
29
|
-
const node = optionalValue.parse(cursor);
|
|
30
|
-
|
|
31
|
-
expect(node?.name).toBe("john");
|
|
32
|
-
expect(node?.value).toBe("John");
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
test("No Match pattern.", () => {
|
|
36
|
-
const john = new Literal("john", "John");
|
|
37
|
-
const optionalValue = new OptionalValue(john);
|
|
38
|
-
const cursor = new Cursor("Jane");
|
|
39
|
-
const node = optionalValue.parse(cursor);
|
|
40
|
-
|
|
41
|
-
expect(node).toBe(null);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
test("Name", () => {
|
|
45
|
-
const john = new Literal("john", "John");
|
|
46
|
-
const optionalValue = new OptionalValue(john);
|
|
47
|
-
|
|
48
|
-
expect(optionalValue.name).toBe("optional-value");
|
|
49
|
-
});
|
|
50
|
-
});
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
/** @jest-environment node */
|
|
2
|
-
import OrComposite from "../patterns/composite/OrComposite";
|
|
3
|
-
import Literal from "../patterns/value/Literal";
|
|
4
|
-
import OptionalValue from "../patterns/value/OptionalValue";
|
|
5
|
-
import { Cursor } from "../index";
|
|
6
|
-
|
|
7
|
-
describe("OrComposite", ()=>{
|
|
8
|
-
test("Match.", () => {
|
|
9
|
-
const john = new Literal("john", "John");
|
|
10
|
-
const jane = new Literal("jane", "Jane");
|
|
11
|
-
const cursor = new Cursor("John");
|
|
12
|
-
const name = new OrComposite("name", [john, jane]);
|
|
13
|
-
|
|
14
|
-
const node = name.parse(cursor);
|
|
15
|
-
|
|
16
|
-
expect(node.name).toBe("john");
|
|
17
|
-
expect(node.value).toBe("John");
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
test("No Match", () => {
|
|
21
|
-
const john = new Literal("john", "John");
|
|
22
|
-
const jane = new Literal("jane", "Jane");
|
|
23
|
-
const cursor = new Cursor("Jeffrey");
|
|
24
|
-
const name = new OrComposite("name", [john, jane]);
|
|
25
|
-
|
|
26
|
-
const node = name.parse(cursor);
|
|
27
|
-
|
|
28
|
-
expect(node).toBe(null);
|
|
29
|
-
expect(cursor.getIndex()).toBe(0);
|
|
30
|
-
expect(cursor.hasUnresolvedError()).toBe(true);
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
test("Supplied only one option.", () => {
|
|
34
|
-
const john = new Literal("john", "John");
|
|
35
|
-
|
|
36
|
-
expect(() => {
|
|
37
|
-
new OrComposite("name", [john]);
|
|
38
|
-
}).toThrow();
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
test("Optional Children.", () => {
|
|
42
|
-
const john = new Literal("john", "John");
|
|
43
|
-
const jane = new Literal("jane", "Jane");
|
|
44
|
-
|
|
45
|
-
expect(() => {
|
|
46
|
-
new OrComposite("name", [new OptionalValue(john), new OptionalValue(jane)]);
|
|
47
|
-
}).toThrow();
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
test("parse with null cursor.", () => {
|
|
51
|
-
const john = new Literal("john", "John");
|
|
52
|
-
const jane = new Literal("jane", "Jane");
|
|
53
|
-
const cursor = new Cursor("John");
|
|
54
|
-
const name = new OrComposite("name", [john, jane]);
|
|
55
|
-
|
|
56
|
-
const node = name.parse(cursor);
|
|
57
|
-
|
|
58
|
-
expect(node.name).toBe("john");
|
|
59
|
-
expect(node.value).toBe("John");
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
test("clone.", () => {
|
|
63
|
-
const john = new Literal("john", "John");
|
|
64
|
-
const jane = new Literal("jane", "Jane");
|
|
65
|
-
|
|
66
|
-
const name = new OrComposite("name", [john, jane]);
|
|
67
|
-
const clone = name.clone("name2");
|
|
68
|
-
|
|
69
|
-
expect(clone.name).toBe("name2");
|
|
70
|
-
expect(clone.children.length).toBe(2);
|
|
71
|
-
expect(clone.children[0].name).toBe("john");
|
|
72
|
-
expect(clone.children[1].name).toBe("jane");
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
});
|
|
@@ -1,171 +0,0 @@
|
|
|
1
|
-
/** @jest-environment node */
|
|
2
|
-
import OrValue from "../patterns/value/OrValue";
|
|
3
|
-
import AnyOfThese from "../patterns/value/AnyOfThese";
|
|
4
|
-
import Literal from "../patterns/value/Literal";
|
|
5
|
-
import Cursor from "../Cursor";
|
|
6
|
-
|
|
7
|
-
describe("OrValue", () => {
|
|
8
|
-
test("OrValue: Empty constructor.", () => {
|
|
9
|
-
expect(() => {
|
|
10
|
-
new (OrValue as any)();
|
|
11
|
-
}).toThrow();
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
test("OrValue: Undefined parser.", () => {
|
|
15
|
-
expect(() => {
|
|
16
|
-
new (OrValue as any)("name");
|
|
17
|
-
}).toThrow();
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
test("OrValue: Null patterns.", () => {
|
|
21
|
-
expect(() => {
|
|
22
|
-
new (OrValue as any)("name", null);
|
|
23
|
-
}).toThrow();
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
test("OrValue: Empty array parser.", () => {
|
|
27
|
-
expect(() => {
|
|
28
|
-
new (OrValue as any)("name", []);
|
|
29
|
-
}).toThrow();
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
test("OrValue: One parser.", () => {
|
|
33
|
-
expect(() => {
|
|
34
|
-
new (OrValue as any)("name", [new Literal("some-value", "")]);
|
|
35
|
-
}).toThrow();
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
test("OrValue: Name and patterns.", () => {
|
|
39
|
-
const letter = new AnyOfThese(
|
|
40
|
-
"letter",
|
|
41
|
-
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
42
|
-
);
|
|
43
|
-
const number = new AnyOfThese("number", "0987654321");
|
|
44
|
-
const alphaNumeric = new (OrValue as any)("alpha-numeric", [
|
|
45
|
-
letter,
|
|
46
|
-
number,
|
|
47
|
-
]);
|
|
48
|
-
|
|
49
|
-
const letterCursor = new Cursor("a");
|
|
50
|
-
const numberCursor = new Cursor("1");
|
|
51
|
-
|
|
52
|
-
const letterNode = alphaNumeric.parse(letterCursor);
|
|
53
|
-
const numberNode = alphaNumeric.parse(numberCursor);
|
|
54
|
-
|
|
55
|
-
expect(letterNode.name).toBe("alpha-numeric");
|
|
56
|
-
expect(letterNode.value).toBe("a");
|
|
57
|
-
|
|
58
|
-
expect(numberNode.name).toBe("alpha-numeric");
|
|
59
|
-
expect(numberNode.value).toBe("1");
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
test("OrValue: Optional Pattern as one of the patterns.", () => {
|
|
63
|
-
const letter = new AnyOfThese(
|
|
64
|
-
"letter",
|
|
65
|
-
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
66
|
-
);
|
|
67
|
-
const number = new AnyOfThese("number", "0987654321");
|
|
68
|
-
const alphaNumeric = new (OrValue as any)("alpha-numeric", [
|
|
69
|
-
letter,
|
|
70
|
-
number,
|
|
71
|
-
]);
|
|
72
|
-
|
|
73
|
-
const letterCursor = new Cursor("a");
|
|
74
|
-
const numberCursor = new Cursor("1");
|
|
75
|
-
|
|
76
|
-
const letterNode = alphaNumeric.parse(letterCursor);
|
|
77
|
-
const numberNode = alphaNumeric.parse(numberCursor);
|
|
78
|
-
|
|
79
|
-
expect(letterNode.name).toBe("alpha-numeric");
|
|
80
|
-
expect(letterNode.value).toBe("a");
|
|
81
|
-
|
|
82
|
-
expect(numberNode.name).toBe("alpha-numeric");
|
|
83
|
-
expect(numberNode.value).toBe("1");
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
test("OrValue: Fail.", () => {
|
|
87
|
-
const letter = new AnyOfThese("some-letter", "abc");
|
|
88
|
-
const number = new AnyOfThese("some-number", "123");
|
|
89
|
-
const alphaNumeric = new (OrValue as any)("some-alpha-numeric", [
|
|
90
|
-
letter,
|
|
91
|
-
number,
|
|
92
|
-
]);
|
|
93
|
-
|
|
94
|
-
const letterCursor = new Cursor("d");
|
|
95
|
-
const numberCursor = new Cursor("4");
|
|
96
|
-
|
|
97
|
-
const letterNode = alphaNumeric.parse(letterCursor);
|
|
98
|
-
const numberNode = alphaNumeric.parse(numberCursor);
|
|
99
|
-
|
|
100
|
-
expect(letterCursor.getIndex()).toBe(0);
|
|
101
|
-
expect(letterCursor.hasUnresolvedError()).toBe(true);
|
|
102
|
-
|
|
103
|
-
expect(numberCursor.getIndex()).toBe(0);
|
|
104
|
-
expect(numberCursor.hasUnresolvedError()).toBe(true);
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
test("OrValue: Clone.", () => {
|
|
108
|
-
const letter = new AnyOfThese("some-letter", "abc");
|
|
109
|
-
const number = new AnyOfThese("some-number", "123");
|
|
110
|
-
const alphaNumeric = new (OrValue as any)("some-alpha-numeric", [
|
|
111
|
-
letter,
|
|
112
|
-
number,
|
|
113
|
-
]);
|
|
114
|
-
|
|
115
|
-
const clone = alphaNumeric.clone();
|
|
116
|
-
|
|
117
|
-
expect(alphaNumeric.children.length).toBe(clone.children.length);
|
|
118
|
-
expect(alphaNumeric.name).toBe(clone.name);
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
test("OrValue: Invalid patterns.", () => {
|
|
122
|
-
expect(() => {
|
|
123
|
-
new (OrValue as any)("some-alpha-numeric", [{}, null]);
|
|
124
|
-
});
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
test("OrValue: Not enough patterns.", () => {
|
|
128
|
-
expect(() => {
|
|
129
|
-
new (OrValue as any)("some-alpha-numeric", [{}]);
|
|
130
|
-
});
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
test("OrValue: Bad name.", () => {
|
|
134
|
-
expect(() => {
|
|
135
|
-
new (OrValue as any)({}, [new Literal("a", "a"), new Literal("a", "a")]);
|
|
136
|
-
});
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
test("OrValue: Bad cursor.", () => {
|
|
140
|
-
expect(() => {
|
|
141
|
-
new (OrValue as any)("A", [
|
|
142
|
-
new Literal("a", "a"),
|
|
143
|
-
new Literal("a", "a"),
|
|
144
|
-
]).parse();
|
|
145
|
-
});
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
test("OrValue: Furthest Parse Error.", () => {
|
|
149
|
-
const longer = new Literal("longer", "Longer");
|
|
150
|
-
const bang = new Literal("bang", "Bang");
|
|
151
|
-
const orValue = new (OrValue as any)("test", [longer, bang]);
|
|
152
|
-
const cursor = new Cursor("Longed");
|
|
153
|
-
|
|
154
|
-
orValue.parse(cursor);
|
|
155
|
-
|
|
156
|
-
expect(cursor.getIndex()).toBe(0);
|
|
157
|
-
expect(cursor.hasUnresolvedError()).toBe(true);
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
test("OrValue: Last pattern matches.", () => {
|
|
161
|
-
const longer = new Literal("longer", "Longer");
|
|
162
|
-
const bang = new Literal("bang", "Bang");
|
|
163
|
-
const orValue = new (OrValue as any)("test", [longer, bang]);
|
|
164
|
-
const cursor = new Cursor("Bang");
|
|
165
|
-
|
|
166
|
-
const node = orValue.parse(cursor);
|
|
167
|
-
|
|
168
|
-
expect(node.name).toBe("test");
|
|
169
|
-
expect(node.value).toBe("Bang");
|
|
170
|
-
});
|
|
171
|
-
});
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/** @jest-environment node */
|
|
2
|
-
import Permutor from "../Permutor";
|
|
3
|
-
|
|
4
|
-
describe("Permutor", () => {
|
|
5
|
-
test("Permutor: simple.", () => {
|
|
6
|
-
const permutor = new Permutor();
|
|
7
|
-
const array = [
|
|
8
|
-
[0, 1],
|
|
9
|
-
[0, 1],
|
|
10
|
-
[0, 1],
|
|
11
|
-
];
|
|
12
|
-
|
|
13
|
-
const result = permutor.permute(array);
|
|
14
|
-
expect(result.join(",")).toBe("000,001,010,011,100,101,110,111");
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
test("Permutor: larger.", () => {
|
|
18
|
-
const permutor = new Permutor();
|
|
19
|
-
const array = [
|
|
20
|
-
[0, 1, 2],
|
|
21
|
-
[0, 1],
|
|
22
|
-
[0, 1, 2, 3],
|
|
23
|
-
];
|
|
24
|
-
|
|
25
|
-
const result = permutor.permute(array);
|
|
26
|
-
expect(result.join(",")).toBe(
|
|
27
|
-
"000,001,002,003,010,011,012,013,100,101,102,103,110,111,112,113,200,201,202,203,210,211,212,213"
|
|
28
|
-
);
|
|
29
|
-
});
|
|
30
|
-
});
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import Cursor from "../Cursor";
|
|
2
|
-
import ReferencePattern from "../patterns/ReferencePattern";
|
|
3
|
-
import AndValue from "../patterns/value/AndValue";
|
|
4
|
-
import Literal from "../patterns/value/Literal";
|
|
5
|
-
import OrValue from "../patterns/value/OrValue";
|
|
6
|
-
|
|
7
|
-
describe("ReferencePattern", () => {
|
|
8
|
-
test("Reference to node in different branch.", () => {
|
|
9
|
-
const a = new Literal("a", "a");
|
|
10
|
-
const b = new Literal("b", "b");
|
|
11
|
-
const c = new Literal("c", "c");
|
|
12
|
-
const a_b_c = new AndValue("a-and-b-and-c", [
|
|
13
|
-
a,
|
|
14
|
-
b,
|
|
15
|
-
new ReferencePattern("c"),
|
|
16
|
-
]);
|
|
17
|
-
const b_c = new AndValue("b-and-c", [b, c]);
|
|
18
|
-
const main = new OrValue("main", [a_b_c, b_c]);
|
|
19
|
-
|
|
20
|
-
const result = main.parse(new Cursor("abc"));
|
|
21
|
-
|
|
22
|
-
expect(result).not.toBeNull();
|
|
23
|
-
});
|
|
24
|
-
});
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
/** @jest-environment node */
|
|
2
|
-
import { RegexValue } from "../index";
|
|
3
|
-
|
|
4
|
-
test("RegexValue: exec.", () => {
|
|
5
|
-
const notA = new RegexValue("not-a", "[^a]+");
|
|
6
|
-
|
|
7
|
-
const result = notA.exec("John");
|
|
8
|
-
const result2 = notA.exec("a");
|
|
9
|
-
|
|
10
|
-
const expectedValue = {
|
|
11
|
-
children: [],
|
|
12
|
-
value: "John",
|
|
13
|
-
type: "regex-value",
|
|
14
|
-
name: "not-a",
|
|
15
|
-
startIndex: 0,
|
|
16
|
-
endIndex: 3,
|
|
17
|
-
isComposite: false,
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
expect(JSON.stringify(result)).toBe(JSON.stringify(expectedValue));
|
|
21
|
-
expect(result2).toBe(null);
|
|
22
|
-
});
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
/** @jest-environment node */
|
|
2
|
-
import RepeatComposite from "../patterns/composite/RepeatComposite";
|
|
3
|
-
import AndComposite from "../patterns/composite/AndComposite";
|
|
4
|
-
import OptionalComposite from "../patterns/composite/OptionalComposite";
|
|
5
|
-
import Literal from "../patterns/value/Literal";
|
|
6
|
-
import Cursor from "../Cursor";
|
|
7
|
-
import OrComposite from "../patterns/composite/OrComposite";
|
|
8
|
-
|
|
9
|
-
describe("RepeatComposite", () => {
|
|
10
|
-
test("Cannot use optional patterns.", () => {
|
|
11
|
-
const firstName = new Literal("John", "John");
|
|
12
|
-
const lastName = new Literal("Doe", "Doe");
|
|
13
|
-
const andComposite = new AndComposite("full-name", [firstName, lastName]);
|
|
14
|
-
|
|
15
|
-
expect(() => {
|
|
16
|
-
new RepeatComposite("full-names", new OptionalComposite(andComposite));
|
|
17
|
-
}).toThrow();
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
test("clone with custom name.", () => {
|
|
21
|
-
const firstName = new Literal("John", "John");
|
|
22
|
-
const lastName = new Literal("Doe", "Doe");
|
|
23
|
-
const andComposite = new AndComposite("full-name", [firstName, lastName]);
|
|
24
|
-
const fullnames = new RepeatComposite("full-names", andComposite);
|
|
25
|
-
const clone = fullnames.clone("full-names-2");
|
|
26
|
-
|
|
27
|
-
expect(clone.name).toBe("full-names-2");
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test("parse.", () => {
|
|
31
|
-
const a = new Literal("a", "A");
|
|
32
|
-
const b = new Literal("b", "B");
|
|
33
|
-
const space = new Literal("space", " ");
|
|
34
|
-
const or = new OrComposite("names", [a, b]);
|
|
35
|
-
|
|
36
|
-
const repeat = new RepeatComposite("repeat", or, space);
|
|
37
|
-
const result = repeat.parse(new Cursor("A B"));
|
|
38
|
-
|
|
39
|
-
expect(result?.children[0].value).toBe("A");
|
|
40
|
-
expect(result?.children[0].name).toBe("a");
|
|
41
|
-
expect(result?.children[1].value).toBe(" ");
|
|
42
|
-
expect(result?.children[1].name).toBe("space");
|
|
43
|
-
expect(result?.children[2].value).toBe("B");
|
|
44
|
-
expect(result?.children[2].name).toBe("b");
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
test("Cannot end on a divider.", () => {
|
|
48
|
-
const a = new Literal("a", "A");
|
|
49
|
-
const b = new Literal("b", "B");
|
|
50
|
-
const space = new Literal("space", " ");
|
|
51
|
-
const or = new OrComposite("names", [a, b]);
|
|
52
|
-
|
|
53
|
-
const repeat = new RepeatComposite("repeat", or, space);
|
|
54
|
-
const result = repeat.parse(new Cursor("A B "));
|
|
55
|
-
|
|
56
|
-
expect(result).toBe(null);
|
|
57
|
-
});
|
|
58
|
-
});
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
/** @jest-environment node */
|
|
2
|
-
import ValueNode from "../ast/ValueNode";
|
|
3
|
-
import assert from "assert";
|
|
4
|
-
|
|
5
|
-
describe("ValueNode", () => {
|
|
6
|
-
test("clone.", () => {
|
|
7
|
-
const node = new ValueNode("type", "name", "value", 0, 1);
|
|
8
|
-
const clone = node.clone();
|
|
9
|
-
|
|
10
|
-
assert.equal(node.name, clone.name);
|
|
11
|
-
assert.equal(node.value, clone.value);
|
|
12
|
-
assert.equal(node.startIndex, clone.startIndex);
|
|
13
|
-
assert.equal(node.endIndex, clone.endIndex);
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
test("Without indexes.", () => {
|
|
17
|
-
const node = new ValueNode("type", "name", "value");
|
|
18
|
-
|
|
19
|
-
assert.equal(node.name, "name");
|
|
20
|
-
assert.equal(node.value, "value");
|
|
21
|
-
assert.equal(node.startIndex, 0);
|
|
22
|
-
assert.equal(node.endIndex, 0);
|
|
23
|
-
});
|
|
24
|
-
});
|
|
File without changes
|