-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathJavaPretty.hs
74 lines (65 loc) · 2.14 KB
/
JavaPretty.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
{- |
Module : SAWScript.JavaPretty
Description : Pretty printer for Java Class datatype.
License : BSD3
Maintainer : atomb
Stability : provisional
-}
module SAWScript.JavaPretty where
import Data.Maybe (fromMaybe)
import Text.PrettyPrint.ANSI.Leijen hiding ((<$>))
import Language.JVM.Common
import Verifier.Java.Codebase
prettyClass :: Class -> Doc
prettyClass cls = vcat $
[ empty
, text "Class name:" <+> text (unClassName (className cls)) <+>
parens (commas attrs)
, text "Superclass:" <+> text (fromMaybe "none" (fmap unClassName (superClass cls)))
, empty
] ++
(if null (classInterfaces cls)
then []
else [ text "Interfaces:"
, indent 2 (vcat (map (text . unClassName) (classInterfaces cls)))
, empty
]) ++
[ text "Fields:"
, indent 2 (vcat (map prettyField (classFields cls)))
, empty
, text "Methods:"
, indent 2 (vcat (map prettyMethod (classMethods cls)))
, empty
]
where attrs = concat
[ if classIsPublic cls then [text "public"] else []
, if classIsFinal cls then [text "final"] else []
, if classHasSuperAttribute cls then [text "super"] else []
, if classIsInterface cls then [text "interface"] else []
, if classIsAbstract cls then [text "abstract"] else []
]
prettyField :: Field -> Doc
prettyField f = hsep $
[ text (show (fieldVisibility f)) ] ++
attrs ++
[ text (show (ppType (fieldType f))) -- TODO: Ick. Different PPs.
, text (fieldName f)
]
where attrs = concat
[ if fieldIsStatic f then [text "static"] else []
, if fieldIsFinal f then [text "final"] else []
, if fieldIsVolatile f then [text "volatile"] else []
, if fieldIsTransient f then [text "transient"] else []
]
prettyMethod :: Method -> Doc
prettyMethod m =
hsep $
(if methodIsStatic m then [text "static"] else []) ++
[ maybe (text "void") prettyType ret
, text name
, (parens . commas . map prettyType) params
]
where (MethodKey name params ret) = methodKey m
prettyType = text . show . ppType -- TODO: Ick.
commas :: [Doc] -> Doc
commas = sep . punctuate comma