0 | module Text.ILex.Parser
  1 |
  2 | import Data.Buffer
  3 | import Derive.Prelude
  4 | import Syntax.T1
  5 | import public Data.Linear.Ref1
  6 | import public Data.Prim.Bits32
  7 | import public Text.ByteBounds
  8 | import public Text.ILex.LTENat
  9 | import public Text.ILex.Lexer
 10 | import public Text.ILex.RExp
 11 | import public Text.ParseError
 12 |
 13 | %default total
 14 | %language ElabReflection
 15 |
 16 | --------------------------------------------------------------------------------
 17 | -- FFI
 18 | --------------------------------------------------------------------------------
 19 |
 20 | %foreign "scheme:(lambda (x i) (make-vector x i))"
 21 |          "javascript:lambda:(i,x,w) => Array(i).fill(x)"
 22 | prim__newMachine : Bits32 -> AnyPtr -> PrimIO AnyPtr
 23 |
 24 | %foreign "scheme:(lambda (x i) (vector-ref x i))"
 25 |          "javascript:lambda:(x,bi) => x[bi]"
 26 | prim__machineGet : AnyPtr -> Bits32 -> AnyPtr
 27 |
 28 | %foreign "scheme:(lambda (x i w) (vector-set! x i w))"
 29 |          "javascript:lambda:(x,i,w) => {x[i] = w}"
 30 | prim__machineSet : AnyPtr -> Bits32 -> (val : AnyPtr) -> PrimIO ()
 31 |
 32 | export
 33 | record Arr32 (n : Bits32) (a : Type) where
 34 |   constructor A32
 35 |   ptr : AnyPtr
 36 |
 37 | export %inline
 38 | at : Arr32 n a -> Index n -> a
 39 | at (A32 p) x = believe_me $ prim__machineGet p x.val
 40 |
 41 | public export
 42 | record Entry (n : Bits32) (a : Type) where
 43 |   constructor E
 44 |   index : Index n
 45 |   value : a
 46 |
 47 | export %inline
 48 | entry : Cast t (Index n) => t -> a -> Entry n a
 49 | entry x v = E (cast x) v
 50 |
 51 | export
 52 | arr32 : (n : Bits32) -> (dflt : a) -> List (Entry n a) -> Arr32 n a
 53 | arr32 n dflt es =
 54 |   run1 $ \t =>
 55 |    let p # t := ffi (prim__newMachine n (believe_me dflt)) t
 56 |     in fill es p t
 57 |
 58 |   where
 59 |     fill : List (Entry n a) -> AnyPtr -> F1 x (Arr32 n a)
 60 |     fill []             p t = A32 p # t
 61 |     fill (E x d :: xs) p t =
 62 |      let _ # t := ffi (prim__machineSet p x.val (believe_me d)) t
 63 |       in fill xs p t
 64 |
 65 | public export
 66 | 0 Lex1 : (q : Type) -> (r : Bits32) -> (s : Type -> Type) -> Type
 67 | Lex1 q r s = Arr32 r (DFA1 q r s)
 68 |
 69 | --------------------------------------------------------------------------------
 70 | -- HasBytes Interface
 71 | --------------------------------------------------------------------------------
 72 |
 73 | ||| An interface for mutable parser stacks `s` that facilitates
 74 | ||| parsing string tokens containing escape sequences.
 75 | public export
 76 | interface HasBytes (0 s : Type -> Type) where
 77 |   constructor MkHB
 78 |   copy :
 79 |        (size, offset : Nat)
 80 |     -> ByteString
 81 |     -> IBuffer size
 82 |     -> (from, till : Ref q (LTENat size))
 83 |     -> s q
 84 |     -> s q
 85 |
 86 |   bufSize   : s q -> Nat
 87 |
 88 |   ||| Remainder of the previous bytestring that should
 89 |   ||| be used as the beginning of the current token.
 90 |   prev      : s q -> ByteString
 91 |
 92 |   ||| The byte vector currently being processed.
 93 |   cur       : (v : s q) -> IBuffer (bufSize v)
 94 |
 95 |   ||| Absolute position of the first byte of `prev`.
 96 |   prevOffset    : s q -> Nat
 97 |
 98 |   ||| Absolute position of the first byte of `cur`
 99 |   ||| (this equals `prevOffset + prev.size` but should be
100 |   ||| provided as a separate field for reasons of efficiency)
101 |   curOffset    : s q -> Nat
102 |
103 |   ||| Lower relative token bound (lower position in `cur`)
104 |   from         : (v : s q) -> Ref q (LTENat (bufSize v))
105 |
106 |   ||| Next relative token bound (upser position + 1 in `cur`)
107 |   till         : (v : s q) -> Ref q (LTENat (bufSize v))
108 |
109 |   ||| Stack of positions used to keep track of the positions of
110 |   ||| opening parentheses and brackets.
111 |   positions : s q -> Ref q (SnocList BytePos)
112 |
113 | ||| Returns the current substring of the byte vector
114 | ||| (corresponding to the position and length of the current
115 | ||| token).
116 | |||
117 | ||| The remainder of the previous bytestring is prefixed in case
118 | ||| we are currently at position zero.
119 | export %inline
120 | getBytes : HasBytes s => (sk : s q) => F1 q ByteString
121 | getBytes t =
122 |   let rf # t := read1 (from sk) t
123 |       rt # t := read1 (till sk) t
124 |    in case rf.val of
125 |         0 => (prev sk <+> bytesFromTill (cur sk) rf rt) # t
126 |         _ => bytesFromTill (cur sk) rf rt # t
127 |
128 | export %inline
129 | getString : HasBytes s => (sk : s q) => F1 q String
130 | getString t =
131 |   let rf # t := read1 (from sk) t
132 |       rt # t := read1 (till sk) t
133 |    in case rf.val of
134 |         0 => (toString $ prev sk <+> bytesFromTill (cur sk) rf rt) # t
135 |         _ => stringFromTill (cur sk) rf rt # t
136 |
137 | export %inline
138 | toFinalPos : HasBytes s => (sk : s q) => F1' q
139 | toFinalPos t =
140 |  let _ # t := write1 (from sk) (last $ bufSize sk) t
141 |   in write1 (till sk) (last $ bufSize sk) t
142 |
143 | ||| A parser is a system of automata, where each
144 | ||| lexicographic token determines the next automaton
145 | ||| state plus lexer to use.
146 | public export
147 | record P1 (q,e : Type) (a : Type) where
148 |   constructor P
149 |   {states    : Bits32}
150 |   {0 state   : Type -> Type}
151 |   init       : Index states
152 |   stck       : (size : Nat) -> IBuffer size -> F1 q (state q)
153 |   lex        : Lex1 q states state
154 |   chunk      : state q -> F1 q (Maybe a)
155 |   err        : Arr32 states (state q -> F1 q e)
156 |   eoi        : Index states -> state q -> F1 q (Either e a)
157 |   {auto hasb : HasBytes state}
158 |
159 | public export
160 | 0 PST : (p : P1 q e a) -> Type
161 | PST p = p.state q
162 |
163 | public export
164 | 0 PIx : (p : P1 q e a) -> Type
165 | PIx p = Index p.states
166 |
167 | public export
168 | 0 PStep : (p : P1 q e a) -> Type
169 | PStep p = Step q p.states p.state
170 |
171 | public export
172 | 0 PRun : (p : P1 q e a) -> Type
173 | PRun p = Run1 q p.states p.state
174 |
175 | ||| An array of arrays describing a lexer's state machine.
176 | public export
177 | 0 PByteStep : Nat -> (p : P1 q e a) -> Type
178 | PByteStep n p = IArray 256 (Transition n $ Run1 q p.states p.state)
179 |
180 | ||| An array of arrays describing a lexer's state machine.
181 | public export
182 | 0 PStepper : Nat -> (p : P1 q e a) -> Type
183 | PStepper n p = IArray (S n) (PByteStep n p)
184 |
185 | export
186 | arrFail :
187 |      (0 s : Type -> Type)
188 |   -> Arr32 r (s q -> F1 q e)
189 |   -> Index r
190 |   -> s q
191 |   -> F1 q (Either e x)
192 | arrFail s arr ix st t =
193 |  let eo      := arr `at` ix
194 |      err # t := eo st t
195 |   in Left err # t
196 |
197 | export %inline
198 | fail : (p : P1 q e a) -> PIx p -> PST p -> F1 q (Either e x)
199 | fail p = arrFail p.state p.err
200 |
201 | export %inline
202 | failFun1 : (p : P1 q e a) -> PIx p -> Fun1 q p.state (Either e x)
203 | failFun1 p st (E sk t) = arrFail p.state p.err st sk t
204 |
205 | public export
206 | 0 Parser1 : (e : Type) -> (a : Type) -> Type
207 | Parser1 e a = {0 q : _} -> P1 q e a
208 |
209 | export %inline
210 | lex1 : {r : _} -> List (Entry r (DFA1 q r s)) -> Lex1 q r s
211 | lex1 es = arr32 r (dfa []) es
212 |