-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathRegExpBuilder.dart
218 lines (189 loc) · 4.72 KB
/
RegExpBuilder.dart
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
210
211
212
213
214
215
216
217
218
class RegExpBuilder {
StringBuffer _literal;
bool _ignoreCase;
bool _multiLine;
HashSet<String> _specialCharactersInsideCharacterClass;
HashSet<String> _specialCharactersOutsideCharacterClass;
StringBuffer _escapedString;
int _min;
int _max;
String _of;
bool _ofAny;
String _from;
String _notFrom;
String _like;
String _behind;
String _notBehind;
String _either;
bool _reluctant;
bool _capture;
RegExpBuilder() {
_literal = new StringBuffer();
_specialCharactersInsideCharacterClass = new HashSet.from([@"^", @"-", @"]"]);
_specialCharactersOutsideCharacterClass = new HashSet.from([@".", @"^", @"$", @"*", @"+", @"?", @"(", @")", @"[", @"{"]);
_escapedString = new StringBuffer();
_clear();
}
void _clear() {
_ignoreCase = false;
_multiLine = false;
_min = -1;
_max = -1;
_of = "";
_ofAny = false;
_from = "";
_notFrom = "";
_like = "";
_behind = "";
_notBehind = "";
_either = "";
_reluctant = false;
_capture = false;
}
void _flushState() {
if (_of != "" || _ofAny || _from != "" || _notFrom != "" || _like != "") {
var captureLiteral = _capture ? "" : "?:";
var quantityLiteral = _getQuantityLiteral();
var characterLiteral = _getCharacterLiteral();
var reluctantLiteral = _reluctant ? "?" : "";
var behindLiteral = _behind != "" ? "(?=$_behind)" : "";
var notBehindLiteral = _notBehind != "" ? "(?!$_notBehind)" : "";
_literal.add("($captureLiteral(?:$characterLiteral)$quantityLiteral$reluctantLiteral)$behindLiteral$notBehindLiteral");
_clear();
}
}
String _getQuantityLiteral() {
if (_min != -1) {
if (_max != -1) {
return "{$_min,$_max}";
}
return "{$_min,}";
}
return "{0,$_max}";
}
String _getCharacterLiteral() {
if (_of != "") {
return _of;
}
if (_ofAny) {
return ".";
}
if (_from != "") {
return "[$_from]";
}
if (_notFrom != "") {
return "[^$_notFrom]";
}
if (_like != "") {
return _like;
}
}
String getLiteral() {
_flushState();
return _literal.toString();
}
RegExp getRegExp() {
_flushState();
return new RegExp(_literal.toString(), _ignoreCase, _multiLine);
}
RegExpBuilder ignoreCase() {
_ignoreCase = true;
return this;
}
RegExpBuilder multiLine() {
_multiLine = true;
return this;
}
RegExpBuilder start() {
_literal.add("(?:^)");
return this;
}
RegExpBuilder end() {
_flushState();
_literal.add(@"(?:$)");
return this;
}
RegExpBuilder either(Function r) {
_flushState();
_either = r(new RegExpBuilder()).getLiteral();
return this;
}
RegExpBuilder or(Function r) {
var either = _either;
var or = r(new RegExpBuilder()).getLiteral();
_literal.add("(?:(?:$either)|(?:$or))");
_clear();
return this;
}
RegExpBuilder exactly(int n) {
_flushState();
_min = n;
_max = n;
return this;
}
RegExpBuilder min(int n) {
_flushState();
_min = n;
return this;
}
RegExpBuilder max(int n) {
_flushState();
_max = n;
return this;
}
RegExpBuilder of(String s) {
_of = _escapeOutsideCharacterClass(s);
return this;
}
RegExpBuilder ofAny() {
_ofAny = true;
return this;
}
RegExpBuilder from(List<String> s) {
_from = _escapeInsideCharacterClass(Strings.concatAll(s));
return this;
}
RegExpBuilder notFrom(List<String> s) {
_notFrom = _escapeInsideCharacterClass(Strings.concatAll(s));
return this;
}
RegExpBuilder like(Function r) {
_like = r(new RegExpBuilder()).getLiteral();
return this;
}
RegExpBuilder reluctantly() {
_reluctant = true;
return this;
}
RegExpBuilder behind(Function r) {
_behind = r(new RegExpBuilder()).getLiteral();
return this;
}
RegExpBuilder notBehind(Function r) {
_notBehind = r(new RegExpBuilder()).getLiteral();
return this;
}
RegExpBuilder asCapturingGroup() {
_capture = true;
return this;
}
String _escapeInsideCharacterClass(String s) {
return _escapeSpecialCharacters(s, _specialCharactersInsideCharacterClass);
}
String _escapeOutsideCharacterClass(String s) {
return _escapeSpecialCharacters(s, _specialCharactersOutsideCharacterClass);
}
String _escapeSpecialCharacters(String s, Set<String> specialCharacters) {
_escapedString.clear();
for (var i = 0; i < s.length; i++) {
var character = s[i];
if (specialCharacters.contains(character)) {
_escapedString.add("\\$character");
}
else {
_escapedString.add(character);
}
}
return _escapedString.toString();
}
}