Skip to content

Commit 9ed4ba1

Browse files
committed
Disallow custom consumers in lookbehinds
This implementation currently just throws an error during compilation of the expression which for DSL based expressions will simply crash the program on execution
1 parent 179d8e7 commit 9ed4ba1

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

Sources/_StringProcessing/ByteCodeGen.swift

+29
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,10 @@ fileprivate extension Compiler.ByteCodeGen {
388388
_ kind: (forwards: Bool, positive: Bool),
389389
_ child: DSLTree.Node
390390
) throws {
391+
guard !child.containsCustomConsumer else {
392+
throw Unsupported("Lookarounds with custom consumers")
393+
}
394+
391395
let previousReverse = reverse
392396
reverse = !kind.forwards
393397
if kind.positive {
@@ -1369,3 +1373,28 @@ extension DSLTree.CustomCharacterClass {
13691373
return false
13701374
}
13711375
}
1376+
1377+
extension DSLTree.Node {
1378+
var containsCustomConsumer: Bool {
1379+
switch self {
1380+
case .orderedChoice(let array), .concatenation(let array):
1381+
array.contains { $0.containsCustomConsumer }
1382+
case .capture(_, _, let node, _):
1383+
node.containsCustomConsumer
1384+
case .nonCapturingGroup(_, let node):
1385+
node.containsCustomConsumer
1386+
case .ignoreCapturesInTypedOutput(let node):
1387+
node.containsCustomConsumer
1388+
case .conditional(_, let node, let node2):
1389+
node.containsCustomConsumer || node2.containsCustomConsumer
1390+
case .quantification(_, _, let node):
1391+
node.containsCustomConsumer
1392+
case .convertedRegexLiteral(let node, _):
1393+
node.containsCustomConsumer
1394+
case .customCharacterClass, .atom, .trivia, .empty, .quotedLiteral, .absentFunction, .characterPredicate:
1395+
false
1396+
case .consumer, .matcher:
1397+
true
1398+
}
1399+
}
1400+
}

Sources/_StringProcessing/Utility/RegexFactory.swift

+2
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ public struct _RegexFactory {
173173
public func lookbehindNonCapturing<Output>(
174174
_ component: some RegexComponent
175175
) -> Regex<Output> {
176+
// TODO: Compiler error if component contains a custom consumer?
176177
.init(node: .nonCapturingGroup(.lookbehind, component.regex.root))
177178
}
178179

@@ -181,6 +182,7 @@ public struct _RegexFactory {
181182
public func negativeLookbehindNonCapturing<Output>(
182183
_ component: some RegexComponent
183184
) -> Regex<Output> {
185+
// TODO: Compiler error if component contains a custom consumer?
184186
.init(node: .nonCapturingGroup(.negativeLookbehind, component.regex.root))
185187
}
186188

0 commit comments

Comments
 (0)