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
const TOKEN_DOT: char = '.';
const TOKEN_RECURSIVE: char = '~';
const TOKEN_ARRAY_OPEN: char = '[';
const TOKEN_ARRAY_CLOSE: char = ']';
const TOKEN_FIRST_OPEN: char = '{';
const TOKEN_FIRST_CLOSE: char = '}';
const TOKEN_FIRST_SEP: char = ',';
const TOKEN_MULTI_OPEN: char = '(';
const TOKEN_MULTI_CLOSE: char = ')';
const TOKEN_MULTI_SEP: char = '|';
const TOKEN_WILDCARD: char = '*';
const TOKEN_REGEX: char = '/';

const TOKEN_ESCAPE: char = '\\';
const TOKEN_STRING_WRAP: char = '"';

pub struct Lexer<'a> {
    path: &'a str,
    head: usize,
    escape_token: Option<char>,
}

impl<'a> From<&'a str> for Lexer<'a> {
    fn from(path: &'a str) -> Lexer<'a> {
        Lexer {
            path,
            head: 0,
            escape_token: None,
        }
    }
}

impl<'a> Lexer<'a> {
    pub fn token(&mut self) -> Option<&'a str> {
        if self.head >= self.path.len() {
            return None;
        }

        let c = self.path[self.head..].char_indices();
        let mut tok: Option<&str> = None;
        let mut next_index = self.head;
        let mut escape_next = false;

        'charloop: for (i, char) in c {
            let index = i + self.head;

            match char {
                TOKEN_DOT | TOKEN_WILDCARD | TOKEN_RECURSIVE | TOKEN_ARRAY_OPEN
                | TOKEN_ARRAY_CLOSE | TOKEN_FIRST_OPEN | TOKEN_FIRST_CLOSE | TOKEN_MULTI_OPEN
                | TOKEN_MULTI_CLOSE | TOKEN_FIRST_SEP | TOKEN_MULTI_SEP => {
                    // if the previous token was an escape token, we just want to add this to the
                    // existing token
                    let escape_all = match self.escape_token {
                        None => false,
                        Some(_) => true,
                    };

                    if escape_next || escape_all {
                        tok = Some(&self.path[self.head..index + char.len_utf8()]);
                        next_index = index + char.len_utf8();
                        escape_next = false;
                        continue 'charloop;
                    }

                    // check the token, if there is a value in there we consumed
                    // an identifier first, so we need to return that. We keep
                    // keep track of the previous_index so that tokens greater
                    // than a single byte can be used.
                    if let Some(_) = tok {
                        break 'charloop;
                    }

                    // otherwise we are starting with a token, so lets return that
                    tok = Some(&self.path[self.head..index + char.len_utf8()]);
                    next_index = index + char.len_utf8();
                    break 'charloop;
                }
                TOKEN_ESCAPE => {
                    // collect the escapes so we can return something if the input is "\\\\\\\"
                    tok = Some(&self.path[self.head..index + char.len_utf8()]);
                    escape_next = true;
                }
                TOKEN_STRING_WRAP | TOKEN_REGEX => {
                    // if the previous character was an escape we can ignore
                    // the double quote
                    if escape_next {
                        escape_next = false;
                        continue;
                    }

                    // make sure the ending token matches the starting token
                    // we may add more string wrappers in the future, besides
                    // these two.
                    if let Some(t) = self.escape_token {
                        if t != char {
                            continue;
                        }
                    }

                    // return the wrapping token as it's own token.
                    if let None = tok {
                        tok = Some(&self.path[self.head..index + char.len_utf8()]);
                        next_index = index + char.len_utf8();

                        // toggle escaping when we encounter an escape token
                        match self.escape_token {
                            None => self.escape_token = Some(char),
                            Some(_) => self.escape_token = None,
                        }

                        break 'charloop;
                    }

                    if let Some(_) = self.escape_token {
                        // after we turn it off collect the token, without
                        // grabbing the ending "
                        tok = Some(&self.path[self.head..index]);
                        next_index = index;
                        break 'charloop;
                    }

                    // if we are just starting we want to collect the previous
                    // token if it exists
                    if let Some(_) = tok {
                        break 'charloop;
                    }
                }
                _ => {
                    escape_next = false;
                    tok = Some(&self.path[self.head..index + char.len_utf8()]);
                    next_index = index + char.len_utf8();
                }
            }
        }

        // move the head forward to the last seen index
        self.head = next_index;
        tok
    }
}

#[cfg(test)]
mod test {
    use super::*;

    macro_rules! test_lexor {
        ($path:expr, $($args:tt),*) => {{
            let mut p = Lexer::from($path);
            let mut toks = vec![];
            let expected: Vec<&str> = vec![$($args),*];

            let mut tok = p.token();
            while let Some(t) = tok {
                toks.push(t);
                tok = p.token();
            }

            assert_eq!(toks, expected)
        }};
    }

    #[test]
    fn test_lexor() {
        test_lexor!("Im.am a.fish", "Im", ".", "am a", ".", "fish");
        test_lexor!(" ", " ");
        test_lexor!(
            "labels.{hostname|host}",
            "labels",
            ".",
            "{",
            "hostname",
            "|",
            "host",
            "}"
        );
        test_lexor!("labels\\.hostname", "labels\\.hostname");
        test_lexor!(r#""labels.hostname""#, "\"", "labels.hostname", "\"");
        test_lexor!(r#""some\"thing""#, "\"", "some\\\"thing", "\"");
        test_lexor!(
            r#""one""two""three""four""#,
            "\"",
            "one",
            "\"",
            "\"",
            "two",
            "\"",
            "\"",
            "three",
            "\"",
            "\"",
            "four",
            "\""
        );
        test_lexor!(
            "€€€.€€€.€.asdf.asdf",
            "€€€",
            ".",
            "€€€",
            ".",
            "€",
            ".",
            "asdf",
            ".",
            "asdf"
        );
        test_lexor!("/.*/.something", "/", ".*", "/", ".", "something");
        test_lexor!("/asd\"asdf/", "/", "asd\"asdf", "/");
    }
}