Skip to content

Commit dfb5ce6

Browse files
authored
Make Pattern Equatable. (#44)
Make AnyPattern not use protocol type.
1 parent 6825786 commit dfb5ce6

File tree

5 files changed

+21
-24
lines changed

5 files changed

+21
-24
lines changed

Sources/Patterns/Atomic Patterns/OneOf.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ public struct OneOf: Pattern, RegexConvertible {
7676
public func createInstructions(_ instructions: inout Instructions) {
7777
instructions.append(.checkElement(group.contains))
7878
}
79+
80+
public static func == (lhs: OneOf, rhs: OneOf) -> Bool {
81+
lhs.description == rhs.description
82+
}
7983
}
8084

8185
// MARK: OneOfConvertible

Sources/Patterns/Grammar.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ public class Grammar: Pattern {
112112

113113
instructions[startIndex + 1] = .jump(offset: instructions.endIndex - startIndex - 1)
114114
}
115+
116+
public static func == (lhs: Grammar, rhs: Grammar) -> Bool {
117+
lhs.patterns.elementsEqual(rhs.patterns, by: { $0 == $1 })
118+
}
115119
}
116120

117121
infix operator <-: AssignmentPrecedence

Sources/Patterns/Operations on Patterns/AnyPattern.swift

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public struct AnyPattern: Pattern {
2222
/// The wrapped pattern. If you know the exact type you can unwrap it again.
2323
public let wrapped: Any
2424

25-
public init(_ p: Pattern) {
25+
public init<P: Pattern>(_ p: P) {
2626
_instructions = p.createInstructions
2727
_description = { p.description }
2828
wrapped = p
@@ -38,6 +38,10 @@ public struct AnyPattern: Pattern {
3838
_description = { p.description }
3939
wrapped = p
4040
}
41+
42+
public static func == (lhs: AnyPattern, rhs: AnyPattern) -> Bool {
43+
lhs.description == rhs.description
44+
}
4145
}
4246

4347
/// Allows AnyPattern to be defined by a string with patterns in interpolations.
@@ -47,23 +51,21 @@ public struct AnyPattern: Pattern {
4751
extension AnyPattern: ExpressibleByStringInterpolation {
4852
public struct StringInterpolation: StringInterpolationProtocol {
4953
@usableFromInline
50-
var patterns = [Pattern]()
54+
var pattern = AnyPattern("")
5155

5256
@inlinable
53-
public init(literalCapacity: Int, interpolationCount: Int) {
54-
patterns.reserveCapacity(literalCapacity + interpolationCount)
55-
}
57+
public init(literalCapacity: Int, interpolationCount: Int) {}
5658

5759
@inlinable
5860
public mutating func appendLiteral(_ literal: String) {
5961
if !literal.isEmpty {
60-
patterns.append(Literal(literal))
62+
pattern = AnyPattern(pattern Literal(literal))
6163
}
6264
}
6365

6466
@inlinable
65-
public mutating func appendInterpolation(_ newpatterns: Pattern...) {
66-
patterns.append(contentsOf: newpatterns)
67+
public mutating func appendInterpolation<P: Pattern>(_ newpattern: P) {
68+
pattern = AnyPattern(pattern newpattern)
6769
}
6870
}
6971

@@ -74,19 +76,6 @@ extension AnyPattern: ExpressibleByStringInterpolation {
7476

7577
@inlinable
7678
public init(stringInterpolation: StringInterpolation) {
77-
var patterns = stringInterpolation.patterns[...]
78-
guard let first = patterns.popFirst() else {
79-
self.init(Literal(""))
80-
return
81-
}
82-
guard let second = patterns.popFirst() else {
83-
self.init(first)
84-
return
85-
}
86-
var result = AnyPattern(first) AnyPattern(second)
87-
while let next = patterns.popFirst() {
88-
result = AnyPattern(result) AnyPattern(next)
89-
}
90-
self.init(result)
79+
self.init(stringInterpolation.pattern)
9180
}
9281
}

Sources/Patterns/Pattern And Instruction.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77

88
/// Something that can create Instructions for the Parser.
9-
public protocol Pattern: CustomStringConvertible {
9+
public protocol Pattern: CustomStringConvertible, Equatable {
1010
typealias Input = String
1111
typealias ParsedRange = Range<Input.Index>
1212
typealias Instructions = ContiguousArray<Instruction<Input>>

Tests/PatternsTests/ConcatenationTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class ConcatenationTests: XCTestCase {
149149
lazy var rangeAndProperty: Parser<String> = {
150150
let hexNumber = Capture(name: "codePoint", hexDigit+)
151151
let hexRange = AnyPattern("\(hexNumber)..\(hexNumber)") / hexNumber
152-
return try! Parser(search: AnyPattern("\n\(hexRange, Skip()); \(Capture(name: "property", Skip())) "))
152+
return try! Parser(search: AnyPattern("\n\(hexRange Skip()); \(Capture(name: "property", Skip())) "))
153153
}()
154154

155155
func testStringInterpolation() throws {

0 commit comments

Comments
 (0)