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

CSharp and Java producing different error messages for expected tokens #3700

Open
kaby76 opened this issue May 7, 2022 · 5 comments
Open

Comments

@kaby76
Copy link
Contributor

kaby76 commented May 7, 2022

For:

  • Grammar is grammars-v4/csharp.
  • Antlr4.10.1 Java and CSharp targets.
  • Uses the standard console error listener.
  • You can use trgen to generate for either CSharp or Java target on Windows or Linux.

Output:

Expected output:

  • There should not be any diff in the error message between targets.
@kaby76
Copy link
Contributor Author

kaby76 commented Mar 8, 2023

This needs to be solved since the build compares outputs--and soon the .tree files--across targets.

@ericvergnaud
Copy link
Contributor

This could be solved by converting the set to a naturally ordered list.

@kaby76
Copy link
Contributor Author

kaby76 commented Mar 8, 2023

This could be solved by converting the set to a naturally ordered list.

I think there's more to it because the LA sets are not the same at all between CSharp and Java targets.

# remastered for the CSharp target, testing the Java target.
diff --git a/csharp/examples/issue-2612.txt.errors b/csharp/examples/issue-2612.txt.errors
index 3c03c4a0..6832b6ca 100644
--- a/csharp/examples/issue-2612.txt.errors
+++ b/csharp/examples/issue-2612.txt.errors
@@ -1 +1 @@
-line 4:17 mismatched input ':' expecting ';'
+line 4:17 mismatched input ':' expecting {'add', 'alias', '__arglist', 'ascending', 'async', 'await', 'by', 'descending', 'dynamic', 'equals', 'from', 'get', 'group', 'into', 'join', 'let', 'nameof', 'on', 'orderby', 'partial', 'remove', 'select', 'set', 'unmanaged', 'var', 'when', 'where', 'yield', IDENTIFIER, '[', '*', '?'}
Test failed.

@ericvergnaud
Copy link
Contributor

That bug is strange because many tests in the test suite check for errors...
Can you add it to the 'ParserErrors' test suite ? Then we can check it against all targets ?

@kaby76
Copy link
Contributor Author

kaby76 commented May 9, 2024

This is an old bug, but I am working out why there are differences because someone is trying to use grammars-v4/csharp (CSharp target) in StackOverflow (https://stackoverflow.com/questions/78424669/linking-visitmember-access-and-visitifstatement-methods-of-antlr4#comment138303496_78424669). (I created the desc.xml to describe what ports work for a grammar, and to try to prevent people from using bad ports. CSharp is not listed, but people ignore that anyway.)

The CSharp code and Java code error recovery are not similar.

// even that didn't work; must throw the exception
throw new InputMismatchException(recognizer);

// even that didn't work; must throw the exception
InputMismatchException e;
if (nextTokensContext == null) {
e = new InputMismatchException(recognizer);
} else {
e = new InputMismatchException(recognizer, nextTokensState, nextTokensContext);
}
throw e;

"nextTokensContext" is not null, so Java does something completely different from CSharp.

In fact, the code is not similar across targets.

// Even that didn't work; must throw the exception.
throw InputMismatchException(recognizer);

// even that didn't work; must throw the exception
InputMismatchException e;
if (nextTokensContext == null) {
e = InputMismatchException(recognizer);
} else {
e = InputMismatchException(
recognizer,
nextTokensState!,
nextTokensContext,
);
}
throw e;

// even that didn't work must panic the exception
recognizer.SetError(NewInputMisMatchException(recognizer))

// even that didn't work; must throw the exception
throw new InputMismatchException(recognizer);

# even that didn't work; must throw the exception
raise InputMismatchException(recognizer)

It seems the change in Java was for #1922 0803c74 but it wasn't applied across targets and they were available. https://github.com/antlr/antlr4/tree/0803c74eb255fe5e6fe5a6f11d879198d305e715/runtime . Sloppy coding.

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

No branches or pull requests

2 participants