@@ -332,7 +332,7 @@ extension Source {
332
332
/// Quantifier -> ('*' | '+' | '?' | '{' Range '}') QuantKind?
333
333
/// QuantKind -> '?' | '+'
334
334
///
335
- mutating func lexQuantifier( ) throws -> (
335
+ mutating func lexQuantifier( context : ParsingContext ) throws -> (
336
336
Located < Quant . Amount > , Located < Quant . Kind >
337
337
) ? {
338
338
let amt : Located < Quant . Amount > ? = try recordLoc { src in
@@ -341,7 +341,9 @@ extension Source {
341
341
if src. tryEat ( " ? " ) { return . zeroOrOne }
342
342
343
343
return try src. tryEating { src in
344
- guard src. tryEat ( " { " ) , let range = try src. lexRange ( ) , src. tryEat ( " } " )
344
+ guard src. tryEat ( " { " ) ,
345
+ let range = try src. lexRange ( context: context) ,
346
+ src. tryEat ( " } " )
345
347
else { return nil }
346
348
return range. value
347
349
}
@@ -363,7 +365,7 @@ extension Source {
363
365
/// | ExpRange
364
366
/// ExpRange -> '..<' <Int> | '...' <Int>
365
367
/// | <Int> '..<' <Int> | <Int> '...' <Int>?
366
- mutating func lexRange( ) throws -> Located < Quant . Amount > ? {
368
+ mutating func lexRange( context : ParsingContext ) throws -> Located < Quant . Amount > ? {
367
369
try recordLoc { src in
368
370
try src. tryEating { src in
369
371
let lowerOpt = try src. lexNumber ( )
@@ -375,7 +377,7 @@ extension Source {
375
377
let closedRange : Bool ?
376
378
if src. tryEat ( " , " ) {
377
379
closedRange = true
378
- } else if src . experimentalRanges && src. tryEat ( " . " ) {
380
+ } else if context . experimentalRanges && src. tryEat ( " . " ) {
379
381
try src. expect ( " . " )
380
382
if src. tryEat ( " . " ) {
381
383
closedRange = true
@@ -477,12 +479,12 @@ extension Source {
477
479
///
478
480
/// TODO: Need to support some escapes
479
481
///
480
- mutating func lexQuote( ) throws -> AST . Quote ? {
482
+ mutating func lexQuote( context : ParsingContext ) throws -> AST . Quote ? {
481
483
let str = try recordLoc { src -> String ? in
482
484
if src. tryEat ( sequence: #"\Q"# ) {
483
485
return try src. expectQuoted ( endingWith: #"\E"# ) . value
484
486
}
485
- if src . experimentalQuotes, src. tryEat ( " \" " ) {
487
+ if context . experimentalQuotes, src. tryEat ( " \" " ) {
486
488
return try src. expectQuoted ( endingWith: " \" " , ignoreEscaped: true ) . value
487
489
}
488
490
return nil
@@ -501,12 +503,12 @@ extension Source {
501
503
///
502
504
/// TODO: Swift-style nested comments, line-ending comments, etc
503
505
///
504
- mutating func lexComment( ) throws -> AST . Trivia ? {
506
+ mutating func lexComment( context : ParsingContext ) throws -> AST . Trivia ? {
505
507
let trivia : Located < String > ? = try recordLoc { src in
506
508
if src. tryEat ( sequence: " (?# " ) {
507
509
return try src. expectQuoted ( endingWith: " ) " ) . value
508
510
}
509
- if src . experimentalComments, src. tryEat ( sequence: " /*") {
511
+ if context . experimentalComments, src. tryEat ( sequence: " /*") {
510
512
return try src.expectQuoted(endingWith: "*/" ) . value
511
513
}
512
514
return nil
@@ -517,16 +519,34 @@ extension Source {
517
519
518
520
/// Try to consume non-semantic whitespace as trivia
519
521
///
522
+ /// Whitespace -> ' '+
523
+ ///
520
524
/// Does nothing unless `SyntaxOptions.nonSemanticWhitespace` is set
521
- mutating func lexNonSemanticWhitespace( ) throws -> AST . Trivia ? {
522
- guard syntax. ignoreWhitespace else { return nil }
525
+ mutating func lexNonSemanticWhitespace(
526
+ context: ParsingContext
527
+ ) throws -> AST . Trivia ? {
528
+ guard context. ignoreWhitespace else { return nil }
523
529
let trivia : Located < String > ? = recordLoc { src in
524
530
src. tryEatPrefix { $0 == " " } ? . string
525
531
}
526
532
guard let trivia = trivia else { return nil }
527
533
return AST . Trivia ( trivia)
528
534
}
529
535
536
+ /// Try to consume trivia.
537
+ ///
538
+ /// Trivia -> Comment | Whitespace
539
+ ///
540
+ mutating func lexTrivia( context: ParsingContext ) throws -> AST . Trivia ? {
541
+ if let comment = try lexComment ( context: context) {
542
+ return comment
543
+ }
544
+ if let whitespace = try lexNonSemanticWhitespace ( context: context) {
545
+ return whitespace
546
+ }
547
+ return nil
548
+ }
549
+
530
550
/// Try to lex a matching option.
531
551
///
532
552
/// MatchingOption -> 'i' | 'J' | 'm' | 'n' | 's' | 'U' | 'x' | 'xx' | 'w'
@@ -761,6 +781,7 @@ extension Source {
761
781
/// comments, like quotes, cannot be quantified.
762
782
///
763
783
mutating func lexGroupStart(
784
+ context: ParsingContext
764
785
) throws -> Located < AST . Group . Kind > ? {
765
786
try recordLoc { src in
766
787
try src. tryEating { src in
@@ -825,7 +846,7 @@ extension Source {
825
846
}
826
847
827
848
// (_:)
828
- if src . experimentalCaptures && src. tryEat ( sequence: " _: " ) {
849
+ if context . experimentalCaptures && src. tryEat ( sequence: " _: " ) {
829
850
return . nonCapture
830
851
}
831
852
// TODO: (name:)
@@ -960,9 +981,12 @@ extension Source {
960
981
///
961
982
/// GroupConditionalStart -> '(?' GroupStart
962
983
///
963
- mutating func lexGroupConditionalStart( ) throws -> Located < AST . Group . Kind > ? {
984
+ mutating func lexGroupConditionalStart(
985
+ context: ParsingContext
986
+ ) throws -> Located < AST . Group . Kind > ? {
964
987
try tryEating { src in
965
- guard src. tryEat ( sequence: " (? " ) , let group = try src. lexGroupStart ( )
988
+ guard src. tryEat ( sequence: " (? " ) ,
989
+ let group = try src. lexGroupStart ( context: context)
966
990
else { return nil }
967
991
968
992
// Implicitly scoped groups are not supported here.
0 commit comments