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
67module KDL.Parser (
78 parse ,
89 parseFile ,
10+
11+ -- * Configurable parsing
12+ ParseConfig (.. ),
13+ parseWith ,
14+ parseFileWith ,
915) where
1016
1117import Data.Bifunctor (first )
18+ import Data.Default (def )
1219import Data.Text (Text )
1320import Data.Text qualified as Text
1421import 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+ )
1627import KDL.Types (Document )
1728import Text.Megaparsec qualified as Megaparsec
1829
1930parse :: 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