I am trying to write a silly tool using Rust that transforms C code like this
int sys_close(int fd) {
file_t *f = proc_getfile(proc_curr(), fd);
if (!f){
return -1;
}else{
fclose(f);
proc_curr()->files[fd] = NULL;
return 0;
}
}
to this
int sys_close(int fd) {
return ({
file_t *f = proc_getfile(proc_curr(), fd);
!f ? -1 : (fclose(f), proc_curr()->files[fd] = NULL, 0);
});
}
To make this transformation, I want to first generate an AST from C source code, then reorder elements in the AST, and finally print transformed code by visiting the tree.
I do not want to write a parser myself, so I am looking for a Rust crate to generate the AST for me. Becasue this tool only cares about tranformation of code in a small range of code, so it’s better that the crate does not perform semantic checks and only focus on generating an AST.
I tried lang-c
, but it seemed to performed some kind of type check, because undeclared types such as uint32_t
is regarded as a SyntaxError
. Also lang-c
uses either clang
or gcc
as its frontend so include file will be checked. I want a library that does not perform any kind of check or try to read include files, only to parse input source code and generates an AST. I want to know whether there is a crate matches my need.
Thanks in advance.
kirisame is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.