1 | module Data.ByteString.Search.DFA.Types
3 | import Data.Array.Core
5 | import Data.ByteString
8 | import Data.Linear.Ref1
10 | %hide Data.Buffer.Core.get
11 | %hide Data.Buffer.Core.set
12 | %hide Data.List.Elem.get
26 | dfaStateLimit : Bits32
27 | dfaStateLimit = 0x01000000
35 | DFAStateCount : Type
36 | DFAStateCount = Index dfaStateLimit
44 | DFAState : Bits32 -> Type
53 | record DFAIndex (states : Bits32) where
54 | constructor MkDFAIndex
55 | state : Index states
67 | 0 dfaIndexLT : (state : Bits32)
68 | -> {states : Bits32}
69 | -> (0 statesPrf : states < Data.ByteString.Search.DFA.Types.dfaStateLimit)
70 | -> (0 statePrf : state < states)
72 | -> ((state * 256) + cast byte) < (states * 256)
73 | dfaIndexLT state statesPrf statePrf byte =
85 | dfaIndex : {states : Bits32}
86 | -> {auto 0 statesPrf : states < Data.ByteString.Search.DFA.Types.dfaStateLimit}
88 | -> Index (states * 256)
89 | dfaIndex (MkDFAIndex (I state {prf}) byte) =
90 | I ((state * 256) + cast byte) {prf = dfaIndexLT state statesPrf prf byte}
98 | dfaStateValue : DFAState states -> Bits32
99 | dfaStateValue (I state) = state
108 | dfaTransitionIndex : {states : Bits32}
109 | -> {auto 0 statesPrf : states < Data.ByteString.Search.DFA.Types.dfaStateLimit}
112 | -> Index (states * 256)
113 | dfaTransitionIndex state byte =
114 | dfaIndex (MkDFAIndex state byte)
122 | record DFATable (s : Type) (states : Bits32) where
123 | constructor MkDFATable
129 | newDFATable : {states : Bits32}
130 | -> {auto 0 statesPrf : states < Data.ByteString.Search.DFA.Types.dfaStateLimit}
131 | -> F1 s (DFATable s states)
132 | newDFATable {states} t =
133 | let arr # t := ffi (prim__emptyArray $
cast (states * 256)) t
134 | in MkDFATable arr # t
145 | dfaTransition : {states : Bits32}
146 | -> {auto 0 statesPrf : states < Data.ByteString.Search.DFA.Types.dfaStateLimit}
147 | -> DFATable s states
150 | -> F1 s (DFAState states)
151 | dfaTransition table state byte t =
152 | let I index = dfaTransitionIndex state byte
153 | in believe_me (prim__arrayGet table.arr (cast index)) # t
164 | setDFATransition : {states : Bits32}
165 | -> {auto 0 statesPrf : states < Data.ByteString.Search.DFA.Types.dfaStateLimit}
166 | -> DFATable s states
171 | setDFATransition table state byte next =
172 | let I index = dfaTransitionIndex state byte
173 | in ffi (prim__arraySet table.arr (cast index) (believe_me next))
182 | record DFAStateSpace where
183 | constructor MkDFAStateSpace
185 | 0 statesPositive : 0 < states
186 | 0 statesBounded : states < Data.ByteString.Search.DFA.Types.dfaStateLimit
196 | dfaStateSpace : ByteString -> Maybe DFAStateSpace
198 | let states : Bits32
199 | states = cast $
S (length bs)
200 | in case tryIndex {r = dfaStateLimit} states of
203 | Just (I states' {prf}) =>
204 | Just (MkDFAStateSpace states' (believe_me ()) prf)
215 | record DFAutomaton (s : Type) where
216 | constructor MkDFAutomaton
217 | space : DFAStateSpace
218 | table : DFATable s space.states
228 | toDFAState : (space : DFAStateSpace)
230 | -> Maybe (DFAState space.states)
231 | toDFAState space n =
232 | tryIndex {r = space.states} (cast n)
240 | zeroDFAState : (space : DFAStateSpace)
241 | -> DFAState space.states
242 | zeroDFAState space =
243 | I 0 {prf = space.statesPositive}