forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregex.rs
More file actions
298 lines (263 loc) · 10.8 KB
/
Copy pathregex.rs
File metadata and controls
298 lines (263 loc) · 10.8 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
use std::collections::HashSet;
use regex::Regex;
fn group(choices: &[&str]) -> String {
["(", &choices.join("|"), ")"].concat()
}
fn maybe(choices: &[&str]) -> String {
[&group(choices), "?"].concat()
}
pub const WHITESPACE: &str = r"[ \f\t]*";
pub const COMMENT: &str = r"#[^\r\n]*";
pub const NAME: &str = r"\w+";
pub const HEXNUMBER: &str = r"0[xX](?:_?[0-9a-fA-F])+";
pub const BINNUMBER: &str = r"0[bB](?:_?[01])+";
pub const OCTNUMBER: &str = r"0[oO](?:_?[0-7])+";
pub const DECNUMBER: &str = r"(?:0(?:_?0)*|[1-9](?:_?[0-9])*)";
/// INTNUMBER = group(Hexnumber, Binnumber, Octnumber, Decnumber)
pub fn get_intnumber_pattern() -> String {
group(&[HEXNUMBER, BINNUMBER, OCTNUMBER, DECNUMBER])
}
pub const EXPONENT: &str = r"[eE][-+]?[0-9](?:_?[0-9])*";
/// POINTFLOAT = group(
/// r"[0-9](?:_?[0-9])*\.(?:[0-9](?:_?[0-9])*)?",
/// r"\.[0-9](?:_?[0-9])*",
/// ) + maybe(EXPONENT)
pub fn get_pointfloat_pattern() -> String {
[
group(&[
r"[0-9](?:_?[0-9])*\.(?:[0-9](?:_?[0-9])*)?",
r"\.[0-9](?:_?[0-9])*",
]),
maybe(&[EXPONENT]),
]
.concat()
}
/// EXPFLOAT = r"[0-9](?:_?[0-9])*" + EXPONENT
pub fn get_expfloat_pattern() -> String {
[r"[0-9](?:_?[0-9])*", EXPONENT].concat()
}
/// FLOATNUMBER = group(POINTFLOAT, EXPFLOAT)
pub fn get_floatnumber_pattern() -> String {
group(&[&get_pointfloat_pattern(), &get_expfloat_pattern()])
}
/// IMAGNUMBER = group(r"[0-9](?:_?[0-9])*[jJ]", FLOATNUMBER + r"[jJ]")
pub fn get_imagnumber_pattern() -> String {
group(&[
r"[0-9](?:_?[0-9])*[jJ]",
&[&get_floatnumber_pattern(), r"[jJ]"].concat(),
])
}
/// NUMBER = group(IMAGNUMBER, FLOATNUMBER, INTNUMBER)
pub fn get_number_pattern() -> String {
group(&[
&get_imagnumber_pattern(),
&get_floatnumber_pattern(),
&get_intnumber_pattern(),
])
}
pub const VALID_STRING_PREFIXES: &[&str] = &[
"b", "B", "r", "R", "u", "U", "f", "F", "br", "BR", "bR", "Br", "fr", "FR", "fR", "Fr", "rb",
"RB", "Rb", "rB", "rf", "RF", "Rf", "rF",
];
/// Get a hash set containing all possible initial single-quoted string
/// delimiters.
pub fn get_single_quote_set() -> HashSet<String> {
let mut set = HashSet::new();
set.insert("\"".to_string());
set.insert("'".to_string());
for prefix in VALID_STRING_PREFIXES {
set.insert([*prefix, "\""].concat());
set.insert([*prefix, "'"].concat());
}
set
}
/// Get a hash set containing all possible initial triple-quoted string
/// delimiters.
pub fn get_triple_quote_set() -> HashSet<String> {
let mut set = HashSet::new();
set.insert("\"\"\"".to_string());
set.insert("'''".to_string());
for prefix in VALID_STRING_PREFIXES {
set.insert([*prefix, "\"\"\""].concat());
set.insert([*prefix, "'''"].concat());
}
set
}
/// STRINGPREFIX = group(VALID_STRING_PREFIXES)
pub fn get_stringprefix_pattern() -> String {
[&group(VALID_STRING_PREFIXES), "?"].concat()
}
// Tail end of ' string
pub const SINGLE: &str = "'";
// Tail end of " string
pub const DOUBLE: &str = "\"";
// Tail end of ''' string
pub const SINGLE3: &str = "'''";
// Tail end of """ string
pub const DOUBLE3: &str = "\"\"\"";
/// TRIPLE = group(STRINGPREFIX + "'''", STRINGPREFIX + '"""')
pub fn get_triple_pattern() -> String {
let stringprefix = &get_stringprefix_pattern();
group(&[
&[stringprefix, "'''"].concat(),
&[stringprefix, "\"\"\""].concat(),
])
}
/// Because of leftmost-then-longest match semantics, be sure to put the longest
/// operators first (e.g., if = came before ==, == would get recognized as two
/// instances of =).
///
/// OPERATOR = group(r"\*\*=?", r">>=?", r"<<=?", r"!=",
/// r"//=?", r"->",
/// r"[+\-*/%&@|^=<>]=?",
/// r"~")
pub fn get_operator_pattern() -> String {
group(&[
r"\*\*=?",
r">>=?",
r"<<=?",
r"!=",
r"//=?",
r"->",
r"[+\-*/%&@|^=<>]=?",
r"~",
])
}
pub const BRACKET: &str = r"[\[\](){}]";
/// SPECIAL = group(r'\r?\n', r'\.\.\.', r'[:;.,@]')
pub fn get_special_pattern() -> String {
group(&[r"\r?\n", r"\.\.\.", r"[:;.,@]"])
}
/// FUNNY = group(OPERATOR, BRACKET, SPECIAL)
pub fn get_funny_pattern() -> String {
group(&[&get_operator_pattern(), BRACKET, &get_special_pattern()])
}
/// First (or only) line of ' or " string.
/// ContStr = group(StringPrefix + r"'[^\n'\\]*(?:\\.[^\n'\\]*)*" +
/// group("'", r'\\\r?\n'),
/// StringPrefix + r'"[^\n"\\]*(?:\\.[^\n"\\]*)*' +
/// group('"', r'\\\r?\n'))
pub fn get_contstr_pattern() -> String {
let stringprefix_pattern = &get_stringprefix_pattern();
group(&[
&[
stringprefix_pattern,
r###"'[^\n'\\]*(?:\\.[^\n'\\]*)*"###,
&group(&["'", r"\\\r?\n"]),
]
.concat(),
&[
stringprefix_pattern,
r###""[^\n"\\]*(?:\\.[^\n"\\]*)*"###,
&group(&["\"", r"\\\r?\n"]),
]
.concat(),
])
}
/// PSEUDOEXTRAS = group(r'\\\r?\n|\z', COMMENT, TRIPLE)
pub fn get_pseudoextras_pattern() -> String {
group(&[r"\\\r?\n|\z", COMMENT, &get_triple_pattern()])
}
/// PSEUDOTOKEN = WHITESPACE + group(PSEUDOEXTRAS, NUMBER, FUNNY, CONTSTR, NAME)
pub fn get_pseudotoken_pattern() -> String {
[
WHITESPACE,
&group(&[
&get_pseudoextras_pattern(),
&get_number_pattern(),
&get_funny_pattern(),
&get_contstr_pattern(),
NAME,
]),
]
.concat()
}
/// Compile the given regex with the "beginning of text" anchor prepended. This
/// forces the regex to match at the *beginning of a string* (as in python's
/// re.match method) instead of anywhere in the string.
pub fn compile_anchored(re: &str) -> Regex {
Regex::new(&[r"\A", re].concat()).unwrap()
}
#[cfg(test)]
mod tests {
use super::*;
use regex::Regex;
#[test]
fn test_get_regex_patterns() {
assert_eq!(
get_intnumber_pattern(),
"(0[xX](?:_?[0-9a-fA-F])+|0[bB](?:_?[01])+|0[oO](?:_?[0-7])+|(?:0(?:_?0)*|[1-9](?:_?[0-9])*))",
);
assert_eq!(
get_pointfloat_pattern(),
"([0-9](?:_?[0-9])*\\.(?:[0-9](?:_?[0-9])*)?|\\.[0-9](?:_?[0-9])*)([eE][-+]?[0-9](?:_?[0-9])*)?",
);
assert_eq!(
get_expfloat_pattern(),
"[0-9](?:_?[0-9])*[eE][-+]?[0-9](?:_?[0-9])*",
);
assert_eq!(
get_floatnumber_pattern(),
"(([0-9](?:_?[0-9])*\\.(?:[0-9](?:_?[0-9])*)?|\\.[0-9](?:_?[0-9])*)([eE][-+]?[0-9](?:_?[0-9])*)?|[0-9](?:_?[0-9])*[eE][-+]?[0-9](?:_?[0-9])*)",
);
assert_eq!(
get_imagnumber_pattern(),
"([0-9](?:_?[0-9])*[jJ]|(([0-9](?:_?[0-9])*\\.(?:[0-9](?:_?[0-9])*)?|\\.[0-9](?:_?[0-9])*)([eE][-+]?[0-9](?:_?[0-9])*)?|[0-9](?:_?[0-9])*[eE][-+]?[0-9](?:_?[0-9])*)[jJ])",
);
assert_eq!(
get_number_pattern(),
"(([0-9](?:_?[0-9])*[jJ]|(([0-9](?:_?[0-9])*\\.(?:[0-9](?:_?[0-9])*)?|\\.[0-9](?:_?[0-9])*)([eE][-+]?[0-9](?:_?[0-9])*)?|[0-9](?:_?[0-9])*[eE][-+]?[0-9](?:_?[0-9])*)[jJ])|(([0-9](?:_?[0-9])*\\.(?:[0-9](?:_?[0-9])*)?|\\.[0-9](?:_?[0-9])*)([eE][-+]?[0-9](?:_?[0-9])*)?|[0-9](?:_?[0-9])*[eE][-+]?[0-9](?:_?[0-9])*)|(0[xX](?:_?[0-9a-fA-F])+|0[bB](?:_?[01])+|0[oO](?:_?[0-7])+|(?:0(?:_?0)*|[1-9](?:_?[0-9])*)))",
);
assert_eq!(
get_stringprefix_pattern(),
"(b|B|r|R|u|U|f|F|br|BR|bR|Br|fr|FR|fR|Fr|rb|RB|Rb|rB|rf|RF|Rf|rF)?",
);
assert_eq!(
get_triple_pattern(),
"((b|B|r|R|u|U|f|F|br|BR|bR|Br|fr|FR|fR|Fr|rb|RB|Rb|rB|rf|RF|Rf|rF)?\'\'\'|(b|B|r|R|u|U|f|F|br|BR|bR|Br|fr|FR|fR|Fr|rb|RB|Rb|rB|rf|RF|Rf|rF)?\"\"\")",
);
assert_eq!(
get_operator_pattern(),
"(\\*\\*=?|>>=?|<<=?|!=|//=?|->|[+\\-*/%&@|^=<>]=?|~)",
);
assert_eq!(get_special_pattern(), "(\\r?\\n|\\.\\.\\.|[:;.,@])",);
assert_eq!(
get_funny_pattern(),
"((\\*\\*=?|>>=?|<<=?|!=|//=?|->|[+\\-*/%&@|^=<>]=?|~)|[\\[\\](){}]|(\\r?\\n|\\.\\.\\.|[:;.,@]))",
);
assert_eq!(
get_contstr_pattern(),
"((b|B|r|R|u|U|f|F|br|BR|bR|Br|fr|FR|fR|Fr|rb|RB|Rb|rB|rf|RF|Rf|rF)?\'[^\\n\'\\\\]*(?:\\\\.[^\\n\'\\\\]*)*(\'|\\\\\\r?\\n)|(b|B|r|R|u|U|f|F|br|BR|bR|Br|fr|FR|fR|Fr|rb|RB|Rb|rB|rf|RF|Rf|rF)?\"[^\\n\"\\\\]*(?:\\\\.[^\\n\"\\\\]*)*(\"|\\\\\\r?\\n))",
);
assert_eq!(
get_pseudoextras_pattern(),
"(\\\\\\r?\\n|\\z|#[^\\r\\n]*|((b|B|r|R|u|U|f|F|br|BR|bR|Br|fr|FR|fR|Fr|rb|RB|Rb|rB|rf|RF|Rf|rF)?\'\'\'|(b|B|r|R|u|U|f|F|br|BR|bR|Br|fr|FR|fR|Fr|rb|RB|Rb|rB|rf|RF|Rf|rF)?\"\"\"))",
);
assert_eq!(
get_pseudotoken_pattern(),
"[ \\f\\t]*((\\\\\\r?\\n|\\z|#[^\\r\\n]*|((b|B|r|R|u|U|f|F|br|BR|bR|Br|fr|FR|fR|Fr|rb|RB|Rb|rB|rf|RF|Rf|rF)?\'\'\'|(b|B|r|R|u|U|f|F|br|BR|bR|Br|fr|FR|fR|Fr|rb|RB|Rb|rB|rf|RF|Rf|rF)?\"\"\"))|(([0-9](?:_?[0-9])*[jJ]|(([0-9](?:_?[0-9])*\\.(?:[0-9](?:_?[0-9])*)?|\\.[0-9](?:_?[0-9])*)([eE][-+]?[0-9](?:_?[0-9])*)?|[0-9](?:_?[0-9])*[eE][-+]?[0-9](?:_?[0-9])*)[jJ])|(([0-9](?:_?[0-9])*\\.(?:[0-9](?:_?[0-9])*)?|\\.[0-9](?:_?[0-9])*)([eE][-+]?[0-9](?:_?[0-9])*)?|[0-9](?:_?[0-9])*[eE][-+]?[0-9](?:_?[0-9])*)|(0[xX](?:_?[0-9a-fA-F])+|0[bB](?:_?[01])+|0[oO](?:_?[0-7])+|(?:0(?:_?0)*|[1-9](?:_?[0-9])*)))|((\\*\\*=?|>>=?|<<=?|!=|//=?|->|[+\\-*/%&@|^=<>]=?|~)|[\\[\\](){}]|(\\r?\\n|\\.\\.\\.|[:;.,@]))|((b|B|r|R|u|U|f|F|br|BR|bR|Br|fr|FR|fR|Fr|rb|RB|Rb|rB|rf|RF|Rf|rF)?\'[^\\n\'\\\\]*(?:\\\\.[^\\n\'\\\\]*)*(\'|\\\\\\r?\\n)|(b|B|r|R|u|U|f|F|br|BR|bR|Br|fr|FR|fR|Fr|rb|RB|Rb|rB|rf|RF|Rf|rF)?\"[^\\n\"\\\\]*(?:\\\\.[^\\n\"\\\\]*)*(\"|\\\\\\r?\\n))|\\w+)",
);
}
#[test]
fn test_parse_regex_patterns() {
let patterns = &[
("get_intnumber_pattern", get_intnumber_pattern()),
("get_pointfloat_pattern", get_pointfloat_pattern()),
("get_expfloat_pattern", get_expfloat_pattern()),
("get_floatnumber_pattern", get_floatnumber_pattern()),
("get_imagnumber_pattern", get_imagnumber_pattern()),
("get_number_pattern", get_number_pattern()),
("get_stringprefix_pattern", get_stringprefix_pattern()),
("get_triple_pattern", get_triple_pattern()),
("get_operator_pattern", get_operator_pattern()),
("get_special_pattern", get_special_pattern()),
("get_funny_pattern", get_funny_pattern()),
("get_contstr_pattern", get_contstr_pattern()),
("get_pseudoextras_pattern", get_pseudoextras_pattern()),
("get_pseudotoken_pattern", get_pseudotoken_pattern()),
];
for (name, pattern) in patterns {
Regex::new(&pattern).expect(name);
}
}
}