@@ -20,7 +20,10 @@ use alloc::{
20
20
vec,
21
21
vec:: Vec ,
22
22
} ;
23
- use core:: fmt;
23
+ use core:: {
24
+ fmt:: { self , Display } ,
25
+ str:: FromStr ,
26
+ } ;
24
27
25
28
use log:: debug;
26
29
@@ -3260,6 +3263,18 @@ impl<'a> Parser<'a> {
3260
3263
}
3261
3264
}
3262
3265
3266
+ fn parse < T : FromStr > ( s : String , loc : Location ) -> Result < T , ParserError >
3267
+ where
3268
+ <T as FromStr >:: Err : Display ,
3269
+ {
3270
+ s. parse :: < T > ( ) . map_err ( |e| {
3271
+ ParserError :: ParserError ( format ! (
3272
+ "Could not parse '{s}' as {}: {e}{loc}" ,
3273
+ core:: any:: type_name:: <T >( )
3274
+ ) )
3275
+ } )
3276
+ }
3277
+
3263
3278
/// Parse a comma-separated list of 1+ SelectItem
3264
3279
pub fn parse_projection ( & mut self ) -> Result < Vec < SelectItem > , ParserError > {
3265
3280
// BigQuery and Snowflake allow trailing commas, but only in project lists
@@ -5281,7 +5296,7 @@ impl<'a> Parser<'a> {
5281
5296
let _ = self . consume_token ( & Token :: Eq ) ;
5282
5297
let next_token = self . next_token ( ) ;
5283
5298
match next_token. token {
5284
- Token :: Number ( s, _) => Some ( s . parse :: < u32 > ( ) . expect ( "literal int" ) ) ,
5299
+ Token :: Number ( s, _) => Some ( Self :: parse :: < u32 > ( s , next_token . location ) ? ) ,
5285
5300
_ => self . expected ( "literal int" , next_token) ?,
5286
5301
}
5287
5302
} else {
@@ -6725,10 +6740,7 @@ impl<'a> Parser<'a> {
6725
6740
// The call to n.parse() returns a bigdecimal when the
6726
6741
// bigdecimal feature is enabled, and is otherwise a no-op
6727
6742
// (i.e., it returns the input string).
6728
- Token :: Number ( ref n, l) => match n. parse ( ) {
6729
- Ok ( n) => Ok ( Value :: Number ( n, l) ) ,
6730
- Err ( e) => parser_err ! ( format!( "Could not parse '{n}' as number: {e}" ) , location) ,
6731
- } ,
6743
+ Token :: Number ( n, l) => Ok ( Value :: Number ( Self :: parse ( n, location) ?, l) ) ,
6732
6744
Token :: SingleQuotedString ( ref s) => Ok ( Value :: SingleQuotedString ( s. to_string ( ) ) ) ,
6733
6745
Token :: DoubleQuotedString ( ref s) => Ok ( Value :: DoubleQuotedString ( s. to_string ( ) ) ) ,
6734
6746
Token :: TripleSingleQuotedString ( ref s) => {
@@ -6820,9 +6832,7 @@ impl<'a> Parser<'a> {
6820
6832
pub fn parse_literal_uint ( & mut self ) -> Result < u64 , ParserError > {
6821
6833
let next_token = self . next_token ( ) ;
6822
6834
match next_token. token {
6823
- Token :: Number ( s, _) => s. parse :: < u64 > ( ) . map_err ( |e| {
6824
- ParserError :: ParserError ( format ! ( "Could not parse '{s}' as u64: {e}" ) )
6825
- } ) ,
6835
+ Token :: Number ( s, _) => Self :: parse :: < u64 > ( s, next_token. location ) ,
6826
6836
_ => self . expected ( "literal int" , next_token) ,
6827
6837
}
6828
6838
}
@@ -9273,20 +9283,20 @@ impl<'a> Parser<'a> {
9273
9283
return self . expected ( "literal number" , next_token) ;
9274
9284
} ;
9275
9285
self . expect_token ( & Token :: RBrace ) ?;
9276
- RepetitionQuantifier :: AtMost ( n . parse ( ) . expect ( "literal int" ) )
9286
+ RepetitionQuantifier :: AtMost ( Self :: parse ( n , token . location ) ? )
9277
9287
}
9278
9288
Token :: Number ( n, _) if self . consume_token ( & Token :: Comma ) => {
9279
9289
let next_token = self . next_token ( ) ;
9280
9290
match next_token. token {
9281
9291
Token :: Number ( m, _) => {
9282
9292
self . expect_token ( & Token :: RBrace ) ?;
9283
9293
RepetitionQuantifier :: Range (
9284
- n . parse ( ) . expect ( "literal int" ) ,
9285
- m . parse ( ) . expect ( "literal int" ) ,
9294
+ Self :: parse ( n , token . location ) ? ,
9295
+ Self :: parse ( m , token . location ) ? ,
9286
9296
)
9287
9297
}
9288
9298
Token :: RBrace => {
9289
- RepetitionQuantifier :: AtLeast ( n . parse ( ) . expect ( "literal int" ) )
9299
+ RepetitionQuantifier :: AtLeast ( Self :: parse ( n , token . location ) ? )
9290
9300
}
9291
9301
_ => {
9292
9302
return self . expected ( "} or upper bound" , next_token) ;
@@ -9295,7 +9305,7 @@ impl<'a> Parser<'a> {
9295
9305
}
9296
9306
Token :: Number ( n, _) => {
9297
9307
self . expect_token ( & Token :: RBrace ) ?;
9298
- RepetitionQuantifier :: Exactly ( n . parse ( ) . expect ( "literal int" ) )
9308
+ RepetitionQuantifier :: Exactly ( Self :: parse ( n , token . location ) ? )
9299
9309
}
9300
9310
_ => return self . expected ( "quantifier range" , token) ,
9301
9311
}
@@ -10329,7 +10339,7 @@ impl<'a> Parser<'a> {
10329
10339
} else {
10330
10340
let next_token = self . next_token ( ) ;
10331
10341
let quantity = match next_token. token {
10332
- Token :: Number ( s, _) => s . parse :: < u64 > ( ) . expect ( "literal int" ) ,
10342
+ Token :: Number ( s, _) => Self :: parse :: < u64 > ( s , next_token . location ) ? ,
10333
10343
_ => self . expected ( "literal int" , next_token) ?,
10334
10344
} ;
10335
10345
Some ( TopQuantity :: Constant ( quantity) )
0 commit comments