@@ -331,7 +331,9 @@ extension Parser {
331
331
///
332
332
/// Diagnoses on overflow
333
333
///
334
- mutating func lexNumber( _ kind: RadixKind = . decimal) -> AST . Atom . Number ? {
334
+ mutating func lexNumber(
335
+ _ kind: RadixKind = . decimal
336
+ ) -> AST . Atom . Number ? {
335
337
guard let str = tryEatPrefix ( kind. characterFilter) else {
336
338
return nil
337
339
}
@@ -342,6 +344,26 @@ extension Parser {
342
344
return . init( i, at: str. location)
343
345
}
344
346
347
+ /// Try to eat a quantification bound, such as appears in `/x{3,12}`
348
+ ///
349
+ /// Returns: `nil` if there's no number, otherwise the number
350
+ ///
351
+ /// Diagnoses on overflow. Currently, we will diagnose for any values over `UInt16.max`
352
+ ///
353
+ mutating func lexQuantBound( ) -> AST . Atom . Number ? {
354
+ let kind = RadixKind . decimal
355
+ guard let str = tryEatPrefix ( kind. characterFilter) else {
356
+ return nil
357
+ }
358
+ guard let i = UInt16 ( str. value, radix: kind. radix) else {
359
+ error ( . numberOverflow( str. value) , at: str. location)
360
+ return . init( nil , at: str. location)
361
+ }
362
+
363
+ return . init( Int ( i) , at: str. location)
364
+ }
365
+
366
+
345
367
/// Expect a number of a given `kind`, diagnosing if a number cannot be
346
368
/// parsed.
347
369
mutating func expectNumber( _ kind: RadixKind = . decimal) -> AST . Atom . Number {
@@ -492,7 +514,7 @@ extension Parser {
492
514
493
515
return p. tryEating { p in
494
516
guard p. tryEat ( " { " ) ,
495
- let range = p. lexRange ( trivia: & trivia) ,
517
+ let range = p. lexQuantRange ( trivia: & trivia) ,
496
518
p. tryEat ( " } " )
497
519
else { return nil }
498
520
return range. value
@@ -519,12 +541,14 @@ extension Parser {
519
541
/// | ExpRange
520
542
/// ExpRange -> '..<' <Int> | '...' <Int>
521
543
/// | <Int> '..<' <Int> | <Int> '...' <Int>?
522
- mutating func lexRange( trivia: inout [ AST . Trivia ] ) -> Located < Quant . Amount > ? {
544
+ mutating func lexQuantRange(
545
+ trivia: inout [ AST . Trivia ]
546
+ ) -> Located < Quant . Amount > ? {
523
547
recordLoc { p in
524
548
p. tryEating { p in
525
549
if let t = p. lexWhitespace ( ) { trivia. append ( t) }
526
550
527
- let lowerOpt = p. lexNumber ( )
551
+ let lowerOpt = p. lexQuantBound ( )
528
552
529
553
if let t = p. lexWhitespace ( ) { trivia. append ( t) }
530
554
@@ -546,7 +570,7 @@ extension Parser {
546
570
547
571
if let t = p. lexWhitespace ( ) { trivia. append ( t) }
548
572
549
- var upperOpt = p. lexNumber ( )
573
+ var upperOpt = p. lexQuantBound ( )
550
574
if closedRange == false {
551
575
// If we have an open range, the upper bound should be adjusted down.
552
576
upperOpt? . value? -= 1
0 commit comments