-
Notifications
You must be signed in to change notification settings - Fork 7
/
preamble-saildoc-macros.tex
280 lines (259 loc) · 8.87 KB
/
preamble-saildoc-macros.tex
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
\makeatletter
\newcommand{\makesailcmds@core}[2]{%
\input{#2/commands.tex}
\ea\newcommand\csname #1code\endcsname[1]{%
\csname #1fcl##1execute\endcsname%
\bigskip%
}%
\ea\WithSuffix\ea\newcommand\csname #1code\endcsname*[1]{%
\csname #1fcl##1execute\endcsname%
}%
\ea\newcommand\csname #1valandfun\endcsname[1]{%
\csname #1##1\endcsname \csname #1fn#1\endcsname%
}%
}
% We could have sail macro call us back for more formatting flexibility.
%\newcommand{\saildescribe}[2]{
% \lstinputlisting[language=sail]{#2}
%
% \hangindent=\parindent #1
%}
% The following macros define how we would like sail code to be documented.
% There is one per category of sail top-level (val spec, typedef, function, function clause etc)
% currently we only use val and fcl.
% They are called by latex generated by sail with
% #1 the latex for any doc-comment from the sail
% #2 a lstinputlisting invocation that
\newcommand{\saildocval}[2]{%
#2%
\par%
\hangindent=\parindent #1%
\medskip%
}
\newcommand{\saildocfcl}[2]{%
#1 #2%
}
\newcommand{\saildocfn}[2]{%
#1 #2%
}
\newcommand{\saildoctype}[2]{%
#1 #2%
}
\newcommand{\@saildoclabelled@capture}[2]{%
\global\def\@saildoclabelled@name{#1}%
\global\def\@saildoclabelled@body{#2}%
}
\newcommand{\@saildocfcl@capture}[2]{%
\global\def\@saildocfcl@doc{#1}%
\global\def\@saildocfcl@fcl{#2}%
}
\newcommand{\@saildoc@makeerrcmd}[1]{%
\ea\def\csname #1@error\endcsname{%
\GenericError{[saildoc] }{Missing definition}{%
[saildoc] \@backslashchar#1 should have been defined.\MessageBreak%
Check your Sail version if you re-generated the LaTeX.%
}{}%
}%
}
\@saildoc@makeerrcmd{@saildoclabelled@name}
\@saildoc@makeerrcmd{@saildoclabelled@body}
\@saildoc@makeerrcmd{@saildocfcl@doc}
\@saildoc@makeerrcmd{@saildocfcl@fcl}
\newcommand{\@saildoc@makeforbiddenseccmd}[1]{%
\ea\def\csname @saildoc@#1\ea\endcsname{%
\GenericError{[saildoc] }{Forbidden command}{%
[saildoc] \@backslashchar#1 is not allowed.%
}{}%
}%
}
% Always use starred variant
\newcommand{\@saildoc@makenestedseccmd}[2]{%
\ea\def\csname @saildoc@#1\endcsname{%
\@ifstar{}{}\csname #2\endcsname*%
}%
}
\@saildoc@makeforbiddenseccmd{part}
\@saildoc@makeforbiddenseccmd{chapter}
\@saildoc@makeforbiddenseccmd{section}
% subsection is special (but still maps to subsubsection); see below
\@saildoc@makenestedseccmd{subsubsection}{paragraph}
\@saildoc@makenestedseccmd{paragraph}{subparagraph}
\@saildoc@makeforbiddenseccmd{subparagraph}
\let\@saildoc@subsection@allowed\@empty
\newcommand{\@saildoc@subsection@allow}[1]{%
\ea\def\ea\@saildoc@subsection@allowed\ea{%
\@saildoc@subsection@allowed%
\@saildoc@subsection@allowed@iter{#1}%
}%
}
\@saildoc@subsection@allow{Description}
\@saildoc@subsection@allow{Exceptions}
\@saildoc@subsection@allow{Notes}
\newcommand{\@saildoc@subsection@valid}[1]{}
\newcommand{\@saildoc@subsection@invalid}[1]{%
\GenericError{[saildoc] }{Invalid subsection}{%
[saildoc] `#1' is not a valid subsection.%
}{}%
}
\newcommand{\@saildoc@subsection@duplicate}[1]{%
\GenericError{[saildoc] }{Duplicate subsection}{%
[saildoc] `#1' is defined more than once.%
}{}%
}
\newcommand{\@saildoc@subsection@validate}[1]{{%
\def\@saildoc@subsection@allowed@iter##1{%
\ifthenelse{\equal{#1}{##1}}{%
\let\@saildoc@subsection@validate@action\@saildoc@subsection@valid%
}{%
}%
}%
\ea\ifx\csname @saildoc@subsection@body@#1\endcsname\relax%
\let\@saildoc@subsection@validate@action\@saildoc@subsection@invalid%
\@saildoc@subsection@allowed%
\else%
\let\@saildoc@subsection@validate@action\@saildoc@subsection@duplicate%
\fi%
\@saildoc@subsection@validate@action{#1}%
}}
\NewEnviron{@saildoc@subsection}[1]{%
\@saildoc@subsection@validate{#1}%
\ea\ea\ea\global\ea\ea\ea\def\ea\csname @saildoc@subsection@body@#1\ea\endcsname\ea{\BODY}%
}
\newcommand{\@saildoc@subsection@print}[1]{%
\ea\ifx\csname @saildoc@subsection@body@#1\endcsname\relax%
\else%
\ea\ifx\csname @saildoc@subsection@body@#1\endcsname\@empty%
\else%
\subsubsection*{#1}%
\csname @saildoc@subsection@body@#1\endcsname%
\fi%
\fi%
}
\newcommand{\@saildoc@subsection@clear}{{%
\def\@saildoc@subsection@allowed@iter##1{%
\ea\global\ea\let\csname @saildoc@subsection@body@##1\endcsname\@undefined%
}%
\@saildoc@subsection@allowed%
}}
\newcommand{\@saildoc@xpatchcmd@repeat}[3]{%
\xpatchcmd{#1}{#2}{#3}{\@saildoc@xpatchcmd@repeat{#1}{#2}{#3}}{}%
}
\newcommand{\@saildoc@environ@guard}[2]{%
% See \makesailcmds for why this space is needed
#1{#2} %
}
\newcommand{\@saildoc@textbf}[1]{%
\ifcsname @capperm@\detokenize{#1}\endcsname%
\csname @capperm@\detokenize{#1}\endcsname%
\else%
\textbf{#1}%
\fi%
}
\newcommand{\makesailcmds}[2]{%
\makesailcmds@core{#1}{#2}%
\ea\newcommand\csname #1isarefbody\endcsname[1]{{%
%
% Given:
%
% \saildoclabelled{foo}{\saildocfcl{bar}{baz}}
%
% we expand to capture foo, and expand the second argument again to capture
% bar and baz.
%
\global\let\@saildoclabelled@name\@saildoclabelled@name@error%
\global\let\@saildoclabelled@body\@saildoclabelled@body@error%
\global\let\@saildocfcl@doc\@saildocfcl@doc@error%
\global\let\@saildocfcl@fcl\@saildocfcl@fcl@error%
%
\let\saildoclabelled\@saildoclabelled@capture%
\let\saildocfcl\@saildocfcl@capture%
%
\csname #1code\endcsname*{##1}%
\@saildoclabelled@body%
%
\ea\ea\ea\ifx\ea\ea\ea\relax\ea\detokenize\ea{\@saildocfcl@doc}\relax%
\GenericWarning{}{#1 Warning: `##1` is not documented}%
\fi%
%
% Now for the fcl body, rewrite:
%
% Foo
% \subsection*{Exceptions}
% Bar
% \subsection*{Notes}
% Baz
%
% to:
%
% \@saildoc@environ@guard{\begin{@saildoc@subsection}}{Description}
% Foo
% \end{@saildoc@subsection}
% \@saildoc@environ@guard{\begin{@saildoc@subsection}}{Exceptions}
% Bar
% \end{@saildoc@subsection}
% \@saildoc@environ@guard{\begin{@saildoc@subsection}}{Notes}
% Baz
% \end{@saildoc@subsection}
%
% as well as using \@saildoc@subsubsection etc for all the other section
% commands. We allow the non-starred \subsection too.
%
% The extra space inserted by \@saildoc@environ@guard is required to avoid:
%
% \begin{@saildoc@subsection}{Foo}\end{@saildoc@subsection}
%
% as the lack of a token before \end confuses environ and makes it split
% Foo into argument "F" and body "oo". The space gets stripped away so
% \BODY will be empty.
%
\xpretocmd{\@saildocfcl@doc}{\@saildoc@environ@guard{\begin{@saildoc@subsection}}{Description}}{}{}%
\@saildoc@xpatchcmd@repeat{\@saildocfcl@doc}{\part}{\@saildoc@part}%
\@saildoc@xpatchcmd@repeat{\@saildocfcl@doc}{\chapter}{\@saildoc@chapter}%
\@saildoc@xpatchcmd@repeat{\@saildocfcl@doc}{\section}{\@saildoc@section}%
\@saildoc@xpatchcmd@repeat{\@saildocfcl@doc}{\subsection*}{\end{@saildoc@subsection}\@saildoc@environ@guard{\begin{@saildoc@subsection}}}%
\@saildoc@xpatchcmd@repeat{\@saildocfcl@doc}{\subsection}{\end{@saildoc@subsection}\@saildoc@environ@guard{\begin{@saildoc@subsection}}}%
\@saildoc@xpatchcmd@repeat{\@saildocfcl@doc}{\subsubsection}{\@saildoc@subsubsection}%
\@saildoc@xpatchcmd@repeat{\@saildocfcl@doc}{\paragraph}{\@saildoc@paragraph}%
\@saildoc@xpatchcmd@repeat{\@saildocfcl@doc}{\subparagraph}{\@saildoc@subparagraph}%
\xapptocmd{\@saildocfcl@doc}{\end{@saildoc@subsection}}{}{}%
%
% We also want to format various special names in our own way, all of which
% currently use \textbf in the saildoc output.
%
\@saildoc@xpatchcmd@repeat{\@saildocfcl@doc}{\textbf}{\@saildoc@textbf}%
%
% Now we have the right \begin and \end macros, with the latter directly
% visible to environ without any expansion, we can capture their contents
% by expanding again.
%
\@saildocfcl@doc%
%
% Finally reassemble the documentation in the right order with the Sail in
% the right place. We use \csuse to avoid having to pre-initialise
% everything to \@empty.
%
% Also add a label so that instruction references from saildoc resolve
% correctly. This label is not added by the saildoc generator so we insert
% it manually here using the sail mangling: <prefix>z<insnname>. This is
% really a valspec mangling, which allows us to link to the description
% rather than the function body and so saildoc's inability to reference
% function clauses in markdown turns out to be useful.
%
\label{#1z##1}%
\@saildoc@subsection@print{Description}%
%
\subsubsection*{Semantics}%
\phantomsection%
\label{\@saildoclabelled@name}%
\noindent\@saildocfcl@fcl%
%
\@saildoc@subsection@print{Exceptions}%
%
\@saildoc@subsection@print{Notes}%
%
% Reset state for next time
%
\@saildoc@subsection@clear%
}}%
}
\makeatother