0 | module Text.ILex.Dependent.Bytes
3 | import Text.ILex.Char.UTF8
4 | import public Data.ByteString
5 | import public Text.ByteBounds
6 | import public Text.ILex.RExp
7 | import public Text.ILex.LTENat
14 | data Bytes : (n : Nat) -> Type where
20 | -> (prev : ByteString)
23 | -> (cur : IBuffer n)
26 | -> (prevOffset : Nat)
31 | -> (curOffset : Nat)
36 | init : {n : _} -> IBuffer n -> Bytes n
37 | init buf = BS empty buf 0 0
40 | newBytes : {n : _} -> (offset : Nat) -> ByteString -> IBuffer n -> Bytes n
46 | , curOffset = o + bs.size
71 | getBytes : In a -> ByteString
72 | getBytes (I (BS p c po co) rf rt _) =
74 | 0 => p <+> bytesFromTill c rf rt
75 | _ => bytesFromTill c rf rt
78 | getString : In a -> String
79 | getString (I (BS p c po co) rf rt _) =
81 | 0 => toString $
p <+> bytesFromTill c rf rt
82 | _ => stringFromTill c rf rt
85 | toFinalPos' : Bytes n -> a -> In a
86 | toFinalPos' bs@(BS {}) = I bs (last n) (last n)
89 | toFinalPos : In a -> In a
90 | toFinalPos (I bs@(BS {n} {}) _ _ v) = I bs (last n) (last n) v
95 | startPos : In a -> BytePos
96 | startPos (I (BS p c po co) (LN f) _ _) =
103 | endPos : In a -> BytePos
104 | endPos (I (BS p c po co) (LN rf) (LN rt) _) =
106 | 0 => endPos po (co+rt)
107 | _ => endPos (co+rf) (co+rt)
111 | bounds : In a -> ByteBounds
112 | bounds (I (BS p c po co) (LN rf) (LN rt) _) =
113 | let till := co + rt
115 | 0 => let from := po in BB (BP from) (endPos from till)
116 | _ => let from := co + rf in BB (BP from) (endPos from till)
120 | bounded : In a -> t -> ByteBounded t
121 | bounded x v = B v $
Bytes.bounds x
126 | failHere : In a -> InnerError e -> BBErr e
130 | unexpectedInner : List String -> In a -> InnerError e
131 | unexpectedInner ss i =
136 | s := String.singleton (cast b)
137 | in case isAscii b of
138 | True => Expected ss s
139 | False => InvalidByte b
140 | bs => Expected ss (toString bs)
143 | unexpectedErr : List String -> In a -> BBErr e
144 | unexpectedErr ss i = failHere i $
unexpectedInner ss i
147 | unclosedErr : ByteBounds -> String -> BBErr e
148 | unclosedErr bnds str = B (Unclosed str) bnds
151 | failUnexpected : List String -> In a -> Either (BBErr e) x
152 | failUnexpected ss = Left . unexpectedErr ss
155 | failUnclosed : ByteBounds -> String -> Either (BBErr e) x
156 | failUnclosed bnds = Left . unclosedErr bnds