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] Parsing source with syntax error causes ClassCastExceptin #2612

Closed
yososs opened this issue May 6, 2022 · 6 comments
Closed

[CSharp] Parsing source with syntax error causes ClassCastExceptin #2612

yososs opened this issue May 6, 2022 · 6 comments
Labels

Comments

@yososs
Copy link

yososs commented May 6, 2022

I found this to be a cast error when entering the wrong C++ syntax into CSharp's parser.
This does not appear to be the correct way to handle syntax errors.

Sources containing syntax error

class A{
  void a() {
    int[] nums = {1, 3, 5, 7};
    for (int num : nums){
        b(num);
    }
  }
}

Added the following cast check to avoid cast errors in CSharpParserBase.java.

public abstract class CSharpParserBase extends Parser
{
    protected CSharpParserBase(TokenStream input)
    {
	super(input);
    }

    protected boolean IsLocalVariableDeclaration()
    {
   // fix
    if (!(this._ctx instanceof CSharpParser.Local_variable_declarationContext)) {
    	return false;
    }

	CSharpParser.Local_variable_declarationContext local_var_decl = (CSharpParser.Local_variable_declarationContext)this._ctx;
	if (local_var_decl == null) return true;
	CSharpParser.Local_variable_typeContext local_variable_type = local_var_decl.local_variable_type();
	if (local_variable_type == null) return true;
	if (local_variable_type.getText().equals("var")) return false;
	return true;
    }
}

However, it should be a syntax error, but it is not a syntax error.
Few people may consider this use case important.
Maybe it should be managed as a separate issue.

@KvanTTT KvanTTT added the csharp label May 6, 2022
@kaby76
Copy link
Contributor

kaby76 commented May 7, 2022

The grammars-v4/csharp/ parser gives a syntax error for that input:

# run in Bash shell.
git clone https://github.com/antlr/grammars-v4.git
cd grammars-v4/csharp
trgen
cd Generated
make
cat - << HERE > test.in
class A{
  void a() {
    int[] nums = {1, 3, 5, 7};
    for (int num : nums){
        b(num);
    }
  }
}
HERE
dotnet run -file test.in

Output:

line 4:17 mismatched input ':' expecting ';'
Time: 00:00:00.1457023
Parse failed.

Please clarify what you are trying to do.

@yososs
Copy link
Author

yososs commented May 7, 2022

I am intentionally entering source code that results in a syntax error. The expected result is that CSharpParser reports a syntax error.

  1. the actual result is that ClassCastException is thrown
  2. if I fix this problem by adding a cast check, no syntax error is reported

2 is a pointing error, as I found a problem with my checking method.

To reproduce 1, follow the steps

You need to test on a java target.

  1. save the above source in examples/test.cs
  2. mvn3 test
Caused by: java.lang.ClassCastException: class CSharpParser$Local_variable_declaratorContext cannot be cast to class CSharpParser$Local_variable_declarationContext (CSharpParser$Local_variable_declaratorContext and CSharpParser$Local_variable_declarationContext are in unnamed module of loader java.net.URLClassLoader @6570e3cb)
	at CSharpParserBase.IsLocalVariableDeclaration(CSharpParserBase.java:13)
	at CSharpParser.local_variable_declaration_sempred(CSharpParser.java:20502)
	at CSharpParser.sempred(CSharpParser.java:20489)
	at org.antlr.v4.runtime.atn.SemanticContext$Predicate.eval(SemanticContext.java:94)
	at org.antlr.v4.runtime.atn.ParserATNSimulator.evalSemanticContext(ParserATNSimulator.java:1430)
	at org.antlr.v4.runtime.atn.ParserATNSimulator.splitAccordingToSemanticValidity(ParserATNSimulator.java:1346)
	at org.antlr.v4.runtime.atn.ParserATNSimulator.getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(ParserATNSimulator.java:1301)
	at org.antlr.v4.runtime.atn.ParserATNSimulator.execATN(ParserATNSimulator.java:469)
	at org.antlr.v4.runtime.atn.ParserATNSimulator.adaptivePredict(ParserATNSimulator.java:393)
	at CSharpParser.local_variable_declarator(CSharpParser.java:9577)
	at CSharpParser.local_variable_declaration(CSharpParser.java:9434)
	at CSharpParser.for_initializer(CSharpParser.java:10278)
	at CSharpParser.simple_embedded_statement(CSharpParser.java:8896)
	at CSharpParser.embedded_statement(CSharpParser.java:8223)
	at CSharpParser.statement(CSharpParser.java:7626)
	at CSharpParser.statement_list(CSharpParser.java:10209)
	at CSharpParser.block(CSharpParser.java:9287)
	at CSharpParser.method_body(CSharpParser.java:13611)
	at CSharpParser.method_declaration(CSharpParser.java:19943)
	at CSharpParser.common_member_declaration(CSharpParser.java:12900)
	at CSharpParser.class_member_declaration(CSharpParser.java:12608)
	at CSharpParser.class_member_declarations(CSharpParser.java:12467)
	at CSharpParser.class_body(CSharpParser.java:12408)
	at CSharpParser.class_definition(CSharpParser.java:18904)
	at CSharpParser.type_declaration(CSharpParser.java:11539)
	at CSharpParser.namespace_member_declaration(CSharpParser.java:11447)
	at CSharpParser.namespace_member_declarations(CSharpParser.java:11361)
	at CSharpParser.compilation_unit(CSharpParser.java:421)
	... 31 more

Modifying CShapParserBase.java reports the following syntax error

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, '[', '*', '?'}
com.khubla.antlr.antlr4test.AssertErrorsException: found 1 errors, but missing file test.cs.errors
	at com.khubla.antlr.antlr4test.AssertErrorsErrorListener.assertErrors(AssertErrorsErrorListener.java:64)
	at com.khubla.antlr.antlr4test.ScenarioExecutor.testGrammar(ScenarioExecutor.java:174)
	at com.khubla.antlr.antlr4test.ScenarioExecutor.testGrammars(ScenarioExecutor.java:229)
	at com.khubla.antlr.antlr4test.GrammarTestMojo.testScenarios(GrammarTestMojo.java:360)
	at com.khubla.antlr.antlr4test.GrammarTestMojo.execute(GrammarTestMojo.java:179)
	at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
	at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute(MojoExecutor.java:301)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:211)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:165)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:157)
	at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:121)
	at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
	at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
	at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:127)
	at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:294)
	at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
	at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
	at org.apache.maven.cli.MavenCli.execute(MavenCli.java:960)
	at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:293)
	at org.apache.maven.cli.MavenCli.main(MavenCli.java:196)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:567)
	at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:282)
	at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:225)
	at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:406)
	at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:347)

@kaby76
Copy link
Contributor

kaby76 commented May 7, 2022

Got it. Yes, it doesn't work.

@yososs
Copy link
Author

yososs commented May 7, 2022

I am concerned about the different syntax error messages when run with dotnet and when run with java.

dotnet

line 4:17 mismatched input ':' expecting ';'

java

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, '[', '*', '?'}

@kaby76
Copy link
Contributor

kaby76 commented May 7, 2022

I agree. I have a PR to fix the cast problem, but we'll need to find out why the diff in the runtimes.

@yososs
Copy link
Author

yososs commented May 7, 2022

Close this issue after confirming the merge.

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

No branches or pull requests

3 participants