Logo Fabulous.AST

Oak

Overview

The Oak widget is the root container for all F# AST nodes in the Fabulous.AST DSL. It serves as the entry point for creating F# code and must be used as the outermost container for your AST structure.

Basic Usage

#r "../../src/Fabulous.AST/bin/Release/netstandard2.1/publish/Fantomas.Core.dll"
#r "../../src/Fabulous.AST/bin/Release/netstandard2.1/publish/Fabulous.AST.dll"
#r "../../src/Fabulous.AST/bin/Release/netstandard2.1/publish/Fantomas.FCS.dll"

open Fabulous.AST
open type Fabulous.AST.Ast
open Fantomas.FCS.Text

Oak() { AnonymousModule() { Value("x", Int(42)) } }
|> Gen.mkOak // Convert to an Oak AST node
|> Gen.run // Generate the F# code string
|> printfn "%s"

// produces the following code:
let x = 42

Structure

The Oak widget can contain any combination of the following top-level nodes:

Note: Module nodes must be nested inside either AnonymousModule or Namespace nodes.

Adding Hash Directives

Add compiler directives at the Oak level:

Oak() { AnonymousModule() { NoWarn("0044") } }
|> Gen.mkOak
|> Gen.run
|> printfn "%s"

// produces the following code:
#nowarn 0044

Example with Multiple Components

Here's a more complete example showing how Oak can contain multiple components:

Oak() {
    // Hash directive at the file level
    AnonymousModule() { NoWarn("0044") }

    // A namespace with a module
    Namespace("Widgets") { Module("WidgetModule") { Value("x", String("12")) } }

    // An anonymous module with code
    AnonymousModule() { Value("y", String("12")) } |> _.triviaBefore(Newline())
}
|> Gen.mkOak
|> Gen.run
|> printfn "%s"

// produces the following code:
#nowarn 0044
namespace Widgets

module WidgetModule =
    let x = "12"

let y = "12"

Using EscapeHatch for raw SyntaxOak nodes

Example: injecting a raw top-level binding node into an anonymous module.

open Fantomas.Core.SyntaxOak
open type Fabulous.AST.Ast

Oak() {
    AnonymousModule() {
        // Use the DSL normally
        Value("x", Int 42)

        // If you must inject a raw SyntaxOak node, wrap it in EscapeHatch
        EscapeHatch(
            ModuleDecl.TopLevelBinding(
                BindingNode(
                    None,
                    None,
                    MultipleTextsNode([ SingleTextNode("let", Range.range0) ], Range.range0),
                    false,
                    None,
                    None,
                    Choice1Of2(
                        IdentListNode([ IdentifierOrDot.Ident(SingleTextNode("y", Range.range0)) ], Range.range0)
                    ),
                    None,
                    List.Empty,
                    None,
                    SingleTextNode("=", Range.range0),
                    Expr.Constant(Constant.FromText(SingleTextNode("12", Range.range0))),
                    None,
                    Range.range0
                )
            )
        )
    }
}
|> Gen.mkOak
|> Gen.run
|> printfn "%s"

// produces the following code:
let x = 42
let y = 12

Likewise, if you have other raw nodes (e.g., TypeDefn.Abbrev, EnumCaseNode, etc.), inject them via EscapeHatch(...). The preferred approach remains to use the provided widgets (Abbrev, EnumCase, Record, Value, ...), which do not require EscapeHatch.

Verifying generated code

Gen.parse round-trips the output through Fantomas's F# parser to confirm it is syntactically valid. It returns the rendered source when the code parses, or the parser diagnostics (one per line) otherwise — useful for catching widget combinations that produce broken output before a downstream consumer does. It is curried with the widget last, so it drops into the same pipeline as Gen.run:

Oak() { AnonymousModule() { Value("greet", "x + 1") } }
|> Gen.parse
|> printfn "%s"

// produces the following code:
let greet = x + 1

Gen.parse does not type-check — run the real compiler against the written files for that. There is also an overload that takes a source string directly, and in test suites the producesValid helper builds on it to assert that every generated snippet parses.

