Skip to content

Commit 28e1d0e

Browse files
Add span information if requested (#20)
* Nest format field under Extension type * Break out ParseConfig * Add Span to ext fields
1 parent 3a36e38 commit 28e1d0e

11 files changed

Lines changed: 664 additions & 162 deletions

File tree

src/KDL/Decoder/Arrow.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ import KDL.Types (
128128
NodeList (..),
129129
Value (..),
130130
ValueData (..),
131+
def,
131132
)
132133
import Numeric.Natural (Natural)
133134
import Prelude hiding (any, fail, null)
@@ -627,7 +628,7 @@ instance DecodeNode Node where
627628
, name = name
628629
, entries = []
629630
, children = Nothing
630-
, format = Nothing
631+
, ext = def
631632
}
632633

633634
-- | Decode an argument in the node.
@@ -849,7 +850,7 @@ children decoder =
849850
}
850851
pure b
851852
where
852-
emptyNodeList = NodeList{nodes = [], format = Nothing}
853+
emptyNodeList = NodeList{nodes = [], ext = def}
853854

854855
{----- Decoding ValueData -----}
855856

src/KDL/Parser.hs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE OverloadedRecordDot #-}
12
{-# LANGUAGE OverloadedStrings #-}
23

34
{-|
@@ -6,23 +7,38 @@ Implement the v2 parser specified at: https://kdl.dev/spec/#name-full-grammar
67
module KDL.Parser (
78
parse,
89
parseFile,
10+
11+
-- * Configurable parsing
12+
ParseConfig (..),
13+
parseWith,
14+
parseFileWith,
915
) where
1016

1117
import Data.Bifunctor (first)
18+
import Data.Default (def)
1219
import Data.Text (Text)
1320
import Data.Text qualified as Text
1421
import Data.Text.IO qualified as Text
15-
import KDL.Parser.Internal (p_document)
22+
import KDL.Parser.Internal (
23+
ParseConfig (..),
24+
p_document,
25+
runParser,
26+
)
1627
import KDL.Types (Document)
1728
import Text.Megaparsec qualified as Megaparsec
1829

1930
parse :: Text -> Either Text Document
20-
parse = parse' ""
31+
parse = parseWith def
32+
33+
parseFile :: FilePath -> IO (Either Text Document)
34+
parseFile = parseFileWith def
2135

22-
parse' :: FilePath -> Text -> Either Text Document
23-
parse' fp input =
36+
parseWith :: ParseConfig -> Text -> Either Text Document
37+
parseWith config input =
2438
first (Text.strip . Text.pack . Megaparsec.errorBundlePretty) $
25-
Megaparsec.parse p_document fp input
39+
runParser config p_document input
2640

27-
parseFile :: FilePath -> IO (Either Text Document)
28-
parseFile fp = parse' fp <$> Text.readFile fp
41+
parseFileWith :: ParseConfig -> FilePath -> IO (Either Text Document)
42+
parseFileWith config0 fp = parseWith config <$> Text.readFile fp
43+
where
44+
config = config0{filepath = fp}

0 commit comments

Comments
 (0)