@@ -1170,7 +1170,7 @@ extension Parser {
1170
1170
)
1171
1171
case ( . rawStringDelimiter, _) ? , ( . stringQuote, _) ? , ( . multilineStringQuote, _) ? , ( . singleQuote, _) ? :
1172
1172
return RawExprSyntax ( self . parseStringLiteral ( ) )
1173
- case ( . regexLiteral , _) ? :
1173
+ case ( . extendedRegexDelimiter , _ ) ? , ( . regexSlash , _) ? :
1174
1174
return RawExprSyntax ( self . parseRegexLiteral ( ) )
1175
1175
case ( . nilKeyword, let handle) ? :
1176
1176
let nilKeyword = self . eat ( handle)
@@ -1433,13 +1433,37 @@ extension Parser {
1433
1433
/// Grammar
1434
1434
/// =======
1435
1435
///
1436
- /// regular-expression-literal → '\' `Any valid regular expression characters` '\'
1436
+ /// regular-expression-literal → '#'* '/' `Any valid regular expression characters` '/' '#'*
1437
1437
@_spi ( RawSyntax)
1438
1438
public mutating func parseRegexLiteral( ) -> RawRegexLiteralExprSyntax {
1439
- let ( unexpectedBeforeLiteral, literal) = self . expect ( . regexLiteral)
1439
+ // See if we have an opening set of pounds.
1440
+ let openPounds = self . consume ( if: . extendedRegexDelimiter)
1441
+
1442
+ // Parse the opening slash.
1443
+ let ( unexpectedBeforeSlash, openSlash) = self . expect ( . regexSlash)
1444
+
1445
+ // If we had opening pounds, there should be no trivia for the slash.
1446
+ if let openPounds = openPounds {
1447
+ precondition ( openPounds. trailingTriviaByteLength == 0 && openSlash. leadingTriviaByteLength == 0 )
1448
+ }
1449
+
1450
+ // Parse the pattern and closing slash, avoiding recovery or leading trivia
1451
+ // as the lexer should provide the tokens exactly in order without trivia,
1452
+ // otherwise they should be treated as missing.
1453
+ let pattern = self . expectWithoutRecoveryOrLeadingTrivia ( . regexLiteralPattern)
1454
+ let closeSlash = self . expectWithoutRecoveryOrLeadingTrivia ( . regexSlash)
1455
+
1456
+ // Finally, parse a closing set of pounds.
1457
+ let ( unexpectedBeforeClosePounds, closePounds) = parsePoundDelimiter ( . extendedRegexDelimiter, matching: openPounds)
1458
+
1440
1459
return RawRegexLiteralExprSyntax (
1441
- unexpectedBeforeLiteral,
1442
- regex: literal,
1460
+ openingPounds: openPounds,
1461
+ unexpectedBeforeSlash,
1462
+ openSlash: openSlash,
1463
+ regexPattern: pattern,
1464
+ closeSlash: closeSlash,
1465
+ unexpectedBeforeClosePounds,
1466
+ closingPounds: closePounds,
1443
1467
arena: self . arena
1444
1468
)
1445
1469
}
0 commit comments