Rework the inefficient pretty-printer to use Intermediate too
Getty Ritter
6 years ago
17 | 17 | -- * Default Printing Strategies |
18 | 18 | , basicPrint |
19 | 19 | , flatPrint |
20 |
, un |
|
20 | , unconstrainedPrint | |
21 | 21 | ) where |
22 | 22 | |
23 | 23 | import qualified Data.Foldable as F |
98 | 98 | |
99 | 99 | -- | A default 'SExprPrinter' struct that will always swing subsequent |
100 | 100 | -- expressions onto later lines if they're too long, indenting them |
101 |
-- by two spaces |
|
101 | -- by two spaces, and uses a soft maximum width of 80 characters | |
102 | 102 | basicPrint :: (atom -> Text) -> SExprPrinter atom (SExpr atom) |
103 | 103 | basicPrint printer = SExprPrinter |
104 | 104 | { atomPrinter = printer |
109 | 109 | , indentPrint = True |
110 | 110 | } |
111 | 111 | |
112 | unboundIndentPrint :: (atom -> Text) -> SExprPrinter atom (SExpr atom) | |
113 | unboundIndentPrint printer = SExprPrinter | |
112 | -- | A default 'SExprPrinter' struct that will always swing subsequent | |
113 | -- expressions onto later lines if they're too long, indenting them by | |
114 | -- two spaces, but makes no effort to keep the pretty-printed sources | |
115 | -- inside a maximum width. In the case that we want indented printing | |
116 | -- but don't care about a "maximum" width, we can print more | |
117 | -- efficiently than in other situations. | |
118 | unconstrainedPrint :: (atom -> Text) -> SExprPrinter atom (SExpr atom) | |
119 | unconstrainedPrint printer = SExprPrinter | |
114 | 120 | { atomPrinter = printer |
115 | 121 | , fromCarrier = id |
116 | 122 | , swingIndent = const Swing |
125 | 131 | -- indent information around for each list |
126 | 132 | data Intermediate |
127 | 133 | = IAtom Text |
128 |
| IList Indent |
|
134 | | IList Indent Intermediate (Seq.Seq Intermediate) (Maybe Text) | |
129 | 135 | | IEmpty |
130 | 136 | |
131 | 137 | |
138 | 144 | headOf (SAtom a) = IAtom (printAtom a) |
139 | 145 | headOf SNil = IEmpty |
140 | 146 | headOf (SCons x xs) = |
141 | gather (swing x) (Seq.singleton (headOf x)) xs | |
142 | gather sw rs SNil = | |
143 | IList sw rs Nothing | |
144 | gather sw rs (SAtom a) = | |
145 | IList sw rs (Just (printAtom a)) | |
146 | gather sw rs (SCons x xs) = | |
147 |
gather |
|
147 | gather (swing x) (headOf x) (Seq.empty) xs | |
148 | gather sw hd rs SNil = | |
149 | IList sw hd rs Nothing | |
150 | gather sw hd rs (SAtom a) = | |
151 | IList sw hd rs (Just (printAtom a)) | |
152 | gather sw hd rs (SCons x xs) = | |
153 | gather sw hd (rs Seq.|> headOf x) xs | |
148 | 154 | |
149 | 155 | |
150 | 156 | unboundIndentPrintSExpr :: SExprPrinter a (SExpr a) -> SExpr a -> TL.Text |
158 | 164 | -- this case should never be called with an empty argument to |
159 | 165 | -- @values@, as that should have been translated to @IEmpty@ |
160 | 166 | -- instead. |
161 |
go (IList iv |
|
167 | go (IList iv initial values rest) | |
162 | 168 | -- if we're looking at an s-expression that has no nested |
163 | 169 | -- s-expressions, then we might as well consider it flat and let |
164 | 170 | -- it take the whole line |
165 |
| Just strings <- T.traverse ppBasic |
|
171 | | Just strings <- T.traverse ppBasic (initial Seq.<| values) = | |
166 | 172 | Seq.singleton (B.fromString "(" <> buildUnwords strings <> pTail rest) |
167 | 173 | |
168 | 174 | -- it's not "flat", so we might want to swing after the first thing |
169 | 175 | | Swing <- iv = |
170 | 176 | -- if this match fails, then it means we've failed to |
171 | 177 | -- convert to an Intermediate correctly! |
172 | let x Seq.:< xs = Seq.viewl values | |
173 | butLast = insertParen (go x) <> fmap doIndent (F.foldMap go xs) | |
178 | let butLast = insertParen (go initial) <> fmap doIndent (F.foldMap go values) | |
174 | 179 | in handleTail rest butLast |
175 | 180 | |
176 | 181 | -- ...or after several things |
177 | 182 | | SwingAfter n <- iv = |
178 |
let (hs, xs) = Seq.splitAt n |
|
183 | let (hs, xs) = Seq.splitAt n (initial Seq.<| values) | |
179 | 184 | hd = B.fromString "(" <> buildUnwords (F.foldMap go hs) |
180 | 185 | butLast = hd Seq.<| fmap doIndent (F.foldMap go xs) |
181 | 186 | in handleTail rest butLast |
183 | 188 | -- the 'align' choice is clunkier because we need to know how |
184 | 189 | -- deep to indent, so we have to force the first builder to grab its size |
185 | 190 | | otherwise = |
186 | let x Seq.:< xs = Seq.viewl values | |
187 | -- so we grab that and figure out its length plus two (for | |
191 | let -- so we grab that and figure out its length plus two (for | |
188 | 192 | -- the leading paren and the following space). This uses a |
189 | 193 | -- max because it's possible the first thing is itself a |
190 | 194 | -- multi-line s-expression (in which case it seems like |
191 | 195 | -- using the Align strategy is a terrible idea, but who am |
192 | 196 | -- I to quarrel with the wild fruits upon the Tree of |
193 | 197 | -- Life?) |
194 | len = 2 + F.maximum (fmap (TL.length . B.toLazyText) (go x)) | |
195 | in case Seq.viewl xs of | |
198 | len = 2 + F.maximum (fmap (TL.length . B.toLazyText) (go initial)) | |
199 | in case Seq.viewl values of | |
196 | 200 | -- if there's nothing after the head of the expression, then |
197 | 201 | -- we simply close it |
198 |
Seq.EmptyL -> insertParen (insertCloseParen (go |
|
202 | Seq.EmptyL -> insertParen (insertCloseParen (go initial)) | |
199 | 203 | -- otherwise, we put the first two things on the same line |
200 | 204 | -- with spaces and everything else gets indended the |
201 | 205 | -- forementioned length |
202 | 206 | y Seq.:< ys -> |
203 |
let hd = B.fromString "(" <> buildUnwords (F.foldMap go (Seq.fromList [ |
|
207 | let hd = B.fromString "(" <> buildUnwords (F.foldMap go (Seq.fromList [initial, y])) | |
204 | 208 | butLast = hd Seq.<| fmap (doIndentOf (fromIntegral len)) (F.foldMap go ys) |
205 | 209 | in handleTail rest butLast |
206 | 210 | -- B.fromString "(" <> buildUnwords (F.foldMap go (Seq.fromList [x, y])) |
297 | 301 | setIndentStrategy st pr = pr { swingIndent = st } |
298 | 302 | |
299 | 303 | |
300 | -- Sort of like 'unlines' but without the trailing newline | |
301 | joinLines :: [Text] -> Text | |
302 | joinLines = T.intercalate "\n" | |
303 | ||
304 | ||
305 | 304 | -- Indents a line by n spaces |
306 | 305 | indent :: Int -> Text -> Text |
307 | 306 | indent n ts = T.replicate n " " <> ts |
308 | 307 | |
309 | 308 | |
309 | -- Sort of like 'unlines' but without the trailing newline | |
310 | joinLinesS :: Seq.Seq Text -> Text | |
311 | joinLinesS s = case Seq.viewl s of | |
312 | Seq.EmptyL -> "" | |
313 | t Seq.:< ts | |
314 | | F.null ts -> t | |
315 | | otherwise -> t <> "\n" <> joinLinesS ts | |
316 | ||
317 | ||
318 | -- Sort of like 'unlines' but without the trailing newline | |
319 | unwordsS :: Seq.Seq Text -> Text | |
320 | unwordsS s = case Seq.viewl s of | |
321 | Seq.EmptyL -> "" | |
322 | t Seq.:< ts | |
323 | | F.null ts -> t | |
324 | | otherwise -> t <> " " <> joinLinesS ts | |
325 | ||
326 | ||
310 | 327 | -- Indents every line n spaces, and adds a newline to the beginning |
311 | 328 | -- used in swung indents |
312 | indentAll :: Int -> [Text] -> Text | |
313 | indentAll n = ("\n" <>) . joinLines . map (indent n) | |
329 | indentAllS :: Int -> Seq.Seq Text -> Text | |
330 | indentAllS n = ("\n" <>) . joinLinesS . fmap (indent n) | |
314 | 331 | |
315 | 332 | |
316 | 333 | -- Indents every line but the first by some amount |
317 | 334 | -- used in aligned indents |
318 | indentSubsequent :: Int -> [Text] -> Text | |
319 | indentSubsequent _ [] = "" | |
320 | indentSubsequent _ [t] = t | |
321 | indentSubsequent n (t:ts) = joinLines (t : go ts) | |
322 | where go = map (indent n) | |
335 | indentSubsequentS :: Int -> Seq.Seq Text -> Text | |
336 | indentSubsequentS n s = case Seq.viewl s of | |
337 | Seq.EmptyL -> "" | |
338 | t Seq.:< ts | |
339 | | F.null ts -> t | |
340 | | otherwise -> joinLinesS (t Seq.<| fmap (indent n) ts) | |
341 | -- where go = fmap (indent n) | |
323 | 342 | |
324 | 343 | |
325 | 344 | -- oh god this code is so disgusting |
334 | 353 | Nothing |
335 | 354 | | indentPrint -> TL.toStrict (unboundIndentPrintSExpr pr (fromCarrier expr)) |
336 | 355 | | otherwise -> flatPrintSExpr (fmap atomPrinter (fromCarrier expr)) |
337 | Just _ -> indentPrintSExpr pr expr | |
338 | ||
339 | ||
340 | indentPrintSExpr :: SExprPrinter a (SExpr a) -> SExpr a -> Text | |
341 | indentPrintSExpr SExprPrinter { .. } = pHead 0 | |
356 | Just _ -> indentPrintSExpr' pr expr | |
357 | ||
358 | ||
359 | indentPrintSExpr' :: SExprPrinter a (SExpr a) -> SExpr a -> Text | |
360 | indentPrintSExpr' pr@SExprPrinter { .. } = pp 0 . toIntermediate pr | |
342 | 361 | where |
343 | pHead _ SNil = "()" | |
344 | pHead _ (SAtom a) = atomPrinter a | |
345 | pHead ind (SCons x xs) = gather ind x xs id | |
346 | gather ind h (SCons x xs) k = gather ind h xs (k . (x:)) | |
347 | gather ind h end k = "(" <> hd <> body <> tl <> ")" | |
348 | where tl = case end of | |
349 | SNil -> "" | |
350 | SAtom a -> " . " <> atomPrinter a | |
351 | SCons _ _ -> error "[unreachable]" | |
352 | hd = indentSubsequent ind [pHead (ind+1) h] | |
353 | lst = k [] | |
354 | flat = T.unwords (map (pHead (ind+1)) lst) | |
355 | headWidth = T.length hd + 1 | |
356 | indented = | |
357 | case swingIndent h of | |
358 | SwingAfter n -> | |
359 | let (l, ls) = splitAt n lst | |
360 | t = T.unwords (map (pHead (ind+1)) l) | |
361 | ts = indentAll (ind + indentAmount) | |
362 | (map (pHead (ind + indentAmount)) ls) | |
363 | in t <> ts | |
364 | Swing -> | |
365 | indentAll (ind + indentAmount) | |
366 | (map (pHead (ind + indentAmount)) lst) | |
367 | Align -> | |
368 | indentSubsequent (ind + headWidth + 1) | |
369 | (map (pHead (ind + headWidth + 1)) lst) | |
370 | body | |
371 | | length lst == 0 = "" | |
372 | | Just maxAmt <- maxWidth | |
373 | , T.length flat + ind > maxAmt = " " <> indented | |
374 | | otherwise = " " <> flat | |
375 | ||
376 | -- where | |
377 | -- -- this is the base-case that knows how to print empty lists and | |
378 | -- -- atoms | |
379 | -- pHead _ SNil = B.fromString "()" | |
380 | -- pHead _ (SAtom a) = B.fromText a | |
381 | -- pHead ind (SCons x xs) = gather ind x xs id 0 | |
382 | ||
383 | -- -- otherwise, we trawl through the list grabbing every element... | |
384 | -- gather ind h (SCons x xs) k r = gather ind h xs (k . (x:)) (r + T.length x) | |
385 | -- gather ind h end k r = B.fromString "(" <> hd <> body <> tl <> B.fromString ")" | |
386 | -- where | |
387 | -- tl = case end of | |
388 | -- SNil -> mempty | |
389 | -- SAtom a -> B.fromString " . " <> B.fromText a | |
390 | -- SCons _ _ -> error "[unreachable]" | |
391 | -- hd = indentSubsequent ind [pHead (ind+1) h] | |
392 | -- lst = k [] | |
393 | -- flat = T.unwords (map (pHead (ind+1)) lst) | |
394 | -- headWidth = T.length hd + 1 | |
395 | -- indented = | |
396 | -- case swingIndent h of | |
397 | -- SwingAfter n -> | |
398 | -- let (l, ls) = splitAt n lst | |
399 | -- t = T.unwords (map (pHead (ind+1)) l) | |
400 | -- ts = indentAll (ind + indentAmount) | |
401 | -- (map (pHead (ind + indentAmount)) ls) | |
402 | -- in t <> ts | |
403 | -- Swing -> | |
404 | -- indentAll (ind + indentAmount) | |
405 | -- (map (pHead (ind + indentAmount)) lst) | |
406 | -- Align -> | |
407 | -- indentSubsequent (ind + headWidth + 1) | |
408 | -- (map (pHead (ind + headWidth + 1)) lst) | |
409 | -- body | |
410 | -- | length lst == 0 = B.fromString "" | |
411 | -- | Just maxAmt <- maxWidth | |
412 | -- , T.length flat + ind > maxAmt = B.fromString " " <> indented | |
413 |
|
|
362 | pp _ IEmpty = "()" | |
363 | pp _ (IAtom t) = t | |
364 | pp ind (IList i h values end) = "(" <> hd <> body <> tl <> ")" | |
365 | where | |
366 | tl = case end of | |
367 | Nothing -> "" | |
368 | Just x -> " . " <> x | |
369 | hd = pp (ind+1) h | |
370 | flat = unwordsS (fmap (pp (ind + 1)) values) | |
371 | headWidth = T.length hd + 1 | |
372 | indented = | |
373 | case i of | |
374 | SwingAfter n -> | |
375 | let (l, ls) = Seq.splitAt n values | |
376 | t = unwordsS (fmap (pp (ind+1)) l) | |
377 | ts = indentAllS (ind + indentAmount) | |
378 | (fmap (pp (ind + indentAmount)) ls) | |
379 | in t <> ts | |
380 | Swing -> | |
381 | indentAllS (ind + indentAmount) | |
382 | (fmap (pp (ind + indentAmount)) values) | |
383 | Align -> | |
384 | indentSubsequentS (ind + headWidth + 1) | |
385 | (fmap (pp (ind + headWidth + 1)) values) | |
386 | body | |
387 | | length values == 0 = "" | |
388 | | Just maxAmt <- maxWidth | |
389 | , T.length flat + ind > maxAmt = " " <> indented | |
390 | | otherwise = " " <> flat | |
391 | ||
414 | 392 | |
415 | 393 | -- if we don't indent anything, then we can ignore a bunch of the |
416 | 394 | -- details above |