Javaparser: Can't parse a specific piece of code

Created on 3 Aug 2019  路  11Comments  路  Source: javaparser/javaparser

I was intending to parse this very short Java snippet:

package com.ichi2.anki;  

// Declare SharedDeckDownload so AIDL can find it and knows that it implements 

// the parcelable protocol. 

parcelable SharedDeckDownload;

and Javaparser gives me this error:

Exception in thread "main" com.github.javaparser.ParseProblemException: (line 1,col 23) Parse error. Found  "parcelable" <IDENTIFIER>, expected one of  ";" "@" "\u001a" "abstract" "class" "default" "enum" "final" "import" "interface" "module" "native" "open" "private" "protected" "public" "static" "strictfp" "synchronized" "transient" "transitive" "volatile" <EOF>

Any idea why might that be?
Thanks in advance!

Question (JP usage)

All 11 comments

think you gotta put the field inside a class/interface/something b/c the code above aint valid java
try
````java
package com.ichi2.anki;

class F{
parcelable SharedDeckDownload;
}
````

JavaParser doesn't do snippets.

I am trying to parse source code regardless of the possible syntax / semantic errors inside it (in order to have a dataset of source codes with TRUE / FALSE binary labels), since I need its AST.

I guess there is no way to work around this, since if it's wrong code, Javaparser is not going to do it.

Thanks a lot.

@edefazio Do you know of anything in the order and power of Javaparser to do snippets and give out ASTs?

This is a tall order:
"parse source code regardless of the possible syntax / semantic errors"

Not sure how to get there... its not like there is a tool that can easily discern "code" from text.

Good luck

  • JavaParser can parser snippets as in "parse a statement, an expression, and a bunch more."
  • There is some error recovery for files with syntax errors, but it's pretty minimal and not of much use to most people. You always get an AST, but for syntax errors it often is empty.

Thanks a lot, @matozoid. Is there anyway where I can silence these syntax errors or change the mode of parsing?

You can give the error recovery a try by using the JavaParser class instead of the StaticJavaParser which I assume you're using. That returns a ParseResult instead of an exception that you can take a look at.

Renamed the issue because it was cluttering the issue list :-)

@matozoid I was using JavaParser/StaticJavaParser to parse a java file. The file contains a line which could not be parsed by the parser.

Thread.yield();

The error is
Exception in thread "main" com.github.javaparser.ParseProblemException: (line 6,col 9) Parse error. Found ".", expected one of "%=" "&=" "(" "*=" "++" "+=" "--" "-=" "/=" ";" "<<=" "=" ">>=" ">>>=" "^=" "|="

Any idea? Thanks in advance.

Hi @lhou-pcx - I think you're looking for the parseStatement method.

If that's not the problem, please open a new issue :-)

@matozoid Thanks for the quick reply!

I was using JavaParser to parse all the java files in a repository and make changes to some of them. I guess I will need to open an issue.

Was this page helpful?
0 / 5 - 0 ratings