@@ -24,21 +24,24 @@ use core::fmt;
24
24
25
25
use log:: debug;
26
26
27
- use crate :: dialect:: * ;
28
27
use crate :: keywords:: { self , Keyword } ;
29
28
use crate :: tokenizer:: * ;
30
29
use crate :: { ast:: * , span:: Span } ;
30
+ use crate :: { dialect:: * , span:: Spanned } ;
31
31
32
32
#[ derive( Debug , Clone , PartialEq ) ]
33
33
pub enum ParserError {
34
- TokenizerError ( String ) ,
35
- ParserError ( String ) ,
34
+ TokenizerError { message : String , span : Span } ,
35
+ ParserError { message : String , span : Span } ,
36
36
}
37
37
38
38
// Use `Parser::expected` instead, if possible
39
39
macro_rules! parser_err {
40
- ( $MSG: expr) => {
41
- Err ( ParserError :: ParserError ( $MSG. to_string( ) ) )
40
+ ( $MSG: expr, $SPAN: expr) => {
41
+ Err ( ParserError :: ParserError {
42
+ message: $MSG. to_string( ) ,
43
+ span: $SPAN,
44
+ } )
42
45
} ;
43
46
}
44
47
@@ -84,7 +87,10 @@ impl From<WildcardExpr> for FunctionArgExpr {
84
87
85
88
impl From < TokenizerError > for ParserError {
86
89
fn from ( e : TokenizerError ) -> Self {
87
- ParserError :: TokenizerError ( e. to_string ( ) )
90
+ ParserError :: TokenizerError {
91
+ message : e. to_string ( ) ,
92
+ span : e. span ,
93
+ }
88
94
}
89
95
}
90
96
@@ -94,13 +100,22 @@ impl fmt::Display for ParserError {
94
100
f,
95
101
"sql parser error: {}" ,
96
102
match self {
97
- ParserError :: TokenizerError ( s ) => s ,
98
- ParserError :: ParserError ( s ) => s ,
103
+ ParserError :: TokenizerError { message , .. } => message ,
104
+ ParserError :: ParserError { message , .. } => message ,
99
105
}
100
106
)
101
107
}
102
108
}
103
109
110
+ impl Spanned for ParserError {
111
+ fn span ( & self ) -> Span {
112
+ match self {
113
+ ParserError :: TokenizerError { span, .. } => * span,
114
+ ParserError :: ParserError { span, .. } => * span,
115
+ }
116
+ }
117
+ }
118
+
104
119
#[ cfg( feature = "std" ) ]
105
120
impl std:: error:: Error for ParserError { }
106
121
@@ -396,7 +411,7 @@ impl<'a> Parser<'a> {
396
411
// name, resulting in `NOT 'a'` being recognized as a `TypedString` instead of
397
412
// an unary negation `NOT ('a' LIKE 'b')`. To solve this, we don't accept the
398
413
// `type 'string'` syntax for the custom data types at all.
399
- DataType :: Custom ( ..) => parser_err!( "dummy" ) ,
414
+ DataType :: Custom ( ..) => parser_err!( "dummy" , Span :: new ( ) ) ,
400
415
data_type => Ok ( Expr :: TypedString {
401
416
data_type,
402
417
value: parser. parse_literal_string( ) ?,
@@ -1059,10 +1074,13 @@ impl<'a> Parser<'a> {
1059
1074
}
1060
1075
}
1061
1076
// Can only happen if `get_next_precedence` got out of sync with this function
1062
- _ => parser_err ! ( format!(
1063
- "No infix parser for token '{}'" ,
1064
- Token :: Word { value, span }
1065
- ) ) ,
1077
+ _ => parser_err ! (
1078
+ format!(
1079
+ "No infix parser for token '{}'" ,
1080
+ Token :: Word { value, span } ,
1081
+ ) ,
1082
+ span
1083
+ ) ,
1066
1084
} ,
1067
1085
Token :: DoubleColon { .. } => self . parse_pg_cast ( expr) ,
1068
1086
Token :: ExclamationMark { .. } =>
@@ -1077,7 +1095,7 @@ impl<'a> Parser<'a> {
1077
1095
_ =>
1078
1096
// Can only happen if `get_next_precedence` got out of sync with this function
1079
1097
{
1080
- parser_err ! ( format!( "No infix parser for token '{}'" , tok) )
1098
+ parser_err ! ( format!( "No infix parser for token '{}'" , tok) , tok . span ( ) )
1081
1099
}
1082
1100
}
1083
1101
}
@@ -1282,7 +1300,10 @@ impl<'a> Parser<'a> {
1282
1300
1283
1301
/// Report unexpected token
1284
1302
fn expected < T > ( & self , expected : & str , found : Token ) -> Result < T , ParserError > {
1285
- parser_err ! ( format!( "Expected {}, found: {}" , expected, found) )
1303
+ parser_err ! (
1304
+ format!( "Expected {}, found: {}" , expected, found) ,
1305
+ found. span( )
1306
+ )
1286
1307
}
1287
1308
1288
1309
/// Look for an expected keyword and consume it if it exists
@@ -1415,7 +1436,10 @@ impl<'a> Parser<'a> {
1415
1436
let all = self . parse_keyword ( Keyword :: ALL ) ;
1416
1437
let distinct = self . parse_keyword ( Keyword :: DISTINCT ) ;
1417
1438
if all && distinct {
1418
- return parser_err ! ( "Cannot specify both ALL and DISTINCT" . to_string( ) ) ;
1439
+ return parser_err ! (
1440
+ "Cannot specify both ALL and DISTINCT" . to_string( ) ,
1441
+ self . peek_token( ) . span( )
1442
+ ) ;
1419
1443
} else {
1420
1444
Ok ( distinct)
1421
1445
}
@@ -1600,10 +1624,14 @@ impl<'a> Parser<'a> {
1600
1624
let names = self . parse_comma_separated ( Parser :: parse_object_name) ?;
1601
1625
let cascade = self . parse_keyword ( Keyword :: CASCADE ) ;
1602
1626
let restrict = self . parse_keyword ( Keyword :: RESTRICT ) ;
1603
- let purge = self . parse_keyword ( Keyword :: PURGE ) ;
1604
1627
if cascade && restrict {
1605
- return parser_err ! ( "Cannot specify both CASCADE and RESTRICT in DROP" ) ;
1628
+ self . prev_token ( ) ;
1629
+ return parser_err ! (
1630
+ "Cannot specify both CASCADE and RESTRICT in DROP" ,
1631
+ self . peek_token( ) . span( )
1632
+ ) ;
1606
1633
}
1634
+ let purge = self . parse_keyword ( Keyword :: PURGE ) ;
1607
1635
Ok ( Statement :: Drop {
1608
1636
object_type,
1609
1637
if_exists,
@@ -2182,10 +2210,15 @@ impl<'a> Parser<'a> {
2182
2210
// bigdecimal feature is enabled, and is otherwise a no-op
2183
2211
// (i.e., it returns the input string).
2184
2212
Token :: Number {
2185
- ref value, long, ..
2213
+ ref value,
2214
+ long,
2215
+ span,
2186
2216
} => match value. parse ( ) {
2187
2217
Ok ( n) => Ok ( Value :: Number ( n, long) ) ,
2188
- Err ( e) => parser_err ! ( format!( "Could not parse '{}' as number: {}" , value, e) ) ,
2218
+ Err ( e) => parser_err ! (
2219
+ format!( "Could not parse '{}' as number: {}" , value, e) ,
2220
+ span
2221
+ ) ,
2189
2222
} ,
2190
2223
Token :: SingleQuotedString { ref value, .. } => {
2191
2224
Ok ( Value :: SingleQuotedString ( value. to_string ( ) ) )
@@ -2213,9 +2246,12 @@ impl<'a> Parser<'a> {
2213
2246
/// Parse an unsigned literal integer/long
2214
2247
pub fn parse_literal_uint ( & mut self ) -> Result < u64 , ParserError > {
2215
2248
match self . next_token ( ) {
2216
- Token :: Number { value, .. } => value. parse :: < u64 > ( ) . map_err ( |e| {
2217
- ParserError :: ParserError ( format ! ( "Could not parse '{}' as u64: {}" , value, e) )
2218
- } ) ,
2249
+ Token :: Number { value, span, .. } => {
2250
+ value. parse :: < u64 > ( ) . map_err ( |e| ParserError :: ParserError {
2251
+ message : format ! ( "Could not parse '{}' as u64: {}" , value, e) ,
2252
+ span,
2253
+ } )
2254
+ }
2219
2255
unexpected => self . expected ( "literal int" , unexpected) ,
2220
2256
}
2221
2257
}
@@ -2881,10 +2917,10 @@ impl<'a> Parser<'a> {
2881
2917
Keyword :: FUNCTION => Ok ( ShowCreateObject :: Function ) ,
2882
2918
Keyword :: PROCEDURE => Ok ( ShowCreateObject :: Procedure ) ,
2883
2919
Keyword :: EVENT => Ok ( ShowCreateObject :: Event ) ,
2884
- keyword => Err ( ParserError :: ParserError ( format ! (
2885
- "Unable to map keyword to ShowCreateObject: {:?}" ,
2886
- keyword
2887
- ) ) ) ,
2920
+ keyword => Err ( ParserError :: ParserError {
2921
+ message : format ! ( "Unable to map keyword to ShowCreateObject: {:?}" , keyword ) ,
2922
+ span : self . peek_token ( ) . span ( ) ,
2923
+ } ) ,
2888
2924
} ?;
2889
2925
2890
2926
let obj_name = self . parse_object_name ( ) ?;
@@ -3076,10 +3112,10 @@ impl<'a> Parser<'a> {
3076
3112
| TableFactor :: TableFunction { alias, .. } => {
3077
3113
// but not `FROM (mytable AS alias1) AS alias2`.
3078
3114
if let Some ( inner_alias) = alias {
3079
- return Err ( ParserError :: ParserError ( format ! (
3080
- "duplicate alias {}" ,
3081
- inner_alias
3082
- ) ) ) ;
3115
+ return Err ( ParserError :: ParserError {
3116
+ message : format ! ( "duplicate alias {}" , inner_alias ) ,
3117
+ span : self . peek_token ( ) . span ( ) ,
3118
+ } ) ;
3083
3119
}
3084
3120
// Act as if the alias was specified normally next
3085
3121
// to the table name: `(mytable) AS alias` ->
@@ -3287,7 +3323,11 @@ impl<'a> Parser<'a> {
3287
3323
let cascade = self . parse_keyword ( Keyword :: CASCADE ) ;
3288
3324
let restrict = self . parse_keyword ( Keyword :: RESTRICT ) ;
3289
3325
if cascade && restrict {
3290
- return parser_err ! ( "Cannot specify both CASCADE and RESTRICT in REVOKE" ) ;
3326
+ self . prev_token ( ) ;
3327
+ return parser_err ! (
3328
+ "Cannot specify both CASCADE and RESTRICT in REVOKE" ,
3329
+ self . peek_token( ) . span( )
3330
+ ) ;
3291
3331
}
3292
3332
3293
3333
Ok ( Statement :: Revoke {
0 commit comments