import Cocoa import Darwin import Doubt extension TSInput { init?(path: String) { let file = fopen(path, "r") guard file != nil else { return nil } self.init( payload: file, read_fn: { (payload: UnsafeMutablePointer, bytesRead: UnsafeMutablePointer) -> UnsafePointer in errno = 0 var string: UnsafeMutablePointer = nil var capacity = 0 var length = getline(&string, &capacity, UnsafeMutablePointer(payload)) if length < 0 { if errno == 0 { length = 0 } else { return nil } } bytesRead.memory = length return UnsafePointer(string) }, seek_fn: { (payload: UnsafeMutablePointer, position: TSLength) -> Int32 in fseek(UnsafeMutablePointer(payload), position.bytes, SEEK_CUR) == 0 ? 1 : 0 }) } } func termWithInput(input: TSInput) -> Cofree>? { let document = ts_document_make() defer { ts_document_free(document) } ts_document_set_language(document, ts_language_javascript()) ts_document_set_input(document, input) ts_document_parse(document) let root = ts_document_root_node(document) 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..