Formatting Options

Gen.run formats with Gen.DefaultConfig: Fantomas' defaults (FormatConfig.Default), except that multiline lists, records and other bracketed expressions use the Cramped style. To change the layout, pass your own FormatConfig to the Gen.run(oak, config) overload. Starting from Gen.DefaultConfig keeps the rest of the default style:

open Fantomas.Core

let person =
    Oak() {
        AnonymousModule() {
            Record("Person") {
                Field("FirstName", String())
                Field("LastName", String())
                Field("DateOfBirth", LongIdent("System.DateOnly"))
            }

            Value(
                "people",
                ListExpr(
                    [ RecordExpr(
                          [ RecordFieldExpr("FirstName", String("Ada"))
                            RecordFieldExpr("LastName", String("Lovelace"))
                            RecordFieldExpr("DateOfBirth", Constant("System.DateOnly(1815, 12, 10)")) ]
                      ) ]
                )
            )
        }
    }
    |> Gen.mkOak

// The default style
Gen.run person |> printfn "%s"

// produces the following code:
type Person =
    { FirstName: string
      LastName: string
      DateOfBirth: System.DateOnly }

let people =
    [ { FirstName = "Ada"
        LastName = "Lovelace"
        DateOfBirth = System.DateOnly(1815, 12, 10) } ]

The same tree with two-space indentation and the Aligned bracket style:

Gen.run(
    person,
    { Gen.DefaultConfig with
        IndentSize = 2
        MultilineBracketStyle = Aligned }
)
|> printfn "%s"

// produces the following code:
type Person =
  {
    FirstName: string
    LastName: string
    DateOfBirth: System.DateOnly
  }

let people =
  [
    {
      FirstName = "Ada"
      LastName = "Lovelace"
      DateOfBirth = System.DateOnly(1815, 12, 10)
    }
  ]
namespace Fabulous
namespace Fabulous.AST
type Ast = class end
namespace Fantomas
namespace Fantomas.FCS
namespace Fantomas.FCS.Text
Multiple items
static member Ast.Oak: unit -> CollectionBuilder<Fantomas.Core.SyntaxOak.Oak,'marker>

--------------------
module Oak from Fabulous.AST
static member Ast.AnonymousModule: unit -> CollectionBuilder<Fantomas.Core.SyntaxOak.ModuleOrNamespaceNode,Fantomas.Core.SyntaxOak.ModuleDecl>
static member Ast.Value: name: string * value: string * returnType: string -> WidgetBuilder<Fantomas.Core.SyntaxOak.BindingNode>
   (+0 other overloads)
static member Ast.Value: name: string * value: string * returnType: WidgetBuilder<Fantomas.Core.SyntaxOak.Type> -> WidgetBuilder<Fantomas.Core.SyntaxOak.BindingNode>
   (+0 other overloads)
static member Ast.Value: name: string * value: string -> WidgetBuilder<Fantomas.Core.SyntaxOak.BindingNode>
   (+0 other overloads)
static member Ast.Value: name: string * value: WidgetBuilder<Fantomas.Core.SyntaxOak.Constant> * returnType: string -> WidgetBuilder<Fantomas.Core.SyntaxOak.BindingNode>
   (+0 other overloads)
static member Ast.Value: name: string * value: WidgetBuilder<Fantomas.Core.SyntaxOak.Constant> * returnType: WidgetBuilder<Fantomas.Core.SyntaxOak.Type> -> WidgetBuilder<Fantomas.Core.SyntaxOak.BindingNode>
   (+0 other overloads)
static member Ast.Value: name: string * value: WidgetBuilder<Fantomas.Core.SyntaxOak.Constant> -> WidgetBuilder<Fantomas.Core.SyntaxOak.BindingNode>
   (+0 other overloads)
static member Ast.Value: name: string * value: WidgetBuilder<Fantomas.Core.SyntaxOak.Expr> * returnType: string -> WidgetBuilder<Fantomas.Core.SyntaxOak.BindingNode>
   (+0 other overloads)
