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
fn class_for(c: char) -> Option<&'static str> {
match c {
'd' => Some("0-9"),
'w' => Some("0-9a-zA-Z_"),
's' => Some(" \\t\\n\\r\\f\\v"),
_ => None,
}
}
/// Make sure given regex can be used inside /.../ in Lark syntax.
/// Also if `use_ascii.contains('d')` replace `\d` with `[0-9]` and `\D` with `[^0-9]`.
/// Similarly for `\w`/`\W` (`[0-9a-zA-Z_]`) and `\s`/`\S` (`[ \t\n\r\f\v]`).
/// For standard Unicode Python3 or Rust regex crate semantics `use_ascii = ""`
/// For JavaScript or JSON Schema semantics `use_ascii = "dw"`
/// For Python2 or byte patters in Python3 semantics `use_ascii = "dws"`
/// More flags may be added in future.
pub fn regex_to_lark(rx: &str, use_ascii: &str) -> String {
let mut is_q = false;
let mut res = String::new();
for c in rx.chars() {
let prev_q = is_q;
is_q = false;
match c {
// make sure we don't terminate on /
'/' => res.push_str("\\/"),
// these are optional, but nice
'\n' => res.push_str("\\n"),
'\r' => res.push_str("\\r"),
'\t' => res.push_str("\\t"),
'\\' if !prev_q => {
is_q = true;
}
'd' | 'w' | 's' | 'D' | 'W' | 'S' if prev_q => {
let c2 = c.to_ascii_lowercase();
if use_ascii.contains(c2) {
let class = class_for(c2).unwrap();
res.push('[');
if c != c2 {
res.push('^');
}
res.push_str(class);
res.push(']');
} else {
res.push('\\');
res.push(c);
}
}
_ => {
if prev_q {
res.push('\\');
}
res.push(c);
}
}
}
res
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_digit_conversion_with_ascii() {
// \d => [0-9], \D => [^0-9]
assert_eq!(regex_to_lark(r"\d", "d"), "[0-9]");
assert_eq!(regex_to_lark(r"\D", "d"), "[^0-9]");
}
#[test]
fn test_word_conversion_with_ascii() {
// Only convert if use_ascii contains corresponding letter.
assert_eq!(regex_to_lark(r"\w", "w"), "[0-9a-zA-Z_]");
assert_eq!(regex_to_lark(r"\W", "w"), "[^0-9a-zA-Z_]");
}
#[test]
fn test_space_conversion_with_ascii() {
// \s and \S should convert accordingly.
assert_eq!(regex_to_lark(r"\s", "s"), "[ \\t\\n\\r\\f\\v]");
assert_eq!(regex_to_lark(r"\S", "s"), "[^ \\t\\n\\r\\f\\v]");
}
#[test]
fn test_no_conversion_when_missing_in_use_ascii() {
// If the ascii flag doesn't contain the letter, leave escape as-is.
assert_eq!(regex_to_lark(r"\d", ""), r"\d");
assert_eq!(regex_to_lark(r"\w", "d"), r"\w");
}
#[test]
fn test_escaped_slashes_and_whitespace() {
// '/' should be escaped; newline, tab, carriage return are escaped.
let input = "/a\nb\rc\td";
let expected = r"\/a\nb\rc\td";
assert_eq!(regex_to_lark(input, "dws"), expected);
}
#[test]
fn test_combined_conversions() {
// Combined sequence with all conversions.
let input = r"\d\w\s\D\W\S";
let expected = "[0-9][0-9a-zA-Z_][ \\t\\n\\r\\f\\v][^0-9][^0-9a-zA-Z_][^ \\t\\n\\r\\f\\v]";
assert_eq!(regex_to_lark(input, "dws"), expected);
}
#[test]
fn test_miscellaneous_escapes() {
// \X and \@ are not recognized as special, so they should pass through.
assert_eq!(regex_to_lark(r"\X", ""), r"\X");
assert_eq!(regex_to_lark(r"\@", ""), r"\@");
// Forward slash is escaped.
assert_eq!(regex_to_lark(r"/", ""), r"\/");
assert_eq!(regex_to_lark(r"\/", ""), r"\/");
assert_eq!(regex_to_lark(r"\//", ""), r"\/\/");
assert_eq!(regex_to_lark(r"/\//", ""), r"\/\/\/");
// Double backslash should be preserved.
assert_eq!(regex_to_lark(r"\\", ""), r"\\");
// Quotes should pass through unchanged.
assert_eq!(regex_to_lark("\"", ""), "\"");
assert_eq!(regex_to_lark(r#"a"b"#, ""), r#"a"b"#);
}
}