0 | module Text.ILex.Parser
2 | import Derive.Prelude
5 | import public Data.Prim.Bits32
6 | import public Data.ByteString
7 | import public Data.Linear.Ref1
8 | import public Text.ByteBounds
9 | import public Text.ParseError
10 | import public Text.ILex.RExp
11 | import public Text.ILex.Lexer
14 | %language ElabReflection
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
24 | %foreign "scheme:(lambda (x i) (vector-ref x i))"
25 | "javascript:lambda:(x,bi) => x[bi]"
26 | prim__machineGet : AnyPtr -> Bits32 -> AnyPtr
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 ()
33 | record Arr32 (n : Bits32) (a : Type) where
38 | at : Arr32 n a -> Index n -> a
39 | at (A32 p) x = believe_me $
prim__machineGet p x.val
42 | record Entry (n : Bits32) (a : Type) where
48 | entry : Cast t (Index n) => t -> a -> Entry n a
49 | entry x v = E (cast x) v
52 | arr32 : (n : Bits32) -> (dflt : a) -> List (Entry n a) -> Arr32 n a
55 | let p # t := ffi (prim__newMachine n (believe_me dflt)) t
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
66 | 0 Lex1 : (q : Type) -> (r : Bits32) -> (s : Type -> Type) -> Type
67 | Lex1 q r s = Arr32 r (DFA q r s)
74 | record LTENat (n : Nat) where
77 | {auto 0 prf : LTE val n}
80 | lteNat : (0 m : Nat) -> Ix m n => LTENat n
81 | lteNat _ @{x} = LN (ixToNat x) @{ixLTE x}
84 | first : (0 n : Nat) -> LTENat n
88 | last : (n : Nat) -> LTENat n
92 | bytesFromTill : IBuffer n -> (from, till : LTENat n) -> ByteString
93 | bytesFromTill buf (LN from @{lt1}) (LN till @{lt2}) =
94 | BS (till `minus` from) (BV buf from $
plusMinusBothLTE _ _ lt1 lt2)
97 | stringFromTill : IBuffer n -> (from, till : LTENat n) -> String
98 | stringFromTill buf (LN from @{lt1}) (LN till @{lt2}) =
99 | toString buf from (till `minus` from) @{plusMinusBothLTE _ _ lt1 lt2}
104 | interface HasBytes (0 s : Type -> Type) where
107 | (size, offset : Nat)
110 | -> (from, till : Ref q (LTENat size))
114 | bufSize : s q -> Nat
118 | prev : s q -> ByteString
121 | cur : (v : s q) -> IBuffer (bufSize v)
124 | prevOffset : s q -> Nat
129 | curOffset : s q -> Nat
132 | from : (v : s q) -> Ref q (LTENat (bufSize v))
135 | till : (v : s q) -> Ref q (LTENat (bufSize v))
139 | positions : s q -> Ref q (SnocList BytePos)
148 | getBytes : HasBytes s => (sk : s q) => F1 q ByteString
150 | let rf # t := read1 (from sk) t
151 | rt # t := read1 (till sk) t
153 | 0 => (prev sk <+> bytesFromTill (cur sk) rf rt) # t
154 | _ => bytesFromTill (cur sk) rf rt # t
157 | getString : HasBytes s => (sk : s q) => F1 q String
159 | let rf # t := read1 (from sk) t
160 | rt # t := read1 (till sk) t
162 | 0 => (toString $
prev sk <+> bytesFromTill (cur sk) rf rt) # t
163 | _ => stringFromTill (cur sk) rf rt # t
166 | toFinalPos : HasBytes s => (sk : s q) => F1' q
168 | let _ # t := write1 (from sk) (last $
bufSize sk) t
169 | in write1 (till sk) (last $
bufSize sk) t
175 | record P1 (q,e : Type) (a : Type) where
178 | {0 state : Type -> Type}
179 | init : Index states
180 | stck : (size : Nat) -> IBuffer size -> F1 q (state q)
181 | lex : Lex1 q states state
182 | chunk : state q -> F1 q (Maybe a)
183 | err : Arr32 states (state q -> F1 q e)
184 | eoi : Index states -> state q -> F1 q (Either e a)
185 | {auto hasb : HasBytes state}
188 | 0 PST : (p : P1 q e a) -> Type
192 | 0 PIx : (p : P1 q e a) -> Type
193 | PIx p = Index p.states
196 | 0 PStep : (p : P1 q e a) -> Type
197 | PStep p = Step q p.states p.state
200 | 0 PRun : (p : P1 q e a) -> Type
201 | PRun p = Run1 q p.states p.state
205 | 0 PByteStep : Nat -> (p : P1 q e a) -> Type
206 | PByteStep n p = IArray 256 (Transition n q p.states p.state)
210 | 0 PStepper : Nat -> (p : P1 q e a) -> Type
211 | PStepper n p = IArray (S n) (PByteStep n p)
215 | (0 s : Type -> Type)
216 | -> Arr32 r (s q -> F1 q e)
219 | -> F1 q (Either e x)
220 | arrFail s arr ix st t =
221 | let eo := arr `at` ix
226 | fail : (p : P1 q e a) -> PIx p -> PST p -> F1 q (Either e x)
227 | fail p = arrFail p.state p.err
230 | failFun1 : (p : P1 q e a) -> PIx p -> Fun1 q p.state (Either e x)
231 | failFun1 p st (E sk t) = arrFail p.state p.err st sk t
234 | 0 Parser1 : (e : Type) -> (a : Type) -> Type
235 | Parser1 e a = {0 q : _} -> P1 q e a
238 | lex1 : {r : _} -> List (Entry r (DFA q r s)) -> Lex1 q r s
239 | lex1 es = arr32 r (dfa []) es