0 | module Text.ILex.Dependent.Stack
  1 |
  2 | import public Text.ByteBounds
  3 | import public Text.ILex.Dependent.Parser
  4 |
  5 | %default total
  6 |
  7 | --------------------------------------------------------------------------------
  8 | -- List Stack
  9 | --------------------------------------------------------------------------------
 10 |
 11 | public export
 12 | 0 Val : Bool -> (r,a : Type) -> Type
 13 | Val False r a = r
 14 | Val True  r a = a
 15 |
 16 | public export
 17 | data Stack : Bool -> (r, a : Type) -> Type where
 18 |   Lin  : Stack False r a
 19 |   (:<) : Stack b r a -> Val b r a -> Stack True r a
 20 |
 21 | export
 22 | root : Stack True r a -> r
 23 | root [<v]           = v
 24 | root (s@(sv:<_):<_) = root s
 25 |
 26 | acc : List a -> Stack b r a -> Val b r a -> (r, List a)
 27 | acc as [<]     c = (c, as)
 28 | acc as (sv:<v) c = acc (c::as) sv v
 29 |
 30 | export %inline
 31 | collapse : Stack True r a -> (r,List a)
 32 | collapse (sv:<v) = acc [] sv v
 33 |
 34 | export
 35 | collapseString : Stack True r String -> (r,String)
 36 | collapseString s =
 37 |   case collapse s of
 38 |     (r,[])  => (r,"")
 39 |     (r,[x]) => (r,x)
 40 |     (r,xs)  => (r,fastConcat xs)
 41 |
 42 | export %inline
 43 | app :
 44 |      {0 l      : Type -> Type}
 45 |   -> (x        : RExp True)
 46 |   -> (lex      : l (Stack True r a))
 47 |   -> (va       : a)
 48 |   -> (RExp True, DStep l (Stack True r a))
 49 | app x lex va = (x, Run $ \i => E lex (i.val :< va))
 50 |
 51 | export %inline
 52 | appString :
 53 |      {0 l      : Type -> Type}
 54 |   -> (x        : RExp True)
 55 |   -> (lex      : l (Stack True r a))
 56 |   -> (va       : String -> a)
 57 |   -> (RExp True, DStep l (Stack True r a))
 58 | appString x lex va = (x, Run $ \i => E lex (i.val :< va (getString i)))
 59 |
 60 | export %inline
 61 | appBytes :
 62 |      {0 l      : Type -> Type}
 63 |   -> (x        : RExp True)
 64 |   -> (lex      : l (Stack True r a))
 65 |   -> (va       : ByteString -> a)
 66 |   -> (RExp True, DStep l (Stack True r a))
 67 | appBytes x lex va = (x, Run $ \i => E lex (i.val :< va (getBytes i)))
 68 |
 69 | --------------------------------------------------------------------------------
 70 | -- Stack Root plus Bounds
 71 | --------------------------------------------------------------------------------
 72 |
 73 | public export
 74 | record Root (p : Type -> Type) where
 75 |   constructor R
 76 |   {auto prf : p ptpe}
 77 |   parent    : ptpe
 78 |   bounds    : ByteBounds
 79 |
 80 | export %inline
 81 | opn :
 82 |      {0 p,l    : Type -> Type}
 83 |   -> {auto prf : p t}
 84 |   -> (x        : RExp True)
 85 |   -> (lex      : l (Stack True (Root p) a))
 86 |   -> (RExp True, DStep l t)
 87 | opn x lex = (x, Run $ \i => E lex [< R i.val $ bounds i])
 88 |
 89 | export %inline
 90 | close :
 91 |      {0 p      : Type -> Type}
 92 |   -> (x        : RExp True)
 93 |   -> (cls      : {0 t : _} -> p t => List a -> t -> r)
 94 |   -> (RExp True, Stp (Stack True (Root p) a) r)
 95 | close x cls = (x, Run $ \i => let (R v _, vs) := collapse i.val in cls vs v)
 96 |
 97 | export %inline
 98 | closeStr :
 99 |      {0 p      : Type -> Type}
100 |   -> (x        : RExp True)
101 |   -> (cls      : {0 t : _} -> p t => String -> t -> r)
102 |   -> (RExp True, Stp (Stack True (Root p) String) r)
103 | closeStr x cls =
104 |   (x, Run $ \i => let (R v _, vs) := collapseString i.val in cls vs v)
105 |