@@ -287,23 +287,34 @@ extension Parser {
287287 private mutating func applySyntaxOptions(
288288 of opts: AST . MatchingOptionSequence
289289 ) {
290- // We skip this for multi-line, as extended syntax is always enabled there.
291- if context. syntax. contains ( . multilineExtendedSyntax) { return }
290+ func mapOption( _ option: SyntaxOptions ,
291+ _ pred: ( AST . MatchingOption ) -> Bool ) {
292+ if opts. resetsCurrentOptions {
293+ context. syntax. remove ( option)
294+ }
295+ if opts. adding. contains ( where: pred) {
296+ context. syntax. insert ( option)
297+ }
298+ if opts. removing. contains ( where: pred) {
299+ context. syntax. remove ( option)
300+ }
301+ }
302+ func mapOption( _ option: SyntaxOptions , _ kind: AST . MatchingOption . Kind ) {
303+ mapOption ( option, { $0. kind == kind } )
304+ }
305+
306+ // (?n)
307+ mapOption ( . namedCapturesOnly, . namedCapturesOnly)
292308
293- // Check if we're introducing or removing extended syntax.
309+ // (?x), (?xx)
310+ // We skip this for multi-line, as extended syntax is always enabled there.
294311 // TODO: PCRE differentiates between (?x) and (?xx) where only the latter
295312 // handles non-semantic whitespace in a custom character class. Other
296313 // engines such as Oniguruma, Java, and ICU do this under (?x). Therefore,
297314 // treat (?x) and (?xx) as the same option here. If we ever get a strict
298315 // PCRE mode, we will need to change this to handle that.
299- if opts. resetsCurrentOptions {
300- context. syntax. remove ( . extendedSyntax)
301- }
302- if opts. adding. contains ( where: \. isAnyExtended) {
303- context. syntax. insert ( . extendedSyntax)
304- }
305- if opts. removing. contains ( where: \. isAnyExtended) {
306- context. syntax. remove ( . extendedSyntax)
316+ if !context. syntax. contains ( . multilineExtendedSyntax) {
317+ mapOption ( . extendedSyntax, \. isAnyExtended)
307318 }
308319 }
309320
0 commit comments