serenadeai / java-tree-sitter

Java bindings for tree-sitter
MIT License
126 stars 34 forks source link

parser.parseString - returns null #27

Open hudara opened 1 year ago

hudara commented 1 year ago

I am trying to create a Java parser which parse Javascript files. Here is what I have done:

  1. Build libjava-tree-sitter.dylib file
  2. Created a simple java command line application
  3. Tried to execute the following code:

        try (Parser parser = new Parser()) {
            parser.setLanguage(Languages.javascript());
            try (Tree tree = parser.parseString("function foo(){return 8;}")) {
                Node root = tree.getRootNode();
                Node function = root.getChild(0);
            }
            catch (Exception e){
                System.out.println("Error: " + e.getMessage());
                e.printStackTrace();
            }
        }
    
    }

    The problem is that the returned "tree" object contains pointer "0" (zero) - I guess this is equivalent to "null" Executing the next line (tree.getRootNode) throws an exception:

My questions are:

  1. Any idea what can go wrong?
  2. How can I debug the code in the parser level?
# C  [libjava-tree-sitter.dylib+0x292d4]  ts_tree_root_node+0x0
#
# No core dump will be written. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
Disconnected from the target VM, address: '127.0.0.1:57533', transport: 'socket'
# An error report file with more information is saved as:
# ********/tree-sitter/test-tree-sitter/hs_err_pid41900.log
#
# If you would like to submit a bug report, please visit:
#   https://github.com/Homebrew/homebrew-core/issues
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

Process finished with exit code 134 (interrupted by signal 6: SIGABRT)

OS: MacOS 13.3.1

sogaiu commented 1 year ago

If you're using an Apple Silicon mac and/or the grammar in question has an external parser that is written in C (not C++; check the files in the src directory of the grammar's repository) [1], you might find this PR worth trying.


[1] More about that here.

rondales commented 1 year ago

I am trying to create a Java parser which parse Javascript files. Here is what I have done:

  1. Build libjava-tree-sitter.dylib file
  2. Created a simple java command line application
  3. Tried to execute the following code:
        try (Parser parser = new Parser()) {
            parser.setLanguage(Languages.javascript());
            try (Tree tree = parser.parseString("function foo(){return 8;}")) {
                Node root = tree.getRootNode();
                Node function = root.getChild(0);
            }
            catch (Exception e){
                System.out.println("Error: " + e.getMessage());
                e.printStackTrace();
            }
        }

    }

The problem is that the returned "tree" object contains pointer "0" (zero) - I guess this is equivalent to "null" Executing the next line (tree.getRootNode) throws an exception:

My questions are:

  1. Any idea what can go wrong?
  2. How can I debug the code in the parser level?
# C  [libjava-tree-sitter.dylib+0x292d4]  ts_tree_root_node+0x0
#
# No core dump will be written. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
Disconnected from the target VM, address: '127.0.0.1:57533', transport: 'socket'
# An error report file with more information is saved as:
# ********/tree-sitter/test-tree-sitter/hs_err_pid41900.log
#
# If you would like to submit a bug report, please visit:
#   https://github.com/Homebrew/homebrew-core/issues
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

Process finished with exit code 134 (interrupted by signal 6: SIGABRT)

OS: MacOS 13.3.1