.Gram
Fast, strongly typed C# parsers, generated from a grammar at compile time, with no runtime library. The generated parser is ordinary C# in your own assembly: there is no parser engine or grammar graph to interpret, and nothing to deploy beside your application. One notation covers a six-character colour code, a calculator, feeds written by people and the SQL standard.
A grammar is a small language of its own. Every package ships a skill for coding agents, so an agent can write the grammar with you: Writing grammars with a coding agent.
Start here
Five steps of growing scale, each a parser that builds and runs in the repository:
- A pattern instead of a regular expression: a colour code, and a capture that becomes a property
(
CssColorExample.cs). - A calculator: precedence written on the operators, one grammar published over
int,decimaland a tree (One grammar, three parsers). - A CSV feed with recovery: quoted fields, and a bad line reported where it is while reading goes on (Feeds: CSV with quoted fields).
- A SQL fragment: a guard that decides whether a statement can write anything
(
SqlReadOnlyExample.cs). - A stream of records: a feed read from a
TextReader, in parts, with a trailer checked against records nobody held (StreamingFeedExample.cs).
The Language guide then takes the notation a topic at a time.
Getting started
<PackageReference Include="DotGram" Version="0.3.0"
PrivateAssets="all" ExcludeAssets="runtime" />
The generator runs in the C# compiler of .NET SDK 9.0.300 or newer (Roslyn 4.14; Visual Studio 17.14).
The project may still target net8.0 or older, but an SDK 8 build skips the generator with warning
CS9057 and generates no parser, so the code that calls it does not compile.
A grammar lives in the [Gram] attribute of the class the parser is generated into, or in a
.gram file listed as <AdditionalFiles Include="Name.gram" />:
[Gram("""
Word = ['a'..'z']+
Filter = field: Word & '=' & value: Word
parse Filter
""")]
public static partial class FilterParser;
The generator turns it into Parse, TryParse and Find over text, streams, bytes and
segments, strongly typed results from the named captures, error recovery for record-oriented
input, and compile-time diagnostics that point back into the grammar.
Where to go next
| Writing grammars with a coding agent | Where each package's SKILL.md is, and a loop that works. |
| Language guide | The notation, a page a topic. |
| Language reference | The notation and its bond with C#: the specification. |
| Diagnostics | Every message the generator reports, by identifier. |
| Command line | dotnet dotgram, the generator run outside the build. |
| Feeds | A worked guide: trading files in pipes, CSV and fixed width, bulk-loaded into a database. |
| Visual Studio | The extension, and the StringSyntax annotations. |
| VS Code | The extension for .gram files and grammars in C# strings, and the language server it runs. |
| Untrusted input | What a parser bounds, what it does not, and what to do. |
| Status | What this version does, held against the specification. |
| API reference | The public types of the runtime libraries. |
The source, the examples and the issue tracker are on GitHub.