summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/yquant/yquant-langhelper.tex
blob: 102a8959d90dd8869667ce16173f12b34ed8f17b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
% Parsing attributes
\let\yquant@langhelper@list@attrs=\empty

\protected\long\def\yquant@langhelper@check@attrs[#1]{%
   \appto\yquant@langhelper@list@attrs{#1,}%
   \yquant@env@contscan%
}

% shorthands for declaring attributes
\protected\def\yquant@langhelper@declare@attr{%
   \pgfqkeys{/yquant/all attrs}%
}

\protected\def\yquant@langhelper@declare@attr@global{%
   \pgfqkeys{/yquant/global attrs}%
}

\pgfkeys{%
   /yquant/attr filter handler/.code={%
      \let\yquant@langhelper@filter@@name=\pgfkeyscurrentkeyRAW%
      \let\yquant@langhelper@filter@@value=\pgfkeyscurrentvalue%
      % the key was filtered out, i.e., it is neither a local nor a global attribute.
      \pgfkeysinterruptkeyfilter%
         % execute it in /yquant, if it exists there
         \pgfqkeys{/yquant}{\yquant@langhelper@filter@@name/.try/.expand once=\yquant@langhelper@filter@@value}%
         \ifpgfkeyssuccess%
            \eappto#1{%
               ,/yquant/\unexpanded\expandafter{\yquant@langhelper@filter@@name}%
                  \unless\ifx\yquant@langhelper@filter@@value\pgfkeysnovalue@text%
                     ={\unexpanded\expandafter{\yquant@langhelper@filter@@value}}%
                  \fi%
            }%
         \else%
            \pgfkeys{/yquant/operator style/.expanded=%
               {\unexpanded\expandafter{\yquant@langhelper@filter@@name}=%
                  {\unexpanded\expandafter{\yquant@langhelper@filter@@value}}%
               }%
            }%
            \eappto#1{%
               ,/yquant/operator style={\unexpanded\expandafter{\yquant@langhelper@filter@@name}%
                  \unless\ifx\yquant@langhelper@filter@@value\pgfkeysnovalue@text%
                     ={\unexpanded\expandafter{\yquant@langhelper@filter@@value}}%
                  \fi%
               }%
            }%
         \fi%
      \endpgfkeysinterruptkeyfilter%
   },
   /pgf/key filters/attr filter/.code={%
      % we need to capture every non-attribute (which means filtering them out).
      \expandafter\ifyquant@beginswith\expandafter{\pgfkeyscurrentkey}{/yquant/attrs/}{%
         % the key is in /yquant/attrs. Does it actually exist (but not as a handler, which would define non-existing keys)?
         \if\pgfkeyscasenumber1%
            \pgfkeysfiltercontinuetrue%
         \else%
            \if\pgfkeyscasenumber2%
               \pgfkeysfiltercontinuetrue%
            \else%
               % 0 or 3 for our case means non-existing - now we need to check whether we can find it in the global attributes.
               \pgfkeysifassignable{/yquant/global attrs/\pgfkeyscurrentkeyRAW}{%
                  \pgfkeysfiltercontinuetrue%
               }{%
                  % it is not, so it is not a known attribute for this operator. Filter.
                  \pgfkeysfiltercontinuefalse%
               }%
            \fi%
         \fi%
      }{%
         % the key is not in /yquant/attrs. Is it a global attribute (usually, the user should not access this explicitly, but let's be tolerant here)?
         \expandafter\ifyquant@beginswith\expandafter{\pgfkeyscurrentkey}{/yquant/global attrs/}{%
            % the key is in /yquant/global attrs. Does it actually exist?
            \if\pgfkeyscasenumber1%
               \pgfkeysfiltercontinuetrue%
            \else%
               \if\pgfkeyscasenumber2%
                  \pgfkeysfiltercontinuetrue%
               \else%
                  % no - that's it.
                  \pgfkeysfiltercontinuefalse%
               \fi%
            \fi%
         }{%
            % the key is no known attribute
            \pgfkeysfiltercontinuefalse%
         }%
      }%
   },
   /yquant/attr filter/.style={
      /pgf/key filters/attr filter/.install key filter,
      /yquant/attr filter handler/.install key filter handler=\yquant@attrs@remaining%
   }%
}

