Skip to content

Commit bf4a963

Browse files
Implement v2 parser
1 parent 221f731 commit bf4a963

12 files changed

Lines changed: 1213 additions & 675 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## Unreleased
22

3+
* Implement KDL v2 parser
34
* Implement `KDL.render`, which is format-preserving
45
* Improve rendering parse errors
56
* Include filepath in error messages when `decodeFileWith` fails

kdl-hs.cabal

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,9 @@ library
3535
KDL.Decoder.Monad
3636
KDL.Decoder.Schema
3737
KDL.Parser
38+
KDL.Parser.Internal
3839
KDL.Render
3940
KDL.Types
40-
other-modules:
41-
KDL.Parser.Hustle
42-
KDL.Parser.Hustle.Formatter
43-
KDL.Parser.Hustle.Internal
44-
KDL.Parser.Hustle.Parser
45-
KDL.Parser.Hustle.Types
4641
build-depends:
4742
base < 5
4843
, containers
@@ -89,6 +84,7 @@ test-suite kdl-tests
8984
, filepath
9085
, kdl-hs
9186
, process
87+
, scientific
9288
, skeletest
9389
, temporary
9490
, text

src/KDL/Parser.hs

Lines changed: 9 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,23 @@
1-
{-# LANGUAGE OverloadedStrings #-}
2-
{-# LANGUAGE RecordWildCards #-}
3-
1+
{-|
2+
Implement the v2 parser specified at: https://kdl.dev/spec/#name-full-grammar
3+
-}
44
module KDL.Parser (
55
parse,
66
parseFile,
77
) where
88

9-
import Data.Map qualified as Map
9+
import Data.Bifunctor (first)
1010
import Data.Text (Text)
1111
import Data.Text qualified as Text
1212
import Data.Text.IO qualified as Text
13-
import KDL.Parser.Hustle qualified as Hustle
14-
import KDL.Types (
15-
Ann (..),
16-
Document,
17-
Entry (..),
18-
Identifier (..),
19-
Node (..),
20-
NodeList (..),
21-
Value (..),
22-
ValueData (..),
23-
ValueFormat (..),
24-
)
13+
import KDL.Parser.Internal (p_document)
14+
import KDL.Types (Document)
15+
import Text.Megaparsec qualified as Megaparsec
2516

26-
-- TODO: Implement our own parser that implements the v2.0.0 spec + preserves formatting and comments
2717
parse :: Text -> Either Text Document
2818
parse input =
29-
case Hustle.parse Hustle.document "" input of
30-
Left e -> Left . Text.strip . Text.pack . Hustle.errorBundlePretty $ e
31-
Right (Hustle.Document nodes) -> Right $ fromNodes nodes
32-
where
33-
fromNodes nodes =
34-
NodeList
35-
{ nodes = map fromNode nodes
36-
, format = Nothing
37-
}
38-
39-
fromAnn identifier =
40-
Ann
41-
{ identifier = fromIdentifier identifier
42-
, format = Nothing
43-
}
44-
45-
fromNode Hustle.Node{..} =
46-
Node
47-
{ ann = fromAnn <$> nodeAnn
48-
, name = fromIdentifier nodeName
49-
, entries = map fromArgEntry nodeArgs <> map fromPropEntry (Map.toList nodeProps)
50-
, children = Just $ fromNodes nodeChildren
51-
, format = Nothing
52-
}
53-
54-
fromArgEntry v =
55-
Entry
56-
{ name = Nothing
57-
, value = fromValue v
58-
, format = Nothing
59-
}
60-
61-
fromPropEntry (name, v) =
62-
Entry
63-
{ name = Just $ fromIdentifier name
64-
, value = fromValue v
65-
, format = Nothing
66-
}
67-
68-
fromValue Hustle.Value{..} =
69-
Value
70-
{ ann = fromAnn <$> valueAnn
71-
, data_ =
72-
case valueExp of
73-
Hustle.StringValue s -> Text s
74-
Hustle.IntegerValue x -> Number (fromInteger x)
75-
Hustle.SciValue x -> Number x
76-
Hustle.BooleanValue x -> Bool x
77-
Hustle.NullValue -> Null
78-
, format =
79-
case valueExp of
80-
Hustle.IntegerValue x -> Just ValueFormat{repr = Text.pack $ show x}
81-
_ -> Nothing
82-
}
83-
84-
fromIdentifier (Hustle.Identifier s) =
85-
Identifier
86-
{ value = s
87-
, format = Nothing
88-
}
19+
first (Text.strip . Text.pack . Megaparsec.errorBundlePretty) $
20+
Megaparsec.parse p_document "" input
8921

9022
parseFile :: FilePath -> IO (Either Text Document)
9123
parseFile = fmap parse . Text.readFile

src/KDL/Parser/Hustle.hs

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/KDL/Parser/Hustle/Formatter.hs

Lines changed: 0 additions & 83 deletions
This file was deleted.

src/KDL/Parser/Hustle/Internal.hs

Lines changed: 0 additions & 96 deletions
This file was deleted.

0 commit comments

Comments
 (0)