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
use crate::ast::*;
use crate::parser::*;
fn escaped_regex_body(s: Span) -> ParseResult<Span> {
recognize(preceded(char('\\'), one_of("\\/[]")))(s)
}
fn regex_class(s: Span) -> ParseResult<Span> {
delimited(char('['), take_until("]"), char(']'))(s)
}
fn regex_char(s: Span) -> ParseResult<Span> {
recognize(none_of("/"))(s)
}
fn regex_body(s: Span) -> ParseResult<String> {
fold_many0(
alt((escaped_regex_body, regex_class, regex_char)),
format!(""),
|mut string: String, s| {
string.push_str(*s);
string
},
)(s)
}
fn regex_flags(s: Span) -> ParseResult<String> {
fold_many0(
identifier_continue,
format!(""),
|mut flag: String, flag_char| {
flag.push(flag_char);
flag
},
)(s)
}
pub fn regex_lit(s: Span) -> ParseResult<Node> {
map(
spanned(recognize(tuple((
tag("/"),
regex_body,
tag("/"),
regex_flags,
)))),
|(regex_str, start, end)| {
LiteralValue::RegExp(regex_str.to_string())
.into_node_kind()
.with_pos(start, end)
},
)(s)
}
#[cfg(test)]
mod tests {
use super::*;
use insta::assert_json_snapshot;
#[test]
fn test_regex_lit() {
assert_json_snapshot!(parse_literal("/abc/g".into()).unwrap().1);
assert_json_snapshot!(parse_literal(r#"/abc\\/g"#.into()).unwrap().1);
assert_json_snapshot!(parse_literal(r#"/\\\//g"#.into()).unwrap().1);
assert_json_snapshot!(parse_literal(r#"/[^/]*$/"#.into()).unwrap().1);
}
}