2015-10-26 23:00:52 +03:00
|
|
|
import Cocoa
|
|
|
|
import Doubt
|
2015-10-28 01:38:25 +03:00
|
|
|
import Prelude
|
2015-10-27 18:04:39 +03:00
|
|
|
|
2015-10-28 01:29:27 +03:00
|
|
|
func readFile(path: String) -> String? {
|
|
|
|
guard let data = try? NSString(contentsOfFile: path, encoding: NSUTF8StringEncoding) else { return nil }
|
|
|
|
return data as String?
|
|
|
|
}
|
|
|
|
|
2015-10-28 01:37:34 +03:00
|
|
|
typealias Term = Cofree<String, Range<Int>>
|
|
|
|
|
2015-10-28 01:37:59 +03:00
|
|
|
func termWithInput(string: String) -> Term? {
|
2015-10-27 18:04:18 +03:00
|
|
|
let document = ts_document_make()
|
2015-10-28 01:26:39 +03:00
|
|
|
defer { ts_document_free(document) }
|
2015-10-28 01:29:44 +03:00
|
|
|
return string.withCString {
|
|
|
|
ts_document_set_language(document, ts_language_javascript())
|
|
|
|
ts_document_set_input_string(document, $0)
|
|
|
|
ts_document_parse(document)
|
|
|
|
let root = ts_document_root_node(document)
|
2015-10-28 01:07:00 +03:00
|
|
|
|
2015-10-28 01:29:44 +03:00
|
|
|
return Cofree
|
|
|
|
.ana { node in
|
|
|
|
let count = ts_node_named_child_count(node)
|
|
|
|
guard count > 0 else {
|
|
|
|
return String.fromCString(ts_node_name(node, document)).map(Syntax.Leaf)!
|
|
|
|
}
|
|
|
|
return .Indexed((0..<count).map { ts_node_named_child(node, $0) })
|
|
|
|
} (root)
|
|
|
|
.map {
|
|
|
|
let start = ts_node_pos($0).chars
|
|
|
|
return start..<(start + ts_node_size($0).chars)
|
2015-10-28 01:10:17 +03:00
|
|
|
}
|
2015-10-28 01:29:44 +03:00
|
|
|
}
|
2015-10-28 01:26:39 +03:00
|
|
|
}
|
2015-10-28 01:10:17 +03:00
|
|
|
|
2015-10-28 01:26:39 +03:00
|
|
|
let arguments = BoundsCheckedArray(array: Process.arguments)
|
2015-10-28 01:38:25 +03:00
|
|
|
if let aString = arguments[1].flatMap(readFile), bString = arguments[2].flatMap(readFile), c = arguments[3] {
|
|
|
|
if let a = termWithInput(aString), b = termWithInput(bString) {
|
|
|
|
let diff = Interpreter<Term>(equal: Term.equals(annotation: const(true), leaf: ==), comparable: const(true), cost: Free.sum(Patch.difference)).run(a, b)
|
2015-10-28 01:38:31 +03:00
|
|
|
let range: Range<Int> -> Doubt.JSON = {
|
|
|
|
let start = $0.startIndex
|
|
|
|
let end = $0.endIndex
|
|
|
|
return [
|
|
|
|
.Number(Double(start)),
|
|
|
|
.Number(Double(end - start)),
|
|
|
|
]
|
|
|
|
}
|
|
|
|
let JSON: Doubt.JSON = [
|
|
|
|
"before": .String(aString),
|
|
|
|
"after": .String(bString),
|
|
|
|
"diff": diff.JSON(pure: { $0.JSON { $0.JSON(annotation: range, leaf: Doubt.JSON.String) } }, leaf: Doubt.JSON.String, annotation: {
|
|
|
|
[
|
|
|
|
"before": range($0),
|
|
|
|
"after": range($1),
|
|
|
|
]
|
|
|
|
}),
|
|
|
|
]
|
2015-10-28 01:39:11 +03:00
|
|
|
let data = JSON.serialize()
|
|
|
|
try data.writeToFile(c, options: .DataWritingAtomic)
|
2015-10-28 01:38:25 +03:00
|
|
|
}
|
2015-10-27 18:04:18 +03:00
|
|
|
}
|