@@ -18,7 +18,6 @@ import Codec.CBOR.Cuddle.CDDL.Resolve (MonoReferenced, XXCTree (..))
1818import Codec.CBOR.Cuddle.IndexMappable (IndexMappable (.. ))
1919import Codec.CBOR.Read
2020import Codec.CBOR.Term
21- import Control.Monad.Reader
2221import Data.Bifunctor
2322import Data.Bits hiding (And )
2423import Data.ByteString qualified as BS
@@ -733,47 +732,47 @@ flattenGroup cddl nodes =
733732--
734733-- Essentially the rules we will parse is the choice among the expansions of the
735734-- original rules.
736- expandRules :: Int -> [Rule ] -> Reader CDDL [[Rule ]]
737- expandRules remainingLen []
738- | remainingLen /= 0 = pure []
739- expandRules _ [] = pure [[] ]
740- expandRules remainingLen _
741- | remainingLen < 0 = pure []
742- | remainingLen == 0 = pure [[] ]
743- expandRules remainingLen (x : xs) = do
744- y <- expandRule remainingLen x
745- concat
746- <$> mapM
735+ expandRules :: CDDL -> Int -> [Rule ] -> [[Rule ]]
736+ expandRules _ remainingLen []
737+ | remainingLen /= 0 = []
738+ expandRules _ _ [] = [[] ]
739+ expandRules _ remainingLen _
740+ | remainingLen < 0 = []
741+ | remainingLen == 0 = [[] ]
742+ expandRules cddl remainingLen (x : xs) = do
743+ let y = expandRule cddl remainingLen x
744+ concat $
745+ mapM
747746 ( \ y' -> do
748- suffixes <- expandRules (remainingLen - length y') xs
749- pure [y' ++ ys' | ys' <- suffixes]
747+ suffixes <- expandRules cddl (remainingLen - length y') xs
748+ [y' ++ [ ys'] | ys' <- suffixes]
750749 )
751750 y
752751
753- expandRule :: Int -> Rule -> Reader CDDL [[Rule ]]
754- expandRule maxLen _
755- | maxLen < 0 = pure []
756- expandRule maxLen rule =
757- case rule of
758- Occur o OIOptional -> pure $ [] : [[o] | maxLen > 0 ]
759- Occur o OIZeroOrMore -> ([] : ) <$> expandRule maxLen (Occur o OIOneOrMore )
760- Occur o OIOneOrMore ->
761- if maxLen > 0
762- then ([o] : ) . map (o : ) <$> expandRule (maxLen - 1 ) (Occur o OIOneOrMore )
763- else pure []
764- Occur o (OIBounded low high) -> case (low, high) of
765- (Nothing , Nothing ) -> expandRule maxLen (Occur o OIZeroOrMore )
766- (Just (fromIntegral -> low'), Nothing ) ->
767- if maxLen >= low'
768- then map (replicate low' o ++ ) <$> expandRule (maxLen - low') (Occur o OIZeroOrMore )
769- else pure []
770- (Nothing , Just (fromIntegral -> high')) ->
771- pure [replicate n o | n <- [0 .. min maxLen high']]
772- (Just (fromIntegral -> low'), Just (fromIntegral -> high')) ->
773- if maxLen >= low'
774- then pure [replicate n o | n <- [low' .. min maxLen high']]
775- else pure []
776- _ -> pure [[rule | maxLen > 0 ]]
752+ expandRule :: CDDL -> Int -> Rule -> [[Rule ]]
753+ expandRule cddl maxLen rule
754+ | maxLen < 0 = []
755+ | otherwise =
756+ case rule of
757+ Occur o OIOptional -> [] : [[o] | maxLen > 0 ]
758+ Occur o OIZeroOrMore -> ([] : ) $ expandRule cddl maxLen (Occur o OIOneOrMore )
759+ Occur o OIOneOrMore ->
760+ if maxLen > 0
761+ then ([o] : ) . map (o : ) $ expandRule cddl (maxLen - 1 ) (Occur o OIOneOrMore )
762+ else []
763+ Occur o (OIBounded low high) -> case (low, high) of
764+ (Nothing , Nothing ) -> expandRule cddl maxLen (Occur o OIZeroOrMore )
765+ (Just (fromIntegral -> low'), Nothing ) ->
766+ if maxLen >= low'
767+ then (replicate low' o ++ ) <$> expandRule cddl (maxLen - low') (Occur o OIZeroOrMore )
768+ else []
769+ (Nothing , Just (fromIntegral -> high')) ->
770+ [replicate n o | n <- [0 .. min maxLen high']]
771+ (Just (fromIntegral -> low'), Just (fromIntegral -> high')) ->
772+ if maxLen >= low'
773+ then [replicate n o | n <- [low' .. min maxLen high']]
774+ else []
775+ _ -> [[rule | maxLen > 0 ]]
777776
778777-- | Which rules are optional?
779778isOptional :: Rule -> Bool
@@ -844,7 +843,7 @@ validateList cddl terms rule =
844843 [] -> (if all isOptional rules then Valid else InvalidRule ) rule
845844 _ ->
846845 let sequencesOfRules =
847- runReader ( expandRules (length terms) $ flattenGroup cddl rules) cddl
846+ expandRules cddl (length terms) $ flattenGroup cddl rules
848847 in validateExpandedList cddl terms sequencesOfRules rule
849848 Choice opts -> validateChoice (validateList cddl terms) opts rule
850849 _ -> UnapplicableRule rule
@@ -869,8 +868,7 @@ validateMapWithExpandedRules cddl =
869868 go ((tk, tv) : ts) rs = do
870869 case go' tk tv rs of
871870 Left tt -> ([] , Just tt)
872- Right (res, rs') ->
873- first (res : ) $ go ts rs'
871+ Right (res, rs') -> first (res : ) $ go ts rs'
874872 go _ _ = error " Not yet implemented"
875873
876874 -- For each pair of terms, try to find some rule that can be applied here,
@@ -924,7 +922,7 @@ validateMap cddl terms rule =
924922 [] -> if all isOptional rules then Valid else InvalidRule
925923 _ ->
926924 let sequencesOfRules =
927- runReader ( expandRules (length terms) $ flattenGroup cddl rules) cddl
925+ expandRules cddl (length terms) $ flattenGroup cddl rules
928926 in validateExpandedMap cddl terms sequencesOfRules
929927 Choice opts -> validateChoice (validateMap cddl terms) opts
930928 _ -> UnapplicableRule
0 commit comments