|
1 | | -{-# LANGUAGE OverloadedStrings #-} |
2 | | -{-# LANGUAGE RecordWildCards #-} |
3 | | - |
| 1 | +{-| |
| 2 | +Implement the v2 parser specified at: https://kdl.dev/spec/#name-full-grammar |
| 3 | +-} |
4 | 4 | module KDL.Parser ( |
5 | 5 | parse, |
6 | 6 | parseFile, |
7 | 7 | ) where |
8 | 8 |
|
9 | | -import Data.Map qualified as Map |
| 9 | +import Data.Bifunctor (first) |
10 | 10 | import Data.Text (Text) |
11 | 11 | import Data.Text qualified as Text |
12 | 12 | 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 |
25 | 16 |
|
26 | | --- TODO: Implement our own parser that implements the v2.0.0 spec + preserves formatting and comments |
27 | 17 | parse :: Text -> Either Text Document |
28 | 18 | 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 |
89 | 21 |
|
90 | 22 | parseFile :: FilePath -> IO (Either Text Document) |
91 | 23 | parseFile = fmap parse . Text.readFile |
0 commit comments