-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmatcher.ts
209 lines (186 loc) · 5.33 KB
/
matcher.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { isDigit, isAlpha, isWhitespace, Char } from "../char";
import {
CharacterNode,
CharacterClassNode,
CharacterSetNode,
CharacterRangeNode,
NodeType,
} from "../parser/node";
import { Flags } from "../regexp";
import { Range } from "../util";
const enum MatcherType {
Character,
CharacterRange,
CharacterSet,
CharacterClass,
}
export class Matcher {
@lazy static _flags!: Flags;
constructor(readonly type: MatcherType) {}
matches(code: u32): bool {
return false;
}
static fromCharacterClassNode(
node: CharacterSetNode,
flags: Flags
): CharacterSetMatcher {
return new CharacterSetMatcher(node.charClass, flags.dotAll);
}
static fromCharacterRangeNode(
node: CharacterRangeNode,
flags: Flags
): CharacterRangeMatcher {
return new CharacterRangeMatcher(
new Range(node.from, node.to),
flags.ignoreCase
);
}
static fromCharacterSetNode(
node: CharacterClassNode,
flags: Flags
): CharacterClassMatcher {
Matcher._flags = flags;
const matchers = node.expressions.map<Matcher>((exp) => {
switch (exp.type) {
case NodeType.CharacterRange:
return Matcher.fromCharacterRangeNode(
exp as CharacterRangeNode,
Matcher._flags
);
case NodeType.Character:
return Matcher.fromCharacterNode(
exp as CharacterNode,
Matcher._flags
);
case NodeType.CharacterSet:
return Matcher.fromCharacterClassNode(
exp as CharacterSetNode,
Matcher._flags
);
default:
throw new Error("unsupported node type within character set");
}
});
return new CharacterClassMatcher(matchers, node.negated);
}
static fromCharacterNode(
node: CharacterNode,
flags: Flags
): CharacterMatcher {
return new CharacterMatcher(node.char, flags.ignoreCase);
}
}
export class CharacterMatcher extends Matcher {
constructor(private character: Char, private ignoreCase: bool) {
super(MatcherType.Character);
if (ignoreCase) {
this.character |= 0x20;
}
}
matches(code: u32): bool {
if (this.ignoreCase) {
code |= 0x20;
}
return this.character == code;
}
}
// @ts-ignore
@lazy const LOWERCASE_LETTERS = new Range(Char.a, Char.z);
// @ts-ignore
@lazy const UPPERCASE_LETTERS = new Range(Char.A, Char.Z);
// @ts-ignore
@lazy const UPPER_LOWER_OFFSET = Char.a - Char.A;
export class CharacterRangeMatcher extends Matcher {
private ranges: Range[];
constructor(private range: Range, ignoreCase: bool) {
super(MatcherType.CharacterRange);
this.ranges = [range];
if (ignoreCase) {
const lowerIntersect = range.intersection(LOWERCASE_LETTERS);
if (lowerIntersect) {
this.ranges.push(lowerIntersect.offset(-UPPER_LOWER_OFFSET));
}
const upperIntersect = range.intersection(UPPERCASE_LETTERS);
if (upperIntersect) {
this.ranges.push(upperIntersect.offset(UPPER_LOWER_OFFSET));
}
}
}
matches(code: u32): bool {
for (let i = 0, len = this.ranges.length; i < len; i++) {
if (this.ranges[i].contains(code)) {
return true;
}
}
return false;
}
}
export class CharacterSetMatcher extends Matcher {
constructor(public charClass: Char, private dotAll: bool) {
super(MatcherType.CharacterSet);
}
matches(code: u32): bool {
switch (this.charClass) {
case Char.d:
return isDigit(code);
case Char.D:
return !isDigit(code);
case Char.Dot:
return this.dotAll
? true
: code != Char.CarriageReturn &&
code != Char.LineFeed &&
code != 8232 &&
code != 8233;
case Char.w:
return isAlpha(code) || code == Char.Underscore || isDigit(code);
case Char.W:
return !(isAlpha(code) || code == Char.Underscore || isDigit(code));
case Char.s:
return isWhitespace(code);
case Char.S:
return !isWhitespace(code);
case Char.t:
return code == Char.HorizontalTab;
case Char.r:
return code == Char.CarriageReturn;
case Char.n:
return code == Char.LineFeed;
case Char.v:
return code == Char.VerticalTab;
case Char.f:
return code == Char.FormFeed;
default:
throw new Error(
"unsupported character class - " + String.fromCharCode(this.charClass)
);
}
}
}
export class CharacterClassMatcher extends Matcher {
constructor(public matchers: Matcher[], public negated: bool) {
super(MatcherType.CharacterClass);
}
matches(code: u32): bool {
let match: bool = false;
for (let i = 0, len = this.matchers.length; i < len; i++) {
let matcher = this.matchers[i];
switch (matcher.type) {
case MatcherType.Character:
match = (matcher as CharacterMatcher).matches(code);
break;
case MatcherType.CharacterRange:
match = (matcher as CharacterRangeMatcher).matches(code);
break;
case MatcherType.CharacterSet:
match = (matcher as CharacterSetMatcher).matches(code);
break;
case MatcherType.CharacterClass:
match = (matcher as CharacterClassMatcher).matches(code);
break;
}
if (match) break;
}
return this.negated ? !match : match;
}
}