@@ -1113,7 +1113,11 @@ impl<'s, P: Borrow<Parser>> ParserI<'s, P> {
1113
1113
ast:: ErrorKind :: RepetitionCountUnclosed ,
1114
1114
) ) ;
1115
1115
}
1116
- let count_start = self . parse_decimal ( ) ?;
1116
+ let count_start = specialize_err (
1117
+ self . parse_decimal ( ) ,
1118
+ ast:: ErrorKind :: DecimalEmpty ,
1119
+ ast:: ErrorKind :: RepetitionCountDecimalEmpty ,
1120
+ ) ?;
1117
1121
let mut range = ast:: RepetitionRange :: Exactly ( count_start) ;
1118
1122
if self . is_eof ( ) {
1119
1123
return Err ( self . error (
@@ -1129,7 +1133,11 @@ impl<'s, P: Borrow<Parser>> ParserI<'s, P> {
1129
1133
) ) ;
1130
1134
}
1131
1135
if self . char ( ) != '}' {
1132
- let count_end = self . parse_decimal ( ) ?;
1136
+ let count_end = specialize_err (
1137
+ self . parse_decimal ( ) ,
1138
+ ast:: ErrorKind :: DecimalEmpty ,
1139
+ ast:: ErrorKind :: RepetitionCountDecimalEmpty ,
1140
+ ) ?;
1133
1141
range = ast:: RepetitionRange :: Bounded ( count_start, count_end) ;
1134
1142
} else {
1135
1143
range = ast:: RepetitionRange :: AtLeast ( count_start) ;
@@ -2260,6 +2268,29 @@ impl<'p, 's, P: Borrow<Parser>> ast::Visitor for NestLimiter<'p, 's, P> {
2260
2268
}
2261
2269
}
2262
2270
2271
+ /// When the result is an error, transforms the ast::ErrorKind from the source
2272
+ /// Result into another one. This function is used to return clearer error
2273
+ /// messages when possible.
2274
+ fn specialize_err < T > (
2275
+ result : Result < T > ,
2276
+ from : ast:: ErrorKind ,
2277
+ to : ast:: ErrorKind ,
2278
+ ) -> Result < T > {
2279
+ if let Err ( e) = result {
2280
+ if e. kind == from {
2281
+ Err ( ast:: Error {
2282
+ kind : to,
2283
+ pattern : e. pattern ,
2284
+ span : e. span ,
2285
+ } )
2286
+ } else {
2287
+ Err ( e)
2288
+ }
2289
+ } else {
2290
+ result
2291
+ }
2292
+ }
2293
+
2263
2294
#[ cfg( test) ]
2264
2295
mod tests {
2265
2296
use std:: ops:: Range ;
@@ -3143,6 +3174,18 @@ bar
3143
3174
span: span( 4 ..4 ) ,
3144
3175
kind: ast:: ErrorKind :: RepetitionMissing ,
3145
3176
} ) ;
3177
+ assert_eq ! (
3178
+ parser( r"a{]}" ) . parse( ) . unwrap_err( ) ,
3179
+ TestError {
3180
+ span: span( 2 ..2 ) ,
3181
+ kind: ast:: ErrorKind :: RepetitionCountDecimalEmpty ,
3182
+ } ) ;
3183
+ assert_eq ! (
3184
+ parser( r"a{1,]}" ) . parse( ) . unwrap_err( ) ,
3185
+ TestError {
3186
+ span: span( 4 ..4 ) ,
3187
+ kind: ast:: ErrorKind :: RepetitionCountDecimalEmpty ,
3188
+ } ) ;
3146
3189
assert_eq ! (
3147
3190
parser( r"a{" ) . parse( ) . unwrap_err( ) ,
3148
3191
TestError {
@@ -3153,13 +3196,13 @@ bar
3153
3196
parser( r"a{}" ) . parse( ) . unwrap_err( ) ,
3154
3197
TestError {
3155
3198
span: span( 2 ..2 ) ,
3156
- kind: ast:: ErrorKind :: DecimalEmpty ,
3199
+ kind: ast:: ErrorKind :: RepetitionCountDecimalEmpty ,
3157
3200
} ) ;
3158
3201
assert_eq ! (
3159
3202
parser( r"a{a" ) . parse( ) . unwrap_err( ) ,
3160
3203
TestError {
3161
3204
span: span( 2 ..2 ) ,
3162
- kind: ast:: ErrorKind :: DecimalEmpty ,
3205
+ kind: ast:: ErrorKind :: RepetitionCountDecimalEmpty ,
3163
3206
} ) ;
3164
3207
assert_eq ! (
3165
3208
parser( r"a{9999999999}" ) . parse( ) . unwrap_err( ) ,
@@ -3177,7 +3220,7 @@ bar
3177
3220
parser( r"a{9,a" ) . parse( ) . unwrap_err( ) ,
3178
3221
TestError {
3179
3222
span: span( 4 ..4 ) ,
3180
- kind: ast:: ErrorKind :: DecimalEmpty ,
3223
+ kind: ast:: ErrorKind :: RepetitionCountDecimalEmpty ,
3181
3224
} ) ;
3182
3225
assert_eq ! (
3183
3226
parser( r"a{9,9999999999}" ) . parse( ) . unwrap_err( ) ,
0 commit comments