Skip to content

Break out of quantification loop if there is no forward progress #560

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

Merged
merged 4 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
63 changes: 63 additions & 0 deletions Sources/_StringProcessing/ByteCodeGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,12 @@ fileprivate extension Compiler.ByteCodeGen {
decrement %minTrips and fallthrough

loop-body:
<if can't guarantee forward progress && extraTrips = nil>:
mov currentPosition %pos
evaluate the subexpression
<if can't guarantee forward progress && extraTrips = nil>:
if %pos is currentPosition:
goto exit
goto min-trip-count control block

exit-policy control block:
Expand Down Expand Up @@ -646,7 +651,26 @@ fileprivate extension Compiler.ByteCodeGen {
// <subexpression>
// branch min-trip-count
builder.label(loopBody)

var positionReg: PositionRegister? = nil
// if we aren't sure if the child node will have forward progress and
// we have an unbounded quantification,
if (!optimizationsEnabled || !child.guaranteesForwardProgress),
extraTrips == nil
{
positionReg = builder.makePositionRegister()
builder.buildMoveCurrentPosition(into: positionReg!)
}
try emitNode(child)
if (!optimizationsEnabled || !child.guaranteesForwardProgress),
extraTrips == nil
{
// in all quantifier cases, no matter what minTrips or extraTrips is,
// if we have a successful non-advancing match, branch to exit because it
// can match an arbitrary number of times
builder.buildCondBranch(to: exit, ifSamePositionAs: positionReg!)
}

if minTrips <= 1 {
// fallthrough
} else {
Expand Down Expand Up @@ -832,3 +856,42 @@ fileprivate extension Compiler.ByteCodeGen {
return nil
}
}

extension DSLTree.Node {
var guaranteesForwardProgress: Bool {
switch self {
case .orderedChoice(let children):
return children.allSatisfy { $0.guaranteesForwardProgress }
case .concatenation(let children):
return children.contains(where: { $0.guaranteesForwardProgress })
case .capture(_, _, let node, _):
return node.guaranteesForwardProgress
case .nonCapturingGroup(let kind, let child):
switch kind.ast {
case .lookahead, .negativeLookahead, .lookbehind, .negativeLookbehind:
return false
default: return child.guaranteesForwardProgress
}
case .atom(let atom):
switch atom {
case .changeMatchingOptions, .assertion: return false
default: return true
}
case .trivia, .empty:
return false
case .quotedLiteral(let string):
return !string.isEmpty
case .convertedRegexLiteral(let node, _):
return node.guaranteesForwardProgress
case .consumer, .matcher:
// Allow zero width consumers and matchers
return false
case .customCharacterClass:
return true
case .quantification(let amount, _, let child):
let (atLeast, _) = amount.ast.bounds
return atLeast ?? 0 > 0 && child.guaranteesForwardProgress
default: return false
}
}
}
10 changes: 7 additions & 3 deletions Sources/_StringProcessing/Engine/Backtracking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,18 @@ extension Processor {
// The int registers store values that can be relevant to
// backtracking, such as the number of trips in a quantification.
var intRegisters: [Int]
// Same with position registers
var posRegisters: [Input.Index]

var destructure: (
pc: InstructionAddress,
pos: Position?,
stackEnd: CallStackAddress,
captureEnds: [_StoredCapture],
intRegisters: [Int]
intRegisters: [Int],
PositionRegister: [Input.Index]
) {
(pc, pos, stackEnd, captureEnds, intRegisters)
(pc, pos, stackEnd, captureEnds, intRegisters, posRegisters)
}
}

Expand All @@ -53,7 +56,8 @@ extension Processor {
pos: addressOnly ? nil : currentPosition,
stackEnd: .init(callStack.count),
captureEnds: storedCaptures,
intRegisters: registers.ints)
intRegisters: registers.ints,
posRegisters: registers.positions)
}
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/_StringProcessing/Engine/InstPayload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,10 @@ extension Instruction.Payload {
interpretPair()
}

