0 | module Text.ILex.Interfaces
3 | import Data.Linear.Ref1
6 | import Text.ByteBounds
7 | import Text.ILex.Char.UTF8
8 | import Text.ILex.Parser
9 | import Text.ILex.Util
10 | import Text.ParseError
25 | interface HasBBErr (0 s : Type -> Type) (0 e : Type) | s where
27 | error : s q -> Ref q (Maybe $
BBErr e)
32 | interface HasStringLits (0 s : Type -> Type) where
34 | strings : s q -> Ref q (SnocList String)
39 | interface HasStack (0 s : Type -> Type) (0 a : Type) | s where
41 | stack : s q -> Ref q a
48 | go : a -> (s q => F1 q (Index r)) -> (a,Step q r s)
49 | go x f = (x, Run $
\(E x t) => f t)
52 | ign : a -> (s q => F1 q ()) -> (a,Step q r s)
53 | ign x f = (x, Ign $
\(E x t) => f t)
56 | goBS : HasBytes s => a -> (s q => ByteString -> F1 q (Index r)) -> (a,Step q r s)
57 | goBS x f = (x, Run $
\(E x t) => let bs # t := getBytes t in f bs t)
60 | goStr : HasBytes s => a -> (s q => String -> F1 q (Index r)) -> (a,Step q r s)
61 | goStr x f = (x, Run $
\(E x t) => let bs # t := getBytes t in f (toString bs) t)
65 | writeAs : Ref q a -> a -> r -> F1 q r
66 | writeAs ref v res = write1 ref v >> pure res
70 | push1 : Ref q (SnocList a) -> a -> F1' q
78 | pop1 : Ref q (SnocList a) -> F1' q
81 | sv:<_ => write1 ref sv
87 | replace1 : Ref q a -> a -> F1 q a
88 | replace1 ref v = T1.do
96 | getList : Ref q (SnocList a) -> F1 q (List a)
98 | sv <- replace1 ref [<]
104 | getStack : HasStack s a => (sk : s q) => F1 q a
105 | getStack = read1 (stack sk)
110 | putStack : HasStack s a => (sk : s q) => a -> F1' q
111 | putStack = write1 (stack sk)
115 | putStackAs : HasStack s a => (sk : s q) => a -> v -> F1 q v
116 | putStackAs = writeAs (stack sk)
120 | putStackAsC : Cast b v => HasStack s a => (sk : s q) => a -> b -> F1 q v
121 | putStackAsC res = putStackAs res . cast
125 | modStackAs : (0 s : _) -> HasStack s a => (sk : s q) => (a -> a) -> v -> F1 q v
126 | modStackAs _ f v = getStack >>= \x => putStackAs (f x) v
130 | pushStack : HasStack s (SnocList a) => (sk : s q) => a -> F1' q
131 | pushStack = push1 (stack sk)
135 | pushStackAs : HasStack s (SnocList a) => (sk : s q) => a -> v -> F1 q v
136 | pushStackAs v res = pushStack v >> pure res
141 | countdown : Ref q Nat -> (ifSucc, ifZero : a) -> F1 q a
142 | countdown ref s z t =
143 | let S k # t := read1 ref t | Z # t => z # t
144 | in writeAs ref k s t
149 | countdownAct : Ref q Nat -> (ifSucc, ifZero : F1 q a) -> F1 q a
150 | countdownAct ref s z t =
151 | let S k # t := read1 ref t | Z # t => z t
152 | _ # t := write1 ref k t
159 | parameters {auto sk : s q}
160 | {auto pos : HasStringLits s}
165 | getStr : F1 q String
167 | sv <- replace1 (strings sk) [<]
173 | pushStr' : String -> F1' q
174 | pushStr' str = push1 (strings sk) str
179 | pushStr : Cast t (Index r) => t -> String -> F1 q (Index r)
180 | pushStr res str = T1.do
181 | push1 (strings sk) str
187 | pushChar : Cast t (Index r) => t -> Char -> F1 q (Index r)
188 | pushChar res = pushStr res . singleton
193 | pushBits32 : Cast t (Index r) => t -> Bits32 -> F1 q (Index r)
194 | pushBits32 res = pushChar res . cast
200 | parameters {auto sk : s q}
201 | {auto hb : HasBytes s}
206 | startPos : F1 q BytePos
208 | o <- read1 (offset sk)
209 | r <- read1 (relpos sk)
210 | pure (BP $
cast (cast o + r))
214 | endPos : F1 q BytePos
217 | l <- read1 (len sk)
222 | bounds : F1 q ByteBounds
225 | l <- read1 (len sk)
226 | pure $
BB s (incLen l s)
230 | bounded : F1 q a -> F1 q (ByteBounded a)
232 | let bs # t := Interfaces.bounds t
238 | bounded' : a -> F1 q (ByteBounded a)
240 | let bs # t := Interfaces.bounds t
251 | pushPosition : F1' q
252 | pushPosition = startPos >>= push1 (positions sk)
256 | popPosition : F1' q
257 | popPosition = pop1 (positions sk)
259 | popAndGetBounds : Nat -> F1 q ByteBounds
260 | popAndGetBounds n =
261 | read1 (positions sk) >>= \case
262 | sb:<b => writeAs (positions sk) sb (BB b $
incLen n b)
269 | closeBounds : F1 q ByteBounds
270 | closeBounds = T1.do
272 | read1 (positions sk) >>= \case
273 | sb:<b => writeAs (positions sk) sb (BB b pe)
280 | parameters {auto hae : HasBBErr s e}
285 | failWith : (sk : s q) => BBErr e -> v -> F1 q v
286 | failWith = writeAs (error sk) . Just
291 | failHere : HasBytes s => (sk : s q) => InnerError e -> v -> F1 q v
292 | failHere x res = T1.do
294 | failWith (B x bs) res
300 | parameters {auto hbp : HasBytes s}
304 | ignore : (s q => F1 q ()) -> (a,Step q r s)
308 | ignore' : (a,Step q r s)
309 | ignore' = ign x (() #)
312 | step : (s q => F1 q (Index r)) -> (a,Step q r s)
316 | step' : Cast t (Index r) => t -> (a,Step q r s)
317 | step' x = step (pure $
cast x)
320 | bytes : (s q => ByteString -> F1 q (Index r)) -> (a,Step q r s)
324 | string : (s q => String -> F1 q (Index r)) -> (a,Step q r s)
325 | string f = goStr x f
334 | opn : (s q => F1 q (Index r)) -> (a, Step q r s)
335 | opn f = step $
pushPosition >> f
339 | opn' : Cast t (Index r) => t -> (a, Step q r s)
340 | opn' v = opn $
pure (cast v)
348 | close : (s q => F1 q (Index r)) -> (a, Step q r s)
349 | close f = step $
popPosition >> f
358 | {auto hap : HasStringLits s}
359 | -> (s q => String -> F1 q (Index r))
361 | closeStr f = close $
getStr >>= f
369 | closeWithBounds : (s q => ByteBounds -> F1 q (Index r)) -> (a, Step q r s)
370 | closeWithBounds f = step $
closeBounds >>= f
379 | {auto hap : HasStringLits s}
380 | -> (s q => ByteBounded String -> F1 q (Index r))
382 | closeBoundedStr f = closeWithBounds $
\bs => getStr >>= \s => f (B s bs)
384 | parameters {auto hbp : HasBytes s}
393 | (display : a -> String)
394 | -> (act : a -> Step1 q r s)
396 | -> Maybe (RExp True, Step q r s)
397 | val display act v =
399 | in case unpack (display v) of
400 | cs@(_::_) => Just $
step (chars cs) (f %search)
407 | (displays : a -> List String)
408 | -> (act : a -> Step1 q r s)
410 | -> List (RExp True, Step q r s)
411 | valN displays act v =
413 | in mapMaybe (exp f . unpack) (displays v)
415 | exp : Step1 q r s -> List Char -> Maybe (RExp True, Step q r s)
416 | exp f cs@(_::_)= Just $
step (chars cs) (f %search)
423 | (display : a -> String)
424 | -> (field : s q -> Ref q a)
427 | -> Maybe (RExp True, Step q r s)
428 | writeVal display field res =
429 | val display (\v,x => writeAs (field x) v res)
435 | (displays : a -> List String)
436 | -> (field : s q -> Ref q a)
439 | -> List (RExp True, Step q r s)
440 | writeValN displays field res =
441 | valN displays (\v,x => writeAs (field x) v res)
449 | (display : a -> String)
450 | -> (act : a -> Step1 q r s)
452 | -> List (RExp True, Step q r s)
453 | vals display = mapMaybe . val display
462 | (displays : a -> List String)
463 | -> (act : a -> Step1 q r s)
465 | -> List (RExp True, Step q r s)
466 | valsN displays act vs = vs >>= valN displays act
472 | (display : a -> String)
473 | -> (field : s q -> Ref q a)
476 | -> List (RExp True, Step q r s)
477 | writeVals display field = mapMaybe . writeVal display field
483 | (displays : a -> List String)
484 | -> (field : s q -> Ref q a)
487 | -> List (RExp True, Step q r s)
488 | writeValsN displays field res vs = vs >>= writeValN displays field res
491 | jsonSpace : RExp True
492 | jsonSpace = oneof [' ','\t','\n','\r']
495 | jsonSpaces : RExp True
496 | jsonSpaces = plus jsonSpace
499 | jsonSpaced : HasBytes s => Steps q r s -> Steps q r s
500 | jsonSpaced xs = ignore' jsonSpaces :: xs
506 | parameters {auto he : HasBBErr s e}
507 | {auto pos : HasBytes s}
510 | raise : InnerError e -> Nat -> s q => v -> F1 q v
511 | raise err n res = T1.do
513 | failWith (B err $
BB ps (incLen n ps)) res
516 | unexpected : List String -> s q -> F1 q (BBErr e)
517 | unexpected strs sk =
518 | read1 (error sk) >>= \case
524 | BS 0 _ => pure (B EOI bb)
527 | s := String.singleton (cast b)
528 | in case isAscii b of
529 | True => pure (B (Expected strs s) bb)
530 | False => pure (B (InvalidByte b) bb)
531 | _ => pure (B (Expected strs (toString bs)) bb)
534 | unclosed : String -> s q -> F1 q (BBErr e)
535 | unclosed str sk = T1.do
536 | bnds <- popAndGetBounds (length str)
537 | pure $
B (Unclosed str) bnds
542 | unclosedIfEOI : String -> List String -> s q -> F1 q (BBErr e)
543 | unclosedIfEOI s ss sk =
545 | BS 0 _ => unclosed s sk
546 | bs => unexpected ss sk
552 | unclosedIfNLorEOI : String -> List String -> s q -> F1 q (BBErr e)
553 | unclosedIfNLorEOI s ss sk =
555 | BS 0 _ => unclosed s sk
556 | bs => if elem 0x0a bs then unclosed s sk else unexpected ss sk
561 | -> List (Entry n $
s q -> F1 q (BBErr e))
562 | -> Arr32 n (s q -> F1 q (BBErr e))
563 | errs = arr32 n (unexpected [])
577 | noChunk : s -> F1 q (Maybe a)
578 | noChunk _ t = (Nothing # t)
583 | snocChunk : HasStack s (SnocList a) => s q -> F1 q (Maybe $
List a)
584 | snocChunk sk = T1.do
585 | ss <- replace1 (stack sk) [<]
586 | pure (maybeList ss)