Skip to content

Instantly share code, notes, and snippets.

@Palleas
Created March 3, 2018 23:03
Show Gist options
  • Select an option

  • Save Palleas/d232aa632c76acf78cb11539f1f322cc to your computer and use it in GitHub Desktop.

Select an option

Save Palleas/d232aa632c76acf78cb11539f1f322cc to your computer and use it in GitHub Desktop.
import Foundation
import SourceKittenFramework
let source = """
// Diamond operator
infix operator <>
func <> <A: AnyObject>(f: @escaping (A) -> Void, g: @escaping (A) -> Void) -> (A) -> Void {
return { a in
f(a)
g(a)
}
}
infix operator |>
func |> <A, B>(x: A, f: (A) -> B) -> B {
return f(x)
}
"""
struct CoolOperator {
let name: String
let readableName: String
}
let knownOperators = [
CoolOperator(name: "<>", readableName: "Diamond"),
CoolOperator(name: "|>", readableName: "Pipe")
]
try (Structure(file: File(contents: source))
.dictionary["key.substructure"] as? [SourceKitRepresentable])?
.flatMap { $0 as? [String: SourceKitRepresentable]}
.filter { $0["key.kind"]?.isEqualTo("source.lang.swift.decl.function.free") ?? false }
.flatMap { ($0["key.name"] as? String)?.split(separator: "(").first }
.forEach { op in
if let found = knownOperators.first(where: { op == $0.name }) {
print("Found \(found.readableName) operator!")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment