6 | --------------------------------------------------------------------------------
7 | -- Deriving Parser States
8 | --------------------------------------------------------------------------------
10 | ||| Given names for the number of states (`size`), the
11 | ||| parser state (`state`), and the distinct parser states,
12 | ||| this will compute the actual number of states and define
13 | ||| a constant for each state.
14 | |||
15 | ||| For instance, `deriveParserState "SZ" "ST" ["SIni", "Str", "Err", "Done"]`
16 | ||| will generate the following definitions:
17 | |||
18 | ||| ```idris
19 | ||| public export
20 | ||| SZ : Bits32
21 | ||| SZ = 4
22 | |||
23 | ||| public export
24 | ||| 0 ST : Type
25 | ||| ST = Index SZ
26 | |||
27 | ||| export
28 | ||| SIni : ST
29 | ||| SIni = I 0
30 | |||
31 | ||| export
32 | ||| Str : ST
33 | ||| Str = I 1
34 | |||
35 | ||| export
36 | ||| Err : ST
37 | ||| Err = I 2
38 | |||
39 | ||| export
40 | ||| Done : ST
41 | ||| Done = I 3
42 | |||
43 | ||| export
44 | ||| showST : ST -> String
45 | ||| showST (I 0) = "SIni"
46 | ||| showST (I 1) = "Str"
47 | ||| showST (I 2) = "Err"
48 | ||| showST (I 3) = "Done"
49 | ||| showST _ = "impossible"
50 | ||| ```
51 | export
72 | where
86 | --------------------------------------------------------------------------------
87 | -- Deriving Interfaces
88 | --------------------------------------------------------------------------------
104 | ||| Derives an implementation of `HasBytes` for a record type with the
105 | ||| following fields:
106 | |||
107 | ||| ```idris
108 | ||| prev_ : Ref q ByteString
109 | ||| cur_ : Ref q ByteString
110 | ||| offset_ : Ref q Nat
111 | ||| relpos_ : Ref q Integer
112 | ||| len_ : Ref q Nat
113 | ||| positions_ : Ref q (SnocList BytePos)
114 | ||| ```
115 | export
120 | where
129 | ||| Derives an implementation of `HasStringLits` for a record type with the
130 | ||| following field:
131 | |||
132 | ||| ```idris
133 | ||| strings_ : Ref q (SnocList String)
134 | ||| ```
135 | export
140 | where
157 | where
165 | -- `Ref q (Maybe (BoundedErr e))`
169 | ||| Derives an implementation of `HasError` for a record type with the
170 | ||| following field:
171 | |||
172 | ||| ```idris
173 | ||| error_ : Ref q (Maybe $ BoundedErr e)
174 | ||| ```
175 | |||
176 | ||| The error type `e` can be freely chosen (it can also be a parameter) and
177 | ||| will be determined when deriving the implementation.
178 | export
184 | where
194 | ||| Derives an implementation of `HasBBEr` for a record type with the
195 | ||| following field:
196 | |||
197 | ||| ```idris
198 | ||| error_ : Ref q (Maybe $ BBErr e)
199 | ||| ```
200 | |||
201 | ||| The error type `e` can be freely chosen (it can also be a parameter) and
202 | ||| will be determined when deriving the implementation.
203 | export
209 | where
227 | where
235 | -- `Ref q a`
239 | ||| Derives an implementation of `HasStack` for a record type with the
240 | ||| following field:
241 | |||
242 | ||| ```idris
243 | ||| stack_ : Ref q a
244 | ||| ```
245 | |||
246 | ||| The stack type `a` can be freely chosen (it can also be a parameter) and
247 | ||| will be determined when deriving the implementation.
248 | export
254 | where
264 | ||| Derives implementations of the following interfaces for a suitable
265 | ||| record type: `HasBytePos`, `HasStringLits`, `HasBBErr`, and
266 | ||| `HasStack`.
267 | export
270 | sequenceJoin
275 | ]