javalang icon indicating copy to clipboard operation
javalang copied to clipboard

Why does Javalang gives error when calling parse.member_declaration method

Open Thirunayan22 opened this issue 4 years ago • 2 comments

When I call the parser.parse_member_declaration on a tokenized source code containing import statements Javalang is giving the below error.

Java code

import java.util.*;class Test { public static void main(String[] args){ int x=3; if(x>2){System.out.println('Hola');} } }

image

What is the cause of this error?

Thirunayan22 avatar Jun 28 '21 18:06 Thirunayan22

@c2nes

Thirunayan22 avatar Jun 28 '21 18:06 Thirunayan22

Try parse_compilation_unit() instead.

Imports statements are only allowed in a "compilation unit" which is what a complete .java file corresponds to. A "member declaration" is a declaration within a class (e.g. a field, method, or nested type). Since import statements are not allowed within class/interface/enum declarations you are receiving a syntax error.

c2nes avatar Jun 28 '21 20:06 c2nes