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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* Copyright 2020 Boyd Johnson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ------------------------------------------------------------------------------
*/

//! Parser struct should be used to parse bytes in to json.

use crate::{
    common::{Enclosing, ParserStatus},
    Context, Handler, Status,
};
use json_tools::{Buffer, BufferType, Lexer, Token, TokenType};
use std::io::BufRead;

/// Main Parser struct.
pub struct Parser<'a, H> {
    handler: &'a mut H,
    context: Context,
}

impl<'a, H: Handler> Parser<'a, H> {
    /// Construct a new Parser from a Handler.
    pub fn new(handler: &'a mut H) -> Self {
        Parser {
            handler,
            context: Context::default(),
        }
    }

    /// Parse until Handler method returns Abort or EOF.
    ///
    /// # Errors
    ///    - Will return an error Result if the JSON is malformed, or if the underlying
    ///   Reader returns an error.
    pub fn parse<B: BufRead>(&mut self, read: &mut B) -> Result<(), ParseError> {
        let context = &mut self.context;

        let mut lexer = Lexer::new(Wrapper(read), BufferType::Bytes(20));

        while !matches!(
            context.parser_status(),
            ParserStatus::ParseComplete | ParserStatus::LexicalError
        ) {
            let status = match lexer.next() {
                Some(Token {
                    kind: TokenType::BracketClose,
                    ..
                }) => {
                    let status = self.handler.handle_end_array(context);
                    if context.last_enclosing() == Some(Enclosing::LeftBracket) {
                        context.remove_last_enclosing();
                        context.dec_brackets();
                    } else {
                        return Err(ParseError::MalformedJson(format!("Parsed right bracket without a corresponding left bracket: braces: {}, brackets: {}", context.num_open_braces(), context.num_open_brackets())));
                    }

                    if context.last_enclosing() == Some(Enclosing::LeftBrace) {
                        context.update_status(ParserStatus::MapGotVal);
                    } else if context.last_enclosing() == Some(Enclosing::LeftBracket) {
                        context.update_status(ParserStatus::ArrayGotVal);
                    } else {
                        context.update_status(ParserStatus::GotValue);
                    }
                    Some(status)
                }
                Some(Token {
                    kind: TokenType::CurlyClose,
                    ..
                }) => {
                    let status = self.handler.handle_end_map(context);

                    if context.last_enclosing() == Some(Enclosing::LeftBrace) {
                        context.remove_last_enclosing();
                        context.dec_braces();
                    } else {
                        context.update_status(ParserStatus::LexicalError);
                    }

                    if context.last_enclosing() == Some(Enclosing::LeftBrace) {
                        context.update_status(ParserStatus::MapGotVal);
                    } else if context.last_enclosing() == Some(Enclosing::LeftBracket) {
                        context.update_status(ParserStatus::ArrayGotVal);
                    } else {
                        context.update_status(ParserStatus::GotValue);
                    }

                    Some(status)
                }
                Some(Token {
                    kind: TokenType::BracketOpen,
                    ..
                }) => {
                    let status = self.handler.handle_start_array(context);
                    context.add_enclosing(Enclosing::LeftBracket);
                    context.inc_brackets();
                    context.update_status(ParserStatus::ArrayStart);
                    Some(status)
                }
                Some(Token {
                    kind: TokenType::CurlyOpen,
                    ..
                }) => {
                    let status = self.handler.handle_start_map(context);
                    context.add_enclosing(Enclosing::LeftBrace);
                    context.inc_braces();
                    context.update_status(ParserStatus::MapStart);

                    Some(status)
                }
                Some(Token {
                    kind: TokenType::Null,
                    ..
                }) => {
                    let status = self.handler.handle_null(context);

                    update_context_status_value(context);

                    Some(status)
                }
                Some(Token {
                    kind: TokenType::Number,
                    buf,
                }) => {
                    let status = match buf {
                        Buffer::MultiByte(b) => match std::str::from_utf8(&b) {
                            Ok(s) => match s.parse::<i64>() {
                                Ok(num) => self.handler.handle_int(context, num),
                                Err(_) => match s.parse::<f64>() {
                                    Ok(num) => self.handler.handle_double(context, num),
                                    Err(_) => panic!("Could not parse number as i64 or f64"),
                                },
                            },
                            Err(e) => return Err(ParseError::MalformedJson(e.to_string())),
                        },
                        Buffer::Span(_) => panic!("Unexpected Span when handling number"),
                    };

                    update_context_status_value(context);

                    Some(status)
                }
                Some(Token {
                    kind: TokenType::String,
                    buf,
                }) => {
                    let string = match buf {
                        Buffer::MultiByte(ref b) => match std::str::from_utf8(b) {
                            Ok(s) => s,
                            Err(e) => return Err(ParseError::MalformedJson(e.to_string())),
                        },
                        Buffer::Span(_) => panic!("Unexpected span in string buffer"),
                    };

                    if context.parser_status() == ParserStatus::ArrayNeedVal
                        || context.parser_status() == ParserStatus::ArrayStart
                    {
                        let status = self.handler.handle_string(context, string);
                        context.update_status(ParserStatus::ArrayGotVal);
                        Some(status)
                    } else if context.parser_status() == ParserStatus::MapNeedVal {
                        let status = self.handler.handle_string(context, string);
                        context.update_status(ParserStatus::MapGotVal);
                        Some(status)
                    } else if context.parser_status() == ParserStatus::Start {
                        let status = self.handler.handle_string(context, string);
                        context.update_status(ParserStatus::GotValue);
                        Some(status)
                    } else if context.parser_status() == ParserStatus::MapNeedKey
                        || context.parser_status() == ParserStatus::MapStart
                    {
                        let status = self.handler.handle_map_key(context, string);
                        context.update_status(ParserStatus::MapSep);
                        Some(status)
                    } else {
                        context.update_status(ParserStatus::LexicalError);
                        None
                    }
                }
                Some(Token {
                    kind: TokenType::BooleanTrue,
                    ..
                }) => {
                    let status = self.handler.handle_bool(context, true);

                    update_context_status_value(context);

                    Some(status)
                }
                Some(Token {
                    kind: TokenType::BooleanFalse,
                    ..
                }) => {
                    let status = self.handler.handle_bool(context, false);

                    update_context_status_value(context);

                    Some(status)
                }
                Some(Token {
                    kind: TokenType::Comma,
                    ..
                }) => {
                    if context.parser_status() == ParserStatus::MapGotVal {
                        context.update_status(ParserStatus::MapNeedKey);
                    } else if context.parser_status() == ParserStatus::ArrayGotVal {
                        context.update_status(ParserStatus::ArrayNeedVal);
                    } else {
                        context.update_status(ParserStatus::LexicalError);
                    }

                    None
                }
                Some(Token {
                    kind: TokenType::Colon,
                    ..
                }) => {
                    if context.parser_status() == ParserStatus::MapSep {
                        context.update_status(ParserStatus::MapNeedVal);
                    }

                    None
                }
                Some(Token {
                    kind: TokenType::Invalid,
                    buf,
                }) => {
                    return Err(ParseError::MalformedJson(format!("{:?}", buf)));
                }
                None => {
                    self.context.update_status(ParserStatus::ParseComplete);
                    break;
                }
            };

            if status == Some(Status::Abort) {
                return Ok(());
            }
        }
        if self.context.parser_status() == ParserStatus::LexicalError {
            return Err(ParseError::MalformedJson(format!(
                "Parse failed due to malformed json: open braces: {}, open brackets: {}",
                self.context.num_open_braces(),
                self.context.num_open_brackets()
            )));
        }

        Ok(())
    }

