Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Regex lookbehind support #760

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions Sources/RegexBuilder/Anchor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,64 @@ public struct NegativeLookahead<Output>: _BuiltinRegexComponent {
self.init(_RegexFactory().negativeLookaheadNonCapturing(component()))
}
}

/// A regex component that allows a match to continue only if its contents
/// match at the given location.
///
/// A lookbehind is a zero-length assertion that its included regex matches at
/// a particular position. Lookbehinds do not advance the overall matching
/// position in the input string — once a lookbehind succeeds, matching continues
/// in the regex from the same position.
@available(SwiftStdlib 5.7, *) // TODO: How should this be gated?
Comment on lines +230 to +237
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably want to adjust the wording to denote the "behind" part of the assertion. CC @natecook1000

public struct Lookbehind<Output>: _BuiltinRegexComponent {
public var regex: Regex<Output>

init(_ regex: Regex<Output>) {
self.regex = regex
}

/// Creates a lookbehind from the given regex component.
public init<R: RegexComponent>(
_ component: R
) where R.RegexOutput == Output {
self.init(_RegexFactory().lookbehindNonCapturing(component))
}

/// Creates a lookbehind from the regex generated by the given builder closure.
public init<R: RegexComponent>(
@RegexComponentBuilder _ component: () -> R
) where R.RegexOutput == Output {
self.init(_RegexFactory().lookbehindNonCapturing(component()))
}
}

/// A regex component that allows a match to continue only if its contents
/// do not match at the given location.
///
/// A negative lookbehind is a zero-length assertion that its included regex
/// does not match at a particular position. Lookbehinds do not advance the
/// overall matching position in the input string — once a lookbehind succeeds,
/// matching continues in the regex from the same position.
@available(SwiftStdlib 5.7, *) // TODO: How should this be gated?
public struct NegativeLookbehind<Output>: _BuiltinRegexComponent {
public var regex: Regex<Output>

init(_ regex: Regex<Output>) {
self.regex = regex
}

/// Creates a negative lookbehind from the given regex component.
public init<R: RegexComponent>(
_ component: R
) where R.RegexOutput == Output {
self.init(_RegexFactory().negativeLookbehindNonCapturing(component))
}

/// Creates a negative lookbehind from the regex generated by the given builder
/// closure.
public init<R: RegexComponent>(
@RegexComponentBuilder _ component: () -> R
) where R.RegexOutput == Output {
self.init(_RegexFactory().negativeLookbehindNonCapturing(component()))
}
}
3 changes: 3 additions & 0 deletions Sources/_RegexParser/Regex/AST/MatchingOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ extension AST {

// NSRegularExpression compatibility special-case
case nsreCompatibleDot // no AST representation

// Lookbehind support
case reverse // no AST representation
}

public var kind: Kind
Expand Down
13 changes: 13 additions & 0 deletions Sources/_RegexParser/Regex/Parse/Parse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,19 @@ extension Parser {
mutating func parseCustomCharacterClass(
_ start: Source.Located<CustomCC.Start>
) -> CustomCC {
// Excessively nested recursion is a common DOS attack, so limit
// our recursion.
context.parseDepth += 1
defer { context.parseDepth -= 1 }
guard context.parseDepth < context.maxParseDepth else {
self.errorAtCurrentPosition(.nestingTooDeep)

// This is not generally recoverable and further errors will be
// incorrect
diags.suppressFurtherDiagnostics = true
return .init(start, [], start.location)
}

let alreadyInCCC = context.isInCustomCharacterClass
context.isInCustomCharacterClass = true
defer { context.isInCustomCharacterClass = alreadyInCCC }
Expand Down
8 changes: 4 additions & 4 deletions Sources/_RegexParser/Regex/Parse/Sema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ extension RegexValidator {
case .caseInsensitive, .possessiveByDefault, .reluctantByDefault,
.singleLine, .multiline, .namedCapturesOnly, .extended, .extraExtended,
.asciiOnlyDigit, .asciiOnlyWord, .asciiOnlySpace, .asciiOnlyPOSIXProps,
.nsreCompatibleDot:
.nsreCompatibleDot, .reverse:
break
}
}
Expand Down Expand Up @@ -370,7 +370,7 @@ extension RegexValidator {
}
switch kind.value {
case .capture, .namedCapture, .nonCapture, .lookahead, .negativeLookahead,
.atomicNonCapturing:
.atomicNonCapturing, .lookbehind, .negativeLookbehind:
break

case .balancedCapture:
Expand All @@ -384,8 +384,8 @@ extension RegexValidator {
case .nonAtomicLookahead:
error(.unsupported("non-atomic lookahead"), at: kind.location)

case .lookbehind, .negativeLookbehind, .nonAtomicLookbehind:
error(.unsupported("lookbehind"), at: kind.location)
case .nonAtomicLookbehind:
error(.unsupported("non-atomic lookbehind"), at: kind.location)

case .scriptRun, .atomicScriptRun:
error(.unsupported("script run"), at: kind.location)
Expand Down
Loading