0 | module Text.TSV.Decoder
3 | import Data.List.Quantifiers as LQ
6 | import Data.Vect.Quantifiers as VQ
7 | import Text.Lex.Manual.Syntax
8 | import Text.Parse.Manual
17 | interface TSVDecoder a where
18 | constructor MkTSVDecoder
19 | decodeFrom : Tok False e a
21 | str' : SnocList Char -> AutoTok e String
22 | str' sc [] = Succ (pack $
sc <>> []) []
24 | if isControl x then Succ (pack $
sc <>> []) (x::xs) else str' (sc :< x) xs
27 | str : Tok False e String
29 | str (x::xs) = if isControl x then Succ "" (x::xs) else weaken $
str' [<x] xs
32 | refine : Tok b1 e a -> (a -> Maybe b) -> Tok False e b
33 | refine f g cs = case weaken (f cs) of
34 | Succ va cs2 => case g va of
35 | Just vb => Succ vb cs2
36 | Nothing => unknownRange Same cs2
37 | Fail x y z => Fail x y z
40 | refineNum : Tok b1 e a -> (a -> Maybe b) -> Tok False e b
41 | refineNum f g cs = case weaken (f cs) of
42 | Succ va cs2 @{p} => case g va of
43 | Just vb => Succ vb cs2
44 | Nothing => range (OutOfBounds $
packPrefix p) Same cs2
45 | Fail x y z => Fail x y z
48 | natural : (Nat -> Maybe a) -> Tok False e a
49 | natural = refineNum (tok dec)
52 | boundedNat : Cast Nat a => Nat -> Tok False e a
53 | boundedNat m = natural $
\x => if x <= m then Just (cast x) else Nothing
56 | integral : (Integer -> Maybe a) -> Tok False e a
57 | integral = refineNum (tok int)
60 | boundedInt : Cast Integer a => Integer -> Integer -> Tok False e a
61 | boundedInt l u = integral $
\x =>
62 | if x >= l && x <= u then Just (cast x) else Nothing
65 | float : (Double -> Maybe a) -> Tok False e a
66 | float = refineNum double
69 | TSVDecoder Bits8 where
70 | decodeFrom = boundedNat 0xff
73 | TSVDecoder Bits16 where
74 | decodeFrom = boundedNat 0xffff
77 | TSVDecoder Bits32 where
78 | decodeFrom = boundedNat 0xffff_ffff
81 | TSVDecoder Bits64 where
82 | decodeFrom = boundedNat 0xffff_ffff_ffff_ffff
85 | TSVDecoder Int8 where
86 | decodeFrom = boundedInt (-
0x80) 0x7f
89 | TSVDecoder Int16 where
90 | decodeFrom = boundedInt (-
0x8000) 0x7fff
93 | TSVDecoder Int32 where
94 | decodeFrom = boundedInt (-
0x8000_0000) 0x7fff_ffff
97 | TSVDecoder Int64 where
98 | decodeFrom = boundedInt (-
0x8000_0000_0000_0000) 0x7fff_ffff_ffff_ffff
101 | TSVDecoder Nat where
102 | decodeFrom cs = weaken $
dec cs
105 | TSVDecoder Integer where
106 | decodeFrom cs = weaken $
int cs
109 | TSVDecoder Double where
110 | decodeFrom cs = weaken $
double cs
113 | TSVDecoder String where
117 | TSVDecoder Bool where
118 | decodeFrom = refine str $
\case
119 | "True" => Just True
120 | "False" => Just False
121 | "true" => Just True
122 | "false" => Just False
130 | TSVDecoder a => TSVDecoder (Maybe a) where
131 | decodeFrom [] = Succ Nothing []
132 | decodeFrom (x :: xs) =
133 | if isControl x then Succ Nothing (x::xs)
134 | else Just <$> decodeFrom (x::xs)
137 | TSVDecoder a => TSVDecoder b => TSVDecoder (a,b) where
138 | decodeFrom = Tok.[| MkPair decodeFrom decodeFrom |]
145 | tab : Tok False e ()
146 | tab ('\t' :: xs) = Succ () xs
147 | tab (x :: xs) = single (Expected ["<tab>"] (singleton x)) Same
148 | tab [] = eoiAt Same
150 | decAll : All (TSVDecoder . f) ks => Tok False e (LQ.All.All f ks)
151 | decAll @{[]} = pure []
152 | decAll @{[_]} = \cs => (::[]) <$> decodeFrom cs
153 | decAll @{_::_} = Tok.do
159 | decAllV : All (TSVDecoder . f) ks => Tok False e (VQ.All.All f ks)
160 | decAllV @{[]} = pure []
161 | decAllV @{[_]} = \cs => (::[]) <$> decodeFrom cs
162 | decAllV @{_::_} = Tok.do
169 | All (TSVDecoder . f) ks => TSVDecoder (LQ.All.All f ks) where
170 | decodeFrom = decAll
173 | All (TSVDecoder . f) ks => TSVDecoder (VQ.All.All f ks) where
174 | decodeFrom = decAllV
181 | {auto _ : TSVDecoder a}
184 | -> (cs : List Char)
185 | -> (0 acc : SuffixAcc cs)
186 | -> Either (Bounded $
InnerError e) (List a)
187 | readTable' sx pos [] _ = Right (sx <>> [])
188 | readTable' sx pos xs (SA r) = case decodeFrom {a} xs of
189 | Succ v xs2 @{p} => case xs2 of
190 | [] => Right (sx <>> [v])
191 | '\n' :: t2 => readTable' (sx :< v) (incLine pos) t2 r
192 | '\r'::'\n' :: t2 => readTable' (sx :< v) (incLine pos) t2 r
194 | let bs := Bounds.oneChar (move pos p)
195 | in if isControl c then Left $
B (InvalidControl c) bs
196 | else unexpected (B (show c) bs)
197 | Fail x y z => Left (boundedErr pos x y z)
201 | {auto _ : TSVDecoder a}
204 | -> Either (ParseError e) (List a)
206 | mapFst (toParseError o s) (readTable' [<] begin (unpack s) suffixAcc)