Skip to content

Commit cf057ad

Browse files
committed
Repeat does not get to participate in inline
1 parent c0202a9 commit cf057ad

File tree

1 file changed

+61
-19
lines changed

1 file changed

+61
-19
lines changed

Sources/_StringProcessing/PrintAsPattern.swift

+61-19
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ extension PrettyPrinter {
121121
print("/* TODO: conditional */")
122122

123123
case let .quantification(amount, kind, child):
124-
let amount = amount.ast._patternBase
124+
let amountStr = amount.ast._patternBase
125125
var kind = kind.ast?._patternBase ?? ""
126126

127127
// If we've updated our quantification behavior, then use that. This
@@ -131,10 +131,10 @@ extension PrettyPrinter {
131131
kind = quantificationBehavior._patternBase
132132
}
133133

134-
var blockName = "\(amount)(\(kind))"
134+
var blockName = "\(amountStr)(\(kind))"
135135

136136
if kind == ".eager" {
137-
blockName = "\(amount)"
137+
blockName = "\(amountStr)"
138138
}
139139

140140
// Special case single child character classes for repetition nodes.
@@ -146,6 +146,20 @@ extension PrettyPrinter {
146146
// One(.digit)
147147
// }
148148
//
149+
func printAtom(_ pattern: String) {
150+
indent()
151+
152+
if kind != ".eager" {
153+
blockName.removeLast()
154+
output("\(blockName), ")
155+
} else {
156+
output("\(blockName)(")
157+
}
158+
159+
output("\(pattern))")
160+
terminateLine()
161+
}
162+
149163
func printSimpleCCC(
150164
_ ccc: DSLTree.CustomCharacterClass
151165
) {
@@ -163,23 +177,42 @@ extension PrettyPrinter {
163177
terminateLine()
164178
}
165179

166-
switch child {
167-
case let .customCharacterClass(ccc):
168-
if ccc.isSimplePrint {
169-
printSimpleCCC(ccc)
170-
return
171-
}
172-
173-
break
174-
case let .convertedRegexLiteral(.customCharacterClass(ccc), _):
175-
if ccc.isSimplePrint {
176-
printSimpleCCC(ccc)
177-
return
180+
// We can only do this for Optionally, ZeroOrMore, and OneOrMore. Cannot
181+
// do it right now for Repeat.
182+
if amount.ast.supportsInlineComponent {
183+
switch child {
184+
case let .atom(a):
185+
if let pattern = a._patternBase(&self), pattern.canBeWrapped {
186+
printAtom(pattern.0)
187+
return
188+
}
189+
190+
break
191+
case let .customCharacterClass(ccc):
192+
if ccc.isSimplePrint {
193+
printSimpleCCC(ccc)
194+
return
195+
}
196+
197+
break
198+
199+
case let .convertedRegexLiteral(.atom(a), _):
200+
if let pattern = a._patternBase(&self), pattern.canBeWrapped {
201+
printAtom(pattern.0)
202+
return
203+
}
204+
205+
break
206+
case let .convertedRegexLiteral(.customCharacterClass(ccc), _):
207+
if ccc.isSimplePrint {
208+
printSimpleCCC(ccc)
209+
return
210+
}
211+
212+
break
213+
default:
214+
break
178215
}
179-
180-
break
181-
default:
182-
break
183216
}
184217

185218
printBlock(blockName) { printer in
@@ -939,6 +972,15 @@ extension AST.Quantification.Amount {
939972
case let .range(n, m): return "Repeat(\(n.value)...\(m.value))"
940973
}
941974
}
975+
976+
var supportsInlineComponent: Bool {
977+
switch self {
978+
case .zeroOrMore: return true
979+
case .oneOrMore: return true
980+
case .zeroOrOne: return true
981+
default: return false
982+
}
983+
}
942984
}
943985

944986
extension AST.Quantification.Kind {

0 commit comments

Comments
 (0)