0 | module Text.ILex.Dependent.Parser
  1 |
  2 | import Data.Buffer
  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
  9 |
 10 | %default total
 11 |
 12 | public export
 13 | 0 Stp : (s,a : Type) -> Type
 14 | Stp s a = Trans (In s -> a)
 15 |
 16 | public export
 17 | 0 Stps : (s,a : Type) -> Type
 18 | Stps s a = TokenMap (Stp s a)
 19 |
 20 | parameters {0 s,a : Type}
 21 |            (vx    : RExp True)
 22 |
 23 |   export %inline
 24 |   ignore : (RExp True,Stp s a)
 25 |   ignore = (vx, Ign)
 26 |
 27 |   export %inline
 28 |   step : (In s -> a) -> (RExp True, Stp s a)
 29 |   step f = (vx, Run f)
 30 |
 31 |   export %inline
 32 |   vstep : (s -> a) -> (RExp True, Stp s a)
 33 |   vstep f = (vx, Run $ f . val)
 34 |
 35 |   export %inline
 36 |   bytes : (ByteString -> s -> a) -> (RExp True, Stp s a)
 37 |   bytes f = (vx, Run $ \i => f (getBytes i) i.val)
 38 |
 39 |   export %inline
 40 |   string : (String -> s -> a) -> (RExp True, Stp s a)
 41 |   string f = (vx, Run $ \i => f (getString i) i.val)
 42 |
 43 | export
 44 | jsonSpace : RExp True
 45 | jsonSpace = oneof [' ','\t','\n','\r']
 46 |
 47 | export %inline
 48 | jsonSpaces : RExp True
 49 | jsonSpaces = plus jsonSpace
 50 |
 51 | export %inline
 52 | jsonSpaced : Stps s a -> Stps s a
 53 | jsonSpaced xs = ignore jsonSpaces :: xs
 54 |
 55 | public export
 56 | 0 Result : (p : Type -> Type) -> Type
 57 | Result p = Entry p Prelude.id
 58 |
 59 | export %inline
 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))
 62 |
 63 | export %inline
 64 | to : {0 p : _} -> x -> p t -> (x, Stp t $ Result p)
 65 | to vx v = (vx, Run (E v . val))
 66 |
 67 | public export
 68 | 0 DStep : (p : Type -> Type) -> Type -> Type
 69 | DStep p t = Stp t (Result p)
 70 |
 71 | public export
 72 | 0 DSteps : (p : Type -> Type) -> Type -> Type
 73 | DSteps p t = Stps t (Result p)
 74 |
 75 | public export
 76 | 0 DFun : Type -> Type -> Type
 77 | DFun a t = In t -> a
 78 |
 79 | public export
 80 | 0 Run : (p : Type -> Type) -> Type -> Type
 81 | Run p x = In x -> Result p
 82 |
 83 | ||| An array of arrays describing a lexer's state machine.
 84 | public export
 85 | 0 DDFA : (p : Type -> Type) -> Type -> Type
 86 | DDFA p x = DFA (Run p x)
 87 |
 88 | ||| A parser is a system of automata, where each
 89 | ||| lexicographic token determines the next automaton
 90 | ||| state plus lexer to use.
 91 | public export
 92 | record IParser (e,a : Type) where
 93 |   constructor P
 94 |   {0 lexer   : Type -> Type}
 95 |   {0 itpe    : Type}
 96 |   init       : lexer itpe
 97 |   istack     : itpe
 98 |   lex        : TArray lexer (DDFA lexer)
 99 |   err        : TArray lexer (DFun e)
100 |   eoi        : TArray lexer (DFun $ Either e a)
101 |   {lexers    : Bits32}
102 |   {auto en   : TEnum lexer lexers}
103 |
104 | public export
105 | 0 PStep : (p : IParser e a) -> Type -> Type
106 | PStep p i = Stp i (Result p.lexer)
107 |
108 | public export
109 | 0 PRun : (p : IParser e a) -> Type -> Type
110 | PRun p t = Run p.lexer t
111 |
112 | ||| An array of arrays describing a lexer's stack machine.
113 | public export
114 | 0 PByteTrans : Nat -> (p : IParser e a) -> Type -> Type
115 | PByteTrans n p t = IArray 256 (Transition n $ PRun p t)
116 |
117 | ||| An array of arrays describing a lexer's stack machine.
118 | public export
119 | 0 PMachine : Nat -> (p : IParser e a) -> Type -> Type
120 | PMachine n p t = IArray (S n) (PByteTrans n p t)
121 |
122 | parameters {0 p     : Type -> Type}
123 |            {n       : Bits32}
124 |            {auto en : TEnum p n}
125 |
126 |   export %inline
127 |   arrErr : Entries p (DFun $ BBErr e) -> TArray p (DFun $ BBErr e)
128 |   arrErr = tarray _ _ $ unexpectedErr []
129 |
130 |   export %inline
131 |   arrEOI :
132 |        Entries p (DFun $ Either (BBErr e) a)
133 |     -> TArray p (DFun $ Either (BBErr e) a)
134 |   arrEOI = tarray _ _ $ failUnexpected []
135 |
136 |   export %inline
137 |   lexers : Entries p (DDFA p) -> TArray p (DDFA p)
138 |   lexers = tarray _ _ $ dfa []
139 |
140 | parameters (p  : IParser e a)
141 |
142 |   export %inline
143 |   failI : p.lexer t -> In t -> Either e x
144 |   failI i arg = Left $ TArray.at @{p.en} p.err i arg
145 |
146 |   export %inline
147 |   eoi : p.lexer t -> In t -> Either e a
148 |   eoi i arg = TArray.at @{p.en} p.eoi i arg
149 |