c2nes/javalang

Why does Javalang gives error when calling parse.member_declaration method

Opened this issue · 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?

c2nes commented

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.