1
//! Resumable scan for the end of one form: each byte is visited once however
2
//! the input is split into chunks.
3

            
4
pub(crate) enum Scan {
5
    Complete(usize),
6
    Incomplete,
7
    Invalid(String),
8
}
9

            
10
/// Where a string literal stands. `""` waits for a third quote to tell an empty
11
/// string from a triple-quoted one.
12
#[derive(Debug, Clone, Copy)]
13
enum Lit {
14
    Open1,
15
    Open2,
16
    Plain { escaped: bool },
17
    Triple { run: u8 },
18
    Hash,
19
}
20

            
21
enum Step {
22
    More,
23
    End,
24
    EndBefore,
25
}
26

            
27
impl Lit {
28
15359569
    fn step(&mut self, byte: u8) -> Step {
29
15359569
        match *self {
30
247879
            Self::Open1 if byte == b'"' => *self = Self::Open2,
31
            Self::Open1 => {
32
234587
                *self = Self::Plain {
33
234587
                    escaped: byte == b'\\',
34
234587
                };
35
            }
36
13291
            Self::Open2 if byte == b'"' => *self = Self::Triple { run: 0 },
37
13288
            Self::Open2 => return Step::EndBefore,
38
857001
            Self::Plain { escaped: true } => *self = Self::Plain { escaped: false },
39
14241342
            Self::Plain { escaped: false } => match byte {
40
857001
                b'\\' => *self = Self::Plain { escaped: true },
41
234587
                b'"' => return Step::End,
42
13149754
                _ => {}
43
            },
44
35
            Self::Triple { run } if byte == b'"' => {
45
9
                if run == 2 {
46
3
                    return Step::End;
47
6
                }
48
6
                *self = Self::Triple { run: run + 1 };
49
            }
50
26
            Self::Triple { .. } => *self = Self::Triple { run: 0 },
51
21
            Self::Hash if byte == b'"' => return Step::End,
52
17
            Self::Hash => {}
53
        }
54
15111687
        Step::More
55
15359569
    }
56
}
57

            
58
#[derive(Debug, Clone, Copy)]
59
enum Code {
60
    Plain,
61
    Comment,
62
    HashSeen,
63
    Lit(Lit),
64
}
65

            
66
#[derive(Debug, Clone, Copy)]
67
enum State {
68
    Prefix,
69
    PrefixComma,
70
    Head,
71
    Lit(Lit),
72
    List { depth: usize, code: Code },
73
}
74

            
75
enum Flow {
76
    Next,
77
    Stall,
78
    Done(Scan),
79
}
80

            
81
#[derive(Debug)]
82
pub(crate) struct Scanner {
83
    pos: usize,
84
    head: usize,
85
    state: State,
86
}
87

            
88
impl Scanner {
89
43361157
    pub(crate) fn new() -> Self {
90
43361157
        Self {
91
43361157
            pos: 0,
92
43361157
            head: 0,
93
43361157
            state: State::Prefix,
94
43361157
        }
95
43361157
    }
96

            
97
    /// Scans `bytes`, the form's whole text so far, from where the last call stopped.
98
43388142
    pub(crate) fn advance(&mut self, bytes: &[u8]) -> Scan {
99
804662463
        while self.pos < bytes.len() {
100
804634347
            match self.step(bytes) {
101
761274321
                Flow::Next => {}
102
1
                Flow::Stall => break,
103
43360025
                Flow::Done(scan) => return scan,
104
            }
105
        }
106
28117
        Scan::Incomplete
107
43388142
    }
108

            
109
    /// Scans `bytes` as the whole input: an undecided head becomes an atom and
110
    /// a trailing `""` an empty string.
111
40071161
    pub(crate) fn finish(&mut self, bytes: &[u8]) -> Scan {
112
40071161
        match self.advance(bytes) {
113
327
            Scan::Incomplete => match self.state {
114
1
                State::Head if self.head < bytes.len() => Scan::Complete(bytes.len()),
115
1
                State::Lit(Lit::Open2) => Scan::Complete(bytes.len()),
116
325
                _ => Scan::Incomplete,
117
            },
118
40070834
            scan => scan,
119
        }
120
40071161
    }
121

            
122
804634347
    fn step(&mut self, bytes: &[u8]) -> Flow {
123
804634347
        let byte = bytes[self.pos];
124
804634347
        match self.state {
125
43761157
            State::Prefix => self.prefix(byte),
126
            State::PrefixComma if byte == b'@' => self.go(State::Prefix, 1),
127
            State::PrefixComma => self.go(State::Prefix, 0),
128
43361155
            State::Head => self.head(bytes),
129
30
            State::Lit(lit) => self.lit(lit, byte),
130
717512005
            State::List { depth, code } => self.list(depth, code, byte),
131
        }
132
804634347
    }
133

            
134
747940348
    fn go(&mut self, state: State, consumed: usize) -> Flow {
135
747940348
        self.state = state;
136
747940348
        self.pos += consumed;
137
747940348
        Flow::Next
138
747940348
    }
139

            
140
43761157
    fn prefix(&mut self, byte: u8) -> Flow {
141
43761157
        match byte {
142
400002
            b'\'' | b'`' => self.go(State::Prefix, 1),
143
            b',' => self.go(State::PrefixComma, 1),
144
            _ => {
145
43361155
                self.head = self.pos;
146
43361155
                self.go(State::Head, 0)
147
            }
148
        }
149
43761157
    }
150

            
151
43361155
    fn head(&mut self, bytes: &[u8]) -> Flow {
152
43361155
        let open = State::List {
153
43361155
            depth: 1,
154
43361155
            code: Code::Plain,
155
43361155
        };
156
43361155
        let from = self.head;
157
43361155
        match &bytes[from..] {
158
13333963
            [b'(', ..] => self.goto(open, from + 1),
159
8
            [b'"', ..] => self.goto(State::Lit(Lit::Open1), from + 1),
160
2
            [b')', ..] => Flow::Done(Scan::Invalid("unexpected ')'".into())),
161
1
            [b'#', b'"', ..] => self.goto(State::Lit(Lit::Hash), from + 2),
162
1
            [b'#', b'u', b'8', b'(', ..] => self.goto(open, from + 4),
163
1
            [b'#'] | [b'#', b'u'] | [b'#', b'u', b'8'] => Flow::Stall,
164
30027179
            rest => Flow::Done(Scan::Complete(from + atom_len(rest))),
165
        }
166
43361155
    }
167

            
168
13333973
    fn goto(&mut self, state: State, pos: usize) -> Flow {
169
13333973
        self.state = state;
170
13333973
        self.pos = pos;
171
13333973
        Flow::Next
172
13333973
    }
173

            
174
30
    fn lit(&mut self, mut lit: Lit, byte: u8) -> Flow {
175
30
        match lit.step(byte) {
176
22
            Step::More => self.go(State::Lit(lit), 1),
177
8
            Step::End => Flow::Done(Scan::Complete(self.pos + 1)),
178
            Step::EndBefore => Flow::Done(Scan::Complete(self.pos)),
179
        }
180
30
    }
181

            
182
717512005
    fn list(&mut self, depth: usize, code: Code, byte: u8) -> Flow {
183
717512005
        let (depth, code, consumed) = match (code, byte) {
184
614567
            (Code::Plain, b'(') => (depth + 1, Code::Plain, 1),
185
13947239
            (Code::Plain, b')') if depth == 1 => return Flow::Done(Scan::Complete(self.pos + 1)),
186
614403
            (Code::Plain, b')') => (depth - 1, Code::Plain, 1),
187
247871
            (Code::Plain, b'"') => (depth, Code::Lit(Lit::Open1), 1),
188
10
            (Code::Plain, b'#') => (depth, Code::HashSeen, 1),
189
163
            (Code::Plain, b';') => (depth, Code::Comment, 1),
190
687341622
            (Code::Plain, _) => (depth, Code::Plain, 1),
191
3
            (Code::HashSeen, b'"') => (depth, Code::Lit(Lit::Hash), 1),
192
7
            (Code::HashSeen, _) => (depth, Code::Plain, 0),
193
163
            (Code::Comment, b'\n') => (depth, Code::Plain, 1),
194
821
            (Code::Comment, _) => (depth, Code::Comment, 1),
195
15359539
            (Code::Lit(mut lit), byte) => match lit.step(byte) {
196
15111665
                Step::More => (depth, Code::Lit(lit), 1),
197
234586
                Step::End => (depth, Code::Plain, 1),
198
13288
                Step::EndBefore => (depth, Code::Plain, 0),
199
            },
200
        };
201
704179169
        self.go(State::List { depth, code }, consumed)
202
717512005
    }
203
}
204

            
205
30027179
fn atom_len(rest: &[u8]) -> usize {
206
30027179
    rest.iter()
207
120562230
        .position(|b| matches!(b, b' ' | b'\t' | b'\n' | b'\r' | b'(' | b')' | b';'))
208
30027179
        .unwrap_or(rest.len())
209
30027179
}
210

            
211
#[cfg(test)]
212
mod tests;