static member Ast.Value: name: string * value: WidgetBuilder<Fantomas.Core.SyntaxOak.Expr> * returnType: WidgetBuilder<Fantomas.Core.SyntaxOak.Type> -> WidgetBuilder<Fantomas.Core.SyntaxOak.BindingNode>
   (+0 other overloads)
static member Ast.Value: name: string * value: WidgetBuilder<Fantomas.Core.SyntaxOak.Expr> -> WidgetBuilder<Fantomas.Core.SyntaxOak.BindingNode>
   (+0 other overloads)
static member Ast.Value: name: WidgetBuilder<Fantomas.Core.SyntaxOak.Constant> * value: WidgetBuilder<Fantomas.Core.SyntaxOak.Constant> * returnType: string -> WidgetBuilder<Fantomas.Core.SyntaxOak.BindingNode>
   (+0 other overloads)
static member Ast.Int: value: int -> WidgetBuilder<Fantomas.Core.SyntaxOak.Constant>
static member Ast.Int: unit -> WidgetBuilder<Fantomas.Core.SyntaxOak.Type>
type Gen = static member mkOak: root: WidgetBuilder<'node> -> 'node static member parse: source: string -> string + 1 overload static member run: oak: Oak -> string + 1 overload static member DefaultConfig: FormatConfig
<summary> Renders a widget tree to F# source and verifies the result. <c>mkOak</c> turns the root widget into a Fantomas node (recursively building its children), <c>run</c> formats that node to source (optionally with a config), and <c>parse</c> round-trips the source back through the parser to confirm it is syntactically valid. Designed for pipeline use, e.g. <c>widget |&gt; Gen.parse</c> or <c>widget |&gt; Gen.mkOak |&gt; Gen.run |&gt; Gen.parse</c>. </summary>
static member Gen.mkOak: root: WidgetBuilder<'node> -> 'node
static member Gen.run: oak: Fantomas.Core.SyntaxOak.Oak -> string
static member Gen.run: oak: Fantomas.Core.SyntaxOak.Oak * config: Fantomas.Core.FormatConfig -> string
val printfn: format: Printf.TextWriterFormat<'T> -> 'T
static member Ast.NoWarn: value: string -> WidgetBuilder<Fantomas.Core.SyntaxOak.ModuleDecl>
static member Ast.NoWarn: value: WidgetBuilder<Fantomas.Core.SyntaxOak.Constant> -> WidgetBuilder<Fantomas.Core.SyntaxOak.ModuleDecl>
static member Ast.NoWarn: args: string seq -> WidgetBuilder<Fantomas.Core.SyntaxOak.ModuleDecl>
static member Ast.NoWarn: args: WidgetBuilder<Fantomas.Core.SyntaxOak.Constant> seq -> WidgetBuilder<Fantomas.Core.SyntaxOak.ModuleDecl>
static member Ast.Namespace: name: string -> CollectionBuilder<Fantomas.Core.SyntaxOak.ModuleOrNamespaceNode,Fantomas.Core.SyntaxOak.ModuleDecl>
static member Ast.Module: name: string -> CollectionBuilder<Fantomas.Core.SyntaxOak.ModuleDecl,Fantomas.Core.SyntaxOak.ModuleDecl>
Multiple items
static member Ast.String: value: WidgetBuilder<Fantomas.Core.SyntaxOak.Constant> -> WidgetBuilder<Fantomas.Core.SyntaxOak.Constant>
static member Ast.String: value: string -> WidgetBuilder<Fantomas.Core.SyntaxOak.Constant>
static member Ast.String: unit -> WidgetBuilder<Fantomas.Core.SyntaxOak.Type>

--------------------
module String from Fabulous.AST

--------------------
module String from Microsoft.FSharp.Core
Multiple items
static member Ast.Newline: unit -> WidgetBuilder<Fantomas.Core.SyntaxOak.TriviaContent>

