mirror of
https://github.com/github/semantic.git
synced 2024-11-25 11:04:00 +03:00
Merge pull request #123 from github/output-source-range-annotations-to-json
Output source range annotations to json
This commit is contained in:
commit
910f938431
@ -124,5 +124,21 @@ public protocol CofreeType {
|
||||
|
||||
extension Cofree: CofreeType {}
|
||||
|
||||
extension CofreeType where Self.Annotation == Range<String.Index> {
|
||||
public func JSON(source: String) -> Doubt.JSON {
|
||||
return unwrap.JSON(
|
||||
ifLeaf: { _ in .String(source[extract]) },
|
||||
ifRecur: {
|
||||
[
|
||||
"range": [
|
||||
"offset": .Number(Double(source.startIndex.distanceTo($0.extract.startIndex))),
|
||||
"length": .Number(Double($0.extract.count)),
|
||||
],
|
||||
"unwrap": $0.JSON(source)
|
||||
]
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
import Prelude
|
||||
|
@ -156,3 +156,13 @@ extension Patch: PatchType {
|
||||
self = .Insert(after)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension PatchType where Element: CofreeType, Element.Annotation == Range<String.Index> {
|
||||
public func JSON(a a: String, b: String) -> Doubt.JSON {
|
||||
return [
|
||||
"before": state.before.map { $0.JSON(a) } ?? nil,
|
||||
"after": state.after.map { $0.JSON(b) } ?? nil,
|
||||
]
|
||||
}
|
||||
}
|
||||
|
@ -74,7 +74,7 @@ public func == <F: Equatable, A: Equatable> (left: Syntax<F, A>, right: Syntax<F
|
||||
// MARK: - JSON
|
||||
|
||||
extension Syntax {
|
||||
public func JSON(ifLeaf ifLeaf: A -> Doubt.JSON, ifRecur: Recur -> Doubt.JSON) -> Doubt.JSON {
|
||||
public func JSON(@noescape ifLeaf ifLeaf: A -> Doubt.JSON, @noescape ifRecur: Recur -> Doubt.JSON) -> Doubt.JSON {
|
||||
switch self {
|
||||
case let .Leaf(a):
|
||||
return ifLeaf(a)
|
||||
|
@ -4,6 +4,13 @@ import Either
|
||||
import Prelude
|
||||
import Madness
|
||||
|
||||
func benchmark<T>(label: String? = nil, _ f: () -> T) -> T {
|
||||
let start = NSDate.timeIntervalSinceReferenceDate()
|
||||
let result = f()
|
||||
let end = NSDate.timeIntervalSinceReferenceDate()
|
||||
print((label.map { "\($0): " } ?? "") + "\(end - start)s")
|
||||
return result
|
||||
}
|
||||
|
||||
let arguments = BoundsCheckedArray(array: Process.arguments)
|
||||
|
||||
@ -20,16 +27,48 @@ print(parse(json, input: dictWithMembers))
|
||||
let dictWithArray = "{\"hello\": [\"world\"],\"sup\": [\"cat\", \"dog\", \"keith\"] }"
|
||||
print(parse(json, input: dictWithArray))
|
||||
|
||||
func diffAndSerialize(a aString: String, b bString: String) -> String? {
|
||||
let aParsed = benchmark("parsing a") { curry(parse)(json)(aString) }
|
||||
guard let a = aParsed.right else {
|
||||
_ = aParsed.left.map { print("error parsing a:", $0) }
|
||||
return nil
|
||||
}
|
||||
let bParsed = benchmark("parsing b") { curry(parse)(json)(bString) }
|
||||
guard let b = bParsed.right else {
|
||||
_ = bParsed.left.map { print("error parsing b:", $0) }
|
||||
return nil
|
||||
}
|
||||
|
||||
let diff = benchmark("diffing a & b") {
|
||||
Interpreter<CofreeJSON>(equal: CofreeJSON.equals(annotation: const(true), leaf: ==), comparable: const(true), cost: Free.sum(Patch.difference)).run(a, b)
|
||||
}
|
||||
|
||||
let range: Range<String.Index> -> Doubt.JSON = {
|
||||
let start = Int(String($0.startIndex))!
|
||||
let end = Int(String($0.endIndex))!
|
||||
return [
|
||||
.Number(Double(start)),
|
||||
.Number(Double(end - start)),
|
||||
]
|
||||
}
|
||||
let JSON = benchmark("converting diff to JSON") {
|
||||
diff.JSON(ifPure: { $0.JSON { $0.JSON(annotation: range, leaf: { $0.JSON }) } }, ifLeaf: { $0.JSON })
|
||||
}
|
||||
|
||||
let data = benchmark("serializing JSON to NSData") {
|
||||
JSON.serialize()
|
||||
}
|
||||
|
||||
return benchmark("decoding data into string") {
|
||||
NSString(data: data, encoding: NSUTF8StringEncoding) as String?
|
||||
}
|
||||
}
|
||||
|
||||
let readFile = { (path: String) -> String? in
|
||||
guard let data = try? NSString(contentsOfFile: path, encoding: NSUTF8StringEncoding) else { return nil }
|
||||
return data as String?
|
||||
}
|
||||
|
||||
if let a = arguments[1].flatMap(readFile).flatMap({
|
||||
curry(parse)(json)($0).right
|
||||
}), b = arguments[2].flatMap(readFile).flatMap({
|
||||
curry(parse)(json)($0).right
|
||||
}) {
|
||||
let diff = Interpreter(comparable: const(true), cost: Diff.sum(Patch.difference)).run(a, b)
|
||||
if let a = arguments[1].flatMap(readFile), b = arguments[2].flatMap(readFile), diff = diffAndSerialize(a: a, b: b) {
|
||||
print(diff)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user