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 `HasPosition` for a record type with the
105 | ||| following fields:
106 | |||
107 | ||| ```idris
108 | ||| line_ : Ref q Nat
109 | ||| col_ : Ref q Nat
110 | ||| positions_ : Ref q Nat
111 | ||| ```
112 | export
117 | where
126 | ||| Derives an implementation of `HasBytes` for a record type with the
127 | ||| following field:
128 | |||
129 | ||| ```idris
130 | ||| bytes_ : Ref q ByteString
131 | ||| ```
132 | export
137 | where
146 | ||| Derives an implementation of `HasStringLits` for a record type with the
147 | ||| following field:
148 | |||
149 | ||| ```idris
150 | ||| strings_ : Ref q (SnocList String)
151 | ||| ```
152 | export
157 | where
174 | where
182 | -- `Ref q (Maybe (BoundedErr e))`
186 | ||| Derives an implementation of `HasError` for a record type with the
187 | ||| following field:
188 | |||
189 | ||| ```idris
190 | ||| error_ : Ref q (Maybe $ BoundedErr e)
191 | ||| ```
192 | |||
193 | ||| The error type `e` can be freely chosen (it can also be a parameter) and
194 | ||| will be determined when deriving the implementation.
195 | export
201 | where
219 | where
227 | -- `Ref q a`
231 | ||| Derives an implementation of `HasStack` for a record type with the
232 | ||| following field:
233 | |||
234 | ||| ```idris
235 | ||| stack_ : Ref q a
236 | ||| ```
237 | |||
238 | ||| The stack type `a` can be freely chosen (it can also be a parameter) and
239 | ||| will be determined when deriving the implementation.
240 | export
246 | where
256 | ||| Derives implementations of the following interfaces for a suitable
257 | ||| record type: `HasPosition`, `HasBytes`, `HasStringLits`, `HasError`, and
258 | ||| `HasStack`.
259 | export
262 | sequenceJoin
268 | ]