0 | module Text.ILex.Dependent.Runner
  1 |
  2 | import Data.Buffer
  3 | import public Text.ParseError
  4 | import public Text.FC
  5 | import public Text.ILex.Dependent.Parser
  6 |
  7 | %default total
  8 |
  9 | ||| Tries to parse a byte vector into a value.
 10 | export
 11 | run : {n : _} -> IParser e a -> IBuffer n -> Either e a
 12 |
 13 | ||| Like `run` but processes a UTF-8 string instead.
 14 | export %inline
 15 | runString : IParser e a -> String -> Either e a
 16 | runString l s = run l (fromString s)
 17 |
 18 | ||| Like `run` but processes a `ByteString` instead.
 19 | export %inline
 20 | runBytes : IParser e a -> ByteString -> Either e a
 21 | runBytes l (BS s bv) = run l (toIBuffer bv)
 22 |
 23 | ||| Like `run` but processes a `ByteString` instead.
 24 | export
 25 | runList : IParser e a -> List ByteString -> Either e a
 26 |
 27 | ||| Like `run` but fails with a proper parse error
 28 | ||| including error bounds and highlighting of
 29 | ||| the section where an error occurred.
 30 | export %inline
 31 | parse :
 32 |      {n : _}
 33 |   -> IParser (BBErr e) a
 34 |   -> Origin
 35 |   -> IBuffer n
 36 |   -> Either (ParseError e) a
 37 | parse l o buf = mapFst (toParseError o (toString buf 0 n)) (run l buf)
 38 |
 39 | ||| Like `parse` but processes a UTF-8 string instead.
 40 | export %inline
 41 | parseString :
 42 |      IParser (BBErr e) a
 43 |   -> Origin
 44 |   -> String
 45 |   -> Either (ParseError e) a
 46 | parseString l o s = parse l o (fromString s)
 47 |
 48 | ||| Like `parse` but processes a `ByteString` instead.
 49 | export %inline
 50 | parseBytes :
 51 |      IParser (BBErr e) a
 52 |   -> Origin
 53 |   -> ByteString
 54 |   -> Either (ParseError e) a
 55 | parseBytes l o bs = mapFst (toParseError o (toString bs)) (runBytes l bs)
 56 |
 57 | --------------------------------------------------------------------------------
 58 | -- Lexer run loop
 59 | --------------------------------------------------------------------------------
 60 |
 61 | ||| Lexing state.
 62 | |||
 63 | ||| This encapsulates the current state as well as
 64 | ||| the remainder of the previous chunk that marks
 65 | ||| the beginning of the current token.
 66 | public export
 67 | record LexState (p : IParser e a) where
 68 |   constructor LST
 69 |   {0 sts      : Nat}
 70 |   {0 ltpe     : Type}
 71 |   lexer       : p.lexer ltpe
 72 |   stack       : In ltpe
 73 |   dfa         : PMachine sts p ltpe
 74 |   cur         : PByteTrans sts p ltpe
 75 |   tok         : PStep p ltpe
 76 |
 77 | export
 78 | init : {n : _} -> IBuffer n -> (p : IParser e a) -> LexState p
 79 | init buf p =
 80 |  let sk := I (Bytes.init buf) (first n) (first n) p.istack
 81 |      L _ dfa := at @{p.en} p.lex  p.init
 82 |   in LST p.init sk dfa (dfa `at` 0) Err
 83 |
 84 | ||| Result of a partial lexing step: In such a step, we lex
 85 | ||| till the end of a chunk of bytes, allowing for a remainder of
 86 | ||| bytes that could not yet be identified as a tokens.
 87 | public export
 88 | 0 LoopRes : (p : IParser e a) -> Type
 89 | LoopRes p = Either e (LexState p)
 90 |
 91 | export
 92 | lastStep : (p : IParser e a) -> LexState p -> Either e a
 93 | lastStep p (LST st sk _ _ tok) =
 94 |   case tok of
 95 |     Err => case getBytes sk of
 96 |       BS 0 _ => eoi p st sk
 97 |       _      => failI p st sk
 98 |     Run f => let E st2 k2 := f sk in eoi p st2 (toFinalPos' sk.bytes k2)
 99 |     Ign   => eoi p st (toFinalPos sk)
100 |
101 | parameters {0 e,a : Type}
102 |            {0 n   : Nat}
103 |            (p     : IParser e a)
104 |            (bs    : Bytes n)
105 |            (buf   : IBuffer n)
106 |
107 |   %inline
108 |   stp :
109 |        (0 st   : p.lexer t)
110 |     -> (sk     : t)
111 |     -> (f      : In t -> x)
112 |     -> (from   : LTENat n)
113 |     -> (0 till : Nat)
114 |     -> {auto pix : Ix till n}
115 |     -> x
116 |   stp st sk f from till = f (I bs from (lteNat till) sk)
117 |
118 |   step :
119 |        (st          : p.lexer t)
120 |     -> (sk          : t)
121 |     -> (dfa         : PMachine k p t)
122 |     -> (cur         : PByteTrans k p t)
123 |     -> (from        : LTENat n)
124 |     -> (pos         : Nat)
125 |     -> {auto posIx  : Ix pos n}
126 |     -> LoopRes p
127 |
128 |   succ :
129 |        (st          : p.lexer t)
130 |     -> (sk          : t)
131 |     -> (dfa         : PMachine k p t)
132 |     -> (cur         : PByteTrans k p t)
133 |     -> (last        : PRun p t)
134 |     -> (from        : LTENat n)
135 |     -> (pos         : Nat)
136 |     -> {auto posIx  : Ix pos n}
137 |     -> LoopRes p
138 |
139 |   igno :
140 |        (st          : p.lexer t)
141 |     -> (sk          : t)
142 |     -> (dfa         : PMachine k p t)
143 |     -> (cur         : PByteTrans k p t)
144 |     -> (from        : LTENat n)
145 |     -> (pos         : Nat)
146 |     -> {auto posIx  : Ix pos n}
147 |     -> LoopRes p
148 |
149 |   loop :
150 |        (st : p.lexer t)
151 |     -> (sk : t)
152 |     -> (pos : Nat)
153 |     -> {auto x : Ix pos n}
154 |     -> LoopRes p
155 |   loop st sk 0     =
156 |    let cs := I bs (lteNat 0) (lteNat 0) sk
157 |        L _ dfa := at @{p.en} p.lex st
158 |     in Right (LST st cs dfa (dfa `at` 0) Err)
159 |   loop st sk (S k) =
160 |    let from    := lteNat $ S k
161 |        L _ dfa := at @{p.en} p.lex st
162 |        cur     := dfa `at` 0
163 |     in case cur `atByte` (buf `ix` k) of
164 |          Done f       => let E s2 k2 := stp st sk f from k in loop s2 k2 k
165 |          Ignore       => loop st sk k
166 |          Move   nxt f => succ st sk dfa (dfa `at` nxt) f from k
167 |          MoveI  nxt   => igno st sk dfa (dfa `at` nxt)   from k
168 |          MoveE  nxt   => step st sk dfa (dfa `at` nxt)   from k
169 |          _            => stp st sk (failI p st) from k
170 |
171 |   succ st sk dfa cur f from 0     =
172 |    let cs := I bs from (lteNat 0) sk
173 |     in Right (LST st cs dfa cur (Run f))
174 |   succ st sk dfa cur f from (S k) =
175 |    let byte := buf `ix` k
176 |     in case cur `atByte` byte of
177 |          Keep         => succ st sk dfa cur f from k
178 |          Done f       => let E s2 k2 := stp st sk f from k in loop s2 k2 k
179 |          Ignore       => loop st sk k
180 |          Move   nxt f => succ st sk dfa (dfa `at` nxt) f from k
181 |          MoveI  nxt   => igno st sk dfa (dfa `at` nxt)   from k
182 |          MoveE  nxt   => step st sk dfa (dfa `at` nxt)   from k
183 |          Bottom       => let E s2 k2 := stp st sk f from (S k) in loop s2 k2 (S k)
184 |
185 |   igno st sk dfa cur from 0     =
186 |    let cs := I bs from (lteNat 0) sk
187 |     in Right (LST st cs dfa cur Ign)
188 |   igno st sk dfa cur from (S k) =
189 |    let byte := buf `ix` k
190 |     in case cur `atByte` byte of
191 |          Keep         => igno st sk dfa cur   from k
192 |          Done f       => let E s2 k2 := stp st sk f from k in loop s2 k2 k
193 |          Ignore       => loop st sk k
194 |          Move   nxt f => succ st sk dfa (dfa `at` nxt) f from k
195 |          MoveI  nxt   => igno st sk dfa (dfa `at` nxt)   from k
196 |          MoveE  nxt   => step st sk dfa (dfa `at` nxt)   from k
197 |          Bottom       => loop st sk (S k)
198 |
199 |   step st sk dfa cur from 0     =
200 |    let cs := I bs from (lteNat 0) sk
201 |     in Right (LST st cs dfa cur Err)
202 |   step st sk dfa cur from (S k) =
203 |    let byte := buf `ix` k
204 |     in case cur `atByte` byte of
205 |          Keep         => step st sk dfa cur from k
206 |          Done f       => let E s2 k2 := stp st sk f from k in loop s2 k2 k
207 |          Ignore       => loop st sk k
208 |          Move   nxt f => succ st sk dfa (dfa `at` nxt) f from k
209 |          MoveI  nxt   => igno st sk dfa (dfa `at` nxt)   from k
210 |          MoveE  nxt   => step st sk dfa (dfa `at` nxt)   from k
211 |          Bottom       => stp st sk (failI p st) from k
212 |
213 | export partial
214 | stepState : {n : _} -> IBuffer n -> (p : IParser e a) -> LexState p -> LoopRes p
215 | stepState {n = 0}   buf p lst = Right lst
216 | stepState {n = S k} buf p (LST st sk dfa cr tok) =
217 |  let bs   := getBytes sk
218 |      BP o := startPos sk
219 |      rf   := first $ S k
220 |      bts  := newBytes o bs buf
221 |      byte := at buf 0
222 |   in case cr `atByte` byte of
223 |        Keep         => case tok of
224 |          Run f  => succ p bts buf st sk.val dfa cr f rf k
225 |          Ign    => igno p bts buf st sk.val dfa cr   rf k
226 |          Err    => step p bts buf st sk.val dfa cr   rf k
227 |        Done f       =>
228 |         let E s2 k2 := stp p bts buf st sk.val f rf k
229 |          in loop p bts buf s2 k2 k
230 |        Ignore       => loop p bts buf st sk.val k
231 |        Move   nxt f => succ p bts buf st sk.val dfa (dfa `at` nxt) f rf k
232 |        MoveI  nxt   => igno p bts buf st sk.val dfa (dfa `at` nxt)   rf k
233 |        MoveE  nxt   => step p bts buf st sk.val dfa (dfa `at` nxt)   rf k
234 |        Bottom     => case tok of
235 |          Run f  =>
236 |           let E s2 k2 := stp p bts buf st sk.val f rf (S k)
237 |               bts2            := newBytes (o+bs.size) empty buf
238 |            in loop p bts2 buf s2 k2 (S k)
239 |          Ign    =>
240 |           let bts2            := newBytes (o+bs.size) empty buf
241 |            in loop p bts2 buf st sk.val (S k)
242 |          Err => stp p bts buf st sk.val (failI p st) rf k
243 |
244 | run p buf =
245 |   let lst        := Runner.init buf p
246 |       Right lst2 := stepState buf p lst | Left x => Left x
247 |    in lastStep p lst2
248 |
249 | loopAll : (p : IParser e a) -> LexState p -> List ByteString -> Either e a
250 | loopAll p lst []              = lastStep p lst
251 | loopAll p lst (BS n bv :: xs) =
252 |   case stepState (toIBuffer bv) p lst of
253 |     Left x     => Left x
254 |     Right lst2 => loopAll p lst2 xs
255 |
256 | runList p bs = let lst := Runner.init empty p in loopAll p lst bs
257 |