Skip to content

Keep track of initial options in compiled program #412

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
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Keep track of a regex's initial options
The initial options are stored in the lower program, and include
all options that are set before the first attempted match. Note that
not all initial options are global - a leading option-setting group
is included in initial options, even though it applies only to a
portion of the overall regex.
  • Loading branch information
natecook1000 committed May 13, 2022
commit a8bea8d7d9a8611984db871df7f4130a4c0d9bf7
6 changes: 6 additions & 0 deletions Sources/_StringProcessing/ByteCodeGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ extension Compiler.ByteCodeGen {
builder.buildUnresolvedReference(id: id)

case let .changeMatchingOptions(optionSequence):
if !builder.hasReceivedInstructions {
builder.initialOptions.apply(optionSequence.ast)
}
options.apply(optionSequence.ast)

case let .unconverted(astAtom):
Expand Down Expand Up @@ -379,6 +382,9 @@ extension Compiler.ByteCodeGen {
throw Unreachable("These should produce a capture node")

case .changeMatchingOptions(let optionSequence):
if !builder.hasReceivedInstructions {
builder.initialOptions.apply(optionSequence)
}
options.apply(optionSequence)
try emitNode(child)

Expand Down
9 changes: 8 additions & 1 deletion Sources/_StringProcessing/Engine/MEBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ extension MEProgram where Input.Element: Hashable {
var failAddressToken: AddressToken? = nil

var captureList = CaptureList()
var initialOptions = MatchingOptions()

// Symbolic reference resolution
var unresolvedReferences: [ReferenceID: [InstructionAddress]] = [:]
Expand Down Expand Up @@ -77,6 +78,11 @@ extension MEProgram.Builder {
var lastInstructionAddress: InstructionAddress {
.init(instructions.endIndex - 1)
}

/// `true` if the builder has received any instructions.
var hasReceivedInstructions: Bool {
!instructions.isEmpty
}

mutating func buildNop(_ r: StringRegister? = nil) {
instructions.append(.init(.nop, .init(optionalString: r)))
Expand Down Expand Up @@ -353,7 +359,8 @@ extension MEProgram.Builder {
registerInfo: regInfo,
captureList: captureList,
referencedCaptureOffsets: referencedCaptureOffsets,
namedCaptureOffsets: namedCaptureOffsets)
namedCaptureOffsets: namedCaptureOffsets,
initialOptions: initialOptions)
}

mutating func reset() { self = Self() }
Expand Down
2 changes: 2 additions & 0 deletions Sources/_StringProcessing/Engine/MEProgram.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ struct MEProgram<Input: Collection> where Input.Element: Equatable {
let captureList: CaptureList
let referencedCaptureOffsets: [ReferenceID: Int]
let namedCaptureOffsets: [String: Int]

var initialOptions: MatchingOptions
}

extension MEProgram: CustomStringConvertible {
Expand Down
8 changes: 8 additions & 0 deletions Sources/_StringProcessing/MatchingOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ extension MatchingOptions {
stack[stack.count - 1].apply(sequence)
_invariantCheck()
}

// @testable
/// Returns true if the options at the top of `stack` are equal to those
/// for `other`.
func _equal(to other: MatchingOptions) -> Bool {
stack.last == other.stack.last
}
}

// MARK: Matching behavior API
Expand Down Expand Up @@ -127,6 +134,7 @@ extension MatchingOptions {
}
}

// MARK: - Implementation
extension MatchingOptions {
/// An option that changes the behavior of a regular expression.
fileprivate enum Option: Int {
Expand Down
10 changes: 1 addition & 9 deletions Sources/_StringProcessing/Regex/ASTConversion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,7 @@

extension AST {
var dslTree: DSLTree {
return DSLTree(
root.dslTreeNode, options: globalOptions?.dslTreeOptions)
}
}

extension AST.GlobalMatchingOptionSequence {
var dslTreeOptions: DSLTree.Options {
// TODO: map options
return .init()
return DSLTree(root.dslTreeNode)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/_StringProcessing/Regex/Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,6 @@ extension Regex {

@_spi(RegexBuilder)
public init(node: DSLTree.Node) {
self.program = Program(tree: .init(node, options: nil))
self.program = Program(tree: .init(node))
}
}
4 changes: 1 addition & 3 deletions Sources/_StringProcessing/Regex/DSLTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
@_spi(RegexBuilder)
public struct DSLTree {
var root: Node
var options: Options?

init(_ r: Node, options: Options?) {
init(_ r: Node) {
self.root = r
self.options = options
}
}

Expand Down
47 changes: 47 additions & 0 deletions Tests/RegexTests/CompileTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,51 @@ extension RegexTests {
try testCompilationEquivalence(row)
}
}

func testCompileInitialOptions() throws {
func expectInitialOptions<T>(
_ regex: Regex<T>,
_ optionSequence: AST.MatchingOptionSequence,
file: StaticString = #file,
line: UInt = #line
) throws {
var options = MatchingOptions()
options.apply(optionSequence)

XCTAssertTrue(
regex.program.loweredProgram.initialOptions._equal(to: options),
file: file, line: line)
}

func expectInitialOptions(
_ pattern: String,
_ optionSequence: AST.MatchingOptionSequence,
file: StaticString = #file,
line: UInt = #line
) throws {
let regex = try Regex(pattern)
try expectInitialOptions(regex, optionSequence, file: file, line: line)
}

try expectInitialOptions(".", matchingOptions())
try expectInitialOptions("(?i)(?-i).", matchingOptions())

try expectInitialOptions("(?i).", matchingOptions(adding: [.caseInsensitive]))
try expectInitialOptions("(?i).(?-i)", matchingOptions(adding: [.caseInsensitive]))

try expectInitialOptions(
"(?im)(?s).",
matchingOptions(adding: [.caseInsensitive, .multiline, .singleLine]))
try expectInitialOptions(".", matchingOptions())
try expectInitialOptions(
"(?im)(?s).(?u)",
matchingOptions(adding: [.caseInsensitive, .multiline, .singleLine]))

try expectInitialOptions(
"(?i:.)",
matchingOptions(adding: [.caseInsensitive]))
try expectInitialOptions(
"(?i:.)(?m:.)",
matchingOptions(adding: [.caseInsensitive]))
}
}