2015-10-26 23:00:52 +03:00
|
|
|
import Cocoa
|
2015-10-27 18:04:00 +03:00
|
|
|
import Darwin
|
2015-10-26 23:00:52 +03:00
|
|
|
import Doubt
|
2015-10-27 18:04:39 +03:00
|
|
|
|
2015-10-27 18:04:00 +03:00
|
|
|
extension TSInput {
|
2015-10-28 01:23:26 +03:00
|
|
|
init?(path: String) {
|
2015-10-27 18:04:00 +03:00
|
|
|
let file = fopen(path, "r")
|
2015-10-28 01:23:26 +03:00
|
|
|
guard file != nil else { return nil }
|
2015-10-27 18:04:00 +03:00
|
|
|
self.init(
|
|
|
|
payload: file,
|
|
|
|
read_fn: { (payload: UnsafeMutablePointer<Void>, bytesRead: UnsafeMutablePointer<Int>) -> UnsafePointer<Int8> in
|
2015-10-27 18:45:42 +03:00
|
|
|
errno = 0
|
2015-10-27 18:27:17 +03:00
|
|
|
var string: UnsafeMutablePointer<Int8> = nil
|
2015-10-27 18:40:03 +03:00
|
|
|
var capacity = 0
|
2015-10-27 18:45:42 +03:00
|
|
|
var length = getline(&string, &capacity, UnsafeMutablePointer<FILE>(payload))
|
|
|
|
if length < 0 {
|
|
|
|
if errno == 0 { length = 0 }
|
|
|
|
else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bytesRead.memory = length
|
2015-10-27 18:27:17 +03:00
|
|
|
return UnsafePointer<Int8>(string)
|
2015-10-27 18:04:00 +03:00
|
|
|
},
|
|
|
|
seek_fn: { (payload: UnsafeMutablePointer<Void>, position: TSLength) -> Int32 in
|
2015-10-27 18:33:18 +03:00
|
|
|
fseek(UnsafeMutablePointer<FILE>(payload), position.bytes, SEEK_CUR) == 0 ? 1 : 0
|
2015-10-27 18:04:00 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2015-10-26 23:00:52 +03:00
|
|
|
|
2015-10-28 01:26:39 +03:00
|
|
|
func termWithInput(input: TSInput) -> Cofree<String, Range<Int>>? {
|
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-27 18:04:18 +03:00
|
|
|
ts_document_set_language(document, ts_language_javascript())
|
2015-10-28 01:26:39 +03:00
|
|
|
ts_document_set_input(document, input)
|
2015-10-27 18:04:18 +03:00
|
|
|
ts_document_parse(document)
|
|
|
|
let root = ts_document_root_node(document)
|
2015-10-28 01:07:00 +03:00
|
|
|
|
2015-10-28 01:26:39 +03:00
|
|
|
return Cofree
|
2015-10-28 01:10:17 +03:00
|
|
|
.ana { node in
|
2015-10-28 01:20:21 +03:00
|
|
|
let count = ts_node_named_child_count(node)
|
2015-10-28 01:10:17 +03:00
|
|
|
guard count > 0 else {
|
|
|
|
return String.fromCString(ts_node_name(node, document)).map(Syntax.Leaf)!
|
|
|
|
}
|
2015-10-28 01:20:21 +03:00
|
|
|
return .Indexed((0..<count).map { ts_node_named_child(node, $0) })
|
2015-10-28 01:10:17 +03:00
|
|
|
} (root)
|
2015-10-28 01:13:49 +03:00
|
|
|
.map {
|
|
|
|
let start = ts_node_pos($0).chars
|
|
|
|
return start..<(start + ts_node_size($0).chars)
|
|
|
|
}
|
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)
|
|
|
|
if let a = arguments[1].flatMap(TSInput.init).flatMap(termWithInput) {
|
|
|
|
print(a)
|
2015-10-27 18:04:18 +03:00
|
|
|
}
|