0 | module Text.ILex.Dependent.Parser
3 | import public Data.ByteString
4 | import public Data.TArray
5 | import public Text.ByteBounds
6 | import public Text.ILex.DFA
7 | import public Text.ILex.Dependent.Bytes
8 | import public Text.ParseError
13 | 0 Stp : (s,a : Type) -> Type
14 | Stp s a = Trans (In s -> a)
17 | 0 Stps : (s,a : Type) -> Type
18 | Stps s a = TokenMap (Stp s a)
20 | parameters {0 s,a : Type}
24 | ignore : (RExp True,Stp s a)
28 | step : (In s -> a) -> (RExp True, Stp s a)
29 | step f = (vx, Run f)
32 | vstep : (s -> a) -> (RExp True, Stp s a)
33 | vstep f = (vx, Run $
f . val)
36 | bytes : (ByteString -> s -> a) -> (RExp True, Stp s a)
37 | bytes f = (vx, Run $
\i => f (getBytes i) i.val)
40 | string : (String -> s -> a) -> (RExp True, Stp s a)
41 | string f = (vx, Run $
\i => f (getString i) i.val)
44 | jsonSpace : RExp True
45 | jsonSpace = oneof [' ','\t','\n','\r']
48 | jsonSpaces : RExp True
49 | jsonSpaces = plus jsonSpace
52 | jsonSpaced : Stps s a -> Stps s a
53 | jsonSpaced xs = ignore jsonSpaces :: xs
56 | 0 Result : (p : Type -> Type) -> Type
57 | Result p = Entry p Prelude.id
60 | stepTo : {0 p : _} -> x -> p t -> (In s -> t) -> (x, Stp s $
Result p)
61 | stepTo vx v f = (vx, Run (E v . f))
64 | to : {0 p : _} -> x -> p t -> (x, Stp t $
Result p)
65 | to vx v = (vx, Run (E v . val))
68 | 0 DStep : (p : Type -> Type) -> Type -> Type
69 | DStep p t = Stp t (Result p)
72 | 0 DSteps : (p : Type -> Type) -> Type -> Type
73 | DSteps p t = Stps t (Result p)
76 | 0 DFun : Type -> Type -> Type
77 | DFun a t = In t -> a
80 | 0 Run : (p : Type -> Type) -> Type -> Type
81 | Run p x = In x -> Result p
85 | 0 DDFA : (p : Type -> Type) -> Type -> Type
86 | DDFA p x = DFA (Run p x)
92 | record IParser (e,a : Type) where
94 | {0 lexer : Type -> Type}
98 | lex : TArray lexer (DDFA lexer)
99 | err : TArray lexer (DFun e)
100 | eoi : TArray lexer (DFun $
Either e a)
102 | {auto en : TEnum lexer lexers}
105 | 0 PStep : (p : IParser e a) -> Type -> Type
106 | PStep p i = Stp i (Result p.lexer)
109 | 0 PRun : (p : IParser e a) -> Type -> Type
110 | PRun p t = Run p.lexer t
114 | 0 PByteTrans : Nat -> (p : IParser e a) -> Type -> Type
115 | PByteTrans n p t = IArray 256 (Transition n $
PRun p t)
119 | 0 PMachine : Nat -> (p : IParser e a) -> Type -> Type
120 | PMachine n p t = IArray (S n) (PByteTrans n p t)
122 | parameters {0 p : Type -> Type}
124 | {auto en : TEnum p n}
127 | arrErr : Entries p (DFun $
BBErr e) -> TArray p (DFun $
BBErr e)
128 | arrErr = tarray _ _ $
unexpectedErr []
132 | Entries p (DFun $
Either (BBErr e) a)
133 | -> TArray p (DFun $
Either (BBErr e) a)
134 | arrEOI = tarray _ _ $
failUnexpected []
137 | lexers : Entries p (DDFA p) -> TArray p (DDFA p)
138 | lexers = tarray _ _ $
dfa []
140 | parameters (p : IParser e a)
143 | failI : p.lexer t -> In t -> Either e x
144 | failI i arg = Left $
TArray.at @{p.en} p.err i arg
147 | eoi : p.lexer t -> In t -> Either e a
148 | eoi i arg = TArray.at @{p.en} p.eoi i arg