0 | module Text.ILex.Parser
2 | import Derive.Prelude
4 | import public Data.Prim.Bits32
5 | import public Data.ByteString
6 | import public Data.Linear.Ref1
7 | import public Text.Bounds
8 | import public Text.ParseError
9 | import public Text.ILex.RExp
10 | import public Text.ILex.Lexer
13 | %language ElabReflection
19 | %foreign "scheme:(lambda (x i) (make-vector x i))"
20 | "javascript:lambda:(i,x,w) => Array(i).fill(x)"
21 | prim__newMachine : Bits32 -> AnyPtr -> PrimIO AnyPtr
23 | %foreign "scheme:(lambda (x i) (vector-ref x i))"
24 | "javascript:lambda:(x,bi) => x[bi]"
25 | prim__machineGet : AnyPtr -> Bits32 -> AnyPtr
27 | %foreign "scheme:(lambda (x i w) (vector-set! x i w))"
28 | "javascript:lambda:(x,i,w) => {x[i] = w}"
29 | prim__machineSet : AnyPtr -> Bits32 -> (val : AnyPtr) -> PrimIO ()
35 | interface HasBytes (0 s : Type -> Type) where
37 | bytes : s q -> Ref q ByteString
40 | record Arr32 (n : Bits32) (a : Type) where
45 | at : Arr32 n a -> Index n -> a
46 | at (A32 p) x = believe_me $
prim__machineGet p x.val
49 | record Entry (n : Bits32) (a : Type) where
55 | entry : Cast t (Index n) => t -> a -> Entry n a
56 | entry x v = E (cast x) v
59 | arr32 : (n : Bits32) -> (dflt : a) -> List (Entry n a) -> Arr32 n a
62 | let p # t := ffi (prim__newMachine n (believe_me dflt)) t
66 | fill : List (Entry n a) -> AnyPtr -> F1 x (Arr32 n a)
67 | fill [] p t = A32 p # t
68 | fill (E x d :: xs) p t =
69 | let _ # t := ffi (prim__machineSet p x.val (believe_me d)) t
73 | 0 Lex1 : (q : Type) -> (r : Bits32) -> (s : Type -> Type) -> Type
74 | Lex1 q r s = Arr32 r (DFA q r s)
80 | record P1 (q,e : Type) (a : Type) where
83 | {0 state : Type -> Type}
85 | stck : F1 q (state q)
86 | lex : Lex1 q states state
87 | chunk : state q -> F1 q (Maybe a)
88 | err : Arr32 states (state q -> F1 q e)
89 | eoi : Index states -> state q -> F1 q (Either e a)
90 | {auto hasb : HasBytes state}
93 | 0 PST : (p : P1 q e a) -> Type
97 | 0 PIx : (p : P1 q e a) -> Type
98 | PIx p = Index p.states
101 | 0 PStep : (p : P1 q e a) -> Type
102 | PStep p = Step1 q p.states p.state
106 | 0 PByteStep : Nat -> (p : P1 q e a) -> Type
107 | PByteStep n p = IArray 256 (Transition n q p.states p.state)
111 | 0 PStepper : Nat -> (p : P1 q e a) -> Type
112 | PStepper n p = IArray (S n) (PByteStep n p)
116 | (0 s : Type -> Type)
117 | -> Arr32 r (s q -> F1 q e)
120 | -> F1 q (Either e x)
121 | arrFail s arr ix st t =
122 | let eo := arr `at` ix
127 | fail : (p : P1 q e a) -> PIx p -> PST p -> F1 q (Either e x)
128 | fail p = arrFail p.state $
p.err
131 | lastStep : (p : P1 q e a) -> PStep p -> PIx p -> PST p -> F1 q (Either e a)
132 | lastStep p f st stck t =
133 | let r # t := f (stck # t)
134 | _ # t := write1 (bytes @{p.hasb} stck) "" t
138 | 0 Parser1 : (e : Type) -> (a : Type) -> Type
139 | Parser1 e a = {0 q : _} -> P1 q e a
142 | lex1 : {r : _} -> List (Entry r (DFA q r s)) -> Lex1 q r s
143 | lex1 es = arr32 r (dfa []) es