3 | import Data.ByteString
4 | import Derive.Prelude
8 | %language ElabReflection
17 | record Position where
31 | Semigroup Position where
32 | P l1 c1 <+> P 0 c2 = P l1 (c1+c2)
33 | P l1 c1 <+> P l2 c2 = P (l1+l2) c2
36 | Monoid Position where
45 | public export %inline
46 | incCol : Position -> Position
51 | public export %inline
52 | incLine : Position -> Position
53 | incLine p = P (S p.line) 0
56 | public export %inline
57 | addCol : Nat -> Position -> Position
58 | addCol n = {col $= (+ n)}
64 | relativeTo : (p2,p1 : Position) -> Position
65 | relativeTo (P l c) (P ls cs) =
66 | if l <= ls then P 0 (c `minus` cs) else P (l `minus` ls) c
75 | incString : String -> Position -> Position
76 | incString s (P l c) = go l c (unpack s)
78 | go : Nat -> Nat -> List Char -> Position
80 | go l c ('\n' :: xs) = go (S l) 0 xs
81 | go l c (x :: xs) = go l (S c) xs
91 | incBytes : ByteString -> Position -> Position
92 | incBytes (BS n bv) (P l c) = go l c n
94 | go : Nat -> Nat -> (k : Nat) -> (x : Ix k n) => Position
98 | 0x0a => go (S l) 0 k
99 | b => case b < 128 || b .&. 0b1100_0000 == 0b1100_000 of
100 | True => go l (S c) k
107 | public export %inline
108 | next : Char -> Position -> Position
109 | next '\n' = incLine
112 | %runElab derive "Position" [Show,Eq,Ord]
115 | Interpolation Position where
116 | interpolate (P l c) = show (l+1) ++ ":" ++ show (c+1)
125 | data Bounds : Type where
126 | BS : (start, end : Position) -> Bounds
129 | %runElab derive "Bounds" [Show,Eq]
133 | relativeTo : Bounds -> Position -> Bounds
134 | relativeTo NoBounds _ = NoBounds
135 | relativeTo (BS s e) p = BS (s `relativeTo` p) (e `relativeTo` p)
138 | public export %inline
139 | oneChar : Position -> Bounds
140 | oneChar p = BS p $
incCol p
143 | Interpolation Bounds where
144 | interpolate (BS s e) = "\{s}--\{e}"
145 | interpolate NoBounds = ""
148 | Semigroup Bounds where
151 | BS s1 e1 <+> BS s2 e2 = BS (min s1 s2) (max e1 e2)
154 | Monoid Bounds where
163 | record Bounded ty where
168 | %runElab derive "Bounded" [Show,Eq]
172 | bounded : a -> (start,end : Position) -> Bounded a
173 | bounded v s e = B v $
BS s e
176 | public export %inline
177 | oneChar : a -> Position -> Bounded a
178 | oneChar v p = B v $
oneChar p
182 | app : Bounded (a -> b) -> Bounded a -> Bounded b
183 | app (B vf b1) (B va b2) = B (vf va) (b1 <+> b2)
187 | bind : Bounded a -> (a -> Bounded b) -> Bounded b
190 | in B vb (b1 <+> b2)
193 | fromPosition : Bounded a -> Position -> Bounded a
194 | fromPosition (B v $
BS s e) y = B v $
BS (y <+> s) (y <+> e)
195 | fromPosition (B v NoBounds) y = B v $
BS y (incCol y)
198 | Functor Bounded where
199 | map f (B val bs) = B (f val) bs
201 | public export %inline
202 | Applicative Bounded where
203 | pure v = B v neutral
206 | public export %inline
207 | Monad Bounded where
211 | Foldable Bounded where
212 | foldr c n b = c b.val n
213 | foldl c n b = c n b.val
214 | foldMap f b = f b.val
219 | Traversable Bounded where
220 | traverse f (B v bs) = (`B` bs) <$> f v