@@ -234,7 +234,7 @@ instance Num ArrayEntry where
234234 fromInteger i =
235235 ArrayEntry
236236 Nothing
237- (NoChoice . T2Literal . Unranged $ LInt (fromIntegral i))
237+ (NoChoice . T2Range . Unranged $ LInt (fromIntegral i))
238238 def
239239 Nothing
240240 (+) = error " Cannot treat ArrayEntry as a number"
@@ -265,7 +265,7 @@ instance IsList Group where
265265
266266data Type2
267267 = T2Constrained Constrained
268- | T2Literal Ranged
268+ | T2Range Ranged
269269 | T2Map Map
270270 | T2Array Array
271271 | T2Tagged (Tagged Type0 )
@@ -280,7 +280,7 @@ data Type2
280280type Type0 = Choice Type2
281281
282282instance Num Type0 where
283- fromInteger i = NoChoice . T2Literal . Unranged $ LInt (fromIntegral i)
283+ fromInteger i = NoChoice . T2Range . Unranged $ LInt (fromIntegral i)
284284 (+) = error " Cannot treat Type0 as a number"
285285 (*) = error " Cannot treat Type0 as a number"
286286 abs = error " Cannot treat Type0 as a number"
@@ -512,20 +512,36 @@ le v bound =
512512
513513-- Ranges
514514
515+ data RangeBound =
516+ RangeBoundLiteral Literal
517+ | RangeBoundRef (Named Type0 )
518+ deriving Show
519+
520+ class IsRangeBound a where
521+ toRangeBound :: a -> RangeBound
522+
523+ instance IsRangeBound Literal where
524+ toRangeBound = RangeBoundLiteral
525+
526+ instance IsRangeBound Integer where
527+ toRangeBound = RangeBoundLiteral . inferInteger
528+
529+ instance IsRangeBound (Named Type0 ) where
530+ toRangeBound = RangeBoundRef
531+
515532data Ranged where
516533 Ranged ::
517- { lb :: Literal ,
518- ub :: Literal ,
534+ { lb :: RangeBound ,
535+ ub :: RangeBound ,
519536 bounds :: C. RangeBound
520537 } ->
521538 Ranged
522539 Unranged :: Literal -> Ranged
523540 deriving (Show )
524541
525- -- | Establish a closed range bound. Currently specialised to Int for type
526- -- inference purposes.
527- (...) :: Integer -> Integer -> Ranged
528- l ... u = Ranged (inferInteger l) (inferInteger u) C. Closed
542+ -- | Establish a closed range bound.
543+ (...) :: (IsRangeBound a , IsRangeBound b ) => a -> b -> Ranged
544+ l ... u = Ranged (toRangeBound l) (toRangeBound u) C. Closed
529545
530546infixl 9 ...
531547
@@ -558,27 +574,27 @@ instance IsType0 ArrayChoice where
558574 toType0 = NoChoice . T2Array . NoChoice
559575
560576instance IsType0 Ranged where
561- toType0 = NoChoice . T2Literal
577+ toType0 = NoChoice . T2Range
562578
563579instance IsType0 Literal where
564- toType0 = NoChoice . T2Literal . Unranged
580+ toType0 = NoChoice . T2Range . Unranged
565581
566582-- We also allow going directly from primitive types to Type2
567583instance IsType0 Integer where
568- toType0 = NoChoice . T2Literal . Unranged . inferInteger
584+ toType0 = NoChoice . T2Range . Unranged . inferInteger
569585
570586instance IsType0 T. Text where
571587 toType0 :: T. Text -> Type0
572- toType0 = NoChoice . T2Literal . Unranged . LText
588+ toType0 = NoChoice . T2Range . Unranged . LText
573589
574590instance IsType0 ByteString where
575- toType0 = NoChoice . T2Literal . Unranged . LBytes
591+ toType0 = NoChoice . T2Range . Unranged . LBytes
576592
577593instance IsType0 Float where
578- toType0 = NoChoice . T2Literal . Unranged . LFloat
594+ toType0 = NoChoice . T2Range . Unranged . LFloat
579595
580596instance IsType0 Double where
581- toType0 = NoChoice . T2Literal . Unranged . LDouble
597+ toType0 = NoChoice . T2Range . Unranged . LDouble
582598
583599instance IsType0 (Value a ) where
584600 toType0 = NoChoice . T2Constrained . unconstrained
@@ -722,7 +738,7 @@ instance IsChoosable GRef Type2 where
722738 toChoice = toChoice . T2GenericRef
723739
724740instance IsChoosable ByteString Type2 where
725- toChoice = toChoice . T2Literal . Unranged . LBytes
741+ toChoice = toChoice . T2Range . Unranged . LBytes
726742
727743instance IsChoosable Constrained Type2 where
728744 toChoice = toChoice . T2Constrained
@@ -731,7 +747,7 @@ instance (IsType0 a) => IsChoosable (Tagged a) Type2 where
731747 toChoice = toChoice . T2Tagged . fmap toType0
732748
733749instance IsChoosable Literal Type2 where
734- toChoice = toChoice . T2Literal . Unranged
750+ toChoice = toChoice . T2Range . Unranged
735751
736752instance IsChoosable (Value a ) Type2 where
737753 toChoice = toChoice . T2Constrained . unconstrained
@@ -944,6 +960,7 @@ collectFrom topRs =
944960 goChoice f (NoChoice x) = f x
945961 goChoice f (ChoiceOf x xs) = f x >> goChoice f xs
946962 goT0 = goChoice goT2
963+ goT2 (T2Range r) = goRanged r
947964 goT2 (T2Map m) = goChoice (mapM_ goMapEntry . unMapChoice) m
948965 goT2 (T2Array m) = goChoice (mapM_ goArrayEntry . unArrayChoice) m
949966 goT2 (T2Tagged (Tagged _ t0)) = goT0 t0
@@ -975,7 +992,11 @@ collectFrom topRs =
975992 goKey (TypeKey k) = goT2 k
976993 goKey _ = pure ()
977994 goGroup (Group g) = mapM_ goT0 g
978-
995+ goRanged (Unranged _) = pure ()
996+ goRanged (Ranged lb ub _) = goRangeBound lb >> goRangeBound ub
997+ goRangeBound (RangeBoundLiteral _) = pure ()
998+ goRangeBound (RangeBoundRef r) = goRule r
999+
9791000--------------------------------------------------------------------------------
9801001-- Conversion to CDDL
9811002--------------------------------------------------------------------------------
@@ -1052,7 +1073,7 @@ toCDDL' mkPseudoRoot hdl =
10521073 T2Constrained (Constrained x constr _) ->
10531074 -- TODO Need to handle choices at the top level
10541075 applyConstraint constr (C. T2Name (toCDDLConstrainable x) Nothing )
1055- T2Literal l -> toCDDLRanged l
1076+ T2Range l -> toCDDLRanged l
10561077 T2Map m ->
10571078 C. Type1
10581079 (C. T2Map $ mapToCDDLGroup m)
@@ -1112,8 +1133,12 @@ toCDDL' mkPseudoRoot hdl =
11121133 C. Type1 (C. T2Value $ toCDDLValue x) Nothing
11131134 toCDDLRanged (Ranged lb ub rop) =
11141135 C. Type1
1115- (C. T2Value $ toCDDLValue lb)
1116- (Just (C. RangeOp rop, C. T2Value $ toCDDLValue ub))
1136+ (toCDDLRangeBound lb)
1137+ (Just (C. RangeOp rop, toCDDLRangeBound ub))
1138+
1139+ toCDDLRangeBound :: RangeBound -> C. Type2
1140+ toCDDLRangeBound (RangeBoundLiteral l) = C. T2Value $ toCDDLValue l
1141+ toCDDLRangeBound (RangeBoundRef (Named n _ _)) = C. T2Name (C. Name n) Nothing
11171142
11181143 toCDDLGroup :: Named Group -> C. WithComments C. Rule
11191144 toCDDLGroup (Named n (Group t0s) c) =
0 commit comments