| 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 where |
| 10 |
|
| 11 |
open import Data.Maybe.Base |
| 12 |
open import Text.Format.Generic |
| 13 |
|
| 14 |
-- Formatted types |
| 15 |
open import Data.Char.Base using (Char) |
| 16 |
open import Data.Integer.Base using (ℤ) |
| 17 |
open import Data.Float.Base using (Float) |
| 18 |
open import Data.Nat.Base using (ℕ) |
| 19 |
open import Data.String.Base using (String) |
| 20 |
|
| 21 |
------------------------------------------------------------------------ |
| 22 |
-- Basic types |
| 23 |
|
| 24 |
data ArgChunk : Set where |
| 25 |
ℕArg ℤArg FloatArg CharArg StringArg : ArgChunk |
| 26 |
|
| 27 |
------------------------------------------------------------------------ |
| 28 |
-- Semantics |
| 29 |
|
| 30 |
ArgType : (fmt : ArgChunk) → Set |
| 31 |
ArgType ℕArg = ℕ |
| 32 |
ArgType ℤArg = ℤ |
| 33 |
ArgType FloatArg = Float |
| 34 |
ArgType CharArg = Char |
| 35 |
ArgType StringArg = String |
| 36 |
|
| 37 |
lexArg : Char → Maybe ArgChunk |
| 38 |
lexArg 'd' = just ℤArg |
| 39 |
lexArg 'i' = just ℤArg |
| 40 |
lexArg 'u' = just ℕArg |
| 41 |
lexArg 'f' = just FloatArg |
| 42 |
lexArg 'c' = just CharArg |
| 43 |
lexArg 's' = just StringArg |
| 44 |
lexArg _ = nothing |
| 45 |
|
| 46 |
formatSpec : FormatSpec |
| 47 |
formatSpec .FormatSpec.ArgChunk = ArgChunk |
| 48 |
formatSpec .FormatSpec.ArgType = ArgType |
| 49 |
formatSpec .FormatSpec.lexArg = lexArg |
| 50 |
|
| 51 |
open Format formatSpec public |
| 52 |
|
| 53 |
pattern `ℕ = Arg ℕArg |
| 54 |
pattern `ℤ = Arg ℤArg |
| 55 |
pattern `Float = Arg FloatArg |
| 56 |
pattern `Char = Arg CharArg |
| 57 |
pattern `String = Arg StringArg |