Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaParser finds "." in place of first element after a yeild statement. #4400

Open
Decencies opened this issue Apr 29, 2024 · 12 comments
Open

Comments

@Decencies
Copy link

Decencies commented Apr 29, 2024

parse error: (line 98,col 31) Parse error. Found ".", expected one of "," ";" "=" "@" "["

Line 98:
yield rIHCHOIOCIOICHHRHHIOHIHIC.OOICCOCOCRHCOCIORIHHIHHHH("user", "publicServerGame", var4_6, var2_3);

Column 31 (whitespace ommited):
yield rIHCHOIOCIOICHHRHHIOHIHIC.OOICCOCOCRHCOCIORIHHIHHHH("user", "publicServerGame", var4_6, var2_3);
^

Evidently there is no "." written at column 31

@Decencies
Copy link
Author

Code formatting doesn't do it justice, here's a picture with an I-beam indicating the exact place:
image

@jlerbsc jlerbsc changed the title [BUG] JavaParser finds "." in place of first element after a yeild statement. JavaParser finds "." in place of first element after a yeild statement. Apr 29, 2024
@jlerbsc
Copy link
Collaborator

jlerbsc commented Apr 29, 2024

Can you provide us with a unit test?

@Decencies
Copy link
Author

Can you provide us with a unit test?

Would the code being parsed suffice? I don't really know how to setup a unit test for this

@Decencies
Copy link
Author

Can you provide us with a unit test?

Would the code being parsed suffice? I don't really know how to setup a unit test for this

https://pastebin.com/TjCbBvWS

@Decencies
Copy link
Author

The program is using the latest version of JavaParser available on maven-central (3.25.10)

@jlerbsc
Copy link
Collaborator

jlerbsc commented Apr 29, 2024

Thank you. Can you show us how you configure the parser.

@Decencies
Copy link
Author

@jlerbsc
Copy link
Collaborator

jlerbsc commented Apr 29, 2024

I get no exceptions when I parse your class with this code snippet.

JavaParser javaParser = new JavaParser();
javaParser.getParserConfiguration().setLanguageLevel(ParserConfiguration.LanguageLevel.RAW);
ParseResult<CompilationUnit> result = javaParser.parse(code);

@Decencies
Copy link
Author

Very weird, it seems to work standalone for me too....

@Decencies
Copy link
Author

Very weird, it seems to work standalone for me too....

nevermind, result.isSuccessful() is not true

@Decencies
Copy link
Author

Decencies commented Apr 29, 2024

Very weird, it seems to work standalone for me too....

nevermind, result.isSuccessful() is not true

JavaParser javaParser = new JavaParser();
javaParser.getParserConfiguration().setLanguageLevel(ParserConfiguration.LanguageLevel.RAW);
ParseResult<CompilationUnit> result = javaParser.parse(code);

if (!result.isSuccessful()) {
	for (Problem problem : result.getProblems()) {
		System.out.println(problem.getMessage());
	}
}
Parse error. Found ".", expected one of  "," ";" "=" "@" "["
Parse error. Found ".", expected one of  "," ";" "=" "@" "["
Parse error. Found ".", expected one of  "," ";" "=" "@" "["
Parse error. Found ".", expected one of  "," ";" "=" "@" "["

@jlerbsc
Copy link
Collaborator

jlerbsc commented Apr 30, 2024

Your test case can be reduced to the following.

There seem to be 2 problems:
The label parsing which does not tolerate the '.' character;
The parsing of the expression which does not tolerate a negative integer;

@Test
void issue4400() {
	String code =
			"enum LEVEL {\r\n"
			+ "		LOW, MEDIUM, HIGH;\r\n"
			+ "	}\r\n"
			+ "	\r\n"
			+ "	public class Foo {\r\n"
			+ "		public static int match(LEVEL level) {\n"
			+ "	          return switch (level) {\n"
			+ "			default -> throw new UnsupportedOperationException();\n"
			+ "			case LEVEL.LOW -> -1;\n"
			+ "		  };\n"
			+ "		}\n"
			+ "	}";
	JavaParser javaParser = new JavaParser();
	javaParser.getParserConfiguration().setLanguageLevel(ParserConfiguration.LanguageLevel.RAW);
	ParseResult<CompilationUnit> result = javaParser.parse(code);

	if (!result.isSuccessful()) {
		for (Problem problem : result.getProblems()) {
			System.out.println(problem.getMessage());
		}
	}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants