intuicio_parser/
regex.rs

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
use crate::{
    ParseResult, Parser, ParserExt, ParserHandle, ParserNoValue, ParserOutput, ParserRegistry,
};

pub mod shorthand {
    use super::*;
    use crate::shorthand::map;

    pub fn regex(pattern: impl AsRef<str>) -> ParserHandle {
        RegexParser::new(pattern).into_handle()
    }

    pub fn regex_capture(pattern: impl AsRef<str>, capture: impl ToString) -> ParserHandle {
        RegexParser::new_capture(pattern, capture).into_handle()
    }

    pub fn any() -> ParserHandle {
        regex(r".")
    }

    pub fn nl() -> ParserHandle {
        regex(r"[\r\n]")
    }

    pub fn digit_hex() -> ParserHandle {
        regex(r"[0-9a-fA-F]&")
    }

    pub fn digit() -> ParserHandle {
        regex(r"\d")
    }

    pub fn number_int_pos() -> ParserHandle {
        regex(r"\d+")
    }

    pub fn number_int() -> ParserHandle {
        regex(r"-?\d+")
    }

    pub fn number_float() -> ParserHandle {
        regex(r"-?\d+(\.\d+(e-?\d+)?)?")
    }

    pub fn alphanum() -> ParserHandle {
        regex(r"\w")
    }

    pub fn alpha_low() -> ParserHandle {
        regex(r"[a-z]")
    }

    pub fn alpha_up() -> ParserHandle {
        regex(r"[A-Z]")
    }

    pub fn alpha() -> ParserHandle {
        regex(r"[a-zA-Z]")
    }

    pub fn word() -> ParserHandle {
        regex(r"\w+")
    }

    pub fn string(open: &str, close: &str) -> ParserHandle {
        let open = open.escape_unicode().to_string();
        let close = close.escape_unicode().to_string();
        let pattern = format!("{0}(?<content>[^{1}]*){1}", open, close);
        map(regex_capture(pattern, "content"), move |value: String| {
            snailquote::unescape(&value).unwrap()
        })
    }

    pub fn id_start() -> ParserHandle {
        regex(r"[a-zA-Z_]")
    }

    pub fn id_continue() -> ParserHandle {
        regex(r"[0-9a-zA-Z_]*")
    }

    pub fn id() -> ParserHandle {
        regex(r"[a-zA-Z_][0-9a-zA-Z_]*")
    }

    pub fn ws() -> ParserHandle {
        WhiteSpaceParser::default().into_handle()
    }

    pub fn ows() -> ParserHandle {
        OptionalWhiteSpaceParser::default().into_handle()
    }
}

#[derive(Clone)]
pub struct RegexParser {
    regex: regex::Regex,
    capture: Option<String>,
}

impl RegexParser {
    pub fn new(pattern: impl AsRef<str>) -> Self {
        let pattern = format!(r"^{}", pattern.as_ref());
        Self {
            regex: regex::Regex::new(&pattern).expect("Expected valid regex"),
            capture: None,
        }
    }

    pub fn new_capture(pattern: impl AsRef<str>, capture: impl ToString) -> Self {
        let pattern = format!(r"^{}", pattern.as_ref());
        Self {
            regex: regex::Regex::new(&pattern).expect("Expected valid regex"),
            capture: Some(capture.to_string()),
        }
    }
}

impl Parser for RegexParser {
    fn parse<'a>(&self, _: &ParserRegistry, input: &'a str) -> ParseResult<'a> {
        if let Some(capture) = self.capture.as_deref() {
            if let Some(cap) = self.regex.captures(input) {
                Ok((
                    &input[cap.get(0).unwrap().end()..],
                    ParserOutput::new(
                        cap.name(capture)
                            .map(|mat| mat.as_str())
                            .unwrap_or("")
                            .to_owned(),
                    )
                    .ok()
                    .unwrap(),
                ))
            } else {
                Err(format!(
                    "Expected regex match '{}' with capture: '{}'",
                    self.regex, capture
                )
                .into())
            }
        } else if let Some(mat) = self.regex.find(input) {
            Ok((
                &input[mat.end()..],
                ParserOutput::new(mat.as_str().to_owned()).ok().unwrap(),
            ))
        } else {
            Err(format!("Expected regex match '{}'", self.regex).into())
        }
    }
}

#[derive(Clone)]
pub struct WhiteSpaceParser(RegexParser);

impl Default for WhiteSpaceParser {
    fn default() -> Self {
        Self(RegexParser::new(r"\s+"))
    }
}

impl Parser for WhiteSpaceParser {
    fn parse<'a>(&self, registry: &ParserRegistry, input: &'a str) -> ParseResult<'a> {
        match self.0.parse(registry, input) {
            Ok((rest, _)) => Ok((rest, ParserOutput::new(ParserNoValue).ok().unwrap())),
            Err(error) => Err(error),
        }
    }
}

#[derive(Clone)]
pub struct OptionalWhiteSpaceParser(RegexParser);

impl Default for OptionalWhiteSpaceParser {
    fn default() -> Self {
        Self(RegexParser::new(r"\s*"))
    }
}

impl Parser for OptionalWhiteSpaceParser {
    fn parse<'a>(&self, registry: &ParserRegistry, input: &'a str) -> ParseResult<'a> {
        match self.0.parse(registry, input) {
            Ok((rest, _)) => Ok((rest, ParserOutput::new(ParserNoValue).ok().unwrap())),
            Err(error) => Err(error),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        regex::{OptionalWhiteSpaceParser, RegexParser, WhiteSpaceParser},
        shorthand::{ows, regex, regex_capture, string, ws},
        ParserRegistry,
    };

    fn is_async<T: Send + Sync>() {}

    #[test]
    fn test_regex() {
        is_async::<RegexParser>();
        is_async::<WhiteSpaceParser>();
        is_async::<OptionalWhiteSpaceParser>();

        let registry = ParserRegistry::default();

        let keyword = regex_capture(r"\s+(?<name>\w+)\s+", "name");
        let (rest, result) = keyword.parse(&registry, " foo ").unwrap();
        assert_eq!(rest, "");
        assert_eq!(result.read::<String>().unwrap().as_str(), "foo");

        let keyword = string("`", "`");
        let (rest, result) = keyword.parse(&registry, "`Hello World!`").unwrap();
        assert_eq!(rest, "");
        assert_eq!(result.read::<String>().unwrap().as_str(), "Hello World!");

        let keyword = string("(", ")");
        let (rest, result) = keyword.parse(&registry, "(Hello World!)").unwrap();
        assert_eq!(rest, "");
        assert_eq!(result.read::<String>().unwrap().as_str(), "Hello World!");

        let keyword = regex(r"\w+");
        assert_eq!(keyword.parse(&registry, "foo bar").unwrap().0, " bar");

        let ws = ws();
        assert_eq!(ws.parse(&registry, "   \t  \n").unwrap().0, "");
        assert_eq!(
            format!("{}", ws.parse(&registry, "a").err().unwrap()),
            "Expected regex match '^\\s+'"
        );

        let ows = ows();
        assert_eq!(ows.parse(&registry, "   \t  \n").unwrap().0, "");
        assert_eq!(ows.parse(&registry, "foo").unwrap().0, "foo");
    }
}