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