Created
March 3, 2018 23:03
-
-
Save Palleas/d232aa632c76acf78cb11539f1f322cc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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