PrintAs.hs revision 84e7cfca5b97aef300acdaa8cf63a3572f9151c0
e9458b1a7a19a63aa4c179f9ab20f4d50681c168Jens ElknerModule : $Header$
81d182b21020b815887e9057959228546cf61b6bChristian MaederDescription : print the abstract syntax so that it can be re-parsed
076b559b2ea7b2f1d303df992ae71cd6c6fe563cChristian MaederCopyright : (c) Christian Maeder and Uni Bremen 2003
98890889ffb2e8f6f722b00e265a211f13b5a861Corneliu-Claudiu ProdescuLicense : similar to LGPL, see HetCATS/LICENSE.txt or LIZENZ.txt
3f69b6948966979163bdfe8331c38833d5d90ecdChristian MaederMaintainer : Christian.Maeder@dfki.de
076b559b2ea7b2f1d303df992ae71cd6c6fe563cChristian MaederStability : experimental
076b559b2ea7b2f1d303df992ae71cd6c6fe563cChristian MaederPortability : portable
5b1f1d57c75562a7af79e8256f4afa07febe921bChristian Maederprinting data types of the abstract syntax
ac510075311023bf24175f7a76b89ec2bbda0626Christian Maederimport Data.List (groupBy, mapAccumL)
5b1f1d57c75562a7af79e8256f4afa07febe921bChristian Maeder-- | short cut for: if b then empty else d
5b1f1d57c75562a7af79e8256f4afa07febe921bChristian MaedernoPrint :: Bool -> Doc -> Doc
5b1f1d57c75562a7af79e8256f4afa07febe921bChristian MaedernoPrint b d = if b then empty else d
99f16a0f9ca757410960ff51a79b034503384fe2Christian MaedernoNullPrint :: [a] -> Doc -> Doc
5b1f1d57c75562a7af79e8256f4afa07febe921bChristian MaedernoNullPrint = noPrint . null
6e5180855658f12f9059d9041f447bf0935de344Christian MaedersemiDs :: Pretty a => [a] -> Doc
5b1f1d57c75562a7af79e8256f4afa07febe921bChristian MaedersemiDs = fsep . punctuate semi . map pretty
76647324ed70f33b95a881b536d883daccf9568dChristian MaedersemiAnnoted :: Pretty a => [Annoted a] -> Doc
76647324ed70f33b95a881b536d883daccf9568dChristian MaedersemiAnnoted = vcat . map (printSemiAnno pretty True)
4e013227ed41ccd2e3d09dd44bedd651e1901f38Christian Maederinstance Pretty Variance where
568a1ce407fd05a2007c5db3c5c57098bf13997fChristian Maeder pretty = sidDoc . mkSimpleId . show
76fa667489c5e0868ac68de9f0253ac10f73d0b5Christian Maederinstance Pretty a => Pretty (AnyKind a) where
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder pretty knd = case knd of
76fa667489c5e0868ac68de9f0253ac10f73d0b5Christian Maeder ClassKind ci -> pretty ci
5b1f1d57c75562a7af79e8256f4afa07febe921bChristian Maeder FunKind v k1 k2 _ -> fsep [pretty v <>
76647324ed70f33b95a881b536d883daccf9568dChristian Maeder FunKind _ _ _ _ -> parens
fd5d3885a092ac0727fa2436cdfc3b248318ebd8Christian Maeder _ -> id) (pretty k1)
529f678f015ae5276f87da63114cdce750b366aeChristian MaedervarOfTypeArg :: TypeArg -> Id
529f678f015ae5276f87da63114cdce750b366aeChristian MaedervarOfTypeArg (TypeArg i _ _ _ _ _ _) = i
529f678f015ae5276f87da63114cdce750b366aeChristian Maederinstance Pretty TypePattern where
529f678f015ae5276f87da63114cdce750b366aeChristian Maeder pretty tp = case tp of
529f678f015ae5276f87da63114cdce750b366aeChristian Maeder TypePattern name@(Id ts cs _) args _ ->
ac510075311023bf24175f7a76b89ec2bbda0626Christian Maeder let ds = map (pretty . varOfTypeArg) args in
8a1f427564a5ae2db32332512237ef645289c34dChristian Maeder if placeCount name == length args then
529f678f015ae5276f87da63114cdce750b366aeChristian Maeder let (ras, dts) = mapAccumL ( \ l t -> if isPlace t then
529f678f015ae5276f87da63114cdce750b366aeChristian Maeder x : r -> (r, x)
568a1ce407fd05a2007c5db3c5c57098bf13997fChristian Maeder _ -> error "Pretty TypePattern"
ac510075311023bf24175f7a76b89ec2bbda0626Christian Maeder else (l, printTypeToken t)) ds ts
fd5d3885a092ac0727fa2436cdfc3b248318ebd8Christian Maeder in fsep $ dts ++ (if null cs then [] else
fd5d3885a092ac0727fa2436cdfc3b248318ebd8Christian Maeder [brackets $ sepByCommas $ map printTypeId cs])
ac9e33c3c35b2663e5cb76483228910f142d9576Christian Maeder else printTypeId name <+> fsep ds
ac9e33c3c35b2663e5cb76483228910f142d9576Christian Maeder TypePatternToken t -> printTypeToken t
ac9e33c3c35b2663e5cb76483228910f142d9576Christian Maeder MixfixTypePattern ts -> fsep $ map pretty ts
5b1f1d57c75562a7af79e8256f4afa07febe921bChristian Maeder BracketTypePattern k l _ -> bracket k $ ppWithCommas l
5b1f1d57c75562a7af79e8256f4afa07febe921bChristian Maeder TypePatternArg t _ -> parens $ pretty t
ac9e33c3c35b2663e5cb76483228910f142d9576Christian Maeder-- | put proper brackets around a document
ac9e33c3c35b2663e5cb76483228910f142d9576Christian Maederbracket :: BracketKind -> Doc -> Doc
ac9e33c3c35b2663e5cb76483228910f142d9576Christian Maederbracket b = case b of
ac9e33c3c35b2663e5cb76483228910f142d9576Christian Maeder Parens -> parens
ac9e33c3c35b2663e5cb76483228910f142d9576Christian Maeder Squares -> brackets
ac9e33c3c35b2663e5cb76483228910f142d9576Christian Maeder Braces -> specBraces
ac9e33c3c35b2663e5cb76483228910f142d9576Christian Maeder NoBrackets -> id
4be371b81d055e03a5946e4ec333613f313d689bChristian Maeder-- | print a 'Kind' plus a preceding colon (or nothing)
ac510075311023bf24175f7a76b89ec2bbda0626Christian MaederprintKind :: Kind -> Doc
7de39d39bc1700cc8a9bb9df90b920aad9e18d4aChristian MaederprintKind k = noPrint (k == universe) $ printVarKind InVar (VarKind k)
ac510075311023bf24175f7a76b89ec2bbda0626Christian Maeder-- | print the kind of a variable with its variance and a preceding colon
529f678f015ae5276f87da63114cdce750b366aeChristian MaederprintVarKind :: Variance -> VarKind -> Doc
ac510075311023bf24175f7a76b89ec2bbda0626Christian MaederprintVarKind e vk = case vk of
568a1ce407fd05a2007c5db3c5c57098bf13997fChristian Maeder space <> less <+> pretty t
ac510075311023bf24175f7a76b89ec2bbda0626Christian Maeder VarKind k -> space <> colon <+>
fd5d3885a092ac0727fa2436cdfc3b248318ebd8Christian Maeder pretty e <> pretty k
81700fac589336e88451a2a8474a893a28506438Christian Maeder MissingKind -> empty
ac510075311023bf24175f7a76b89ec2bbda0626Christian Maederdata TypePrec = Outfix | Prefix | ProdInfix | FunInfix | Absfix
7c57322afb6342e5cc8b1fdc96050b707407fc61Christian Maeder deriving (Eq, Ord)
5b1f1d57c75562a7af79e8256f4afa07febe921bChristian MaederparenPrec :: TypePrec -> (TypePrec, Doc) -> Doc
5b1f1d57c75562a7af79e8256f4afa07febe921bChristian MaederparenPrec p1 (p2, d) = if p2 < p1 then d else parens d