@@ -23,6 +23,7 @@ use alloc::{
23
23
use core:: fmt;
24
24
25
25
use log:: debug;
26
+
26
27
use recursion:: RecursionCounter ;
27
28
use IsLateral :: * ;
28
29
use IsOptional :: * ;
@@ -2319,8 +2320,23 @@ impl<'a> Parser<'a> {
2319
2320
2320
2321
/// Report unexpected token
2321
2322
pub fn expected < T > ( & self , expected : & str , found : TokenWithLocation ) -> Result < T , ParserError > {
2323
+ let start_off = self . index . saturating_sub ( 10 ) ;
2324
+ let end_off = self . index . min ( self . tokens . len ( ) ) ;
2325
+ let near_tokens = & self . tokens [ start_off..end_off] ;
2326
+ struct TokensDisplay < ' a > ( & ' a [ TokenWithLocation ] ) ;
2327
+ impl < ' a > fmt:: Display for TokensDisplay < ' a > {
2328
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
2329
+ for token in self . 0 {
2330
+ write ! ( f, "{}" , token) ?;
2331
+ }
2332
+ Ok ( ( ) )
2333
+ }
2334
+ }
2322
2335
parser_err ! (
2323
- format!( "Expected {expected}, found: {found}" ) ,
2336
+ format!(
2337
+ "Expected {expected}, found: {found}\n Near `{}`" ,
2338
+ TokensDisplay ( near_tokens)
2339
+ ) ,
2324
2340
found. location
2325
2341
)
2326
2342
}
@@ -8234,7 +8250,7 @@ mod tests {
8234
8250
assert_eq ! (
8235
8251
ast,
8236
8252
Err ( ParserError :: ParserError (
8237
- "Expected [NOT] NULL or TRUE|FALSE or [NOT] DISTINCT FROM after IS, found: a at Line: 1, Column 16"
8253
+ "Expected [NOT] NULL or TRUE|FALSE or [NOT] DISTINCT FROM after IS, found: a\n Near `SELECT this is` at Line: 1, Column 16"
8238
8254
. to_string( )
8239
8255
) )
8240
8256
) ;
0 commit comments