I'm trying to install ANTLR 4 support in a .NET 5 project in Visual Studio 16.9.2, but I can't figure out which of the myriad ANTLR NuGet packages I should use. I want one that doesn't require Java at all (not just at runtime) and that doesn't require an extension.
I initially used the Antlr4 and Antlr4.Runtime packages, and they seem to work together, but then I found that they're missing the CharStreams class, which as far as I know is the intended way to get an ICharStream object.
I then tried Antlr4.Runtime.Standard instead of Antlr4.Runtime, but that doesn't appear to be compatible with the Antlr4 package. (It gave me many compile errors.) Then I tried switching out Antlr4 for other packages but couldn't find the one that works with Antlr4.Runtime.Standard.
Would someone point me to the valid combinations of generator + runtime, please? There are too many official packages to keep doing trial-and-error. I've looked to several online tutorials, but they tend to be for previous versions of VS that still use a VSIX.
Here is a rundown on the packages you could use:
- Antlr4.Runtime -- This is Harwell's port of Antlr4 v4.6.6. This package contains a tool, runtime, and build rules for a complete experience with NET. But, this toolset is many years old and is not being maintained. It cannot be used with the "official" Antlr toolset and runtime in any way. Some of the grammars in grammars-v4 target this toolchain, but I have been rewriting most of the grammars there to work against the "official" version, not Harwell's code. There is no shim to interoperate grammars targeted to this old runtime with the newer/"official" runtime, but I am writing one. This package works with a number of old-style target frameworks.
- Antlr4.Runtime.Standard. This is the "official", supported release of Antlr4. This is a netstandard2.0 library, and it contains just the runtime. To use it, the Antlr4 JAR tool must generate the parser. It does not include the tool, nor the build rules.
- Antlr4BuildTasks. This is a set of build rules for working with Antlr4.Runtime.Standard. It's a fork I made of Harwell's tool, and I have been polishing it for years. It provides a seamless build for Antlr4 programs without manually downloading and running the Antlr4 Jar tool: it downloads the jar for you and contains a self-contained Java environment to call the JAR. It uses Java, but you don't need to install Java, and won't even know it's called unless you turn on diagnostic output for a build. It's not integrated into VS2019 so it can work with MSBUILD, dotnet, VS2019, VSCode, Ryder, etc. This code works anywhere: Windows, Linux, OSX. (Source repo and more installation details here.)
You should also be aware of the several other tools I have been writing (for years) to generate Antlr programs:
- Antlr4BuildTasks.Templates. This is a simple Dotnet template that you can install, generate, and run a new Antlr C# program via
dotnet new antlr; dotnet run
. It's not very flexible, but it's simple and works to create a simple Arithmetic example that you can then adapt for your needs. There's no downloading and install of Java or the Antlr4 JAR--it's all done for you invisibly with a "build" since it uses my Antlr4BuildTasks package.
- dotnet-antlr. This program is a StringTemplate program that generates an Antlr4 program for many target languages. I currently have it working for C#, Java, JavaScript, Dart, Go, Python3, and will add other targets at some point. I also have code generation
for Harwell's Antlr4cs/Antlr4.Runtime library. It generates makefiles to provide a basic, consistent interface to build and run the Antlr program, but one can shove in any set of templates to generate whatever you want. To use the makefiles,
use Msys2. It also generates a .csproj file, so you can use that without Make
whatsoever. The program is being used for CI testing in the grammars-v4 builds. I am currently developing this program and plan on moving it into a toolchain for editing/transforming grammars.
I have an extension for VS2019 for Antlr but I recommend that you just use VSCode with my extension or Mike's extension if you need to actually see text colorization of the grammar in order to edit a frigging grammar. Note, VS2019 supports only an ancient version of LSP, and doesn't support semantic highlighting. I complained to MS, but they are extremely slow in updating VS2019. I had to write an LSP type library to work past the limited package MS wrote.
Edit: The nuget package Antlr4.CodeGenerator is the package that actually encodes the parser generator (aka "tool") for Harwell's port of Antlr4 v4.6.6. It encodes the build rules for msbuild. The equivalent to this for the "official" Antlr4 tool is Antrl4BuildTasks.