DEV Community

jkone27
jkone27

Posted on

Learning some Fantomas AST

Made a test encoder/decored for a simple namespace declaration with Fantomas OAK Ast, see fantomas tools page

given

Oak (1,0-1,15)
        ModuleOrNamespaceNode (1,0-1,15)
            ModuleOrNamespaceHeaderNode (1,0-1,15)
                MultipleTextsNode (1,0-1,9)
                    namespace (1,0-1,9)
                IdentListNode (1,10-1,15)
                    A (1,10-1,11)
                    B (1,12-1,13)
                    C (1,14-1,15)
Enter fullscreen mode Exit fullscreen mode

it will output A.B.C (lowered)

you can run it as dotnet fsi script.fsx or in vscode with dotnet sdk installed. cheers!

let oakAst = 
    "hello.one.two"
    |> FantomasWriter.makeNamespace  []

oakAst 
|> FantomasReader.extractNsFromOak 
|> Option.iter (printfn "NAMESPACE: %s")
Enter fullscreen mode Exit fullscreen mode

Fabulous.AST

if you want to make your life easier and drop the custom write part for Oak, you can make use of the awesome Fabulous.AST DSL nuget package

#r "nuget:Fabulous.AST"

open System
open Fabulous.AST
open type Fabulous.AST.Ast

// using Fabulous.AST life becomes easier for writing oak
let oakAst = 
    Ast.Oak(){
        Ast.Namespace("hello.one.two") {}
    }
    |> Gen.mkOak

Enter fullscreen mode Exit fullscreen mode

Here a Gist of It

Top comments (0)