Skip to content

Commit

Permalink
correct parse_object fn
Browse files Browse the repository at this point in the history
  • Loading branch information
taytzehao committed Oct 8, 2023
1 parent 04f33a1 commit bb85ba9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
50 changes: 48 additions & 2 deletions src/sqlparser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1793,6 +1793,11 @@ impl Parser {
}
}

/// Return nth non-whitespace token that has not yet been processed
pub fn peek_token_no_skip(&self) -> Option<&TokenWithLocation> {
self.tokens.get(self.index)
}

/// Return the first non-whitespace token that has not yet been processed
/// (or None if reached end-of-file) and mark it as processed. OK to call
/// repeatedly after reaching EOF.
Expand Down Expand Up @@ -1855,6 +1860,28 @@ impl Parser {
))
}

fn get_near_tokens_display(&self) -> String {
let start_off = self.index.saturating_sub(10);
let end_off = self.index.min(self.tokens.len());
let near_tokens = &self.tokens[start_off..end_off];

// Directly create a formatted string of the nearby tokens
let tokens_str: Vec<String> = near_tokens
.iter()
.map(|token| format!("{}", token.token))
.collect();

format!("Near \"{}\"", tokens_str.join(""))
}

pub fn at_or_near_error<T>(&self, found: TokenWithLocation) -> Result<T, ParserError> {
parser_err!(format!(
"syntax error at or near {}\n{}",
found,
self.get_near_tokens_display()
))
}

/// Look for an expected keyword and consume it if it exists
#[must_use]
pub fn parse_keyword(&mut self, expected: Keyword) -> bool {
Expand Down Expand Up @@ -3335,8 +3362,27 @@ impl Parser {
let mut idents = vec![];
loop {
idents.push(self.parse_identifier()?);
if !self.consume_token(&Token::Period) {
break;

match self.peek_token_no_skip() {
Some(token_with_location) => match token_with_location.token {
Token::Period => {
self.next_token();
}
Token::Minus => {
return self.at_or_near_error(
token_with_location
.token
.clone()
.with_location(token_with_location.clone().location),
);
}
_ => {
break;
}
},
None => {
break;
}
}
}
Ok(ObjectName(idents))
Expand Down
4 changes: 4 additions & 0 deletions src/sqlparser/tests/testdata/create.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@
error_msg: |-
sql parser error: Expected literal string, found: null at line:1, column:45
Near " tmp with encrypted password null"
- input: CREATE SOURCE foo-bar WITH () FORMAT PLAIN ENCODE PROTOBUF ();
error_msg: |-
sql parser error: syntax error at or near - at line:1, column:19
Near "CREATE SOURCE foo"

0 comments on commit bb85ba9

Please sign in to comment.