@@ -331,7 +331,9 @@ extension Parser {
331331 ///
332332 /// Diagnoses on overflow
333333 ///
334- mutating func lexNumber( _ kind: RadixKind = . decimal) -> AST . Atom . Number ? {
334+ mutating func lexNumber(
335+ _ kind: RadixKind = . decimal
336+ ) -> AST . Atom . Number ? {
335337 guard let str = tryEatPrefix ( kind. characterFilter) else {
336338 return nil
337339 }
@@ -342,6 +344,26 @@ extension Parser {
342344 return . init( i, at: str. location)
343345 }
344346
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+
345367 /// Expect a number of a given `kind`, diagnosing if a number cannot be
346368 /// parsed.
347369 mutating func expectNumber( _ kind: RadixKind = . decimal) -> AST . Atom . Number {
@@ -492,7 +514,7 @@ extension Parser {
492514
493515 return p. tryEating { p in
494516 guard p. tryEat ( " { " ) ,
495- let range = p. lexRange ( trivia: & trivia) ,
517+ let range = p. lexQuantRange ( trivia: & trivia) ,
496518 p. tryEat ( " } " )
497519 else { return nil }
498520 return range. value
@@ -519,12 +541,14 @@ extension Parser {
519541 /// | ExpRange
520542 /// ExpRange -> '..<' <Int> | '...' <Int>
521543 /// | <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 > ? {
523547 recordLoc { p in
524548 p. tryEating { p in
525549 if let t = p. lexWhitespace ( ) { trivia. append ( t) }
526550
527- let lowerOpt = p. lexNumber ( )
551+ let lowerOpt = p. lexQuantBound ( )
528552
529553 if let t = p. lexWhitespace ( ) { trivia. append ( t) }
530554
@@ -546,7 +570,7 @@ extension Parser {
546570
547571 if let t = p. lexWhitespace ( ) { trivia. append ( t) }
548572
549- var upperOpt = p. lexNumber ( )
573+ var upperOpt = p. lexQuantBound ( )
550574 if closedRange == false {
551575 // If we have an open range, the upper bound should be adjusted down.
552576 upperOpt? . value? -= 1
0 commit comments