init(pos: PositionRegister, pos2: PositionRegister) {
self.init(pos, pos2)
init(addr: InstructionAddress, position: PositionRegister) {
self.init(addr, position)
}
var pairedPosPos: (PositionRegister, PositionRegister) {
var pairedAddrPos: (InstructionAddress, PositionRegister) {
interpretPair()
}

Expand Down
18 changes: 18 additions & 0 deletions Sources/_StringProcessing/Engine/Instruction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ extension Instruction {
///
case moveImmediate

/// Move the current position into a register
///
/// moveCurrentPosition(into: PositionRegister)
///
/// Operands:
/// - Position register to move into
case moveCurrentPosition

// MARK: General Purpose: Control flow

/// Branch to a new instruction
Expand All @@ -57,6 +65,16 @@ extension Instruction {
///
case condBranchZeroElseDecrement

/// Conditionally branch if the current position is the same as the register
///
/// condBranch(
/// to: InstAddr, ifSamePositionAs: PositionRegister)
///
/// Operands:
/// - Instruction address to branch to, if the position in the register is the same as currentPosition
/// - Position register to check against
case condBranchSamePosition

// TODO: Function calls

// MARK: - Matching
Expand Down
23 changes: 22 additions & 1 deletion Sources/_StringProcessing/Engine/MEBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ extension MEProgram {
var nextIntRegister = IntRegister(0)
var nextCaptureRegister = CaptureRegister(0)
var nextValueRegister = ValueRegister(0)
var nextPositionRegister = PositionRegister(0)

// Special addresses or instructions
var failAddressToken: AddressToken? = nil
Expand Down Expand Up @@ -105,6 +106,14 @@ extension MEProgram.Builder {
fixup(to: t)
}

mutating func buildCondBranch(
to t: AddressToken,
ifSamePositionAs r: PositionRegister
) {
instructions.append(.init(.condBranchSamePosition, .init(position: r)))
fixup(to: t)
}

mutating func buildSave(_ t: AddressToken) {
instructions.append(.init(.save))
fixup(to: t)
Expand Down Expand Up @@ -211,6 +220,10 @@ extension MEProgram.Builder {
.init(value: value, capture: capture)))
}

mutating func buildMoveCurrentPosition(into r: PositionRegister) {
instructions.append(.init(.moveCurrentPosition, .init(position: r)))
}

mutating func buildBackreference(
_ cap: CaptureRegister
) {
Expand Down Expand Up @@ -257,7 +270,8 @@ extension MEProgram.Builder {
switch inst.opcode {
case .condBranchZeroElseDecrement:
payload = .init(addr: addr, int: inst.payload.int)

case .condBranchSamePosition:
payload = .init(addr: addr, position: inst.payload.position)
case .branch, .save, .saveAddress, .clearThrough:
payload = .init(addr: addr)

Expand All @@ -281,6 +295,7 @@ extension MEProgram.Builder {
regInfo.sequences = sequences.count
regInfo.ints = nextIntRegister.rawValue
regInfo.values = nextValueRegister.rawValue
regInfo.positions = nextPositionRegister.rawValue
regInfo.bitsets = asciiBitsets.count
regInfo.consumeFunctions = consumeFunctions.count
regInfo.assertionFunctions = assertionFunctions.count
Expand Down Expand Up @@ -421,6 +436,12 @@ extension MEProgram.Builder {
return r
}

mutating func makePositionRegister() -> PositionRegister {
let r = nextPositionRegister
defer { nextPositionRegister.rawValue += 1 }
return r
}

// TODO: A register-mapping helper struct, which could release
// registers without monotonicity required

Expand Down
16 changes: 13 additions & 3 deletions Sources/_StringProcessing/Engine/Processor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ extension Processor {
}

mutating func signalFailure() {
guard let (pc, pos, stackEnd, capEnds, intRegisters) =
guard let (pc, pos, stackEnd, capEnds, intRegisters, posRegisters) =
savePoints.popLast()?.destructure
else {
state = .fail
Expand All @@ -259,6 +259,7 @@ extension Processor {
callStack.removeLast(callStack.count - stackEnd.rawValue)
storedCaptures = capEnds
registers.ints = intRegisters
registers.positions = posRegisters
}

mutating func abort(_ e: Error? = nil) {
Expand Down Expand Up @@ -315,7 +316,10 @@ extension Processor {

registers[reg] = int
controller.step()

case .moveCurrentPosition:
let reg = payload.position
registers[reg] = currentPosition
controller.step()
case .branch:
controller.pc = payload.addr

Expand All @@ -327,7 +331,13 @@ extension Processor {
registers[int] -= 1
controller.step()
}

case .condBranchSamePosition:
let (addr, pos) = payload.pairedAddrPos
if registers[pos] == currentPosition {
controller.pc = addr
} else {
controller.step()
}
case .save:
let resumeAddr = payload.addr
let sp = makeSavePoint(resumeAddr)
Expand Down
14 changes: 14 additions & 0 deletions Sources/_StringProcessing/Engine/Registers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ extension Processor {
var ints: [Int]

var values: [Any]

var positions: [Input.Index]
}
}

Expand All @@ -66,6 +68,12 @@ extension Processor.Registers {
values[i.rawValue] = newValue
}
}
subscript(_ i: PositionRegister) -> Input.Index {
get { positions[i.rawValue] }
set {
positions[i.rawValue] = newValue
}
}
subscript(_ i: ElementRegister) -> Input.Element {
elements[i.rawValue]
}
Expand All @@ -89,6 +97,8 @@ extension Processor.Registers {
}

extension Processor.Registers {
static let sentinelIndex = "".startIndex

init(
_ program: MEProgram,
_ sentinel: String.Index
Expand Down Expand Up @@ -120,11 +130,15 @@ extension Processor.Registers {

self.values = Array(
repeating: SentinelValue(), count: info.values)
self.positions = Array(
repeating: Processor.Registers.sentinelIndex,
count: info.positions)
Copy link
Member

Choose a reason for hiding this comment

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

Future: Why the start index instead of the provided sentinel? Should or could we construct an invalid index and at least assert that we're never loading that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

SentinelValue only works on the value registers since they are [Any]. For now we only emit position registers in this one case, if/when we have more complicated cases in the future we'll want some kinda validation like that

}

mutating func reset(sentinel: Input.Index) {
self.ints._setAll(to: 0)
self.values._setAll(to: SentinelValue())
self.positions._setAll(to: Processor.Registers.sentinelIndex)
}
}

Expand Down
36 changes: 36 additions & 0 deletions Tests/RegexTests/CompileTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,40 @@ extension RegexTests {
expectProgram(for: "[abc]", semanticLevel: .unicodeScalar, doesNotContain: [.matchBitset])
expectProgram(for: "[abc]", semanticLevel: .unicodeScalar, contains: [.consumeBy])
}

func testQuantificationForwardProgressCompile() {
Copy link
Member

Choose a reason for hiding this comment

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

I'm ok with these for now, but do note that the kinds of tests can become extremely fragile and are likely to get dropped if control flow is ever overhauled. Make sure anything important is represented by both functional tests and more precise unit testing.

E.g. you can test the guaranteesForwardProgress query on regexes, and that wouldn't be fragile.

// Unbounded quantification + non forward progressing inner nodes
// Expect to emit the position checking instructions
expectProgram(for: #"(?:(?=a)){1,}"#, contains: [.moveCurrentPosition, .condBranchSamePosition])
expectProgram(for: #"(?:\b)*"#, contains: [.moveCurrentPosition, .condBranchSamePosition])
expectProgram(for: #"(?:(?#comment))+"#, contains: [.moveCurrentPosition, .condBranchSamePosition])
expectProgram(for: #"(?:|)+"#, contains: [.moveCurrentPosition, .condBranchSamePosition])
expectProgram(for: #"(?:\w|)+"#, contains: [.moveCurrentPosition, .condBranchSamePosition])
expectProgram(for: #"(?:\w|(?i-i:))+"#, contains: [.moveCurrentPosition, .condBranchSamePosition])
expectProgram(for: #"(?:\w|(?#comment))+"#, contains: [.moveCurrentPosition, .condBranchSamePosition])
expectProgram(for: #"(?:\w|(?#comment)(?i-i:))+"#, contains: [.moveCurrentPosition, .condBranchSamePosition])
expectProgram(for: #"(?:\w|(?i))+"#, contains: [.moveCurrentPosition, .condBranchSamePosition])

// Bounded quantification, don't emit position checking
expectProgram(for: #"(?:(?=a)){1,4}"#, doesNotContain: [.moveCurrentPosition, .condBranchSamePosition])
expectProgram(for: #"(?:\b)?"#, doesNotContain: [.moveCurrentPosition, .condBranchSamePosition])
expectProgram(for: #"(?:(?#comment)){,4}"#, doesNotContain: [.moveCurrentPosition, .condBranchSamePosition])
expectProgram(for: #"(?:|){,4}"#, doesNotContain: [.moveCurrentPosition, .condBranchSamePosition])
expectProgram(for: #"(?:\w|){,4}"#, doesNotContain: [.moveCurrentPosition, .condBranchSamePosition])
expectProgram(for: #"(?:\w|(?i-i:)){,4}"#, doesNotContain: [.moveCurrentPosition, .condBranchSamePosition])
expectProgram(for: #"(?:\w|(?#comment)){,4}"#, doesNotContain: [.moveCurrentPosition, .condBranchSamePosition])
expectProgram(for: #"(?:\w|(?#comment)(?i-i:)){,4}"#, doesNotContain: [.moveCurrentPosition, .condBranchSamePosition])
expectProgram(for: #"(?:\w|(?i)){,4}"#, doesNotContain: [.moveCurrentPosition, .condBranchSamePosition])

// Inner node is a quantification that does not guarantee forward progress
expectProgram(for: #"(a*)*"#, contains: [.moveCurrentPosition, .condBranchSamePosition])
expectProgram(for: #"(a?)*"#, contains: [.moveCurrentPosition, .condBranchSamePosition])
expectProgram(for: #"(a{,5})*"#, contains: [.moveCurrentPosition, .condBranchSamePosition])
expectProgram(for: #"((\b){,4})*"#, contains: [.moveCurrentPosition, .condBranchSamePosition])
expectProgram(for: #"((\b){1,4})*"#, contains: [.moveCurrentPosition, .condBranchSamePosition])
expectProgram(for: #"((|){1,4})*"#, contains: [.moveCurrentPosition, .condBranchSamePosition])
// Inner node is a quantification that guarantees forward progress
expectProgram(for: #"(a+)*"#, doesNotContain: [.moveCurrentPosition, .condBranchSamePosition])
expectProgram(for: #"(a{1,})*"#, doesNotContain: [.moveCurrentPosition, .condBranchSamePosition])
}
}
28 changes: 27 additions & 1 deletion Tests/RegexTests/MatchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1895,5 +1895,31 @@ extension RegexTests {
XCTAssertEqual(matches.count, 3)
}
}
}

func expectCompletion(regex: String, in target: String) {
let expectation = XCTestExpectation(description: "Run the given regex to completion")
Task.init {
let r = try! Regex(regex)
let val = target.matches(of: r).isEmpty
expectation.fulfill()
return val
}
wait(for: [expectation], timeout: 3.0)
}

func testQuantificationForwardProgress() {
expectCompletion(regex: #"(?:(?=a)){1,}"#, in: "aa")
expectCompletion(regex: #"(?:\b)+"#, in: "aa")
expectCompletion(regex: #"(?:(?#comment))+"#, in: "aa")
expectCompletion(regex: #"(?:|)+"#, in: "aa")
expectCompletion(regex: #"(?:\w|)+"#, in: "aa")
expectCompletion(regex: #"(?:\w|(?i-i:))+"#, in: "aa")
expectCompletion(regex: #"(?:\w|(?#comment))+"#, in: "aa")
expectCompletion(regex: #"(?:\w|(?#comment)(?i-i:))+"#, in: "aa")
expectCompletion(regex: #"(?:\w|(?i))+"#, in: "aa")
expectCompletion(regex: #"(a*)*"#, in: "aa")
expectCompletion(regex: #"(a?)*"#, in: "aa")
expectCompletion(regex: #"(a{,4})*"#, in: "aa")
expectCompletion(regex: #"((|)+)*"#, in: "aa")
}
}