Skip to content

Share the same processor in firstMatch #497

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 1 commit into from
Jun 17, 2022
Merged
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
46 changes: 39 additions & 7 deletions Sources/_StringProcessing/Engine/Processor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,38 @@ struct Processor<
typealias Element = Input.Element

let input: Input
let bounds: Range<Position>
let matchMode: MatchMode
let instructions: InstructionList<Instruction>

// MARK: Resettable state

// The subject bounds.
//
// FIXME: This also conflates search bounds too!
var bounds: Range<Position>

// The current position in the subject
var currentPosition: Position

let instructions: InstructionList<Instruction>
var controller: Controller

var cycleCount = 0

/// Our register file
var registers: Registers

// Used for back tracking
var savePoints: [SavePoint] = []

var callStack: [InstructionAddress] = []

var storedCaptures: Array<_StoredCapture>

var state: State = .inProgress

var failureReason: Error? = nil


// MARK: Metrics, debugging, etc.
var cycleCount = 0
var isTracingEnabled: Bool

var storedCaptures: Array<_StoredCapture>
}

extension Processor {
Expand Down Expand Up @@ -88,6 +96,30 @@ extension Processor {
_checkInvariants()
}


mutating func reset(searchBounds: Range<Position>) {
// FIXME: We currently conflate both subject bounds and search bounds
// This should just reset search bounds
self.bounds = searchBounds
self.currentPosition = self.bounds.lowerBound

self.controller = Controller(pc: 0)

self.registers.reset(sentinel: bounds.upperBound)

self.savePoints.removeAll(keepingCapacity: true)
self.callStack.removeAll(keepingCapacity: true)

for idx in storedCaptures.indices {
storedCaptures[idx] = .init()
}

self.state = .inProgress
self.failureReason = nil

_checkInvariants()
}

func _checkInvariants() {
assert(end <= input.endIndex)
assert(start >= input.startIndex)
Expand Down
139 changes: 65 additions & 74 deletions Sources/_StringProcessing/Engine/Registers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,19 @@ struct SentinelValue: Hashable, CustomStringConvertible {
extension Processor {
/// Our register file
struct Registers {
// currently, these are static readonly

// MARK: static / read-only, non-resettable

// Verbatim elements to compare against
var elements: [Element]

// currently, these are static readonly
// Verbatim sequences to compare against
//
// TODO: We want to be `String` instead of `[Character]`...
// TODO: Degenericize Processor and store Strings
var sequences: [[Element]] = []

// currently, hold output of assertions
var bools: [Bool] // TODO: bitset

// currently, these are static readonly
var consumeFunctions: [MEProgram<Input>.ConsumeFunction]

// currently, these are static readonly
var assertionFunctions: [MEProgram<Input>.AssertionFunction]

// Captured-value constructors
Expand All @@ -44,69 +42,61 @@ extension Processor {
// currently, these are for comments and abort messages
var strings: [String]

// MARK: writeable, resettable

// currently, hold output of assertions
var bools: [Bool] // TODO: bitset

// currently, useful for range-based quantification
var ints: [Int]

// unused
var floats: [Double] = []

// Currently, used for `movePosition` and `matchSlice`
var positions: [Position] = []

var values: [Any]
}
}

// unused
var instructionAddresses: [InstructionAddress] = []

// unused, any application?
var classStackAddresses: [CallStackAddress] = []

// unused, any application?
var positionStackAddresses: [PositionStackAddress] = []

// unused, any application?
var savePointAddresses: [SavePointStackAddress] = []

subscript(_ i: StringRegister) -> String {
strings[i.rawValue]
}
subscript(_ i: SequenceRegister) -> [Element] {
sequences[i.rawValue]
}
subscript(_ i: IntRegister) -> Int {
get { ints[i.rawValue] }
set { ints[i.rawValue] = newValue }
}
subscript(_ i: BoolRegister) -> Bool {
get { bools[i.rawValue] }
set { bools[i.rawValue] = newValue }
}
subscript(_ i: PositionRegister) -> Position {
get { positions[i.rawValue] }
set { positions[i.rawValue] = newValue }
}
subscript(_ i: ValueRegister) -> Any {
get { values[i.rawValue] }
set {
values[i.rawValue] = newValue
}
}
subscript(_ i: ElementRegister) -> Element {
elements[i.rawValue]
}
subscript(_ i: ConsumeFunctionRegister) -> MEProgram<Input>.ConsumeFunction {
consumeFunctions[i.rawValue]
}
subscript(_ i: AssertionFunctionRegister) -> MEProgram<Input>.AssertionFunction {
assertionFunctions[i.rawValue]
}
subscript(_ i: TransformRegister) -> MEProgram<Input>.TransformFunction {
transformFunctions[i.rawValue]
}
subscript(_ i: MatcherRegister) -> MEProgram<Input>.MatcherFunction {
matcherFunctions[i.rawValue]
extension Processor.Registers {
subscript(_ i: StringRegister) -> String {
strings[i.rawValue]
}
subscript(_ i: SequenceRegister) -> [Input.Element] {
sequences[i.rawValue]
}
subscript(_ i: IntRegister) -> Int {
get { ints[i.rawValue] }
set { ints[i.rawValue] = newValue }
}
subscript(_ i: BoolRegister) -> Bool {
get { bools[i.rawValue] }
set { bools[i.rawValue] = newValue }
}
subscript(_ i: PositionRegister) -> Input.Index {
get { positions[i.rawValue] }
set { positions[i.rawValue] = newValue }
}
subscript(_ i: ValueRegister) -> Any {
get { values[i.rawValue] }
set {
values[i.rawValue] = newValue
}
}
subscript(_ i: ElementRegister) -> Input.Element {
elements[i.rawValue]
}
subscript(_ i: ConsumeFunctionRegister) -> MEProgram<Input>.ConsumeFunction {
consumeFunctions[i.rawValue]
}
subscript(_ i: AssertionFunctionRegister) -> MEProgram<Input>.AssertionFunction {
assertionFunctions[i.rawValue]
}
subscript(_ i: TransformRegister) -> MEProgram<Input>.TransformFunction {
transformFunctions[i.rawValue]
}
subscript(_ i: MatcherRegister) -> MEProgram<Input>.MatcherFunction {
matcherFunctions[i.rawValue]
}
}

extension Processor.Registers {
Expand Down Expand Up @@ -141,20 +131,26 @@ extension Processor.Registers {

self.ints = Array(repeating: 0, count: info.ints)

self.floats = Array(repeating: 0, count: info.floats)

self.positions = Array(repeating: sentinel, count: info.positions)

self.values = Array(
repeating: SentinelValue(), count: info.values)
}

self.instructionAddresses = Array(repeating: 0, count: info.instructionAddresses)

self.classStackAddresses = Array(repeating: 0, count: info.classStackAddresses)

self.positionStackAddresses = Array(repeating: 0, count: info.positionStackAddresses)
mutating func reset(sentinel: Input.Index) {
self.bools._setAll(to: false)
self.ints._setAll(to: 0)
self.positions._setAll(to: sentinel)
self.values._setAll(to: SentinelValue())
}
}

self.savePointAddresses = Array(repeating: 0, count: info.savePointAddresses)
// TODO: Productize into general algorithm
extension MutableCollection {
mutating func _setAll(to e: Element) {
for idx in self.indices {
self[idx] = e
}
}
}

Expand Down Expand Up @@ -196,12 +192,7 @@ extension Processor.Registers: CustomStringConvertible {
\(formatRegisters("bools", bools))\
\(formatRegisters("strings", strings))\
\(formatRegisters("ints", ints))\
\(formatRegisters("floats", floats))\
\(formatRegisters("positions", positions))\
\(formatRegisters("instructionAddresses", instructionAddresses))\
\(formatRegisters("classStackAddresses", classStackAddresses))\
\(formatRegisters("positionStackAddresses", positionStackAddresses))\
\(formatRegisters("savePointAddresses", savePointAddresses))\

"""
}
Expand Down
35 changes: 35 additions & 0 deletions Sources/_StringProcessing/Executor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,33 @@ struct Executor {
self.engine = Engine(program, enableTracing: enablesTracing)
}

@available(SwiftStdlib 5.7, *)
func firstMatch<Output>(
_ input: String,
in inputRange: Range<String.Index>,
graphemeSemantic: Bool
) throws -> Regex<Output>.Match? {
var cpu = engine.makeProcessor(
input: input, bounds: inputRange, matchMode: .partialFromFront)

var low = inputRange.lowerBound
let high = inputRange.upperBound
while true {
if let m: Regex<Output>.Match = try _match(
input, in: low..<high, using: &cpu
) {
return m
}
if low >= high { return nil }
if graphemeSemantic {
input.formIndex(after: &low)
} else {
input.unicodeScalars.formIndex(after: &low)
}
cpu.reset(searchBounds: low..<high)
}
}

@available(SwiftStdlib 5.7, *)
func match<Output>(
_ input: String,
Expand All @@ -27,7 +54,15 @@ struct Executor {
) throws -> Regex<Output>.Match? {
var cpu = engine.makeProcessor(
input: input, bounds: inputRange, matchMode: mode)
return try _match(input, in: inputRange, using: &cpu)
}

@available(SwiftStdlib 5.7, *)
func _match<Output>(
_ input: String,
in inputRange: Range<String.Index>,
using cpu: inout Processor<String>
) throws -> Regex<Output>.Match? {
guard let endIdx = cpu.consume() else {
if let e = cpu.failureReason {
throw e
Expand Down
21 changes: 2 additions & 19 deletions Sources/_StringProcessing/Regex/Match.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,27 +137,10 @@ extension Regex {
_ input: String,
in inputRange: Range<String.Index>
) throws -> Regex<Output>.Match? {
// FIXME: Something more efficient, likely an engine interface, and we
// should scrap the RegexConsumer crap and call this

let executor = Executor(program: regex.program.loweredProgram)
let graphemeSemantic = regex.initialOptions.semanticLevel == .graphemeCluster

var low = inputRange.lowerBound
let high = inputRange.upperBound
while true {
if let m: Regex<Output>.Match = try executor.match(
input, in: low..<high, .partialFromFront
) {
return m
}
if low >= high { return nil }
if graphemeSemantic {
input.formIndex(after: &low)
} else {
input.unicodeScalars.formIndex(after: &low)
}
}
return try executor.firstMatch(
input, in: inputRange, graphemeSemantic: graphemeSemantic)
}
}

Expand Down