Skip to content

Commit bd62c1f

Browse files
committed
Parametrise CDDL AST over decorations
1 parent 6c7d7cb commit bd62c1f

File tree

10 files changed

+439
-347
lines changed

10 files changed

+439
-347
lines changed

src/Codec/CBOR/Cuddle/CDDL.hs

Lines changed: 159 additions & 86 deletions
Large diffs are not rendered by default.

src/Codec/CBOR/Cuddle/CDDL/Prelude.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
{-# LANGUAGE DataKinds #-}
12
{-# LANGUAGE OverloadedStrings #-}
23

34
module Codec.CBOR.Cuddle.CDDL.Prelude (prependPrelude) where
45

56
import Codec.CBOR.Cuddle.CDDL (CDDL (..))
7+
import Codec.CBOR.Cuddle.Comments (Comment)
68
import Codec.CBOR.Cuddle.Parser (pCDDL)
79
import Text.Megaparsec (errorBundlePretty, parse)
810

911
-- TODO switch to quasiquotes
10-
cddlPrelude :: CDDL
12+
cddlPrelude :: CDDL Comment
1113
cddlPrelude =
1214
either (error . errorBundlePretty) id $
1315
parse
@@ -57,5 +59,5 @@ cddlPrelude =
5759
\ null = nil \
5860
\ undefined = #7.23"
5961

60-
prependPrelude :: CDDL -> CDDL
62+
prependPrelude :: CDDL Comment -> CDDL Comment
6163
prependPrelude = (cddlPrelude <>)

src/Codec/CBOR/Cuddle/CDDL/Resolve.hs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ import Optics.Core
7070
-- 1. Rule extensions
7171
--------------------------------------------------------------------------------
7272

73-
type CDDLMap = Map.Map Name (Parametrised TypeOrGroup)
73+
type CDDLMap i = Map.Map Name (Parametrised (TypeOrGroup i))
7474

7575
type Parametrised a = ParametrisedWith [Name] a
7676

@@ -82,24 +82,24 @@ parameters :: Parametrised a -> [Name]
8282
parameters (Unparametrised _) = mempty
8383
parameters (Parametrised _ ps) = ps
8484

85-
asMap :: CDDL -> CDDLMap
85+
asMap :: CDDL i -> CDDLMap i
8686
asMap cddl = foldl' go Map.empty rules
8787
where
8888
rules = cddlTopLevel cddl
8989
go x (TopLevelComment _) = x
9090
go x (TopLevelRule r) = assignOrExtend x r
9191

92-
assignOrExtend :: CDDLMap -> Rule -> CDDLMap
92+
assignOrExtend :: CDDLMap i -> Rule i -> CDDLMap i
9393
assignOrExtend m (Rule n gps assign tog _) = case assign of
9494
-- Equals assignment
9595
AssignEq -> Map.insert n (toParametrised tog gps) m
9696
AssignExt -> Map.alter (extend tog gps) n m
9797

9898
extend ::
99-
TypeOrGroup ->
99+
TypeOrGroup i ->
100100
Maybe GenericParam ->
101-
Maybe (Parametrised TypeOrGroup) ->
102-
Maybe (Parametrised TypeOrGroup)
101+
Maybe (Parametrised (TypeOrGroup i)) ->
102+
Maybe (Parametrised (TypeOrGroup i))
103103
extend tog _gps (Just existing) = case (underlying existing, tog) of
104104
(TOGType _, TOGType (Type0 new)) ->
105105
Just $
@@ -139,23 +139,23 @@ deriving instance Show (CTreeRoot OrRef)
139139
-- | Build a CTree incorporating references.
140140
--
141141
-- This translation cannot fail.
142-
buildRefCTree :: CDDLMap -> RefCTree
142+
buildRefCTree :: CDDLMap i -> RefCTree
143143
buildRefCTree rules = CTreeRoot $ fmap toCTreeRule rules
144144
where
145145
toCTreeRule ::
146-
Parametrised TypeOrGroup ->
146+
Parametrised (TypeOrGroup i) ->
147147
ParametrisedWith [Name] (CTree.Node OrRef)
148148
toCTreeRule = fmap toCTreeTOG
149149

150-
toCTreeTOG :: TypeOrGroup -> CTree.Node OrRef
150+
toCTreeTOG :: TypeOrGroup i -> CTree.Node OrRef
151151
toCTreeTOG (TOGType t0) = toCTreeT0 t0
152152
toCTreeTOG (TOGGroup ge) = toCTreeGroupEntry ge
153153

154-
toCTreeT0 :: Type0 -> CTree.Node OrRef
154+
toCTreeT0 :: Type0 i -> CTree.Node OrRef
155155
toCTreeT0 (Type0 (t1 NE.:| [])) = toCTreeT1 t1
156156
toCTreeT0 (Type0 xs) = It . CTree.Choice $ toCTreeT1 <$> xs
157157

158-
toCTreeT1 :: Type1 -> CTree.Node OrRef
158+
toCTreeT1 :: Type1 i -> CTree.Node OrRef
159159
toCTreeT1 (Type1 t2 Nothing _) = toCTreeT2 t2
160160
toCTreeT1 (Type1 t2 (Just (op, t2')) _) = case op of
161161
RangeOp bound ->
@@ -173,7 +173,7 @@ buildRefCTree rules = CTreeRoot $ fmap toCTreeRule rules
173173
, CTree.controller = toCTreeT2 t2'
174174
}
175175

176-
toCTreeT2 :: Type2 -> CTree.Node OrRef
176+
toCTreeT2 :: Type2 i -> CTree.Node OrRef
177177
toCTreeT2 (T2Value v) = It $ CTree.Literal v
178178
toCTreeT2 (T2Name n garg) =
179179
Ref n (fromGenArgs garg)
@@ -215,35 +215,35 @@ buildRefCTree rules = CTreeRoot $ fmap toCTreeRule rules
215215
toCTreeDataItem _ =
216216
It $ CTree.Postlude PTAny
217217

218-
toCTreeGroupEntry :: GroupEntry -> CTree.Node OrRef
219-
toCTreeGroupEntry (GroupEntry (Just occi) _ (GEType mmkey t0)) =
218+
toCTreeGroupEntry :: GroupEntry i -> CTree.Node OrRef
219+
toCTreeGroupEntry (GroupEntry (Just occi) (GEType mmkey t0) _) =
220220
It $
221221
CTree.Occur
222222
{ CTree.item = toKVPair mmkey t0
223223
, CTree.occurs = occi
224224
}
225-
toCTreeGroupEntry (GroupEntry Nothing _ (GEType mmkey t0)) = toKVPair mmkey t0
226-
toCTreeGroupEntry (GroupEntry (Just occi) _ (GERef n margs)) =
225+
toCTreeGroupEntry (GroupEntry Nothing (GEType mmkey t0) _) = toKVPair mmkey t0
226+
toCTreeGroupEntry (GroupEntry (Just occi) (GERef n margs) _) =
227227
It $
228228
CTree.Occur
229229
{ CTree.item = Ref n (fromGenArgs margs)
230230
, CTree.occurs = occi
231231
}
232-
toCTreeGroupEntry (GroupEntry Nothing _ (GERef n margs)) = Ref n (fromGenArgs margs)
233-
toCTreeGroupEntry (GroupEntry (Just occi) _ (GEGroup g)) =
232+
toCTreeGroupEntry (GroupEntry Nothing (GERef n margs) _) = Ref n (fromGenArgs margs)
233+
toCTreeGroupEntry (GroupEntry (Just occi) (GEGroup g) _) =
234234
It $
235235
CTree.Occur
236236
{ CTree.item = groupToGroup g
237237
, CTree.occurs = occi
238238
}
239-
toCTreeGroupEntry (GroupEntry Nothing _ (GEGroup g)) = groupToGroup g
239+
toCTreeGroupEntry (GroupEntry Nothing (GEGroup g) _) = groupToGroup g
240240

241-
fromGenArgs :: Maybe GenericArg -> [CTree.Node OrRef]
241+
fromGenArgs :: Maybe (GenericArg i) -> [CTree.Node OrRef]
242242
fromGenArgs = maybe [] (\(GenericArg xs) -> NE.toList $ fmap toCTreeT1 xs)
243243

244244
-- Interpret a group as an enumeration. Note that we float out the
245245
-- choice options
246-
toCTreeEnum :: Group -> CTree.Node OrRef
246+
toCTreeEnum :: Group i -> CTree.Node OrRef
247247
toCTreeEnum (Group (a NE.:| [])) =
248248
It . CTree.Enum . It . CTree.Group $ toCTreeGroupEntry <$> gcGroupEntries a
249249
toCTreeEnum (Group xs) =
@@ -253,14 +253,14 @@ buildRefCTree rules = CTreeRoot $ fmap toCTreeRule rules
253253
groupEntries = fmap gcGroupEntries xs
254254

255255
-- Embed a group in another group, again floating out the choice options
256-
groupToGroup :: Group -> CTree.Node OrRef
256+
groupToGroup :: Group i -> CTree.Node OrRef
257257
groupToGroup (Group (a NE.:| [])) =
258258
It . CTree.Group $ fmap toCTreeGroupEntry (gcGroupEntries a)
259259
groupToGroup (Group xs) =
260260
It . CTree.Choice $
261261
fmap (It . CTree.Group . fmap toCTreeGroupEntry) (gcGroupEntries <$> xs)
262262

263-
toKVPair :: Maybe MemberKey -> Type0 -> CTree.Node OrRef
263+
toKVPair :: Maybe (MemberKey i) -> Type0 i -> CTree.Node OrRef
264264
toKVPair Nothing t0 = toCTreeT0 t0
265265
toKVPair (Just mkey) t0 =
266266
It $
@@ -272,7 +272,7 @@ buildRefCTree rules = CTreeRoot $ fmap toCTreeRule rules
272272
}
273273

274274
-- Interpret a group as a map. Note that we float out the choice options
275-
toCTreeMap :: Group -> CTree.Node OrRef
275+
toCTreeMap :: Group i -> CTree.Node OrRef
276276
toCTreeMap (Group (a NE.:| [])) = It . CTree.Map $ fmap toCTreeGroupEntry (gcGroupEntries a)
277277
toCTreeMap (Group xs) =
278278
It
@@ -281,14 +281,14 @@ buildRefCTree rules = CTreeRoot $ fmap toCTreeRule rules
281281

282282
-- Interpret a group as an array. Note that we float out the choice
283283
-- options
284-
toCTreeArray :: Group -> CTree.Node OrRef
284+
toCTreeArray :: Group i -> CTree.Node OrRef
285285
toCTreeArray (Group (a NE.:| [])) =
286286
It . CTree.Array $ fmap toCTreeGroupEntry (gcGroupEntries a)
287287
toCTreeArray (Group xs) =
288288
It . CTree.Choice $
289289
fmap (It . CTree.Array . fmap toCTreeGroupEntry) (gcGroupEntries <$> xs)
290290

291-
toCTreeMemberKey :: MemberKey -> CTree.Node OrRef
291+
toCTreeMemberKey :: MemberKey i -> CTree.Node OrRef
292292
toCTreeMemberKey (MKValue v) = It $ CTree.Literal v
293293
toCTreeMemberKey (MKBareword (Name n _)) = It $ CTree.Literal (Value (VText n) mempty)
294294
toCTreeMemberKey (MKType t1) = toCTreeT1 t1
@@ -556,7 +556,7 @@ buildMonoCTree (CTreeRoot ct) = do
556556
-- Combined resolution
557557
--------------------------------------------------------------------------------
558558

559-
fullResolveCDDL :: CDDL -> Either NameResolutionFailure (CTreeRoot' Identity MonoRef)
559+
fullResolveCDDL :: CDDL i -> Either NameResolutionFailure (CTreeRoot' Identity MonoRef)
560560
fullResolveCDDL cddl = do
561561
let refCTree = buildRefCTree (asMap cddl)
562562
rCTree <- buildResolvedCTree refCTree

src/Codec/CBOR/Cuddle/Huddle.hs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ import GHC.Exts (IsList (Item, fromList, toList))
108108
import GHC.Generics (Generic)
109109
import Optics.Core (lens, view, (%~), (&), (.~), (^.))
110110
import Prelude hiding ((/))
111+
import Codec.CBOR.Cuddle.Comments (Comment)
111112

112113
data Named a = Named
113114
{ name :: T.Text
@@ -432,7 +433,7 @@ unconstrained v = Constrained (CValue v) def []
432433
-- | A constraint on a 'Value' is something applied via CtlOp or RangeOp on a
433434
-- Type2, forming a Type1.
434435
data ValueConstraint a = ValueConstraint
435-
{ applyConstraint :: C.Type2 -> C.Type1
436+
{ applyConstraint :: C.Type2 Comment -> C.Type1 Comment
436437
, showConstraint :: String
437438
}
438439

@@ -462,7 +463,7 @@ instance IsSizeable CGRefType
462463

463464
-- | Things which can be used on the RHS of the '.size' operator.
464465
class IsSize a where
465-
sizeAsCDDL :: a -> C.Type2
466+
sizeAsCDDL :: a -> C.Type2 Comment
466467
sizeAsString :: a -> String
467468

468469
instance IsSize Word where
@@ -1062,15 +1063,15 @@ collectFromInit rules =
10621063
--------------------------------------------------------------------------------
10631064

10641065
-- | Convert from Huddle to CDDL, generating a top level root element.
1065-
toCDDL :: Huddle -> CDDL
1066+
toCDDL :: Huddle -> CDDL Comment
10661067
toCDDL = toCDDL' True
10671068

10681069
-- | Convert from Huddle to CDDL, skipping a root element.
1069-
toCDDLNoRoot :: Huddle -> CDDL
1070+
toCDDLNoRoot :: Huddle -> CDDL Comment
10701071
toCDDLNoRoot = toCDDL' False
10711072

10721073
-- | Convert from Huddle to CDDL for the purpose of pretty-printing.
1073-
toCDDL' :: Bool -> Huddle -> CDDL
1074+
toCDDL' :: Bool -> Huddle -> CDDL Comment
10741075
toCDDL' mkPseudoRoot hdl =
10751076
C.fromRules
10761077
$ ( if mkPseudoRoot
@@ -1082,12 +1083,12 @@ toCDDL' mkPseudoRoot hdl =
10821083
toCDDLItem (HIRule r) = toCDDLRule r
10831084
toCDDLItem (HIGroup g) = toCDDLGroup g
10841085
toCDDLItem (HIGRule g) = toGenRuleDef g
1085-
toTopLevelPseudoRoot :: [Rule] -> C.Rule
1086+
toTopLevelPseudoRoot :: [Rule] -> C.Rule Comment
10861087
toTopLevelPseudoRoot topRs =
10871088
toCDDLRule $
10881089
comment "Pseudo-rule introduced by Cuddle to collect root elements" $
10891090
"huddle_root_defs" =:= arr (fromList (fmap a topRs))
1090-
toCDDLRule :: Rule -> C.Rule
1091+
toCDDLRule :: Rule -> C.Rule Comment
10911092
toCDDLRule (Named n t0 c) =
10921093
(\x -> C.Rule (C.Name n mempty) Nothing C.AssignEq x (foldMap C.Comment c))
10931094
. C.TOGType
@@ -1103,18 +1104,18 @@ toCDDL' mkPseudoRoot hdl =
11031104
toCDDLValue' (LText t) = C.VText t
11041105
toCDDLValue' (LBytes b) = C.VBytes b
11051106

1106-
mapToCDDLGroup :: Map -> C.Group
1107+
mapToCDDLGroup :: Map -> C.Group Comment
11071108
mapToCDDLGroup xs = C.Group $ mapChoiceToCDDL <$> choiceToNE xs
11081109

1109-
mapChoiceToCDDL :: MapChoice -> C.GrpChoice
1110+
mapChoiceToCDDL :: MapChoice -> C.GrpChoice Comment
11101111
mapChoiceToCDDL (MapChoice entries) = C.GrpChoice (fmap mapEntryToCDDL entries) mempty
11111112

1112-
mapEntryToCDDL :: MapEntry -> C.GroupEntry
1113+
mapEntryToCDDL :: MapEntry -> C.GroupEntry Comment
11131114
mapEntryToCDDL (MapEntry k v occ cmnt) =
11141115
C.GroupEntry
11151116
(toOccurrenceIndicator occ)
1116-
cmnt
11171117
(C.GEType (Just $ toMemberKey k) (toCDDLType0 v))
1118+
cmnt
11181119

11191120
toOccurrenceIndicator :: Occurs -> Maybe C.OccurrenceIndicator
11201121
toOccurrenceIndicator (Occurs Nothing Nothing) = Nothing
@@ -1123,7 +1124,7 @@ toCDDL' mkPseudoRoot hdl =
11231124
toOccurrenceIndicator (Occurs (Just 1) Nothing) = Just C.OIOneOrMore
11241125
toOccurrenceIndicator (Occurs lb ub) = Just $ C.OIBounded lb ub
11251126

1126-
toCDDLType1 :: Type2 -> C.Type1
1127+
toCDDLType1 :: Type2 -> C.Type1 Comment
11271128
toCDDLType1 = \case
11281129
T2Constrained (Constrained x constr _) ->
11291130
-- TODO Need to handle choices at the top level
@@ -1142,26 +1143,26 @@ toCDDL' mkPseudoRoot hdl =
11421143
T2Generic g -> C.Type1 (toGenericCall g) Nothing mempty
11431144
T2GenericRef (GRef n) -> C.Type1 (C.T2Name (C.Name n mempty) Nothing) Nothing mempty
11441145

1145-
toMemberKey :: Key -> C.MemberKey
1146+
toMemberKey :: Key -> C.MemberKey Comment
11461147
toMemberKey (LiteralKey (Literal (LText t) _)) = C.MKBareword (C.Name t mempty)
11471148
toMemberKey (LiteralKey v) = C.MKValue $ toCDDLValue v
11481149
toMemberKey (TypeKey t) = C.MKType (toCDDLType1 t)
11491150

1150-
toCDDLType0 :: Type0 -> C.Type0
1151+
toCDDLType0 :: Type0 -> C.Type0 Comment
11511152
toCDDLType0 = C.Type0 . fmap toCDDLType1 . choiceToNE
11521153

1153-
arrayToCDDLGroup :: Array -> C.Group
1154+
arrayToCDDLGroup :: Array -> C.Group Comment
11541155
arrayToCDDLGroup xs = C.Group $ arrayChoiceToCDDL <$> choiceToNE xs
11551156

1156-
arrayChoiceToCDDL :: ArrayChoice -> C.GrpChoice
1157+
arrayChoiceToCDDL :: ArrayChoice -> C.GrpChoice Comment
11571158
arrayChoiceToCDDL (ArrayChoice entries cmt) = C.GrpChoice (fmap arrayEntryToCDDL entries) cmt
11581159

1159-
arrayEntryToCDDL :: ArrayEntry -> C.GroupEntry
1160+
arrayEntryToCDDL :: ArrayEntry -> C.GroupEntry Comment
11601161
arrayEntryToCDDL (ArrayEntry k v occ cmnt) =
11611162
C.GroupEntry
11621163
(toOccurrenceIndicator occ)
1163-
cmnt
11641164
(C.GEType (fmap toMemberKey k) (toCDDLType0 v))
1165+
cmnt
11651166

11661167
toCDDLPostlude :: Value a -> C.Name
11671168
toCDDLPostlude VBool = C.Name "bool" mempty
@@ -1181,7 +1182,7 @@ toCDDL' mkPseudoRoot hdl =
11811182
CRef r -> C.Name (name r) mempty
11821183
CGRef (GRef n) -> C.Name n mempty
11831184

1184-
toCDDLRanged :: Ranged -> C.Type1
1185+
toCDDLRanged :: Ranged -> C.Type1 Comment
11851186
toCDDLRanged (Unranged x) =
11861187
C.Type1 (C.T2Value $ toCDDLValue x) Nothing mempty
11871188
toCDDLRanged (Ranged lb ub rop) =
@@ -1190,18 +1191,18 @@ toCDDL' mkPseudoRoot hdl =
11901191
(Just (C.RangeOp rop, toCDDLRangeBound ub))
11911192
mempty
11921193

1193-
toCDDLRangeBound :: RangeBound -> C.Type2
1194+
toCDDLRangeBound :: RangeBound -> C.Type2 Comment
11941195
toCDDLRangeBound (RangeBoundLiteral l) = C.T2Value $ toCDDLValue l
11951196
toCDDLRangeBound (RangeBoundRef (Named n _ _)) = C.T2Name (C.Name n mempty) Nothing
11961197

1197-
toCDDLGroup :: Named Group -> C.Rule
1198+
toCDDLGroup :: Named Group -> C.Rule Comment
11981199
toCDDLGroup (Named n (Group t0s) c) =
11991200
C.Rule
12001201
(C.Name n mempty)
12011202
Nothing
12021203
C.AssignEq
12031204
( C.TOGGroup
1204-
. C.GroupEntry Nothing mempty
1205+
. (\x -> C.GroupEntry Nothing x mempty)
12051206
. C.GEGroup
12061207
. C.Group
12071208
. (NE.:| [])
@@ -1212,13 +1213,13 @@ toCDDL' mkPseudoRoot hdl =
12121213
)
12131214
(foldMap C.Comment c)
12141215

1215-
toGenericCall :: GRuleCall -> C.Type2
1216+
toGenericCall :: GRuleCall -> C.Type2 Comment
12161217
toGenericCall (Named n gr _) =
12171218
C.T2Name
12181219
(C.Name n mempty)
12191220
(Just . C.GenericArg $ fmap toCDDLType1 (args gr))
12201221

1221-
toGenRuleDef :: GRuleDef -> C.Rule
1222+
toGenRuleDef :: GRuleDef -> C.Rule Comment
12221223
toGenRuleDef (Named n gr c) =
12231224
C.Rule
12241225
(C.Name n mempty)

0 commit comments

Comments
 (0)