@@ -1158,7 +1158,7 @@ impl<'a> Parser<'a> {
11581158 let mut id_parts: Vec<Ident> = vec![w.to_ident(w_span)];
11591159 let mut ending_wildcard: Option<TokenWithSpan> = None;
11601160 while self.consume_token(&Token::Period) {
1161- let next_token = self.next_token_ref ();
1161+ let ( next_token, next_token_index) = self.next_token_ref_with_index ();
11621162 match &next_token.token {
11631163 Token::Word(w) => id_parts.push(w.to_ident(next_token.span)),
11641164 Token::Mul => {
@@ -1168,12 +1168,14 @@ impl<'a> Parser<'a> {
11681168 ending_wildcard = Some(next_token.clone());
11691169 break;
11701170 } else {
1171- return self.expected_current("an identifier after '.'");
1171+ return self
1172+ .expected_at("an identifier after '.'", next_token_index);
11721173 }
11731174 }
11741175 Token::SingleQuotedString(s) => id_parts.push(Ident::with_quote('\'', s)),
11751176 _ => {
1176- return self.expected_current("an identifier or a '*' after '.'");
1177+ return self
1178+ .expected_at("an identifier or a '*' after '.'", next_token_index);
11771179 }
11781180 }
11791181 }
@@ -1274,7 +1276,7 @@ impl<'a> Parser<'a> {
12741276
12751277 let dialect = self.dialect;
12761278
1277- let next_token = self.next_token_ref ();
1279+ let ( next_token, next_token_index) = self.next_token_ref_with_index ();
12781280 let span = next_token.span;
12791281 let expr = match &next_token.token {
12801282 Token::Word(w) => {
@@ -1423,7 +1425,7 @@ impl<'a> Parser<'a> {
14231425 self.prev_token();
14241426 self.parse_duckdb_struct_literal()
14251427 }
1426- _ => self.expected_current ("an expression"),
1428+ _ => self.expected_at ("an expression", next_token_index ),
14271429 }?;
14281430
14291431 let expr = self.try_parse_method(expr)?;
@@ -3424,7 +3426,7 @@ impl<'a> Parser<'a> {
34243426 token: Token::Whitespace(_),
34253427 span: _,
34263428 }) => continue,
3427- token => return (token.unwrap_or(&EOF_TOKEN), self.index),
3429+ token => return (token.unwrap_or(&EOF_TOKEN), self.index - 1 ),
34283430 }
34293431 }
34303432 }
@@ -3461,9 +3463,17 @@ impl<'a> Parser<'a> {
34613463 )
34623464 }
34633465
3466+ /// report `found` was encountered instead of `expected`
3467+ pub fn expected_ref<T>(&self, expected: &str, found: &TokenWithSpan) -> Result<T, ParserError> {
3468+ parser_err!(
3469+ format!("Expected: {expected}, found: {found}"),
3470+ found.span.start
3471+ )
3472+ }
3473+
34643474 /// Report that the current token was found instead of `expected`.
3465- pub fn expected_current <T>(&self, expected: &str) -> Result<T, ParserError> {
3466- let found = self.tokens.get(self. index).unwrap_or(&EOF_TOKEN);
3475+ pub fn expected_at <T>(&self, expected: &str, index: usize ) -> Result<T, ParserError> {
3476+ let found = self.tokens.get(index).unwrap_or(&EOF_TOKEN);
34673477 parser_err!(
34683478 format!("Expected: {expected}, found: {found}"),
34693479 found.span.start
@@ -3568,7 +3578,10 @@ impl<'a> Parser<'a> {
35683578 Ok(keyword)
35693579 } else {
35703580 let keywords: Vec<String> = keywords.iter().map(|x| format!("{x:?}")).collect();
3571- self.expected_current(&format!("one of {}", keywords.join(" or ")))
3581+ self.expected_ref(
3582+ &format!("one of {}", keywords.join(" or ")),
3583+ self.peek_token_ref(),
3584+ )
35723585 }
35733586 }
35743587
@@ -3578,7 +3591,7 @@ impl<'a> Parser<'a> {
35783591 if let Some(token) = self.parse_keyword_token_ref(expected) {
35793592 Ok(token.clone())
35803593 } else {
3581- self.expected_current (format!("{:?}", &expected).as_str())
3594+ self.expected_ref (format!("{:?}", &expected).as_str(), self.peek_token_ref ())
35823595 }
35833596 }
35843597
@@ -3591,7 +3604,7 @@ impl<'a> Parser<'a> {
35913604 if self.parse_keyword_token_ref(expected).is_some() {
35923605 Ok(())
35933606 } else {
3594- self.expected_current (format!("{:?}", &expected).as_str())
3607+ self.expected_ref (format!("{:?}", &expected).as_str(), self.peek_token_ref ())
35953608 }
35963609 }
35973610
@@ -3635,7 +3648,7 @@ impl<'a> Parser<'a> {
36353648 if self.peek_token_ref() == expected {
36363649 Ok(self.next_token())
36373650 } else {
3638- self.expected_current (&expected.to_string())
3651+ self.expected_ref (&expected.to_string(), self.peek_token_ref ())
36393652 }
36403653 }
36413654
@@ -8104,7 +8117,7 @@ impl<'a> Parser<'a> {
81048117 &mut self,
81058118 ) -> Result<(DataType, MatchedTrailingBracket), ParserError> {
81068119 let dialect = self.dialect;
8107- let next_token = self.next_token_ref ();
8120+ let ( next_token, next_token_index) = self.next_token_ref_with_index ();
81088121 let mut trailing_bracket: MatchedTrailingBracket = false.into();
81098122 let mut data = match &next_token.token {
81108123 Token::Word(w) => match w.keyword {
@@ -8402,7 +8415,7 @@ impl<'a> Parser<'a> {
84028415 }
84038416 }
84048417 },
8405- _ => self.expected_current ("a data type name"),
8418+ _ => self.expected_at ("a data type name", next_token_index ),
84068419 }?;
84078420
84088421 // Parse array data types. Note: this is postgresql-specific and different from
0 commit comments