dotnet dotgram

The reference for DotGram.Cli, the .NET tool that runs the DotGram generator outside the build. What it is for and when to reach for it is in the README's "Big grammars, fast builds, fast starts"; this page is every command, option and exit code, and the contract. dotnet dotgram --help prints the same list and dotnet dotgram --skill the guide for coding agents; a test holds --help and this page to the options the parser reads.

A project either references the DotGram package and generates in its build, or has its parsers generated by DotGram.Cli and committed — never both: the build would generate the same types again.

Install

dotnet new tool-manifest                   # once, in the repository root (or install with --global)
dotnet tool install DotGram.Cli
dnx DotGram.Cli generate ...               # .NET 10: run without installing

The tool runs on .NET 8 and later, on the newest runtime installed (what an identifier may be is decided by the runtime's Unicode tables, and the tool must decide as the build does), and reads a project with a .NET SDK 8.0.100 or newer. It generates for a project only where that SDK's compiler can run DotGram (SDK 9.0.300 or newer for this version); a .gram input needs no SDK compiler. The version of the tool is the version of the DotGram generator it carries; dotnet dotgram --version prints both.

Two distributions

The generator outside the build ships twice, from one source:

The .NET tool The native executable
What it is the DotGram.Cli package on NuGet, run as dotnet dotgram dotgram (dotgram.exe on Windows), one file per platform, compiled ahead of time
Platforms wherever .NET 8 or later runs linux-x64, linux-arm64, win-x64, win-arm64, osx-x64, osx-arm64
Needs .NET 8 or later; an SDK to read a project no .NET and no SDK; on Linux, the system's ICU library, as .NET itself
Where from dotnet tool install DotGram.Cli an archive per platform, attached to the GitHub release of the version
A .gram yes yes: the same options, output, messages and exit codes
A .csproj yes no: exit 3, pointing at the .NET tool
Unicode tables (GRAM0007) the newest .NET runtime installed those of .NET 10, which it is built with

A project brings its other source generators, which run beside DotGram's (see A project), and the tool loads them from their assemblies as the build does; a native executable cannot load an assembly, so a project is the .NET tool's. Everything else is the same program: dotgram generate calc.gram -o out --namespace Demo writes what dotnet dotgram generate writes, byte for byte, and the messages name dotnet dotgram in both.

What the native executable is for is speed: it does not compile Roslyn and the generator each time it starts, as the .NET tool does. Generating a small grammar (the calculator of the examples) takes about 0.2 s against the .NET tool's 2.4 s; on the largest grammar in the repository (T-SQL, with its 75 source files), about a third less than the .NET tool's 28 s. Measured on Linux x64 on a busy machine: directions, not benchmarks.

What an identifier may be is the runtime's Unicode tables' to say (GRAM0007). The .NET tool runs on the newest runtime installed, as the build's compiler does; the native executable carries the runtime it was built with, .NET 10, and keeps its tables. A grammar whose names use a letter that a later Unicode version added can then pass a build on a later .NET and be refused by the native executable until it is built again with that .NET. Generate such a grammar with the .NET tool.

On macOS, a file downloaded by a browser is quarantined, and Gatekeeper refuses an executable that Apple has not notarized; xattr -d com.apple.quarantine dotgram lifts it for the file you downloaded.

Commands

dotnet dotgram generate <project.csproj | grammar.gram> -o <dir> [options] [@file.rsp]
dotnet dotgram --help | help | generate --help | --skill | --version
Command What it does
generate writes the parsers a build would generate
--skill prints the guide for coding agents (SKILL.md, which also ships in the package)
--version prints the tool's and the generator's versions
--help prints the usage, the commands and every option of generate; generate --help prints only generate's
help the same as --help

The input's kind is its extension: .csproj is a whole project, .gram is one grammar.

A project

The tool does not compile the project. It asks MSBuild for what the compiler would be given, for each target framework: the C# files, the references, the language version and symbols, the source encoding, the additional files, the analyzer configuration and the analyzers, the generators of other packages among them, which run beside DotGram's. That runs the project's InitialTargets and the targets that run before CoreCompile (they write AssemblyInfo, the global usings and the analyzer configuration under obj/, and anything of the project's own hooked there), not Build, AfterBuild or anything after the compiler. The projects it references are built if they are not up to date. Every framework must generate the same files. The project's language version, symbols, references, packages and source encoding (CodePage) are the build's to decide, which is why --reference, --package, --define and --langversion are not options of a project run.

The project must be restored: the tool never restores.

A project that references another version of DotGram than the tool carries is generated with the tool's, with a warning naming both. A project that references none is generated with the tool's, and says so.

The contract

generate writes the files DotGram would generate in dotnet build of the project (configuration Release unless --configuration says otherwise), byte for byte, in the encoding the compiler writes them (UTF-8 with a byte order mark), with the tool's DotGram, for a restored project, built with the same SDK, whose own targets do not change the compiler's inputs outside the IDE's design-time pass. A difference on a project inside that condition, which every ordinary SDK-style project is, is a defect in the tool: report it.

Two differences are by design:

  • #line paths are relative to the output folder, so that committed files do not carry the path of the machine that wrote them. --line-paths absolute gives the build's text.
  • The generation report files (DotGramReportGeneration) are not written.

The EmbeddedAttribute definition is the compiler's text and may differ in line endings from your SDK's. The diagnostics of DotGram itself are shown at the severities the build gives them (.editorconfig, /nowarn, /warnaserror); an error among them stops the run with exit 2, except when every one of them is GRAM3004 (a type a grammar names is not in view): that run exits 3. The SDK's compiler must be at least as new as the one DotGram was built for, as for the build, which skips a generator built for a newer compiler (CS9057): the run then exits 3.

What it does not do

It does not compile the project and does not tell you whether it builds: errors in grammar actions, in the generated parser or anywhere else in the project show when your build compiles the files. It reports the errors in your own sources that it sees, at the compiler's default severities (/warnaserror, /nowarn, rulesets and .editorconfig severities are not applied), and stops there as the build would; it does not check the project as a whole. --allow-unresolved generates anyway, reporting them as warnings.

It runs what an IDE runs when it opens the project: MSBuild's Compile target with the compiler skipped, as a design-time build (DesignTimeBuild=true). A custom target that does not skip design-time builds runs too, and one that follows the compiler finds no new assembly. It does not restore: after changing package references, run dotnet restore first. When this is not good enough, build with EmitCompilerGeneratedFiles=true and copy the generated files.

A grammar

dotnet dotgram generate calc.gram -o out --namespace Demo --option Lexical=true

The tool writes the host class from the options (<Name>.Host.g.cs) and generates the grammar into it, for net10.0 unless --framework says another. There is no build to ask, so the options below say what a build would. The parser is compiled with its host and the --sources before anything is written: an error stops the run (exit 2), and a warning in the generated parser is printed and does not, though a build that treats warnings as errors refuses it.

Options

Option Input What it does
-o, --output <dir> both where the files go (required); nothing else in the folder is touched
--framework <tfm> both one framework only: for a project, one of its target frameworks (default: each of them); for a .gram, netstandard2.0, or net8.0 or later, a framework later than net10.0 compiled against net10.0's references (default: net10.0)
--configuration <name> project the project's configuration (default Release)
--line-paths relative\|absolute both #line paths relative to the output folder (default), or as a build writes them
--allow-unresolved both generate even where the project's code has errors (reported as warnings)
--verbose both print the stack trace of a failure inside the generator
--reference <file.dll\|dir> .gram an assembly the grammar's types come from (every *.dll of a directory)
--package <Name>@<Version> .gram a package's assemblies, from the global packages folder
--define <SYMBOL> .gram a preprocessor symbol, beside the framework's; several in one value, separated by ; or ,
--langversion <version> .gram the C# version (default: the SDK's for the framework)
--namespace <Ns> .gram the host's namespace (default: none)
--class <Name> .gram the host's name (default: the file's name)
--public .gram a public host (default: internal)
--option <Name>=<value> .gram a [Gram] property: Lexical=true, Carrier=Immediate, LocationType=My.ISpan, Name=Fast, TargetMethodPartSize=200, …
--sources <file.cs\|glob> .gram C# the grammar builds, and a partial host with the members its hooks call (no [Gram] on it)
--help, -h, -? print the help
--version print the versions
--skill print the guide for agents