    /// Parse has already returned from an EOF. This method checks that
    /// there were the right number of closing braces and brackets.
    ///
    /// # Errors
    ///    - Returns an error Result if the JSON was malformed.
    pub fn finish_parse(self) -> Result<(), ParseError> {
        if self.context.parser_status() != ParserStatus::ParseComplete {
            return Err(ParseError::MalformedJson(
                "Did not reach a ParseComplete status".to_owned(),
            ));
        }
        if self.context.num_open_braces() != 0 {
            return Err(ParseError::MalformedJson(format!(
                "Number of open braces: {}, number of open brackets: {}",
                self.context.num_open_braces(),
                self.context.num_open_brackets()
            )));
        }
        if self.context.num_open_brackets() != 0 {
            return Err(ParseError::MalformedJson(format!(
                "Number of open braces: {}, number of open brackets: {}",
                self.context.num_open_braces(),
                self.context.num_open_brackets()
            )));
        }

        Ok(())
    }
}

/// `ParseError`
#[derive(PartialEq, Eq)]
pub enum ParseError {
    /// Bytes can't be decoded as UTF-8 to a string.
    Utf8Error(String),
    /// The json is malformed in some way, missing closing brace, bracket, or
    /// a value can't be parsed due to being malformed.
    MalformedJson(String),
    /// Error of the underlying Reader.
    ReadError(String),
}

impl std::error::Error for ParseError {
    fn description(&self) -> &str {
        match self {
            ParseError::Utf8Error(ref msg)
            | ParseError::MalformedJson(ref msg)
            | ParseError::ReadError(ref msg) => msg,
        }
    }

    fn cause(&self) -> Option<&dyn std::error::Error> {
        None
    }
}

impl std::fmt::Display for ParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ParseError::Utf8Error(ref msg) => {
                write!(f, "Error converting bytes to UTF-8 encoded string: {}", msg)
            }
            ParseError::MalformedJson(ref msg) => write!(f, "Error: Malformed Json: {}", msg),
            ParseError::ReadError(ref msg) => write!(f, "Error: Read Error: {}", msg),
        }
    }
}

impl std::fmt::Debug for ParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Display::fmt(&self, f)
    }
}

impl From<std::io::Error> for ParseError {
    fn from(other: std::io::Error) -> Self {
        ParseError::ReadError(other.to_string())
    }
}

fn update_context_status_value(context: &mut Context) {
    if context.parser_status() == ParserStatus::ArrayNeedVal
        || context.parser_status() == ParserStatus::ArrayStart
    {
        context.update_status(ParserStatus::ArrayGotVal);
    } else if context.parser_status() == ParserStatus::MapNeedVal {
        context.update_status(ParserStatus::MapGotVal);
    } else if context.parser_status() == ParserStatus::Start {
        context.update_status(ParserStatus::GotValue);
    } else {
        context.update_status(ParserStatus::LexicalError);
    }
}

struct Wrapper<'a>(&'a mut dyn BufRead);

impl<'a> Iterator for Wrapper<'a> {
    type Item = u8;

    fn next(&mut self) -> Option<Self::Item> {
        let buffer = self.0.fill_buf().ok();
        match buffer.map(|b| b.first()).flatten().copied() {
            Some(b) => {
                self.0.consume(1);
                Some(b)
            }
            None => None,
        }
    }
}