@@ -92,7 +92,7 @@ import Control.Monad (when)
9292import Control.Monad.State (MonadState (get ), execState , modify )
9393import Data.ByteString (ByteString )
9494import Data.Default.Class (Default (.. ))
95- import Data.Generics.Product (field , getField )
95+ import Data.Generics.Product (HasField' ( field' ), field , getField )
9696import Data.List.NonEmpty qualified as NE
9797import Data.Map.Ordered.Strict (OMap )
9898import Data.Map.Ordered.Strict qualified as OMap
@@ -113,9 +113,9 @@ data Named a = Named
113113 }
114114 deriving (Functor , Generic )
115115
116- -- | Add a description to a rule, to be included as a comment.
117- comment :: T. Text -> Named a -> Named a
118- comment desc n = n & field @ " description" .~ Just desc
116+ -- | Add a description to a rule or group entry , to be included as a comment.
117+ comment :: ( HasField' " description " a ( Maybe T. Text)) => T. Text -> a -> a
118+ comment desc n = n & field' @ " description" .~ Just desc
119119
120120instance Show (Named a ) where
121121 show (Named n _ _) = T. unpack n
@@ -201,7 +201,8 @@ asKey r = case toType0 r of
201201data MapEntry = MapEntry
202202 { key :: Key ,
203203 value :: Type0 ,
204- quantifier :: Occurs
204+ quantifier :: Occurs ,
205+ description :: Maybe T. Text
205206 }
206207 deriving (Generic , Show )
207208
@@ -221,7 +222,8 @@ data ArrayEntry = ArrayEntry
221222 -- here because they can be illustrative in the generated CDDL.
222223 key :: Maybe Key ,
223224 value :: Type0 ,
224- quantifier :: Occurs
225+ quantifier :: Occurs ,
226+ description :: Maybe T. Text
225227 }
226228 deriving (Generic , Show )
227229
@@ -231,6 +233,7 @@ instance Num ArrayEntry where
231233 Nothing
232234 (NoChoice . T2Literal . Unranged $ LInt (fromIntegral i))
233235 def
236+ Nothing
234237 (+) = error " Cannot treat ArrayEntry as a number"
235238 (*) = error " Cannot treat ArrayEntry as a number"
236239 abs = error " Cannot treat ArrayEntry as a number"
@@ -584,7 +587,8 @@ instance IsEntryLike ArrayEntry where
584587 { key = Just $ getField @ " key" me,
585588 value =
586589 getField @ " value" me,
587- quantifier = getField @ " quantifier" me
590+ quantifier = getField @ " quantifier" me,
591+ description = Nothing
588592 }
589593
590594instance IsEntryLike Type0 where
@@ -596,7 +600,8 @@ k ==> gc =
596600 MapEntry
597601 { key = k,
598602 value = toType0 gc,
599- quantifier = def
603+ quantifier = def,
604+ description = Nothing
600605 }
601606
602607infixl 8 ==>
@@ -620,7 +625,8 @@ instance IsGroupOrArrayEntry ArrayEntry where
620625 ArrayEntry
621626 { key = Nothing ,
622627 value = toType0 x,
623- quantifier = def
628+ quantifier = def,
629+ description = Nothing
624630 }
625631
626632instance IsGroupOrArrayEntry Type0 where
@@ -899,9 +905,9 @@ collectFrom topRs =
899905 mapM_ goT2 $ args g
900906 goT2 (T2Basic (Constrained _ _ refs)) = mapM_ goRule refs
901907 goT2 _ = pure ()
902- goArrayEntry (ArrayEntry (Just k) t0 _) = goKey k >> goT0 t0
903- goArrayEntry (ArrayEntry Nothing t0 _) = goT0 t0
904- goMapEntry (MapEntry k t0 _) = goKey k >> goT0 t0
908+ goArrayEntry (ArrayEntry (Just k) t0 _ _ ) = goKey k >> goT0 t0
909+ goArrayEntry (ArrayEntry Nothing t0 _ _ ) = goT0 t0
910+ goMapEntry (MapEntry k t0 _ _ ) = goKey k >> goT0 t0
905911 goKey (TypeKey k) = goT2 k
906912 goKey _ = pure ()
907913 goGroup (Group g) = mapM_ goT0 g
@@ -960,12 +966,15 @@ toCDDL' mkPseudoRoot hdl =
960966 mapChoiceToCDDL :: MapChoice -> C. GrpChoice
961967 mapChoiceToCDDL (MapChoice entries) = fmap mapEntryToCDDL entries
962968
963- mapEntryToCDDL :: MapEntry -> C. GroupEntry
964- mapEntryToCDDL (MapEntry k v occ) =
965- C. GEType
966- (toOccurrenceIndicator occ)
967- (Just $ toMemberKey k)
968- (toCDDLType0 v)
969+ mapEntryToCDDL :: MapEntry -> C. WithComments C. GroupEntry
970+ mapEntryToCDDL (MapEntry k v occ cmnt) =
971+ C. WithComments
972+ ( C. GEType
973+ (toOccurrenceIndicator occ)
974+ (Just $ toMemberKey k)
975+ (toCDDLType0 v)
976+ )
977+ (fmap C. Comment cmnt)
969978
970979 toOccurrenceIndicator :: Occurs -> Maybe C. OccurrenceIndicator
971980 toOccurrenceIndicator (Occurs Nothing Nothing ) = Nothing
@@ -1006,13 +1015,15 @@ toCDDL' mkPseudoRoot hdl =
10061015 arrayChoiceToCDDL :: ArrayChoice -> C. GrpChoice
10071016 arrayChoiceToCDDL (ArrayChoice entries) = fmap arrayEntryToCDDL entries
10081017
1009- arrayEntryToCDDL :: ArrayEntry -> C. GroupEntry
1010- arrayEntryToCDDL (ArrayEntry k v occ) =
1011- C. GEType
1012- (toOccurrenceIndicator occ)
1013- (fmap toMemberKey k)
1014- (toCDDLType0 v)
1015-
1018+ arrayEntryToCDDL :: ArrayEntry -> C. WithComments C. GroupEntry
1019+ arrayEntryToCDDL (ArrayEntry k v occ cmnt) =
1020+ C. WithComments
1021+ ( C. GEType
1022+ (toOccurrenceIndicator occ)
1023+ (fmap toMemberKey k)
1024+ (toCDDLType0 v)
1025+ )
1026+ (fmap C. Comment cmnt)
10161027 toCDDLPostlude :: Value a -> C. Name
10171028 toCDDLPostlude VBool = C. Name " bool"
10181029 toCDDLPostlude VUInt = C. Name " uint"
@@ -1042,7 +1053,7 @@ toCDDL' mkPseudoRoot hdl =
10421053 . C. GEGroup Nothing
10431054 . C. Group
10441055 . (NE. :| [] )
1045- $ fmap (C. GEType Nothing Nothing . toCDDLType0) t0s
1056+ $ fmap (C. noComment . C. GEType Nothing Nothing . toCDDLType0) t0s
10461057 )
10471058 (fmap C. Comment c)
10481059
0 commit comments