Skip to content

Commit 289013f

Browse files
committed
Handle refactoring changeMatchingOptions
1 parent 5461119 commit 289013f

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

Sources/_RegexParser/Regex/Printing/PrettyPrinter.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public struct PrettyPrinter {
4343

4444
// The current default quantification behavior
4545
public var quantificationBehavior: AST.Quantification.Kind = .eager
46+
47+
// A stack of the current added inline matching options, e.g. (?s)
48+
public var inlineMatchingOptions: [[AST.MatchingOption]] = []
4649
}
4750

4851
// MARK: - Raw interface
@@ -142,4 +145,18 @@ extension PrettyPrinter {
142145
printIndented(f)
143146
print(endDelimiter)
144147
}
148+
149+
/// Pushes the list of matching options to the current stack of other matching
150+
/// options and increases the indentation level by 1.
151+
public mutating func pushMatchingOptions(_ options: [AST.MatchingOption]) {
152+
indentLevel += 1
153+
inlineMatchingOptions.append(options)
154+
}
155+
156+
/// Pops the most recent list of matching options from the printer and
157+
/// decreases the indentation level by 1.
158+
public mutating func popMatchingOptions() -> [AST.MatchingOption] {
159+
indentLevel -= 1
160+
return inlineMatchingOptions.removeLast()
161+
}
145162
}

Sources/_StringProcessing/PrintAsPattern.swift

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,51 @@ extension PrettyPrinter {
7474
printBlock("Regex") { printer in
7575
printer.printAsPattern(convertedFromAST: node, isTopLevel: true)
7676
}
77+
78+
printInlineMatchingOptions()
79+
}
80+
81+
mutating func printInlineMatchingOptions() {
82+
for matchingOptions in inlineMatchingOptions {
83+
let options = popMatchingOptions()
84+
85+
printIndented { printer in
86+
for option in options {
87+
switch option.kind {
88+
case .asciiOnlyDigit:
89+
printer.print(".asciiOnlyDigits()")
90+
91+
case .asciiOnlyPOSIXProps:
92+
printer.print(".asciiOnlyCharacterClasses()")
93+
94+
case .asciiOnlySpace:
95+
printer.print(".asciiOnlyWhitespace()")
96+
97+
case .asciiOnlyWord:
98+
printer.print(".asciiOnlyWordCharacters()")
99+
100+
case .caseInsensitive:
101+
printer.print(".ignoresCase()")
102+
103+
case .multiline:
104+
printer.print(".anchorsMatchLineEndings()")
105+
106+
case .reluctantByDefault:
107+
// This is handled by altering every OneOrMore, etc by changing each
108+
// individual repetition behavior instead of creating a nested regex.
109+
continue
110+
111+
case .singleLine:
112+
printer.print(".dotMatchesNewlines()")
113+
114+
default:
115+
break
116+
}
117+
}
118+
}
119+
120+
print("}")
121+
}
77122
}
78123

79124
// FIXME: Use of back-offs like height and depth
@@ -424,7 +469,7 @@ extension PrettyPrinter {
424469
// Also in the same vein, if we have a few atom members but no
425470
// nonAtomMembers, then we can emit a single .anyOf(...) for them.
426471
if !charMembers.isEmpty, nonCharMembers.isEmpty {
427-
let anyOf = ".anyOf(\(charMembers))"
472+
let anyOf = "CharacterClass.anyOf(\(charMembers))"
428473

429474
indent()
430475

@@ -502,15 +547,15 @@ extension PrettyPrinter {
502547
if wrap {
503548
output("One(.anyOf(\(String(c)._quoted)))")
504549
} else {
505-
output(".anyOf(\(String(c)._quoted))")
550+
output("CharacterClass.anyOf(\(String(c)._quoted))")
506551
}
507552

508553
case let .scalar(s):
509554

510555
if wrap {
511556
output("One(.anyOf(\(s._dslBase._bareQuoted)))")
512557
} else {
513-
output(".anyOf(\(s._dslBase._bareQuoted))")
558+
output("CharacterClass.anyOf(\(s._dslBase._bareQuoted))")
514559
}
515560

516561
case let .unconverted(a):
@@ -538,7 +583,7 @@ extension PrettyPrinter {
538583
if wrap {
539584
output("One(.anyOf(\(s._quoted)))")
540585
} else {
541-
output(".anyOf(\(s._quoted))")
586+
output("CharacterClass.anyOf(\(s._quoted))")
542587
}
543588

544589
case .trivia(_):
@@ -1285,10 +1330,20 @@ extension DSLTree.Atom {
12851330
switch add.kind {
12861331
case .reluctantByDefault:
12871332
printer.quantificationBehavior = .reluctant
1333+
1334+
// Don't create a nested Regex for (?U), we handle this by altering
1335+
// every individual repetitionBehavior for things like OneOrMore.
1336+
if matchingOptions.ast.adding.count == 1 {
1337+
return nil
1338+
}
1339+
12881340
default:
12891341
break
12901342
}
12911343
}
1344+
1345+
printer.print("Regex {")
1346+
printer.pushMatchingOptions(matchingOptions.ast.adding)
12921347
}
12931348

12941349
return nil

0 commit comments

Comments
 (0)