@@ -44,8 +44,8 @@ pub enum ParserError {
44
44
45
45
// Use `Parser::expected` instead, if possible
46
46
macro_rules! parser_err {
47
- ( $MSG: expr) => {
48
- Err ( ParserError :: ParserError ( $MSG. to_string ( ) ) )
47
+ ( $MSG: expr, $loc : expr ) => {
48
+ Err ( ParserError :: ParserError ( format! ( "{}{}" , $MSG, $loc ) ) )
49
49
} ;
50
50
}
51
51
@@ -724,6 +724,7 @@ impl<'a> Parser<'a> {
724
724
// Note also that naively `SELECT date` looks like a syntax error because the `date` type
725
725
// name is not followed by a string literal, but in fact in PostgreSQL it is a valid
726
726
// expression that should parse as the column name "date".
727
+ let loc = self . peek_token ( ) . location ;
727
728
return_ok_if_some ! ( self . maybe_parse( |parser| {
728
729
match parser. parse_data_type( ) ? {
729
730
DataType :: Interval => parser. parse_interval( ) ,
@@ -734,7 +735,7 @@ impl<'a> Parser<'a> {
734
735
// name, resulting in `NOT 'a'` being recognized as a `TypedString` instead of
735
736
// an unary negation `NOT ('a' LIKE 'b')`. To solve this, we don't accept the
736
737
// `type 'string'` syntax for the custom data types at all.
737
- DataType :: Custom ( ..) => parser_err!( "dummy" ) ,
738
+ DataType :: Custom ( ..) => parser_err!( "dummy" , loc ) ,
738
739
data_type => Ok ( Expr :: TypedString {
739
740
data_type,
740
741
value: parser. parse_literal_string( ) ?,
@@ -909,7 +910,7 @@ impl<'a> Parser<'a> {
909
910
let tok = self . next_token ( ) ;
910
911
let key = match tok. token {
911
912
Token :: Word ( word) => word. to_ident ( ) ,
912
- _ => return parser_err ! ( format!( "Expected identifier, found: {tok}" ) ) ,
913
+ _ => return parser_err ! ( format!( "Expected identifier, found: {tok}" ) , tok . location ) ,
913
914
} ;
914
915
Ok ( Expr :: CompositeAccess {
915
916
expr : Box :: new ( expr) ,
@@ -1220,7 +1221,7 @@ impl<'a> Parser<'a> {
1220
1221
r#in : Box :: new ( from) ,
1221
1222
} )
1222
1223
} else {
1223
- parser_err ! ( "Position function must include IN keyword" . to_string( ) )
1224
+ parser_err ! ( "Position function must include IN keyword" . to_string( ) , self . peek_token ( ) . location )
1224
1225
}
1225
1226
}
1226
1227
@@ -1884,7 +1885,7 @@ impl<'a> Parser<'a> {
1884
1885
}
1885
1886
}
1886
1887
// Can only happen if `get_next_precedence` got out of sync with this function
1887
- _ => parser_err ! ( format!( "No infix parser for token {:?}" , tok. token) ) ,
1888
+ _ => parser_err ! ( format!( "No infix parser for token {:?}" , tok. token) , tok . location ) ,
1888
1889
}
1889
1890
} else if Token :: DoubleColon == tok {
1890
1891
self . parse_pg_cast ( expr)
@@ -1935,7 +1936,7 @@ impl<'a> Parser<'a> {
1935
1936
} )
1936
1937
} else {
1937
1938
// Can only happen if `get_next_precedence` got out of sync with this function
1938
- parser_err ! ( format!( "No infix parser for token {:?}" , tok. token) )
1939
+ parser_err ! ( format!( "No infix parser for token {:?}" , tok. token) , tok . location )
1939
1940
}
1940
1941
}
1941
1942
@@ -2213,7 +2214,7 @@ impl<'a> Parser<'a> {
2213
2214
2214
2215
/// Report unexpected token
2215
2216
pub fn expected < T > ( & self , expected : & str , found : TokenWithLocation ) -> Result < T , ParserError > {
2216
- parser_err ! ( format!( "Expected {expected}, found: {found}" ) )
2217
+ parser_err ! ( format!( "Expected {expected}, found: {found}" ) , found . location )
2217
2218
}
2218
2219
2219
2220
/// Look for an expected keyword and consume it if it exists
@@ -2378,13 +2379,14 @@ impl<'a> Parser<'a> {
2378
2379
/// Parse either `ALL`, `DISTINCT` or `DISTINCT ON (...)`. Returns `None` if `ALL` is parsed
2379
2380
/// and results in a `ParserError` if both `ALL` and `DISTINCT` are found.
2380
2381
pub fn parse_all_or_distinct ( & mut self ) -> Result < Option < Distinct > , ParserError > {
2382
+ let loc = self . peek_token ( ) . location ;
2381
2383
let all = self . parse_keyword ( Keyword :: ALL ) ;
2382
2384
let distinct = self . parse_keyword ( Keyword :: DISTINCT ) ;
2383
2385
if !distinct {
2384
2386
return Ok ( None ) ;
2385
2387
}
2386
2388
if all {
2387
- return parser_err ! ( "Cannot specify both ALL and DISTINCT" . to_string( ) ) ;
2389
+ return parser_err ! ( "Cannot specify both ALL and DISTINCT" . to_string( ) , loc ) ;
2388
2390
}
2389
2391
let on = self . parse_keyword ( Keyword :: ON ) ;
2390
2392
if !on {
@@ -2985,75 +2987,76 @@ impl<'a> Parser<'a> {
2985
2987
let mut user = vec ! [ ] ;
2986
2988
let mut admin = vec ! [ ] ;
2987
2989
2988
- while let Some ( keyword) = self . parse_one_of_keywords ( & optional_keywords) {
2990
+ while let Some ( keyword) = self . parse_one_of_keywords ( & optional_keywords) {
2991
+ let loc = self . tokens . get ( self . index - 1 ) . map_or ( Location { line : 0 , column : 0 } , |t| t. location ) ;
2989
2992
match keyword {
2990
2993
Keyword :: AUTHORIZATION => {
2991
2994
if authorization_owner. is_some ( ) {
2992
- parser_err ! ( "Found multiple AUTHORIZATION" )
2995
+ parser_err ! ( "Found multiple AUTHORIZATION" , loc )
2993
2996
} else {
2994
2997
authorization_owner = Some ( self . parse_object_name ( ) ?) ;
2995
2998
Ok ( ( ) )
2996
2999
}
2997
3000
}
2998
3001
Keyword :: LOGIN | Keyword :: NOLOGIN => {
2999
3002
if login. is_some ( ) {
3000
- parser_err ! ( "Found multiple LOGIN or NOLOGIN" )
3003
+ parser_err ! ( "Found multiple LOGIN or NOLOGIN" , loc )
3001
3004
} else {
3002
3005
login = Some ( keyword == Keyword :: LOGIN ) ;
3003
3006
Ok ( ( ) )
3004
3007
}
3005
3008
}
3006
3009
Keyword :: INHERIT | Keyword :: NOINHERIT => {
3007
3010
if inherit. is_some ( ) {
3008
- parser_err ! ( "Found multiple INHERIT or NOINHERIT" )
3011
+ parser_err ! ( "Found multiple INHERIT or NOINHERIT" , loc )
3009
3012
} else {
3010
3013
inherit = Some ( keyword == Keyword :: INHERIT ) ;
3011
3014
Ok ( ( ) )
3012
3015
}
3013
3016
}
3014
3017
Keyword :: BYPASSRLS | Keyword :: NOBYPASSRLS => {
3015
3018
if bypassrls. is_some ( ) {
3016
- parser_err ! ( "Found multiple BYPASSRLS or NOBYPASSRLS" )
3019
+ parser_err ! ( "Found multiple BYPASSRLS or NOBYPASSRLS" , loc )
3017
3020
} else {
3018
3021
bypassrls = Some ( keyword == Keyword :: BYPASSRLS ) ;
3019
3022
Ok ( ( ) )
3020
3023
}
3021
3024
}
3022
3025
Keyword :: CREATEDB | Keyword :: NOCREATEDB => {
3023
3026
if create_db. is_some ( ) {
3024
- parser_err ! ( "Found multiple CREATEDB or NOCREATEDB" )
3027
+ parser_err ! ( "Found multiple CREATEDB or NOCREATEDB" , loc )
3025
3028
} else {
3026
3029
create_db = Some ( keyword == Keyword :: CREATEDB ) ;
3027
3030
Ok ( ( ) )
3028
3031
}
3029
3032
}
3030
3033
Keyword :: CREATEROLE | Keyword :: NOCREATEROLE => {
3031
3034
if create_role. is_some ( ) {
3032
- parser_err ! ( "Found multiple CREATEROLE or NOCREATEROLE" )
3035
+ parser_err ! ( "Found multiple CREATEROLE or NOCREATEROLE" , loc )
3033
3036
} else {
3034
3037
create_role = Some ( keyword == Keyword :: CREATEROLE ) ;
3035
3038
Ok ( ( ) )
3036
3039
}
3037
3040
}
3038
3041
Keyword :: SUPERUSER | Keyword :: NOSUPERUSER => {
3039
3042
if superuser. is_some ( ) {
3040
- parser_err ! ( "Found multiple SUPERUSER or NOSUPERUSER" )
3043
+ parser_err ! ( "Found multiple SUPERUSER or NOSUPERUSER" , loc )
3041
3044
} else {
3042
3045
superuser = Some ( keyword == Keyword :: SUPERUSER ) ;
3043
3046
Ok ( ( ) )
3044
3047
}
3045
3048
}
3046
3049
Keyword :: REPLICATION | Keyword :: NOREPLICATION => {
3047
3050
if replication. is_some ( ) {
3048
- parser_err ! ( "Found multiple REPLICATION or NOREPLICATION" )
3051
+ parser_err ! ( "Found multiple REPLICATION or NOREPLICATION" , loc )
3049
3052
} else {
3050
3053
replication = Some ( keyword == Keyword :: REPLICATION ) ;
3051
3054
Ok ( ( ) )
3052
3055
}
3053
3056
}
3054
3057
Keyword :: PASSWORD => {
3055
3058
if password. is_some ( ) {
3056
- parser_err ! ( "Found multiple PASSWORD" )
3059
+ parser_err ! ( "Found multiple PASSWORD" , loc )
3057
3060
} else {
3058
3061
password = if self . parse_keyword ( Keyword :: NULL ) {
3059
3062
Some ( Password :: NullPassword )
@@ -3066,7 +3069,7 @@ impl<'a> Parser<'a> {
3066
3069
Keyword :: CONNECTION => {
3067
3070
self . expect_keyword ( Keyword :: LIMIT ) ?;
3068
3071
if connection_limit. is_some ( ) {
3069
- parser_err ! ( "Found multiple CONNECTION LIMIT" )
3072
+ parser_err ! ( "Found multiple CONNECTION LIMIT" , loc )
3070
3073
} else {
3071
3074
connection_limit = Some ( Expr :: Value ( self . parse_number_value ( ) ?) ) ;
3072
3075
Ok ( ( ) )
@@ -3075,7 +3078,7 @@ impl<'a> Parser<'a> {
3075
3078
Keyword :: VALID => {
3076
3079
self . expect_keyword ( Keyword :: UNTIL ) ?;
3077
3080
if valid_until. is_some ( ) {
3078
- parser_err ! ( "Found multiple VALID UNTIL" )
3081
+ parser_err ! ( "Found multiple VALID UNTIL" , loc )
3079
3082
} else {
3080
3083
valid_until = Some ( Expr :: Value ( self . parse_value ( ) ?) ) ;
3081
3084
Ok ( ( ) )
@@ -3084,14 +3087,14 @@ impl<'a> Parser<'a> {
3084
3087
Keyword :: IN => {
3085
3088
if self . parse_keyword ( Keyword :: ROLE ) {
3086
3089
if !in_role. is_empty ( ) {
3087
- parser_err ! ( "Found multiple IN ROLE" )
3090
+ parser_err ! ( "Found multiple IN ROLE" , loc )
3088
3091
} else {
3089
3092
in_role = self . parse_comma_separated ( Parser :: parse_identifier) ?;
3090
3093
Ok ( ( ) )
3091
3094
}
3092
3095
} else if self . parse_keyword ( Keyword :: GROUP ) {
3093
3096
if !in_group. is_empty ( ) {
3094
- parser_err ! ( "Found multiple IN GROUP" )
3097
+ parser_err ! ( "Found multiple IN GROUP" , loc )
3095
3098
} else {
3096
3099
in_group = self . parse_comma_separated ( Parser :: parse_identifier) ?;
3097
3100
Ok ( ( ) )
@@ -3102,23 +3105,23 @@ impl<'a> Parser<'a> {
3102
3105
}
3103
3106
Keyword :: ROLE => {
3104
3107
if !role. is_empty ( ) {
3105
- parser_err ! ( "Found multiple ROLE" )
3108
+ parser_err ! ( "Found multiple ROLE" , loc )
3106
3109
} else {
3107
3110
role = self . parse_comma_separated ( Parser :: parse_identifier) ?;
3108
3111
Ok ( ( ) )
3109
3112
}
3110
3113
}
3111
3114
Keyword :: USER => {
3112
3115
if !user. is_empty ( ) {
3113
- parser_err ! ( "Found multiple USER" )
3116
+ parser_err ! ( "Found multiple USER" , loc )
3114
3117
} else {
3115
3118
user = self . parse_comma_separated ( Parser :: parse_identifier) ?;
3116
3119
Ok ( ( ) )
3117
3120
}
3118
3121
}
3119
3122
Keyword :: ADMIN => {
3120
3123
if !admin. is_empty ( ) {
3121
- parser_err ! ( "Found multiple ADMIN" )
3124
+ parser_err ! ( "Found multiple ADMIN" , loc )
3122
3125
} else {
3123
3126
admin = self . parse_comma_separated ( Parser :: parse_identifier) ?;
3124
3127
Ok ( ( ) )
@@ -3181,14 +3184,16 @@ impl<'a> Parser<'a> {
3181
3184
// specifying multiple objects to delete in a single statement
3182
3185
let if_exists = self . parse_keywords ( & [ Keyword :: IF , Keyword :: EXISTS ] ) ;
3183
3186
let names = self . parse_comma_separated ( Parser :: parse_object_name) ?;
3187
+
3188
+ let loc = self . peek_token ( ) . location ;
3184
3189
let cascade = self . parse_keyword ( Keyword :: CASCADE ) ;
3185
3190
let restrict = self . parse_keyword ( Keyword :: RESTRICT ) ;
3186
3191
let purge = self . parse_keyword ( Keyword :: PURGE ) ;
3187
3192
if cascade && restrict {
3188
- return parser_err ! ( "Cannot specify both CASCADE and RESTRICT in DROP" ) ;
3193
+ return parser_err ! ( "Cannot specify both CASCADE and RESTRICT in DROP" , loc ) ;
3189
3194
}
3190
3195
if object_type == ObjectType :: Role && ( cascade || restrict || purge) {
3191
- return parser_err ! ( "Cannot specify CASCADE, RESTRICT, or PURGE in DROP ROLE" ) ;
3196
+ return parser_err ! ( "Cannot specify CASCADE, RESTRICT, or PURGE in DROP ROLE" , loc ) ;
3192
3197
}
3193
3198
Ok ( Statement :: Drop {
3194
3199
object_type,
@@ -4446,7 +4451,8 @@ impl<'a> Parser<'a> {
4446
4451
fn parse_literal_char ( & mut self ) -> Result < char , ParserError > {
4447
4452
let s = self . parse_literal_string ( ) ?;
4448
4453
if s. len ( ) != 1 {
4449
- return parser_err ! ( format!( "Expect a char, found {s:?}" ) ) ;
4454
+ let loc = self . tokens . get ( self . index - 1 ) . map_or ( Location { line : 0 , column : 0 } , |t| t. location ) ;
4455
+ return parser_err ! ( format!( "Expect a char, found {s:?}" ) , loc) ;
4450
4456
}
4451
4457
Ok ( s. chars ( ) . next ( ) . unwrap ( ) )
4452
4458
}
@@ -4525,7 +4531,7 @@ impl<'a> Parser<'a> {
4525
4531
// (i.e., it returns the input string).
4526
4532
Token :: Number ( ref n, l) => match n. parse ( ) {
4527
4533
Ok ( n) => Ok ( Value :: Number ( n, l) ) ,
4528
- Err ( e) => parser_err ! ( format!( "Could not parse '{n}' as number: {e}" ) ) ,
4534
+ Err ( e) => parser_err ! ( format!( "Could not parse '{n}' as number: {e}" ) , location ) ,
4529
4535
} ,
4530
4536
Token :: SingleQuotedString ( ref s) => Ok ( Value :: SingleQuotedString ( s. to_string ( ) ) ) ,
4531
4537
Token :: DoubleQuotedString ( ref s) => Ok ( Value :: DoubleQuotedString ( s. to_string ( ) ) ) ,
@@ -6465,10 +6471,11 @@ impl<'a> Parser<'a> {
6465
6471
. parse_keywords ( & [ Keyword :: GRANTED , Keyword :: BY ] )
6466
6472
. then ( || self . parse_identifier ( ) . unwrap ( ) ) ;
6467
6473
6474
+ let loc = self . peek_token ( ) . location ;
6468
6475
let cascade = self . parse_keyword ( Keyword :: CASCADE ) ;
6469
6476
let restrict = self . parse_keyword ( Keyword :: RESTRICT ) ;
6470
6477
if cascade && restrict {
6471
- return parser_err ! ( "Cannot specify both CASCADE and RESTRICT in REVOKE" ) ;
6478
+ return parser_err ! ( "Cannot specify both CASCADE and RESTRICT in REVOKE" , loc ) ;
6472
6479
}
6473
6480
6474
6481
Ok ( Statement :: Revoke {
0 commit comments