tree-sitter-java icon indicating copy to clipboard operation
tree-sitter-java copied to clipboard

`local_variable_declaration` and `ERROR` is generated when it encounters descriptors.

Open lixinye-nju opened this issue 4 years ago • 2 comments

Problem description

Consider the following method:

@CheckReturnValue @Nonnull static public <E>String arrayToString(@Nonnull E[] v){
  StringBuilder retval=new StringBuilder();
  boolean first=true;
  for (  E e : v) {
    if (!first) {
      retval.append(',');
    }
    first=false;
    retval.append('[');
    retval.append(e.toString());
    retval.append(']');
  }
  return new String(retval);
}

Expected parse tree output (optional)

Parse code with output method_definition

Actual parse tree output (optional)

  1. local_variable_declaration: @CheckReturnValue @Nonnull static public <E>String
  2. expression_statement : arrayToString(@Nonnull E[] v){\n StringBuilder retval=new StringBuilder();

lixinye-nju avatar Oct 09 '21 08:10 lixinye-nju

I think this just needs to be inside of a class declaration.

maxbrunsfeld avatar Oct 09 '21 17:10 maxbrunsfeld

Thx! Putting it inside of a class declaration works. But it is quite confusing that why parser could handle statement StringBuilder retval = new StringBuilder() and whole class declaration but fails to parse a single method declaration.

lixinye-nju avatar Oct 10 '21 10:10 lixinye-nju

Top level items are statement, of which expression_statement is a part of, but method_declaration is inside a class/interface body

amaanq avatar Jul 13 '23 07:07 amaanq