Mike Gerwitz

Activist for User Freedom

summaryrefslogtreecommitdiffstats
blob: 4859e9113b512395e68c1f04aa8e44713e5b1f8f (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
% LoVullo rater specification params
%%

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{raterspec/param}
\ProcessOptions\relax


% special keywords for base param types
\def\@coreptype{core}
\def\@enumptype{enum}
\def\@unionptype{union}

\def\typedef{%
  \begingroup% ended by \@dotypedef
  \catcode`\_=11\relax
  \@dotypedef
}
\def\@dotypedef#1 #2: #3 \par{%
  \goodbreak%
  \index{parameter type!#2@\texttt{#2}|(}%
  \begingroup%
    % given foo<bar>, get foo and bar separately
    \edef\@root{\@ptype@root#1<||}%
    \edef\@type{\@ptype@type#1<#1>||}%
    \expandafter\gdef\csname ptype@#2\endcsname{#3}%
    % place definition into aux file; will be available next pass
    \protected@write\@auxout{}{%
      \string\expandafter\string\gdef
      \string\csname\space ptype@#2\string\endcsname{#3}%
    }%
    % typeset definition with link
    \item[%
      \hypertarget{@ptype:#2}{#2}%
      \ifx\@root\@coreptype\else%
        \ $\in\textrm{\rm\@type}$%
      \fi%
    ]\hfill\\%
    \@margin@sig%
    % ensure that the parent type actually exists
    \ifx\@root\@coreptype\else%
      \expandafter\ifx\csname ptype@\@type\endcsname\relax%
        \PackageTenativeError{raterspec}{%
          unknown parent param type `\@type' for `#2'}
      \fi%
    \fi%
    % process any keywords
    \ifx\@root\@enumptype
      \aftergroup\@typedef@enum
    \else\ifx\@root\@unionptype
      \aftergroup\@typedef@union
    \fi\fi
    \index{parameter type!#2@\texttt{#2}|)}%
    #3\par
  \endgroup
  \endgroup% from \typedef
}

% given a type foo<bar>, returns foo
\def\@ptype@root#1<#2||{#1}
% given a type foo<bar>, returns bar
\def\@ptype@type#1<#2>#3||{#2}

% strips the description and then puts all enums in an aligned environment
\def\@typedef@enum#1\enumv#2\par{%
  #1%
  \vskip0em\relax
  \begingroup
    \small
    % provide some extra give to prevent this from appearing on its own line
    \vspace{0em plus 0.5\parskip}%
    \goodbreak
    {\bf Defined values:}\\
    \vspace{0.5\parskip}%
    \begin{tabular}{lc|l}
      % restore the enumv that we gobbled up
      \enumv#2
    \end{tabular}
  \endgroup
}
\def\enumv[#1]#2#3{%
  % define constant
  \expandafter\gdef\csname const@#2\endcsname{#1}%
  \expandafter\gdef\csname const@value@#2\endcsname{#3}%
  % place definition into aux file; will be available next pass
  \protected@write\@auxout{}{%
    \string\expandafter\string\gdef
    \string\csname\space const@#2\string\endcsname{#1}%
    \string\expandafter\string\gdef
    \string\csname\space constvalue@#2\string\endcsname{#3}%
  }%
  % typeset
  \index{constant!{\tt#2}}%
  {\tt\hypertarget{@const:#2}{#2}} & $#3$ & #1 \\
}

% strips the description and then puts all enums in an aligned environment
\def\@typedef@union#1\unionv#2\par{%
  #1%
  \vskip0em\relax
  \begingroup
    \small
    {\bf Any defined value of:}%
    \vspace{-\parskip}%
    \begin{itemize}
      \setlength{\itemsep}{-\parskip}%
      % restore the unionv that we gobbled up
      \unionv#2
    \end{itemize}
  \endgroup
}
\def\unionv#1{%
  % does this type exist?
  \expandafter\ifx\csname ptype@#1\endcsname\relax%
  \PackageTenativeError{raterspec}{unknown param type `#1' for union}
  \fi%
  \item{\typeref{#1}}
}

\def\typeref#1{%
  \index{parameter type!#1@\texttt{#1}}%
  \hyperlink{@ptype:#1}{\tt#1}%
}
\let\type\typeref

\def\constref #1{%
  \@ifnumeric{#1}%
    % numeric constants are simply output with no hyperlink
    $#1$%
  \else
    \index{constant!{\tt#1}}\hyperlink{@const:#1}{\tt#1}%
  \fi
}
\let\const\constref

% constant reference, show description
\def\constrefd#1{%
  \@ifnumeric{#1}%
    % numeric constants are simply output with no hyperlink
    $#1$%
  \else
    \index{constant!{\tt#1}}%
    \hyperlink{@const:#1}%
    {\csname const@#1\endcsname}%
  \fi
}
\def\constrefdd#1{\constrefd{#1}\dotfill}
% constant reference, show value
\def\constrefv#1{%
  \@ifnumeric{#1}%
    % numeric constants are simply output with no hyperlink
    $#1$%
  \else
    \index{constant!{\tt#1}}%
    \hyperlink{@const:#1}%
    {\csname const@value@#1\endcsname}%
  \fi
}
% link to constant with alternate text
\def\constrefalt#1#2{%
  \index{constant!{\tt#1}}%
  \hyperlink{@const:#1}{#2}%
}

\def\@type@exists@err#1{{%
  \expandafter\let\expandafter\@chk\csname ptype@#1\endcsname
  \ifx\@chk\relax
  \PackageTenativeError{raterspec}{parameter `#1' is undefined}%
  \fi
}}
\def\@const@exists@err#1{{%
  % ignore numeric constants (lazy check; anything that begins with a number)
  \@ifnumeric{#1}\else
    \expandafter\let\expandafter\@chk\csname const@#1\endcsname
    \ifx\@chk\relax
      \PackageTenativeError{raterspec}{constant `#1' is undefined}%
    \fi
  \fi
}}

\def\@const@desc#1{{%
  \@ifnumeric{#1}%
    % just output the text
    $#1$%
  \else
    % output desc
    \csname const@#1\endcsname
  \fi
}}


% denotes a param that will likely be calculated
\def\metaparamdef{\paramdef}


% param definition
% note that the description is included in this input as well; this allows us to
% use this information for code generation/etc
\def\paramdef{%
  \begingroup
  \catcode`_=11\relax
  \@doparamdef
}
\def\@doparamdef#1 #2: #3 \par{%
  \goodbreak%
  \index{parameter!#2@\texttt{#2}|(}%
  \begingroup%
    \def\@given{#1}%
    % given foo<bar>, get foo and bar separately
    \edef\@root{\@ptype@root#1<||}%
    \edef\@type{\@ptype@type#1<#1>||}%
    \expandafter\gdef\csname param@#2\endcsname{#3}%
    % place definition into aux file; will be available next pass
    \protected@write\@auxout{}{%
      \string\expandafter\string\gdef
      \string\csname\space param@#2\string\endcsname{#3}%
    }%
    % does the given type exist (note that this check assumes that the typedef
    % appears before the param in the specs, as is currently the case)
    \expandafter\ifx\csname ptype@\@type\endcsname\relax%
      \PackageTenativeError{raterspec}{unknown param type: \@type}
    \fi%
    % typeset definition with link
    \item[%
      \hypertarget{@param:#2}{{\tt#2}}%
      \ $\in\textrm{%
        \rm\typeref{\@type}%
        % output root only if provided (if not, then because of how \@type
        % called \@ptype@type, it will be equal to #1)
        \ifx\@root\@given\else%
          \ $\langle$\@root$\rangle$
        \fi
      }$%
    ]\hfill\\%
    \@margin@sig%
    % remove mysterious space from margin sig
    \hskip-1ex\relax%
    #3%
    \ifx\@root\@given
      \@codegen{<lv:param name="#2" type="\@type" desc="#3"/>}%
    \else
      \@codegen{<lv:param name="#2" type="\@type" set="\@root"%
        \space desc="#3"/>%
      }%
    \fi
  \endgroup%
  \index{parameter!#2@\texttt{#2}|)}%
  \endgroup
}

\def\paramref{%
  \begingroup% ended by \@dotypedef
  \catcode`\_=11\relax
  \@doparamref
}
\def\@doparamref#1{%
  % prevent scantokens from adding a space at the end
  \endlinechar-1\relax
  \expandafter\let\expandafter\@@chk\csname param@#1\endcsname
  \ifx\@@chk\relax
    \PackageTenativeError{raterspec}{unknown param: #1}
  \fi
  \index{parameter!#1@\texttt{#1}}%
  \hyperlink{@param:#1}%
    {\tt\scantokens{#1}}%
  \endgroup
}
\let\param\paramref

\def\paramrefd#1{%
  \expandafter\let\expandafter\@@pchk\csname param@#1\endcsname
  \ifx\@@pchk\empty
    \PackageTenativeError{raterspec}{Unknown parameter: #1}%
  \else
    % output
    \@@pchk
  \fi
}

\def\locparam#1{%
  \ifmmode
    \mathop{\paramref{#1}_\loc}
  \else
    $\locparam{#1}$%
  \fi
}