--------------------
module Newline from Fabulous.AST
namespace Fantomas.Core
module SyntaxOak from Fantomas.Core
Multiple items
static member Ast.Oak: unit -> CollectionBuilder<Oak,'marker>

--------------------
module Oak from Fabulous.AST

--------------------
type Oak = inherit NodeBase new: parsedHashDirectives: ParsedHashDirectiveNode list * modulesOrNamespaces: ModuleOrNamespaceNode list * m: range -> Oak override Children: Node array member ModulesOrNamespaces: ModuleOrNamespaceNode list member ParsedHashDirectives: ParsedHashDirectiveNode list

--------------------
new: parsedHashDirectives: ParsedHashDirectiveNode list * modulesOrNamespaces: ModuleOrNamespaceNode list * m: range -> Oak
static member Ast.AnonymousModule: unit -> CollectionBuilder<ModuleOrNamespaceNode,ModuleDecl>
static member Ast.Value: name: string * value: string * returnType: string -> WidgetBuilder<BindingNode>
   (+0 other overloads)
static member Ast.Value: name: string * value: string * returnType: WidgetBuilder<Type> -> WidgetBuilder<BindingNode>
   (+0 other overloads)
static member Ast.Value: name: string * value: string -> WidgetBuilder<BindingNode>
   (+0 other overloads)
static member Ast.Value: name: string * value: WidgetBuilder<Constant> * returnType: string -> WidgetBuilder<BindingNode>
   (+0 other overloads)
static member Ast.Value: name: string * value: WidgetBuilder<Constant> * returnType: WidgetBuilder<Type> -> WidgetBuilder<BindingNode>
   (+0 other overloads)
static member Ast.Value: name: string * value: WidgetBuilder<Constant> -> WidgetBuilder<BindingNode>
   (+0 other overloads)
static member Ast.Value: name: string * value: WidgetBuilder<Expr> * returnType: string -> WidgetBuilder<BindingNode>
   (+0 other overloads)
static member Ast.Value: name: string * value: WidgetBuilder<Expr> * returnType: WidgetBuilder<Type> -> WidgetBuilder<BindingNode>
   (+0 other overloads)
static member Ast.Value: name: string * value: WidgetBuilder<Expr> -> WidgetBuilder<BindingNode>
   (+0 other overloads)
static member Ast.Value: name: WidgetBuilder<Constant> * value: WidgetBuilder<Constant> * returnType: string -> WidgetBuilder<BindingNode>
   (+0 other overloads)
static member Ast.Int: value: int -> WidgetBuilder<Constant>
static member Ast.Int: unit -> WidgetBuilder<Type>
Multiple items
static member Ast.EscapeHatch: node: 'T -> WidgetBuilder<'T>

--------------------
module EscapeHatch from Fabulous.AST
Multiple items
module ModuleDecl from Fabulous.AST
<summary> Shared attribute definitions used across module declaration widget types </summary>

--------------------
type ModuleDecl = | OpenList of OpenListNode | HashDirectiveList of HashDirectiveListNode | Attributes of ModuleDeclAttributesNode | DeclExpr of Expr | Exception of ExceptionDefnNode | ExternBinding of ExternBindingNode | TopLevelBinding of BindingNode | ModuleAbbrev of ModuleAbbrevNode | NestedModule of NestedModuleNode | TypeDefn of TypeDefn ... static member Node: x: ModuleDecl -> Node
union case ModuleDecl.TopLevelBinding: BindingNode -> ModuleDecl
Multiple items
module BindingNode from Fabulous.AST

--------------------
type BindingNode = inherit NodeBase new: xmlDoc: XmlDocNode option * attributes: MultipleAttributeListNode option * leadingKeyword: MultipleTextsNode * isMutable: bool * inlineNode: SingleTextNode option * accessibility: SingleTextNode option * functionName: Choice<IdentListNode,Pattern> * genericTypeParameters: TyparDecls option * parameters: Pattern list * returnType: BindingReturnInfoNode option * equals: SingleTextNode * expr: Expr * inKeyword: SingleTextNode option * range: range -> BindingNode member Accessibility: SingleTextNode option member Attributes: MultipleAttributeListNode option override Children: Node array member Equals: SingleTextNode member Expr: Expr member FunctionName: Choice<IdentListNode,Pattern> member GenericTypeParameters: TyparDecls option member In: SingleTextNode option ...

