Debian Codegraph @

texmf-dist/doc/latex/expkv-bundle/pkg-pop.tex

raw
1 \genericekv\expkvp{-pop}
2
3 The \expkvp\ is mainly written to lay the basis for \expkvc's and \expkvd's
4 key-defining front ends. Historically the two packages shared pretty similar
5 code. To unify this and reduce the overall code amount some auxiliary package
6 was originally planned, but then I realised that with little to no overhead
7 (apart from documentation) this can also be provided to users. Well, and then I
8 thought, why not make the whole thing expandable as well. And here we are.
9
10 So what's the idea? This package provides a \textbf{p}refix \textbf{o}riented
11 \textbf{p}arser\footnote{Naming packages is hard, especially when the name
12 should fit a particular naming scheme. Big thanks to samcarter for helping
13 me: \url{https://topanswers.xyz/tex?q=1985}. The author apologises that there is
14 no \texttt{expkv-pnk}, \texttt{expkv-rok}, \texttt{expkv-jaz} or any other music
15 themed name in \expkvbundle.} with two kinds of prefixes. The first is called a
16 \prefix, of which an item can have arbitrary many, the second a \type, of which
17 every item has only one. To distinguish the concept of an optional \prefix\ from
18 the generic term \enquote{prefix} I'll use this formatting whenever the special
19 kind of prefix is meant.
20
21 Another peculiarity of \expkvp\ compared to the other packages in
22 \expkvbundle\ is that it doesn't separate \Nkey{}s from \Vkey{}s as
23 strictly. Instead a |NoVal|-marker is used as the value. If this is not what you
24 want you can use \cs{ekvpValueAlwaysRequired} (see there).
25
26
27 \subsection{Parsing Rules}
28
29 A parser is processing only the \key\ of a \kv\ pair. The \key\ is split at
30 spaces (braces might be lost by this process!). Each split off piece is checked
31 whether it's a defined prefix. If it's a \type\ parsing of the \key\ stops and
32 the remainder is considered a \meta{name}. If it's a \prefix\ it'll be recorded
33 and parsing goes on. If it's neither parsing is also stopped (and the last
34 parsed space delimited part is put back -- braces might've been lost at this
35 step). If a no-type rule has been defined (\cs{ekvpDefNoType}) that one is
36 executed else an error is thrown.
37
38 The \prefix\ or \type\ has to match after being |\detokenize|d, whereas the
39 \meta{name} will be unchanged (except for stripping off the prefixes). If only a
40 \key\ is given (so no =\val\ used) the same is done, and instead of \val\ a
41 no-value marker is used (if that accidentally ends up in the input stream this
42 looks like this: \csname c\string_novalue\string_tl\endcsname; this is the same
43 as the marker used by \pkg{expl3} for those familiar with it).
44
45 A \prefix\ has two parts to it, a \meta{pre} and a \meta{post} code, whereas a
46 \type\ only results in a \type-action (or the no-type action if that's defined
47 and no \type\ found). The parsing result can also be seen in
48 \autoref{fig:p:result}.
49
50 \begin{figure}[b]
51 \centering
52 \def\plevel#1{\raisebox{1ex}{#1}}%
53 \def\ts#1{\textsubscript{$#1$}}%
54 \begingroup
55 \ttfamily
56 pre\ts1
57 \plevel
58 {%
59 pre\ts2
60 \plevel
61 {%
62 \ldots\space
63 \plevel
64 {%
65 pre\ts n
66 \plevel{\type-action}
67 post\ts n
68 }%
69 \ldots\space
70 }%
71 post\ts2
72 }%
73 post\ts{1}%
74 \endgroup
75 \caption
76 {%
77 Structure of a single \kv\ pair's parsing result with $n$~\prefix{}es%
78 \label{fig:p:result}%
79 }
80 \end{figure}
81
82 Please note that \expkvp's parsers are fully expandable as long as your
83 \prefixes\ and \types\ are. Additionally \expkvp\ doesn't provide means to
84 define parsers, \prefixes, or \types\ |\protected|. As a result, make sure
85 you'll always call |\ekvpParse| inside of a |\protected| macro if you need
86 anything that's unexpandable or else your code might not do what you intended
87 since some states may not be updated when expandable code tries to access them.
88 The macro |\ekvpProtect| can help to overcome this issue, but it's more of a
89 last resort than a clean solution.
90
91 \subsection{Defining Parsers}
92
93 \begin{function}{\ekvpNewParser}
94 \begin{syntax}
95 \cs{ekvpNewParser}\marg{parser}
96 \end{syntax}
97 Defines a new parser called \meta{parser}. Every parser has to be defined this
98 way. Throws an error if the parser is already defined.
99 \end{function}
100
101 \begin{function}{\ekvpDefType}
102 \begin{syntax}
103 \cs{ekvpDefType}\marg{parser}\marg{type}\marg{code}
104 \end{syntax}
105 Defines a \type\ that is called \meta{type} for the parser \meta{parser}. If
106 the \type\ is parsed the \meta{code} will be used as a \type-action. Inside of
107 \meta{code} you can use |#1| to refer to the \meta{name} (the remainder of the
108 \key\ after stripping off all the prefixes), |#2| to use the unaltered \key,
109 and |#3| to access the \val\ which was given to your \key.
110 \end{function}
111
112 \begin{function}{\ekvpDefPrefix}
113 \begin{syntax}
114 \cs{ekvpDefPrefix}\marg{parser}\marg{prefix}\marg{pre}\marg{post}
115 \end{syntax}
116 Defines a \prefix\ that is called \meta{prefix} for the parser \meta{parser}.
117 If the \prefix\ is encountered the code in \meta{pre} will be put before the
118 \type-action and the code in \meta{post} will be put behind it.
119 If multiple \prefix{}es are used the \meta{pre} of the first will be put first
120 and the \meta{post} of the first will be put last. Inside of \meta{pre} and
121 \meta{post} |#1| is replaced with the found \type, |#2| the \meta{name}, and
122 |#3| the unaltered \key. If no valid type was found and the no-type rule
123 defined with \cs{ekvpDefNoType} is executed the argument |#1| will be empty.
124 \end{function}
125
126 \begin{function}{\ekvpDefAutoPrefix}
127 \begin{syntax}
128 \cs{ekvpDefAutoPrefix}\marg{parser}\marg{pre}\marg{post}
129 \end{syntax}
130 You can also define a \prefix-like rule that is executed on each element
131 automatically. So the \meta{pre} and \meta{post} code of this will be inserted
132 for every valid element of the \kv\ list. Just like for \cs{ekvpDefPrefix} you
133 can access the \type\ with |#1|, the \meta{name} with |#2|, and the unaltered
134 \key\ with |#3|.
135 \end{function}
136
137 \begin{function}{\ekvpDefPrefixStore}
138 \begin{syntax}
139 \cs{ekvpDefPrefixStore}\marg{parser}\marg{prefix}\meta{cs}\marg{pre}\marg{post}
140 \end{syntax}
141 This is a shortcut to define a \prefix\ named \meta{prefix} for \meta{parser}
142 that'll store \meta{pre} inside of \meta{cs} (which should be a single control
143 sequence) before the \type-action and afterwards store \meta{post} in it. Both
144 definitions (in \meta{pre} and in \meta{post}) are put inside |\ekvpProtect|.
145 \end{function}
146
147 \begin{function}{\ekvpDefPrefixLet}
148 \begin{syntax}
149 \cs{ekvpDefPrefixLet}\marg{parser}\marg{prefix}\meta{cs}\meta{pre}\meta{post}
150 \end{syntax}
151 This is similar to \cs{ekvpDefPrefixStore}, but instead of storing in the
152 \meta{cs} it'll be let to the single tokens specified by \meta{pre} and
153 \meta{post}. If either \meta{pre} or \meta{post} contains more than a single
154 token the remainder is put after the |\let| statement. Both assignments (in
155 \meta{pre} and in \meta{post}) are put inside |\ekvpProtect|.
156 \end{function}
157
158 \begin{function}{\ekvpLet}
159 \begin{syntax}
160 \cs{ekvpLet}\marg{parser_1}\marg{type}\marg{name_1}\oarg{parser_2}\marg{name_2}
161 \end{syntax}
162 Copies the definition of a \prefix\ or \type. The \meta{type} should be one of
163 |prefix|, or |type|. The \prefix\ or \type\ \meta{name_1} for \meta{parser_1}
164 will be let equal to the \prefix\ or \type\ \meta{name_2} of \meta{parser_2}.
165 If you omit the optional \meta{parser_2} it will default to \meta{parser_1}.
166 \end{function}
167
168
169 \subsection{Changing Default Behaviours}
170
171 \begin{function}{\ekvpValueAlwaysRequired}
172 \begin{syntax}
173 \cs{ekvpValueAlwaysRequired}\marg{parser}
174 \end{syntax}
175 By default a special no-value marker will be provided for \val\ if no value
176 was given to a key. If this is used instead an error will be thrown that a
177 value is required.
178 \end{function}
179
180 \begin{function}{\ekvpDefNoValue}
181 \begin{syntax}
182 \cs{ekvpDefNoValue}\marg{parser}\marg{code}
183 \end{syntax}
184 This is a third alternative to the default behaviour and
185 \cs{ekvpValueAlwaysRequired}. With this macro you can stop normal parsing if
186 no value was specified and instead run \meta{code}. Inside of \meta{code} the
187 unprocessed \Nkey\ is available as |#1|. No further processing of this \kv\
188 list element takes place.
189 \end{function}
190
191 \begin{function}{\ekvpUseNoValueMarker}
192 \begin{syntax}
193 \cs{ekvpUseNoValueMarker}\marg{parser}\marg{marker}
194 \end{syntax}
195 This macro changes the no-value marker from the package default to
196 \meta{marker}. Note that macros like \cs{ekvpAssertValue} don't work with
197 markers different from the default one.
198 \end{function}
199
200 \begin{function}{\ekvpDefNoValuePrefix}
201 \begin{syntax}
202 \cs{ekvpDefNoValuePrefix}\marg{parser}\marg{pre}\marg{post}
203 \end{syntax}
204 It is also possible to handle \Nkey{}s as if this was some special \prefix.
205 So if a \Nkey\ is encountered you'll have \meta{pre} and \meta{post} put
206 before and behind the \type-action (as the outermost \prefix). The no-value
207 marker will be forwarded as \val. If you want to change a parser's marker and
208 use this you have to use \cs{ekvpUseNoValueMarker} before calling
209 \cs{ekvpDefNoValuePrefix}, and you must not use \cs{ekvpDefNoValue} or
210 \cs{ekvpValueAlwaysRequired} before using \cs{ekvpDefNoValuePrefix} (both
211 result in undefined behaviour).
212 \end{function}
213
214 \begin{function}{\ekvpDefNoType}
215 \begin{syntax}
216 \cs{ekvpDefNoType}\marg{parser}\marg{code}
217 \end{syntax}
218 This defines an action if no valid \type\ was found, otherwise this behaves
219 like \cs{ekvpDefType} with the same arguments |#1| (\meta{name}), |#2|
220 (unaltered \key), and |#3| (\val) in \meta{code}. If this isn't used for the
221 \meta{parser} instead an error will be thrown whenever no \type\ is found.
222 \end{function}
223
224
225 \subsection{Markers}
226
227 \expkvp\ will place three markers for each list element that was parsed and
228 defines an auxiliary to gobble up to that marker. After each marker an
229 additional group is placed containing the current list element (excluding the
230 \val). The gobblers gobble that group as well. Those markers are:
231
232 \begin{function}{\ekvpEOP,\ekvpGobbleP}
233 Is placed after all the \prefix{}es' \meta{pre} code, directly before the
234 \type-action.
235 \end{function}
236
237 \begin{function}{\ekvpEOT,\ekvpGobbleT}
238 Is placed after the \type-action, directly before the last \prefix's
239 \meta{post} code.
240 \end{function}
241
242 \begin{function}{\ekvpEOA,\ekvpGobbleA}
243 Is placed at the end of the complete result of the current element, so after
244 all the \prefix{}es' \meta{post} code.
245 \end{function}
246
247
248 \subsection{Helpers in Actions}
249
250 \begin{function}{\ekvpIfNoVal}
251 \begin{syntax}
252 \cs{ekvpIfNoVal}\marg{arg}\marg{true}\marg{false}
253 \end{syntax}
254 This will expand to \meta{true} if the \meta{arg} is the special no-value
255 marker, otherwise \meta{false} is left in the input stream.
256 \end{function}
257
258 \begin{function}{\ekvpAssertIf,\ekvpAssertIfNot}
259 \begin{syntax}
260 \cs{ekvpAssertIf}\oarg{marker}\marg{if}\marg{message}
261 \end{syntax}
262 This macro will run the \TeX-if test specified by \meta{if} (should expand to
263 any \TeX-style if, e.g., \cs[no-index]{iftrue} or
264 \cs[no-index]{ifx}\meta{A}\meta{B}). If the test is true everything's fine,
265 else an error message is thrown using \meta{message} followed by the current
266 element and everything up to \meta{marker} is gobbled (\meta{marker} can be
267 any of |EOT|, which is the default, |EOP|, or |EOA|). The |Not| variant will
268 invert the logic, so if the \TeX-style if is true this will throw the error.
269 \end{function}
270
271 \begin{function}{\ekvpAssertTF,\ekvpAssertTFNot}
272 \begin{syntax}
273 \cs{ekvpAssertTF}\oarg{marker}\marg{if}\marg{message}
274 \end{syntax}
275 This is pretty similar to \cs{ekvpAssertIf}, but \meta{if} should be a test
276 that uses its first argument if it's true and its second otherwise (so an
277 error is thrown if the second argument is used, nothing happens otherwise).
278 The |Not| variant is again inversed.
279 \end{function}
280
281 \begin{function}{\ekvpAssertValue,\ekvpAssertNoValue}
282 \begin{syntax}
283 \cs{ekvpAssertValue}\oarg{marker}\marg{arg}
284 \end{syntax}
285 Asserts that \meta{arg} is not the no-value marker (\cs{ekvpAssertValue}) or
286 is the no-value marker (\cs{ekvpAssertNoValue}), otherwise throws an error and
287 gobbles everything up to \meta{marker} (like \cs{ekvpAssertIf}).
288 \end{function}
289
290 \begin{function}{\ekvpAssertOneValue,\ekvpAssertTwoValues}
291 \begin{syntax}
292 \cs{ekvpAssertOneValue}\oarg{marker}\marg{arg}
293 \end{syntax}
294 Asserts that \meta{arg} contains exactly one or two values (which could both
295 be either single tokens or braced groups -- spaces between the two values in
296 the \cs{ekvpAssertTwoValues} case are ignored), if the number of values
297 doesn't match an error is thrown and everything up to \meta{marker} gobbled.
298 \end{function}
299
300 \begin{function}{\ekvpProtect}
301 \begin{syntax}
302 \cs{ekvpProtect}\marg{code}
303 \end{syntax}
304 This macro protects \meta{code} from further expanding in every context a
305 |\protected| macro wouldn't expand. It needs at least two steps of expansion.
306 When a |\protected| macro would expand this will remove the braces around
307 \meta{code} and \TeX\ will process \meta{code} the same way it normally would.
308 After the first step of expansion it'll leave two macros, and after each
309 further full expansion these two macros stay there. Since \expkvp\ offers no
310 method to define \prefixes\ or \types\ |\protected| you can instead use this
311 macro. But if your parser needs any assignments you should nest the
312 |\ekvpParse| call in a |\protected| macro anyway.
313 \end{function}
314
315 \subsection{Using Parsers}
316
317 \begin{function}{\ekvpParse}
318 \begin{syntax}
319 \cs{ekvpParse}\marg{parser}\kvarg
320 \end{syntax}
321 Parses the \kv\ list as defined for \meta{parser}. This expands in exactly two
322 steps, and returns inside of |\unexpanded|, so doesn't expand any further in
323 an |\edef| or |\expanded|. After the two steps it'll directly leave the code
324 as though every \prefix's \meta{pre} and \meta{post} code and the \type-action
325 were input directly along with the different markers.
326 \end{function}
327
328 \subsection{The Boring Macros}
329 Just for the sake of completeness.
330 \begin{function}{\ekvpDate,\ekvpVersion}
331 Store the date and version, respectively.
332 \end{function}
333
334 \subsection{Examples}
335 \begin{example}{Defining a parser similar to \expkvd}
336 \ekvset{enverb}{store,no-tcb}%
337 Let's define a parser that is similar to \expkvd's |\ekvdefinekeys|. First we
338 define a new parser named |exdef|:
339 \begin{enverb}
340 \ekvpNewParser{exdef}
341 \end{enverb}
342 We'll need to provide our prefixes, |long| and |protected|. The following
343 initialises them and defines their action.
344 \begin{enverb}
345 \newcommand*\exLong{}
346 \newcommand*\exProtected{}
347 \ekvpDefPrefixLet{exdef}{long}\exLong\long\empty
348 \ekvpDefPrefixLet{exdef}{protected}\exProtected\protected\empty
349 \end{enverb}
350 Now we define a few types, I'll go with |noval|, |store|, and |code| for
351 starters. We exploit the fact that |\ekvdef| and |\ekvdefNoVal| will expand
352 the first argument, so we can simply store the set name in a macro.
353 \begin{enverb}
354 \ekvpDefType{exdef}{code}
355 {%
356 \ekvpAssertValue{#3}%
357 \exProtected\exLong\ekvdef\exSetName{#1}{#3}%
358 }
359 \ekvpDefType{exdef}{noval}
360 {%
361 \ekvpAssertValue{#3}%
362 \ekvpAssertIfNot{\ifx\exLong\long}{`long' not accepted}%
363 \exProtected\ekvdefNoVal\exSetName{#1}{#3}%
364 }
365 \ekvpDefType{exdef}{store}
366 {%
367 \ekvpAssertOneValue{#3}%
368 \ifdefined#3\else
369 \let#3\empty
370 \fi
371 \protected\exLong\ekvdef\exSetName{#1}{\edef#3{\unexpanded{##1}}}%
372 }
373 \end{enverb}
374 Now we need a user facing macro that puts the pieces together (this uses
375 |\NewDocumentCommand| instead of |\newcommand| because the former defines
376 macros |\protected|).
377 \begin{enverb}
378 \NewDocumentCommand\exdefinekeys{m +m}
379 {\def\exSetName{#1}\ekvpParse{exdef}{#2}}
380 \end{enverb}
381 Now we got that sorted so we can use our little parser:
382 \ekvset{enverb}{no-store,undo-no-tcb}%
383 \begin{enverb}[restore]
384 \newif\ifbar
385 \exdefinekeys{exampleset}
386 {
387 long store foo = \myfoo
388 ,protected noval bar = \bartrue
389 ,code baz = baz was called with \detokenize{#1}
390 }
391 \ekvset{exampleset}{foo=Foobar,bar,baz=\empty}
392 \ifbar bar was true so: \myfoo\fi
393 \end{enverb}
394 \end{example}
395
396 \begin{example}{Visualising the expandability of \cs[no-index]{ekvpParse}}
397 \ekvset{enverb}{store,no-tcb}%
398 With this example we want to take a closer look at the expansion of
399 |\ekvpParse|. So please bear with me if this doesn't make much sense
400 otherwise. One of the issues is that \prefixes\ are a somewhat unordered
401 concept, and only \types\ must come last. We could juggle with flags (an
402 expandable data-storage, see \autoref{sec:c:flags}) to overcome this somehow
403 just to define some pseudo-syntax here, but I guess that's not worth it.
404 Anyhow, here goes nothing.\par
405 First we need a parser
406 \begin{enverb}
407 \ekvpNewParser{exexp}
408 \end{enverb}
409 and a \prefix. We could define one that ensures the name starts of with
410 a letter. We also want each element to start a new line, which we do using an
411 auto prefix.
412 \begin{enverb}
413 \newcommand\ifletter{}
414 \long\def\ifletter#1#2\stop{\ifcat a\noexpand#1}
415 \ekvpDefPrefix{exexp}{letter}
416 {\ekvpAssertIf{\ifletter#2\stop}{not a letter}}
417 { (really a letter)}
418 \ekvpDefAutoPrefix{exexp}{\noindent}{\par}
419 \end{enverb}
420 Finally we define a \type\ and a |NoType| rule:
421 \begin{enverb}
422 \ekvpDefType{exexp}{*}{$#1\cdot#3 = \the\numexpr#1*#3\relax$}
423 \ekvpDefNoType{exexp}{the #3th letter is #1}
424 \end{enverb}
425 Now we want to see whether the thing is expandable:
426 \ekvset{enverb}{no-store,undo-no-tcb}%
427 \begin{enverb}[restore,below]
428 \raggedright
429 \edef\foo{\ekvpParse{exexp}{letter e = 5, * 4 = \empty3}}
430 1st full expansion
431 \texttt{\meaning\foo}\par
432 \medskip
433 \edef\foo{\foo}
434 2nd full expansion
435 \texttt{\meaning\foo}\par
436 \medskip
437 \foo
438 \end{enverb}
439 \end{example}