Closed openrefactorymunawar closed 6 months ago
I used the following code:
private TSTree parseWithTreeSitter(String fileName, IModelFileElement file) throws IOException {
TSParser parser = new TSParser();
TSLanguage c = new TreeSitterC();
parser.setLanguage(c);
String code = new String(Files.readAllBytes(Paths.get(fileName)));
byte[] buffer = new byte[code.length() + 50];
TSReader reader = (buf, offset, position) -> {
if(offset >= code.length()){
return 0;
}
ByteBuffer charBuffer = ByteBuffer.wrap(buf);
charBuffer.put(code.getBytes());
return code.length();
};
TSTree tree = parser.parse(buffer, null, reader, TSInputEncoding.TSInputEncodingUTF8);
return tree;
}
And I was calling this method iteratively on all the .c files in the project.
Hi, please use byte[] instead of wrapping it in String, and make sure the return value of TSReader#read won't overflow the buffer.
If you prefer to read all file content to memory, please consinder Parser#parseStringEncoding
.
Hope this helps.
I have been trying to use the Java binding to parse C code of a large application (https://github.com/zephyrproject-rtos/zephyr). It has 23,000+ C files. I was iteratively parsing each of the files of the application.
The application is abruptly halting with the following information.
I had two different runs and it stops at different points of execution.