Refactoring

This commit is contained in:
Sven Weidauer 2020-12-30 12:50:26 +01:00
parent cf00c1cd30
commit 7d277a0e43
10 changed files with 384 additions and 312 deletions

View file

@ -0,0 +1,60 @@
struct Color: CustomStringConvertible, Equatable {
let r, g, b, a: UInt8
init(r: UInt8, g: UInt8, b: UInt8, a: UInt8 = 0xFF) {
self.r = r
self.g = g
self.b = b
self.a = a
}
init?(_ array: [UInt8]) {
guard array.count >= 3 else { return nil }
r = array[0]
g = array[1]
b = array[2]
a = array.count >= 4 ? array[3] : 0xFF
}
var description: String {
return a != 0xFF ? String(format: "#%02X%02X%02X%02X", r, g, b, a): String(format: "#%02X%02X%02X", r, g, b)
}
}
enum ColorDef {
case reference(String)
case color(Color)
}
extension Dictionary where Key == String, Value == ColorDef {
func resolve(_ name: String, visited: Set<String> = []) throws -> Color {
var visited = visited
guard visited.insert(name).inserted else {
throw Errors.cyclicReference(name)
}
switch self[name] {
case nil:
throw Errors.missingReference(name)
case .color(let color):
return color
case .reference(let referenced):
return try resolve(referenced, visited: visited)
}
}
func sorted() -> [Element] {
sorted(by: Self.compare)
}
static func compare(_ a: (String, ColorDef), _ b: (String, ColorDef)) -> Bool {
switch (a, b) {
case ((_, .color), (_, .reference)): return true
case ((_, .reference), (_, .color)): return false
case let ((left, _), (right, _)): return left.localizedStandardCompare(right) == .orderedAscending
}
}
}

View file

@ -0,0 +1,125 @@
import Foundation
private extension CharacterSet {
static let hex = CharacterSet(charactersIn: "0123456789abcdef")
static let name = alphanumerics.union(CharacterSet.init(charactersIn: "_/"))
}
private extension Collection {
func chunks(size: Int) -> UnfoldSequence<Self.SubSequence, Self.Index> {
sequence(state: startIndex) { state -> SubSequence? in
guard state != endIndex else { return nil }
let next = index(state, offsetBy: size, limitedBy: endIndex) ?? endIndex
defer { state = next }
return self[state..<next]
}
}
}
extension Scanner {
func string(_ s: String) -> Bool {
return scanString(s) != nil
}
func color() -> Color? {
if string("#"), let digits = scanCharacters(from: .hex) {
switch digits.count {
case 3, 4: //rgb(a)
let digits = digits.chunks(size: 1)
.compactMap { UInt8($0, radix: 16) }
.map { $0 << 4 | $0 }
return Color(digits)
case 6, 8: //rrggbb(aa)
let digits = digits.chunks(size: 2).compactMap { UInt8($0, radix: 16) }
return Color(digits)
default: return nil
}
}
if string("rgba"), string("("), let components = commaSeparated(), components.count == 4, string(")") {
return Color(components)
}
if string("rgb"), string("("), let components = commaSeparated(), components.count == 3, string(")") {
return Color(components)
}
return nil
}
func colorReference() -> String? {
guard string("@") else { return nil }
return name()
}
func name() -> String? {
guard let name = scanCharacters(from: .name), !name.isEmpty else {
return nil
}
return name
}
func colorDef() -> ColorDef? {
if let color = color() {
return .color(color)
}
if let ref = colorReference() {
return .reference(ref)
}
return nil
}
func colorLine() -> (String, ColorDef)? {
guard let name = self.name(),
let def = colorDef(),
endOfLine() else {
return nil
}
return (name, def)
}
func endOfLine() -> Bool {
guard isAtEnd || string("\n") else {
return false
}
_ = scanCharacters(from: .whitespacesAndNewlines)
return true
}
func colorList() throws -> [String: ColorDef] {
var result: [String: ColorDef] = [:]
while !isAtEnd {
guard let (name, def) = colorLine() else {
throw Errors.syntaxError
}
guard !result.keys.contains(name) else {
throw Errors.duplicateColor(name)
}
result[name] = def
}
return result
}
func commaSeparated() -> [UInt8]? {
var result: [UInt8] = []
repeat {
guard let int = scanInt(), let component = UInt8(exactly: int) else {
return nil
}
result.append(component)
} while string(",")
return result
}
}