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,159 +0,0 @@
|
|
|
1
|
-
import CompositePattern from "./CompositePattern";
|
|
2
|
-
import CompositeNode from "../../ast/CompositeNode";
|
|
3
|
-
import ParseError from "../../patterns/ParseError";
|
|
4
|
-
import OptionalValue from "../value/OptionalValue";
|
|
5
|
-
import OptionalComposite from "./OptionalComposite";
|
|
6
|
-
import Pattern from "../Pattern";
|
|
7
|
-
import Cursor from "../../Cursor";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
export default class AndComposite extends CompositePattern {
|
|
11
|
-
public index!: number;
|
|
12
|
-
public nodes!: CompositeNode[];
|
|
13
|
-
public node!: CompositeNode | null;
|
|
14
|
-
public cursor!: Cursor;
|
|
15
|
-
public mark!: number;
|
|
16
|
-
|
|
17
|
-
constructor(name: string, patterns: Pattern[] = []) {
|
|
18
|
-
super("and-composite", name, patterns);
|
|
19
|
-
this._assertArguments();
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
private _assertArguments() {
|
|
23
|
-
if (this._children.length < 2) {
|
|
24
|
-
throw new Error(
|
|
25
|
-
"Invalid Argument: AndValue needs to have more than one value pattern."
|
|
26
|
-
);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
private _reset(cursor: Cursor) {
|
|
31
|
-
this.index = 0;
|
|
32
|
-
this.nodes = [];
|
|
33
|
-
this.node = null;
|
|
34
|
-
this.cursor = cursor;
|
|
35
|
-
this.mark = this.cursor.mark();
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
parse(cursor: Cursor) {
|
|
39
|
-
this._reset(cursor);
|
|
40
|
-
this._tryPatterns();
|
|
41
|
-
|
|
42
|
-
return this.node;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
private _tryPatterns() {
|
|
46
|
-
while (true) {
|
|
47
|
-
const pattern = this._children[this.index];
|
|
48
|
-
const node = pattern.parse(this.cursor) as CompositeNode;
|
|
49
|
-
|
|
50
|
-
if (this.cursor.hasUnresolvedError()) {
|
|
51
|
-
this.cursor.moveToMark(this.mark);
|
|
52
|
-
break;
|
|
53
|
-
} else {
|
|
54
|
-
this.nodes.push(node);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
if (!this._next()) {
|
|
58
|
-
this._processValue();
|
|
59
|
-
break;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
private _next() {
|
|
65
|
-
if (this._hasMorePatterns()) {
|
|
66
|
-
if (this.cursor.hasNext()) {
|
|
67
|
-
// If the last result was a failed optional, then don't increment the cursor.
|
|
68
|
-
if (this.nodes[this.nodes.length - 1] != null) {
|
|
69
|
-
this.cursor.next();
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
this.index++;
|
|
73
|
-
return true;
|
|
74
|
-
} else if (this.nodes[this.nodes.length - 1] == null) {
|
|
75
|
-
this.index++;
|
|
76
|
-
return true;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
this._assertRestOfPatternsAreOptional();
|
|
80
|
-
return false;
|
|
81
|
-
} else {
|
|
82
|
-
return false;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
private _hasMorePatterns() {
|
|
87
|
-
return this.index + 1 < this._children.length;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
private _assertRestOfPatternsAreOptional() {
|
|
91
|
-
const areTheRestOptional = this.children.every((pattern, index) => {
|
|
92
|
-
return (
|
|
93
|
-
index <= this.index ||
|
|
94
|
-
pattern instanceof OptionalValue ||
|
|
95
|
-
pattern instanceof OptionalComposite
|
|
96
|
-
);
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
if (!areTheRestOptional) {
|
|
100
|
-
const parseError = new ParseError(
|
|
101
|
-
`Could not match ${this.name} before string ran out.`,
|
|
102
|
-
this.index,
|
|
103
|
-
this
|
|
104
|
-
);
|
|
105
|
-
this.cursor.throwError(parseError);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
private _processValue() {
|
|
110
|
-
if (!this.cursor.hasUnresolvedError()) {
|
|
111
|
-
this.nodes = this.nodes.filter((node) => node != null);
|
|
112
|
-
|
|
113
|
-
const lastNode = this.nodes[this.nodes.length - 1];
|
|
114
|
-
const startIndex = this.mark;
|
|
115
|
-
const endIndex = lastNode.endIndex;
|
|
116
|
-
|
|
117
|
-
this.node = new CompositeNode(
|
|
118
|
-
"and-composite",
|
|
119
|
-
this.name,
|
|
120
|
-
startIndex,
|
|
121
|
-
endIndex
|
|
122
|
-
);
|
|
123
|
-
|
|
124
|
-
this.node.children = this.nodes;
|
|
125
|
-
|
|
126
|
-
this.cursor.index = this.node.endIndex;
|
|
127
|
-
this.cursor.addMatch(this, this.node);
|
|
128
|
-
} else {
|
|
129
|
-
this.node = null;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
clone(name?: string) {
|
|
134
|
-
if (typeof name !== "string") {
|
|
135
|
-
name = this.name;
|
|
136
|
-
}
|
|
137
|
-
return new AndComposite(name, this._children);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
getTokens() {
|
|
141
|
-
let tokens: string[] = [];
|
|
142
|
-
|
|
143
|
-
for (let x = 0; x < this._children.length; x++) {
|
|
144
|
-
const child = this._children[x];
|
|
145
|
-
|
|
146
|
-
if (
|
|
147
|
-
child instanceof OptionalValue ||
|
|
148
|
-
child instanceof OptionalComposite
|
|
149
|
-
) {
|
|
150
|
-
tokens = tokens.concat(child.getTokens());
|
|
151
|
-
} else {
|
|
152
|
-
tokens = tokens.concat(child.getTokens());
|
|
153
|
-
break;
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
return tokens;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import CompositePattern from "./CompositePattern";
|
|
2
|
-
import Pattern from "../Pattern";
|
|
3
|
-
import Cursor from "../../Cursor";
|
|
4
|
-
|
|
5
|
-
export default class OptionalComposite extends CompositePattern {
|
|
6
|
-
public mark: any;
|
|
7
|
-
|
|
8
|
-
constructor(pattern: Pattern) {
|
|
9
|
-
super("optional-composite", "optional-composite", [pattern]);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
parse(cursor: Cursor) {
|
|
13
|
-
const mark = cursor.mark();
|
|
14
|
-
this.mark = mark;
|
|
15
|
-
|
|
16
|
-
const node = this.children[0].parse(cursor);
|
|
17
|
-
|
|
18
|
-
if (cursor.hasUnresolvedError()) {
|
|
19
|
-
cursor.resolveError();
|
|
20
|
-
cursor.moveToMark(mark);
|
|
21
|
-
return null;
|
|
22
|
-
} else {
|
|
23
|
-
if (node != null){
|
|
24
|
-
cursor.addMatch(this, node);
|
|
25
|
-
}
|
|
26
|
-
return node;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
clone() {
|
|
31
|
-
return new OptionalComposite(this.children[0]);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
getTokens() {
|
|
35
|
-
return this._children[0].getTokens();
|
|
36
|
-
}
|
|
37
|
-
}
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import CompositePattern from "./CompositePattern";
|
|
2
|
-
import OptionalValue from "../value/OptionalValue";
|
|
3
|
-
import OptionalComposite from "./OptionalComposite";
|
|
4
|
-
import Pattern from "../Pattern";
|
|
5
|
-
import Cursor from "../../Cursor";
|
|
6
|
-
|
|
7
|
-
export default class OrComposite extends CompositePattern {
|
|
8
|
-
public cursor: any;
|
|
9
|
-
public mark: any;
|
|
10
|
-
public index: any;
|
|
11
|
-
public node: any;
|
|
12
|
-
|
|
13
|
-
constructor(name: string, patterns: Pattern[]) {
|
|
14
|
-
super("or-composite", name, patterns);
|
|
15
|
-
this._assertArguments();
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
private _assertArguments() {
|
|
19
|
-
if (this._children.length < 2) {
|
|
20
|
-
throw new Error(
|
|
21
|
-
"Invalid Argument: OrValue needs to have more than one value pattern."
|
|
22
|
-
);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const hasOptionalChildren = this._children.some(
|
|
26
|
-
(pattern) =>
|
|
27
|
-
pattern instanceof OptionalValue || pattern instanceof OptionalComposite
|
|
28
|
-
);
|
|
29
|
-
|
|
30
|
-
if (hasOptionalChildren) {
|
|
31
|
-
throw new Error("OrComposite cannot have optional values.");
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
private _reset(cursor: Cursor) {
|
|
36
|
-
this.cursor = cursor;
|
|
37
|
-
this.mark = null;
|
|
38
|
-
this.index = 0;
|
|
39
|
-
this.node = null;
|
|
40
|
-
this.mark = cursor.mark();
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
parse(cursor: Cursor) {
|
|
44
|
-
this._reset(cursor);
|
|
45
|
-
this._tryPattern();
|
|
46
|
-
|
|
47
|
-
if (this.node != null) {
|
|
48
|
-
this.cursor.addMatch(this, this.node);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return this.node;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
private _tryPattern() {
|
|
55
|
-
while (true) {
|
|
56
|
-
const pattern = this._children[this.index];
|
|
57
|
-
|
|
58
|
-
this.node = pattern.parse(this.cursor);
|
|
59
|
-
|
|
60
|
-
if (this.cursor.hasUnresolvedError()) {
|
|
61
|
-
if (this.index + 1 < this._children.length) {
|
|
62
|
-
this.cursor.resolveError();
|
|
63
|
-
this.index++;
|
|
64
|
-
this.cursor.moveToMark(this.mark);
|
|
65
|
-
} else {
|
|
66
|
-
this.node = null;
|
|
67
|
-
break;
|
|
68
|
-
}
|
|
69
|
-
} else {
|
|
70
|
-
this.cursor.index = this.node.endIndex;
|
|
71
|
-
break;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
clone(name?: string) {
|
|
77
|
-
if (typeof name !== "string") {
|
|
78
|
-
name = this.name;
|
|
79
|
-
}
|
|
80
|
-
return new OrComposite(name, this._children);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
getTokens() {
|
|
84
|
-
const tokens = this._children.map((c) => c.getTokens());
|
|
85
|
-
|
|
86
|
-
const hasPrimitiveTokens = tokens.every((t) =>
|
|
87
|
-
t.every((value) => typeof value === "string")
|
|
88
|
-
);
|
|
89
|
-
|
|
90
|
-
if (hasPrimitiveTokens && tokens.length > 0) {
|
|
91
|
-
return tokens.reduce((acc, t) => acc.concat(t), []);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return this._children[0].getTokens();
|
|
95
|
-
}
|
|
96
|
-
}
|
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
import CompositePattern from "./CompositePattern";
|
|
2
|
-
import CompositeNode from "../../ast/CompositeNode";
|
|
3
|
-
import ParseError from "../ParseError";
|
|
4
|
-
import OptionalComposite from "./OptionalComposite";
|
|
5
|
-
import Pattern from "../Pattern";
|
|
6
|
-
import Cursor from "../../Cursor";
|
|
7
|
-
import Node from "../../ast/Node";
|
|
8
|
-
|
|
9
|
-
export default class RepeatComposite extends CompositePattern {
|
|
10
|
-
private _pattern: Pattern;
|
|
11
|
-
private _divider: Pattern;
|
|
12
|
-
public nodes: Node[] = [];
|
|
13
|
-
public cursor!: Cursor;
|
|
14
|
-
public mark: number = 0;
|
|
15
|
-
public node: CompositeNode | null = null;
|
|
16
|
-
|
|
17
|
-
constructor(name: string, pattern: Pattern, divider?: Pattern) {
|
|
18
|
-
super(
|
|
19
|
-
"repeat-composite",
|
|
20
|
-
name,
|
|
21
|
-
divider != null ? [pattern, divider] : [pattern]
|
|
22
|
-
);
|
|
23
|
-
this._pattern = this.children[0];
|
|
24
|
-
this._divider = this.children[1];
|
|
25
|
-
this._assertArguments();
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
private _assertArguments() {
|
|
29
|
-
if (this._pattern instanceof OptionalComposite) {
|
|
30
|
-
throw new Error(
|
|
31
|
-
"Invalid Arguments: The pattern cannot be a optional pattern."
|
|
32
|
-
);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
private _reset(cursor: Cursor) {
|
|
37
|
-
this.nodes = [];
|
|
38
|
-
this.cursor = cursor;
|
|
39
|
-
this.mark = this.cursor.mark();
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
parse(cursor: Cursor) {
|
|
43
|
-
this._reset(cursor);
|
|
44
|
-
this._tryPattern();
|
|
45
|
-
|
|
46
|
-
return this.node;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
private _tryPattern() {
|
|
50
|
-
while (true) {
|
|
51
|
-
const node = this._pattern.parse(this.cursor);
|
|
52
|
-
|
|
53
|
-
if (this.cursor.hasUnresolvedError() || node == null) {
|
|
54
|
-
this._processMatch();
|
|
55
|
-
break;
|
|
56
|
-
} else {
|
|
57
|
-
this.nodes.push(node);
|
|
58
|
-
|
|
59
|
-
if (node.endIndex === this.cursor.lastIndex()) {
|
|
60
|
-
this._processMatch();
|
|
61
|
-
break;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
this.cursor.next();
|
|
65
|
-
|
|
66
|
-
if (this._divider != null) {
|
|
67
|
-
const mark = this.cursor.mark();
|
|
68
|
-
const node = this._divider.parse(this.cursor);
|
|
69
|
-
|
|
70
|
-
if (this.cursor.hasUnresolvedError() || node == null) {
|
|
71
|
-
this.cursor.moveToMark(mark);
|
|
72
|
-
this._processMatch();
|
|
73
|
-
break;
|
|
74
|
-
} else {
|
|
75
|
-
this.nodes.push(node);
|
|
76
|
-
|
|
77
|
-
if (node.endIndex === this.cursor.lastIndex()) {
|
|
78
|
-
this._processMatch();
|
|
79
|
-
break;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
this.cursor.next();
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
private _processMatch() {
|
|
90
|
-
const endsOnDivider = this.nodes.length % 2 === 0;
|
|
91
|
-
const noMatch = this.nodes.length === 0;
|
|
92
|
-
const hasDivider = this._divider != null;
|
|
93
|
-
|
|
94
|
-
this.cursor.resolveError();
|
|
95
|
-
|
|
96
|
-
if ((hasDivider && endsOnDivider) || noMatch) {
|
|
97
|
-
this.cursor.throwError(
|
|
98
|
-
new ParseError(
|
|
99
|
-
`Did not find a repeating match of ${this.name}.`,
|
|
100
|
-
this.mark,
|
|
101
|
-
this
|
|
102
|
-
)
|
|
103
|
-
);
|
|
104
|
-
this.node = null;
|
|
105
|
-
} else {
|
|
106
|
-
this.node = new CompositeNode(
|
|
107
|
-
"repeat-composite",
|
|
108
|
-
this.name,
|
|
109
|
-
this.nodes[0].startIndex,
|
|
110
|
-
this.nodes[this.nodes.length - 1].endIndex
|
|
111
|
-
);
|
|
112
|
-
|
|
113
|
-
this.node.children = this.nodes;
|
|
114
|
-
this.cursor.index = this.node.endIndex;
|
|
115
|
-
|
|
116
|
-
this.cursor.addMatch(this, this.node);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
clone(name?: string) {
|
|
121
|
-
if (typeof name !== "string") {
|
|
122
|
-
name = this.name;
|
|
123
|
-
}
|
|
124
|
-
return new RepeatComposite(name, this._pattern, this._divider);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
getTokens() {
|
|
128
|
-
return this._pattern.getTokens();
|
|
129
|
-
}
|
|
130
|
-
}
|
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
import ValuePattern from "./ValuePattern";
|
|
2
|
-
import ValueNode from "../../ast/ValueNode";
|
|
3
|
-
import ParseError from "../../patterns/ParseError";
|
|
4
|
-
import OptionalValue from "./OptionalValue";
|
|
5
|
-
import Permutor from "../../Permutor";
|
|
6
|
-
import Cursor from "../../Cursor";
|
|
7
|
-
|
|
8
|
-
const permutor = new Permutor();
|
|
9
|
-
|
|
10
|
-
export default class AndValue extends ValuePattern {
|
|
11
|
-
public index: number = 0;
|
|
12
|
-
public nodes: ValueNode[] = [];
|
|
13
|
-
public node: ValueNode | null = null;
|
|
14
|
-
public cursor!: Cursor;
|
|
15
|
-
public mark: number = 0;
|
|
16
|
-
|
|
17
|
-
constructor(name: string, patterns: ValuePattern[]) {
|
|
18
|
-
super("and-value", name, patterns);
|
|
19
|
-
this._assertArguments();
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
private _assertArguments() {
|
|
23
|
-
if (this._children.length < 2) {
|
|
24
|
-
throw new Error(
|
|
25
|
-
"Invalid Argument: AndValue needs to have more than one value pattern."
|
|
26
|
-
);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
private _reset(cursor: Cursor) {
|
|
31
|
-
this.index = 0;
|
|
32
|
-
this.nodes = [];
|
|
33
|
-
this.node = null;
|
|
34
|
-
this.cursor = cursor;
|
|
35
|
-
this.mark = this.cursor.mark();
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
parse(cursor: Cursor) {
|
|
39
|
-
this._reset(cursor);
|
|
40
|
-
this._tryPatterns();
|
|
41
|
-
|
|
42
|
-
return this.node;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
private _tryPatterns() {
|
|
46
|
-
while (true) {
|
|
47
|
-
const pattern = this._children[this.index];
|
|
48
|
-
const node = pattern.parse(this.cursor) as ValueNode;
|
|
49
|
-
|
|
50
|
-
if (this.cursor.hasUnresolvedError()) {
|
|
51
|
-
break;
|
|
52
|
-
} else {
|
|
53
|
-
this.nodes.push(node);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
if (!this._next()) {
|
|
57
|
-
this._processValue();
|
|
58
|
-
break;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
private _next() {
|
|
64
|
-
if (this._hasMorePatterns()) {
|
|
65
|
-
if (this.cursor.hasNext()) {
|
|
66
|
-
// If the last result was a failed optional, then don't increment the cursor.
|
|
67
|
-
if (this.nodes[this.nodes.length - 1] != null) {
|
|
68
|
-
this.cursor.next();
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
this.index++;
|
|
72
|
-
return true;
|
|
73
|
-
} else if (this.nodes[this.nodes.length - 1] == null) {
|
|
74
|
-
this.index++;
|
|
75
|
-
return true;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
this._assertRestOfPatternsAreOptional();
|
|
79
|
-
return false;
|
|
80
|
-
} else {
|
|
81
|
-
return false;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
private _hasMorePatterns() {
|
|
86
|
-
return this.index + 1 < this._children.length;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
private _assertRestOfPatternsAreOptional() {
|
|
90
|
-
const areTheRestOptional = this.children.every((pattern, index) => {
|
|
91
|
-
return index <= this.index || pattern instanceof OptionalValue;
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
if (!areTheRestOptional) {
|
|
95
|
-
const parseError = new ParseError(
|
|
96
|
-
`Could not match ${this.name} before string ran out.`,
|
|
97
|
-
this.index,
|
|
98
|
-
this
|
|
99
|
-
);
|
|
100
|
-
|
|
101
|
-
this.cursor.throwError(parseError);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
private _processValue() {
|
|
106
|
-
if (this.cursor.hasUnresolvedError()) {
|
|
107
|
-
this.node = null;
|
|
108
|
-
} else {
|
|
109
|
-
this.nodes = this.nodes.filter((node) => node != null);
|
|
110
|
-
|
|
111
|
-
const lastNode = this.nodes[this.nodes.length - 1];
|
|
112
|
-
const startIndex = this.mark;
|
|
113
|
-
const endIndex = lastNode.endIndex;
|
|
114
|
-
const value = this.nodes.map((node) => node.value).join("");
|
|
115
|
-
|
|
116
|
-
this.node = new ValueNode(
|
|
117
|
-
"and-value",
|
|
118
|
-
this.name,
|
|
119
|
-
value,
|
|
120
|
-
startIndex,
|
|
121
|
-
endIndex
|
|
122
|
-
);
|
|
123
|
-
|
|
124
|
-
this.cursor.index = this.node.endIndex;
|
|
125
|
-
this.cursor.addMatch(this, this.node);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
clone(name?: string) {
|
|
130
|
-
if (typeof name !== "string") {
|
|
131
|
-
name = this.name;
|
|
132
|
-
}
|
|
133
|
-
return new AndValue(name, this._children as ValuePattern[]);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
getTokens() {
|
|
137
|
-
let tokens: string[] = [];
|
|
138
|
-
|
|
139
|
-
for (let x = 0; x < this._children.length; x++) {
|
|
140
|
-
const child = this._children[x];
|
|
141
|
-
|
|
142
|
-
if (child instanceof OptionalValue) {
|
|
143
|
-
tokens = tokens.concat(child.getTokens());
|
|
144
|
-
} else {
|
|
145
|
-
tokens = tokens.concat(child.getTokens());
|
|
146
|
-
break;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
return tokens;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import ValuePattern from "./ValuePattern";
|
|
2
|
-
import ParseError from "../ParseError";
|
|
3
|
-
import ValueNode from "../../ast/ValueNode";
|
|
4
|
-
import Pattern from "../Pattern";
|
|
5
|
-
import Cursor from "../../Cursor";
|
|
6
|
-
|
|
7
|
-
export default class AnyOfThese extends ValuePattern {
|
|
8
|
-
public characters: string;
|
|
9
|
-
public node: ValueNode | null = null;
|
|
10
|
-
public cursor!: Cursor;
|
|
11
|
-
public mark: number = 0;
|
|
12
|
-
|
|
13
|
-
constructor(name: string, characters: string) {
|
|
14
|
-
super("any-of-these", name);
|
|
15
|
-
this.characters = characters;
|
|
16
|
-
this._assertArguments();
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
private _assertArguments() {
|
|
20
|
-
if (typeof this.characters !== "string") {
|
|
21
|
-
throw new Error(
|
|
22
|
-
"Invalid Arguments: The characters argument needs to be a string of characters."
|
|
23
|
-
);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
if (this.characters.length < 1) {
|
|
27
|
-
throw new Error(
|
|
28
|
-
"Invalid Arguments: The characters argument needs to be at least one character long."
|
|
29
|
-
);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
parse(cursor: Cursor) {
|
|
34
|
-
this._reset(cursor);
|
|
35
|
-
this._tryPattern();
|
|
36
|
-
return this.node;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
private _reset(cursor: Cursor) {
|
|
40
|
-
this.cursor = cursor;
|
|
41
|
-
this.mark = this.cursor.mark();
|
|
42
|
-
this.node = null;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
private _tryPattern() {
|
|
46
|
-
if (this._isMatch()) {
|
|
47
|
-
const value = this.cursor.getChar();
|
|
48
|
-
const index = this.cursor.getIndex();
|
|
49
|
-
|
|
50
|
-
this.node = new ValueNode("any-of-these", this.name, value, index, index);
|
|
51
|
-
|
|
52
|
-
this.cursor.addMatch(this, this.node);
|
|
53
|
-
} else {
|
|
54
|
-
this._processError();
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
private _isMatch() {
|
|
59
|
-
return this.characters.indexOf(this.cursor.getChar()) > -1;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
private _processError() {
|
|
63
|
-
const message = `ParseError: Expected one of these characters, '${
|
|
64
|
-
this.characters
|
|
65
|
-
}' but found '${this.cursor.getChar()}' while parsing for '${this.name}'.`;
|
|
66
|
-
|
|
67
|
-
const parseError = new ParseError(message, this.cursor.getIndex(), this);
|
|
68
|
-
this.cursor.throwError(parseError);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
clone(name?: string) {
|
|
72
|
-
if (typeof name !== "string") {
|
|
73
|
-
name = this.name;
|
|
74
|
-
}
|
|
75
|
-
return new AnyOfThese(name, this.characters);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
getTokens() {
|
|
79
|
-
return this.characters.split("");
|
|
80
|
-
}
|
|
81
|
-
}
|