kawasin73 / prsqlite

Pure Rust implementation of SQLite
Apache License 2.0
563 stars 23 forks source link

Parser cursor implementation #24

Closed kawasin73 closed 1 year ago

kawasin73 commented 1 year ago

When parser fails, it returns the position of cursor as an error.

However adding the base offset on each parse* functions is easy to miss. Pass the base offset to the parse sub-functions to generate the error without accumulating offsets in nested parse_ calls.

// before

fn parse_type_name(input: &[u8]) -> Result<(usize, Vec<MaybeQuotedBytes<'_>>)> {

// after

fn parse_type_name(offset: usize, input: &[u8]) -> Result<Vec<MaybeQuotedBytes<'_>>> {
/// Get next token skipping spaces.
///
/// This also proceeds the cursor even if the input has spaces on.
pub fn next_token<'a>(cur: &mut usize, input: &'a [u8]) -> Option<Token<'a>> {
    let (mut len, mut token) = get_token(&input[*cur..])?;
    *cur += len;
    if token == Token::Space {
        (len, token) = get_token(&input[*cur..])?;
        *cur += len;
    }
    Some(token)
}