clarity-pattern-parser 3.0.15 → 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/LookAhead.ts +32 -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/Reference.ts +127 -0
- 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 +44 -0
- package/src/tests/Not.test.ts +51 -0
- package/src/tests/Or.test.ts +113 -0
- package/src/tests/Pattern.test.ts +41 -141
- 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 -24
- 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 -153
- 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 -69
- 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/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
package/README.md
CHANGED
|
@@ -5,194 +5,3 @@ npm install clarity-pattern-parser
|
|
|
5
5
|
```
|
|
6
6
|
|
|
7
7
|
## How to use
|
|
8
|
-
|
|
9
|
-
There are two constructs within clarity pattern parser. The first is value based patterns. The second is composite based patterns.
|
|
10
|
-
|
|
11
|
-
Value patterns can be composed, however, they are reduced to a single value even if they are composed of many parts.
|
|
12
|
-
|
|
13
|
-
Composite patterns can also be composed, however, they are left with their parts. The best way to understand this is by an example.
|
|
14
|
-
|
|
15
|
-
We will create a pattern for line ending comments, first with value based patterns, and second with some composite based patterns.
|
|
16
|
-
|
|
17
|
-
### Value based comment
|
|
18
|
-
|
|
19
|
-
```javascript
|
|
20
|
-
import {
|
|
21
|
-
Cursor,
|
|
22
|
-
Literal,
|
|
23
|
-
AndValue,
|
|
24
|
-
OrValue,
|
|
25
|
-
RepeatValue,
|
|
26
|
-
NotValue,
|
|
27
|
-
AndComposite
|
|
28
|
-
} from "clarity-pattern-parser";
|
|
29
|
-
import assert from "assert";
|
|
30
|
-
|
|
31
|
-
exports["readme.md: value"] = () => {
|
|
32
|
-
const forwardSlashes = new Literal("forward-slashes", "//");
|
|
33
|
-
const newLine = new Literal("new-line", "\n");
|
|
34
|
-
const returnCarriage = new Literal("return-carriage", "\r");
|
|
35
|
-
const windowsNewLine = new AndValue("windows-new-line", [
|
|
36
|
-
returnCarriage,
|
|
37
|
-
newLine
|
|
38
|
-
]);
|
|
39
|
-
const lineEnd = new OrValue("line-end", [newLine, windowsNewLine]);
|
|
40
|
-
const character = new NotValue("character", lineEnd);
|
|
41
|
-
const comment = new RepeatValue("comment", character);
|
|
42
|
-
|
|
43
|
-
const lineEndingComment = new AndValue("line-ending-comment", [
|
|
44
|
-
forwardSlashes,
|
|
45
|
-
comment,
|
|
46
|
-
lineEnd
|
|
47
|
-
]);
|
|
48
|
-
|
|
49
|
-
const string = "// This is a comment\n";
|
|
50
|
-
const cursor = new Cursor(string);
|
|
51
|
-
const node = lineEndingComment.parse(cursor);
|
|
52
|
-
|
|
53
|
-
assert.equal(node.name, "line-ending-comment"); // --> true
|
|
54
|
-
assert.equal(node.value, string); // --> true
|
|
55
|
-
|
|
56
|
-
const expectedValue = {
|
|
57
|
-
type: "and-value",
|
|
58
|
-
name: "line-ending-comment",
|
|
59
|
-
startIndex: 0,
|
|
60
|
-
endIndex: 20,
|
|
61
|
-
value: "// This is a comment\n"
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
assert.equal(JSON.stringify(node), JSON.stringify(expectedValue)); // --> true
|
|
65
|
-
};
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
### Composite based comment
|
|
69
|
-
|
|
70
|
-
```javascript
|
|
71
|
-
import {
|
|
72
|
-
Cursor,
|
|
73
|
-
Literal,
|
|
74
|
-
AndValue,
|
|
75
|
-
OrValue,
|
|
76
|
-
RepeatValue,
|
|
77
|
-
NotValue,
|
|
78
|
-
AndComposite
|
|
79
|
-
} from "clarity-pattern-parser";
|
|
80
|
-
import assert from "assert";
|
|
81
|
-
|
|
82
|
-
exports["readme.md: composite"] = () => {
|
|
83
|
-
const forwardSlashes = new Literal("forward-slashes", "//");
|
|
84
|
-
const newLine = new Literal("new-line", "\n");
|
|
85
|
-
const returnCarriage = new Literal("return-carriage", "\r");
|
|
86
|
-
const windowsNewLine = new AndValue("windows-new-line", [
|
|
87
|
-
returnCarriage,
|
|
88
|
-
newLine
|
|
89
|
-
]);
|
|
90
|
-
const lineEnd = new OrValue("line-end", [newLine, windowsNewLine]);
|
|
91
|
-
const character = new NotValue("character", lineEnd);
|
|
92
|
-
const comment = new RepeatValue("comment", character);
|
|
93
|
-
|
|
94
|
-
const lineEndingComment = new AndComposite("line-ending-comment", [
|
|
95
|
-
forwardSlashes,
|
|
96
|
-
comment,
|
|
97
|
-
lineEnd
|
|
98
|
-
]);
|
|
99
|
-
|
|
100
|
-
const string = "// This is a comment\n";
|
|
101
|
-
const cursor = new Cursor(string);
|
|
102
|
-
const node = lineEndingComment.parse(cursor);
|
|
103
|
-
|
|
104
|
-
assert.equal(node.name, "line-ending-comment");
|
|
105
|
-
|
|
106
|
-
assert.equal(node.children[0].name, "forward-slashes");
|
|
107
|
-
assert.equal(node.children[0].value, "//");
|
|
108
|
-
|
|
109
|
-
assert.equal(node.children[1].name, "comment");
|
|
110
|
-
assert.equal(node.children[1].value, " This is a comment");
|
|
111
|
-
|
|
112
|
-
assert.equal(node.children[2].name, "line-end");
|
|
113
|
-
assert.equal(node.children[2].value, "\n");
|
|
114
|
-
|
|
115
|
-
const expectedValue = {
|
|
116
|
-
type: "and-composite",
|
|
117
|
-
name: "line-ending-comment",
|
|
118
|
-
startIndex: 0,
|
|
119
|
-
endIndex: 20,
|
|
120
|
-
children: [
|
|
121
|
-
{
|
|
122
|
-
type: "literal",
|
|
123
|
-
name: "forward-slashes",
|
|
124
|
-
startIndex: 0,
|
|
125
|
-
endIndex: 1,
|
|
126
|
-
value: "//"
|
|
127
|
-
},
|
|
128
|
-
{
|
|
129
|
-
type: "repeat-value",
|
|
130
|
-
name: "comment",
|
|
131
|
-
startIndex: 2,
|
|
132
|
-
endIndex: 19,
|
|
133
|
-
value: " This is a comment"
|
|
134
|
-
},
|
|
135
|
-
{
|
|
136
|
-
type: "or-value",
|
|
137
|
-
name: "line-end",
|
|
138
|
-
startIndex: 20,
|
|
139
|
-
endIndex: 20,
|
|
140
|
-
value: "\n"
|
|
141
|
-
}
|
|
142
|
-
]
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
assert.equal(JSON.stringify(node), JSON.stringify(expectedValue)); // --> true
|
|
146
|
-
};
|
|
147
|
-
```
|
|
148
|
-
|
|
149
|
-
### Literal
|
|
150
|
-
|
|
151
|
-
```javascript
|
|
152
|
-
import { Literal } from "clarity-pattern-parser";
|
|
153
|
-
import assert from "assert";
|
|
154
|
-
|
|
155
|
-
// The first parameter is the name of the pattern, the second is the string you are matching.
|
|
156
|
-
const john = new Literal("john", "John");
|
|
157
|
-
|
|
158
|
-
const result = john.exec("John");
|
|
159
|
-
const result2 = john.exec("Jane");
|
|
160
|
-
|
|
161
|
-
const expectedValue = {
|
|
162
|
-
type: "literal",
|
|
163
|
-
name: "john",
|
|
164
|
-
startIndex: 0,
|
|
165
|
-
endIndex: 3,
|
|
166
|
-
value: "John"
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
assert.equal(JSON.stringify(result), JSON.stringify(expectedValue));
|
|
170
|
-
assert.equal(result2, null);
|
|
171
|
-
```
|
|
172
|
-
|
|
173
|
-
### RegexValue
|
|
174
|
-
|
|
175
|
-
```javascript
|
|
176
|
-
import { RegexValue } from "clarity-pattern-parser";
|
|
177
|
-
import assert from "assert";
|
|
178
|
-
|
|
179
|
-
// Notice this isn't a regex literal /[^a]+/
|
|
180
|
-
// This is because we need to modify the expression to use it in the middle of a parse.
|
|
181
|
-
// This also means we shouldn't use things like the start "^" character or the end "$" character.
|
|
182
|
-
// There are several gotchas with RegexValue, however the speed gains are undeniable.
|
|
183
|
-
const notA = new RegexValue("not-a", "[^a]+");
|
|
184
|
-
|
|
185
|
-
const result = notA.exec("John");
|
|
186
|
-
const result2 = notA.exec("a");
|
|
187
|
-
|
|
188
|
-
const expectedValue = {
|
|
189
|
-
type: "regex-value",
|
|
190
|
-
name: "not-a",
|
|
191
|
-
startIndex: 0,
|
|
192
|
-
endIndex: 3,
|
|
193
|
-
value: "John"
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
assert.equal(JSON.stringify(result), JSON.stringify(expectedValue));
|
|
197
|
-
assert.equal(result2, null);
|
|
198
|
-
```
|
package/dist/ast/Node.d.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
export default
|
|
1
|
+
export default class Node {
|
|
2
2
|
type: string;
|
|
3
3
|
name: string;
|
|
4
4
|
startIndex: number;
|
|
5
5
|
endIndex: number;
|
|
6
|
-
isComposite: boolean;
|
|
7
6
|
children: Node[];
|
|
8
7
|
value: string;
|
|
9
|
-
constructor(type: string, name: string, startIndex: number, endIndex: number,
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
constructor(type: string, name: string, startIndex: number, endIndex: number, children?: Node[], value?: string);
|
|
9
|
+
clone(): Node;
|
|
10
|
+
toString(): string;
|
|
12
11
|
}
|