% specifies which attributes can be used for a given command
% #1: command name
% #2: comma-separated list of required attrs
% #3: comma-separated list of optional attrs
\protected\def\yquant@langhelper@setup@attrs#1#2#3{%
   \begingroup%
      \lowercase{\edef\cmd{#1}}%
      \edef\keyscmd{%
         \noexpand\pgfkeyslet{/yquant/attrs/\cmd/.unknown/.@cmd}%
                             \yquant@langhelper@setup@attrs@unknown%
      }%
      \let\keysset=\relax%
      \let\keyscheck=\relax%
      \def\do##1{%
         \eappto\keyscmd{%
            \noexpand\pgfkeysdef{/yquant/attrs/\cmd/##1}{%
               \unexpanded{%
                  \cslet{yquant@langhelper@attrs@req@##1}\relax%
                  \pgfkeysinterruptkeyfilter%
                     \pgfkeys{/yquant/all attrs/##1={####1}}%
                  \endpgfkeysinterruptkeyfilter%
               }
            }%
         }%
         \eappto\keysset{%
            \def\expandafter\noexpand\csname yquant@langhelper@attrs@req@##1\endcsname{%
               \yquant@langhelper@setup@attrs@missing{##1}%
            }%
         }%
         \expandafter\appto\expandafter\keyscheck%
            \csname yquant@langhelper@attrs@req@##1\endcsname%
      }%
      \docsvlist{#2}%
      \global\cslet{yquant@langhelper@setup@attrs@set@\cmd}\keysset%
      \global\cslet{yquant@langhelper@setup@attrs@check@\cmd}\keyscheck%
      \def\do##1{%
         \eappto\keyscmd{%
            \noexpand\pgfkeysdef{/yquant/attrs/\cmd/##1}{%
               \unexpanded{%
                  \pgfkeysinterruptkeyfilter%
                     \pgfkeys{/yquant/all attrs/##1={####1}}%
                  \endpgfkeysinterruptkeyfilter%
               }%
            }%
         }%
      }%
      \docsvlist{#3}%
      \expandafter%
   \endgroup%
   \keyscmd%
}

\protected\def\yquant@langhelper@setup@attrs@unknown#1\pgfeov{%
   % This is in its essence /.search also={/yquant/global attrs}, but with filtering interrupted
   \pgfkeysinterruptkeyfilter%
      \ifpgfkeysaddeddefaultpath%
         % only process keys for which no full path has been provided:
         \pgfkeyssuccessfalse%
         \let\pgfkeys@searchalso@name=\pgfkeyscurrentkeyRAW%
         % We interrupt the filtering process because we only want to append the very top-level command to the remaining-macro.
         \unless\ifpgfkeyssuccess%
            \pgfqkeys{/yquant/global attrs}%
                     {\pgfkeys@searchalso@name={#1}}%
         \fi%
      \else%
         % the /.search also implementation defines this in an unexpanded macro, which is then executed. Any reason?
         \pgfkeysvalueof{/handlers/.unknown/.@cmd}#1\pgfeov%
      \fi%
   \endpgfkeysinterruptkeyfilter%
}

\protected\def\yquant@langhelper@setup@attrs@missing#1{%
   \PackageError{yquant.sty}{Required attribute `#1' missing}%
      {You used a command that requires specifying the attribute `#1'.}%
}%

% Parsing command itself
\protected\def\yquant@langhelper@check@name#1 {%
   \lowercase{\edef\cmd{#1}}%
   \unless\ifcsname yquant@lang@\cmd\endcsname%
      \PackageError{yquant.sty}{Unsupported yquant command: `#1'}%
         {You used a command that is unknown to yquant.}%
   \fi%
   % Provide the association with the correct attributes
   \csname yquant@langhelper@setup@attrs@set@\cmd\endcsname%
   % For "ordinary" uses of the quotes library (without additional options), it would be better not to load the library now, since then the original text (everything in quotes) is just appended to /yquant/operator style. However, then uses with options will break the pgfkeys parser, as they are invalid. For this reason, we must enable the quotes library already at this state, with the consequence that we store the longer version, i.e., the output of the quotes-parsed expression instead.
   \tikz@enable@node@quotes%
   % execute them
   \let\yquant@attrs@remaining=\empty%
   \unless\ifx\yquant@langhelper@list@attrs\empty%
      \edef\yquant@langhelper@list@attrs{%
         \noexpand\pgfqkeysfiltered%
            {/yquant/attrs/\cmd}%
            {\unexpanded\expandafter{\yquant@langhelper@list@attrs}}%
      }%
      \pgfkeys{/yquant/attr filter}%
      \yquant@langhelper@list@attrs%
      \let\yquant@langhelper@list@attrs=\empty% required for nested environments
   \fi%
   % If there was an argument, set it
   \@ifnextchar\bgroup%
      \yquant@langhelper@check@name@i%
      \yquant@langhelper@check@name@ii%
}

\protected\long\def\yquant@langhelper@check@name@i#1{%
   \ifdefined\yquant@lang@attr@value%
      \PackageWarning{yquant.sty}{Node content given explicitly and as an attribute. Using the explicit value.}%
   \fi%
   \def\yquant@lang@attr@value{#1}%
   \let\yquant@langhelper@attrs@req@value=\relax%
   \yquant@langhelper@check@name@ii%
}

\protected\def\yquant@langhelper@check@name@ii{%
   % Check all necessary attributes were given.
   \csname yquant@langhelper@setup@attrs@check@\cmd\endcsname%
   % Finally execute the command
   \csname yquant@lang@\cmd\endcsname%
}

% Defining commands that accept registers and take an optional value
% #1: name of the argument
% #2: command that is to be called with three parameters: the indices of the positive and negative controls and registers, all as named lists
\protected\def\yquant@langhelper@declare@command#1#2{%
   \begingroup%
      \lowercase{\edef\cmd{#1}}%
      \long\protected\csgdef{yquant@lang@\cmd}{%
         \let\cmd=#2%
         \yquant@langhelper@declare@command@@parse%
      }%
   \endgroup%
}

% Defining commands that accept registers and take an additional value
% #1: name of the argument
% #2: command that is to be called with three parameters: the indices of the positive and negative controls and registers, all as named lists
\protected\def\yquant@langhelper@declare@command@uncontrolled#1#2{%
   \begingroup%
      \lowercase{\edef\cmd{#1}}%
      \long\protected\csgdef{yquant@lang@\cmd}{%
         \def\cmd{\yquant@langhelper@declare@command@@uncontrolled{#2}}%
         \yquant@langhelper@declare@command@@parse%
      }%
   \endgroup%
}

\long\def\yquant@langhelper@declare@command@@parse#1;{%
   \yquant@langhelper@declare@command@@extract@nctrl#1~;%
   \yquant@env@rescan%
}

\def\yquant@langhelper@declare@command@@extract@nctrl#1~#2;{%
   \ifstrempty{#2}{%
      \yquant@langhelper@declare@command@@extract@pctrl#1|~~;%
   }{%
      \yquant@langhelper@declare@command@@extract@pctrl#1|~#2;%
   }%
}

\def\yquant@langhelper@declare@command@@extract@pctrl#1|#2~#3~;{%
   \ifstrempty{#2}{%
      \yquant@langhelper@declare@command@@exec#1||~#3;%
   }{%
      \yquant@langhelper@declare@command@@exec#1|#2~#3;%
   }%
}

\protected\def\yquant@langhelper@declare@command@@exec#1|#2|~#3;{%
   \edef\params{%
      {\trim@spaces{#2}}%
      {\trim@spaces{#3}}%
      {\trim@spaces{#1}}%
   }%
   \expandafter\cmd\params%
}

\protected\def\yquant@langhelper@declare@command@@uncontrolled#1#2#3{%
   \ifstrempty{#2}\relax{%
      \PackageError{yquant.sty}{Positive controls are not allowed for this command}{}%
   }%
   \ifstrempty{#3}\relax{%
      \PackageError{yquant.sty}{Negative controls are not allowed for this command}{}%
   }%
   #1%
}

% shorthands for validation of property values
% #1: a macro that will contain the sanitized value
% #2: a TeX register type: most likely \count, \dimen
% #3: a string to be checked
% This macro is not expandable.
\protected\def\yquant@langhelper@validate#1#2#3{%
   \begingroup%
      \afterassignment\yquant@langhelper@validate@i%
      #2 255=#3\relax\yquant@end%
      \unless\ifvalid%
         \PackageError{yquant.sty}{Invalid property value: `#3'}%
            {The value must be assignable to a \protect#2 register.}%
      \fi%
      \expandafter%
   \endgroup%
   \expandafter\def\expandafter#1\expandafter{\the#2 255}%
}

\def\yquant@langhelper@validate@relax{\relax}
\protected\def\yquant@langhelper@validate@i#1\relax#2\yquant@end{%
   % if the valid value was not user-delimited by \relax, #1 and #2 are empty
   % if the valid value was user-delimited by \relax, #1 is empty and #2 is \relax
   \let\ifvalid=\iffalse%
   \ifstrempty{#1}{%
      \def\tmp{#2}%
      \ifx\empty\tmp%
         \csletcs{ifvalid}{iftrue}%
      \else%
         \ifx\yquant@langhelper@validate@relax\tmp%
            \csletcs{ifvalid}{iftrue}%
         \fi%
      \fi%
   }\relax%
   % eat the rest (which should be empty anyway)
}