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 | --------------------------------------------------------------------------------
114 | } sk
115 | )
117 | ||| Derives an implementation of `HasBytes` for a record type with the
118 | ||| following fields:
119 | |||
120 | ||| ```idris
121 | ||| bufSize_ : Nat
122 | ||| prev_ : ByteString
123 | ||| cur_ : IBuffer bufSize_
124 | ||| prevOffset_ : Nat
125 | ||| curOffset_ : Nat
126 | ||| from_ : Ref q (LTENat bufSize_)
127 | ||| till_ : Ref q (LTENat bufSize_)
128 | ||| positions_ : Ref q (SnocList BytePos)
129 | ||| ```
130 | export
135 | where
145 | `( MkHB
147 | bufSize_ prev_ cur_ prevOffset_ curOffset_ from_ till_ positions_
150 | ||| Derives an implementation of `HasStringLits` for a record type with the
151 | ||| following field:
152 | |||
153 | ||| ```idris
154 | ||| strings_ : Ref q (SnocList String)
155 | ||| ```
156 | export
161 | where
178 | where
186 | -- `Ref q (Maybe (BoundedErr e))`
190 | ||| Derives an implementation of `HasError` for a record type with the
191 | ||| following field:
192 | |||
193 | ||| ```idris
194 | ||| error_ : Ref q (Maybe $ BoundedErr e)
195 | ||| ```
196 | |||
197 | ||| The error type `e` can be freely chosen (it can also be a parameter) and
198 | ||| will be determined when deriving the implementation.
199 | export
205 | where
215 | ||| Derives an implementation of `HasBBEr` for a record type with the
216 | ||| following field:
217 | |||
218 | ||| ```idris
219 | ||| error_ : Ref q (Maybe $ BBErr e)
220 | ||| ```
221 | |||
222 | ||| The error type `e` can be freely chosen (it can also be a parameter) and
223 | ||| will be determined when deriving the implementation.
224 | export
230 | where
248 | where
256 | -- `Ref q a`
260 | ||| Derives an implementation of `HasStack` for a record type with the
261 | ||| following field:
262 | |||
263 | ||| ```idris
264 | ||| stack_ : Ref q a
265 | ||| ```
266 | |||
267 | ||| The stack type `a` can be freely chosen (it can also be a parameter) and
268 | ||| will be determined when deriving the implementation.
269 | export
275 | where
285 | ||| Derives implementations of the following interfaces for a suitable
286 | ||| record type: `HasBytePos`, `HasStringLits`, `HasBBErr`, and
287 | ||| `HasStack`.
288 | export
291 | sequenceJoin
296 | ]