Debian Codegraph @

src/Reflection/TCM/Format.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Printf-style versions of typeError and debugPrint
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Reflection.TCM.Format where
10
11 open import Agda.Builtin.Reflection using (Name; Term; ErrorPart; termErr; nameErr; strErr;
12 TC; typeError; debugPrint)
13 open import Level using (Level)
14 open import Function.Base using (_∘_)
15 open import Data.List.Base using (List; [_]; concat)
16 open import Data.Maybe.Base using (Maybe; nothing; just)
17 open import Data.Nat.Base using (ℕ)
18 open import Data.String.Base using (String)
19 open import Data.Sum.Base using (_⊎_; inj₁; inj₂)
20 open import Data.Unit.Base using (⊤)
21
22 import Text.Format as StdFormat using (formatSpec)
23 import Text.Printf as StdPrintf using (printfSpec)
24
25 open import Text.Format.Generic
26 open import Text.Printf.Generic
27
28 private
29 variable
30 ℓ : Level
31 A : Set ℓ
32
33 ------------------------------------------------------------------------
34 -- Format specification.
35 -- This extends the formats from Text.Printf with
36 -- %t - Term
37 -- %q - Name
38 -- %e - List ErrorPart
39 -- Rendering goes to List ErrorPart.
40
41 module Specification where
42
43 data ErrorChunk : Set where
44 `Term `Name `Parts : ErrorChunk
45
46 errorSpec : FormatSpec
47 errorSpec .FormatSpec.ArgChunk = ErrorChunk
48 errorSpec .FormatSpec.ArgType `Term = Term
49 errorSpec .FormatSpec.ArgType `Name = Name
50 errorSpec .FormatSpec.ArgType `Parts = List ErrorPart
51 errorSpec .FormatSpec.lexArg 't' = just `Term
52 errorSpec .FormatSpec.lexArg 'q' = just `Name
53 errorSpec .FormatSpec.lexArg 'e' = just `Parts
54 errorSpec .FormatSpec.lexArg _ = nothing
55
56 formatSpec : FormatSpec
57 formatSpec = unionSpec errorSpec StdFormat.formatSpec
58
59 open PrintfSpec
60
61 printfSpec : PrintfSpec formatSpec (List ErrorPart)
62 printfSpec .renderArg (inj₁ `Term) t = [ termErr t ]
63 printfSpec .renderArg (inj₁ `Name) n = [ nameErr n ]
64 printfSpec .renderArg (inj₁ `Parts) es = es
65 printfSpec .renderArg (inj₂ arg) x = [ strErr (StdPrintf.printfSpec .renderArg arg x) ]
66 printfSpec .renderStr s = [ strErr s ]
67
68 open Specification
69
70 open Format formatSpec
71 open Type formatSpec renaming (map to mapPrintf)
72 open Render printfSpec
73
74 ------------------------------------------------------------------------
75 -- Printf versions of typeError and debugPrint
76
77 typeErrorFmt : (fmt : String) → Printf (lexer fmt) (TC A)
78 typeErrorFmt fmt = mapPrintf (lexer fmt) (typeError ∘ concat) (printf fmt)
79
80 debugPrintFmt : String → ℕ → (fmt : String) → Printf (lexer fmt) (TC ⊤)
81 debugPrintFmt tag lvl fmt = mapPrintf (lexer fmt) (debugPrint tag lvl ∘ concat) (printf fmt)
82
83 -- Combine with "%e" format for nested format calls.
84 errorPartFmt : (fmt : String) → Printf (lexer fmt) (List ErrorPart)
85 errorPartFmt fmt = mapPrintf (lexer fmt) concat (printf fmt)