Treetop is a proof-of-concept C# fluent API generator for context-free protocols. Treetop accepts a context-free grammar, specifying an API protocol or a domain-specific language, and embeds it in C# as a fluent API. The resulting API enforces the grammar at compile-time: A fluent API chain may compile if and only if it encodes a word derived from the grammar.
For example, consider the following context-free grammar deriving palindromes:
S ::= a S a
S ::= b S b
S ::= a
S ::= b
S ::=
(Note that the line S ::=
specifies an S
epsilon-production.)
Treetop converts this grammar into a C# fluent API that accepts only
palindromes at compile time:
// Compiles, "abbabba" is a palindrome
Start.a().b().b().a().b().b().a().Done<Palindrome>();
// Does not compile, "abbbaba" is not a palindrome
Start.a().b().b().b().a().b().a().Done<Palindrome>();
treetop-core
: The core logic of Treetop. This project also contains the Treetop source generator,CFProtocolGenerator.cs
: Visual Studio does not support source generator projects with dependencies, so the generator had to be moved to the core project (see the following discussion). A designated generator project is planned to be added in the future.treetop-cli
: The Treetop command line application. Depends ontreetop-core
.tests
: Tests fortreetop-core
.sandbox
: A sandbox project for testing the source generator within Visual Studio.