Skip to content

Commit fa993aa

Browse files
committed
Eliminate convertedRegexLiteral wrapper node
1 parent 97e7b8a commit fa993aa

File tree

6 files changed

+59
-94
lines changed

6 files changed

+59
-94
lines changed

Sources/_StringProcessing/ByteCodeGen.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ fileprivate extension Compiler.ByteCodeGen {
809809
default:
810810
return false
811811
}
812-
case .convertedRegexLiteral(let node, _):
812+
case .limitCaptureNesting(let node):
813813
return tryEmitFastQuant(node, kind, minTrips, maxExtraTrips)
814814
case .nonCapturingGroup(let groupKind, let node):
815815
// .nonCapture nonCapturingGroups are ignored during compilation
@@ -1203,7 +1203,7 @@ fileprivate extension Compiler.ByteCodeGen {
12031203
switch node {
12041204
case .concatenation(let ch):
12051205
return ch.flatMap(flatten)
1206-
case .convertedRegexLiteral(let n, _), .ignoreCapturesInTypedOutput(let n):
1206+
case .ignoreCapturesInTypedOutput(let n), .limitCaptureNesting(let n):
12071207
return flatten(n)
12081208
default:
12091209
return [node]
@@ -1283,6 +1283,9 @@ fileprivate extension Compiler.ByteCodeGen {
12831283
case let .ignoreCapturesInTypedOutput(child):
12841284
try emitNode(child)
12851285

1286+
case let .limitCaptureNesting(child):
1287+
return try emitNode(child)
1288+
12861289
case .conditional:
12871290
throw Unsupported("Conditionals")
12881291

@@ -1306,9 +1309,6 @@ fileprivate extension Compiler.ByteCodeGen {
13061309
case let .quotedLiteral(s):
13071310
emitQuotedLiteral(s)
13081311

1309-
case let .convertedRegexLiteral(n, _):
1310-
return try emitNode(n)
1311-
13121312
case .absentFunction:
13131313
throw Unsupported("absent function")
13141314
case .consumer:
@@ -1359,8 +1359,6 @@ extension DSLTree.Node {
13591359
return false
13601360
case .quotedLiteral(let string):
13611361
return !string.isEmpty
1362-
case .convertedRegexLiteral(let node, _):
1363-
return node.guaranteesForwardProgress
13641362
case .consumer, .matcher:
13651363
// Allow zero width consumers and matchers
13661364
return false
@@ -1369,6 +1367,8 @@ extension DSLTree.Node {
13691367
case .quantification(let amount, _, let child):
13701368
let (atLeast, _) = amount.ast.bounds
13711369
return atLeast ?? 0 > 0 && child.guaranteesForwardProgress
1370+
case .limitCaptureNesting(let node), .ignoreCapturesInTypedOutput(let node):
1371+
return node.guaranteesForwardProgress
13721372
default: return false
13731373
}
13741374
}

Sources/_StringProcessing/LiteralPrinter.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,9 @@ extension LiteralPrinter {
116116
outputNode(child)
117117
output(")")
118118

119-
case let .ignoreCapturesInTypedOutput(child):
119+
case let .ignoreCapturesInTypedOutput(child),
120+
let .limitCaptureNesting(child):
120121
outputNode(child)
121-
case .convertedRegexLiteral(let node, _):
122-
outputNode(node)
123-
124122
case let .quantification(amount, kind, node):
125123
outputQuantification(amount, kind, node)
126124
case let .customCharacterClass(charClass):

Sources/_StringProcessing/PrintAsPattern.swift

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ extension PrettyPrinter {
179179
case let .ignoreCapturesInTypedOutput(child):
180180
printAsPattern(convertedFromAST: child, isTopLevel: isTopLevel)
181181

182+
case let .limitCaptureNesting(child):
183+
printAsPattern(convertedFromAST: child, isTopLevel: isTopLevel)
184+
182185
case .conditional:
183186
print("/* TODO: conditional */")
184187

@@ -258,20 +261,6 @@ extension PrettyPrinter {
258261

259262
break
260263

261-
case let .convertedRegexLiteral(.atom(a), _):
262-
if let pattern = a._patternBase(&self), pattern.canBeWrapped {
263-
printAtom(pattern.0)
264-
return
265-
}
266-
267-
break
268-
case let .convertedRegexLiteral(.customCharacterClass(ccc), _):
269-
if ccc.isSimplePrint {
270-
printSimpleCCC(ccc)
271-
return
272-
}
273-
274-
break
275264
default:
276265
break
277266
}
@@ -305,13 +294,6 @@ extension PrettyPrinter {
305294
case let .quotedLiteral(v):
306295
print(v._quoted)
307296

308-
case let .convertedRegexLiteral(n, _):
309-
// FIXME: This recursion coordinates with back-off
310-
// check above, so it should work out. Need a
311-
// cleaner way to do this. This means the argument
312-
// label is a lie.
313-
printAsPattern(convertedFromAST: n, isTopLevel: isTopLevel)
314-
315297
case let .customCharacterClass(ccc):
316298
printAsPattern(ccc)
317299

@@ -1431,9 +1413,6 @@ extension DSLTree.Node {
14311413
result += node.getNamedCaptures()
14321414
}
14331415

1434-
case .convertedRegexLiteral(let node, _):
1435-
result += node.getNamedCaptures()
1436-
14371416
case .quantification(_, _, let node):
14381417
result += node.getNamedCaptures()
14391418

Sources/_StringProcessing/Regex/ASTConversion.swift

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,27 @@ internal import _RegexParser
1313

1414
extension AST {
1515
var dslTree: DSLTree {
16-
return DSLTree(root.dslTreeNode)
16+
return DSLTree(.limitCaptureNesting(root.dslTreeNode))
1717
}
1818
}
1919

2020
extension AST.Node {
2121
/// Converts an AST node to a `convertedRegexLiteral` node.
2222
var dslTreeNode: DSLTree.Node {
23-
func wrap(_ node: DSLTree.Node) -> DSLTree.Node {
24-
switch node {
25-
case .convertedRegexLiteral:
26-
// FIXME: DSL can have one item concats
27-
// assertionFailure("Double wrapping?")
28-
return node
29-
default:
30-
break
31-
}
32-
// TODO: Should we do this for the
33-
// single-concatenation child too, or should?
34-
// we wrap _that_?
35-
return .convertedRegexLiteral(node, .init(ast: self))
36-
}
23+
// func wrap(_ node: DSLTree.Node) -> DSLTree.Node {
24+
// switch node {
25+
// case .convertedRegexLiteral(let child, _):
26+
// // FIXME: DSL can have one item concats
27+
//// assertionFailure("Double wrapping?")
28+
// return child
29+
// default:
30+
// break
31+
// }
32+
// // TODO: Should we do this for the
33+
// // single-concatenation child too, or should?
34+
// // we wrap _that_?
35+
// return node
36+
// }
3737

3838
// Convert the top-level node without wrapping
3939
func convert() throws -> DSLTree.Node {
@@ -107,7 +107,8 @@ extension AST.Node {
107107

108108
// FIXME: make total function again
109109
let converted = try! convert()
110-
return wrap(converted)
110+
// return wrap(converted)
111+
return converted
111112
}
112113
}
113114

Sources/_StringProcessing/Regex/DSLList.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ extension DSLTree.Node {
6767
case .orderedChoice(let c), .concatenation(let c):
6868
return c.count
6969

70-
case .convertedRegexLiteral, .capture, .nonCapturingGroup,
71-
.quantification, .ignoreCapturesInTypedOutput, .conditional:
70+
case .capture, .nonCapturingGroup,
71+
.quantification, .ignoreCapturesInTypedOutput,
72+
.limitCaptureNesting, .conditional:
7273
return 1
7374

7475
case .absentFunction:

Sources/_StringProcessing/Regex/DSLTree.swift

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ extension DSLTree {
4444

4545
/// Marks all captures in a subpattern as ignored in strongly-typed output.
4646
case ignoreCapturesInTypedOutput(Node)
47-
47+
case limitCaptureNesting(Node)
48+
4849
// TODO: Consider splitting off grouped conditions, or have
4950
// our own kind
5051

@@ -79,13 +80,6 @@ extension DSLTree {
7980
/// TODO: Consider splitting off expression functions, or have our own kind
8081
case absentFunction(_AST.AbsentFunction)
8182

82-
// MARK: - Tree conversions
83-
84-
/// The target of AST conversion.
85-
///
86-
/// Keeps original AST around for rich syntactic and source information
87-
case convertedRegexLiteral(Node, _AST.ASTNode)
88-
8983
// MARK: - Extensibility points
9084

9185
case consumer(_ConsumerInterface)
@@ -384,8 +378,9 @@ extension DSLTree.Node {
384378
case .orderedChoice(let c), .concatenation(let c):
385379
return !c.isEmpty
386380

387-
case .convertedRegexLiteral, .capture, .nonCapturingGroup,
388-
.quantification, .ignoreCapturesInTypedOutput, .conditional:
381+
case .capture, .nonCapturingGroup,
382+
.quantification, .ignoreCapturesInTypedOutput, .limitCaptureNesting,
383+
.conditional:
389384
return true
390385

391386
case .absentFunction(let abs):
@@ -400,14 +395,14 @@ extension DSLTree.Node {
400395
case let .orderedChoice(v): return v
401396
case let .concatenation(v): return v
402397

403-
case let .convertedRegexLiteral(n, _):
404-
// Treat this transparently
405-
return n.children
406-
407398
case let .capture(_, _, n, _): return [n]
408399
case let .nonCapturingGroup(_, n): return [n]
409400
case let .quantification(_, _, n): return [n]
410-
case let .ignoreCapturesInTypedOutput(n): return [n]
401+
case let .ignoreCapturesInTypedOutput(n): return [n]
402+
403+
case let .limitCaptureNesting(n):
404+
// This is a transparent wrapper
405+
return n.children
411406

412407
case let .conditional(_, t, f): return [t,f]
413408

@@ -424,18 +419,12 @@ extension DSLTree.Node {
424419

425420
extension DSLTree.Node {
426421
var astNode: AST.Node? {
427-
switch self {
428-
case let .convertedRegexLiteral(_, literal): return literal.ast
429-
default: return nil
430-
}
422+
nil
431423
}
432424

433425
/// If this node is for a converted literal, look through it.
434426
var lookingThroughConvertedLiteral: Self {
435-
switch self {
436-
case let .convertedRegexLiteral(n, _): return n
437-
default: return self
438-
}
427+
self
439428
}
440429
}
441430

@@ -468,10 +457,6 @@ extension DSLTree.Node {
468457
switch self {
469458
case .capture:
470459
return true
471-
case let .convertedRegexLiteral(n, re):
472-
assert(n.hasCapture == re.ast.hasCapture)
473-
return n.hasCapture
474-
475460
default:
476461
return self.children.any(\.hasCapture)
477462
}
@@ -655,6 +640,9 @@ extension CaptureList.Builder {
655640
case let .ignoreCapturesInTypedOutput(child):
656641
addCaptures(of: child, optionalNesting: nesting, visibleInTypedOutput: false)
657642

643+
case let .limitCaptureNesting(child):
644+
addCaptures(of: child, optionalNesting: nesting.disablingNesting, visibleInTypedOutput: visibleInTypedOutput)
645+
658646
case let .conditional(cond, trueBranch, falseBranch):
659647
switch cond.ast {
660648
case .group(let g):
@@ -685,11 +673,11 @@ extension CaptureList.Builder {
685673
#endif
686674
}
687675

688-
case let .convertedRegexLiteral(n, _):
689-
// We disable nesting for converted AST trees, as literals do not nest
690-
// captures. This includes literals nested in a DSL.
691-
return addCaptures(of: n, optionalNesting: nesting.disablingNesting, visibleInTypedOutput: visibleInTypedOutput)
692-
676+
// case let .convertedRegexLiteral(n, _):
677+
// // We disable nesting for converted AST trees, as literals do not nest
678+
// // captures. This includes literals nested in a DSL.
679+
// return addCaptures(of: n, optionalNesting: nesting.disablingNesting, visibleInTypedOutput: visibleInTypedOutput)
680+
//
693681
case .matcher:
694682
break
695683

@@ -717,8 +705,8 @@ extension DSLTree.Node {
717705
return true
718706
case .orderedChoice, .concatenation, .capture,
719707
.conditional, .quantification, .customCharacterClass, .atom,
720-
.trivia, .empty, .quotedLiteral, .absentFunction,
721-
.convertedRegexLiteral, .consumer,
708+
.trivia, .empty, .quotedLiteral, .limitCaptureNesting,
709+
.consumer, .absentFunction,
722710
.characterPredicate, .matcher:
723711
return false
724712
}
@@ -805,8 +793,7 @@ extension DSLTree.Node {
805793
options.beginScope()
806794
defer { options.endScope() }
807795
return child._canOnlyMatchAtStartImpl(&options)
808-
case .ignoreCapturesInTypedOutput(let child),
809-
.convertedRegexLiteral(let child, _):
796+
case .ignoreCapturesInTypedOutput(let child), .limitCaptureNesting(let child):
810797
return child._canOnlyMatchAtStartImpl(&options)
811798

812799
// A quantification that doesn't require its child to exist can still
@@ -869,14 +856,13 @@ extension DSLTree {
869856
case let .orderedChoice(v): return v.map(_Tree.init)
870857
case let .concatenation(v): return v.map(_Tree.init)
871858

872-
case let .convertedRegexLiteral(n, _):
873-
// Treat this transparently
874-
return _Tree(n).children
875-
876859
case let .capture(_, _, n, _): return [_Tree(n)]
877860
case let .nonCapturingGroup(_, n): return [_Tree(n)]
878861
case let .quantification(_, _, n): return [_Tree(n)]
879-
case let .ignoreCapturesInTypedOutput(n): return [_Tree(n)]
862+
case let .ignoreCapturesInTypedOutput(n): return [_Tree(n)]
863+
case let .limitCaptureNesting(n):
864+
// This is a transparent wrapper
865+
return _Tree(n).children
880866

881867
case let .conditional(_, t, f): return [_Tree(t), _Tree(f)]
882868

0 commit comments

Comments
 (0)