Debian Codegraph @

importers/ding2tei/src/Language/Ding/Pretty.hs

raw symbols SCIP
1 {-
2 - Language/Ding/Pretty.hs - pretty printing of the Ding AST
3 - Copyright 2020,2022 Einhard Leichtfuß
4 -
5 - This file is part of ding2tei-haskell.
6 -
7 - ding2tei-haskell is free software: you can redistribute it and/or modify
8 - it under the terms of the GNU Affero General Public License as published
9 - by the Free Software Foundation, either version 3 of the License, or
10 - (at your option) any later version.
11 -
12 - ding2tei-haskell is distributed in the hope that it will be useful,
13 - but WITHOUT ANY WARRANTY; without even the implied warranty of
14 - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 - GNU Affero General Public License for more details.
16 -
17 - You should have received a copy of the GNU Affero General Public License
18 - along with ding2tei-haskell. If not, see <https://www.gnu.org/licenses/>.
19 -}
20
21 {-# LANGUAGE FlexibleInstances #-}
22
23 {-|
24 - Pretty printing of the Ding AST.
25 -
26 - Note that the pretty printing does not generally reproduce the originally
27 - parsed Ding syntax; the result may even not be accepted by the parser.
28 -
29 - The former is because
30 - a) during parsing, some information is dropped,
31 - b) enrichment may have changed the order of annotations.
32 - Also, potentially identified examples are not displayed.
33 -
34 - The latter is only the case when grammar inferral (enrichment) was
35 - performed. In this case, grammar information such as "{noun}" may be
36 - inferred, which is not originally present in the Ding dictionary, albeit
37 - useful information during debugging.
38 -}
39 module Language.Ding.Pretty (pretty) where
40
41
42 import Prelude hiding ((<>))
43
44 import Data.List.NonEmpty (NonEmpty)
45 import qualified Data.List.NonEmpty as NEList
46 import Text.PrettyPrint
47
48 import Data.NatLang.Dictionary (Dictionary(..), Body(..))
49 import Data.NatLang.Grammar
50 import Data.NatLang.InflectedForms
51 import Data.NatLang.Usage
52 import Language.Ding.Show.Grammar (showGLC, showCase, showNumber, showPOS)
53 import Language.Ding.Syntax hiding (Dictionary)
54
55
56 -------------------------
57 -- Some helper functions
58
59 -- | Modify a `Doc' iff it is non-empty.
60 applyNE :: (Doc -> Doc) -> Doc -> Doc
61 applyNE f x = if isEmpty x then empty else f x
62
63 -- | Like `punctuate', but remove nonempty `Doc's first.
64 punctuateNE :: Doc -> [Doc] -> [Doc]
65 punctuateNE p = punctuate p . filter (not . isEmpty)
66
67 slashes :: Doc -> Doc
68 slashes x = char '/' <> x <> char '/'
69
70
71 class Pretty a where
72 pretty :: a -> Doc
73
74 prettyList :: [a] -> Doc
75 prettyList = error
76 "Language.Ding.Pretty: This element does not occur in lists."
77
78 prettyNEList :: NonEmpty a -> Doc
79 prettyNEList = prettyList . NEList.toList
80
81
82
83 ------------------------
84 -- The Pretty instances
85
86
87 instance Pretty Ding where
88 pretty (Dictionary header _ _ body) = pretty header $+$ pretty body
89
90
91 instance Pretty (Body Line) where
92 pretty (Body lines) = prettyList lines
93
94
95 -- See also: Language.Ding.HeaderParser
96 instance Pretty Header where
97 pretty (Header version date copyrightHolder copyrightPeriod license url) =
98 foldr ($+$) empty $
99 [ text "# Version" <+> text "::" <+> text version <+> text date
100 , text "# Copyright (c)" <+> text "::" <+> text copyrightHolder
101 <> char ','
102 , text "#" <+> text copyrightPeriod
103 , text "# License" <+> text "::" <+> text license
104 , text "# URL" <+> text "::" <+> text url
105 ]
106
107
108 instance Pretty Line where
109
110 pretty (Line entries) = prettyList left <+> text "::" <+> prettyList right
111 where
112 unzipEntries :: [Entry] -> ([Group], [Group])
113 unzipEntries = unzip . map (\ (Entry l r ) -> (l, r))
114
115 (left, right) = unzipEntries entries
116
117 prettyList = foldr ($+$) empty . map pretty
118
119
120 instance Pretty Entry where
121 pretty (Entry g h) = pretty g <+> text "::" <+> pretty h
122
123
124 instance Pretty Group where
125 pretty (Group us) = prettyList us
126
127 prettyList = hcat . punctuate (text " | ") . map pretty
128
129
130 instance Pretty Unit where
131 pretty u = hsep $
132 [ hsep $ map (parens . text) $ unitPrefixes u
133 , text $ unitHeadword u
134 , prettyList $ unitGrammar u
135 , prettyList $ unitUsages u
136 , applyNE slashes $ hsep $ punctuate semi $ map text $ unitAbbrevs u
137 , maybe empty pretty $ unitInflected u
138 , hsep $ map (parens . text) $ unitSuffixes u
139 , hsep $ map (text . ('~':)) $ unitReferences u
140 ]
141
142 prettyList = hsep . punctuate semi . map pretty
143
144
145 instance Pretty GrammarInfo where
146 pretty (GramLexCategory gram) = pretty gram
147 pretty (Collocate colloc usgs) = pretty colloc <+> prettyList usgs
148
149 prettyList = applyNE braces . hsep . punctuate semi . map pretty
150
151 instance Pretty GramLexCategory where
152 pretty = hcat . punctuate (char '/') . map text . showGLC
153
154 instance Pretty Collocate where
155 pretty (CollocCase iProns cas) = pref <+> char '+' <> pretty cas
156 where
157 pref = hsep $ punctuate comma $ map text iProns
158 pretty (CollocPOS pos) = char '+' <> pretty pos
159 pretty (CollocNumber number) = char '+' <> pretty number
160
161 instance Pretty Case where
162 pretty = text . showCase
163
164 instance Pretty Number where
165 pretty = text . showNumber
166
167 instance Pretty PartOfSpeech where
168 -- Note: showPOS is expected to always give exactly one string here.
169 pretty = hcat . punctuate (char '/') . map text . showPOS
170
171 instance Pretty Usage where
172 pretty (Usage _ str) = text str
173 prettyList = applyNE brackets . hsep . punctuate comma . map pretty
174
175 instance Pretty InflectedForms where
176 pretty (InflectedForms infl1 infl2) =
177 braces $ prettyNEList infl1 <> semi <+> prettyNEList infl2
178
179 instance Pretty InflectedForm where
180 pretty (InflectedForm form usgs) = text form <+> prettyList usgs
181 prettyList = hsep . punctuate comma . map pretty
182
183
184 -- vi: ts=2 sw=2 et