Debian Codegraph @

src/Text/Format/Generic.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Format strings for Printf and Scanf
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Text.Format.Generic where
10
11 open import Level using (0ℓ)
12 open import Effect.Applicative
13 open import Data.Char.Base using (Char)
14 open import Data.List.Base as List hiding (sum)
15 open import Data.Maybe.Base as Maybe
16 open import Data.Nat.Base
17 open import Data.Nat.ListAction using (sum)
18 open import Data.Product.Base using (_,_)
19 open import Data.Product.Nary.NonDependent
20 open import Data.Sum.Base
21 open import Data.String.Base
22 import Data.Sum.Effectful.Left as Sumₗ
23 open import Function.Base
24 open import Function.Nary.NonDependent using (0ℓs; Sets)
25 open import Function.Strict
26
27 ------------------------------------------------------------------------
28 -- Format specifications.
29 -- Defines the supported %-codes.
30
31 record FormatSpec : Set₁ where
32 field
33 ArgChunk : Set
34 ArgType : ArgChunk → Set
35 lexArg : Char → Maybe ArgChunk
36
37 module _ where
38 open FormatSpec
39
40 -- Left-biased union of format specs.
41 unionSpec : FormatSpec → FormatSpec → FormatSpec
42 unionSpec spec₁ spec₂ .ArgChunk = spec₁ .ArgChunk ⊎ spec₂ .ArgChunk
43 unionSpec spec₁ spec₂ .ArgType (inj₁ a) = spec₁ .ArgType a
44 unionSpec spec₁ spec₂ .ArgType (inj₂ a) = spec₂ .ArgType a
45 unionSpec spec₁ spec₂ .lexArg c =
46 Maybe.map inj₁ (spec₁ .lexArg c) <∣>
47 Maybe.map inj₂ (spec₂ .lexArg c)
48
49 module Format (spec : FormatSpec) where
50
51 open FormatSpec spec
52
53 ----------------------------------------------------------------------
54 -- Basic types
55
56 data Chunk : Set where
57 Arg : ArgChunk → Chunk
58 Raw : String → Chunk
59
60 Format : Set
61 Format = List Chunk
62
63 ----------------------------------------------------------------------
64 -- Semantics
65
66 size : Format → ℕ
67 size = sum ∘′ List.map λ { (Raw _) → 0; _ → 1 }
68
69 -- Meaning of a format as a list of value types
70
71 ⟦_⟧ : (fmt : Format) → Sets (size fmt) 0ℓs
72 ⟦ [] ⟧ = _
73 ⟦ Arg a ∷ cs ⟧ = ArgType a , ⟦ cs ⟧
74 ⟦ Raw _ ∷ cs ⟧ = ⟦ cs ⟧
75
76 ----------------------------------------------------------------------
77 -- Lexer: from Strings to Formats
78
79 -- Lexing may fail. To have a useful error message, we defined the
80 -- following enumerated type
81
82 data Error : Set where
83 UnexpectedEndOfString : String → Error
84 -- ^ expected a type declaration; found an empty string
85 InvalidType : String → Char → String → Error
86 -- ^ invalid type declaration
87 -- return a focus: prefix processed, character causing failure, rest
88
89 lexer : String → Error ⊎ List Chunk
90 lexer input = loop [] [] (toList input) where
91
92 open RawApplicative (Sumₗ.applicative Error 0ℓ)
93
94 -- Type synonyms used locally to document the code
95 RevWord = List Char -- Mere characters accumulated so far
96 Prefix = RevWord -- Prefix of the input String already read
97
98 toRevString : RevWord → String
99 toRevString = fromList ∘′ reverse
100
101 -- Push a Raw token if we have accumulated some mere characters
102 push : RevWord → List Chunk → List Chunk
103 push [] ks = ks
104 push cs ks = Raw (toRevString cs) ∷ ks
105
106 -- Main loop
107 loop : RevWord → Prefix → List Char → Error ⊎ List Chunk
108 type : Prefix → List Char → Error ⊎ List Chunk
109
110 loop acc bef [] = pure (push acc [])
111 -- escaped '%' character: treat like a mere character
112 loop acc bef ('%' ∷ '%' ∷ cs) = loop ('%' ∷ acc) ('%' ∷ '%' ∷ bef) cs
113 -- non escaped '%': type declaration following
114 loop acc bef ('%' ∷ cs) = push acc <$> type ('%' ∷ bef) cs
115 -- mere character: push onto the accumulator
116 loop acc bef (c ∷ cs) = loop (c ∷ acc) (c ∷ bef) cs
117
118 type bef [] = inj₁ (UnexpectedEndOfString input)
119 type bef (c ∷ cs) = _∷_ <$> chunk c ⊛ loop [] (c ∷ bef) cs where
120
121 chunk : Char → Error ⊎ Chunk
122 chunk c =
123 case lexArg c of λ where
124 (just ch) → pure (Arg ch)
125 nothing →
126 force′ (toRevString bef) λ prefix →
127 force′ (fromList cs) λ suffix →
128 inj₁ (InvalidType prefix c suffix)