diff --git a/bin/Main.hs b/bin/Main.hs index c7f87a88..f9ca195a 100644 --- a/bin/Main.hs +++ b/bin/Main.hs @@ -6,24 +6,23 @@ module Main (main) where -import Codec.CBOR.Cuddle.CBOR.Gen (generateFromName) -import Codec.CBOR.Cuddle.CBOR.Validator -import Codec.CBOR.Cuddle.CBOR.Validator.Trace ( - Evidenced (..), - SValidity (..), - TraceOptions (..), - prettyValidationTrace, - ) import Codec.CBOR.Cuddle.CDDL (CDDL, Name (..), fromRules, sortCDDL) import Codec.CBOR.Cuddle.CDDL.CTree (CTreeRoot) -import Codec.CBOR.Cuddle.CDDL.Custom.Generator (GenConfig (..), runCBORGen) import Codec.CBOR.Cuddle.CDDL.Postlude (appendPostlude) import Codec.CBOR.Cuddle.CDDL.Resolve ( fullResolveCDDL, ) +import Codec.CBOR.Cuddle.Generator (GenConfig (..), generateFromName, runCBORGen) import Codec.CBOR.Cuddle.IndexMappable (IndexMappable (..), mapCDDLDropExt) -import Codec.CBOR.Cuddle.Parser (ParserStage, pCDDL) -import Codec.CBOR.Cuddle.Pretty (PrettyStage, renderCDDL) +import Codec.CBOR.Cuddle.Parser (ParserPhase, pCDDL) +import Codec.CBOR.Cuddle.Pretty (PrettyPhase, renderCDDL) +import Codec.CBOR.Cuddle.Validator (ValidatorPhase, validateCBOR) +import Codec.CBOR.Cuddle.Validator.Trace ( + Evidenced (..), + SValidity (..), + TraceOptions (..), + prettyValidationTrace, + ) import Codec.CBOR.FlatTerm (toFlatTerm) import Codec.CBOR.Pretty (prettyHexEnc) import Codec.CBOR.Read (deserialiseFromBytes) @@ -334,7 +333,7 @@ main = do ) run options -tryParseFromFile :: FilePath -> IO (CDDL ParserStage) +tryParseFromFile :: FilePath -> IO (CDDL ParserPhase) tryParseFromFile cddlFile = parseFromFile pCDDL cddlFile >>= \case Left err -> do @@ -371,7 +370,7 @@ run = \case | sort fOpts = fromRules $ sortCDDL res | otherwise = res layoutOptions = defaultLayoutOptions {layoutPageWidth = AvailablePerLine 80 1} - formattedText = renderCDDL layoutOptions $ mapIndex @_ @_ @PrettyStage defs + formattedText = renderCDDL layoutOptions $ mapIndex @_ @_ @PrettyPhase defs T.putStr formattedText Validate vOpts cddlFile -> do res <- tryParseFromFile cddlFile diff --git a/cuddle.cabal b/cuddle.cabal index ba4edad5..51de445e 100644 --- a/cuddle.cabal +++ b/cuddle.cabal @@ -38,26 +38,29 @@ common warnings library import: warnings exposed-modules: - Codec.CBOR.Cuddle.CBOR.Gen - Codec.CBOR.Cuddle.CBOR.Validator - Codec.CBOR.Cuddle.CBOR.Validator.Trace + Codec.CBOR.Cuddle + Codec.CBOR.Cuddle.Generator + Codec.CBOR.Cuddle.Huddle + Codec.CBOR.Cuddle.Parser + Codec.CBOR.Cuddle.Validator + + other-modules: Codec.CBOR.Cuddle.CDDL Codec.CBOR.Cuddle.CDDL.CTree Codec.CBOR.Cuddle.CDDL.CTreePhase Codec.CBOR.Cuddle.CDDL.CtlOp - Codec.CBOR.Cuddle.CDDL.Custom.Core - Codec.CBOR.Cuddle.CDDL.Custom.Generator - Codec.CBOR.Cuddle.CDDL.Custom.Validator Codec.CBOR.Cuddle.CDDL.Postlude Codec.CBOR.Cuddle.CDDL.Resolve Codec.CBOR.Cuddle.Comments - Codec.CBOR.Cuddle.Huddle + Codec.CBOR.Cuddle.Core + Codec.CBOR.Cuddle.Generator.Core Codec.CBOR.Cuddle.IndexMappable - Codec.CBOR.Cuddle.Parser Codec.CBOR.Cuddle.Parser.Lexer Codec.CBOR.Cuddle.Pretty Codec.CBOR.Cuddle.Pretty.Columnar Codec.CBOR.Cuddle.Pretty.Utils + Codec.CBOR.Cuddle.Validator.Core + Codec.CBOR.Cuddle.Validator.Trace build-depends: QuickCheck >=2.14.3, @@ -128,17 +131,17 @@ test-suite cuddle-test other-modules: Paths_cuddle - Test.Codec.CBOR.Cuddle.CDDL.Examples - Test.Codec.CBOR.Cuddle.CDDL.Examples.Huddle - Test.Codec.CBOR.Cuddle.CDDL.Gen - Test.Codec.CBOR.Cuddle.CDDL.GeneratorSpec - Test.Codec.CBOR.Cuddle.CDDL.Parser - Test.Codec.CBOR.Cuddle.CDDL.Pretty - Test.Codec.CBOR.Cuddle.CDDL.Pretty.Golden - Test.Codec.CBOR.Cuddle.CDDL.TreeDiff - Test.Codec.CBOR.Cuddle.CDDL.Validator - Test.Codec.CBOR.Cuddle.CDDL.Validator.Golden + Test.Codec.CBOR.Cuddle.Examples + Test.Codec.CBOR.Cuddle.Examples.Huddle + Test.Codec.CBOR.Cuddle.Gen + Test.Codec.CBOR.Cuddle.GeneratorSpec + Test.Codec.CBOR.Cuddle.TreeDiff Test.Codec.CBOR.Cuddle.Huddle + Test.Codec.CBOR.Cuddle.Parser + Test.Codec.CBOR.Cuddle.Pretty + Test.Codec.CBOR.Cuddle.Pretty.Golden + Test.Codec.CBOR.Cuddle.Validator + Test.Codec.CBOR.Cuddle.Validator.Golden type: exitcode-stdio-1.0 hs-source-dirs: test diff --git a/example/Main.hs b/example/Main.hs index 5a693700..2689c043 100644 --- a/example/Main.hs +++ b/example/Main.hs @@ -5,18 +5,15 @@ module Main (main) where -import Codec.CBOR.Cuddle.CBOR.Gen (generateFromName) -import Codec.CBOR.Cuddle.CDDL (Name (..)) -import Codec.CBOR.Cuddle.CDDL.Custom.Generator (GenConfig (..), runCBORGen) -import Codec.CBOR.Cuddle.CDDL.Resolve (MonoSimplePhase, fullResolveCDDL) +import Codec.CBOR.Cuddle (fullResolveCDDL, mapCDDLDropExt, mapIndex, showSimple) +import Codec.CBOR.Cuddle.Generator (GenConfig (..), generateFromName, runCBORGen) import Codec.CBOR.Cuddle.Huddle (toCDDL) -import Codec.CBOR.Cuddle.IndexMappable (IndexMappable (..), mapCDDLDropExt) import Codec.CBOR.Cuddle.Parser (pCDDL) -import Codec.CBOR.Cuddle.Pretty (PrettyStage) import Conway (conway) +import Data.String (IsString (..)) import Data.Text qualified as T import Data.Text.IO qualified as T -import Prettyprinter (Pretty (pretty)) +import Prettyprinter (Pretty (..)) import Prettyprinter.Util (putDocW) import System.Environment (getArgs) import Test.AntiGen (runAntiGen) @@ -31,14 +28,14 @@ main = do parseFromFile pCDDL fn >>= \case Left err -> putStrLn $ errorBundlePretty err Right res -> do - putDocW 80 $ pretty (mapIndex @_ @_ @PrettyStage res) + putDocW 80 $ pretty res putStrLn "\n" putStrLn "--------------------------------------------------------------------------------" putStrLn " Resolving" putStrLn "--------------------------------------------------------------------------------" case fullResolveCDDL (mapCDDLDropExt res) of Left nre -> putStrLn $ "Resolution error: " <> show nre - Right resolved -> print (mapIndex @_ @_ @MonoSimplePhase resolved) + Right resolved -> putStrLn $ showSimple resolved [fn, name] -> do putStrLn "--------------------------------------------------------------------------------" putStrLn " Generating a term" @@ -50,11 +47,11 @@ main = do Left nre -> error $ show nre Right resolved -> do let cfg = GenConfig {gcRoot = mapIndex resolved, gcTwiddle = True} - term <- generate . runAntiGen . runCBORGen cfg $ generateFromName (Name (T.pack name)) + term <- generate . runAntiGen . runCBORGen cfg $ generateFromName (fromString name) print term [] -> do let cw = toCDDL conway - putDocW 80 $ pretty (mapIndex @_ @_ @PrettyStage cw) + putDocW 80 $ pretty cw _ -> putStrLn "Expected filename" parseFromFile :: diff --git a/src/Codec/CBOR/Cuddle.hs b/src/Codec/CBOR/Cuddle.hs new file mode 100644 index 00000000..ef6462f5 --- /dev/null +++ b/src/Codec/CBOR/Cuddle.hs @@ -0,0 +1,16 @@ +module Codec.CBOR.Cuddle ( + -- * Resolver + fullResolveCDDL, + + -- * IndexMap + mapCDDLDropExt, + IndexMappable (..), + + -- * Prettyprinting + showSimple, + renderCDDL, +) where + +import Codec.CBOR.Cuddle.CDDL.Resolve (fullResolveCDDL, showSimple) +import Codec.CBOR.Cuddle.IndexMappable (IndexMappable (..), mapCDDLDropExt) +import Codec.CBOR.Cuddle.Pretty (renderCDDL) diff --git a/src/Codec/CBOR/Cuddle/CDDL.hs b/src/Codec/CBOR/Cuddle/CDDL.hs index 1ef388c2..c799fc10 100644 --- a/src/Codec/CBOR/Cuddle/CDDL.hs +++ b/src/Codec/CBOR/Cuddle/CDDL.hs @@ -39,7 +39,6 @@ module Codec.CBOR.Cuddle.CDDL ( unwrap, compareRuleName, HasName (..), - GRef (..), -- Extension ForAllExtensions, XCddl, @@ -51,6 +50,7 @@ module Codec.CBOR.Cuddle.CDDL ( import Codec.CBOR.Cuddle.CDDL.CtlOp (CtlOp) import Codec.CBOR.Cuddle.Comments (CollectComments (..), Comment, HasComment (..)) +import Codec.CBOR.Cuddle.Core (Name (..)) import Data.ByteString qualified as B import Data.Default.Class (Default (..)) import Data.Function (on) @@ -58,7 +58,6 @@ import Data.Hashable (Hashable) import Data.List.NonEmpty (NonEmpty (..), prependList) import Data.List.NonEmpty qualified as NE import Data.Maybe (mapMaybe) -import Data.String (IsString (..)) import Data.Text qualified as T import Data.Word (Word64, Word8) import GHC.Base (Constraint, Type) @@ -106,7 +105,7 @@ ruleTopLevel _ = Nothing -- | Sort the CDDL Rules on the basis of their names sortCDDL :: CDDL i -> NonEmpty (Rule i) -sortCDDL (CDDL r rs _) = NE.sortBy (compare `on` unName . ruleName) $ r :| mapMaybe ruleTopLevel rs +sortCDDL (CDDL r rs _) = NE.sortBy (compare `on` ruleName) $ r :| mapMaybe ruleTopLevel rs fromRules :: Monoid (XCddl i) => NonEmpty (Rule i) -> CDDL i fromRules (x :| xs) = CDDL x (TopLevelRule <$> xs) mempty @@ -129,46 +128,6 @@ deriving instance ForAllExtensions i Eq => Eq (TopLevel i) deriving instance ForAllExtensions i Show => Show (TopLevel i) --- | --- A name can consist of any of the characters from the set {"A" to --- "Z", "a" to "z", "0" to "9", "_", "-", "@", ".", "$"}, starting --- with an alphabetic character (including "@", "_", "$") and ending --- in such a character or a digit. --- --- * Names are case sensitive. --- --- * It is preferred style to start a name with a lowercase letter. --- --- * The hyphen is preferred over the underscore (except in a --- "bareword" (Section 3.5.1), where the semantics may actually --- require an underscore). --- --- * The period may be useful for larger specifications, to express --- some module structure (as in "tcp.throughput" vs. --- "udp.throughput"). --- --- * A number of names are predefined in the CDDL prelude, as listed --- in Appendix D. --- --- * Rule names (types or groups) do not appear in the actual CBOR --- encoding, but names used as "barewords" in member keys do. -newtype Name = Name {unName :: T.Text} - deriving (Generic) - deriving (Eq, Ord, Show) - deriving newtype (Semigroup, Monoid) - --- | A reference to a generic parameter inside the body of a generic rule. -newtype GRef = GRef T.Text - deriving (Show) - -instance IsString Name where - fromString = Name . T.pack - -instance CollectComments Name where - collectComments _ = [] - -instance Hashable Name - class HasName a where getName :: a -> Name diff --git a/src/Codec/CBOR/Cuddle/CDDL/CTree.hs b/src/Codec/CBOR/Cuddle/CDDL/CTree.hs index 9294d43e..73ecfd60 100644 --- a/src/Codec/CBOR/Cuddle/CDDL/CTree.hs +++ b/src/Codec/CBOR/Cuddle/CDDL/CTree.hs @@ -1,19 +1,33 @@ {-# LANGUAGE DataKinds #-} {-# LANGUAGE DerivingStrategies #-} +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE UndecidableInstances #-} module Codec.CBOR.Cuddle.CDDL.CTree where -import Codec.CBOR.Cuddle.CDDL (Name, OccurrenceIndicator, RangeBound, Value) +import Codec.CBOR.Cuddle.CDDL ( + Name, + OccurrenceIndicator (..), + RangeBound (..), + Value (..), + ValueVariant (..), + ) import Codec.CBOR.Cuddle.CDDL.CtlOp +import Codec.CBOR.Cuddle.Core (GRef) import Control.Monad.Identity (Identity (..)) +import Data.Foldable (Foldable (..)) import Data.Hashable (Hashable) +import Data.Kind (Type) import Data.List.NonEmpty qualified as NE import Data.Map.Strict qualified as Map +import Data.Text (Text) +import Data.Text qualified as T import Data.Word (Word64) import GHC.Generics (Generic) import Generic.Random (genericArbitraryU) +import Prettyprinter (Doc, Pretty (..), encloseSep, group, list, punctuate, tupled, vsep, (<+>)) import Test.QuickCheck (Arbitrary (..)) -------------------------------------------------------------------------------- @@ -85,6 +99,23 @@ foldCTree :: CTree j foldCTree atExt atNode x = runIdentity $ traverseCTree (pure . atExt) (pure . atNode) x +displayCanonicalCTree :: CTree p -> Text +displayCanonicalCTree = \case + Literal (Value v _) -> displayCanonicalValueVariant v + Postlude _ -> _ + Map _ -> _ + Array _ -> _ + Choice _ -> _ + Group _ -> _ + KV _ _ _ -> _ + Occur _ _ -> _ + Range _ _ _ -> _ + Control _ _ _ -> _ + Enum _ -> _ + Unwrap _ -> _ + Tag _ _ -> _ + CTreeE _ -> _ + type Node i = XXCTree i newtype CTreeRoot i = CTreeRoot (Map.Map Name (CTree i)) @@ -146,10 +177,13 @@ instance Arbitrary PTerm where instance Hashable PTerm --- Bounds +class MonadCddl m where + type Phase m :: Type -uintMax :: Integer -uintMax = 2 ^ (64 :: Int) - 1 + -- | Look up a top-level rule by name. + lookupCddl :: Name -> m (Maybe (CTree (Phase m))) -nintMin :: Integer -nintMin = -(2 ^ (64 :: Int)) + -- | Look up the rule bound to a generic parameter at the enclosing rule. + -- Returns 'Nothing' outside of a custom generator/validator that was + -- attached to a generic rule. + lookupGRef :: GRef -> m (Maybe (CTree (Phase m))) diff --git a/src/Codec/CBOR/Cuddle/CDDL/CTreePhase.hs b/src/Codec/CBOR/Cuddle/CDDL/CTreePhase.hs index b17fbc63..8f659817 100644 --- a/src/Codec/CBOR/Cuddle/CDDL/CTreePhase.hs +++ b/src/Codec/CBOR/Cuddle/CDDL/CTreePhase.hs @@ -12,9 +12,9 @@ module Codec.CBOR.Cuddle.CDDL.CTreePhase ( ) where import Codec.CBOR.Cuddle.CDDL (XCddl, XRule, XTerm, XXTopLevel, XXType2) -import Codec.CBOR.Cuddle.CDDL.Custom.Core (RuleTerm) -import Codec.CBOR.Cuddle.CDDL.Custom.Generator (CBORGen) -import Codec.CBOR.Cuddle.CDDL.Custom.Validator (TermValidator) +import Codec.CBOR.Cuddle.Core (RuleTerm) +import Codec.CBOR.Cuddle.Generator.Core (CBORGen) +import Codec.CBOR.Cuddle.Validator.Core (TermValidator) import Data.Default.Class (Default) import Data.Hashable (Hashable) import GHC.Generics (Generic) diff --git a/src/Codec/CBOR/Cuddle/CDDL/Custom/Core.hs b/src/Codec/CBOR/Cuddle/CDDL/Custom/Core.hs deleted file mode 100644 index 05382a58..00000000 --- a/src/Codec/CBOR/Cuddle/CDDL/Custom/Core.hs +++ /dev/null @@ -1,25 +0,0 @@ -{-# LANGUAGE TypeFamilies #-} - -module Codec.CBOR.Cuddle.CDDL.Custom.Core (MonadCddl (..), RuleTerm (..)) where - -import Codec.CBOR.Cuddle.CDDL (GRef, Name) -import Codec.CBOR.Cuddle.CDDL.CTree (CTree) -import Codec.CBOR.Term (Term) -import Data.Kind (Type) - -class MonadCddl m where - type Phase m :: Type - - -- | Look up a top-level rule by name. - lookupCddl :: Name -> m (Maybe (CTree (Phase m))) - - -- | Look up the rule bound to a generic parameter at the enclosing rule. - -- Returns 'Nothing' outside of a custom generator/validator that was - -- attached to a generic rule. - lookupGRef :: GRef -> m (Maybe (CTree (Phase m))) - -data RuleTerm - = SingleTerm Term - | PairTerm Term Term - | GroupTerm [RuleTerm] - deriving (Eq, Ord, Show) diff --git a/src/Codec/CBOR/Cuddle/CDDL/Postlude.hs b/src/Codec/CBOR/Cuddle/CDDL/Postlude.hs index a8e2ae8f..16aad450 100644 --- a/src/Codec/CBOR/Cuddle/CDDL/Postlude.hs +++ b/src/Codec/CBOR/Cuddle/CDDL/Postlude.hs @@ -4,12 +4,12 @@ module Codec.CBOR.Cuddle.CDDL.Postlude where import Codec.CBOR.Cuddle.CDDL (CDDL (..), TopLevel (..), XRule, XTerm, XXType2, appendRules) import Codec.CBOR.Cuddle.IndexMappable (IndexMappable (..)) -import Codec.CBOR.Cuddle.Parser (ParserStage, pCDDL) +import Codec.CBOR.Cuddle.Parser (ParserPhase, pCDDL) import Data.Maybe (mapMaybe) import Text.Megaparsec (errorBundlePretty, parse) -- TODO switch to quasiquotes -cddlPostlude :: CDDL ParserStage +cddlPostlude :: CDDL ParserPhase cddlPostlude = either (error . errorBundlePretty) id $ parse @@ -60,9 +60,9 @@ cddlPostlude = \ undefined = #7.23" appendPostlude :: - ( IndexMappable XXType2 ParserStage i - , IndexMappable XTerm ParserStage i - , IndexMappable XRule ParserStage i + ( IndexMappable XXType2 ParserPhase i + , IndexMappable XTerm ParserPhase i + , IndexMappable XRule ParserPhase i ) => CDDL i -> CDDL i appendPostlude cddl = appendRules cddl $ mapIndex <$> (r : rs) diff --git a/src/Codec/CBOR/Cuddle/CDDL/Resolve.hs b/src/Codec/CBOR/Cuddle/CDDL/Resolve.hs index 7c8c2704..168b5d05 100644 --- a/src/Codec/CBOR/Cuddle/CDDL/Resolve.hs +++ b/src/Codec/CBOR/Cuddle/CDDL/Resolve.hs @@ -36,6 +36,7 @@ module Codec.CBOR.Cuddle.CDDL.Resolve ( NameResolutionFailure (..), MonoReferenced, MonoSimplePhase, + DistRef (..), showSimple, XXCTree (..), ) @@ -67,26 +68,21 @@ import Data.Hashable import Data.List (foldl') #endif import Codec.CBOR.Cuddle.CDDL.CTreePhase (CTreePhase, XRule (..)) -import Codec.CBOR.Cuddle.CDDL.Custom.Core (RuleTerm) -import Codec.CBOR.Cuddle.CDDL.Custom.Generator ( - CBORGen, - GenPhase, - XXCTree (..), - withLocalGenBindings, - ) -import Codec.CBOR.Cuddle.CDDL.Custom.Validator ( +import Codec.CBOR.Cuddle.Core (RuleTerm) +import Codec.CBOR.Cuddle.Generator.Core (CBORGen, GenPhase, XXCTree (..), withLocalGenBindings) +import Codec.CBOR.Cuddle.IndexMappable (IndexMappable (..)) +import Codec.CBOR.Cuddle.Validator.Core ( TermValidator, ValidatorPhase, XXCTree (..), withLocalValidateBindings, ) -import Codec.CBOR.Cuddle.IndexMappable (IndexMappable (..)) import Data.List.NonEmpty qualified as NE import Data.Map.Strict qualified as Map import Data.Text qualified as T import GHC.Generics (Generic) import Optics.Core -import Prettyprinter (Pretty (..), encloseSep, layoutCompact) +import Prettyprinter (Pretty (..), layoutCompact) import Prettyprinter.Render.Text (renderStrict) data ProvidedParameters a = ProvidedParameters @@ -409,21 +405,15 @@ deriving instance Show (CTree.Node i) => Show (DistRef i) instance Hashable (CTree.Node i) => Hashable (DistRef i) -instance Pretty (XXCTree i) => Pretty (DistRef i) where - pretty (GenericRef n) = pretty n - pretty (RuleRef rule []) = pretty rule - pretty (RuleRef rule args) = pretty rule <> encloseSep "<" ">" "," (pretty <$> args) - data instance XXCTree DistReferenced = DRef (DistRef DistReferenced) | DGenerator (CBORGen RuleTerm) (CTree DistReferenced) | DValidator TermValidator (CTree DistReferenced) -type data DistReferencedNoGen +type data DistReferencedSimplePhase -newtype instance XXCTree DistReferencedNoGen = DHRef (DistRef DistReferencedNoGen) +newtype instance XXCTree DistReferencedSimplePhase = DHRef (DistRef DistReferencedSimplePhase) deriving (Eq, Hashable, Show) - deriving newtype (Pretty) resolveRef :: BindingEnv OrReferenced OrReferenced -> @@ -598,8 +588,8 @@ throwNR = throw @"nameResolution" -- | Synthesize a monomorphic rule definition, returning the name synthMono :: Name -> [CTree DistReferenced] -> MonoM Name synthMono origName args = - let dropGenerator = fmap $ mapIndex @_ @DistReferenced @DistReferencedNoGen - argsName = Name (T.intercalate "," $ renderStrict . layoutCompact . pretty <$> dropGenerator args) + let dropGenerator = fmap $ mapIndex @_ @DistReferenced @DistReferencedSimplePhase + argsName = Name (T.intercalate "," $ renderStrict . layoutCompact . prettyCTree <$> dropGenerator args) -- We use % to mark a monomorphised generic rule, '%' is not allowed in -- CDDL names, so there should be no conflicts fresh = "%" <> origName <> "<" <> argsName <> ">" @@ -683,13 +673,13 @@ fullResolveCDDL cddl = do rCTree <- buildResolvedCTree refCTree buildMonoCTree rCTree -instance IndexMappable CTree DistReferenced DistReferencedNoGen where +instance IndexMappable CTree DistReferenced DistReferencedSimplePhase where mapIndex = foldCTree mapExt mapIndex where mapExt (DRef x) = CTreeE . DHRef $ mapIndex x mapExt (DGenerator _ x) = mapIndex x mapExt (DValidator _ x) = mapIndex x -instance IndexMappable DistRef DistReferenced DistReferencedNoGen where +instance IndexMappable DistRef DistReferenced DistReferencedSimplePhase where mapIndex (GenericRef n) = GenericRef n mapIndex (RuleRef n args) = RuleRef n $ mapIndex <$> args diff --git a/src/Codec/CBOR/Cuddle/Core.hs b/src/Codec/CBOR/Cuddle/Core.hs new file mode 100644 index 00000000..fc4bd92a --- /dev/null +++ b/src/Codec/CBOR/Cuddle/Core.hs @@ -0,0 +1,75 @@ +{-# LANGUAGE DerivingStrategies #-} +{-# LANGUAGE TypeFamilies #-} + +module Codec.CBOR.Cuddle.Core ( + RuleTerm (..), + Name (..), + GRef (..), + uintMax, + nintMin, +) where + +import Codec.CBOR.Cuddle.Comments (CollectComments (..)) +import Codec.CBOR.Term (Term) +import Data.Hashable (Hashable) +import Data.String (IsString (..)) +import Data.Text qualified as T +import GHC.Generics (Generic) +import Prettyprinter (Pretty (..)) + +-- | +-- A name can consist of any of the characters from the set {"A" to +-- "Z", "a" to "z", "0" to "9", "_", "-", "@", ".", "$"}, starting +-- with an alphabetic character (including "@", "_", "$") and ending +-- in such a character or a digit. +-- +-- * Names are case sensitive. +-- +-- * It is preferred style to start a name with a lowercase letter. +-- +-- * The hyphen is preferred over the underscore (except in a +-- "bareword" (Section 3.5.1), where the semantics may actually +-- require an underscore). +-- +-- * The period may be useful for larger specifications, to express +-- some module structure (as in "tcp.throughput" vs. +-- "udp.throughput"). +-- +-- * A number of names are predefined in the CDDL prelude, as listed +-- in Appendix D. +-- +-- * Rule names (types or groups) do not appear in the actual CBOR +-- encoding, but names used as "barewords" in member keys do. +newtype Name = Name {unName :: T.Text} + deriving (Generic) + deriving (Eq, Ord, Show) + deriving newtype (Semigroup, Monoid) + +instance Pretty Name where + pretty (Name name) = pretty name + +instance CollectComments Name where + collectComments _ = [] + +instance Hashable Name + +-- | A reference to a generic parameter inside the body of a generic rule. +newtype GRef = GRef T.Text + deriving (Show) + +instance IsString Name where + fromString = Name . T.pack + +data RuleTerm + = SingleTerm Term + | PairTerm Term Term + | GroupTerm [RuleTerm] + deriving (Eq, Ord, Show) + +-- Bounds + +uintMax :: Integer +uintMax = 2 ^ (64 :: Int) - 1 + +nintMin :: Integer +nintMin = -(2 ^ (64 :: Int)) diff --git a/src/Codec/CBOR/Cuddle/CBOR/Gen.hs b/src/Codec/CBOR/Cuddle/Generator.hs similarity index 98% rename from src/Codec/CBOR/Cuddle/CBOR/Gen.hs rename to src/Codec/CBOR/Cuddle/Generator.hs index 5fe79e25..f728558d 100644 --- a/src/Codec/CBOR/Cuddle/CBOR/Gen.hs +++ b/src/Codec/CBOR/Cuddle/Generator.hs @@ -14,11 +14,13 @@ {-# LANGUAGE ViewPatterns #-} -- | Generate example CBOR given a CDDL specification -module Codec.CBOR.Cuddle.CBOR.Gen ( +module Codec.CBOR.Cuddle.Generator ( generateFromName, generateFromGRef, GenPhase, XXCTree (..), + GenConfig (..), + runCBORGen, ) where #if MIN_VERSION_random(1,3,0) @@ -39,22 +41,22 @@ import Codec.CBOR.Cuddle.CDDL.CTree ( ) import Codec.CBOR.Cuddle.CDDL.CTree qualified as CTree import Codec.CBOR.Cuddle.CDDL.CtlOp qualified as CtlOp -import Codec.CBOR.Cuddle.CDDL.Custom.Core (MonadCddl (..), RuleTerm (..)) -import Codec.CBOR.Cuddle.CDDL.Custom.Generator ( +import Codec.CBOR.Cuddle.CDDL.Resolve (XXCTree (..), showSimple) +import Codec.CBOR.Cuddle.Core (MonadCddl (..), RuleTerm (..)) +import Codec.CBOR.Cuddle.Generator.Core ( CBORGen, GenConfig (..), - GenEnv (..), GenPhase, + askTwiddle, disableTwiddle, liftAntiGen, + runCBORGen, withAntiGen, ) -import Codec.CBOR.Cuddle.CDDL.Resolve (XXCTree (..), showSimple) import Codec.CBOR.Term (Term (..)) import Codec.CBOR.Term qualified as CBOR import Codec.CBOR.Write qualified as CBOR import Control.Monad (zipWithM, (<=<)) -import Control.Monad.Reader (asks) import Data.ByteString (ByteString) import Data.ByteString.Lazy qualified as LBS import Data.Char (chr) @@ -143,28 +145,28 @@ genHalf = do twiddleString :: Text -> CBORGen Term twiddleString t = do - twiddle <- asks (gcTwiddle . geConfig) + twiddle <- askTwiddle if twiddle then ($ t) <$> elements [TString, TStringI . TL.fromStrict] else pure $ TString t twiddleList :: [Term] -> CBORGen Term twiddleList t = do - twiddle <- asks (gcTwiddle . geConfig) + twiddle <- askTwiddle if twiddle then ($ t) <$> elements [TList, TListI] else pure $ TList t twiddleBytes :: ByteString -> CBORGen Term twiddleBytes t = do - twiddle <- asks (gcTwiddle . geConfig) + twiddle <- askTwiddle if twiddle then ($ t) <$> elements [TBytes, TBytesI . LBS.fromStrict] else pure $ TBytes t twiddleMap :: [(Term, Term)] -> CBORGen Term twiddleMap t = do - twiddle <- asks (gcTwiddle . geConfig) + twiddle <- askTwiddle if twiddle then ($ t) <$> elements [TMap, TMapI] else pure $ TMap t diff --git a/src/Codec/CBOR/Cuddle/CDDL/Custom/Generator.hs b/src/Codec/CBOR/Cuddle/Generator/Core.hs similarity index 90% rename from src/Codec/CBOR/Cuddle/CDDL/Custom/Generator.hs rename to src/Codec/CBOR/Cuddle/Generator/Core.hs index 8647ecfa..b9508be3 100644 --- a/src/Codec/CBOR/Cuddle/CDDL/Custom/Generator.hs +++ b/src/Codec/CBOR/Cuddle/Generator/Core.hs @@ -1,24 +1,26 @@ {-# LANGUAGE TypeData #-} {-# LANGUAGE TypeFamilies #-} -module Codec.CBOR.Cuddle.CDDL.Custom.Generator ( +module Codec.CBOR.Cuddle.Generator.Core ( GenPhase, + GenConfig (..), CBORGen, - XXCTree (..), HasGenerator (..), - GenConfig (..), - GenEnv (..), + XXCTree (..), liftAntiGen, runCBORGen, withAntiGen, withLocalGenBindings, + + -- * Twiddling + withTwiddle, disableTwiddle, enableTwiddle, + askTwiddle, ) where -import Codec.CBOR.Cuddle.CDDL (GRef (..), Name (..)) -import Codec.CBOR.Cuddle.CDDL.CTree (CTree, CTreeRoot (..), XXCTree) -import Codec.CBOR.Cuddle.CDDL.Custom.Core (MonadCddl (..), RuleTerm) +import Codec.CBOR.Cuddle.CDDL.CTree (CTree, CTreeRoot (..), MonadCddl (..), XXCTree) +import Codec.CBOR.Cuddle.Core (GRef (..), Name (..), RuleTerm) import Control.Monad.Reader (MonadReader (..), ReaderT (..), asks, mapReaderT) import Data.Map.Strict (Map) import Data.Map.Strict qualified as Map @@ -101,3 +103,6 @@ data instance XXCTree GenPhase class HasGenerator a where generatorL :: Lens' a (Maybe (CBORGen RuleTerm)) + +askTwiddle :: CBORGen Bool +askTwiddle = asks $ gcTwiddle . geConfig diff --git a/src/Codec/CBOR/Cuddle/Huddle.hs b/src/Codec/CBOR/Cuddle/Huddle.hs index a11abddc..a6ba7458 100644 --- a/src/Codec/CBOR/Cuddle/Huddle.hs +++ b/src/Codec/CBOR/Cuddle/Huddle.hs @@ -26,7 +26,7 @@ module Codec.CBOR.Cuddle.Huddle ( Type0 (..), -- * AST extensions - HuddleStage, + HuddlePhase, C.XCddl (..), C.XTerm (..), C.XRule (..), @@ -111,7 +111,6 @@ where import Codec.CBOR.Cuddle.CDDL ( CDDL, - GRef (..), GenericParameter (..), HasName (..), Name (..), @@ -119,11 +118,11 @@ import Codec.CBOR.Cuddle.CDDL ( ) import Codec.CBOR.Cuddle.CDDL qualified as C import Codec.CBOR.Cuddle.CDDL.CtlOp qualified as CtlOp -import Codec.CBOR.Cuddle.CDDL.Custom.Core (RuleTerm) -import Codec.CBOR.Cuddle.CDDL.Custom.Generator (CBORGen, HasGenerator (..)) -import Codec.CBOR.Cuddle.CDDL.Custom.Validator (HasValidator (..), TermValidator) import Codec.CBOR.Cuddle.Comments (Comment, HasComment (..)) import Codec.CBOR.Cuddle.Comments qualified as C +import Codec.CBOR.Cuddle.Core (GRef (..), RuleTerm) +import Codec.CBOR.Cuddle.Generator.Core (CBORGen, HasGenerator (..)) +import Codec.CBOR.Cuddle.Validator.Core (HasValidator (..), TermValidator) import Control.Monad (when) import Control.Monad.State (MonadState (get), State, execState, modify) import Data.ByteString (ByteString) @@ -147,36 +146,36 @@ import Optics.Core (lens, view, (%), (%~), (&)) import Optics.Core qualified as L import Prelude hiding ((/)) -type data HuddleStage +type data HuddlePhase -newtype instance C.XTerm HuddleStage = HuddleXTerm C.Comment +newtype instance C.XTerm HuddlePhase = HuddleXTerm C.Comment deriving (Generic, Semigroup, Monoid, Show, Eq) -newtype instance C.XCddl HuddleStage = HuddleXCddl [C.Comment] +newtype instance C.XCddl HuddlePhase = HuddleXCddl [C.Comment] deriving (Generic, Semigroup, Monoid, Show, Eq) -data instance C.XRule HuddleStage = HuddleXRule +data instance C.XRule HuddlePhase = HuddleXRule { hxrComment :: C.Comment , hxrGenerator :: Maybe (CBORGen RuleTerm) , hxrValidator :: Maybe TermValidator } deriving (Generic) -instance HasComment (C.XRule HuddleStage) where +instance HasComment (C.XRule HuddlePhase) where commentL = #hxrComment -instance HasValidator (C.XRule HuddleStage) where +instance HasValidator (C.XRule HuddlePhase) where validatorL = #hxrValidator -instance HasGenerator (C.XRule HuddleStage) where +instance HasGenerator (C.XRule HuddlePhase) where generatorL = #hxrGenerator -instance Default (XRule HuddleStage) +instance Default (XRule HuddlePhase) -newtype instance C.XXTopLevel HuddleStage = HuddleXXTopLevel C.Comment +newtype instance C.XXTopLevel HuddlePhase = HuddleXXTopLevel C.Comment deriving (Generic, Semigroup, Monoid, Show, Eq) -newtype instance C.XXType2 HuddleStage = HuddleXXType2 Void +newtype instance C.XXType2 HuddlePhase = HuddleXXType2 Void deriving (Generic, Semigroup, Show, Eq) -- | Add a description to a rule or group entry, to be included as a comment. @@ -186,7 +185,7 @@ comment desc n = n & commentL %~ (<> desc) data Rule = Rule { ruleName :: Name , ruleDefinition :: Type0 - , ruleExtra :: XRule HuddleStage + , ruleExtra :: XRule HuddlePhase } deriving (Generic) @@ -205,7 +204,7 @@ instance HasName Rule where data GroupDef = GroupDef { gdName :: Name , gdDefinition :: Group - , gdExt :: XRule HuddleStage + , gdExt :: XRule HuddlePhase } deriving (Generic) @@ -527,7 +526,7 @@ unconstrained v = Constrained (CValue v) def [] -- | A constraint on a 'Value' is something applied via CtlOp or RangeOp on a -- Type2, forming a Type1. data ValueConstraint a = ValueConstraint - { applyConstraint :: C.Type2 HuddleStage -> C.Type1 HuddleStage + { applyConstraint :: C.Type2 HuddlePhase -> C.Type1 HuddlePhase , showConstraint :: String } @@ -554,7 +553,7 @@ instance IsSizeable CGRefType -- | Things which can be used on the RHS of the '.size' operator. class IsSize a where - sizeAsCDDL :: a -> C.Type2 HuddleStage + sizeAsCDDL :: a -> C.Type2 HuddlePhase sizeAsString :: a -> String instance IsSize Word where @@ -1022,13 +1021,13 @@ data GRule a = GRule data GRuleCall = GRuleCall { grcName :: Name , grcBody :: GRule Type2 - , grcExtra :: XRule HuddleStage + , grcExtra :: XRule HuddlePhase } data GRuleDef = GRuleDef { grdName :: Name , grdBody :: GRule GRef - , grdExtra :: XRule HuddleStage + , grdExtra :: XRule HuddlePhase } instance HasName GRuleDef where @@ -1185,11 +1184,11 @@ defaultHuddleConfig = } -- | Convert from Huddle to CDDL, generating a top level root element. -toCDDL :: Huddle -> CDDL HuddleStage +toCDDL :: Huddle -> CDDL HuddlePhase toCDDL = toCDDL' defaultHuddleConfig -- | Convert from Huddle to CDDL, skipping a root element. -toCDDLNoRoot :: Huddle -> CDDL HuddleStage +toCDDLNoRoot :: Huddle -> CDDL HuddlePhase toCDDLNoRoot = toCDDL' defaultHuddleConfig @@ -1197,7 +1196,7 @@ toCDDLNoRoot = } -- | Convert from Huddle to CDDL for the purpose of pretty-printing. -toCDDL' :: HuddleConfig -> Huddle -> CDDL HuddleStage +toCDDL' :: HuddleConfig -> Huddle -> CDDL HuddlePhase toCDDL' HuddleConfig {..} hdl = C.fromRules . failOnDuplicate @@ -1222,12 +1221,12 @@ toCDDL' HuddleConfig {..} hdl = toCDDLItem (HIRule r) = toCDDLRule r toCDDLItem (HIGroup g) = toCDDLGroupDef g toCDDLItem (HIGRule g) = toGenRuleDef g - toTopLevelPseudoRoot :: [Rule] -> C.Rule HuddleStage + toTopLevelPseudoRoot :: [Rule] -> C.Rule HuddlePhase toTopLevelPseudoRoot topRs = toCDDLRule $ comment "Pseudo-rule introduced by Cuddle to collect root elements" $ "huddle_root_defs" =:= arr (fromList (fmap a topRs)) - toCDDLRule :: Rule -> C.Rule HuddleStage + toCDDLRule :: Rule -> C.Rule HuddlePhase toCDDLRule (Rule n (Type0 t0) extra) = ( \x -> C.Rule n Nothing C.AssignEq x extra @@ -1246,13 +1245,13 @@ toCDDL' HuddleConfig {..} hdl = toCDDLValue' (LBytes b) = C.VBytes b toCDDLValue' (LBool b) = C.VBool b - mapToCDDLGroup :: Map -> C.Group HuddleStage + mapToCDDLGroup :: Map -> C.Group HuddlePhase mapToCDDLGroup xs = C.Group $ mapChoiceToCDDL <$> choiceToNE xs - mapChoiceToCDDL :: MapChoice -> C.GrpChoice HuddleStage + mapChoiceToCDDL :: MapChoice -> C.GrpChoice HuddlePhase mapChoiceToCDDL (MapChoice entries) = C.GrpChoice (fmap mapEntryToCDDL entries) mempty - mapEntryToCDDL :: MapEntry -> C.GroupEntry HuddleStage + mapEntryToCDDL :: MapEntry -> C.GroupEntry HuddlePhase mapEntryToCDDL (MapEntry k v occ cmnt) = C.GroupEntry (toOccurrenceIndicator occ) @@ -1266,7 +1265,7 @@ toCDDL' HuddleConfig {..} hdl = toOccurrenceIndicator (Occurs (Just 1) Nothing) = Just C.OIOneOrMore toOccurrenceIndicator (Occurs lb ub) = Just $ C.OIBounded lb ub - toCDDLType1 :: Type2 -> C.Type1 HuddleStage + toCDDLType1 :: Type2 -> C.Type1 HuddlePhase toCDDLType1 = \case T2Constrained (Constrained x constr _) -> -- TODO Need to handle choices at the top level @@ -1285,21 +1284,21 @@ toCDDL' HuddleConfig {..} hdl = T2Generic g -> C.Type1 (toGenericCall g) Nothing mempty T2GenericRef (GRef n) -> C.Type1 (C.T2Name (C.Name n) Nothing) Nothing mempty - toMemberKey :: Key -> C.MemberKey HuddleStage + toMemberKey :: Key -> C.MemberKey HuddlePhase toMemberKey (LiteralKey (Literal (LText t) _)) = C.MKBareword (C.Name t) toMemberKey (LiteralKey v) = C.MKValue $ toCDDLValue v toMemberKey (TypeKey t) = C.MKType (toCDDLType1 t) - toCDDLType0 :: Type0 -> C.Type0 HuddleStage + toCDDLType0 :: Type0 -> C.Type0 HuddlePhase toCDDLType0 = C.Type0 . fmap toCDDLType1 . choiceToNE . unType0 - arrayToCDDLGroup :: Array -> C.Group HuddleStage + arrayToCDDLGroup :: Array -> C.Group HuddlePhase arrayToCDDLGroup xs = C.Group $ arrayChoiceToCDDL <$> choiceToNE xs - arrayChoiceToCDDL :: ArrayChoice -> C.GrpChoice HuddleStage + arrayChoiceToCDDL :: ArrayChoice -> C.GrpChoice HuddlePhase arrayChoiceToCDDL (ArrayChoice entries cmt) = C.GrpChoice (fmap arrayEntryToCDDL entries) (HuddleXTerm cmt) - arrayEntryToCDDL :: ArrayEntry -> C.GroupEntry HuddleStage + arrayEntryToCDDL :: ArrayEntry -> C.GroupEntry HuddlePhase arrayEntryToCDDL (ArrayEntry k v occ cmnt) = C.GroupEntry (toOccurrenceIndicator occ) @@ -1324,7 +1323,7 @@ toCDDL' HuddleConfig {..} hdl = CRef r -> getName r CGRef (GRef n) -> C.Name n - toCDDLRanged :: Ranged -> C.Type1 HuddleStage + toCDDLRanged :: Ranged -> C.Type1 HuddlePhase toCDDLRanged (Unranged x) = C.Type1 (C.T2Value $ toCDDLValue x) Nothing mempty toCDDLRanged (Ranged lb ub rop) = @@ -1333,11 +1332,11 @@ toCDDL' HuddleConfig {..} hdl = (Just (C.RangeOp rop, toCDDLRangeBound ub)) mempty - toCDDLRangeBound :: RangeBound -> C.Type2 HuddleStage + toCDDLRangeBound :: RangeBound -> C.Type2 HuddlePhase toCDDLRangeBound (RangeBoundLiteral l) = C.T2Value $ toCDDLValue l toCDDLRangeBound (RangeBoundRef n _) = C.T2Name n Nothing - toCDDLGroupDef :: GroupDef -> C.Rule HuddleStage + toCDDLGroupDef :: GroupDef -> C.Rule HuddlePhase toCDDLGroupDef (GroupDef n (Group t0s) extra) = C.Rule n @@ -1355,13 +1354,13 @@ toCDDL' HuddleConfig {..} hdl = ) extra - toGenericCall :: GRuleCall -> C.Type2 HuddleStage + toGenericCall :: GRuleCall -> C.Type2 HuddlePhase toGenericCall (GRuleCall n gr _) = C.T2Name n (Just . C.GenericArg $ fmap toCDDLType1 (args gr)) - toGenRuleDef :: GRuleDef -> C.Rule HuddleStage + toGenRuleDef :: GRuleDef -> C.Rule HuddlePhase toGenRuleDef (GRuleDef n gr extra) = C.Rule n diff --git a/src/Codec/CBOR/Cuddle/IndexMappable.hs b/src/Codec/CBOR/Cuddle/IndexMappable.hs index a4804b6b..91d50ab2 100644 --- a/src/Codec/CBOR/Cuddle/IndexMappable.hs +++ b/src/Codec/CBOR/Cuddle/IndexMappable.hs @@ -31,7 +31,7 @@ import Codec.CBOR.Cuddle.CDDL.CTreePhase ( XTerm (..), ) import Codec.CBOR.Cuddle.Huddle ( - HuddleStage, + HuddlePhase, XCddl (..), XRule (..), XTerm (..), @@ -39,17 +39,14 @@ import Codec.CBOR.Cuddle.Huddle ( XXType2 (..), ) import Codec.CBOR.Cuddle.Parser ( - ParserStage, + ParserPhase, XCddl (..), - XRule (..), XTerm (..), XXTopLevel (..), XXType2 (..), ) -import Codec.CBOR.Cuddle.Pretty (PrettyStage, XCddl (..), XRule (..), XTerm (..), XXTopLevel (..)) import Data.Bifunctor (Bifunctor (..)) import Data.Coerce (Coercible, coerce) -import Data.Void (absurd) class IndexMappable f i j where mapIndex :: f i -> f j @@ -201,90 +198,56 @@ instance where mapIndex (GrpChoice gs e) = GrpChoice (mapIndex <$> gs) $ mapIndex e --- ParserStage ~ PrettyStage - -instance IndexMappable XCddl ParserStage PrettyStage where - mapIndex (ParserXCddl c) = PrettyXCddl c - -instance IndexMappable XTerm ParserStage PrettyStage where - mapIndex (ParserXTerm c) = PrettyXTerm c - -instance IndexMappable XRule ParserStage PrettyStage where - mapIndex (ParserXRule c) = PrettyXRule c - -instance IndexMappable XXType2 ParserStage PrettyStage where - mapIndex (ParserXXType2 v) = absurd v - -instance IndexMappable XXTopLevel ParserStage PrettyStage where - mapIndex (ParserXXTopLevel c) = PrettyXXTopLevel c - --- ParserStage -> CTreePhase +-- ParserPhase -> CTreePhase -instance IndexMappable XCddl ParserStage CTreePhase where +instance IndexMappable XCddl ParserPhase CTreePhase where mapIndex _ = CTreeXCddl -instance IndexMappable XXType2 ParserStage CTreePhase where +instance IndexMappable XXType2 ParserPhase CTreePhase where mapIndex (ParserXXType2 c) = case c of {} -instance IndexMappable XTerm ParserStage CTreePhase where +instance IndexMappable XTerm ParserPhase CTreePhase where mapIndex _ = CTreeXTerm -instance IndexMappable XRule ParserStage CTreePhase where +instance IndexMappable XRule ParserPhase CTreePhase where mapIndex _ = CTreeXRule Nothing Nothing --- ParserStage -> HuddleStage +-- ParserPhase -> HuddlePhase -instance IndexMappable XCddl ParserStage HuddleStage where +instance IndexMappable XCddl ParserPhase HuddlePhase where mapIndex (ParserXCddl c) = HuddleXCddl c -instance IndexMappable XXTopLevel ParserStage HuddleStage where +instance IndexMappable XXTopLevel ParserPhase HuddlePhase where mapIndex (ParserXXTopLevel c) = HuddleXXTopLevel c -instance IndexMappable XXType2 ParserStage HuddleStage where +instance IndexMappable XXType2 ParserPhase HuddlePhase where mapIndex (ParserXXType2 c) = HuddleXXType2 c -instance IndexMappable XTerm ParserStage HuddleStage where +instance IndexMappable XTerm ParserPhase HuddlePhase where mapIndex (ParserXTerm c) = HuddleXTerm c --- HuddleStage -> CTreePhase +-- HuddlePhase -> CTreePhase -instance IndexMappable XCddl HuddleStage CTreePhase where +instance IndexMappable XCddl HuddlePhase CTreePhase where mapIndex _ = CTreeXCddl -instance IndexMappable XXType2 HuddleStage CTreePhase where +instance IndexMappable XXType2 HuddlePhase CTreePhase where mapIndex (HuddleXXType2 c) = case c of {} -instance IndexMappable XTerm HuddleStage CTreePhase where +instance IndexMappable XTerm HuddlePhase CTreePhase where mapIndex _ = CTreeXTerm -instance IndexMappable XRule HuddleStage CTreePhase where +instance IndexMappable XRule HuddlePhase CTreePhase where mapIndex (HuddleXRule _ g v) = CTreeXRule g v --- HuddleStage -> PrettyStage - -instance IndexMappable XCddl HuddleStage PrettyStage where - mapIndex (HuddleXCddl c) = PrettyXCddl c - -instance IndexMappable XXTopLevel HuddleStage PrettyStage where - mapIndex (HuddleXXTopLevel c) = PrettyXXTopLevel c - -instance IndexMappable XXType2 HuddleStage PrettyStage where - mapIndex (HuddleXXType2 c) = absurd c - -instance IndexMappable XTerm HuddleStage PrettyStage where - mapIndex (HuddleXTerm c) = PrettyXTerm c - -instance IndexMappable XRule HuddleStage PrettyStage where - mapIndex (HuddleXRule c _ _) = PrettyXRule c - --- ParserStage ~ ParserStage +-- ParserPhase ~ ParserPhase -instance IndexMappable XCddl ParserStage ParserStage +instance IndexMappable XCddl ParserPhase ParserPhase -instance IndexMappable XXTopLevel ParserStage ParserStage +instance IndexMappable XXTopLevel ParserPhase ParserPhase -instance IndexMappable XXType2 ParserStage ParserStage +instance IndexMappable XXType2 ParserPhase ParserPhase -instance IndexMappable XTerm ParserStage ParserStage +instance IndexMappable XTerm ParserPhase ParserPhase -instance IndexMappable XRule ParserStage ParserStage +instance IndexMappable XRule ParserPhase ParserPhase diff --git a/src/Codec/CBOR/Cuddle/Parser.hs b/src/Codec/CBOR/Cuddle/Parser.hs index 11be7afb..e53499fc 100644 --- a/src/Codec/CBOR/Cuddle/Parser.hs +++ b/src/Codec/CBOR/Cuddle/Parser.hs @@ -44,30 +44,30 @@ import Text.Megaparsec.Char hiding (space) import Text.Megaparsec.Char qualified as C import Text.Megaparsec.Char.Lexer qualified as L -type data ParserStage +type data ParserPhase -newtype instance XXTopLevel ParserStage = ParserXXTopLevel Comment +newtype instance XXTopLevel ParserPhase = ParserXXTopLevel Comment deriving (Generic, Show, Eq) -newtype instance XXType2 ParserStage = ParserXXType2 Void +newtype instance XXType2 ParserPhase = ParserXXType2 Void deriving (Generic, Show, Eq) -newtype instance XTerm ParserStage = ParserXTerm {unParserXTerm :: Comment} +newtype instance XTerm ParserPhase = ParserXTerm {unParserXTerm :: Comment} deriving (Generic, Semigroup, Monoid, Show, Eq) -newtype instance XRule ParserStage = ParserXRule {unParserXRule :: Comment} +newtype instance XRule ParserPhase = ParserXRule {unParserXRule :: Comment} deriving (Generic, Semigroup, Monoid, Show, Eq) -newtype instance XCddl ParserStage = ParserXCddl [Comment] +newtype instance XCddl ParserPhase = ParserXCddl [Comment] deriving (Generic, Semigroup, Monoid, Show, Eq) -instance HasComment (XTerm ParserStage) where +instance HasComment (XTerm ParserPhase) where commentL = #unParserXTerm -instance HasComment (XRule ParserStage) where +instance HasComment (XRule ParserPhase) where commentL = #unParserXRule -pCDDL :: Parser (CDDL ParserStage) +pCDDL :: Parser (CDDL ParserPhase) pCDDL = do initialComments <- many (try $ C.space *> pCommentBlock <* notFollowedBy pRule) initialRuleComment <- C.space *> optional pCommentBlock @@ -79,7 +79,7 @@ pCDDL = do cddlTail (ParserXXTopLevel <$> initialComments) -pTopLevel :: Parser (TopLevel ParserStage) +pTopLevel :: Parser (TopLevel ParserPhase) pTopLevel = try tlRule <|> tlComment where tlRule = do @@ -88,7 +88,7 @@ pTopLevel = try tlRule <|> tlComment pure . TopLevelRule $ appendComment rule (fold mCmt) tlComment = XXTopLevel . ParserXXTopLevel <$> pCommentBlock -pRule :: Parser (Rule ParserStage) +pRule :: Parser (Rule ParserPhase) pRule = do name <- pName genericParam <- optcomp pGenericParameters @@ -132,23 +132,23 @@ pAssignG = , AssignExt <$ "//=" ] -pGenericParameter :: Parser (GenericParameter ParserStage) +pGenericParameter :: Parser (GenericParameter ParserPhase) pGenericParameter = GenericParameter <$> pName <*> pure mempty -pGenericParameters :: Parser (GenericParameters ParserStage) +pGenericParameters :: Parser (GenericParameters ParserPhase) pGenericParameters = GenericParameters <$> between "<" ">" (NE.sepBy1 (space !*> pGenericParameter <*! space) ",") -pGenericArg :: Parser (GenericArg ParserStage) +pGenericArg :: Parser (GenericArg ParserPhase) pGenericArg = GenericArg <$> between "<" ">" (NE.sepBy1 (space !*> pType1 <*! space) ",") -pType0 :: Parser (Type0 ParserStage) +pType0 :: Parser (Type0 ParserPhase) pType0 = Type0 <$> sepBy1' (space !*> pType1 <*! trailingSpace) (try "/") -pType1 :: Parser (Type1 ParserStage) +pType1 :: Parser (Type1 ParserPhase) pType1 = do v <- pType2 rest <- optional $ do @@ -164,7 +164,7 @@ pType1 = do pure $ Type1 v (Just (tyOp, w)) . ParserXTerm $ cmtFst <> cmtSnd Nothing -> pure $ Type1 v Nothing mempty -pType2 :: Parser (Type2 ParserStage) +pType2 :: Parser (Type2 ParserPhase) pType2 = choice [ T2Value <$> pValue @@ -228,13 +228,13 @@ pCtlOp = ] ) -pGroup :: Parser (Group ParserStage) +pGroup :: Parser (Group ParserPhase) pGroup = Group <$> NE.sepBy1 (space !*> pGrpChoice) "//" -pGrpChoice :: Parser (GrpChoice ParserStage) +pGrpChoice :: Parser (GrpChoice ParserPhase) pGrpChoice = GrpChoice <$> many (space !*> pGrpEntry <*! pOptCom) <*> mempty -pGrpEntry :: Parser (GroupEntry ParserStage) +pGrpEntry :: Parser (GroupEntry ParserPhase) pGrpEntry = do occur <- optcomp pOccur cmt <- space @@ -249,7 +249,7 @@ pGrpEntry = do ] pure $ GroupEntry occur variant (ParserXTerm $ cmt <> cmt') -pMemberKey :: Parser (WithComment (MemberKey ParserStage)) +pMemberKey :: Parser (WithComment (MemberKey ParserPhase)) pMemberKey = choice [ try $ do diff --git a/src/Codec/CBOR/Cuddle/Pretty.hs b/src/Codec/CBOR/Cuddle/Pretty.hs index 2325b58a..95b070f1 100644 --- a/src/Codec/CBOR/Cuddle/Pretty.hs +++ b/src/Codec/CBOR/Cuddle/Pretty.hs @@ -10,7 +10,7 @@ module Codec.CBOR.Cuddle.Pretty ( CommentRender (..), - PrettyStage, + PrettyPhase, XXTopLevel (..), XXType2 (..), XTerm (..), @@ -23,7 +23,25 @@ import Codec.CBOR.Cuddle.CDDL import Codec.CBOR.Cuddle.CDDL.CTree (CTree, PTerm (..), XXCTree) import Codec.CBOR.Cuddle.CDDL.CTree qualified as CT import Codec.CBOR.Cuddle.CDDL.CtlOp (CtlOp) +import Codec.CBOR.Cuddle.CDDL.Resolve (DistRef (..)) import Codec.CBOR.Cuddle.Comments (CollectComments (..), Comment, HasComment (..), unComment) +import Codec.CBOR.Cuddle.Huddle ( + HuddlePhase, + XCddl (..), + XRule (..), + XTerm (..), + XXTopLevel (..), + XXType2 (..), + ) +import Codec.CBOR.Cuddle.IndexMappable (IndexMappable (..)) +import Codec.CBOR.Cuddle.Parser ( + ParserPhase, + XCddl (..), + XRule (..), + XTerm (..), + XXTopLevel (..), + XXType2 (..), + ) import Codec.CBOR.Cuddle.Pretty.Columnar ( Cell (..), CellAlign (..), @@ -51,40 +69,37 @@ import Optics.Core ((^.)) import Prettyprinter import Prettyprinter.Render.Text qualified as PT -type data PrettyStage +type data PrettyPhase -newtype instance XXTopLevel PrettyStage = PrettyXXTopLevel Comment +newtype instance XXTopLevel PrettyPhase = PrettyXXTopLevel Comment deriving (Generic, CollectComments, Show, Eq) -newtype instance XXType2 PrettyStage = PrettyXXType2 Void +newtype instance XXType2 PrettyPhase = PrettyXXType2 Void deriving (Generic, CollectComments, Show, Eq) -newtype instance XTerm PrettyStage = PrettyXTerm {unPrettyXTerm :: Comment} +newtype instance XTerm PrettyPhase = PrettyXTerm {unPrettyXTerm :: Comment} deriving (Generic, CollectComments, Semigroup, Monoid, IsString, Show, Eq) -newtype instance XCddl PrettyStage = PrettyXCddl [Comment] +newtype instance XCddl PrettyPhase = PrettyXCddl [Comment] deriving (Generic, CollectComments, Show, Eq) -newtype instance XRule PrettyStage = PrettyXRule {unPrettyXRule :: Comment} +newtype instance XRule PrettyPhase = PrettyXRule {unPrettyXRule :: Comment} deriving (Generic, CollectComments, Show, Eq) deriving newtype (Default) -instance HasComment (XTerm PrettyStage) where +instance HasComment (XTerm PrettyPhase) where commentL = #unPrettyXTerm -instance HasComment (XRule PrettyStage) where +instance HasComment (XRule PrettyPhase) where commentL = #unPrettyXRule -instance Pretty (CDDL PrettyStage) where - pretty = vsep . fmap pretty . NE.toList . cddlTopLevel +prettyCDDL :: CDDL PrettyPhase -> Doc ann +prettyCDDL = vsep . fmap pretty . NE.toList . cddlTopLevel -instance Pretty (TopLevel PrettyStage) where +instance Pretty (TopLevel PrettyPhase) where pretty (XXTopLevel (PrettyXXTopLevel cmt)) = pretty cmt pretty (TopLevelRule x) = pretty x <> hardline -instance Pretty Name where - pretty (Name name) = pretty name - data CommentRender = PreComment | PostComment @@ -102,10 +117,10 @@ instance Pretty Comment where | c == mempty = mempty | otherwise = prettyCommentNoBreak c <> hardline -type0Def :: Type0 PrettyStage -> Doc ann +type0Def :: Type0 PrettyPhase -> Doc ann type0Def t = nest 2 $ line' <> pretty t -instance Pretty (Rule PrettyStage) where +instance Pretty (Rule PrettyPhase) where pretty (Rule n mgen assign tog cmt) = pretty (cmt ^. commentL) <> groupIfNoComments @@ -122,18 +137,18 @@ instance Pretty (Rule PrettyStage) where AssignEq -> "=" AssignExt -> "//=" -instance Pretty (GenericArg PrettyStage) where +instance Pretty (GenericArg PrettyPhase) where pretty (GenericArg l) = "<" <> concatWith (\x y -> x <> "," <+> y) (pretty <$> NE.toList l) <> ">" -instance Pretty (GenericParameter PrettyStage) where +instance Pretty (GenericParameter PrettyPhase) where pretty (GenericParameter n (PrettyXTerm c)) = pretty n <> prettyCommentNoBreakWS c -instance Pretty (GenericParameters PrettyStage) where +instance Pretty (GenericParameters PrettyPhase) where pretty (GenericParameters l) = "<" <> concatWith (\x y -> x <> "," <+> y) (pretty <$> NE.toList l) <> ">" -instance Pretty (Type0 PrettyStage) where +instance Pretty (Type0 PrettyPhase) where pretty t0@(Type0 (NE.toList -> l)) = groupIfNoComments t0 $ columnarSepBy "/" . Columnar $ type1ToRow <$> l where @@ -153,7 +168,7 @@ instance Pretty TyOp where pretty (RangeOp Closed) = ".." pretty (CtrlOp n) = "." <> pretty n -instance Pretty (Type1 PrettyStage) where +instance Pretty (Type1 PrettyPhase) where pretty (Type1 t2 Nothing (PrettyXTerm cmt)) = groupIfNoComments t2 (pretty t2) <> prettyCommentNoBreakWS cmt pretty (Type1 t2 (Just (tyop, t2')) (PrettyXTerm cmt)) = groupIfNoComments t2 (pretty t2) @@ -161,7 +176,7 @@ instance Pretty (Type1 PrettyStage) where <+> groupIfNoComments t2' (pretty t2') <> prettyCommentNoBreakWS cmt -instance Pretty (Type2 PrettyStage) where +instance Pretty (Type2 PrettyPhase) where pretty (T2Value v) = pretty v pretty (T2Name n mg) = pretty n <> pretty mg pretty (T2Group g) = cEncloseSep "(" ")" mempty [pretty g] @@ -215,7 +230,7 @@ groupIfNoComments x | not (any (mempty /=) $ collectComments x) = group | otherwise = id -columnarGroupChoice :: GrpChoice PrettyStage -> Columnar ann +columnarGroupChoice :: GrpChoice PrettyPhase -> Columnar ann columnarGroupChoice (GrpChoice ges _cmt) = Columnar grpEntryRows where groupEntryRow (GroupEntry oi gev (PrettyXTerm cmt)) = @@ -229,7 +244,7 @@ columnarGroupChoice (GrpChoice ges _cmt) = Columnar grpEntryRows groupEntryVariantCells (GEGroup g) = [Cell (prettyGroup AsGroup g) LeftAlign, emptyCell] grpEntryRows = groupEntryRow <$> ges -prettyGroup :: GroupRender -> Group PrettyStage -> Doc ann +prettyGroup :: GroupRender -> Group PrettyPhase -> Doc ann prettyGroup gr g@(Group (toList -> xs)) = groupIfNoComments g . columnarListing (lEnc <> softspace) rEnc "// " . Columnar $ (\x -> singletonRow . groupIfNoComments x . columnarSepBy "," $ columnarGroupChoice x) <$> xs @@ -239,10 +254,10 @@ prettyGroup gr g@(Group (toList -> xs)) = AsArray -> ("[", "]") AsGroup -> ("(", ")") -instance Pretty (GroupEntry PrettyStage) where +instance Pretty (GroupEntry PrettyPhase) where pretty ge = prettyColumnar . columnarGroupChoice $ GrpChoice [ge] mempty -instance Pretty (MemberKey PrettyStage) where +instance Pretty (MemberKey PrettyPhase) where pretty (MKType t1) = pretty t1 pretty (MKBareword n) = pretty n pretty (MKValue v) = pretty v @@ -278,7 +293,7 @@ instance Pretty PTerm where PTUndefined -> "undefined" -- | Render a pretty-stage CDDL to 'Text', removing trailing whitespace. -renderCDDL :: LayoutOptions -> CDDL PrettyStage -> Text +renderCDDL :: LayoutOptions -> CDDL PrettyPhase -> Text renderCDDL opts = PT.renderStrict . removeTrailingWhitespace . layoutPretty opts . pretty @@ -304,3 +319,67 @@ instance Pretty (XXCTree p) => Pretty (CTree p) where CT.Enum e -> "&" <> pretty e CT.Unwrap x -> "~" <> pretty x CT.Tag t x -> "#6." <> pretty t <> "(" <> pretty x <> ")" + +instance + ( IndexMappable XCddl p PrettyPhase + , IndexMappable XXTopLevel p PrettyPhase + , IndexMappable XXType2 p PrettyPhase + , IndexMappable XTerm p PrettyPhase + , IndexMappable XRule p PrettyPhase + ) => + Pretty (CDDL p) + where + pretty = prettyCDDL . mapIndex + +instance Pretty (XXCTree i) => Pretty (DistRef i) where + pretty (GenericRef n) = pretty n + pretty (RuleRef rule []) = pretty rule + pretty (RuleRef rule args) = pretty rule <> encloseSep "<" ">" "," (pretty <$> args) + +-- * IndexMappable + +-- ParserPhase -> PrettyPhase + +instance IndexMappable XCddl ParserPhase PrettyPhase where + mapIndex (ParserXCddl c) = PrettyXCddl c + +instance IndexMappable XTerm ParserPhase PrettyPhase where + mapIndex (ParserXTerm c) = PrettyXTerm c + +instance IndexMappable XRule ParserPhase PrettyPhase where + mapIndex (ParserXRule c) = PrettyXRule c + +instance IndexMappable XXType2 ParserPhase PrettyPhase where + mapIndex (ParserXXType2 v) = absurd v + +instance IndexMappable XXTopLevel ParserPhase PrettyPhase where + mapIndex (ParserXXTopLevel c) = PrettyXXTopLevel c + +-- HuddlePhase -> PrettyPhase + +instance IndexMappable XCddl HuddlePhase PrettyPhase where + mapIndex (HuddleXCddl c) = PrettyXCddl c + +instance IndexMappable XXTopLevel HuddlePhase PrettyPhase where + mapIndex (HuddleXXTopLevel c) = PrettyXXTopLevel c + +instance IndexMappable XXType2 HuddlePhase PrettyPhase where + mapIndex (HuddleXXType2 c) = absurd c + +instance IndexMappable XTerm HuddlePhase PrettyPhase where + mapIndex (HuddleXTerm c) = PrettyXTerm c + +instance IndexMappable XRule HuddlePhase PrettyPhase where + mapIndex (HuddleXRule c _ _) = PrettyXRule c + +-- PettyPhase ~ PrettyPhase + +instance IndexMappable XCddl PrettyPhase PrettyPhase + +instance IndexMappable XXTopLevel PrettyPhase PrettyPhase + +instance IndexMappable XXType2 PrettyPhase PrettyPhase + +instance IndexMappable XTerm PrettyPhase PrettyPhase + +instance IndexMappable XRule PrettyPhase PrettyPhase diff --git a/src/Codec/CBOR/Cuddle/CBOR/Validator.hs b/src/Codec/CBOR/Cuddle/Validator.hs similarity index 99% rename from src/Codec/CBOR/Cuddle/CBOR/Validator.hs rename to src/Codec/CBOR/Cuddle/Validator.hs index 2b9d89c0..8ffccf98 100644 --- a/src/Codec/CBOR/Cuddle/CBOR/Validator.hs +++ b/src/Codec/CBOR/Cuddle/Validator.hs @@ -3,14 +3,28 @@ {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE ViewPatterns #-} -module Codec.CBOR.Cuddle.CBOR.Validator ( +module Codec.CBOR.Cuddle.Validator ( validateCBOR, validateFromName, validateFromGRef, ValidatorPhase, ) where -import Codec.CBOR.Cuddle.CBOR.Validator.Trace ( +import Codec.CBOR.Cuddle.CDDL hiding (CDDL, Group, Rule) +import Codec.CBOR.Cuddle.CDDL.CTree +import Codec.CBOR.Cuddle.CDDL.CtlOp +import Codec.CBOR.Cuddle.CDDL.Resolve (XXCTree (..), showSimple) +import Codec.CBOR.Cuddle.Core (MonadCddl (..), RuleTerm (..)) +import Codec.CBOR.Cuddle.IndexMappable (IndexMappable (..)) +import Codec.CBOR.Cuddle.Validator.Core ( + CustomValidatorResult (..), + TermValidator, + Validator, + ValidatorPhase, + askCddl, + runValidator, + ) +import Codec.CBOR.Cuddle.Validator.Trace ( ControlInfo (..), Evidenced (..), IsValidationTrace (..), @@ -22,24 +36,8 @@ import Codec.CBOR.Cuddle.CBOR.Validator.Trace ( isValid, mapTrace, ) -import Codec.CBOR.Cuddle.CDDL hiding (CDDL, Group, Rule) -import Codec.CBOR.Cuddle.CDDL.CTree -import Codec.CBOR.Cuddle.CDDL.CtlOp -import Codec.CBOR.Cuddle.CDDL.Custom.Core (MonadCddl (..), RuleTerm (..)) -import Codec.CBOR.Cuddle.CDDL.Custom.Validator ( - CustomValidatorResult (..), - TermValidator, - ValidateEnv (..), - Validator, - ValidatorPhase, - XXCTree (..), - runValidator, - ) -import Codec.CBOR.Cuddle.CDDL.Resolve (showSimple) -import Codec.CBOR.Cuddle.IndexMappable (IndexMappable (..)) import Codec.CBOR.Read import Codec.CBOR.Term -import Control.Monad.Reader (asks) import Data.Bifunctor (Bifunctor (..)) import Data.Bits hiding (And) import Data.ByteString qualified as BS @@ -98,7 +96,7 @@ validateFromGRef ref term = do validateAgainst :: Term -> CTree ValidatorPhase -> Validator () validateAgainst term rule = do - cddl <- asks veRoot + cddl <- askCddl let res = validateTerm cddl term rule if isValid res then pure () diff --git a/src/Codec/CBOR/Cuddle/CDDL/Custom/Validator.hs b/src/Codec/CBOR/Cuddle/Validator/Core.hs similarity index 86% rename from src/Codec/CBOR/Cuddle/CDDL/Custom/Validator.hs rename to src/Codec/CBOR/Cuddle/Validator/Core.hs index 2fdd64a5..45eb8026 100644 --- a/src/Codec/CBOR/Cuddle/CDDL/Custom/Validator.hs +++ b/src/Codec/CBOR/Cuddle/Validator/Core.hs @@ -1,22 +1,20 @@ {-# LANGUAGE TypeData #-} {-# LANGUAGE TypeFamilies #-} -module Codec.CBOR.Cuddle.CDDL.Custom.Validator ( - TermValidator, +module Codec.CBOR.Cuddle.Validator.Core ( ValidatorPhase, Validator, - CustomValidatorResult (..), - XXCTree (..), + TermValidator, HasValidator (..), - ValidateEnv (..), + XXCTree (..), + CustomValidatorResult (..), withLocalValidateBindings, runValidator, + askCddl, ) where -import Codec.CBOR.Cuddle.CDDL (GRef (..), Name (..)) -import Codec.CBOR.Cuddle.CDDL.CTree (CTree, CTreeRoot (..)) -import Codec.CBOR.Cuddle.CDDL.Custom.Core (MonadCddl (..), RuleTerm) -import Codec.CBOR.Cuddle.CDDL.Custom.Generator (XXCTree) +import Codec.CBOR.Cuddle.CDDL.CTree (CTree, CTreeRoot (..), MonadCddl (..), XXCTree) +import Codec.CBOR.Cuddle.Core (GRef (..), Name (..), RuleTerm) import Control.Monad.Reader (MonadReader (..), ReaderT (..), asks) import Data.Map.Strict (Map) import Data.Map.Strict qualified as Map @@ -74,3 +72,6 @@ runValidator :: runValidator (Validator m) cddl = either CustomValidatorFailure (const CustomValidatorSuccess) $ runReaderT m ValidateEnv {veRoot = cddl, veLocal = Map.empty} + +askCddl :: Validator (CTreeRoot ValidatorPhase) +askCddl = asks veRoot diff --git a/src/Codec/CBOR/Cuddle/CBOR/Validator/Trace.hs b/src/Codec/CBOR/Cuddle/Validator/Trace.hs similarity index 99% rename from src/Codec/CBOR/Cuddle/CBOR/Validator/Trace.hs rename to src/Codec/CBOR/Cuddle/Validator/Trace.hs index 062348dc..33cd4a9d 100644 --- a/src/Codec/CBOR/Cuddle/CBOR/Validator/Trace.hs +++ b/src/Codec/CBOR/Cuddle/Validator/Trace.hs @@ -9,7 +9,7 @@ {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE UndecidableInstances #-} -module Codec.CBOR.Cuddle.CBOR.Validator.Trace ( +module Codec.CBOR.Cuddle.Validator.Trace ( SValidity (..), Validity (..), ValidationTrace (..), diff --git a/test/Main.hs b/test/Main.hs index 67504596..c8845aca 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -1,14 +1,14 @@ module Main (main) where import System.IO (BufferMode (..), hSetBuffering, hSetEncoding, stdout, utf8) -import Test.Codec.CBOR.Cuddle.CDDL.Examples qualified as Examples -import Test.Codec.CBOR.Cuddle.CDDL.GeneratorSpec qualified as Generator -import Test.Codec.CBOR.Cuddle.CDDL.Parser (parserSpec) -import Test.Codec.CBOR.Cuddle.CDDL.Pretty (roundtripSpec) -import Test.Codec.CBOR.Cuddle.CDDL.Pretty.Golden qualified as PrettyGolden -import Test.Codec.CBOR.Cuddle.CDDL.Validator qualified as Validator -import Test.Codec.CBOR.Cuddle.CDDL.Validator.Golden qualified as ValidatorGolden +import Test.Codec.CBOR.Cuddle.Examples qualified as Examples +import Test.Codec.CBOR.Cuddle.GeneratorSpec qualified as Generator import Test.Codec.CBOR.Cuddle.Huddle (huddleSpec) +import Test.Codec.CBOR.Cuddle.Parser (parserSpec) +import Test.Codec.CBOR.Cuddle.Pretty (roundtripSpec) +import Test.Codec.CBOR.Cuddle.Pretty.Golden qualified as PrettyGolden +import Test.Codec.CBOR.Cuddle.Validator qualified as Validator +import Test.Codec.CBOR.Cuddle.Validator.Golden qualified as ValidatorGolden import Test.Hspec import Test.Hspec.Runner diff --git a/test/Test/Codec/CBOR/Cuddle/CDDL/Examples.hs b/test/Test/Codec/CBOR/Cuddle/Examples.hs similarity index 97% rename from test/Test/Codec/CBOR/Cuddle/CDDL/Examples.hs rename to test/Test/Codec/CBOR/Cuddle/Examples.hs index fee3b2a2..f06e31c9 100644 --- a/test/Test/Codec/CBOR/Cuddle/CDDL/Examples.hs +++ b/test/Test/Codec/CBOR/Cuddle/Examples.hs @@ -1,6 +1,6 @@ {-# LANGUAGE OverloadedStrings #-} -module Test.Codec.CBOR.Cuddle.CDDL.Examples (spec) where +module Test.Codec.CBOR.Cuddle.Examples (spec) where import Codec.CBOR.Cuddle.CDDL (Value (..), ValueVariant (..)) import Codec.CBOR.Cuddle.CDDL.CTree (CTree (..), CTreeRoot, PTerm (..)) diff --git a/test/Test/Codec/CBOR/Cuddle/CDDL/Examples/Huddle.hs b/test/Test/Codec/CBOR/Cuddle/Examples/Huddle.hs similarity index 97% rename from test/Test/Codec/CBOR/Cuddle/CDDL/Examples/Huddle.hs rename to test/Test/Codec/CBOR/Cuddle/Examples/Huddle.hs index 582cf5d1..01304ce0 100644 --- a/test/Test/Codec/CBOR/Cuddle/CDDL/Examples/Huddle.hs +++ b/test/Test/Codec/CBOR/Cuddle/Examples/Huddle.hs @@ -2,7 +2,7 @@ {-# LANGUAGE OverloadedLists #-} {-# LANGUAGE OverloadedStrings #-} -module Test.Codec.CBOR.Cuddle.CDDL.Examples.Huddle ( +module Test.Codec.CBOR.Cuddle.Examples.Huddle ( huddleRangeArray, huddleArray, huddleMap, @@ -31,10 +31,9 @@ module Test.Codec.CBOR.Cuddle.CDDL.Examples.Huddle ( tagRangeExample, ) where -import Codec.CBOR.Cuddle.CBOR.Gen (generateFromGRef) -import Codec.CBOR.Cuddle.CBOR.Validator (validateFromGRef) import Codec.CBOR.Cuddle.CDDL (Name) -import Codec.CBOR.Cuddle.CDDL.Custom.Core (RuleTerm (..)) +import Codec.CBOR.Cuddle.Core (RuleTerm (..)) +import Codec.CBOR.Cuddle.Generator (generateFromGRef) import Codec.CBOR.Cuddle.Huddle ( CanQuantify (..), Huddle, @@ -57,6 +56,7 @@ import Codec.CBOR.Cuddle.Huddle ( (==>), ) import Codec.CBOR.Cuddle.Huddle qualified as H +import Codec.CBOR.Cuddle.Validator (validateFromGRef) import Codec.CBOR.Term qualified as C import Data.Word (Word64) import Test.QuickCheck.GenT (MonadGen (..), frequency) diff --git a/test/Test/Codec/CBOR/Cuddle/CDDL/Gen.hs b/test/Test/Codec/CBOR/Cuddle/Gen.hs similarity index 94% rename from test/Test/Codec/CBOR/Cuddle/CDDL/Gen.hs rename to test/Test/Codec/CBOR/Cuddle/Gen.hs index 464e8c90..fab5301b 100644 --- a/test/Test/Codec/CBOR/Cuddle/CDDL/Gen.hs +++ b/test/Test/Codec/CBOR/Cuddle/Gen.hs @@ -4,13 +4,13 @@ {-# OPTIONS_GHC -Wno-orphans #-} -- | Hedgehog generators for CDDL -module Test.Codec.CBOR.Cuddle.CDDL.Gen () where +module Test.Codec.CBOR.Cuddle.Gen () where import Codec.CBOR.Cuddle.CDDL import Codec.CBOR.Cuddle.CDDL.CtlOp import Codec.CBOR.Cuddle.Comments (Comment, toComment) -import Codec.CBOR.Cuddle.Parser (ParserStage, XTerm (..)) -import Codec.CBOR.Cuddle.Pretty (PrettyStage, XRule (..), XTerm (..), XXTopLevel (..)) +import Codec.CBOR.Cuddle.Parser (ParserPhase, XTerm (..)) +import Codec.CBOR.Cuddle.Pretty (PrettyPhase, XRule (..), XTerm (..), XXTopLevel (..)) import Data.ByteString (ByteString) import Data.ByteString qualified as BS import Data.List.NonEmpty qualified as NE @@ -19,17 +19,17 @@ import Data.Text qualified as T import Test.QuickCheck import Test.QuickCheck qualified as Gen -instance Arbitrary (CDDL PrettyStage) where +instance Arbitrary (CDDL PrettyPhase) where arbitrary = CDDL <$> arbitrary <*> arbitrary <*> arbitrary shrink = genericShrink -deriving newtype instance Arbitrary (XXTopLevel PrettyStage) +deriving newtype instance Arbitrary (XXTopLevel PrettyPhase) -deriving newtype instance Arbitrary (XTerm PrettyStage) +deriving newtype instance Arbitrary (XTerm PrettyPhase) -deriving newtype instance Arbitrary (XRule PrettyStage) +deriving newtype instance Arbitrary (XRule PrettyPhase) -instance Arbitrary (TopLevel PrettyStage) where +instance Arbitrary (TopLevel PrettyPhase) where arbitrary = Gen.oneof [ XXTopLevel <$> arbitrary @@ -37,7 +37,7 @@ instance Arbitrary (TopLevel PrettyStage) where ] shrink = genericShrink -deriving newtype instance Arbitrary (XTerm ParserStage) +deriving newtype instance Arbitrary (XTerm ParserPhase) instance Arbitrary T.Text where arbitrary = T.pack <$> arbitrary diff --git a/test/Test/Codec/CBOR/Cuddle/CDDL/GeneratorSpec.hs b/test/Test/Codec/CBOR/Cuddle/GeneratorSpec.hs similarity index 94% rename from test/Test/Codec/CBOR/Cuddle/CDDL/GeneratorSpec.hs rename to test/Test/Codec/CBOR/Cuddle/GeneratorSpec.hs index 4f0c7fe0..97baba27 100644 --- a/test/Test/Codec/CBOR/Cuddle/CDDL/GeneratorSpec.hs +++ b/test/Test/Codec/CBOR/Cuddle/GeneratorSpec.hs @@ -2,16 +2,15 @@ {-# LANGUAGE OverloadedLists #-} {-# LANGUAGE OverloadedStrings #-} -module Test.Codec.CBOR.Cuddle.CDDL.GeneratorSpec (spec) where +module Test.Codec.CBOR.Cuddle.GeneratorSpec (spec) where -import Codec.CBOR.Cuddle.CBOR.Gen (GenPhase, generateFromName) -import Codec.CBOR.Cuddle.CBOR.Validator (validateCBOR) import Codec.CBOR.Cuddle.CDDL (Name) import Codec.CBOR.Cuddle.CDDL.CTree (CTreeRoot (..)) -import Codec.CBOR.Cuddle.CDDL.Custom.Generator (GenConfig (..), runCBORGen) import Codec.CBOR.Cuddle.CDDL.Resolve (MonoReferenced, MonoSimplePhase, fullResolveCDDL) +import Codec.CBOR.Cuddle.Generator (GenConfig (..), GenPhase, generateFromName, runCBORGen) import Codec.CBOR.Cuddle.Huddle (Huddle, toCDDL) import Codec.CBOR.Cuddle.IndexMappable (IndexMappable (..), mapCDDLDropExt) +import Codec.CBOR.Cuddle.Validator (validateCBOR) import Codec.CBOR.Pretty (prettyHexEnc) import Codec.CBOR.Read (deserialiseFromBytes) import Codec.CBOR.Term (Term (..), decodeTerm, encodeTerm) @@ -20,7 +19,7 @@ import Data.ByteString.Lazy qualified as LBS import Data.Text qualified as T import Data.Text.Lazy qualified as TL import Test.AntiGen (ZapResult (..), prettyZapResult, runAntiGen, zapAntiGenResult) -import Test.Codec.CBOR.Cuddle.CDDL.Examples.Huddle ( +import Test.Codec.CBOR.Cuddle.Examples.Huddle ( bytesExample, customGenExample, opCertExample, @@ -33,7 +32,7 @@ import Test.Codec.CBOR.Cuddle.CDDL.Examples.Huddle ( tagRangeExample, taggedUintExample, ) -import Test.Codec.CBOR.Cuddle.CDDL.Validator (expectInvalid, genAndValidateRule) +import Test.Codec.CBOR.Cuddle.Validator (expectInvalid, genAndValidateRule) import Test.Hspec (HasCallStack, Spec, describe, runIO, shouldSatisfy, xdescribe) import Test.Hspec.Core.Spec (SpecM) import Test.Hspec.QuickCheck (prop) diff --git a/test/Test/Codec/CBOR/Cuddle/Huddle.hs b/test/Test/Codec/CBOR/Cuddle/Huddle.hs index 1712e905..00c5024c 100644 --- a/test/Test/Codec/CBOR/Cuddle/Huddle.hs +++ b/test/Test/Codec/CBOR/Cuddle/Huddle.hs @@ -16,57 +16,57 @@ import Data.List.NonEmpty (NonEmpty ((:|))) import Data.Text qualified as T import Data.Void (Void) import GHC.Generics (Generic) -import Test.Codec.CBOR.Cuddle.CDDL.Pretty qualified as Pretty +import Test.Codec.CBOR.Cuddle.Pretty qualified as Pretty import Test.Hspec import Test.Hspec.Megaparsec import Text.Megaparsec import Prelude hiding ((/)) -type data TestStage +type data TestPhase -newtype instance XCddl TestStage = TestXCddl [Comment] +newtype instance XCddl TestPhase = TestXCddl [Comment] deriving (Generic, Show, Eq) -instance IndexMappable XCddl ParserStage TestStage where +instance IndexMappable XCddl ParserPhase TestPhase where mapIndex (ParserXCddl x) = TestXCddl x -instance IndexMappable XCddl HuddleStage TestStage where +instance IndexMappable XCddl HuddlePhase TestPhase where mapIndex (HuddleXCddl x) = TestXCddl x -newtype instance XTerm TestStage = TestXTerm Comment +newtype instance XTerm TestPhase = TestXTerm Comment deriving (Generic, Show, Eq) -instance IndexMappable XTerm ParserStage TestStage where +instance IndexMappable XTerm ParserPhase TestPhase where mapIndex (ParserXTerm x) = TestXTerm x -instance IndexMappable XTerm HuddleStage TestStage where +instance IndexMappable XTerm HuddlePhase TestPhase where mapIndex (HuddleXTerm x) = TestXTerm x -newtype instance XRule TestStage = TestXRule Comment +newtype instance XRule TestPhase = TestXRule Comment deriving (Generic, Show, Eq) -instance IndexMappable XRule ParserStage TestStage where +instance IndexMappable XRule ParserPhase TestPhase where mapIndex (ParserXRule x) = TestXRule x -instance IndexMappable XRule HuddleStage TestStage where +instance IndexMappable XRule HuddlePhase TestPhase where mapIndex (HuddleXRule x _ _) = TestXRule x -newtype instance XXTopLevel TestStage = TestXXTopLevel Comment +newtype instance XXTopLevel TestPhase = TestXXTopLevel Comment deriving (Generic, Show, Eq) -instance IndexMappable XXTopLevel ParserStage TestStage where +instance IndexMappable XXTopLevel ParserPhase TestPhase where mapIndex (ParserXXTopLevel x) = TestXXTopLevel x -instance IndexMappable XXTopLevel HuddleStage TestStage where +instance IndexMappable XXTopLevel HuddlePhase TestPhase where mapIndex (HuddleXXTopLevel x) = TestXXTopLevel x -newtype instance XXType2 TestStage = TestXXType2 Void +newtype instance XXType2 TestPhase = TestXXType2 Void deriving (Generic, Show, Eq) -instance IndexMappable XXType2 ParserStage TestStage where +instance IndexMappable XXType2 ParserPhase TestPhase where mapIndex (ParserXXType2 x) = TestXXType2 x -instance IndexMappable XXType2 HuddleStage TestStage where +instance IndexMappable XXType2 HuddlePhase TestPhase where mapIndex (HuddleXXType2 x) = TestXXType2 x huddleSpec :: Spec @@ -228,10 +228,10 @@ shouldMatchParse :: shouldMatchParse x parseFun input = parse parseFun "" (T.pack input) `shouldParse` x shouldMatchParseCDDL :: - CDDL TestStage -> + CDDL TestPhase -> String -> Expectation shouldMatchParseCDDL x = shouldMatchParse x . fmap mapIndex $ pCDDL -toSortedCDDL :: Huddle -> CDDL TestStage +toSortedCDDL :: Huddle -> CDDL TestPhase toSortedCDDL = mapIndex . fromRules . sortCDDL . toCDDLNoRoot diff --git a/test/Test/Codec/CBOR/Cuddle/CDDL/Parser.hs b/test/Test/Codec/CBOR/Cuddle/Parser.hs similarity index 98% rename from test/Test/Codec/CBOR/Cuddle/CDDL/Parser.hs rename to test/Test/Codec/CBOR/Cuddle/Parser.hs index 04fa96e2..1b192396 100644 --- a/test/Test/Codec/CBOR/Cuddle/CDDL/Parser.hs +++ b/test/Test/Codec/CBOR/Cuddle/Parser.hs @@ -1,21 +1,21 @@ {-# LANGUAGE OverloadedStrings #-} -module Test.Codec.CBOR.Cuddle.CDDL.Parser where +module Test.Codec.CBOR.Cuddle.Parser where import Codec.CBOR.Cuddle.CDDL import Codec.CBOR.Cuddle.CDDL.CtlOp qualified as CtlOp import Codec.CBOR.Cuddle.IndexMappable (IndexMappable (..)) import Codec.CBOR.Cuddle.Parser import Codec.CBOR.Cuddle.Parser.Lexer (Parser) -import Codec.CBOR.Cuddle.Pretty (PrettyStage) +import Codec.CBOR.Cuddle.Pretty (PrettyPhase) import Data.List.NonEmpty (NonEmpty (..)) import Data.Text qualified as T import Data.TreeDiff (ToExpr (..), ansiWlBgEditExprCompact, exprDiff) import Prettyprinter (Pretty, defaultLayoutOptions, layoutPretty, pretty) import Prettyprinter.Render.String (renderString) import Prettyprinter.Render.Text (renderStrict) -import Test.Codec.CBOR.Cuddle.CDDL.Gen qualified as Gen () -import Test.Codec.CBOR.Cuddle.CDDL.TreeDiff () +import Test.Codec.CBOR.Cuddle.TreeDiff () +import Test.Codec.CBOR.Cuddle.Gen qualified as Gen () import Test.Hspec import Test.Hspec.Megaparsec import Test.QuickCheck @@ -62,15 +62,15 @@ roundtripSpec = describe "Roundtripping should be id" $ do $ printed `shouldBe` printText parsed tripIndexed :: forall a. - ( IndexMappable a ParserStage PrettyStage - , Eq (a PrettyStage) - , ToExpr (a PrettyStage) - , Show (a PrettyStage) - , Pretty (a PrettyStage) - , Arbitrary (a PrettyStage) + ( IndexMappable a ParserPhase PrettyPhase + , Eq (a PrettyPhase) + , ToExpr (a PrettyPhase) + , Show (a PrettyPhase) + , Pretty (a PrettyPhase) + , Arbitrary (a PrettyPhase) ) => - Parser (a ParserStage) -> Property - tripIndexed = trip . fmap (mapIndex @a @ParserStage @PrettyStage) + Parser (a ParserPhase) -> Property + tripIndexed = trip . fmap (mapIndex @a @ParserPhase @PrettyPhase) printText :: Pretty a => a -> T.Text printText = renderStrict . layoutPretty defaultLayoutOptions . pretty diff --git a/test/Test/Codec/CBOR/Cuddle/CDDL/Pretty.hs b/test/Test/Codec/CBOR/Cuddle/Pretty.hs similarity index 92% rename from test/Test/Codec/CBOR/Cuddle/CDDL/Pretty.hs rename to test/Test/Codec/CBOR/Cuddle/Pretty.hs index 7f9c0742..d7f8fa34 100644 --- a/test/Test/Codec/CBOR/Cuddle/CDDL/Pretty.hs +++ b/test/Test/Codec/CBOR/Cuddle/Pretty.hs @@ -2,7 +2,7 @@ {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE NoImplicitPrelude #-} -module Test.Codec.CBOR.Cuddle.CDDL.Pretty ( +module Test.Codec.CBOR.Cuddle.Pretty ( spec, roundtripSpec, ) where @@ -27,7 +27,7 @@ import Codec.CBOR.Cuddle.Huddle (HuddleItem (..), a, bstr, (<+), (=:=), (=:~)) import Codec.CBOR.Cuddle.Huddle qualified as H import Codec.CBOR.Cuddle.IndexMappable (IndexMappable (..)) import Codec.CBOR.Cuddle.Parser (pCDDL) -import Codec.CBOR.Cuddle.Pretty (PrettyStage, XRule (..), renderCDDL) +import Codec.CBOR.Cuddle.Pretty (PrettyPhase, XRule (..), renderCDDL) import Data.Default.Class (Default (..)) import Data.List.NonEmpty (NonEmpty (..)) import Data.Text qualified as T @@ -36,8 +36,8 @@ import Data.TreeDiff (ToExpr (..), prettyExpr) import Paths_cuddle (getDataFileName) import Prettyprinter (Pretty (..), defaultLayoutOptions, layoutPretty) import Prettyprinter.Render.String (renderString) -import Test.Codec.CBOR.Cuddle.CDDL.Gen () -import Test.Codec.CBOR.Cuddle.CDDL.TreeDiff () +import Test.Codec.CBOR.Cuddle.TreeDiff () +import Test.Codec.CBOR.Cuddle.Gen () import Test.HUnit (assertEqual) import Test.Hspec (Expectation, Spec, describe, it, runIO, shouldBe, xit) import Test.Hspec.QuickCheck (xprop) @@ -51,13 +51,13 @@ prettyPrintsTo x s = assertEqual (show . prettyExpr $ toExpr x) s rendered where rendered = renderString (layoutPretty defaultLayoutOptions (pretty x)) -t2Name :: Type2 PrettyStage +t2Name :: Type2 PrettyPhase t2Name = T2Name (Name "a") mempty -t1Name :: Type1 PrettyStage +t1Name :: Type1 PrettyPhase t1Name = Type1 t2Name Nothing mempty -mkType0 :: Type2 PrettyStage -> Type0 PrettyStage +mkType0 :: Type2 PrettyPhase -> Type0 PrettyPhase mkType0 t2 = Type0 $ Type1 t2 Nothing mempty :| [] spec :: Spec @@ -67,14 +67,14 @@ spec = describe "Pretty printer" $ do qcSpec :: Spec qcSpec = describe "QuickCheck" $ do - xprop "CDDL prettyprinter leaves no trailing spaces" $ \(cddl :: CDDL PrettyStage) -> do + xprop "CDDL prettyprinter leaves no trailing spaces" $ \(cddl :: CDDL PrettyPhase) -> do let prettyStr = T.pack . renderString . layoutPretty defaultLayoutOptions $ pretty cddl stripLines = T.unlines . fmap T.stripEnd . T.lines counterexample (show . prettyExpr $ toExpr cddl) $ prettyStr `shouldBe` stripLines prettyStr -drep :: Rule PrettyStage +drep :: Rule PrettyPhase drep = Rule "drep" @@ -139,7 +139,7 @@ unitSpec = describe "HUnit" $ do describe "Name" $ do it "names" $ Name "a" `prettyPrintsTo` "a" describe "Type0" $ do - it "name" $ Type0 @PrettyStage (t1Name :| []) `prettyPrintsTo` "a" + it "name" $ Type0 @PrettyPhase (t1Name :| []) `prettyPrintsTo` "a" describe "Type1" $ do it "name" $ t1Name `prettyPrintsTo` "a" describe "Type2" $ do @@ -192,7 +192,7 @@ unitSpec = describe "HUnit" $ do `prettyPrintsTo` "[ 1 ;first\n ;multiline comment\n, 2 ;second\n ;multiline comment\n]" describe "Rule" $ do it "simple assignment" $ - Rule @PrettyStage + Rule @PrettyPhase (Name "a") Nothing AssignEq @@ -200,7 +200,7 @@ unitSpec = describe "HUnit" $ do def `prettyPrintsTo` "a = b" it "simple assignment with comment" $ - Rule @PrettyStage + Rule @PrettyPhase (Name "a") Nothing AssignEq @@ -213,7 +213,7 @@ unitSpec = describe "HUnit" $ do describe "Huddle" $ do let huddlePrettyPrintsTo rs str = - mapIndex @_ @_ @PrettyStage (H.toCDDLNoRoot $ H.collectFrom rs) `prettyPrintsTo` str + mapIndex @_ @_ @PrettyPhase (H.toCDDLNoRoot $ H.collectFrom rs) `prettyPrintsTo` str describe "Rule" $ do -- TODO get rid of trailing newline it "simple assignment" $ @@ -275,11 +275,11 @@ prettyRoundtrip testName cddlPath = do Right x -> pure x it testName $ do let - prettyStage1 = mapIndex @_ @_ @PrettyStage original - rendered = renderCDDL defaultLayoutOptions prettyStage1 + prettyPhase1 = mapIndex @_ @_ @PrettyPhase original + rendered = renderCDDL defaultLayoutOptions prettyPhase1 case parse pCDDL "" rendered of Left err -> fail $ "Failed to re-parse pretty-printed CDDL:\n" <> errorBundlePretty err Right reparsed -> - let prettyStage2 = mapIndex @_ @_ @PrettyStage reparsed - in prettyStage2 `shouldBe` prettyStage1 + let prettyPhase2 = mapIndex @_ @_ @PrettyPhase reparsed + in prettyPhase2 `shouldBe` prettyPhase1 diff --git a/test/Test/Codec/CBOR/Cuddle/CDDL/Pretty/Golden.hs b/test/Test/Codec/CBOR/Cuddle/Pretty/Golden.hs similarity index 94% rename from test/Test/Codec/CBOR/Cuddle/CDDL/Pretty/Golden.hs rename to test/Test/Codec/CBOR/Cuddle/Pretty/Golden.hs index 6ec040b5..00bb39d4 100644 --- a/test/Test/Codec/CBOR/Cuddle/CDDL/Pretty/Golden.hs +++ b/test/Test/Codec/CBOR/Cuddle/Pretty/Golden.hs @@ -1,14 +1,14 @@ {-# LANGUAGE OverloadedLists #-} {-# LANGUAGE OverloadedStrings #-} -module Test.Codec.CBOR.Cuddle.CDDL.Pretty.Golden (spec) where +module Test.Codec.CBOR.Cuddle.Pretty.Golden (spec) where import Codec.CBOR.Cuddle.Comments ((//-)) import Codec.CBOR.Cuddle.Huddle (HuddleItem (..), a, (=:=)) import Codec.CBOR.Cuddle.Huddle qualified as H import Codec.CBOR.Cuddle.IndexMappable (mapIndex) import Codec.CBOR.Cuddle.Parser (pCDDL) -import Codec.CBOR.Cuddle.Pretty (PrettyStage, renderCDDL) +import Codec.CBOR.Cuddle.Pretty (PrettyPhase, renderCDDL) import Control.Monad ((<=<)) import Data.Text (Text) import Data.Text.IO qualified as T @@ -43,14 +43,14 @@ prettyPrintGolden testName cddlPath = do it testName $ mkGolden testName $ renderCDDL defaultLayoutOptions $ - mapIndex @_ @_ @PrettyStage cddl + mapIndex @_ @_ @PrettyPhase cddl huddleGolden :: String -> [HuddleItem] -> Spec huddleGolden testName items = it testName $ mkGolden testName $ renderCDDL defaultLayoutOptions $ - mapIndex @_ @_ @PrettyStage $ + mapIndex @_ @_ @PrettyPhase $ H.toCDDLNoRoot $ H.collectFrom items diff --git a/test/Test/Codec/CBOR/Cuddle/CDDL/TreeDiff.hs b/test/Test/Codec/CBOR/Cuddle/TreeDiff.hs similarity index 66% rename from test/Test/Codec/CBOR/Cuddle/CDDL/TreeDiff.hs rename to test/Test/Codec/CBOR/Cuddle/TreeDiff.hs index 689beaf9..effc5bb3 100644 --- a/test/Test/Codec/CBOR/Cuddle/CDDL/TreeDiff.hs +++ b/test/Test/Codec/CBOR/Cuddle/TreeDiff.hs @@ -1,37 +1,12 @@ {-# LANGUAGE UndecidableInstances #-} {-# OPTIONS_GHC -Wno-orphans #-} -module Test.Codec.CBOR.Cuddle.CDDL.TreeDiff () where - -import Codec.CBOR.Cuddle.CDDL ( - Assign, - CDDL, - ForAllExtensions, - GenericArg, - GenericParameter, - GenericParameters, - Group, - GroupEntry, - GroupEntryVariant, - GrpChoice, - MemberKey, - Name, - OccurrenceIndicator, - RangeBound, - Rule, - TopLevel, - TyOp, - Type0, - Type1, - Type2, - TypeOrGroup, - Value, - ValueVariant, - XCddl, - ) +module Test.Codec.CBOR.Cuddle.TreeDiff () where + +import Codec.CBOR.Cuddle.CDDL import Codec.CBOR.Cuddle.CDDL.CtlOp (CtlOp) import Codec.CBOR.Cuddle.Comments (Comment) -import Codec.CBOR.Cuddle.Pretty (PrettyStage, XRule, XTerm, XXTopLevel, XXType2) +import Codec.CBOR.Cuddle.Pretty (PrettyPhase) import Data.TreeDiff (ToExpr) instance ToExpr Name @@ -82,12 +57,12 @@ instance ForAllExtensions p ToExpr => ToExpr (TopLevel p) instance ForAllExtensions p ToExpr => ToExpr (CDDL p) -instance ToExpr (XCddl PrettyStage) +instance ToExpr (XCddl PrettyPhase) -instance ToExpr (XTerm PrettyStage) +instance ToExpr (XTerm PrettyPhase) -instance ToExpr (XRule PrettyStage) +instance ToExpr (XRule PrettyPhase) -instance ToExpr (XXTopLevel PrettyStage) +instance ToExpr (XXTopLevel PrettyPhase) -instance ToExpr (XXType2 PrettyStage) +instance ToExpr (XXType2 PrettyPhase) diff --git a/test/Test/Codec/CBOR/Cuddle/CDDL/Validator.hs b/test/Test/Codec/CBOR/Cuddle/Validator.hs similarity index 96% rename from test/Test/Codec/CBOR/Cuddle/CDDL/Validator.hs rename to test/Test/Codec/CBOR/Cuddle/Validator.hs index f44799cf..bf9339d9 100644 --- a/test/Test/Codec/CBOR/Cuddle/CDDL/Validator.hs +++ b/test/Test/Codec/CBOR/Cuddle/Validator.hs @@ -2,7 +2,7 @@ {-# LANGUAGE OverloadedLists #-} {-# LANGUAGE OverloadedStrings #-} -module Test.Codec.CBOR.Cuddle.CDDL.Validator ( +module Test.Codec.CBOR.Cuddle.Validator ( spec, expectValid, expectInvalid, @@ -10,25 +10,13 @@ module Test.Codec.CBOR.Cuddle.CDDL.Validator ( genAndValidateRule, ) where -import Codec.CBOR.Cuddle.CBOR.Gen (generateFromName) -import Codec.CBOR.Cuddle.CBOR.Validator ( - validateCBOR, - ) -import Codec.CBOR.Cuddle.CBOR.Validator.Trace ( - Evidenced (..), - SValidity (..), - ValidationTrace, - defaultTraceOptions, - prettyValidationTrace, - ) import Codec.CBOR.Cuddle.CDDL (Name (..)) import Codec.CBOR.Cuddle.CDDL.CTree (CTreeRoot (..)) import Codec.CBOR.Cuddle.CDDL.CTree qualified as CTree -import Codec.CBOR.Cuddle.CDDL.Custom.Core (RuleTerm (..)) -import Codec.CBOR.Cuddle.CDDL.Custom.Generator (GenConfig (..), runCBORGen) -import Codec.CBOR.Cuddle.CDDL.Custom.Validator (TermValidator) import Codec.CBOR.Cuddle.CDDL.Postlude (appendPostlude) import Codec.CBOR.Cuddle.CDDL.Resolve (MonoReferenced, fullResolveCDDL) +import Codec.CBOR.Cuddle.Core (RuleTerm (..)) +import Codec.CBOR.Cuddle.Generator (GenConfig (..), generateFromName, runCBORGen) import Codec.CBOR.Cuddle.Huddle ( Huddle, HuddleItem (..), @@ -44,6 +32,15 @@ import Codec.CBOR.Cuddle.Huddle ( import Codec.CBOR.Cuddle.Huddle qualified as H import Codec.CBOR.Cuddle.IndexMappable (mapCDDLDropExt, mapIndex) import Codec.CBOR.Cuddle.Parser (pCDDL) +import Codec.CBOR.Cuddle.Validator (validateCBOR) +import Codec.CBOR.Cuddle.Validator.Core (TermValidator) +import Codec.CBOR.Cuddle.Validator.Trace ( + Evidenced (..), + SValidity (..), + ValidationTrace, + defaultTraceOptions, + prettyValidationTrace, + ) import Codec.CBOR.Pretty (prettyHexEnc) import Codec.CBOR.Term (Term (..), encodeTerm) import Codec.CBOR.Write (toStrictByteString) @@ -62,7 +59,7 @@ import Paths_cuddle (getDataFileName) import Prettyprinter (defaultLayoutOptions, layoutPretty) import Prettyprinter.Render.Terminal (renderStrict) import Test.AntiGen (runAntiGen) -import Test.Codec.CBOR.Cuddle.CDDL.Examples.Huddle ( +import Test.Codec.CBOR.Cuddle.Examples.Huddle ( huddleArray, huddleMap, huddleRangeArray, diff --git a/test/Test/Codec/CBOR/Cuddle/CDDL/Validator/Golden.hs b/test/Test/Codec/CBOR/Cuddle/Validator/Golden.hs similarity index 94% rename from test/Test/Codec/CBOR/Cuddle/CDDL/Validator/Golden.hs rename to test/Test/Codec/CBOR/Cuddle/Validator/Golden.hs index 74924436..f9ce8d95 100644 --- a/test/Test/Codec/CBOR/Cuddle/CDDL/Validator/Golden.hs +++ b/test/Test/Codec/CBOR/Cuddle/Validator/Golden.hs @@ -1,18 +1,14 @@ {-# LANGUAGE GADTs #-} {-# LANGUAGE OverloadedStrings #-} -module Test.Codec.CBOR.Cuddle.CDDL.Validator.Golden (spec) where +module Test.Codec.CBOR.Cuddle.Validator.Golden (spec) where -import Codec.CBOR.Cuddle.CBOR.Validator (validateCBOR) -import Codec.CBOR.Cuddle.CBOR.Validator.Trace ( - defaultTraceOptions, - foldEvidenced, - prettyValidationTrace, - ) import Codec.CBOR.Cuddle.CDDL (Name) import Codec.CBOR.Cuddle.CDDL.Resolve (fullResolveCDDL) import Codec.CBOR.Cuddle.Huddle (Huddle, toCDDL) import Codec.CBOR.Cuddle.IndexMappable (mapCDDLDropExt, mapIndex) +import Codec.CBOR.Cuddle.Validator (validateCBOR) +import Codec.CBOR.Cuddle.Validator.Trace (defaultTraceOptions, foldEvidenced, prettyValidationTrace) import Codec.CBOR.Term (Term (..), encodeTerm) import Codec.CBOR.Write (toStrictByteString) import Codec.CBOR.Write qualified as CBOR @@ -24,7 +20,7 @@ import Paths_cuddle (getDataFileName) import Prettyprinter (defaultLayoutOptions, layoutPretty) import Prettyprinter.Render.Terminal qualified as Ansi import System.FilePath (()) -import Test.Codec.CBOR.Cuddle.CDDL.Examples.Huddle ( +import Test.Codec.CBOR.Cuddle.Examples.Huddle ( cborControlExample, choicesExample, deeplyNestedRefExample,