Fabulous.AST, an Abstract Syntax Tree (AST) Domain Specific Language (DSL) for F#.
$ dotnet add package Fabulous.ASTWelcome to Fabulous.AST, an Abstract Syntax Tree (AST) Domain Specific Language (DSL) for F#.
Fabulous.AST leverages Fantomas to generate F# code from AST. This allows you to create F# code that adheres to the Fantomas style guide while providing a simple and expressive way to represent code as a tree of nodes. This approach simplifies programmatic code manipulation and analysis.
Install Fabulous.AST via NuGet:
dotnet add package Fabulous.AST
Let's compare AST creation using Fantomas directly and Fabulous.AST:
open Fantomas.Core
open Fantomas.Core.SyntaxOak
open Fantomas.FCS.Text
Oak(
[],
[ ModuleOrNamespaceNode(
None,
[ BindingNode(
None,
None,
MultipleTextsNode([ SingleTextNode("let", Range.Zero) ], Range.Zero),
false,
None,
None,
Choice1Of2(IdentListNode([ IdentifierOrDot.Ident(SingleTextNode("x", Range.Zero)) ], Range.Zero)),
None,
[],
None,
SingleTextNode("=", Range.Zero),
Expr.Constant(Constant.FromText(SingleTextNode("12", Range.Zero))),
Range.Zero
)
|> ModuleDecl.TopLevelBinding ],
Range.Zero
) ],
Range.Zero
)
|> CodeFormatter.FormatOakAsync
|> Async.RunSynchronously
|> printfn "%s"
open Fantomas.Core
open Fabulous.AST
open type Fabulous.AST.Ast
Oak() {
AnonymousModule() {
Value("x", "12")
}
}
|> Gen.mkOak
|> Gen.runBoth examples produce the following F# code:
let x = 12As you can see, Fabulous.AST significantly simplifies the process of creating ASTs for F# code.
For comprehensive information about Fabulous.AST, visit our documentation site.
Additional resources:
Show your support for Fabulous.AST:
We welcome contributions to Fabulous.AST!
main branch and follow our Contributor Guide.Fabulous.AST is released under the MIT License.