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
#[derive(Debug, PartialEq, Clone, Copy)]
#[repr(u8)]
pub(super) enum Token {
    Comma,
    OpeningBrace,           // (
    ClosingBrace,           // )
    OpeningSquareBracket,   // [
    ClosingSquareBracket,   // ]
    OpeningCurly,           // {
    ClosingCurly,           // }
    Minus,                  // -
    Dot,                    // .
    CastOperator,           // ::
    DoubleQuotedIdentifier, // "
    StringLiteral,          // '...'
    CStyleStringLiteral,    // E'...'
    Digits,                 // sequence of digits without dots or sign
    Identifier,
    UnterminatedStringLiteral,
    Whitespace,
    Unknown,
}

pub(super) fn tokenize(default_string: &str) -> Vec<(Token, u32)> {
    let mut char_indices = default_string.char_indices().map(|(idx, c)| (idx as u32, c)).peekable();
    let mut out = Vec::new();

    loop {
        match char_indices.next() {
            None => return out,
            Some((start, ',')) => out.push((Token::Comma, start)),
            Some((start, '(')) => out.push((Token::OpeningBrace, start)),
            Some((start, ')')) => out.push((Token::ClosingBrace, start)),
            Some((start, '[')) => out.push((Token::OpeningSquareBracket, start)),
            Some((start, ']')) => out.push((Token::ClosingSquareBracket, start)),
            Some((start, '-')) => out.push((Token::Minus, start)),
            Some((start, '.')) => out.push((Token::Dot, start)),
            Some((start, '{')) => out.push((Token::OpeningCurly, start)),
            Some((start, '}')) => out.push((Token::ClosingCurly, start)),
            Some((start, ':')) => match char_indices.peek() {
                Some((_, ':')) => {
                    char_indices.next();
                    out.push((Token::CastOperator, start))
                }
                None | Some(_) => {
                    out.push((Token::Unknown, start));
                    return out;
                }
            },
            Some((start, c)) if c.is_ascii_digit() => loop {
                match char_indices.peek() {
                    Some((_, c)) if c.is_ascii_digit() => {
                        char_indices.next();
                    }
                    None | Some(_) => {
                        out.push((Token::Digits, start));
                        break;
                    }
                }
            },
            Some((start, c)) if c == 'E' || c == 'e' => match char_indices.peek() {
                Some((_, '\'')) => {
                    // C-style string.
                    char_indices.next();

                    loop {
                        match char_indices.next() {
                            Some((_, '\\')) => match char_indices.next() {
                                None => {
                                    out.push((Token::UnterminatedStringLiteral, start));
                                    break;
                                }
                                Some(_) => {
                                    // consume
                                }
                            },
                            Some((_, '\'')) => {
                                out.push((Token::CStyleStringLiteral, start));
                                break;
                            }
                            Some(_) => {
                                // consume
                            }
                            None => {
                                out.push((Token::UnterminatedStringLiteral, start));
                                break;
                            }
                        }
                    }
                }
                Some((_, c)) if c.is_ascii_alphanumeric() => {
                    char_indices.next();
                    // identifier
                    loop {
                        match char_indices.peek() {
                            Some((_, c)) if c.is_ascii_alphanumeric() || *c == '_' || *c == '-' => {
                                char_indices.next();
                            }
                            Some((_, _)) | None => {
                                out.push((Token::Identifier, start));
                                break;
                            }
                        }
                    }
                }
                None | Some(_) => {
                    out.push((Token::Identifier, start));
                }
            },
            Some((start, '\'')) => loop {
                match char_indices.next() {
                    None => {
                        out.push((Token::UnterminatedStringLiteral, start));
                        return out;
                    }
                    Some((_, '\'')) => match char_indices.peek() {
                        Some((_, '\'')) => {
                            char_indices.next();
                        }
                        None | Some(_) => {
                            out.push((Token::StringLiteral, start));
                            break;
                        }
                    },
                    Some((_, _)) => (),
                }
            },
            Some((start, '"')) => loop {
                match char_indices.next() {
                    None => {
                        out.push((Token::UnterminatedStringLiteral, start));
                        return out;
                    }
                    Some((_, '"')) => {
                        out.push((Token::DoubleQuotedIdentifier, start));
                        break;
                    }
                    Some((_, '\\')) => {
                        // Consume the next character.
                        char_indices.next();
                    }
                    Some((_, _)) => (),
                }
            },
            Some((start, c)) if c.is_ascii_whitespace() => loop {
                match char_indices.peek() {
                    Some((_idx, c)) if c.is_ascii_whitespace() => {
                        char_indices.next();
                    }
                    None | Some(_) => {
                        out.push((Token::Whitespace, start));
                        break;
                    }
                }
            },
            Some((start, c)) if c.is_ascii_alphabetic() => loop {
                match char_indices.peek() {
                    Some((_idx, c)) if c.is_ascii_alphanumeric() || *c == '_' || *c == '-' => {
                        char_indices.next();
                    }
                    None | Some(_) => {
                        out.push((Token::Identifier, start));
                        break;
                    }
                }
            },
            Some((start, _)) => out.push((Token::Unknown, start)),
        }
    }
}