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)
76 | interface HasBytes (0 s : Type -> Type) where
80 | prev : s q -> Ref q ByteString
83 | cur : s q -> Ref q ByteString
87 | offset : s q -> Ref q Nat
94 | relpos : s q -> Ref q Integer
97 | len : s q -> Ref q Nat
101 | positions : s q -> Ref q (SnocList BytePos)
110 | getBytes : HasBytes s => (sk : s q) => F1 q ByteString
112 | bs <- read1 (cur sk)
113 | p <- read1 (relpos sk)
114 | l <- read1 (len sk)
115 | case prim__lt_Integer p 0 of
116 | 0 => pure (substring (cast p) l bs)
117 | n => read1 (prev sk) >>= \pre =>
118 | pure (pre <+> take (cast $
cast l + p) bs)
124 | record P1 (q,e : Type) (a : Type) where
127 | {0 state : Type -> Type}
128 | init : Index states
129 | stck : F1 q (state q)
130 | lex : Lex1 q states state
131 | chunk : state q -> F1 q (Maybe a)
132 | err : Arr32 states (state q -> F1 q e)
133 | eoi : Index states -> state q -> F1 q (Either e a)
134 | {auto hasb : HasBytes state}
137 | 0 PST : (p : P1 q e a) -> Type
141 | 0 PIx : (p : P1 q e a) -> Type
142 | PIx p = Index p.states
145 | 0 PStep : (p : P1 q e a) -> Type
146 | PStep p = Step q p.states p.state
149 | 0 PRun : (p : P1 q e a) -> Type
150 | PRun p = Run1 q p.states p.state
153 | 0 PIgn : (p : P1 q e a) -> Type
154 | PIgn p = Ign1 q p.states p.state
158 | 0 PByteStep : Nat -> (p : P1 q e a) -> Type
159 | PByteStep n p = IArray 256 (Transition n q p.states p.state)
163 | 0 PStepper : Nat -> (p : P1 q e a) -> Type
164 | PStepper n p = IArray (S n) (PByteStep n p)
168 | (0 s : Type -> Type)
169 | -> Arr32 r (s q -> F1 q e)
172 | -> F1 q (Either e x)
173 | arrFail s arr ix st t =
174 | let eo := arr `at` ix
179 | fail : (p : P1 q e a) -> PIx p -> PST p -> F1 q (Either e x)
180 | fail p = arrFail p.state p.err
183 | 0 Parser1 : (e : Type) -> (a : Type) -> Type
184 | Parser1 e a = {0 q : _} -> P1 q e a
187 | lex1 : {r : _} -> List (Entry r (DFA q r s)) -> Lex1 q r s
188 | lex1 es = arr32 r (dfa []) es