~kmaasrud/DjotNet

1e97f3bbc87d7aed8d4df2d0525789520f278bc1 — kmaasrud 1 year, 16 days ago fc863e7 main
create simple line-by-line parser
3 files changed, 53 insertions(+), 1 deletions(-)

A .config/dotnet-tools.json
M src/Library/Program.fs
A src/Library/example.dj
A .config/dotnet-tools.json => .config/dotnet-tools.json +12 -0
@@ 0,0 1,12 @@
{
  "version": 1,
  "isRoot": true,
  "tools": {
    "fantomas": {
      "version": "5.2.1",
      "commands": [
        "fantomas"
      ]
    }
  }
}
\ No newline at end of file

M src/Library/Program.fs => src/Library/Program.fs +36 -1
@@ 1,1 1,36 @@
module Main = printfn "Hello from DjotNet"
open System

type Node =
    | Doc of List<Node>
    | Heading of int * string
    | Paragraph of string
    | Blank

module Node =
    let print (node: Node) : string =
        match node with
        | Doc _ -> "Doc"
        | Heading(level, text) -> $"Heading{level}({text})"
        | Paragraph text -> $"Paragraph({text})"
        | Blank -> ""

module Parser =
    let parseLine (line: string) : Node =
        match line with
        | "" -> Blank
        | _ ->
            match line.[0] with
            | '#' ->
                let level = line |> Seq.takeWhile ((=) '#') |> Seq.length
                let text = line.[(level + 1) ..]
                Heading(level, text)
            | _ -> Paragraph(line)

    let parse = Array.map parseLine

module Main =
    [<EntryPoint>]
    let main filename =
        let content = IO.File.ReadAllLines(Array.head filename)
        content |> Parser.parse |> Array.map Node.print |> Array.iter (printfn "%s")
        0

A src/Library/example.dj => src/Library/example.dj +5 -0
@@ 0,0 1,5 @@
# This is a header

This is a paragraph

## This is another header
\ No newline at end of file