Skip to content

Rename Match and MatchResult to Output and Regex.Match. #221

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
Mar 21, 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: 3 additions & 3 deletions Sources/Exercises/Participants/RegexParticipant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ private func extractFromCaptures(
private func graphemeBreakPropertyData<RP: RegexComponent>(
forLine line: String,
using regex: RP
) -> GraphemeBreakEntry? where RP.Match == (Substring, Substring, Substring?, Substring) {
line.match(regex).map(\.match).flatMap(extractFromCaptures)
) -> GraphemeBreakEntry? where RP.Output == (Substring, Substring, Substring?, Substring) {
line.match(regex).map(\.output).flatMap(extractFromCaptures)
}

private func graphemeBreakPropertyDataLiteral(
Expand Down Expand Up @@ -91,7 +91,7 @@ private func graphemeBreakPropertyData(
TryCapture(OneOrMore(.word)) { Unicode.GraphemeBreakProperty($0) }
ZeroOrMore(.any)
}.map {
let (_, lower, upper, property) = $0.match
let (_, lower, upper, property) = $0.output
return GraphemeBreakEntry(lower...(upper ?? lower), property)
}
}
24 changes: 12 additions & 12 deletions Sources/VariadicsGenerator/VariadicsGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ var standardError = StandardErrorStream()

typealias Counter = Int64
let regexComponentProtocolName = "RegexComponent"
let matchAssociatedTypeName = "Match"
let outputAssociatedTypeName = "Output"
let patternProtocolRequirementName = "regex"
let regexTypeName = "Regex"
let baseMatchTypeName = "Substring"
Expand Down Expand Up @@ -202,15 +202,15 @@ struct VariadicsGenerator: ParsableCommand {
// Emit concatenation type declaration.

let whereClause: String = {
var result = " where R0.Match == "
var result = " where R0.\(outputAssociatedTypeName) == "
if leftArity == 0 {
result += "W0"
} else {
result += "(W0"
result += (0..<leftArity).map { ", C\($0)" }.joined()
result += ")"
}
result += ", R1.Match == "
result += ", R1.\(outputAssociatedTypeName) == "
if rightArity == 0 {
result += "W1"
} else {
Expand Down Expand Up @@ -267,7 +267,7 @@ struct VariadicsGenerator: ParsableCommand {
}
output(")")
}
output("> where R0.\(matchAssociatedTypeName) == ")
output("> where R0.\(outputAssociatedTypeName) == ")
if leftArity == 0 {
output("W0")
} else {
Expand Down Expand Up @@ -348,10 +348,10 @@ struct VariadicsGenerator: ParsableCommand {
self.matchType = arity == 0
? baseMatchTypeName
: "(\(baseMatchTypeName), \(quantifiedCaptures))"
self.whereClauseForInit = "where \(matchAssociatedTypeName) == \(matchType)" +
(arity == 0 ? "" : ", Component.Match == (W, \(capturesJoined))")
self.whereClauseForInit = "where \(outputAssociatedTypeName) == \(matchType)" +
(arity == 0 ? "" : ", Component.\(outputAssociatedTypeName) == (W, \(capturesJoined))")
self.whereClause = arity == 0 ? "" :
"where Component.Match == (W, \(capturesJoined))"
"where Component.\(outputAssociatedTypeName) == (W, \(capturesJoined))"
}
}

Expand Down Expand Up @@ -468,10 +468,10 @@ struct VariadicsGenerator: ParsableCommand {
let whereClause: String = {
var result = "where R0: \(regexComponentProtocolName), R1: \(regexComponentProtocolName)"
if leftArity > 0 {
result += ", R0.\(matchAssociatedTypeName) == (W0, \((0..<leftArity).map { "C\($0)" }.joined(separator: ", ")))"
result += ", R0.\(outputAssociatedTypeName) == (W0, \((0..<leftArity).map { "C\($0)" }.joined(separator: ", ")))"
}
if rightArity > 0 {
result += ", R1.\(matchAssociatedTypeName) == (W1, \((leftArity..<leftArity+rightArity).map { "C\($0)" }.joined(separator: ", ")))"
result += ", R1.\(outputAssociatedTypeName) == (W1, \((leftArity..<leftArity+rightArity).map { "C\($0)" }.joined(separator: ", ")))"
}
return result
}()
Expand Down Expand Up @@ -516,7 +516,7 @@ struct VariadicsGenerator: ParsableCommand {
}()
let whereClause: String = """
where R: \(regexComponentProtocolName), \
R.\(matchAssociatedTypeName) == (W, \(captures))
R.\(outputAssociatedTypeName) == (W, \(captures))
"""
let resultCaptures = (0..<arity).map { "C\($0)?" }.joined(separator: ", ")
output("""
Expand Down Expand Up @@ -544,8 +544,8 @@ struct VariadicsGenerator: ParsableCommand {
}
let rawNewMatchType = newMatchType(newCaptureType: "W")
let transformedNewMatchType = newMatchType(newCaptureType: "NewCapture")
let whereClauseRaw = "where \(matchAssociatedTypeName) == \(rawNewMatchType), R.\(matchAssociatedTypeName) == \(matchType)"
let whereClauseTransformed = "where \(matchAssociatedTypeName) == \(transformedNewMatchType), R.\(matchAssociatedTypeName) == \(matchType)"
let whereClauseRaw = "where \(outputAssociatedTypeName) == \(rawNewMatchType), R.\(outputAssociatedTypeName) == \(matchType)"
let whereClauseTransformed = "where \(outputAssociatedTypeName) == \(transformedNewMatchType), R.\(outputAssociatedTypeName) == \(matchType)"
output("""
// MARK: - Non-builder capture arity \(arity)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ extension RegexConsumer {
consumed.base,
in: range, mode: .partialFromFront
) else { return nil }
return (result.range.upperBound, result.match)
return (result.range.upperBound, result.output)
}
}

// TODO: Explicitly implement the non-matching consumer/searcher protocols as
// well, taking advantage of the fact that the captures can be ignored

extension RegexConsumer: MatchingCollectionConsumer {
public typealias Match = R.Match
public typealias Match = R.Output

public func matchingConsuming(
_ consumed: Consumed, in range: Range<Consumed.Index>
Expand Down
12 changes: 6 additions & 6 deletions Sources/_StringProcessing/Executor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ struct Executor {
self.engine = Engine(program, enableTracing: enablesTracing)
}

func match<Match>(
func match<Output>(
_ input: String,
in inputRange: Range<String.Index>,
_ mode: MatchMode
) throws -> MatchResult<Match>? {
) throws -> Regex<Output>.Match? {
var cpu = engine.makeProcessor(
input: input, bounds: inputRange, matchMode: mode)

Expand All @@ -43,8 +43,8 @@ struct Executor {
// FIXME: This is a workaround for not tracking (or
// specially compiling) whole-match values.
let value: Any?
if Match.self != Substring.self,
Match.self != (Substring, DynamicCaptures).self,
if Output.self != Substring.self,
Output.self != (Substring, DynamicCaptures).self,
caps.isEmpty
{
value = cpu.registers.values.first
Expand All @@ -53,7 +53,7 @@ struct Executor {
value = nil
}

return MatchResult(
return .init(
input: input,
range: range,
rawCaptures: caps,
Expand All @@ -65,7 +65,7 @@ struct Executor {
_ input: String,
in inputRange: Range<String.Index>,
_ mode: MatchMode
) throws -> MatchResult<(Substring, DynamicCaptures)>? {
) throws -> Regex<(Substring, DynamicCaptures)>.Match? {
try match(input, in: inputRange, mode)
}
}
4 changes: 2 additions & 2 deletions Sources/_StringProcessing/RegexDSL/Anchor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ extension Anchor {
public func lookahead<R: RegexComponent>(
negative: Bool = false,
@RegexComponentBuilder _ content: () -> R
) -> Regex<R.Match> {
) -> Regex<R.Output> {
Regex(node: .nonCapturingGroup(negative ? .negativeLookahead : .lookahead, content().regex.root))
}

public func lookahead<R: RegexComponent>(
_ component: R,
negative: Bool = false
) -> Regex<R.Match> {
) -> Regex<R.Output> {
Regex(node: .nonCapturingGroup(negative ? .negativeLookahead : .lookahead, component.regex.root))
}
24 changes: 12 additions & 12 deletions Sources/_StringProcessing/RegexDSL/Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import _MatchingEngine

/// A type that represents a regular expression.
public protocol RegexComponent {
associatedtype Match
var regex: Regex<Match> { get }
associatedtype Output
var regex: Regex<Output> { get }
}

/// A regular expression.
public struct Regex<Match>: RegexComponent {
public struct Regex<Output>: RegexComponent {
/// A program representation that caches any lowered representation for
/// execution.
internal class Program {
Expand Down Expand Up @@ -80,37 +80,37 @@ public struct Regex<Match>: RegexComponent {

public init<Content: RegexComponent>(
_ content: Content
) where Content.Match == Match {
) where Content.Output == Output {
self = content.regex
}

public init<Content: RegexComponent>(
@RegexComponentBuilder _ content: () -> Content
) where Content.Match == Match {
) where Content.Output == Output {
self.init(content())
}

public var regex: Regex<Match> {
public var regex: Regex<Output> {
self
}
}


public struct MockRegexLiteral<Match>: RegexComponent {
public struct MockRegexLiteral<Output>: RegexComponent {
public typealias MatchValue = Substring
public let regex: Regex<Match>
public let regex: Regex<Output>

public init(
_ string: String,
_ syntax: SyntaxOptions = .traditional,
matching: Match.Type = Match.self
matching: Output.Type = Output.self
) throws {
regex = Regex(ast: try parse(string, syntax))
}
}

public func r<Match>(
_ s: String, matching matchType: Match.Type = Match.self
) -> MockRegexLiteral<Match> {
public func r<Output>(
_ s: String, matching matchType: Output.Type = Output.self
) -> MockRegexLiteral<Output> {
try! MockRegexLiteral(s, matching: matchType)
}
Loading