-- ends the options: every argument after it is the input, a dash in front or not. An option of a .gram input given with a project is a usage error, not ignored.

Response files

@file.rsp anywhere on the command line is replaced by the arguments in that file, as csc reads one: words separated by blanks or line ends, "quoted" to keep blanks, a line that begins with # a comment, @ nesting. A relative path in the file is read against the file's own directory. The first line the tool prints is the arguments as it understood them, response files expanded.

# grammars.rsp
src/Grammars/Grammars.csproj
-o src/Grammars/Generated

The output folder

The generated files are written in the encoding of the contract, and .dotgram.json beside them lists them with the versions of the tool and the generator that wrote them. A later run deletes a file the manifest lists and the run no longer writes, and touches nothing else in the folder. A file is rewritten only when its text changed, so a run that changes nothing changes no timestamp.

Keeping the files current

Nothing in the build notices that a grammar changed: run the command again after editing a grammar, or a type it builds, and read the diff of the folder. To check it in CI, run it there and fail if the folder differs:

dotnet dotgram generate @grammars.rsp
git status --porcelain -- src/Grammars/Generated    # fail if it prints anything

src/Grammars/Generated/** -text in the repository's .gitattributes keeps the files exactly as the generator writes them: the folder's path from the root, since a bare Generated/** matches only a folder beside the .gitattributes; the tool prints the line for its output folder. It prints the GRAM diagnostics; the editor does not show them in a project that no longer references the package.

Under core.autocrlf=true Git converts line endings on checkout and commit, and a fresh generation can then differ from what is committed, so git status is not clean. The generated C# is written with CRLF and .dotgram.json with LF; -text (the line above) keeps both exactly as written. The grammar sources (.gram files, and the .cs files that hold inline grammars) are read as checked out, so pin their line endings too, so that every machine generates from the same text. Later lines of .gitattributes win, so the output folder's -text line comes last: a *.cs rule after it would convert the generated files.

*.gram                    text eol=lf
src/Grammars/**/*.cs      text eol=lf
src/Grammars/Generated/** -text

With these lines, a fresh generation after a checkout under core.autocrlf=true leaves git status clean, and the committed files are byte for byte what the tool wrote.

Exit codes

Code Meaning
0 generated
2 grammar errors (nothing written), unless every one is GRAM3004, which exits 3; for a .gram, a parser that does not compile
3 usage, input or output; a project not restored; MSBuild failing for a framework; a generator that will not load; an error the project's build would report too (nothing written); frameworks that generate different files

Not to do

  • Do not reference the DotGram package and compile the generated folder in the same project.
  • Do not edit the generated files: the next run overwrites them.
  • Do not commit the output of one framework for a multi-targeted project generated with --framework: the folder is compiled by every framework the project targets.