Net4x.AntlrPostBuild
1.0.0.26245
dotnet add package Net4x.AntlrPostBuild --version 1.0.0.26245
NuGet\Install-Package Net4x.AntlrPostBuild -Version 1.0.0.26245
<PackageReference Include="Net4x.AntlrPostBuild" Version="1.0.0.26245" />
<PackageVersion Include="Net4x.AntlrPostBuild" Version="1.0.0.26245" />
<PackageReference Include="Net4x.AntlrPostBuild" />
paket add Net4x.AntlrPostBuild --version 1.0.0.26245
#r "nuget: Net4x.AntlrPostBuild, 1.0.0.26245"
#:package Net4x.AntlrPostBuild@1.0.0.26245
#addin nuget:?package=Net4x.AntlrPostBuild&version=1.0.0.26245
#tool nuget:?package=Net4x.AntlrPostBuild&version=1.0.0.26245
Net4x.AntlrPostBuild
A build-only MSBuild tool that post-processes ANTLR-generated C# sources so that one set of generated files compiles against both ANTLR C# runtimes:
Antlr4.Runtime.Standard— the modern runtime used on .NET Standard / .NET (Core), andAntlr4.Runtime— the legacy runtime used on .NET Framework 3.5 / 4.0, which is built on Sharpen and exposes a different API for a handful of members.
Instead of maintaining two copies of a generated lexer/parser, the tool rewrites the few lines that
differ into #if NETSTANDARD / #else blocks, and writes the result into the parser project.
Install
dotnet add package Net4x.AntlrPostBuild
The package contains no assemblies to reference — it ships an MSBuild .targets file plus the tool
itself under native/. Mark it as a build-only dependency:
<PackageReference Include="Net4x.AntlrPostBuild" Version="1.0.*" PrivateAssets="All" />
Net4x.AntlrPostBuild.targets is imported automatically and adds two targets that run
AfterTargets="PostBuildEvent" — PrepareParser on Windows (runs AntlrPostBuild.exe) and
UnixPrepareParser elsewhere (runs dotnet AntlrPostBuild.dll). Both pass the consuming project's
directory as -ProjectDir:.
What it rewrites
Each rule matches a line by substring and replaces that whole line with a conditional block:
| Generated code | Rewritten to (non-NETSTANDARD branch) |
|---|---|
public override int[] SerializedAtn { get { return _serializedATN; } } |
public override string SerializedAtn returning JsonConvert.SerializeObject(_serializedATN) |
new ATNDeserializer().Deserialize(_serializedATN); |
new ATNDeserializer().Deserialize(Array.ConvertAll(_serializedATN, i => (char)i)) |
var charStream = CharStreams.fromString(script); |
CharStreams.fromStream(new MemoryStream(Encoding.UTF8.GetBytes(script))) |
this.TokenFactory.Create(new Tuple<ITokenSource, ICharStream>(…), MySQLLexer.DOT_SYMBOL, … |
the same call using Antlr4.Runtime.Sharpen.Tuple<…> |
The NETSTANDARD branch always keeps the original generated line, so the modern runtime path is
unchanged. Files are otherwise copied verbatim.
Note that rewritten files are written with CRLF line endings (the injected blocks are CRLF, and a
file that matches more than the last rule is normalised to CRLF throughout). Files with no match are
copied through byte for byte. If your repository enforces LF, add the output directory to
.gitattributes accordingly.
Because the legacy branch uses Newtonsoft.Json, MemoryStream and Encoding, the target project
must reference Newtonsoft.Json and have System.IO / System.Text in scope for the old targets.
Expected layout
The tool is built for a specific solution shape. Given -ProjectDir:<dir> it:
- reads every
*.csin<dir>\Antlr(the ANTLR-generated output); - adds the runtime base classes from
<dir>\..\grammars-v4\sql\...\CSharp\—PlSqlLexerBase,PlSqlParserBase,MySqlLexerBase,MySqlParserBase,SqlMode,SqlModes,PostgreSQLLexerBase,PostgreSQLParserBase,LexerDispatchingErrorListener,ParserDispatchingErrorListener; - deletes every
*.csin<dir>\..\SqlParameterizer.Parser\Antlr(creating the directory if needed); - writes the rewritten files there under their original names.
<solution>\
<your project>\Antlr\ <- input: ANTLR-generated .cs
grammars-v4\sql\...\CSharp\ <- input: hand-written runtime base classes
SqlParameterizer.Parser\Antlr\ <- output: cleared, then rewritten copies
⚠️ Step 3 is destructive. The output directory is treated as generated: do not keep hand-written sources in
SqlParameterizer.Parser\Antlr.
If an expected base-class file is missing, the tool throws and the build fails.
Command line
AntlrPostBuild -ProjectDir:"<path>" [-Configuration:"<cfg>"]
-ProjectDir:— required; surrounding single or double quotes are trimmed. Without it the tool printsProjectDir argument is required.and exits with-1.-Configuration:— accepted for compatibility and currently ignored.
Exit codes: 0 on success, -1 if the argument is missing, the input directory does not exist, or
a stale output file could not be deleted.
Requirements
- The tool itself runs on .NET 10 (
native/AntlrPostBuild.exeon Windows,dotnet native/AntlrPostBuild.dllelsewhere). - The consuming project can target anything; the package is
net35;net40;netstandard2.0;net10.0compatible and contributes no runtime assemblies.
License / source
Learn more about Target Frameworks and .NET Standard.
-
.NETFramework 3.5
- No dependencies.
-
.NETFramework 4.0
- No dependencies.
-
.NETStandard 2.0
- No dependencies.
-
net10.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0.26245 | 93 | 9/3/2026 |
| 1.0.0 | 212 | 11/27/2025 |