GumTreeDiff / tree-sitter-parser

A tree generated based upon the amazing tree-sitter library.
GNU Affero General Public License v3.0
5 stars 6 forks source link

How to parse C files through Java API? #2

Closed SherloqueChang closed 1 year ago

SherloqueChang commented 2 years ago

I've installed tree-sitter-parser, but how can I get C code edit actions by invoking Java API in a Java project? The original implementaion is using cgum, but my OS is windows and I couldn't compile it. Here is my code:

    Run.initGenerators();

    Tree src = null; // retrieves and applies the default parser for the file
    try {
        src = TreeGenerators.getInstance().getTree(srcFile).getRoot();
        Tree dst = TreeGenerators.getInstance().getTree(dstFile).getRoot(); // retrieves and applies the default parser for the file
        Matcher defaultMatcher = Matchers.getInstance().getMatcher(); // retrieves the default matcher
        MappingStore mappings = defaultMatcher.match(src, dst); // computes the mappings between the trees
        EditScriptGenerator editScriptGenerator = new SimplifiedChawatheScriptGenerator(); // instantiates the simplified Chawathe script generator
        EditScript actions = editScriptGenerator.computeActions(mappings); // computes the edit script
        List<Action> actionsList = actions.asList();
        for(Action action: actionsList) {
            System.out.println(action);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }`
jrfaller commented 2 years ago

Hi!

You have to force the tree sitter backend like this:

Tree src = new CTreeSitterTreeGenerator().generateFrom().file(srcFile).getRoot();
Tree dst = new CTreeSitterTreeGenerator().generateFrom().file(dstFile).getRoot();

If everything is installed correctly and you have a very recent clone of GumTree, it should work :-)

Cheers!

SherloqueChang commented 2 years ago

Ahh...another question here: why should I have a clone of GumTree?

I'm using Maven dependency like: `

com.github.gumtreediff client 3.0.0

`

In this case, can I just add dependency in my pom.xml file? If so, what should be?

jrfaller commented 2 years ago

Sorry, but the tree sitter integration has not yet been released to Maven!

SherloqueChang commented 2 years ago

Ok, I understand now. Thanks a lot!