--------------------
new: xmlDoc: XmlDocNode option * attributes: MultipleAttributeListNode option * leadingKeyword: MultipleTextsNode * isMutable: bool * inlineNode: SingleTextNode option * accessibility: SingleTextNode option * functionName: Choice<IdentListNode,Pattern> * genericTypeParameters: TyparDecls option * parameters: Pattern list * returnType: BindingReturnInfoNode option * equals: SingleTextNode * expr: Expr * inKeyword: SingleTextNode option * range: range -> BindingNode
union case Option.None: Option<'T>
Multiple items
type MultipleTextsNode = inherit NodeBase new: content: SingleTextNode list * range: range -> MultipleTextsNode override Children: Node array member Content: SingleTextNode list

--------------------
new: content: SingleTextNode list * range: range -> MultipleTextsNode
Multiple items
module SingleTextNode from Fabulous.AST

--------------------
type SingleTextNode = inherit NodeBase new: idText: string * range: range -> SingleTextNode member AddCursor: position: pos -> unit override ToString: unit -> string override Children: Node array member Text: string member TryGetCursor: pos option

--------------------
new: idText: string * range: range -> SingleTextNode
Multiple items
module Range from Fantomas.FCS.Text

--------------------
type Range = member ApplyLineDirectives: unit -> range member End: pos member EndColumn: int member EndLine: int member EndRange: range member FileName: string member IsSynthetic: bool member Start: pos member StartColumn: int member StartLine: int ...
val range0: range
union case Choice.Choice1Of2: 'T1 -> Choice<'T1,'T2>
Multiple items
type IdentListNode = inherit NodeBase new: content: IdentifierOrDot list * range: range -> IdentListNode override Children: Node array member Content: IdentifierOrDot list member IsEmpty: bool static member Empty: IdentListNode

--------------------
new: content: IdentifierOrDot list * range: range -> IdentListNode
type IdentifierOrDot = | Ident of SingleTextNode | KnownDot of SingleTextNode | UnknownDot member Range: range option
union case IdentifierOrDot.Ident: SingleTextNode -> IdentifierOrDot
Multiple items
static member Ast.List: unit -> WidgetBuilder<Type>

--------------------
module List from Fabulous.AST

--------------------
module List from Microsoft.FSharp.Collections

--------------------
type List<'T> = | op_Nil | op_ColonColon of Head: 'T * Tail: 'T list interface IReadOnlyList<'T> interface IReadOnlyCollection<'T> interface IEnumerable interface IEnumerable<'T> member GetReverseIndex: rank: int * offset: int -> int member GetSlice: startIndex: int option * endIndex: int option -> 'T list static member Cons: head: 'T * tail: 'T list -> 'T list member Head: 'T member IsEmpty: bool member Item: index: int -> 'T with get ...
property List.Empty: 'T list with get
Multiple items
union case TextSegment.Expr: WidgetBuilder<FillExprNode> * int -> TextSegment

--------------------
module Expr from Fabulous.AST

--------------------
type Expr = | Lazy of ExprLazyNode | Single of ExprSingleNode | Constant of Constant | Null of SingleTextNode | Quote of ExprQuoteNode | Typed of ExprTypedNode | New of ExprNewNode | Tuple of ExprTupleNode | StructTuple of ExprStructTupleNode | ArrayOrList of ExprArrayOrListNode ... static member Node: x: Expr -> Node member HasParentheses: bool
union case Expr.Constant: Constant -> Expr
Multiple items
static member Ast.Constant: value: string -> WidgetBuilder<Constant>

--------------------
module Constant from Fabulous.AST

