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 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
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
14 changes: 13 additions & 1 deletion Sources/_StringProcessing/Regex/Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ extension Regex {
self.tree = tree
}
}

/// The set of matching options that applies to the start of this regex.
///
/// Note that the initial options may not apply to the entire regex. For
/// example, in this regex, only case insensitivity (`i`) and Unicode scalar
/// semantics (set by API) apply to the entire regex, while ASCII character
/// classes (`P`) is part of `initialOptions` but not global:
///
/// let regex = /(?i)(?P:\d+\s*)abc/.semanticLevel(.unicodeScalar)
var initialOptions: MatchingOptions {
program.loweredProgram.initialOptions
}
}

@available(SwiftStdlib 5.7, *)
Expand All @@ -102,6 +114,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
8 changes: 6 additions & 2 deletions Sources/_StringProcessing/Regex/Match.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,12 @@ extension Regex {
if let m = try _match(input, in: low..<high, mode: .partialFromFront) {
return m
}
if low == high { return nil }
input.formIndex(after: &low)
if low >= high { return nil }
if regex.initialOptions.semanticLevel == .graphemeCluster {
input.formIndex(after: &low)
} else {
input.unicodeScalars.formIndex(after: &low)
}
}
}
}
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]))
}
}
10 changes: 10 additions & 0 deletions Tests/RegexTests/MatchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1618,5 +1618,15 @@ extension RegexTests {

// TODO: Add test for grapheme boundaries at start/end of match

func testCase() {
let regex = try! Regex(#".\N{SPARKLING HEART}."#)
let input = "🧟‍♀️💖🧠 or 🧠💖☕️"
let characterMatches = input.matches(of: regex)
XCTAssertEqual(characterMatches.map { $0.0 }, ["🧟‍♀️💖🧠", "🧠💖☕️"])

let scalarMatches = input.matches(of: regex.matchingSemantics(.unicodeScalar))
let scalarExpected: [Substring] = ["\u{FE0F}💖🧠", "🧠💖☕"]
XCTAssertEqual(scalarMatches.map { $0.0 }, scalarExpected)
}
}