I was just looking for the same thing for .NET Core 2.0.
The current version of ANTLR4 is 4.6.5 Beta 1. In this version, the C # generator was ported to C #, so Java dependency was removed. This is still experimental and needs to be enabled manually:
<Antlr4UseCSharpGenerator>True</Antlr4UseCSharpGenerator>
Files are not generated when the .g4
file is .g4
. They will be generated when dotnet build
called.
The globbing file works as expected. But changing settings, such as <Visitor>false</Visitor>
, requires calling dotnet clean
before dotnet build
.
The default Antlr task parameters can be found in the source :
<Antlr4> <Generator>MSBuild:Compile</Generator> <CustomToolNamespace Condition="'$(Antlr4IsSdkProject)' != 'True'">$(RootNamespace)</CustomToolNamespace> <CopyToOutputDirectory>Never</CopyToOutputDirectory> <Encoding>UTF-8</Encoding> <TargetLanguage>CSharp</TargetLanguage> <Listener>true</Listener> <Visitor>true</Visitor> <Abstract>false</Abstract> <ForceAtn>false</ForceAtn> </Antlr4>
The globe works, so if I want to collect all g4
files and turn off visitors, all I need to write is:
<ItemGroup> <Antlr4 Include="**/*.g4" > <Visitor>false</Visitor> </Antlr4> </ItemGroup>
My entire csproj
file is as follows:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netstandard2.0</TargetFramework> <Antlr4UseCSharpGenerator>True</Antlr4UseCSharpGenerator> </PropertyGroup> <ItemGroup> <PackageReference Include="Antlr4"> <Version>4.6.5-beta001</Version> </PackageReference> </ItemGroup> <ItemGroup> <Antlr4 Include="**/*.g4" > <Visitor>false</Visitor> </Antlr4> </ItemGroup> </Project>
source share