--------------------
type Constant = | FromText of SingleTextNode | Unit of UnitNode | Measure of ConstantMeasureNode static member Node: c: Constant -> NodeBase
union case Constant.FromText: SingleTextNode -> Constant
static member Gen.run: oak: Oak -> string
static member Gen.run: oak: Oak * config: Fantomas.Core.FormatConfig -> string
static member Gen.parse: widget: WidgetBuilder<Oak> -> string
static member Gen.parse: source: string -> string
val person: Oak
Multiple items
static member Ast.Record: name: string -> CollectionBuilder<TypeDefn,FieldNode>

--------------------
module Record from Fabulous.AST
Multiple items
static member Ast.Field: name: string * fieldType: string -> WidgetBuilder<FieldNode>
static member Ast.Field: name: string * fieldType: WidgetBuilder<Type> -> WidgetBuilder<FieldNode>
static member Ast.Field: fieldType: string -> WidgetBuilder<FieldNode>
static member Ast.Field: fieldType: WidgetBuilder<Type> -> WidgetBuilder<FieldNode>

--------------------
module Field from Fabulous.AST
Multiple items
static member Ast.String: value: WidgetBuilder<Constant> -> WidgetBuilder<Constant>
static member Ast.String: value: string -> WidgetBuilder<Constant>
static member Ast.String: unit -> WidgetBuilder<Type>

--------------------
module String from Fantomas.Core

--------------------
module String from Fabulous.AST

--------------------
module String from Microsoft.FSharp.Core
static member Ast.LongIdent: value: string -> WidgetBuilder<Type>
static member Ast.LongIdent: value: string seq -> WidgetBuilder<Type>
static member Ast.ListExpr: value: string seq -> WidgetBuilder<Expr>
static member Ast.ListExpr: value: WidgetBuilder<Constant> seq -> WidgetBuilder<Expr>
static member Ast.ListExpr: value: WidgetBuilder<Expr> seq -> WidgetBuilder<Expr>
Multiple items
static member Ast.RecordExpr: unit -> CollectionBuilder<Expr,RecordFieldNode>
static member Ast.RecordExpr: fields: WidgetBuilder<RecordFieldNode> seq -> WidgetBuilder<Expr>
static member Ast.RecordExpr: copyInfo: string * fields: WidgetBuilder<RecordFieldNode> seq -> WidgetBuilder<Expr>
static member Ast.RecordExpr: copyInfo: WidgetBuilder<Constant> * fields: WidgetBuilder<RecordFieldNode> seq -> WidgetBuilder<Expr>
static member Ast.RecordExpr: copyInfo: WidgetBuilder<Expr> * fields: WidgetBuilder<RecordFieldNode> seq -> WidgetBuilder<Expr>

--------------------
module RecordExpr from Fabulous.AST
static member Ast.RecordFieldExpr: name: string * expr: string -> WidgetBuilder<RecordFieldNode>
static member Ast.RecordFieldExpr: name: string * expr: WidgetBuilder<Constant> -> WidgetBuilder<RecordFieldNode>
static member Ast.RecordFieldExpr: name: string * expr: WidgetBuilder<Expr> -> WidgetBuilder<RecordFieldNode>
static member Gen.run: oak: Oak -> string
static member Gen.run: oak: Oak * config: FormatConfig -> string
property Gen.DefaultConfig: FormatConfig with get
<summary> The config &lt;c&gt;run&lt;/c&gt; uses when none is given: Fantomas's defaults, except that multiline lists, arrays and records keep the &lt;c&gt;Cramped&lt;/c&gt; bracket style that Fabulous.AST has always produced (Fantomas 8 changed its own default to &lt;c&gt;Aligned&lt;/c&gt;). </summary>
type MultilineBracketStyle = | Cramped | Aligned | Stroustrup static member OfConfigString: cfgString: string -> MultilineBracketStyle option static member ToConfigString: cfg: MultilineBracketStyle -> string
union case MultilineBracketStyle.Aligned: MultilineBracketStyle

Type something to start searching.