1
votes

I used Antlr3 a view years ago with Java. Right now I'm coding in C# and I would like to catch up with the changes in Antrl4 and use it in C# code generation. With

java org.antlr.v4.Tool -Dlanguage=CSharp Hello.g4 -visitor

the C#-sources are generated. For testing an exploring I would like to use the grun-Testrig. In java this should be the following, but in C# I get an error, obviously because the compiled Lexer/Parser should be executed in DotNet-Runtime and not in Java.

$java org.antlr.v4.runtime.misc.TestRig Hello -gui Warning: TestRig moved to org.antlr.v4.gui.TestRig; calling automatically Can't load Hello as lexer or parser

How can I use an equivalent TestRig in C#-Environment?

1
The way I do it is to: 1. use the NuGet to avoid calling the ANTLR tool yourself, the package integrates seamlessy with the build pipeline. 2. Just write NUnit unit tests like usual, you can get full control over what you're testing. I never felt the need to use TestRig. - Lucas Trzesniewski

1 Answers

1
votes

I did this by using pre-build events in the properties of the c# project in VisualStudio.

I made a PreBuildEvents.bat as follows:

set ThisDrive=%~d0
set ThisDirectory=%~dp0
%ThisDrive%
cd %ThisDirectory%
call setPath.bat
java org.antlr.v4.Tool -Dlanguage=CSharp MyGrammar.g4
java org.antlr.v4.Tool -Dlanguage=Java   MyGrammar.g4
javac -d .\class *.java

and call it in the pre-build events.

Then I can simulate the results using my grun.bat:

java -cp "./class;%CLASSPATH%" org.antlr.v4.gui.TestRig %*

This will not work if you've included c# code in the rule actions, however, since there will be syntax errors in the java files.