Skip to content

Commit 79d6a78

Browse files
committed
Generate a top-level definition at the start
1 parent 41ecc4a commit 79d6a78

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@
1313

1414
* Improvements in the pretty printer - various groups should be formatted more
1515
cleanly
16+
17+
## 0.3.1.0 -- 2024-08-15
18+
19+
* `collectFrom` now adds a first definition in the generated Huddle referencing
20+
the top level elements, to be compatible with the CDDL spec.

cuddle.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cabal-version: 3.4
22
name: cuddle
3-
version: 0.3.0.1
3+
version: 0.3.1.0
44
synopsis: CDDL Generator and test utilities
55

66
-- description:

src/Codec/CBOR/Cuddle/Huddle.hs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,19 @@ type Rule = Named Type0
118118

119119
-- | Top-level Huddle type is a list of rules.
120120
data Huddle = Huddle
121-
{ rules :: NE.NonEmpty Rule,
121+
{ -- | Root elements
122+
roots :: [Rule],
123+
rules :: NE.NonEmpty Rule,
122124
groups :: [Named Group],
123125
gRules :: [GRuleDef]
124126
}
125-
deriving (Show)
127+
deriving (Generic, Show)
126128

127129
-- | This instance is mostly used for testing
128130
instance IsList Huddle where
129131
type Item Huddle = Rule
130132
fromList [] = error "Huddle: Cannot have empty ruleset"
131-
fromList (x : xs) = Huddle (x NE.:| xs) mempty mempty
133+
fromList (x : xs) = Huddle mempty (x NE.:| xs) mempty mempty
132134

133135
toList = NE.toList . rules
134136

@@ -815,7 +817,9 @@ binding2 fRule t0 t1 =
815817
-- Collecting all top-level rules
816818
--------------------------------------------------------------------------------
817819

818-
-- | Collect all rules starting from a given point.
820+
-- | Collect all rules starting from a given point. This will also insert a
821+
-- single pseudo-rule as the first element which references the specified
822+
-- top-level rules.
819823
collectFrom :: [Rule] -> Huddle
820824
collectFrom topRs =
821825
toHuddle $
@@ -825,7 +829,8 @@ collectFrom topRs =
825829
where
826830
toHuddle (rules, groups, gRules) =
827831
Huddle
828-
{ rules = NE.fromList $ view _2 <$> HaskMap.toList rules,
832+
{ roots = topRs,
833+
rules = NE.fromList $ view _2 <$> HaskMap.toList rules,
829834
groups = view _2 <$> HaskMap.toList groups,
830835
gRules = view _2 <$> HaskMap.toList gRules
831836
}
@@ -871,10 +876,16 @@ collectFrom topRs =
871876
toCDDL :: Huddle -> CDDL
872877
toCDDL hdl =
873878
C.CDDL $
874-
fmap toCDDLRule (rules hdl)
875-
`appendList` fmap toCDDLGroup (groups hdl)
876-
`appendList` fmap toGenRuleDef (gRules hdl)
879+
toTopLevelPseudoRoot (roots hdl)
880+
NE.<| fmap toCDDLRule (rules hdl)
881+
`appendList` fmap toCDDLGroup (groups hdl)
882+
`appendList` fmap toGenRuleDef (gRules hdl)
877883
where
884+
toTopLevelPseudoRoot :: [Rule] -> C.WithComments C.Rule
885+
toTopLevelPseudoRoot topRs =
886+
toCDDLRule $
887+
comment "Pseudo-rule introduced by Cuddle to collect root elements" $
888+
"huddle_root_defs" =:= arr (fromList (fmap a topRs))
878889
-- This function is missing from NonEmpty prior to 4.16, so we temporarily
879890
-- add it here.
880891
appendList :: NE.NonEmpty a -> [a] -> NE.NonEmpty a

0 commit comments

Comments
 (0)