0 | module Text.ILex.Stack
2 | import Data.Linear.Ref1
5 | import Text.ILex.Derive
6 | import Text.ILex.Interfaces
7 | import Text.ILex.Parser
8 | import Text.ILex.Util
9 | import Text.ParseError
15 | %language ElabReflection
32 | record Stack (e,a : Type) (r : Bits32) (q : Type) where
38 | positions_ : Ref q (SnocList Position)
44 | state_ : Ref q (Index r)
47 | strings_ : Ref q (SnocList String)
50 | error_ : Ref q (Maybe $
BoundedErr e)
53 | bytes_ : Ref q ByteString
55 | %runElab derive "Stack" [FullStack]
59 | init : (0 p : 0 < r) => a -> F1 q (Stack e a r q)
69 | pure (S ln cl ps sk st ss er bs)
75 | %runElab deriveParserState "VSz" "VST" ["VIni", "VErr", "VDone"]
84 | data Tok : (e, a : Type) -> Type where
91 | Const : a -> Tok e a
94 | Txt : (String -> Either e a) -> Tok e a
97 | Bytes : (ByteString -> Either e a) -> Tok e a
100 | const : a -> Tok e a
104 | txt : (String -> a) -> Tok e a
105 | txt f = Txt (Right . f)
108 | bytes : (ByteString -> a) -> Tok e a
109 | bytes f = Bytes (Right . f)
112 | (RExpOf True b, Tok e a)
113 | -> (RExpOf True b, Step q VSz (Stack e (Maybe a) VSz))
116 | Ignore => conv' x VIni
117 | Const v => conv x (\_ => putStackAs (Just v) VDone)
119 | read x $
\s => case f s of
120 | Right v => putStackAs (Just v) VDone
121 | Left e => failHere (Custom e) VErr
123 | conv x $
\s => case f s of
124 | Right v => putStackAs (Just v) VDone
125 | Left e => failHere (Custom e) VErr
128 | (RExpOf True b, Tok e a)
129 | -> Maybe (RExpOf True b, Step q VSz (Stack e (Maybe a) VSz))
130 | ignore (x,Ignore) = Just $
conv' x VDone
133 | valEOI : VST -> Stack e (Maybe a) VSz q -> F1 q (Either (BoundedErr e) a)
135 | if i == VDone || i == VIni
136 | then replace1 sk.stack_ Nothing >>= \case
137 | Just v => pure (Right v)
138 | Nothing => unexpected [] sk >>= pure . Left
139 | else unexpected [] sk >>= pure . Left
142 | 0 PVal1 : (q,e : Type) -> (a : Type) -> Type
143 | PVal1 q e a = P1 q (BoundedErr e) a
147 | value : Maybe a -> TokenMap (Tok e a) -> PVal1 q e a
149 | let iniSteps := E VIni $
dfa (map toStep m)
150 | doneSteps := E VDone $
dfa (mapMaybe ignore m)
151 | states := lex1 [iniSteps, doneSteps]
152 | in P VIni (init mv) states noChunk (errs []) valEOI
158 | parameters {0 r : Bits32}
160 | {auto pos : HasPosition s}
161 | {auto stk : HasStack s (SnocList $
Bounded a)}
165 | lexPushNL : Nat -> a -> F1 q (Index r)
166 | lexPushNL n tok = T1.do
167 | bs <- tokenBounds n
169 | push1 (stack sk) (B tok bs)
173 | lexPush : Nat -> a -> F1 q (Index r)
174 | lexPush n tok = T1.do
175 | bs <- tokenBounds n
176 | push1 (stack sk) (B tok bs)
179 | parameters (x : RExpOf True b)
180 | {auto pos : HasPosition s}
181 | {auto stk : HasStack s (SnocList $
Bounded a)}
182 | {auto 0 lt : 0 < r}
189 | ctok : {n : _} -> (0 prf : ConstSize n x) => a -> (RExpOf True b, Step q r s)
190 | ctok v = go x $
lexPush n v
193 | readTok : HasBytes s => (String -> a) -> (RExpOf True b, Step q r s)
194 | readTok f = goStr x $
\s => lexPush (length s) (f s)
197 | convTok : HasBytes s => (ByteString -> a) -> (RExpOf True b, Step q r s)
198 | convTok f = goBS x $
\bs => lexPush bs.size (f bs)
201 | nltok : HasBytes s => a -> (RExpOf True b, Step q r s)
202 | nltok v = goBS x $
\bs => lexPushNL bs.size v
206 | {auto 0 lt : 0 < r}
207 | -> {auto pos : HasPosition s}
208 | -> {auto stk : HasStack s (SnocList a)}
209 | -> {auto err : HasError s e}
210 | -> {auto bts : HasBytes s}
213 | -> F1 q (Either (BoundedErr e) $
List a)
216 | then getList (stack sk) >>= pure . Right
217 | else unexpected [] sk >>= pure . Left
220 | 0 L1 : (q,e : Type) -> (a : Type) -> Type
221 | L1 q e a = P1 q (BoundedErr e) (List $
Bounded a)
224 | 0 Lexer : (e : Type) -> Type -> Type
225 | Lexer e a = {0 q : Type} -> L1 q e a
230 | -> {auto 0 lt : 0 < r}
231 | -> Steps q r (Stack e (SnocList $
Bounded a) r)
234 | P Ini (init [<]) (lex1 [E Ini $
dfa m]) snocChunk (errs []) lexEOI