3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-06 17:44:08 +00:00

add treesitter functionality

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
Nikolaj Bjorner 2025-02-13 09:56:49 -08:00
parent bedc95c4c7
commit 45f3ea3bf4

View file

@ -3,12 +3,37 @@ script({
files: "src/muz/spacer/spacer_qe_project.cpp" files: "src/muz/spacer/spacer_qe_project.cpp"
}) })
// return function names and source code of the functions
function get_functions(captures : QueryCapture[], code : string) {
return captures.map(({ name, node }) => ({
code : node.text
}))
}
const inputFile = env.files[0]; const inputFile = env.files[0];
const { captures } = await parsers.code(inputFile);
// pretty-print tree sitter parse tree of captures:
console.log(JSON.stringify(captures, null,2)) const { captures: functions } = await parsers.code(
inputFile,
`(function_definition) @function`
);
let funs = get_functions(functions, inputFile.content);
for (const fun of funs) {
// todo put files in a different directory
let name = fun.code.split('(')[0].trim();
name = name
.replace(/::/g, '_')
.replace(/ /g, '_');
let outputFile = path.basename(inputFile.filename)
.replace(/\.cpp$/, `.${name}.cpp`)
.replace(/\.h$/, `.${name}.h`);
outputFile = "slice_" + outputFile;
outputFile = path.join("code_slices", outputFile);
await workspace.writeText(outputFile, `//Extracted ${name} in ${inputFile.filename}\n${fun.code}\n\n`);
}