1
1
mirror of https://github.com/github/semantic.git synced 2024-12-21 13:51:44 +03:00
semantic/prototype/doubt-difftool/main.swift
2015-10-27 11:45:54 -04:00

46 lines
1.3 KiB
Swift

import Cocoa
import Darwin
import Doubt
extension TSInput {
init(path: String) {
let file = fopen(path, "r")
self.init(
payload: file,
read_fn: { (payload: UnsafeMutablePointer<Void>, bytesRead: UnsafeMutablePointer<Int>) -> UnsafePointer<Int8> in
errno = 0
var string: UnsafeMutablePointer<Int8> = nil
var capacity = 0
var length = getline(&string, &capacity, UnsafeMutablePointer<FILE>(payload))
if length < 0 {
if errno == 0 { length = 0 }
else {
return nil
}
}
bytesRead.memory = length
return UnsafePointer<Int8>(string)
},
seek_fn: { (payload: UnsafeMutablePointer<Void>, position: TSLength) -> Int32 in
fseek(UnsafeMutablePointer<FILE>(payload), position.bytes, SEEK_CUR) == 0 ? 1 : 0
})
}
}
let arguments = BoundsCheckedArray(array: Process.arguments)
if let a = arguments[1] {
guard NSFileManager.defaultManager().fileExistsAtPath(a) else {
print("\(a): No such file or directory")
exit(1)
}
let a = TSInput(path: a)
let document = ts_document_make()
ts_document_set_language(document, ts_language_javascript())
ts_document_set_input(document, a)
ts_document_parse(document)
let root = ts_document_root_node(document)
print(String.fromCString(ts_node_name(root, document)))
ts_document_free(document)
}