PrintAs.hs revision 120efeede54a5f7650cda8e91363bd6832eac9a9
fcd50ed0f526645ca50bad2170e3b98b911b7678Ewaryst SchulzModule : $Header$
fcd50ed0f526645ca50bad2170e3b98b911b7678Ewaryst SchulzCopyright : (c) Christian Maeder and Uni Bremen 2003
fcd50ed0f526645ca50bad2170e3b98b911b7678Ewaryst SchulzLicense : similar to LGPL, see HetCATS/LICENSE.txt or LIZENZ.txt
fcd50ed0f526645ca50bad2170e3b98b911b7678Ewaryst SchulzMaintainer : maeder@tzi.de
fcd50ed0f526645ca50bad2170e3b98b911b7678Ewaryst SchulzStability : experimental
fcd50ed0f526645ca50bad2170e3b98b911b7678Ewaryst SchulzPortability : portable
fcd50ed0f526645ca50bad2170e3b98b911b7678Ewaryst Schulzprinting data types of the abstract syntax
fcd50ed0f526645ca50bad2170e3b98b911b7678Ewaryst Schulzimport Data.List (groupBy)
fcd50ed0f526645ca50bad2170e3b98b911b7678Ewaryst Schulz-- | short cut for: if b then empty else d
fcd50ed0f526645ca50bad2170e3b98b911b7678Ewaryst SchulznoPrint :: Bool -> Doc -> Doc
fcd50ed0f526645ca50bad2170e3b98b911b7678Ewaryst SchulznoPrint b d = if b then empty else d
fcd50ed0f526645ca50bad2170e3b98b911b7678Ewaryst SchulznoNullPrint :: [a] -> Doc -> Doc
ad306df140215d8fb88d14bbb7d685011e0f770bRobert SavunoNullPrint = noPrint . null
fcd50ed0f526645ca50bad2170e3b98b911b7678Ewaryst SchulzsemiDs :: Pretty a => [a] -> Doc
fcd50ed0f526645ca50bad2170e3b98b911b7678Ewaryst SchulzsemiDs = fsep . punctuate semi . map pretty
fcd50ed0f526645ca50bad2170e3b98b911b7678Ewaryst SchulzsemiAnnoted :: Pretty a => [Annoted a] -> Doc
ad306df140215d8fb88d14bbb7d685011e0f770bRobert SavusemiAnnoted = vcat . map (printSemiAnno pretty True)
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savuinstance Pretty Variance where
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu pretty = sidDoc . mkSimpleId . show
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savuinstance Pretty a => Pretty (AnyKind a) where
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu pretty knd = case knd of
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu ClassKind ci -> pretty ci
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu FunKind v k1 k2 _ -> fsep [pretty v <>
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu FunKind _ _ _ _ -> parens
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu _ -> id) (pretty k1)
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu , pretty k2]
ad306df140215d8fb88d14bbb7d685011e0f770bRobert SavuvarOfTypeArg :: TypeArg -> Id
ad306df140215d8fb88d14bbb7d685011e0f770bRobert SavuvarOfTypeArg (TypeArg i _ _ _ _ _ _) = i
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savuinstance Pretty TypePattern where
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu pretty tp = case tp of
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu TypePattern name args _ -> pretty name
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu <+> fsep (map (pretty . varOfTypeArg) args)
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu TypePatternToken t -> pretty t
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu MixfixTypePattern ts -> fsep $ map pretty ts
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu BracketTypePattern k l _ -> bracket k $ ppWithCommas l
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu TypePatternArg t _ -> parens $ pretty t
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu-- | put proper brackets around a document
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savubracket :: BracketKind -> Doc -> Doc
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savubracket b = case b of
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu Parens -> parens
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu Squares -> brackets
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu Braces -> specBraces
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu NoBrackets -> id
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu-- | print a 'Kind' plus a preceding colon (or nothing)
ad306df140215d8fb88d14bbb7d685011e0f770bRobert SavuprintKind :: Kind -> Doc
ad306df140215d8fb88d14bbb7d685011e0f770bRobert SavuprintKind k = noPrint (k == universe) $ printVarKind InVar (VarKind k)
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu-- | print the kind of a variable with its variance and a preceding colon
ad306df140215d8fb88d14bbb7d685011e0f770bRobert SavuprintVarKind :: Variance -> VarKind -> Doc
ad306df140215d8fb88d14bbb7d685011e0f770bRobert SavuprintVarKind e vk = case vk of
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu Downset t ->
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu space <> less <+> pretty t
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu VarKind k -> space <> colon <+>
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu pretty e <> pretty k
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu MissingKind -> empty
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savudata TypePrec = Outfix | Prefix | ProdInfix | FunInfix deriving (Eq, Ord)
ad306df140215d8fb88d14bbb7d685011e0f770bRobert SavuparenPrec :: TypePrec -> (TypePrec, Doc) -> Doc
ad306df140215d8fb88d14bbb7d685011e0f770bRobert SavuparenPrec p1 (p2, d) = if p2 < p1 then d else parens d
ad306df140215d8fb88d14bbb7d685011e0f770bRobert SavuprintTypeToken :: Token -> Doc
ad306df140215d8fb88d14bbb7d685011e0f770bRobert SavuprintTypeToken t = let
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu l = ("*", cross) : map ( \ (a, d) -> (show a, d) )
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu [ (FunArr, funArrow)
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu , (PFunArr, pfun)
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu , (ContFunArr, cfun)
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu , (PContFunArr, pcfun) ]
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu in case lookup (tokStr t) l of
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu _ -> pretty t
ad306df140215d8fb88d14bbb7d685011e0f770bRobert SavutoMixType :: Type -> (TypePrec, Doc)
ad306df140215d8fb88d14bbb7d685011e0f770bRobert SavutoMixType typ = case typ of
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu ExpandedType t1 _ -> toMixType t1
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu BracketType k l _ -> (Outfix, bracket k $ sepByCommas $ map
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu (snd . toMixType) l)
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu KindedType t kind _ -> (Prefix,
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu fsep [parenPrec Prefix $ toMixType t, colon, pretty kind])
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu MixfixType ts -> (Prefix, fsep $ map (snd . toMixType) ts)
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu _ -> let (topTy, tyArgs) = getTypeAppl typ in
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu case topTy of
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu TypeName name@(Id ts cs _) _k _i -> let topDoc = pretty name in
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu case tyArgs of
ad306df140215d8fb88d14bbb7d685011e0f770bRobert Savu [] -> (Outfix, pretty name)