For this issue, I do not bring a lot of news. With Christmas and other requests, I had little time to continue this tutorial. But, having someone to test the previous issues of this tutorial (thanks, Vicente), we found out that the custom Build Task developed in the first parts of this tutorial has problems when paths have spaces (or in Windows machine if you use those).

In this part of the tutorial, we will do a quick thing: replace our custom Build ANTLR task with a proper one, Antlr4BuildTasks, maintained by Ken Domino and others, available as a Nuget package, and open source.

The first step is to remove the reference to our build task and delete all generated files, that will confuse the new generation tool.

From the Logo2Svg.csproj file, remove the following code:

Logo2Svg.csproj
  <Target Name="BuildAntlr" BeforeTargets="CoreCompile" Inputs="$(GrammarPath)/LogoLexer.g4 $(GrammarPath)/LogoParser.g4" Outputs="$(GrammarPath)/LogoParser.cs $(GrammarPath)/LogoLexer.cs $(GrammarPath)/LogoParserBaseVisitor.cs $(GrammarPath)/LogoParserVisitor.cs">
    <BuildAntlr JavaPath="/usr/bin/java" AntlrJar="/opt/local/lib/antlr-4.13.1-complete.jar" Listeners="false" Namespace="Logo2Svg.Language" Visitors="true" OutputDir="$(GrammarPath)" Files="$(GrammarPath)/LogoLexer.g4 $(GrammarPath)/LogoParser.g4">
     </BuildAntlr>
     <ItemGroup>
        <Compile Include="$(GrammarPath)/LogoParser.cs" />
        <Compile Include="$(GrammarPath)/LogoLexer.cs" />
        <Compile Include="$(GrammarPath)/LogoParserVisitor.cs" />
        <Compile Include="$(GrammarPath)/LogoParserBaseVisitor.cs" />
     </ItemGroup>
  </Target>

  <Target Name="CleanGeneratedFiles" BeforeTargets="Build">
        <Delete Files="$(GrammarPath)/LogoParser.cs" />
        <Delete Files="$(GrammarPath)/LogoLexer.cs" />
        <Delete Files="$(GrammarPath)/LogoParserVisitor.cs" />
        <Delete Files="$(GrammarPath)/LogoParserBaseVisitor.cs" />
  </Target>
XML

Then, remove all files under the Languages folder, except for the two g4 files. We will also clean the solution completely. While dotnet has a clean command for that, be sure to clean up everything. Remove the bin/ and obj/ folders from the Logo2Svg folder.

Finally, add the new Nuget reference:

dotnet add Logo2Svg package Antlr4BuildTasks
Bash

This will add the reference to this Nuget in your Logo2Svg.csproj file. It should look like the following line:

Logo2Svg.csproj
    <PackageReference Include="Antlr4BuildTasks" Version="12.7.0" />
XML

Finally, we need to configure properly this package. Add the following block to Logo2Svg.csproj:

Logo2Svg.csproj
   <ItemGroup>
     <Antlr4 Include="$(GrammarPath)/*.g4">
       <Listener>false</Listener>
       <Visitor>true</Visitor>
       <Package>Logo2Svg.Language</Package>
       <Errors>true</Errors>
       <JavaExec>/usr/bin/java</JavaExec>
     </Antlr4>
   </ItemGroup>
XML

This build task has other fields you can configure. Here we specify where to find the grammar files, we do not want listeners but rather visitors. We also specify the package name. The Errors field specifies that we want the build to fail on a warning, as if was an error. This way we do not miss any message that might be hidden somehow in the rest of the build output. Finally, this plugin downloads itself a Java interpreter and the ANTLR jar file. To reduce the amount of disk space in the disk I decided to specify a Java binary path. This way the plugin will not download Java, but just the ANTLR jar file.

Everything should be set up. Just run build everything up, and test. You should be good to go.

Leave a Reply