Debian Codegraph @

src/Text/Pretty.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Pretty Printing
5 -- This module is based on Jean-Philippe Bernardy's functional pearl
6 -- "A Pretty But Not Greedy Printer"
7 ------------------------------------------------------------------------
8
9 {-# OPTIONS --with-K #-}
10
11 open import Data.Nat.Base using (ℕ)
12
13 module Text.Pretty (width : ℕ) where
14
15 import Level
16 open import Data.Char.Base using (Char)
17 open import Data.List.Base
18 using (List; _∷_; []; [_]; uncons; _++_; map; filter)
19 open import Data.List.NonEmpty as List⁺ using (foldr₁)
20 open import Data.Maybe.Base using (maybe′)
21 open import Data.Product.Base using (uncurry)
22 open import Data.String.Base using (String; fromList; replicate)
23 open import Function.Base using (_∘_; _∘′_; _$_)
24
25 open import Effect.Monad using (RawMonad)
26 import Data.List.Effectful as List
27 open RawMonad (List.monad {Level.zero})
28
29 import Data.Nat.Properties as ℕ
30 open import Data.List.Extrema.Core ℕ.≤-totalOrder using (⊓ᴸ)
31
32 ------------------------------------------------------------------------
33 -- Internal representation of documents and rendering function
34
35 import Text.Pretty.Core as Core
36
37 record Doc : Set where
38 constructor mkDoc
39 field runDoc : List Core.Block
40 open Doc public
41
42 render : Doc → String
43 render = Core.render
44 ∘ maybe′ (foldr₁ (⊓ᴸ Core.Block.height) ∘′ uncurry List⁺._∷_) Core.empty
45 ∘ uncons
46 ∘′ runDoc
47
48 ------------------------------------------------------------------------
49 -- Basic building blocks
50
51 fail : Doc
52 fail = mkDoc []
53
54 text : String → Doc
55 text = mkDoc ∘′ filter (Core.valid width) ∘ pure ∘ Core.text
56
57 empty : Doc
58 empty = text ""
59
60 char : Char → Doc
61 char c = text (fromList (c ∷ []))
62
63 spaces : ℕ → Doc
64 spaces n = text (replicate n ' ')
65
66 semi colon comma space dot : Doc
67 semi = char ';'; colon = char ':'
68 comma = char ','; space = char ' '; dot = char '.'
69
70 backslash forwardslash equal : Doc
71 backslash = char '\\'; forwardslash = char '/'; equal = char '='
72
73 squote dquote : Doc
74 squote = char '\''; dquote = char '"'
75
76 lparen rparen langle rangle : Doc
77 lparen = char '('; rparen = char ')'
78 langle = char '<'; rangle = char '>'
79
80 lbrace rbrace llbrace rrbrace : Doc
81 lbrace = char '{'; rbrace = char '}'
82 llbrace = char '⦃'; rrbrace = char '⦄'
83
84 lbracket rbracket llbracket rrbracket : Doc
85 lbracket = char '['; rbracket = char ']'
86 llbracket = char '⟦'; rrbracket = char '⟧'
87
88 ------------------------------------------------------------------------
89 -- Combining two documents
90
91 infixr 5 _<>_
92 _<>_ : Doc → Doc → Doc
93 xs <> ys = mkDoc $
94 let candidates = Core._<>_ <$> runDoc xs ⊛ runDoc ys in
95 filter (Core.valid width) candidates
96
97
98 flush : Doc → Doc
99 flush = mkDoc ∘′ map Core.flush ∘′ runDoc
100
101 infixr 5 _<+>_
102 _<+>_ : Doc → Doc → Doc
103 x <+> y = x <> space <> y
104
105 infixr 5 _$$_
106 _$$_ : Doc → Doc → Doc
107 x $$ y = flush x <> y
108
109 infixr 4 _<|>_
110 _<|>_ : Doc → Doc → Doc
111 x <|> y = mkDoc (runDoc x ++ runDoc y)
112
113 ------------------------------------------------------------------------
114 -- Combining lists of documents
115
116 foldDoc : (Doc → Doc → Doc) → List Doc → Doc
117 foldDoc _ [] = empty
118 foldDoc _ (x ∷ []) = x
119 foldDoc f (x ∷ xs) = f x (foldDoc f xs)
120
121 hsep vcat : List Doc → Doc
122 hsep = foldDoc _<+>_
123 vcat = foldDoc _$$_
124
125 sep : List Doc → Doc
126 sep [] = empty
127 sep xs = hsep xs <|> vcat xs
128
129 ------------------------------------------------------------------------
130 -- Defined combinators
131
132 parens : Doc → Doc
133 parens d = lparen <> d <> rparen
134
135 commaSep : List Doc → Doc
136 commaSep = foldDoc (λ d e → d <> comma <+> e)
137
138 newline : Doc
139 newline = flush empty