Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/KDL/Decoder/Arrow.hs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ import KDL.Types (
NodeList (..),
Value (..),
ValueData (..),
def,
)
import Numeric.Natural (Natural)
import Prelude hiding (any, fail, null)
Expand Down Expand Up @@ -627,7 +628,7 @@ instance DecodeNode Node where
, name = name
, entries = []
, children = Nothing
, format = Nothing
, ext = def
}

-- | Decode an argument in the node.
Expand Down Expand Up @@ -849,7 +850,7 @@ children decoder =
}
pure b
where
emptyNodeList = NodeList{nodes = [], format = Nothing}
emptyNodeList = NodeList{nodes = [], ext = def}

{----- Decoding ValueData -----}

Expand Down
30 changes: 23 additions & 7 deletions src/KDL/Parser.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE OverloadedRecordDot #-}
{-# LANGUAGE OverloadedStrings #-}

{-|
Expand All @@ -6,23 +7,38 @@ Implement the v2 parser specified at: https://kdl.dev/spec/#name-full-grammar
module KDL.Parser (
parse,
parseFile,

-- * Configurable parsing
ParseConfig (..),
parseWith,
parseFileWith,
) where

import Data.Bifunctor (first)
import Data.Default (def)
import Data.Text (Text)
import Data.Text qualified as Text
import Data.Text.IO qualified as Text
import KDL.Parser.Internal (p_document)
import KDL.Parser.Internal (
ParseConfig (..),
p_document,
runParser,
)
import KDL.Types (Document)
import Text.Megaparsec qualified as Megaparsec

parse :: Text -> Either Text Document
parse = parse' ""
parse = parseWith def

parseFile :: FilePath -> IO (Either Text Document)
parseFile = parseFileWith def

parse' :: FilePath -> Text -> Either Text Document
parse' fp input =
parseWith :: ParseConfig -> Text -> Either Text Document
parseWith config input =
first (Text.strip . Text.pack . Megaparsec.errorBundlePretty) $
Megaparsec.parse p_document fp input
runParser config p_document input

parseFile :: FilePath -> IO (Either Text Document)
parseFile fp = parse' fp <$> Text.readFile fp
parseFileWith :: ParseConfig -> FilePath -> IO (Either Text Document)
parseFileWith config0 fp = parseWith config <$> Text.readFile fp
where
config = config0{filepath = fp}
Loading