@@ -1170,7 +1170,7 @@ extension Parser {
11701170 )
11711171 case ( . rawStringDelimiter, _) ? , ( . stringQuote, _) ? , ( . multilineStringQuote, _) ? , ( . singleQuote, _) ? :
11721172 return RawExprSyntax ( self . parseStringLiteral ( ) )
1173- case ( . regexLiteral , _) ? :
1173+ case ( . extendedRegexDelimiter , _ ) ? , ( . regexSlash , _) ? :
11741174 return RawExprSyntax ( self . parseRegexLiteral ( ) )
11751175 case ( . nilKeyword, let handle) ? :
11761176 let nilKeyword = self . eat ( handle)
@@ -1433,13 +1433,37 @@ extension Parser {
14331433 /// Grammar
14341434 /// =======
14351435 ///
1436- /// regular-expression-literal → '\' `Any valid regular expression characters` '\'
1436+ /// regular-expression-literal → '#'* '/' `Any valid regular expression characters` '/' '#'*
14371437 @_spi ( RawSyntax)
14381438 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+
14401459 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,
14431467 arena: self . arena
14441468 )
14451469 }
0 commit comments