| 1 |
% \begin{meta-comment} |
| 2 |
% |
| 3 |
% syntax.dtx |
| 4 |
% |
| 5 |
% Syntax typesetting package for LaTeX 2e |
| 6 |
% |
| 7 |
% (c) 1996 Mark Wooding |
| 8 |
% |
| 9 |
% \end{meta-comment} |
| 10 |
% |
| 11 |
% \begin{meta-comment} <general public licence> |
| 12 |
%% |
| 13 |
%% syntax package -- typesetting syntax descriptions |
| 14 |
%% Copyright (c) 1996 Mark Wooding |
| 15 |
%% |
| 16 |
%% This program is free software; you can redistribute it and/or modify |
| 17 |
%% it under the terms of the GNU General Public License as published by |
| 18 |
%% the Free Software Foundation; either version 2 of the License, or |
| 19 |
%% (at your option) any later version. |
| 20 |
%% |
| 21 |
%% This program is distributed in the hope that it will be useful, |
| 22 |
%% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 |
%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 |
%% GNU General Public License for more details. |
| 25 |
%% |
| 26 |
%% You should have received a copy of the GNU General Public License |
| 27 |
%% along with this program; if not, write to the Free Software |
| 28 |
%% Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 29 |
%% |
| 30 |
% \end{meta-comment} |
| 31 |
% |
| 32 |
% \begin{meta-comment} <Package preamble> |
| 33 |
%<+package>\NeedsTeXFormat{LaTeX2e} |
| 34 |
%<+package>\ProvidesPackage{syntax} |
| 35 |
%<+package> [1996/05/17 1.07 Syntax typesetting (MDW)] |
| 36 |
% \end{meta-comment} |
| 37 |
% |
| 38 |
% \CheckSum{1439} |
| 39 |
%% \CharacterTable |
| 40 |
%% {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z |
| 41 |
%% Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z |
| 42 |
%% Digits \0\1\2\3\4\5\6\7\8\9 |
| 43 |
%% Exclamation \! Double quote \" Hash (number) \# |
| 44 |
%% Dollar \$ Percent \% Ampersand \& |
| 45 |
%% Acute accent \' Left paren \( Right paren \) |
| 46 |
%% Asterisk \* Plus \+ Comma \, |
| 47 |
%% Minus \- Point \. Solidus \/ |
| 48 |
%% Colon \: Semicolon \; Less than \< |
| 49 |
%% Equals \= Greater than \> Question mark \? |
| 50 |
%% Commercial at \@ Left bracket \[ Backslash \\ |
| 51 |
%% Right bracket \] Circumflex \^ Underscore \_ |
| 52 |
%% Grave accent \` Left brace \{ Vertical bar \| |
| 53 |
%% Right brace \} Tilde \~} |
| 54 |
%% |
| 55 |
% |
| 56 |
% \begin{meta-comment} <driver> |
| 57 |
% |
| 58 |
%<*driver> |
| 59 |
% |
| 60 |
% This hacking will remember the old default underscore character. Even if |
| 61 |
% T1 fonts are being used, it will get the grotty version. Why is it that |
| 62 |
% all of the encoding handling ends up looking like this? |
| 63 |
% |
| 64 |
\expandafter\let\expandafter\oldus\csname?\string\textunderscore\endcsname |
| 65 |
% |
| 66 |
\input{mdwtools} |
| 67 |
\describespackage{syntax} |
| 68 |
\DeclareRobustCommand\syn{\package{syntax}} |
| 69 |
\mdwdoc |
| 70 |
%</driver> |
| 71 |
% |
| 72 |
% \end{meta-comment} |
| 73 |
% |
| 74 |
% \section{User guide} |
| 75 |
% |
| 76 |
% \subsection{Introduction} |
| 77 |
% |
| 78 |
% The \syn\ package provides a number of commands and environments which |
| 79 |
% extend \LaTeX\ and allow you to typeset good expositions of syntax. |
| 80 |
% |
| 81 |
% The package provides several different types of features: probably not all |
| 82 |
% of these will be required by every document which needs the package: |
| 83 |
% \begin{itemize} |
| 84 |
% \item A system of abbreviated forms for typesetting syntactic items. |
| 85 |
% \item An environment for typesetting BNF-type grammars |
| 86 |
% \item A collection of environments for building syntax diagrams. |
| 87 |
% \end{itemize} |
| 88 |
% |
| 89 |
% The package also includes some other features which, while not necessarily |
| 90 |
% syntax-related, will probably come in handy for similar types of document: |
| 91 |
% \begin{itemize} |
| 92 |
% \item An abbreviated notation for verbatim text, similar to the |
| 93 |
% \package{shortvrb} package. |
| 94 |
% \item A slightly different underscore character, which works as expected |
| 95 |
% in text and maths modes. |
| 96 |
% \end{itemize} |
| 97 |
% |
| 98 |
% \subsection{The abbreviated verbatim notation} |
| 99 |
% |
| 100 |
% In documents describing programming languages and libraries, it can become |
| 101 |
% tedious to type "\verb|...|" every time. Like Frank Mittelbach's |
| 102 |
% \package{shortvrb} package, \syn\ provides a way of setting up single-^^A |
| 103 |
% character abbreviations. The only real difference between the two is that |
| 104 |
% the declarations provided by \syn\ obey \LaTeX's normal scoping rules. |
| 105 |
% |
| 106 |
% \DescribeMacro\shortverb |
| 107 |
% You can set up a character as a `verbatim shorthand' character using the |
| 108 |
% |\shortverb| command. This takes a single argument, which should be a |
| 109 |
% single-character control sequence containing the character you want to use. |
| 110 |
% So, for example, the command |
| 111 |
% \begin{listing} |
| 112 |
%\shortverb{\|} |
| 113 |
% \end{listing} |
| 114 |
% would set up the `"|"' character to act as a verbatim delimiter. While a |
| 115 |
% |\shortverb| declaration is in force, any text surrounded by (in this case) |
| 116 |
% vertical bar characters will be typeset as if using the normal |\verb| |
| 117 |
% command. |
| 118 |
% |
| 119 |
% \DescribeEnv{shortverb} |
| 120 |
% Since \LaTeX\ allows any declaration to be used as an environment, you can |
| 121 |
% use a \env{shortverb} environment to delimit the text over which your |
| 122 |
% character is active: |
| 123 |
% \begin{listing} |
| 124 |
%Some text... |
| 125 |
%\begin{shortverb}{\|} |
| 126 |
%... |
| 127 |
%\end{shortverb} |
| 128 |
% \end{listing} |
| 129 |
% |
| 130 |
% \DescribeMacro\unverb |
| 131 |
% If you want to disable a |\shortverb| character without ending the scope |
| 132 |
% of other declarations, you can use the |\unverb| command, passing it |
| 133 |
% a character as a control sequence, in the same way as above. |
| 134 |
% |
| 135 |
% The default \TeX/\LaTeX\ underscore character is rather too short for |
| 136 |
% use in identifiers. For example: |
| 137 |
% |
| 138 |
% \begingroup \let\_=\oldus |
| 139 |
% \begin{demo}{Old-style underscores} |
| 140 |
%Typing long underscore-filled |
| 141 |
%names, like big\_function\_name, |
| 142 |
%is normally tedious. The normal |
| 143 |
%positioning of the underscore |
| 144 |
%is wrong, too. |
| 145 |
% \end{demo} |
| 146 |
% \endgroup |
| 147 |
% |
| 148 |
% The \syn\ package redefines the |\_| command to draw a more attractive |
| 149 |
% underscore character. It also allows you to use the |_|~character |
| 150 |
% directly to produce an underscore outside of maths mode: |_|~behaves |
| 151 |
% as a subscript character as usual inside maths mode. |
| 152 |
% |
| 153 |
% \begin{demo}{New \syn\ underscores} |
| 154 |
%You can use underscore-filled |
| 155 |
%names, like big_function_name, |
| 156 |
%simply and naturally. Of |
| 157 |
%course, subscripts still work |
| 158 |
%normally in maths mode, e.g., |
| 159 |
%$x_i$. |
| 160 |
% \end{demo} |
| 161 |
% |
| 162 |
% \subsection{Typesetting syntactic items} |
| 163 |
% \begin{synshorts} |
| 164 |
% |
| 165 |
% The \syn\ package provides some simple commands for typesetting syntactic |
| 166 |
% items. |
| 167 |
% |
| 168 |
% \DescribeMacro\synt |
| 169 |
% Typing "\\synt{"<text>"}" typesets <text> as a \lq non-terminal', |
| 170 |
% in italics and surrounded by angle brackets. If you use "\\synt" a lot, |
| 171 |
% you can use the incantation |
| 172 |
% \begin{listing} |
| 173 |
%\def\<#1>{\synt{#1}} |
| 174 |
% \end{listing} |
| 175 |
% to allow you to type "\\<"<text>">" as an alternative to |
| 176 |
% "\\synt{"<text>"}". |
| 177 |
% |
| 178 |
% \DescribeMacro\lit |
| 179 |
% You can also display literal text, which the reader should type directly, |
| 180 |
% using the "\\lit" command. |
| 181 |
% |
| 182 |
% \begin{demo}{Use of \cmd\lit} |
| 183 |
%Type \lit{ls} to display a |
| 184 |
%list of files. |
| 185 |
% \end{demo} |
| 186 |
% |
| 187 |
% Note that the literal text appears in quotes. To suppress the quotes, |
| 188 |
% use the `*' variant. |
| 189 |
% |
| 190 |
% The "\\lit" command produces slightly better output than "\\verb" for |
| 191 |
% running text, since the spaces are somewhat narrower. However, "\\verb" |
| 192 |
% allows you to type arbitrary characters, which are treated literally, |
| 193 |
% whereas you must use commands such as "\\{" to use special characters |
| 194 |
% within the argument to "\\lit". Of course, you can use "\\lit" anywhere |
| 195 |
% in the document: "\\verb" mustn't be used inside a command argument. |
| 196 |
% \end{synshorts} |
| 197 |
% |
| 198 |
% \subsection{Abbreviated forms for syntactic items} |
| 199 |
% |
| 200 |
% It would be very tedious to require the use of commands like |\synt| |
| 201 |
% when building syntax descriptions like BNF grammars. It would also make |
| 202 |
% your \LaTeX\ source hard to read. Therefore, \syn\ provides some |
| 203 |
% abbreviated forms which make typesetting syntax quicker and easier. |
| 204 |
% |
| 205 |
% Since the abbreviated forms use several characters which you may want to |
| 206 |
% use in normal text, they aren't enabled by default. They only work |
| 207 |
% with special commands and environments provided by the \syn\ package. |
| 208 |
% |
| 209 |
% The abbreviated forms are shown in the table below: |
| 210 |
% |
| 211 |
% \begin{tab}[\synshorts]{ll} \hline |
| 212 |
% \bf Input & \bf Output \\ \hline |
| 213 |
% "<some text>" & <some text> \\ |
| 214 |
% "`some text'" & `some text' \\ |
| 215 |
% "\"some text\"" & "some text" \\ \hline |
| 216 |
% \end{tab} |
| 217 |
% |
| 218 |
% Within one of these abbreviated forms, text is treated more-or-less |
| 219 |
% verbatim: |
| 220 |
% \begin{itemize} |
| 221 |
% |
| 222 |
% \item Any |$|, |%|, |^|, |&|, |{|, |}|, |~| or |#| characters are treated |
| 223 |
% literally: their normal special meanings are ignored. |
| 224 |
% |
| 225 |
% \item Other special characters, with the exception of |\|, are also treated |
| 226 |
% literally: this includes any characters made special by |\shortverb|. |
| 227 |
% |
| 228 |
% \end{itemize} |
| 229 |
% |
| 230 |
% However, the |\| character retains its meaning. Since the brace |
| 231 |
% characters are not recognised, most commands can't be used within |
| 232 |
% abbreviated forms. However, you can use special commands to type some |
| 233 |
% of the remaining special characters: |
| 234 |
% |
| 235 |
% \begin{tab}[\synshorts]{ll} \hline |
| 236 |
% \bf Command & \bf Result \\ \hline |
| 237 |
% "\\\\" & A `\\' character \\ |
| 238 |
% "\\>" & A `>' character \\ |
| 239 |
% "\\'" & A `\'' character \\ |
| 240 |
% "\\\"" & A `"' character \\ |
| 241 |
% "\\\ " & A `\ ' character (not a space) \\ \hline |
| 242 |
% \end{tab} |
| 243 |
% |
| 244 |
% Note that |\\|, |\>|, |\"| and \verb*|\ | are only useful in a |\tt| font, |
| 245 |
% i.e., inside |`...'| and |"..."| forms, since the characters don't exist |
| 246 |
% in normal fonts. The |\>|, |\"| and |\'| commands are only provided so |
| 247 |
% you can use these characters within |<...>|, |"..."| and |`...'| forms |
| 248 |
% respectively: in the other forms, there is no need to use the special |
| 249 |
% command. |
| 250 |
% |
| 251 |
% In addition, when the above abbreviations are enabled, the character "|" |
| 252 |
% is set to typeset a \syntax{|} symbol, which is conventionally used to |
| 253 |
% separate alternatives in syntax descriptions. |
| 254 |
% |
| 255 |
% \DescribeMacro\syntax |
| 256 |
% Normally, these abbreviated forms are enabled only within special |
| 257 |
% environments, such as \env{grammar} and \env{syntdiag}. To use them |
| 258 |
% in running text, use the |\syntax| command. The abbreviations are made |
| 259 |
% active within the argument of the |\syntax| command.\footnote{^^A |
| 260 |
% The argument of the \cmd\syntax\ command may contain commands such |
| 261 |
% as \cmd\verb, which are normally not allowed within arguments. |
| 262 |
% } Note that you cannot use the |\syntax| command within the argument |
| 263 |
% of another command. |
| 264 |
% |
| 265 |
% \DescribeMacro\synshorts |
| 266 |
% \DescribeEnv{synshorts} |
| 267 |
% You can also enable the syntax shortcuts using the |\synshorts| declaration |
| 268 |
% or the \env{synshorts} environment. This enables the syntax shortcuts |
| 269 |
% until the scope of the declaration ends. |
| 270 |
% |
| 271 |
% \DescribeMacro\synshortsoff |
| 272 |
% If syntax shortcuts are enabled, you can disable them using the |
| 273 |
% |\synshortsoff| declaration. |
| 274 |
% |
| 275 |
% \subsection{The \env{grammar} environment} |
| 276 |
% |
| 277 |
% \DescribeEnv{grammar} |
| 278 |
% For typesetting formal grammars, for example, of programming languages, |
| 279 |
% the \syn\ package provides a \env{grammar} environment. Within this |
| 280 |
% environment, the abbreviated forms described above are enabled. |
| 281 |
% |
| 282 |
% Within the environment, separate production rules should be separated by |
| 283 |
% blank lines. You can use the normal |\\| command to perform line-breaking |
| 284 |
% of a production rule. Note that a production rule must begin with a |
| 285 |
% nonterminal name enclosed in angle brackets (|<| \dots |>|), followed by |
| 286 |
% whitespace, then some kind of production operator (usually `::=') and then |
| 287 |
% some more whitespace. You can control how this text is actually typeset, |
| 288 |
% however. |
| 289 |
% |
| 290 |
% \DescribeMacro{\[[} |
| 291 |
% \DescribeMacro{\]]} |
| 292 |
% You can use syntax diagrams (see below) instead of a straight piece of BNF |
| 293 |
% by enclosing it in a |\[[| \dots |\]]| pair. Note that you can't mix |
| 294 |
% syntax diagrams and BNF in a production rule, and you will get something |
| 295 |
% which looks very strange if you try. |
| 296 |
% |
| 297 |
% \DescribeMacro\alt |
| 298 |
% In addition, a command |\alt| is provided for splitting long production |
| 299 |
% rules over several lines: the |\alt| command starts a new line and places |
| 300 |
% a \syntax{|} character slightly in the left margin. This is useful when |
| 301 |
% a symbol has many alternative productions. |
| 302 |
% |
| 303 |
% \begin{demo}[w]{The \env{grammar} environment} |
| 304 |
%\begin{grammar} |
| 305 |
%<statement> ::= <ident> `=' <expr> |
| 306 |
% \alt `for' <ident> `=' <expr> `to' <expr> `do' <statement> |
| 307 |
% \alt `{' <stat-list> `}' |
| 308 |
% \alt <empty> |
| 309 |
% |
| 310 |
%<stat-list> ::= <statement> `;' <stat-list> | <statement> |
| 311 |
%\end{grammar} |
| 312 |
% \end{demo} |
| 313 |
% |
| 314 |
% You can modify the appearance of grammars using three length parameters: |
| 315 |
% |
| 316 |
% \begin{description} \def\makelabel{\hskip\labelsep\cmd} |
| 317 |
% |
| 318 |
% \item [\grammarparsep] is the amount of space inserted between production |
| 319 |
% rules. It is a rubber length whose default value is 8\,pt, with |
| 320 |
% 1\,pt of stretch and shrink. |
| 321 |
% |
| 322 |
% \item [\grammarindent] is the amount by which the right hand side of a |
| 323 |
% production rule is indented from the left margin. It is a rigid |
| 324 |
% length. Its default value is 2\,em. |
| 325 |
% |
| 326 |
% \end{description} |
| 327 |
% |
| 328 |
% \DescribeMacro\grammarlabel |
| 329 |
% You can also control how the `label' is typeset by redefining the |
| 330 |
% |\grammarlabel| command. The command is given two arguments: the name of |
| 331 |
% the nonterminal (which was enclosed in angle brackets), and the `production |
| 332 |
% operator'. The command is expected to produce the label. By default, it |
| 333 |
% typesets the nonterminal name using |\synt| and the operator at opposite |
| 334 |
% ends of the label, separated by an |\hfill|. |
| 335 |
% |
| 336 |
% \subsection{Syntax diagrams} |
| 337 |
% |
| 338 |
% A full formal BNF grammar can be somewhat overwhelming for less technical |
| 339 |
% readers. Documents aimed at such readers tend to display grammatical |
| 340 |
% structures as \emph{syntax diagrams}. |
| 341 |
% |
| 342 |
% \DescribeEnv{syntdiag} |
| 343 |
% A syntax diagram is always enclosed in a \env{syntdiag} environment. You |
| 344 |
% should think of the environment as enclosing a new sort of \LaTeX\ mode: |
| 345 |
% trying to type normal text into a syntax diagram will result in very ugly |
| 346 |
% output. \LaTeX\ ignores spaces and return characters while in syntax |
| 347 |
% diagram mode. |
| 348 |
% |
| 349 |
% The syntax of the environment is very simple: |
| 350 |
% |
| 351 |
% \begin{grammar} |
| 352 |
% <synt-diag-env> ::= \[[ |
| 353 |
% "\\begin{syntdiag}" |
| 354 |
% \begin{stack} \\ "[" <decls> "]" \end{stack} |
| 355 |
% <text> |
| 356 |
% "\\end{syntdiag}" |
| 357 |
% \]] |
| 358 |
% \end{grammar} |
| 359 |
% |
| 360 |
% The \<decls> contain any declarations you want to insert, to control |
| 361 |
% the environment. The parameters to tweak are described below. |
| 362 |
% |
| 363 |
% Within a syntax diagram, you can include syntactic items using the |
| 364 |
% abbreviated forms described elsewhere. The output from these forms is |
| 365 |
% modified slightly in syntax diagram mode so that the diagram looks |
| 366 |
% right. |
| 367 |
% |
| 368 |
% I probably ought to point out now that the syntax diagram typesetting |
| 369 |
% commands produce beautiful-looking diagrams with all the rules and curves |
| 370 |
% accurately positioned. Some device drivers don't position these objects |
| 371 |
% correctly in their output. I've had particular trouble with |dvips|. I'll |
| 372 |
% say it again: it's not my fault! |
| 373 |
% |
| 374 |
% \DescribeEnv{syntdiag*} |
| 375 |
% The \env{syntdiag} environment only works in paragraph mode, and it acts |
| 376 |
% rather like a paragraph, splitting over several lines when appropriate. |
| 377 |
% If you just want to typeset a snippet of a syntax diagram, you can |
| 378 |
% use the starred environment \env{syntdiag$*$}. |
| 379 |
% |
| 380 |
% \begin{grammar} |
| 381 |
% <synt-diag-star-env> ::= \[[ |
| 382 |
% "\\begin{syntdiag*}" |
| 383 |
% \begin{stack} \\ "[" <decls> "]" \end{stack} |
| 384 |
% \begin{stack} \\ "[" <width> "]" \end{stack} |
| 385 |
% <text> |
| 386 |
% "\\end{syntdiag*}" |
| 387 |
% \]] |
| 388 |
% \end{grammar} |
| 389 |
% |
| 390 |
% When typesetting little demos like this, it's not normal to fully adorn |
| 391 |
% the syntax diagram with the full double arrows |
| 392 |
% (`\begin{syntdiag*}[\left{>>-}\right{-><}]\tok{$\cdots$}\end{syntdiag*}'). |
| 393 |
% The two declarations \syntax{"\\left{"<arrow>"}" and "\\right{"<arrow>"}"} |
| 394 |
% allow you to choose the arrows on each side of the syntax diagram snippet. |
| 395 |
% The possible values of \<arrow> are shown in the table-ette below: |
| 396 |
% |
| 397 |
% ^^A Time to remember what I learned about tables while writing mdwtab. |
| 398 |
% ^^A Just for the embarassment factor, here's the number of attempts I |
| 399 |
% ^^A took to get the table below to look right: __6. Hmm... not as bad |
| 400 |
% ^^A as I expected. Most of them were fine-tuning things. |
| 401 |
% |
| 402 |
% \medskip ^^A Leave a vertical gap |
| 403 |
% \hbox to\columnwidth{\hfil\vbox{\tabskip=0pt ^^A Centre it horizontally |
| 404 |
% \sdsize \csname sd@setsize\endcsname ^^A Position syntdiag arrows |
| 405 |
% \halign to .5\columnwidth{ ^^A Set the table width |
| 406 |
% &\ttfamily\ignorespaces#\unskip\hfil\tabskip=0pt ^^A Typeset the name |
| 407 |
% &\quad\csname sd@arr@#\endcsname\hfil ^^A Typeset the arrow |
| 408 |
% &\setbox0=\hbox{#}\tabskip=0pt plus 1fil\cr ^^A Stretch between columns |
| 409 |
% >>-&>>-& &>-&>-& &->&->\cr |
| 410 |
% -><&-><& &...&...& &-&-\cr |
| 411 |
% }}\hfil} ^^A Close the boxing |
| 412 |
% \medskip ^^A And leave another gap |
| 413 |
% |
| 414 |
% These declarations should be used only in the optional argument to the |
| 415 |
% \env{syntdiag$*$} command. The second optional argument to the |
| 416 |
% environment, if specified, fixes the width of the syntax diagram snippet; |
| 417 |
% if you omit this argument, the diagram is made just wide enough to |
| 418 |
% fit everything in. |
| 419 |
% |
| 420 |
% \begin{figure} |
| 421 |
% \begin{demo}[w]{Example of \env{syntdiag$*$}} |
| 422 |
%\newcommand{\bs}[2]{% |
| 423 |
% \begin{minipage}{1.6in}% |
| 424 |
% \begin{syntdiag*}[\left{#1}\right{#2}][1.6in]% |
| 425 |
%} |
| 426 |
%\newcommand{\es}{\end{syntdiag*}\end{minipage}} |
| 427 |
% |
| 428 |
%\begin{center} |
| 429 |
%\begin{tabular}{cl} \\ \hline |
| 430 |
%\bf Construction & \bf Meaning \\ \hline |
| 431 |
%\bs {>>-} {...} \es & Start of syntax diagram \\ |
| 432 |
%\bs {...} {-><} \es & End of syntax diagram \\ |
| 433 |
%\bs {>-} {...} \es & Continued on next line \\ |
| 434 |
%\bs {...} {->} \es & Continued from previous line \\ \hline |
| 435 |
%\bs {...} {...} |
| 436 |
% \begin{stack} <option-a> \\ <option-b> \\ <option-c> \end{stack} |
| 437 |
%\es & Alternatives: choose any one \\ |
| 438 |
%\bs {...} {...} |
| 439 |
% \begin{rep} <repeat-me> \\ <separator> \end{rep} |
| 440 |
%\es & One or more items, with separators \\ \hline |
| 441 |
%\end{tabular} |
| 442 |
%\end{center} |
| 443 |
% \end{demo} |
| 444 |
% \end{figure} |
| 445 |
% |
| 446 |
% \DescribeMacro\tok |
| 447 |
% You can also include text using the |\tok| command. The argument of this |
| 448 |
% command is typeset in \LaTeX's LR~mode and inserted into the diagram. |
| 449 |
% Syntax abbreviations are allowed within the argument, so you can, for |
| 450 |
% example, include textual descriptions like |
| 451 |
% \begin{listing} |
| 452 |
%\tok{any <char> except `"'} |
| 453 |
% \end{listing} |
| 454 |
% |
| 455 |
% \DescribeEnv{stack} |
| 456 |
% Within a syntax diagram, a choice between several different items is |
| 457 |
% shown by stacking the alternatives vertically. In \LaTeX, this is done |
| 458 |
% by enclosing the items in a \env{stack} environment. Each individual item |
| 459 |
% is separated by |\\| commands, as in the \env{array} and \env{tabular} |
| 460 |
% environments. Each row may contain any syntax diagram material, including |
| 461 |
% |\tok| commands and other \env{stack} environments. |
| 462 |
% |
| 463 |
% Note if you end a \env{stack} environment with a |\\| command, a blank |
| 464 |
% row is added to the bottom of the stack, indicating that none of the items |
| 465 |
% need be specified. |
| 466 |
% |
| 467 |
% \DescribeEnv{rep} |
| 468 |
% Text which can be repeated is enclosed in a \env{rep} environment: the |
| 469 |
% text is displayed with a backwards pointing arrow drawn over it, showing |
| 470 |
% that it may be repeated. Optionally, you can specify text to be |
| 471 |
% displayed in the arrow, separating it from the main text with a |\\| |
| 472 |
% command. |
| 473 |
% |
| 474 |
% Note that items on the backwards arrow of a \env{rep} construction should |
| 475 |
% be displayed \emph{backwards}. You must put the individual items in |
| 476 |
% reverse order when building this part of your diagrams. \syn\ will |
| 477 |
% correctly reverse the arrows on \env{rep} structures, but apart from |
| 478 |
% this, you must cope on your own. You are recommended to keep these parts |
| 479 |
% of your diagrams as simple as possible to avoid confusing readers. |
| 480 |
% |
| 481 |
% \begin{demo}[w]{A syntax diagram} |
| 482 |
%\begin{syntdiag} |
| 483 |
%<ident> `(' |
| 484 |
% \begin{rep} \begin{stack} \\ |
| 485 |
% <type> \begin{stack} \\ <ident> \end{stack} |
| 486 |
% \end{stack} \\ `,' \end{rep} |
| 487 |
%\begin{stack} \\ `...' \end{stack} `)' |
| 488 |
%\end{syntdiag} |
| 489 |
% \end{demo} |
| 490 |
% |
| 491 |
% \subsubsection{Line breaking in syntax diagrams} |
| 492 |
% |
| 493 |
% Syntax diagrams are automatically broken over lines and across pages. |
| 494 |
% Lines are only broken between items on the outermost level of the diagram: |
| 495 |
% i.e., not within \env{stack} or \env{rep} environments. |
| 496 |
% |
| 497 |
% You can force a line break at a particular place by using the |\\| command |
| 498 |
% as usual. This supports all the usual \LaTeX\ features: a `|*|' variant |
| 499 |
% which prohibits page breaking, and an optional argument specifying the |
| 500 |
% extra vertical space between lines. |
| 501 |
% |
| 502 |
% \subsubsection{Customising syntax diagrams} |
| 503 |
% |
| 504 |
% There are two basic styles of syntax diagrams supported: |
| 505 |
% |
| 506 |
% \begin{description} |
| 507 |
% |
| 508 |
% \item [square] Lines in the syntax diagram join at squared-off corners. |
| 509 |
% This appears to be the standard way of displaying syntax diagrams |
| 510 |
% in IBM manuals, and most other documents I've seen. |
| 511 |
% |
| 512 |
% \item [rounded] Lines curve around corners. Also, no arrows are drawn |
| 513 |
% around repeating loops: the curving of the lines provides this |
| 514 |
% information instead. This style is used in various texts on |
| 515 |
% Pascal, and appears to be more popular in academic circles. |
| 516 |
% |
| 517 |
% \end{description} |
| 518 |
% |
| 519 |
% You can specify the style you want to use for syntax diagrams by giving |
| 520 |
% the style name as an option on the |\usepackage| command. For example, |
| 521 |
% to force rounded edges to be used, you could say |
| 522 |
% |
| 523 |
% \begin{listing} |
| 524 |
%\usepackage[rounded]{syntax} |
| 525 |
% \end{listing} |
| 526 |
% |
| 527 |
% \DescribeMacro\sdsize |
| 528 |
% \DescribeMacro\sdlengths |
| 529 |
% The \env{syntdiag} environment takes an option argument, which should |
| 530 |
% contain declarations which are obeyed while the environment is set up. |
| 531 |
% The default value of this argument is `|\sdsize\sdlengths|'. The |
| 532 |
% |\sdsize| command sets the default type size for the environment: this is |
| 533 |
% normally |\small|. |\sdlengths| sets the values of the length parameters |
| 534 |
% used by the environment based on the current text size. These parameters |
| 535 |
% are described below. |
| 536 |
% |
| 537 |
% For example, if you wanted to reduce the type size of the diagrams still |
| 538 |
% further, you could use the command |
| 539 |
% \begin{listing} |
| 540 |
%\begin{syntdiag}[\tiny\sdlengths] |
| 541 |
% \end{listing} |
| 542 |
% |
| 543 |
% The following length parameters may be altered: |
| 544 |
% |
| 545 |
% \begin{description} \def\makelabel{\hskip\labelsep\cmd} |
| 546 |
% |
| 547 |
% \item [\sdstartspace] The length of the rule between the arrows which |
| 548 |
% begin each line of the syntax diagram and the first item on the line. |
| 549 |
% Note that most objects have some space on either side of them as |
| 550 |
% well. This is a rubber length. Its default value is 1\,em, although |
| 551 |
% it can shrink by up to 10\,pt. |
| 552 |
% |
| 553 |
% \item [\sdendspace] The length of the rule between the last item on a |
| 554 |
% line and the arrow at the very end. Note that the final line also |
| 555 |
% has extra rubber space on the end. This is a rubber length. Its |
| 556 |
% default value is 1\,em, although it will shrink by up to 10\,pt. |
| 557 |
% |
| 558 |
% \item [\sdmidskip] The length of the rule on either side of a large |
| 559 |
% construction (either a \env{stack} or a \env{rep}). It is a rubber |
| 560 |
% length. Its default value is \smallf 1/2\,em, with a very small |
| 561 |
% amount of infinite stretch. |
| 562 |
% |
| 563 |
% \item [\sdtokskip] The length of the rule on either side of a |\tok| |
| 564 |
% item or syntax abbreviation. It is a rubber length. Its default |
| 565 |
% value is \smallf 1/4\,em, with a very small amount of inifnite |
| 566 |
% stretch. |
| 567 |
% |
| 568 |
% \item [\sdfinalskip] The length of the rule which finishes the last line |
| 569 |
% of a syntax diagram. It is a rubber length. Its default value is |
| 570 |
% \smallf 1/2\,em, with 10000\,fil of stretch, which will left-align |
| 571 |
% the items on the line.\footnote{^^A |
| 572 |
% This is a little \TeX nical. The idea is that if a stray 1\,fil |
| 573 |
% of stretch is added to the end of the line, it won't be noticed. |
| 574 |
% However, the alignment of the text on the line can still be |
| 575 |
% modified using \cmd{\sd@rule}\cmd{\hfill}, if you're feeling |
| 576 |
% brave. |
| 577 |
% } |
| 578 |
% |
| 579 |
% \item [\sdrulewidth] Half the width of the rules used in the diagram. |
| 580 |
% It is a rigid length. Its default value is 0.2\,pt. |
| 581 |
% |
| 582 |
% \item [\sdcirclediam] The diameter of the circle from which the quadrants |
| 583 |
% used in rounded-style diagrams are taken. This must be a multiple |
| 584 |
% of 4\,pt, or else the lines on the diagram won't match up. |
| 585 |
% |
| 586 |
% \end{description} |
| 587 |
% |
| 588 |
% In addition, you should call |\sdsetstrut| passing it the total height |
| 589 |
% (\({\rm height}+{\rm depth}\)) of a normal line of text at the current |
| 590 |
% size. Normally, the value of |\baselineskip| will be appropriate. |
| 591 |
% |
| 592 |
% You can also alter the appearance of \env{stack}s and \env{rep}s by using |
| 593 |
% their optional positioning arguments. By default, \env{stack}s descend |
| 594 |
% below the main line of the diagram, and \env{rep}s extend above it. |
| 595 |
% Specifying an optional argument of |[b]| for either environment reverses |
| 596 |
% this, putting \env{stack}s above and \env{rep}s below the line. |
| 597 |
% |
| 598 |
% \subsection{Changing the presentation styles} |
| 599 |
% |
| 600 |
% You can change the way in which the syntax items are typeset by altering |
| 601 |
% some simple commands (using |\renewcommand|). Each item (nonterminals, |
| 602 |
% as typeset by |\synt|, and quoted and unquoted terminals, as typeset by |
| 603 |
% |\lit| and |\lit*|) has two style commands associated with it, as shown |
| 604 |
% in the table below. |
| 605 |
% |
| 606 |
% \begin{tab}{lll} \hline |
| 607 |
% \bf Syntax item & \bf Left command & \bf Right command \\ \hline |
| 608 |
% Nonterminals & |\syntleft| & |\syntright| \\ |
| 609 |
% Quoted terminals & |\litleft| & |\litright| \\ |
| 610 |
% Unquoted terminals & |\ulitleft| & |\ulitright| \\ \hline |
| 611 |
% \end{tab} |
| 612 |
% |
| 613 |
% It's not too hard to see how this works. For example, if you look at |
| 614 |
% the implementation for |\syntleft| and |\syntright| in the implementation |
| 615 |
% section, you'll notice that they're defined like this: |
| 616 |
% \begin{listing} |
| 617 |
%\newcommand{\syntleft}{$\langle$\normalfont\itshape} |
| 618 |
%\newcommand{\syntright}{$\rangle$} |
| 619 |
% \end{listing} |
| 620 |
% I think this is fairly simple, if you understand things like font changing. |
| 621 |
% |
| 622 |
% Note that changing these style commands alters the appearance of all syntax |
| 623 |
% objects of the appropriate types, as created by the |\synt| and |\lit| |
| 624 |
% commands, in \env{grammar} environments, and in syntax diagrams. |
| 625 |
% |
| 626 |
% |
| 627 |
% \section{Change history} |
| 628 |
% |
| 629 |
% \subsection*{Version 1.07} |
| 630 |
% |
| 631 |
% \begin{itemize} |
| 632 |
% \item Fixed problem with underscore hacking in a \env{tabbing} environment. |
| 633 |
% \end{itemize} |
| 634 |
% |
| 635 |
% \subsection*{Version 1.06} |
| 636 |
% |
| 637 |
% \begin{itemize} |
| 638 |
% \item Added style hooks for syntax items. |
| 639 |
% \item Improved colour handling in syntax diagrams, thanks to the |\doafter| |
| 640 |
% package. |
| 641 |
% \item Fixed some nasty bugs in the \env{grammar} environment which confused |
| 642 |
% other lists and ruined the spacing. The \env{grammar} handling is |
| 643 |
% now much tidier in general. |
| 644 |
% \end{itemize} |
| 645 |
% |
| 646 |
% \subsection*{Version 1.05} |
| 647 |
% |
| 648 |
% \begin{itemize} |
| 649 |
% \item Fixed `the bug' in the syntax diagram typesetting. It now breaks |
| 650 |
% lines almost psychically, and doesn't break in the wrong places. |
| 651 |
% \item Almost rewrote the \env{grammar} environment. It now does lots of |
| 652 |
% the list handling itself, to allow more versatile typesetting of the |
| 653 |
% left hand sides. There's lots of evil in there now. |
| 654 |
% \item Added some more configurability. In particular, two new settings |
| 655 |
% have been added to control \env{grammar} environments, and a neat |
| 656 |
% way of adding new syntax diagram structures has been introduced. |
| 657 |
% \end{itemize} |
| 658 |
% |
| 659 |
% \subsection*{Version 1.04} |
| 660 |
% |
| 661 |
% \begin{itemize} |
| 662 |
% \item Changed the vertical positioning of the rules, to make all the text |
| 663 |
% line up properly. While the old version was elegant and simple, it |
| 664 |
% had the drawback of looking nasty. |
| 665 |
% \item Allow line breaks at underscores, but don't if there's another one |
| 666 |
% afterwards. Also, prevent losing following space if underscore is |
| 667 |
% written to a file. |
| 668 |
% \end{itemize} |
| 669 |
% |
| 670 |
% \subsection*{Version 1.02} |
| 671 |
% |
| 672 |
% \begin{itemize} |
| 673 |
% \item Added support for rounded corners in syntax diagrams. |
| 674 |
% \item Changed lots of |\hskip| commands to |\kern|s, to prevent possible |
| 675 |
% line breaks. |
| 676 |
% \end{itemize} |
| 677 |
% |
| 678 |
% \subsection*{Version 1.01} |
| 679 |
% |
| 680 |
% \begin{itemize} |
| 681 |
% \item Allowed disabling of underscore active character, to avoid messing |
| 682 |
% up filenames. |
| 683 |
% \item Added |\grammarparsep| and |\grammarindent| length parameters to |
| 684 |
% control the appearance of grammars. |
| 685 |
% \end{itemize} |
| 686 |
% |
| 687 |
% \implementation |
| 688 |
% |
| 689 |
% \section{Implementation of \syn} |
| 690 |
% |
| 691 |
% \begin{macrocode} |
| 692 |
%<*package> |
| 693 |
% \end{macrocode} |
| 694 |
% |
| 695 |
% \subsection{Options handling} |
| 696 |
% |
| 697 |
% We define all the options we know about, and then see what's been put |
| 698 |
% on the usepackage line. |
| 699 |
% |
| 700 |
% The options we provide currently are as follows: |
| 701 |
% |
| 702 |
% \begin{description} |
| 703 |
% \item [rounded] draws neatly rounded edges on the diagram. |
| 704 |
% \item [square] draws squared-off edges on the diagram. This is the |
| 705 |
% default. |
| 706 |
% \item [nounderscore] disables the undescore active character, The |\_| |
| 707 |
% command still produces the nice version created here. |
| 708 |
% \end{description} |
| 709 |
% |
| 710 |
% \begin{macrocode} |
| 711 |
\DeclareOption{rounded}{\sd@roundtrue} |
| 712 |
\DeclareOption{square}{\sd@roundfalse} |
| 713 |
\DeclareOption{nounderscore}{\@uscorefalse} |
| 714 |
% \end{macrocode} |
| 715 |
% |
| 716 |
% Now process the options: |
| 717 |
% |
| 718 |
% \begin{macrocode} |
| 719 |
\newif\ifsd@round |
| 720 |
\newif\if@uscore\@uscoretrue |
| 721 |
\ExecuteOptions{square} |
| 722 |
\ProcessOptions |
| 723 |
% \end{macrocode} |
| 724 |
% |
| 725 |
% \subsection{Special character handling} |
| 726 |
% |
| 727 |
% A lot of the \syn\ package requires the use special active characters. |
| 728 |
% These must be added to two lists: |\dospecials|, which is used by |\verb| |
| 729 |
% and friends, and |\@sanitize|, which is used by |\index|. The two macros |
| 730 |
% here, |\addspecial| and |\remspecial|, provide these registration |
| 731 |
% facilities. |
| 732 |
% |
| 733 |
% Two similar macros are found in Frank Mittelbach's \package{doc} package: |
| 734 |
% these have the disadvantage of global operation. My macros here are based |
| 735 |
% on Frank's, which in turn appear to be based on Donald Knuth's list |
| 736 |
% handling code presented in Appendix~D of \textit{The \TeX book}. |
| 737 |
% |
| 738 |
% Both these macros take a single argument: a single-character control |
| 739 |
% sequence containing the special character to be added to or removed from |
| 740 |
% the lists. |
| 741 |
% |
| 742 |
% \begin{macro}{\addspecial} |
| 743 |
% |
| 744 |
% This is reasonably straightforward. We remove the sequence from the lists, |
| 745 |
% in case it's already there, and add it in in the obvious way. This |
| 746 |
% requires a little bit of fun with |\expandafter|. |
| 747 |
% |
| 748 |
% \begin{macrocode} |
| 749 |
\def\addspecial#1{% |
| 750 |
\remspecial{#1}% |
| 751 |
\expandafter\def\expandafter\dospecials\expandafter{\dospecials\do#1}% |
| 752 |
\expandafter\def\expandafter\@santize\expandafter{% |
| 753 |
\@sanitize\@makeother#1}% |
| 754 |
} |
| 755 |
% \end{macrocode} |
| 756 |
% |
| 757 |
% \end{macro} |
| 758 |
% |
| 759 |
% \begin{macro}{\remspecial} |
| 760 |
% |
| 761 |
% This is the difficult bit. Since |\dospecials| and |\@sanitize| have the |
| 762 |
% form of list macros, we can redefine |\do| and |\@makeother| to do the |
| 763 |
% job for us. We must be careful to put the old meaning of |\@makeother| |
| 764 |
% back. The current implementation assumes it knows what |\@makeother| does. |
| 765 |
% |
| 766 |
% \begin{macrocode} |
| 767 |
\def\remspecial#1{% |
| 768 |
\def\do##1{\ifnum`#1=`##1 \else\noexpand\do\noexpand##1\fi}% |
| 769 |
\edef\dospecials{\dospecials}% |
| 770 |
\def\@makeother##1{\ifnum`#1=`##1 \else% |
| 771 |
\noexpand\@makeother\noexpand##1\fi}% |
| 772 |
\edef\@sanitize{\@sanitize}% |
| 773 |
\def\@makeother##1{\catcode`##112}% |
| 774 |
} |
| 775 |
% \end{macrocode} |
| 776 |
% |
| 777 |
% \end{macro} |
| 778 |
% |
| 779 |
% \subsection{Underscore handling} |
| 780 |
% |
| 781 |
% When typing a lot of identifiers, it can be irksome to have to escape |
| 782 |
% all `|_|' characters in the manuscript. We make the underscore character |
| 783 |
% active, so that it typesets an underscore in horizontal mode, and does |
| 784 |
% its usual job as a subscript operator in maths mode. Underscore must |
| 785 |
% already be in the special character lists, because of its use as a |
| 786 |
% subscript character, so this doesn't cause us a problem. |
| 787 |
% |
| 788 |
% \begin{macro}{\underscore} |
| 789 |
% |
| 790 |
% The |\underscore| macro typesets an underline character, using a horizontal |
| 791 |
% rule. This is positioned slightly below the baseline, and is also slightly |
| 792 |
% wider than the default \TeX\ underscore. This code is based on a similar |
| 793 |
% implementation found in the \package{lgrind} package. |
| 794 |
% |
| 795 |
% \begin{macrocode} |
| 796 |
\def\underscore{% |
| 797 |
\leavevmode% |
| 798 |
\kern.06em% |
| 799 |
\vbox{% |
| 800 |
\hrule\@width.6em\@depth.4ex\@height-.34ex% |
| 801 |
}% |
| 802 |
\ifdim\fontdimen\@ne\font=\z@% |
| 803 |
\kern.06em% |
| 804 |
\fi% |
| 805 |
} |
| 806 |
% \end{macrocode} |
| 807 |
% |
| 808 |
% \end{macro} |
| 809 |
% |
| 810 |
% \begin{macro}{\@uscore} |
| 811 |
% |
| 812 |
% This macro is called by the `|_|' active character to sort out what to do. |
| 813 |
% |
| 814 |
% If this is maths mode, we use the |\sb| macro, which is already defined |
| 815 |
% to do subscripting. Otherwise, we call |\textunderscore|, which picks the |
| 816 |
% nicest underscore it can find. |
| 817 |
% |
| 818 |
% There's some extra cunningness here, because I'd like to be able to |
| 819 |
% hyphenate after underscores usually, but not when there's another one |
| 820 |
% following. And then, because \env{tabbing} redefines |\_|, there's some |
| 821 |
% more yukkiness to handle that: the usual |\@tabacckludge| mechanism doesn't |
| 822 |
% cope with this particular case. |
| 823 |
% |
| 824 |
% \begin{macrocode} |
| 825 |
\let\usc@builtindischyphen\- |
| 826 |
\def\@uscore.{% |
| 827 |
\ifmmode% |
| 828 |
\expandafter\@firstoftwo% |
| 829 |
\else% |
| 830 |
\expandafter\@secondoftwo% |
| 831 |
\fi% |
| 832 |
\sb% |
| 833 |
{\textunderscore\@ifnextchar_{}{\usc@builtindischyphen}}% |
| 834 |
} |
| 835 |
% \end{macrocode} |
| 836 |
% |
| 837 |
% \end{macro} |
| 838 |
% |
| 839 |
% Now we set up the active character. Note the |\protect|, which makes |
| 840 |
% underscores work reasonably well in moving arguments. Note also the way |
| 841 |
% we end with a some funny stuff to prevent spaces being lost if this is |
| 842 |
% written to a file. |
| 843 |
% |
| 844 |
% \begin{macrocode} |
| 845 |
\if@uscore |
| 846 |
\AtBeginDocument{% |
| 847 |
\catcode`\_\active% |
| 848 |
\begingroup% |
| 849 |
\lccode`\~`\_% |
| 850 |
\lowercase{\endgroup\def~{\protect\@uscore.}}% |
| 851 |
} |
| 852 |
\fi |
| 853 |
% \end{macrocode} |
| 854 |
% |
| 855 |
% Finally, we redefine the |\_| macro to use our own |\underscore|, because |
| 856 |
% it's prettier. Actually, we don't: we just redefine the |
| 857 |
% |\?\textunderscore| command (funny name, isn't it?). |
| 858 |
% |
| 859 |
% \begin{macrocode} |
| 860 |
\expandafter\let\csname?\string\textunderscore\endcsname\underscore |
| 861 |
% \end{macrocode} |
| 862 |
% |
| 863 |
% \subsection{Abbreviated verbatim notation} |
| 864 |
% |
| 865 |
% In similar style to the \package{doc} package, we allow the user to set up |
| 866 |
% characters which delimit verbatim text. Unlike \package{doc}, we make |
| 867 |
% such changes local to the current group. This is performed through the |
| 868 |
% |\shortverb| and |\unverb| commands. |
| 869 |
% |
| 870 |
% The implementations of these commands are based upon the |\MakeShortVerb| |
| 871 |
% and |\DeleteShortVerb| commands of the \package{doc} package, although |
| 872 |
% these versions have effect local to the current grouping level. This |
| 873 |
% prevents their redefinition of |\dospecials| from interfering with the |
| 874 |
% grammar shortcuts, which require local changes only. |
| 875 |
% |
| 876 |
% The command |\shortverb| takes a single argument: a single-character |
| 877 |
% control sequence defining which character to make into the verbatim text |
| 878 |
% delimiter. We store the old meaning of the active character in a control |
| 879 |
% sequence called |\mn@\|\<char>. Note that this control sequence |
| 880 |
% contains a backslash character, which is a little odd. We also define a |
| 881 |
% command |\cc@\|\<char> which will return everything to normal. This |
| 882 |
% is used by the |\unverb| command. |
| 883 |
% |
| 884 |
% \begin{macro}{\shortverb} |
| 885 |
% |
| 886 |
% Here we build the control sequences we need to make everything work nicely. |
| 887 |
% The active character is defined via |\lowercase|, using the |~| character: |
| 888 |
% this is already made active by \TeX\@. |
| 889 |
% |
| 890 |
% The actual code requires lots of fiddling with |\expandafter| and friends. |
| 891 |
% |
| 892 |
% \begin{macrocode} |
| 893 |
\def\shortverb#1{% |
| 894 |
% \end{macrocode} |
| 895 |
% |
| 896 |
% First, we check to see if the command |\cc@\|\<char> has been defined. |
| 897 |
% |
| 898 |
% \begin{macrocode} |
| 899 |
\@ifundefined{cc@\string#1}{% |
| 900 |
% \end{macrocode} |
| 901 |
% |
| 902 |
% If it hasn't been defined, we add the character to the specials list. |
| 903 |
% |
| 904 |
% \begin{macrocode} |
| 905 |
\addspecial#1% |
| 906 |
% \end{macrocode} |
| 907 |
% |
| 908 |
% Now we set our character to be the lowercase version of |~|, which allows |
| 909 |
% us to use it, even though we don't know what it is. |
| 910 |
% |
| 911 |
% \begin{macrocode} |
| 912 |
\begingroup% |
| 913 |
\lccode`\~`#1% |
| 914 |
% \end{macrocode} |
| 915 |
% |
| 916 |
% Finally, we reach the tricky bit. All of this is lowercased, so any |
| 917 |
% occurrences of |~| are replaced by the user's special character. |
| 918 |
% |
| 919 |
% \begin{macrocode} |
| 920 |
\lowercase{% |
| 921 |
\endgroup% |
| 922 |
% \end{macrocode} |
| 923 |
% |
| 924 |
% We remember the current meaning of the character, in case it has one. We |
| 925 |
% have to use |\csname| to build the rather strange name we use for this. |
| 926 |
% |
| 927 |
% \begin{macrocode} |
| 928 |
\expandafter\let\csname mn@\string#1\endcsname~% |
| 929 |
% \end{macrocode} |
| 930 |
% |
| 931 |
% Now we build |\cc@\|\<char>. This is done with |\edef|, since more |
| 932 |
% of this needs to be expanded now than not. In this way, the actual macros |
| 933 |
% we create end up being very short. |
| 934 |
% |
| 935 |
% \begin{macrocode} |
| 936 |
\expandafter\edef\csname cc@\string#1\endcsname{% |
| 937 |
% \end{macrocode} |
| 938 |
% |
| 939 |
% First, add a command to restore the character's old catcode. |
| 940 |
% |
| 941 |
% \begin{macrocode} |
| 942 |
\catcode`\noexpand#1\the\catcode`#1% |
| 943 |
% \end{macrocode} |
| 944 |
% |
| 945 |
% Now we restore the character's old meaning, using the version we saved |
| 946 |
% earlier. |
| 947 |
% |
| 948 |
% \begin{macrocode} |
| 949 |
\let\noexpand~\expandafter\noexpand% |
| 950 |
\csname mn@\string#1\endcsname% |
| 951 |
% \end{macrocode} |
| 952 |
% |
| 953 |
% Now we remove the character from the specials lists. |
| 954 |
% |
| 955 |
% \begin{macrocode} |
| 956 |
\noexpand\remspecial\noexpand#1% |
| 957 |
% \end{macrocode} |
| 958 |
% |
| 959 |
% Finally, we delete this macro, so that |\unverb| will generate a warning |
| 960 |
% if the character is |\unverb|ed again. |
| 961 |
% |
| 962 |
% \begin{macrocode} |
| 963 |
\let\csname cc@\string#1\endcsname\relax% |
| 964 |
}% |
| 965 |
% \end{macrocode} |
| 966 |
% |
| 967 |
% All of that's over now. We set up the new definition of the character, |
| 968 |
% in terms of |\verb|, and make the character active. The nasty |\syn@ttspace| |
| 969 |
% is there to make the spacing come out right. It's all right really. Honest. |
| 970 |
% |
| 971 |
% \begin{macrocode} |
| 972 |
\def~{\verb~\syn@ttspace}% |
| 973 |
}% |
| 974 |
\catcode`#1\active% |
| 975 |
% \end{macrocode} |
| 976 |
% |
| 977 |
% If our magic control sequence already existed, we can assume that the |
| 978 |
% character is already a verbatim delimiter, and raise a warning. |
| 979 |
% |
| 980 |
% \begin{macrocode} |
| 981 |
}{% |
| 982 |
\PackageWarning{syntax}{Character `\expandafter\@gobble\string#1' |
| 983 |
is already a verbatim\MessageBreak |
| 984 |
delimiter}% |
| 985 |
}% |
| 986 |
} |
| 987 |
% \end{macrocode} |
| 988 |
% |
| 989 |
% \end{macro} |
| 990 |
% |
| 991 |
% \begin{macro}{\unverb} |
| 992 |
% |
| 993 |
% This is actually terribly easy: we just use the |\cc@\|\<char> command |
| 994 |
% we definied earlier, after making sure that it's been defined. |
| 995 |
% |
| 996 |
% \begin{macrocode} |
| 997 |
\def\unverb#1{% |
| 998 |
\@ifundefined{cc@\string#1}{% |
| 999 |
\PackageWarning{syntax}{Character `\expandafter\@gobble\string#1' |
| 1000 |
is not a verbatim\MessageBreak |
| 1001 |
delimiter}% |
| 1002 |
}{% |
| 1003 |
\csname cc@\string#1\endcsname% |
| 1004 |
}% |
| 1005 |
} |
| 1006 |
% \end{macrocode} |
| 1007 |
% |
| 1008 |
% \end{macro} |
| 1009 |
% |
| 1010 |
% \subsection{Style hooks for syntax forms} |
| 1011 |
% |
| 1012 |
% To allow the appearance of syntax things to be configured, we provide some |
| 1013 |
% redefinable bits. |
| 1014 |
% |
| 1015 |
% The three types of objects (nonterminal symbols, and quoted and unquoted |
| 1016 |
% terminals) each have two macros associated with them: one which does the |
| 1017 |
% `left' bit of the typesetting, and one which does the `right' bit. The |
| 1018 |
% items are typeset as LR~boxes. I'll be extra good while defining these |
| 1019 |
% hooks, so that it's obvious what's going on; macho \TeX\ hacker things |
| 1020 |
% resume after this section. |
| 1021 |
% |
| 1022 |
% \begin{macro}{\syntleft} |
| 1023 |
% \begin{macro}{\syntright} |
| 1024 |
% |
| 1025 |
% I can't see why anyone would want to change the typesetting of |
| 1026 |
% nonterminals, although I'll provide the hooks for symmetry's sake. |
| 1027 |
% |
| 1028 |
% \begin{macrocode} |
| 1029 |
\newcommand{\syntleft}{$\langle$\normalfont\itshape} |
| 1030 |
\newcommand{\syntright}{$\rangle$} |
| 1031 |
% \end{macrocode} |
| 1032 |
% |
| 1033 |
% \end{macro} |
| 1034 |
% \end{macro} |
| 1035 |
% |
| 1036 |
% \begin{macro}{\ulitleft} |
| 1037 |
% \begin{macro}{\ulitright} |
| 1038 |
% \begin{macro}{\litleft} |
| 1039 |
% \begin{macro}{\litright} |
| 1040 |
% |
| 1041 |
% Now we can define the left and right parts of quoted and unquoted |
| 1042 |
% terminals. US~readers may want to put double quotes around the quoted |
| 1043 |
% terminals, for example. |
| 1044 |
% |
| 1045 |
% \begin{macrocode} |
| 1046 |
\newcommand{\ulitleft}{\normalfont\ttfamily\syn@ttspace\frenchspacing} |
| 1047 |
\newcommand{\ulitright}{} |
| 1048 |
\newcommand{\litleft}{`\bgroup\ulitleft} |
| 1049 |
\newcommand{\litright}{\ulitright\egroup'} |
| 1050 |
% \end{macrocode} |
| 1051 |
% |
| 1052 |
% \end{macro} |
| 1053 |
% \end{macro} |
| 1054 |
% \end{macro} |
| 1055 |
% \end{macro} |
| 1056 |
% |
| 1057 |
% \subsection{Simple syntax typesetting} |
| 1058 |
% |
| 1059 |
% In general text, we allow access to our typesetting conventions through |
| 1060 |
% standard \LaTeX\ commands. |
| 1061 |
% |
| 1062 |
% \begin{macro}{\synt} |
| 1063 |
% |
| 1064 |
% The |\synt| macro typesets its argument as a syntactic quantity. It puts |
| 1065 |
% the text of the argument in italics, and sets angle brackets around it. |
| 1066 |
% Breaking of a |\synt| object across lines is forbidden. |
| 1067 |
% |
| 1068 |
% \begin{macrocode} |
| 1069 |
\def\synt#1{\mbox{\syntleft{#1\/}\syntright}} |
| 1070 |
% \end{macrocode} |
| 1071 |
% |
| 1072 |
% \end{macro} |
| 1073 |
% |
| 1074 |
% \begin{macro}{\lit} |
| 1075 |
% |
| 1076 |
% The |\lit| macro typesets its argument as literal text, to be typed in. |
| 1077 |
% Normally, this means setting the text in |\tt| font, and putting quotes |
| 1078 |
% around it, although the quotes can be suppressed by using the $*$-variant. |
| 1079 |
% |
| 1080 |
% The |\syn@ttspace| macro sets up the spacing for the text nicely: |\tt| |
| 1081 |
% spaces tend to be a little wide. |
| 1082 |
% |
| 1083 |
% \begin{macrocode} |
| 1084 |
\def\lit{\@ifstar{\lit@i\ulitleft\ulitright}{\lit@i\litleft\litright}} |
| 1085 |
\def\lit@i#1#2#3{\mbox{#1{#3\/}#2}} |
| 1086 |
% \end{macrocode} |
| 1087 |
% |
| 1088 |
% \end{macro} |
| 1089 |
% |
| 1090 |
% \begin{macro}{\syn@ttspace} |
| 1091 |
% |
| 1092 |
% This sets up the |\spaceskip| value for |\tt| text. |
| 1093 |
% |
| 1094 |
% \begin{macrocode} |
| 1095 |
\def\syn@ttspace@{\spaceskip.35em\@plus.2em\@minus.15em\relax} |
| 1096 |
% \end{macrocode} |
| 1097 |
% |
| 1098 |
% However, this isn't always the right thing to do. |
| 1099 |
% |
| 1100 |
% \begin{macrocode} |
| 1101 |
\def\ttthinspace{\let\syn@ttspace\syn@ttspace@} |
| 1102 |
\def\ttthickspace{\let\syn@ttspace\@empty} |
| 1103 |
% \end{macrocode} |
| 1104 |
% |
| 1105 |
% I know what I like thoough. |
| 1106 |
% |
| 1107 |
% \begin{macrocode} |
| 1108 |
\ttthinspace |
| 1109 |
% \end{macrocode} |
| 1110 |
% |
| 1111 |
% \end{macro} |
| 1112 |
% |
| 1113 |
% \subsubsection{The shortcuts} |
| 1114 |
% |
| 1115 |
% The easy part is over now. The next job is to set up the `grammar |
| 1116 |
% shortcuts' which allow easy changing of styles. |
| 1117 |
% |
| 1118 |
% We support four shortcuts: |
| 1119 |
% \begin{itemize} |
| 1120 |
% \item |`literal text'| typesets \syntax{`literal text'} |
| 1121 |
% \item |<non-terminal>| typesets \syntax{<non-terminal>} |
| 1122 |
% \item |"unquoted text"| typesets \syntax{"unquoted text"} |
| 1123 |
% \item \verb"|" typesets a \syntax{|} character |
| 1124 |
% \end{itemize} |
| 1125 |
% These are all implemented through active characters, which are enabled |
| 1126 |
% using the |\syntaxShortcuts| macro, described below. |
| 1127 |
% |
| 1128 |
% \begin{macro}{\readupto} |
| 1129 |
% |
| 1130 |
% \syntax{"\\readupto{"<char>"}{"<decls>"}{"<command>"}"} will read all |
| 1131 |
% characters up until the next occurrence of \<char>. Normally, all |
| 1132 |
% special characters will be deactivated. However, you can reactivate some |
| 1133 |
% characters, using the \<decls> argument, which is processed before the |
| 1134 |
% text is read. |
| 1135 |
% |
| 1136 |
% The code is borrowed fairly obviously from the \LaTeXe\ source for the |
| 1137 |
% |\verb| command. |
| 1138 |
% |
| 1139 |
% \begin{macrocode} |
| 1140 |
\def\readupto#1#2#3{% |
| 1141 |
\bgroup% |
| 1142 |
\verb@eol@error% |
| 1143 |
\let\do\@makeother\dospecials% |
| 1144 |
#2% |
| 1145 |
\catcode`#1\active% |
| 1146 |
\lccode`\~`#1% |
| 1147 |
\gdef\verb@balance@group{\verb@egroup% |
| 1148 |
\@latex@error{\noexpand\verb illegal in command argument}\@ehc}% |
| 1149 |
\def\@vhook{\verb@egroup#3}% |
| 1150 |
\aftergroup\verb@balance@group% |
| 1151 |
\lowercase{\let~\@vhook}% |
| 1152 |
} |
| 1153 |
% \end{macrocode} |
| 1154 |
% |
| 1155 |
% \end{macro} |
| 1156 |
% |
| 1157 |
% \begin{macro}{\syn@assist} |
| 1158 |
% |
| 1159 |
% The |\syn@assist| macro is used for defining three of the shortcuts. It |
| 1160 |
% is called as |
| 1161 |
% |
| 1162 |
% \begin{quote} |
| 1163 |
% \syntax{"\\syn@assist{"<left-decls>"}{"<actives>"}{"<delimeter>"}" \\ |
| 1164 |
% \null \quad "{"<right-decls>"}{"<end-cmd>"}"} |
| 1165 |
% \end{quote} |
| 1166 |
% |
| 1167 |
% It creates an hbox, sets up the escape sequences for quoting our magic |
| 1168 |
% characters, and then typesets a box containing |
| 1169 |
% |
| 1170 |
% \begin{quote} |
| 1171 |
% \syntax{<left-decls>"{"<delimited-text>"\\/}"<right-decls>} |
| 1172 |
% \end{quote} |
| 1173 |
% |
| 1174 |
% The \<left-decls> and \<right-decls> can be |\relax| if they're not |
| 1175 |
% required. |
| 1176 |
% |
| 1177 |
% The \<actives> argument is passed to |\readupto|, to allow some special |
| 1178 |
% characters through. By default, we re-enable |\|, and make `\verb*" "' |
| 1179 |
% typeset some space glue, rather than a space character. A macro |
| 1180 |
% `\verb*"\ "' is defined to actually print a space character, which yield |
| 1181 |
% `\verb*" "' in the `|\tt|' font. |
| 1182 |
% |
| 1183 |
% Finally, it defines a |\ch| command, which, given a single-character |
| 1184 |
% control sequence as its argument, typesets the character. This is useful, |
| 1185 |
% since |`| has been made active when we set up these calls, so the |
| 1186 |
% direct |\char`\|\<char> doesn't work. |
| 1187 |
% |
| 1188 |
% \begin{macrocode} |
| 1189 |
\def\syn@assist#1#2#3#4#5{% |
| 1190 |
% \end{macrocode} |
| 1191 |
% |
| 1192 |
% First, we start the box, and open a group. We use |\mbox| because it |
| 1193 |
% does all the messing with |\leavevmode| which is needed. |
| 1194 |
% |
| 1195 |
% \begin{macrocode} |
| 1196 |
\mbox\bgroup% |
| 1197 |
% \end{macrocode} |
| 1198 |
% |
| 1199 |
% Next job is to set up the escape sequences. |
| 1200 |
% |
| 1201 |
% \begin{macrocode} |
| 1202 |
\chardef\\`\\% |
| 1203 |
\chardef\>`\>% |
| 1204 |
\chardef\'`\'% |
| 1205 |
\chardef\"`\"% |
| 1206 |
\chardef\ `\ % |
| 1207 |
% \end{macrocode} |
| 1208 |
% |
| 1209 |
% Now to define |\ch|. This is done the obvious way. |
| 1210 |
% |
| 1211 |
% \begin{macrocode} |
| 1212 |
\def\ch##1{\char`##1}% |
| 1213 |
% \end{macrocode} |
| 1214 |
% |
| 1215 |
% For active characters, we do some fiddling with |\lccode|s. |
| 1216 |
% |
| 1217 |
% \begin{macrocode} |
| 1218 |
\def\act##1{% |
| 1219 |
\catcode`##1\active% |
| 1220 |
\begingroup% |
| 1221 |
\lccode`\~`##1% |
| 1222 |
\lowercase{\endgroup\def~}% |
| 1223 |
}% |
| 1224 |
% \end{macrocode} |
| 1225 |
% |
| 1226 |
% Finally, we do the real work of setting the text. We use |\readupto| to |
| 1227 |
% actually find the text we want. |
| 1228 |
% |
| 1229 |
% \begin{macrocode} |
| 1230 |
#1% |
| 1231 |
\begingroup% |
| 1232 |
\readupto#3{% |
| 1233 |
\catcode`\\0% |
| 1234 |
\catcode`\ 10% |
| 1235 |
#2% |
| 1236 |
}{% |
| 1237 |
\/\endgroup#4\egroup#5% |
| 1238 |
}% |
| 1239 |
} |
| 1240 |
% \end{macrocode} |
| 1241 |
% |
| 1242 |
% \end{macro} |
| 1243 |
% |
| 1244 |
% \begin{macro}{\syn@shorts} |
| 1245 |
% |
| 1246 |
% This macro actually defines the expansions for the active characters. |
| 1247 |
% We have to do this separately because |`| must be active when we use it |
| 1248 |
% in the |\def|, but we can't do that and use |\catcode| at the same time. |
| 1249 |
% The arguments are commands to do before and after the actual command. |
| 1250 |
% These are passed up from |\syntaxShortcuts|. |
| 1251 |
% |
| 1252 |
% All of the characters use |\syn@assist| in the obvious way except for |
| 1253 |
% \verb"|", which drops into maths mode instead. |
| 1254 |
% |
| 1255 |
% Note that when changing the catcodes, we must save |`| until last. |
| 1256 |
% |
| 1257 |
% \begin{macrocode} |
| 1258 |
\begingroup |
| 1259 |
\catcode`\<\active |
| 1260 |
\catcode`\|\active |
| 1261 |
\catcode`\"\active |
| 1262 |
\catcode`\`\active |
| 1263 |
% |
| 1264 |
\gdef\syn@shorts#1#2{% |
| 1265 |
% \end{macrocode} |
| 1266 |
% |
| 1267 |
% The `|<|' character must typeset its argument in italics. We make `|_|' |
| 1268 |
% do the same as the `|\_|' command. |
| 1269 |
% |
| 1270 |
% \begin{macrocode} |
| 1271 |
\def<{% |
| 1272 |
#1% |
| 1273 |
\syn@assist% |
| 1274 |
\syntleft% |
| 1275 |
{\act_{\@uscore.}}% |
| 1276 |
>% |
| 1277 |
\syntright% |
| 1278 |
{#2}% |
| 1279 |
}% |
| 1280 |
% \end{macrocode} |
| 1281 |
% |
| 1282 |
% The `|`|' and `|"|' characters should print its argument in |\tt| font. |
| 1283 |
% We change the `|\tt|' space glue to provide nicer spacing on the line. |
| 1284 |
% |
| 1285 |
% \begin{macrocode} |
| 1286 |
\def`{% |
| 1287 |
#1% |
| 1288 |
\syn@assist% |
| 1289 |
\litleft% |
| 1290 |
\relax% |
| 1291 |
'% |
| 1292 |
\litright% |
| 1293 |
{#2}% |
| 1294 |
}% |
| 1295 |
\def"{% |
| 1296 |
#1% |
| 1297 |
\syn@assist% |
| 1298 |
\ulitleft% |
| 1299 |
\relax% |
| 1300 |
"% |
| 1301 |
\ulitright% |
| 1302 |
{#2}% |
| 1303 |
}% |
| 1304 |
% \end{macrocode} |
| 1305 |
% |
| 1306 |
% Finally, the `\verb"|"' character is typeset by using the mysterious |
| 1307 |
% |\textbar| command. |
| 1308 |
% |
| 1309 |
% \begin{macrocode} |
| 1310 |
\def|{\textbar}% |
| 1311 |
% \end{macrocode} |
| 1312 |
% |
| 1313 |
% We're finished here now. |
| 1314 |
% |
| 1315 |
% \begin{macrocode} |
| 1316 |
} |
| 1317 |
% |
| 1318 |
\endgroup |
| 1319 |
% \end{macrocode} |
| 1320 |
% |
| 1321 |
% \end{macro} |
| 1322 |
% |
| 1323 |
% \begin{macro}{\syntaxShortcuts} |
| 1324 |
% |
| 1325 |
% This is a user-level command which enables the use of our shortcuts in the |
| 1326 |
% current group. It uses |\addspecial|, defined below, to register the |
| 1327 |
% active characters, sets up their definitions and activates them. |
| 1328 |
% |
| 1329 |
% The two arguments are commands to be performed before and after the |
| 1330 |
% handling of the abbreviation. In this way, you can further process the |
| 1331 |
% output. |
| 1332 |
% |
| 1333 |
% This command is not intended to be used directly by users: it should be |
| 1334 |
% used by other macros and packages which wish to take advantage of the |
| 1335 |
% facilities offered by this package. We provide a |\synshorts| declaration |
| 1336 |
% (which may be used as an environment, of course) which is more `user |
| 1337 |
% palatable'. |
| 1338 |
% |
| 1339 |
% \begin{macrocode} |
| 1340 |
\def\syntaxShortcuts#1#2{% |
| 1341 |
\syn@shorts{#1}{#2}% |
| 1342 |
\addspecial\`% |
| 1343 |
\addspecial\<% |
| 1344 |
\addspecial\|% |
| 1345 |
\addspecial\"% |
| 1346 |
\catcode`\|\active% |
| 1347 |
\catcode`\<\active% |
| 1348 |
\catcode`\"\active% |
| 1349 |
\catcode`\`\active% |
| 1350 |
} |
| 1351 |
% |
| 1352 |
\def\synshorts{\syntaxShortcuts\relax\relax} |
| 1353 |
% \end{macrocode} |
| 1354 |
% |
| 1355 |
% \end{macro} |
| 1356 |
% |
| 1357 |
% \begin{macro}{\synshortsoff} |
| 1358 |
% |
| 1359 |
% This macro can be useful occasionally: it disables the syntax shortcuts, |
| 1360 |
% so you can type normal text for a while. |
| 1361 |
% |
| 1362 |
% \begin{macrocode} |
| 1363 |
\def\synshortsoff{% |
| 1364 |
\catcode`\|12% |
| 1365 |
\catcode`\<12% |
| 1366 |
\catcode`\"12% |
| 1367 |
\catcode`\`12% |
| 1368 |
} |
| 1369 |
% \end{macrocode} |
| 1370 |
% |
| 1371 |
% \end{macro} |
| 1372 |
% |
| 1373 |
% \begin{macro}{\syntax} |
| 1374 |
% |
| 1375 |
% The |\syntax| macro typesets its argument, allowing the use of our |
| 1376 |
% shortcuts within the argument. |
| 1377 |
% |
| 1378 |
% Actually, we go to some trouble to ensure that the argument to |\syntax| |
| 1379 |
% \emph{isn't} a real argument so we can change catcodes as we go. We |
| 1380 |
% use the |\let\@let@token=| trick from \PlainTeX\ to do this. |
| 1381 |
% |
| 1382 |
% \begin{macrocode} |
| 1383 |
\def\syntax#{\bgroup\syntaxShortcuts\relax\relax\let\@let@token} |
| 1384 |
% \end{macrocode} |
| 1385 |
% |
| 1386 |
% \end{macro} |
| 1387 |
% |
| 1388 |
% \begin{environment}{grammar} |
| 1389 |
% |
| 1390 |
% The \env{grammar} environment is the final object we have to define. It |
| 1391 |
% allows typesetting of beautiful BNF grammars. |
| 1392 |
% |
| 1393 |
% First, we define the length parameters we need: |
| 1394 |
% |
| 1395 |
% \begin{macrocode} |
| 1396 |
\newskip\grammarparsep |
| 1397 |
\grammarparsep8\p@\@plus\p@\@minus\p@ |
| 1398 |
\newdimen\grammarindent |
| 1399 |
\grammarindent2em |
| 1400 |
% \end{macrocode} |
| 1401 |
% |
| 1402 |
% Now define the default label typesetting. This macro is designed to be |
| 1403 |
% replaced by a user, so we'll be extra-well-behaved and use genuine \LaTeX\ |
| 1404 |
% commands. Well, almost \dots |
| 1405 |
% |
| 1406 |
% \begin{macrocode} |
| 1407 |
\newcommand{\grammarlabel}[2]{% |
| 1408 |
\synt{#1} \hfill#2% |
| 1409 |
} |
| 1410 |
% \end{macrocode} |
| 1411 |
% |
| 1412 |
% Now for a bit of hacking to make the item stuff work properly. This gets |
| 1413 |
% done for every new paragraph that's started without an |\item| command. |
| 1414 |
% |
| 1415 |
% First, store the left hand side of the production in a box. Then I'll |
| 1416 |
% end the paragraph, and insert some nasty glue to take up all the space, |
| 1417 |
% so no-one will ever notice that there was a paragraph break there. The |
| 1418 |
% strut just makes sure that I know exactly how high the line is. |
| 1419 |
% |
| 1420 |
% \begin{macrocode} |
| 1421 |
\def\gr@implitem<#1> #2 {% |
| 1422 |
\sbox\z@{\hskip\labelsep\grammarlabel{#1}{#2}}% |
| 1423 |
\strut\@@par% |
| 1424 |
\vskip-\parskip% |
| 1425 |
\vskip-\baselineskip% |
| 1426 |
% \end{macrocode} |
| 1427 |
% |
| 1428 |
% The |\item| command will notice that I've inserted these funny glues and |
| 1429 |
% try to remove them: I'll stymie its efforts by inserting an invisible |
| 1430 |
% rule. Then I'll insert the label using |\item| in the normal way. |
| 1431 |
% |
| 1432 |
% \begin{macrocode} |
| 1433 |
\hrule\@height\z@\@depth\z@\relax% |
| 1434 |
\item[\unhbox\z@]% |
| 1435 |
% \end{macrocode} |
| 1436 |
% |
| 1437 |
% Just before I go, I'll make \lit{<} back into an active character. |
| 1438 |
% |
| 1439 |
% \begin{macrocode} |
| 1440 |
\catcode`\<\active% |
| 1441 |
} |
| 1442 |
% \end{macrocode} |
| 1443 |
% |
| 1444 |
% Now for the environment proper. Deep down, it's a list environment, with |
| 1445 |
% some nasty tricks to stop anyone from noticing. |
| 1446 |
% |
| 1447 |
% The first job is to set up the list from the parameters I'm given. |
| 1448 |
% |
| 1449 |
% \begin{macrocode} |
| 1450 |
\newenvironment{grammar}{% |
| 1451 |
\list{}{% |
| 1452 |
\labelwidth\grammarindent% |
| 1453 |
\leftmargin\grammarindent% |
| 1454 |
\advance\grammarindent\labelsep |
| 1455 |
\itemindent\z@% |
| 1456 |
\listparindent\z@% |
| 1457 |
\parsep\grammarparsep% |
| 1458 |
}% |
| 1459 |
% \end{macrocode} |
| 1460 |
% |
| 1461 |
% We have major problems in |\raggedright| layouts, which try to use |\par| |
| 1462 |
% to start new lines. We go back to normal |\\| newlines to try and bodge |
| 1463 |
% our way around these problems. |
| 1464 |
% |
| 1465 |
% \begin{macrocode} |
| 1466 |
\let\\\@normalcr |
| 1467 |
% \end{macrocode} |
| 1468 |
% |
| 1469 |
% Now to enable the shortcuts. |
| 1470 |
% |
| 1471 |
% \begin{macrocode} |
| 1472 |
\syntaxShortcuts\relax\relax% |
| 1473 |
% \end{macrocode} |
| 1474 |
% |
| 1475 |
% Now a little bit of magic. The |\alt| macro moves us to a new line, and |
| 1476 |
% typesets a vertical bar in the margin. This allows typesetting of |
| 1477 |
% multiline alternative productions in a pretty way. |
| 1478 |
% |
| 1479 |
% \begin{macrocode} |
| 1480 |
\def\alt{\\\llap{\textbar\quad}}% |
| 1481 |
% \end{macrocode} |
| 1482 |
% |
| 1483 |
% Now for another bit of magic. We set up some |\par| cleverness to spot |
| 1484 |
% the start of each production rule and format it in some cunning and |
| 1485 |
% user-defined way. |
| 1486 |
% |
| 1487 |
% \begin{macrocode} |
| 1488 |
\def\gr@setpar{% |
| 1489 |
\def\par{% |
| 1490 |
\parshape\@ne\@totalleftmargin\linewidth% |
| 1491 |
\@@par% |
| 1492 |
\catcode`\<12% |
| 1493 |
\everypar{% |
| 1494 |
\everypar{}% |
| 1495 |
\catcode`\<\active% |
| 1496 |
\gr@implitem% |
| 1497 |
}% |
| 1498 |
}% |
| 1499 |
}% |
| 1500 |
\gr@setpar% |
| 1501 |
\par% |
| 1502 |
% \end{macrocode} |
| 1503 |
% |
| 1504 |
% Now set up the |\[[| and |\]]| commands to do the right thing. We have |
| 1505 |
% to check the next character to see if it's correct, otherwise we'll |
| 1506 |
% open a maths display as usual. |
| 1507 |
% |
| 1508 |
% \begin{macrocode} |
| 1509 |
\let\gr@leftsq\[% |
| 1510 |
\let\gr@rightsq\]% |
| 1511 |
\def\gr@endsyntdiag]{\end{syntdiag}\gr@setpar\par}% |
| 1512 |
\def\[{\@ifnextchar[{\begin{syntdiag}\@gobble}\gr@leftsq}% |
| 1513 |
\def\]{\@ifnextchar]\gr@endsyntdiag\gr@rightsq}% |
| 1514 |
% \end{macrocode} |
| 1515 |
% |
| 1516 |
% Well, that's it for this side of the environment. |
| 1517 |
% |
| 1518 |
% \begin{macrocode} |
| 1519 |
}{% |
| 1520 |
% \end{macrocode} |
| 1521 |
% |
| 1522 |
% Closing the environment is a simple matter of tidying away the list. |
| 1523 |
% |
| 1524 |
% \begin{macrocode} |
| 1525 |
\@newlistfalse% |
| 1526 |
\everypar{}% |
| 1527 |
\endlist% |
| 1528 |
} |
| 1529 |
% \end{macrocode} |
| 1530 |
% |
| 1531 |
% \end{environment} |
| 1532 |
% |
| 1533 |
% \subsection{Syntax diagrams} |
| 1534 |
% |
| 1535 |
% Now we come to the final and most complicated part of the package. |
| 1536 |
% |
| 1537 |
% Syntax diagrams are drawn using arrow characters from \LaTeX's line font, |
| 1538 |
% used in the \env{picture} environment, and rules. The horizontal rules |
| 1539 |
% of the diagram are drawn along the baselines of the lines in which they |
| 1540 |
% are placed. The text items in the diagram are placed in boxes and lowered |
| 1541 |
% below the main baseline. Struts are added throughout to keep the vertical |
| 1542 |
% spacing consistent. |
| 1543 |
% |
| 1544 |
% The vertical structures (stacks and loops) are all implemented with \TeX's |
| 1545 |
% primitive |\halign| command. |
| 1546 |
% |
| 1547 |
% \subsubsection{User-configurable parameters} |
| 1548 |
% |
| 1549 |
% First, we allocate the \<dimen> and \<skip> arguments needed. Fixed |
| 1550 |
% lengths, as the \LaTeX book calls them, are allocated as \<dimen>s, to |
| 1551 |
% take some of the load off of all the \<skip> registers. |
| 1552 |
% |
| 1553 |
% \begin{macrocode} |
| 1554 |
\newskip\sdstartspace |
| 1555 |
\newskip\sdendspace |
| 1556 |
\newskip\sdmidskip |
| 1557 |
\newskip\sdtokskip |
| 1558 |
\newskip\sdfinalskip |
| 1559 |
\newdimen\sdrulewidth |
| 1560 |
\newdimen\sdcirclediam |
| 1561 |
\newdimen\sdindent |
| 1562 |
% \end{macrocode} |
| 1563 |
% |
| 1564 |
% We need some \TeX\ \<dimen>s for our own purposes, to get everything in |
| 1565 |
% the right places. We use labels for the `temporary' \TeX\ parameters |
| 1566 |
% which we use, to avoid wasting registers. |
| 1567 |
% |
| 1568 |
% \begin{macrocode} |
| 1569 |
\dimendef\sd@lower\z@ |
| 1570 |
\dimendef\sd@upper\tw@ |
| 1571 |
\dimendef\sd@mid4 |
| 1572 |
\dimendef\sd@topcirc6 |
| 1573 |
\dimendef\sd@botcirc8 |
| 1574 |
% \end{macrocode} |
| 1575 |
% |
| 1576 |
% \begin{macro}{\sd@setsize} |
| 1577 |
% When the text size for syntax diagrams changes, it's necessary to work out |
| 1578 |
% the height for various rules in the diagram. |
| 1579 |
% |
| 1580 |
% \begin{macrocode} |
| 1581 |
\def\sd@setsize{% |
| 1582 |
\sd@mid\ht\strutbox% |
| 1583 |
\advance\sd@mid-\dp\strutbox% |
| 1584 |
\[email protected]\sd@mid% |
| 1585 |
\sd@upper\sdrulewidth% |
| 1586 |
\advance\sd@upper\sd@mid% |
| 1587 |
\sd@lower\sdrulewidth% |
| 1588 |
\advance\sd@lower-\sd@mid% |
| 1589 |
\[email protected]\sdcirclediam% |
| 1590 |
\advance\sd@topcirc\sd@mid% |
| 1591 |
\[email protected]\sdcirclediam% |
| 1592 |
\advance\sd@botcirc-\sd@mid% |
| 1593 |
} |
| 1594 |
% \end{macrocode} |
| 1595 |
% |
| 1596 |
% \end{macro} |
| 1597 |
% |
| 1598 |
% \begin{macro}{\sdsize} |
| 1599 |
% |
| 1600 |
% You can set the default type size used by syntax diagrams by redefining |
| 1601 |
% the |\sdsize| command, using the |\renewcommand| command. |
| 1602 |
% |
| 1603 |
% By default, syntax diagrams are set slightly smaller than the main body |
| 1604 |
% text.\footnote{^^A |
| 1605 |
% I've used pure \LaTeX\ commands for this and the \cmd\sdlengths\ macro, |
| 1606 |
% to try and illustrate how these values might be changed by a user. The |
| 1607 |
% rest of the code is almost obfuscted in its use of raw \TeX\ features, |
| 1608 |
% in an attempt to dissuade more na\"\i ve users from fiddling with it. |
| 1609 |
% I suppose this is what you get when you let assembler hackers loose with |
| 1610 |
% something like \LaTeX. |
| 1611 |
% } |
| 1612 |
% |
| 1613 |
% \begin{macrocode} |
| 1614 |
\newcommand{\sdsize}{% |
| 1615 |
\small% |
| 1616 |
} |
| 1617 |
% \end{macrocode} |
| 1618 |
% |
| 1619 |
% \end{macro} |
| 1620 |
% |
| 1621 |
% \begin{macro}{\sdlengths} |
| 1622 |
% |
| 1623 |
% Finally, the default length parameters are set in the |\sdlengths| command. |
| 1624 |
% You can redefine the command using |\renewcommand|. |
| 1625 |
% |
| 1626 |
% We set up the length parameters here. |
| 1627 |
% |
| 1628 |
% \begin{macrocode} |
| 1629 |
\newcommand{\sdlengths}{% |
| 1630 |
\setlength{\sdstartspace}{1em minus 10pt}% |
| 1631 |
\setlength{\sdendspace}{1em minus 10pt}% |
| 1632 |
\setlength{\sdmidskip}{0.5em plus 0.0001fil}% |
| 1633 |
\setlength{\sdtokskip}{0.25em plus 0.0001fil}% |
| 1634 |
\setlength{\sdfinalskip}{0.5em plus 10000fil}% |
| 1635 |
\setlength{\sdrulewidth}{0.2pt}% |
| 1636 |
\setlength{\sdcirclediam}{8pt}% |
| 1637 |
\setlength{\sdindent}{0pt}% |
| 1638 |
} |
| 1639 |
% \end{macrocode} |
| 1640 |
% |
| 1641 |
% \end{macro} |
| 1642 |
% |
| 1643 |
% \subsubsection{Other declarations} |
| 1644 |
% |
| 1645 |
% We define four switches. The table shows what they're used for. |
| 1646 |
% |
| 1647 |
% \begin{table} |
| 1648 |
% \begin{tab}{lp{3in}} \hline |
| 1649 |
% |
| 1650 |
% \bf Switch & \bf Meaning \\ \hline |
| 1651 |
% |
| 1652 |
% |\ifsd@base| & We are at `base level' in the diagram: |
| 1653 |
% i.e., not in any other sorts of |
| 1654 |
% constructions. This is used to decide |
| 1655 |
% whether to allow line breaking. \\[2pt] |
| 1656 |
% |
| 1657 |
% |\ifsd@top| & The current loop construct is being |
| 1658 |
% typeset with the loop arrow above the |
| 1659 |
% baseline. \\[2pt] |
| 1660 |
% |
| 1661 |
% |\ifsd@toplayer| & We are typesetting the top layer of |
| 1662 |
% a stack. This is used to ensure that |
| 1663 |
% the vertical rules on either side are |
| 1664 |
% typeset at the right height. \\[2pt] |
| 1665 |
% |
| 1666 |
% |\ifsd@backwards| & We're typesetting backwards, because |
| 1667 |
% we're in the middle of a loop arrow. |
| 1668 |
% the only difference this makes is that |
| 1669 |
% any subloops have the arrow on the |
| 1670 |
% side. \\ \hline |
| 1671 |
% |
| 1672 |
% \end{tab} |
| 1673 |
% \caption{Syntax diagram switches} |
| 1674 |
% \end{table} |
| 1675 |
% |
| 1676 |
% \begin{macrocode} |
| 1677 |
\newif\ifsd@base |
| 1678 |
\newif\ifsd@top |
| 1679 |
\newif\ifsd@toplayer |
| 1680 |
\newif\ifsd@backwards |
| 1681 |
% \end{macrocode} |
| 1682 |
% |
| 1683 |
% \begin{macro}{\sd@err} |
| 1684 |
% |
| 1685 |
% We output our errors through this macro, which saves a little typing. |
| 1686 |
% |
| 1687 |
% \begin{macrocode} |
| 1688 |
\def\sd@err{\PackageError{syntax}} |
| 1689 |
% \end{macrocode} |
| 1690 |
% |
| 1691 |
% \end{macro} |
| 1692 |
% |
| 1693 |
% \subsubsection{Arrow-drawing} |
| 1694 |
% |
| 1695 |
% We need to draw some arrows. \LaTeX\ tries to make this as awkward as |
| 1696 |
% possible, so we have to start moving the arrows around in boxes quite a |
| 1697 |
% lot. |
| 1698 |
% |
| 1699 |
% The left and right pointing arrows are fairly simple: we just add some |
| 1700 |
% horizontal spacing to prevent the width of the arrow looking odd. |
| 1701 |
% |
| 1702 |
% \begin{macrocode} |
| 1703 |
\def\sd@arrow{% |
| 1704 |
\ht\tw@\z@% |
| 1705 |
\dp\tw@\z@% |
| 1706 |
\raise\sd@mid\box\tw@% |
| 1707 |
\egroup% |
| 1708 |
} |
| 1709 |
\def\sd@rightarr{% |
| 1710 |
\bgroup% |
| 1711 |
\setbox\tw@\hbox{\kern-6\p@\@linefnt\char'55}% |
| 1712 |
\sd@arrow% |
| 1713 |
} |
| 1714 |
\def\sd@leftarr{% |
| 1715 |
\bgroup% |
| 1716 |
\raise\sd@mid\hbox{\@linefnt\char'33\kern-6\p@}% |
| 1717 |
\sd@arrow% |
| 1718 |
} |
| 1719 |
% \end{macrocode} |
| 1720 |
% |
| 1721 |
% The up arrow is very strange. We need to bring the arrow down to base |
| 1722 |
% level, and smash its height. |
| 1723 |
% |
| 1724 |
% \begin{macrocode} |
| 1725 |
\def\sd@uparr{% |
| 1726 |
\bgroup% |
| 1727 |
\setbox\tw@\hb@xt@\z@{\kern-\sdrulewidth\@linefnt\char'66\hss}% |
| 1728 |
\setbox\tw@\hbox{\lower10\p@\box\tw@}% |
| 1729 |
\sd@arrow% |
| 1730 |
} |
| 1731 |
% \end{macrocode} |
| 1732 |
% |
| 1733 |
% The down arrow is similar, although it's already at the right height. |
| 1734 |
% Thus, we can just smash the box. |
| 1735 |
% |
| 1736 |
% \begin{macrocode} |
| 1737 |
\def\sd@downarr{% |
| 1738 |
\bgroup% |
| 1739 |
\setbox\tw@\hb@xt@\z@{\kern-\sdrulewidth\@linefnt\char'77\hss}% |
| 1740 |
\sd@arrow% |
| 1741 |
} |
| 1742 |
% \end{macrocode} |
| 1743 |
% |
| 1744 |
% \subsubsection{Drawing curves} |
| 1745 |
% |
| 1746 |
% If the user has selected curved edges, we use the \LaTeX\ features provided |
| 1747 |
% to obtain the curves. These are drawn slightly oddly to make it easier |
| 1748 |
% to fit them into the diagram. |
| 1749 |
% |
| 1750 |
% Some explanation about the \LaTeX\ circle font is probably called for |
| 1751 |
% before we go any further. The font consists of sets of four quadrants |
| 1752 |
% of a particular size (and some other characters, which aren't important |
| 1753 |
% at the moment). Each collection of quadrants fit together to form a |
| 1754 |
% perfect circle of a given diameter. The individual quadrant characters |
| 1755 |
% have strange bounding boxes, as described in the files \textit{lcircle.mf} |
| 1756 |
% and \textit{ltpict.dtx}, and also in Appendix~D of \textit{The \TeX book}. |
| 1757 |
% Our job here is to make these quadrants useful in the context of |
| 1758 |
% drawing syntax diagrams. |
| 1759 |
% |
| 1760 |
% \begin{macro}{\sd@circ} |
| 1761 |
% First, we define |\sd@circ|, which performs the common parts of the four |
| 1762 |
% routines. Since the characters in the circle font are grouped together, |
| 1763 |
% we can pick out a particular corner piece by specifying its index into |
| 1764 |
% the group for the required size. The |\sd@circ| routine will pick out |
| 1765 |
% the required character, given this index as an argument, and put it in |
| 1766 |
% box~2, after fiddling with the sizes a little: |
| 1767 |
% \begin{itemize} |
| 1768 |
% |
| 1769 |
% \item We clear the width to zero. The individual routines then add a kern |
| 1770 |
% of the correct amount, so that the quadrant appears in the right |
| 1771 |
% place. |
| 1772 |
% |
| 1773 |
% \item The piece is lowered by half the rule width. This positions the |
| 1774 |
% top and bottom pieces of the circle to be half way over the baseline, |
| 1775 |
% which is the correct position for the rest of the diagram. |
| 1776 |
% |
| 1777 |
% \end{itemize} |
| 1778 |
% |
| 1779 |
% Finally, we make sure we're in horizontal mode: horrific results occur |
| 1780 |
% if this is not the case. I'm sure I don't need to explain this any more |
| 1781 |
% graphically. |
| 1782 |
% |
| 1783 |
% \begin{macrocode} |
| 1784 |
\def\sd@circ#1{% |
| 1785 |
\@getcirc\sdcirclediam% |
| 1786 |
\advance\@tempcnta#1% |
| 1787 |
\setbox\tw@\hbox{\lower\sdrulewidth% |
| 1788 |
\hbox{\@circlefnt\char\@tempcnta}}% |
| 1789 |
\wd\tw@\z@% |
| 1790 |
\leavevmode% |
| 1791 |
} |
| 1792 |
% \end{macrocode} |
| 1793 |
% |
| 1794 |
% \end{macro} |
| 1795 |
% |
| 1796 |
% \begin{macro}{\sd@tlcirc} |
| 1797 |
% \begin{macro}{\sd@trcirc} |
| 1798 |
% \begin{macro}{\sd@blcirc} |
| 1799 |
% \begin{macro}{\sd@brcirc} |
| 1800 |
% |
| 1801 |
% These are the macros which actually draw quadrants of circles. They all |
| 1802 |
% call |\sd@circ|, passing an appropriate index, and then fiddle with the |
| 1803 |
% box sizes and apply kerning specific to the quadrant positioning. |
| 1804 |
% |
| 1805 |
% The exact requirements for positioning are as follows: |
| 1806 |
% |
| 1807 |
% \begin{itemize} |
| 1808 |
% |
| 1809 |
% \item The horizontal parts of the arcs must lie along the baseline (i.e., |
| 1810 |
% half the line must be above the baseline, and half must be below). |
| 1811 |
% This is consistent with the horizontal rules used in the diagram. |
| 1812 |
% |
| 1813 |
% \item The vertical parts must overlap vertical rules on either side, so |
| 1814 |
% that a |\vrule\sd@|\textit{xx}|circ| makes the arc appear to be |
| 1815 |
% a real curve in the line. The requirements are actually somewhat |
| 1816 |
% inconsistent; for example, the \env{stack} environment uses curves |
| 1817 |
% \emph{before} the |\vrule|s. Special requirements like this are |
| 1818 |
% handled as special cases later. |
| 1819 |
% |
| 1820 |
% \item The height and width of the arc are at least roughly correct. |
| 1821 |
% |
| 1822 |
% \end{itemize} |
| 1823 |
% |
| 1824 |
% \begin{macrocode} |
| 1825 |
\def\sd@tlcirc{{% |
| 1826 |
\sd@circ3% |
| 1827 |
\ht\tw@\sdrulewidth% |
| 1828 |
\dp\[email protected]\sdcirclediam% |
| 1829 |
\kern-\tw@\sdrulewidth% |
| 1830 |
\raise\sd@mid\box\tw@% |
| 1831 |
\kern.5\sdcirclediam% |
| 1832 |
}} |
| 1833 |
% \end{macrocode} |
| 1834 |
% |
| 1835 |
% \begin{macrocode} |
| 1836 |
\def\sd@trcirc{{% |
| 1837 |
\sd@circ0% |
| 1838 |
\ht\tw@\sdrulewidth% |
| 1839 |
\dp\[email protected]\sdcirclediam% |
| 1840 |
\kern.5\sdcirclediam% |
| 1841 |
\raise\sd@mid\box\tw@% |
| 1842 |
}} |
| 1843 |
% \end{macrocode} |
| 1844 |
% |
| 1845 |
% \begin{macrocode} |
| 1846 |
\def\sd@blcirc{{% |
| 1847 |
\sd@circ2% |
| 1848 |
\ht\[email protected]\sdcirclediam% |
| 1849 |
\dp\tw@\sdrulewidth% |
| 1850 |
\kern-\tw@\sdrulewidth% |
| 1851 |
\raise\sd@mid\box\tw@% |
| 1852 |
\kern.5\sdcirclediam% |
| 1853 |
}} |
| 1854 |
% \end{macrocode} |
| 1855 |
% |
| 1856 |
% \begin{macrocode} |
| 1857 |
\def\sd@brcirc{{% |
| 1858 |
\sd@circ1% |
| 1859 |
\ht\[email protected]\sdcirclediam% |
| 1860 |
\dp\tw@\sdrulewidth% |
| 1861 |
\kern.5\sdcirclediam% |
| 1862 |
\raise\sd@mid\box\tw@% |
| 1863 |
}} |
| 1864 |
% \end{macrocode} |
| 1865 |
% |
| 1866 |
% \end{macro} |
| 1867 |
% \end{macro} |
| 1868 |
% \end{macro} |
| 1869 |
% \end{macro} |
| 1870 |
% |
| 1871 |
% \begin{macro}{\sd@llc} |
| 1872 |
% \begin{macro}{\sd@rlc} |
| 1873 |
% |
| 1874 |
% In the \env{rep} environment, we need to be able to draw arcs with |
| 1875 |
% horizontal lines running through them. The two macros here do the job |
| 1876 |
% nicely. |\sd@llc| (which is short for left overlapping circle) is |
| 1877 |
% analogous to |\llap|: it puts its argument in a box of zero width, sticking |
| 1878 |
% out to the left. However, it also draws a rule along the baseline. This |
| 1879 |
% is important, as it prevents text from overprinting the arc. |\sd@rlc| |
| 1880 |
% is very similar, just the other way around. |
| 1881 |
% |
| 1882 |
% \begin{macrocode} |
| 1883 |
\def\sd@llc#1{% |
| 1884 |
\hb@[email protected]\sdcirclediam{% |
| 1885 |
\sd@rule\hskip.5\sdcirclediam% |
| 1886 |
\hss% |
| 1887 |
#1% |
| 1888 |
}% |
| 1889 |
} |
| 1890 |
% \end{macrocode} |
| 1891 |
% |
| 1892 |
% \begin{macrocode} |
| 1893 |
\def\sd@rlc#1{% |
| 1894 |
\hb@[email protected]\sdcirclediam{% |
| 1895 |
#1% |
| 1896 |
\hss% |
| 1897 |
\sd@rule\hskip.5\sdcirclediam% |
| 1898 |
}% |
| 1899 |
} |
| 1900 |
% \end{macrocode} |
| 1901 |
% |
| 1902 |
% \end{macro} |
| 1903 |
% \end{macro} |
| 1904 |
% |
| 1905 |
% \subsubsection{Drawing rules} |
| 1906 |
% |
| 1907 |
% It's important to draw the rules \emph{along} the baseline, rather than |
| 1908 |
% above it: hence, the depth of the rule must be equal to the height. |
| 1909 |
% |
| 1910 |
% \begin{macro}{\sd@rule} |
| 1911 |
% |
| 1912 |
% We use rule leaders instead of glue through most of the syntax diagrams. |
| 1913 |
% The command \syntax{"\\sd@rule"<skip>} draws a rule of the correct |
| 1914 |
% dimensions, which has the behaviour of an \syntax{"\\hskip"<skip>}. |
| 1915 |
% |
| 1916 |
% \begin{macrocode} |
| 1917 |
\def\sd@rule{\leaders\hrule\@height\sd@upper\@depth\sd@lower} |
| 1918 |
% \end{macrocode} |
| 1919 |
% |
| 1920 |
% \end{macro} |
| 1921 |
% |
| 1922 |
% \begin{macro}{\sd@gap} |
| 1923 |
% |
| 1924 |
% The gap between elements is added using this macro. It will allow a |
| 1925 |
% line break if we're at the top level of the diagram, using a rather |
| 1926 |
% strange discretionary. |
| 1927 |
% |
| 1928 |
% This is called as \syntax{"\\sd@gap{"<skip-register>"}"}. |
| 1929 |
% |
| 1930 |
% \begin{macrocode} |
| 1931 |
\def\sd@gap#1{% |
| 1932 |
% \end{macrocode} |
| 1933 |
% |
| 1934 |
% First, we see if we're at the top level. Within constructs, we avoid the |
| 1935 |
% overhead of a |\discretionary|. We put half of the width of the skip on |
| 1936 |
% each side of the discretionary break. |
| 1937 |
% |
| 1938 |
% \begin{macrocode} |
| 1939 |
\ifsd@base% |
| 1940 |
\skip@#1% |
| 1941 |
\divide\skip\z@\tw@% |
| 1942 |
\nobreak\sd@rule\hskip\skip@% |
| 1943 |
\discretionary{% |
| 1944 |
\sd@qarrow{->}% |
| 1945 |
}{% |
| 1946 |
\hbox{% |
| 1947 |
\sd@qarrow{>-}% |
| 1948 |
\sd@rule\hskip\sdstartspace% |
| 1949 |
\sd@rule\hskip3.5\p@% |
| 1950 |
}% |
| 1951 |
}{% |
| 1952 |
}% |
| 1953 |
\nobreak\sd@rule\hskip\skip@% |
| 1954 |
% \end{macrocode} |
| 1955 |
% |
| 1956 |
% If we're not at the base level, we just put in a rule of the correct |
| 1957 |
% width. |
| 1958 |
% |
| 1959 |
% \begin{macrocode} |
| 1960 |
\else% |
| 1961 |
\sd@rule\hskip#1% |
| 1962 |
\fi% |
| 1963 |
} |
| 1964 |
% \end{macrocode} |
| 1965 |
% |
| 1966 |
% \end{macro} |
| 1967 |
% |
| 1968 |
% \subsubsection{The \protect\env{syntdiag} environment} |
| 1969 |
% |
| 1970 |
% All syntax diagrams are contained within a \env{syntdiag} environment. |
| 1971 |
% |
| 1972 |
% \begin{environment}{syntdiag} |
| 1973 |
% |
| 1974 |
% The only argument is a collection of declarations, which by |
| 1975 |
% default is |
| 1976 |
% |
| 1977 |
% \begin{listing} |
| 1978 |
%\sdsize\sdlengths |
| 1979 |
% \end{listing} |
| 1980 |
% |
| 1981 |
% However, if the optional argument is not specified, \TeX\ reads the first |
| 1982 |
% character of the environment, which may not be catcoded correctly. We set |
| 1983 |
% up the catcodes first, using the |\syntaxShortcuts| command, and then read |
| 1984 |
% the argument. We don't use |\newcommand|, because that would involve |
| 1985 |
% creating yet \emph{another} macro. Time to fiddle with |\@ifnextchar| |
| 1986 |
% \dots |
| 1987 |
% |
| 1988 |
% \begin{macrocode} |
| 1989 |
\def\syntdiag{% |
| 1990 |
\syntaxShortcuts\sd@tok@i\sd@tok@ii% |
| 1991 |
\@ifnextchar[\syntdiag@i{\syntdiag@i[]}% |
| 1992 |
} |
| 1993 |
% \end{macrocode} |
| 1994 |
% |
| 1995 |
% Now we actually do the job we're meant to. |
| 1996 |
% |
| 1997 |
% \begin{macrocode} |
| 1998 |
\def\syntdiag@i[#1]{% |
| 1999 |
% \end{macrocode} |
| 2000 |
% |
| 2001 |
% The first thing to do is execute the user's declarations. We then set |
| 2002 |
% up things for the font size. |
| 2003 |
% |
| 2004 |
% \begin{macrocode} |
| 2005 |
\sdsize\sdlengths% |
| 2006 |
#1% |
| 2007 |
\sd@setsize% |
| 2008 |
% \end{macrocode} |
| 2009 |
% |
| 2010 |
% Next, we start a list, to change the text layout. |
| 2011 |
% |
| 2012 |
% \begin{macrocode} |
| 2013 |
\list{}{% |
| 2014 |
\leftmargin\sdindent% |
| 2015 |
\rightmargin\leftmargin% |
| 2016 |
\labelsep\z@% |
| 2017 |
\labelwidth\z@% |
| 2018 |
}% |
| 2019 |
\item[]% |
| 2020 |
% \end{macrocode} |
| 2021 |
% |
| 2022 |
% We reconfigure the paragraph format quite a lot now. We clear |
| 2023 |
% |\parfillskip| to avoid any justification at the end of the paragraph. |
| 2024 |
% We also turn off paragraph indentation. |
| 2025 |
% |
| 2026 |
% \begin{macrocode} |
| 2027 |
\parfillskip\z@% |
| 2028 |
\noindent% |
| 2029 |
% \end{macrocode} |
| 2030 |
% |
| 2031 |
% Next, we add in the arrows on the beginning of the line, and a bit of |
| 2032 |
% glue. |
| 2033 |
% |
| 2034 |
% \begin{macrocode} |
| 2035 |
\sd@qarrow{>>-}% |
| 2036 |
\nobreak\sd@rule\hskip\sdstartspace% |
| 2037 |
% \end{macrocode} |
| 2038 |
% |
| 2039 |
% This is the base level of the diagram, so we enable line breaking. |
| 2040 |
% |
| 2041 |
% \begin{macrocode} |
| 2042 |
\sd@basetrue% |
| 2043 |
% \end{macrocode} |
| 2044 |
% |
| 2045 |
% Since the objects being broken are rather large, we enable sloppy line |
| 2046 |
% breaking. We also try to avoid page breaks in mid-diagram, by upping the |
| 2047 |
% |\interlinepenalty|. |
| 2048 |
% |
| 2049 |
% \begin{macrocode} |
| 2050 |
\sloppy% |
| 2051 |
\interlinepenalty100% |
| 2052 |
\hyphenpenalty0% |
| 2053 |
% \end{macrocode} |
| 2054 |
% |
| 2055 |
% We handle all the spacing within the environment, so we make \TeX\ ignore |
| 2056 |
% spaces and newlines. |
| 2057 |
% |
| 2058 |
% \begin{macrocode} |
| 2059 |
\catcode`\ 9% |
| 2060 |
\catcode`\^^M9% |
| 2061 |
% \end{macrocode} |
| 2062 |
% |
| 2063 |
% We now have to change the behaviour of |\\| to line-break syntax diagrams. |
| 2064 |
% |
| 2065 |
% \begin{macrocode} |
| 2066 |
\let\\\sd@newline% |
| 2067 |
\ignorespaces% |
| 2068 |
} |
| 2069 |
% \end{macrocode} |
| 2070 |
% |
| 2071 |
% When we end the diagram, we just have to add in the final fillskip, and |
| 2072 |
% double arrow. |
| 2073 |
% |
| 2074 |
% \begin{macrocode} |
| 2075 |
\def\endsyntdiag{% |
| 2076 |
\unskip% |
| 2077 |
\nobreak\sd@rule\hskip\sdmidskip% |
| 2078 |
\sd@rule\hskip\sdfinalskip% |
| 2079 |
\sd@qarrow{-><}% |
| 2080 |
\endlist% |
| 2081 |
} |
| 2082 |
% \end{macrocode} |
| 2083 |
% |
| 2084 |
% \end{environment} |
| 2085 |
% |
| 2086 |
% \begin{environment}{syntdiag*} |
| 2087 |
% |
| 2088 |
% The starred form of \env{syntdiag} typesets a syntax diagram in LR-mode; |
| 2089 |
% this is useful if you're describing parts of syntax diagrams, for example. |
| 2090 |
% |
| 2091 |
% This is in fact really easy. The first bit which checks for an optional |
| 2092 |
% argument is almost identical to the non-$*$ version. |
| 2093 |
% |
| 2094 |
% \begin{macrocode} |
| 2095 |
\@namedef{syntdiag*}{% |
| 2096 |
\syntaxShortcuts\sd@tok@i\sd@tok@ii% |
| 2097 |
\@ifnextchar[\syntdiag@s@i{\syntdiag@s@i[]}% |
| 2098 |
} |
| 2099 |
% \end{macrocode} |
| 2100 |
% |
| 2101 |
% Handle another optional argument giving the width of the box to fill. |
| 2102 |
% |
| 2103 |
% \begin{macrocode} |
| 2104 |
\def\syntdiag@s@i[#1]{% |
| 2105 |
\@ifnextchar[{\syntdiag@s@ii{#1}}{\syntdiag@s@iii{#1}{\hbox}}% |
| 2106 |
} |
| 2107 |
\def\syntdiag@s@ii#1[#2]{\syntdiag@s@iii{#1}{\hb@xt@#2}} |
| 2108 |
% \end{macrocode} |
| 2109 |
% |
| 2110 |
% Now to actually start the display. This is mostly simple. Just to make |
| 2111 |
% sure about the LR-ness of the typesetting, I'll put everything in an hbox. |
| 2112 |
% |
| 2113 |
% \begin{macrocode} |
| 2114 |
\def\syntdiag@s@iii#1#2{% |
| 2115 |
\leavevmode% |
| 2116 |
#2\bgroup% |
| 2117 |
% \end{macrocode} |
| 2118 |
% |
| 2119 |
% Now configure the typesetting according to the user's wishes. |
| 2120 |
% |
| 2121 |
% \begin{macrocode} |
| 2122 |
\let\@@left\left% |
| 2123 |
\let\@@right\right% |
| 2124 |
\def\left##1{\def\sd@startarr{##1}}% |
| 2125 |
\def\right##1{\def\sd@endarr{##1}}% |
| 2126 |
\left{>-}\right{->}% |
| 2127 |
\sdsize\sdlengths% |
| 2128 |
#1% |
| 2129 |
\sd@setsize% |
| 2130 |
\let\left\@@left% |
| 2131 |
\let\right\@@right% |
| 2132 |
% \end{macrocode} |
| 2133 |
% |
| 2134 |
% Put in the initial double-arrow. |
| 2135 |
% |
| 2136 |
% \begin{macrocode} |
| 2137 |
\sd@qarrow\sd@startarr% |
| 2138 |
\sd@rule\hskip\sdmidskip% |
| 2139 |
% \end{macrocode} |
| 2140 |
% |
| 2141 |
% We're in horizontal mode, so don't bother with linebreaking. |
| 2142 |
% |
| 2143 |
% \begin{macrocode} |
| 2144 |
\sd@basefalse% |
| 2145 |
% \end{macrocode} |
| 2146 |
% |
| 2147 |
% Finally, disable spaces and things. |
| 2148 |
% |
| 2149 |
% \begin{macrocode} |
| 2150 |
\catcode`\ 9% |
| 2151 |
\catcode`\^^M9% |
| 2152 |
\ignorespaces% |
| 2153 |
} |
| 2154 |
% \end{macrocode} |
| 2155 |
% |
| 2156 |
% Ending the environment is very similar. |
| 2157 |
% |
| 2158 |
% \begin{macrocode} |
| 2159 |
\@namedef{endsyntdiag*}{% |
| 2160 |
\unskip% |
| 2161 |
\sd@rule\hskip\sdmidskip% |
| 2162 |
\sd@rule\hskip\sdfinalskip% |
| 2163 |
\sd@qarrow\sd@endarr% |
| 2164 |
\egroup% |
| 2165 |
} |
| 2166 |
% \end{macrocode} |
| 2167 |
% |
| 2168 |
% \end{environment} |
| 2169 |
% |
| 2170 |
% \begin{macro}{\sd@qarrow} |
| 2171 |
% |
| 2172 |
% This typesets the various left and right arrows required in syntax |
| 2173 |
% diagrams. The argument is one of \syntax{`>>-', `->', `>-' or `-><'}. |
| 2174 |
% |
| 2175 |
% \begin{macrocode} |
| 2176 |
\def\sd@qarrow#1{% |
| 2177 |
\begingroup% |
| 2178 |
\lccode`\~=`\<\lowercase{\def~{<}}% |
| 2179 |
\hbox{\csname sd@arr@#1\endcsname}% |
| 2180 |
\endgroup% |
| 2181 |
} |
| 2182 |
\@namedef{sd@arr@>>-}{\sd@rightarr\kern-.5\p@\sd@rightarr\kern-\p@} |
| 2183 |
\@namedef{sd@arr@>-}{\sd@rightarr\kern-\p@} |
| 2184 |
\@namedef{sd@arr@->}{\sd@rightarr} |
| 2185 |
\@namedef{sd@arr@-><}{\sd@rightarr\kern-\p@\sd@leftarr} |
| 2186 |
\@namedef{sd@arr@...}{$\cdots$} |
| 2187 |
\@namedef{sd@arr@-}{} |
| 2188 |
% \end{macrocode} |
| 2189 |
% |
| 2190 |
% \end{macro} |
| 2191 |
% |
| 2192 |
% \begin{macro}{\sd@newline} |
| 2193 |
% |
| 2194 |
% The line breaking within a syntax diagram is controlled by the |
| 2195 |
% |\sd@newline| command, to which |\\| is assigned. |
| 2196 |
% |
| 2197 |
% We support all the standard \LaTeX\ features here. The line breaking |
| 2198 |
% involves adding a fill skip and arrow, moving to the next line, adding |
| 2199 |
% an arrow and a rule, and continuing. |
| 2200 |
% |
| 2201 |
% \begin{macrocode} |
| 2202 |
\def\sd@newline{\@ifstar{\vadjust{\penalty\@M}\sd@nl@i}\sd@nl@i} |
| 2203 |
\def\sd@nl@i{\@ifnextchar[\sd@nl@ii\sd@nl@iii} |
| 2204 |
\def\sd@nl@ii[#1]{\vspace{#1}\sd@nl@iii} |
| 2205 |
\def\sd@nl@iii{% |
| 2206 |
\nobreak\sd@rule\hskip\sdmidskip% |
| 2207 |
\sd@rule\hskip\sdfinalskip% |
| 2208 |
\kern-3\p@% |
| 2209 |
\sd@rightarr% |
| 2210 |
\newline% |
| 2211 |
\sd@rightarr% |
| 2212 |
\nobreak\sd@rule\hskip\sdstartspace% |
| 2213 |
\sd@rule\hskip3.5\p@% |
| 2214 |
} |
| 2215 |
% \end{macrocode} |
| 2216 |
% |
| 2217 |
% \end{macro} |
| 2218 |
% |
| 2219 |
% \subsubsection{Putting things in the right place} |
| 2220 |
% |
| 2221 |
% Syntax diagrams have fairly stiff requirements on the positioning of text |
| 2222 |
% relative to the diagram's rules. To help people (and me) to write |
| 2223 |
% extensions to the syntax diagram typesetting which automatically put things |
| 2224 |
% in the right place, I provide some simple macros. |
| 2225 |
% |
| 2226 |
% \begin{environment}{sdbox} |
| 2227 |
% |
| 2228 |
% By placing some text in the \env{sdbox} environment, it will be read into a |
| 2229 |
% box and then output at the correct height for the syntax diagram. Note |
| 2230 |
% that stuff in the box is set in horizontal (LR) mode, so you'll have to use |
| 2231 |
% a \env{minipage} if you want formatted text. The macro also supplies rules |
| 2232 |
% on either side of the box, with a length given in the environment's |
| 2233 |
% argument. |
| 2234 |
% |
| 2235 |
% Macro writers are given explicit permission to use this environment through |
| 2236 |
% the |\sdbox| and |\endsdbox| commands if this makes life easier. |
| 2237 |
% |
| 2238 |
% The calculation in the |\endsdbox| macro works out how to centre the box |
| 2239 |
% vertically over the baseline. If the box's height is~$h$, and its depth |
| 2240 |
% is~$d$, then its centre-line is $(h+d)/2$ from the bottom of the box. |
| 2241 |
% Since the baseline is already $d$ from the bottom, we need to lower the box |
| 2242 |
% by $(h+d)/2 - d$, or $h/2-d/2$. |
| 2243 |
% |
| 2244 |
% \begin{macrocode} |
| 2245 |
\def\sdbox#1{% |
| 2246 |
\@tempskipa#1\relax% |
| 2247 |
\sd@gap\@tempskipa% |
| 2248 |
\setbox\z@\hbox\bgroup% |
| 2249 |
\begingroup% |
| 2250 |
\catcode`\ 10% |
| 2251 |
\catcode`\^^M5% |
| 2252 |
\synshortsoff% |
| 2253 |
} |
| 2254 |
\def\endsdbox{% |
| 2255 |
\endgroup% |
| 2256 |
\egroup% |
| 2257 |
\@tempdima\ht\z@% |
| 2258 |
\advance\@tempdima-\dp\z@% |
| 2259 |
\advance\@tempdima-\tw@\sd@mid% |
| 2260 |
\lower.5\@tempdima\box\z@% |
| 2261 |
\sd@gap\@tempskipa% |
| 2262 |
} |
| 2263 |
% \end{macrocode} |
| 2264 |
% |
| 2265 |
% \end{environment} |
| 2266 |
% |
| 2267 |
% \subsubsection{Typesetting syntactic items} |
| 2268 |
% |
| 2269 |
% Using the hooks built into the syntax abbreviations above, we typeset |
| 2270 |
% the text into a box, and write it out, centred over the baseline. A strut |
| 2271 |
% helps to keep the actual text baselines level for short pieces of text. |
| 2272 |
% |
| 2273 |
% \begin{macro}{\sd@tok@i} |
| 2274 |
% |
| 2275 |
% The preamble for a syntax abbreviation. We start a box, and set the |
| 2276 |
% space and return characters to work again. A strut is added to the box to |
| 2277 |
% ensure correct vertical spacing for normal text. |
| 2278 |
% |
| 2279 |
% \begin{macrocode} |
| 2280 |
\def\sd@tok@i{% |
| 2281 |
\sdbox\sdtokskip% |
| 2282 |
\strut% |
| 2283 |
\space% |
| 2284 |
} |
| 2285 |
% \end{macrocode} |
| 2286 |
% |
| 2287 |
% \end{macro} |
| 2288 |
% |
| 2289 |
% \begin{macro}{\sd@tok@ii} |
| 2290 |
% |
| 2291 |
% \begin{macrocode} |
| 2292 |
\def\sd@tok@ii{% |
| 2293 |
\space% |
| 2294 |
\endsdbox% |
| 2295 |
} |
| 2296 |
% \end{macrocode} |
| 2297 |
% |
| 2298 |
% \end{macro} |
| 2299 |
% |
| 2300 |
% \subsubsection{Inserting other pieces of text} |
| 2301 |
% |
| 2302 |
% Arbitrary text may be put into a syntax diagram through the use of the |
| 2303 |
% |\tok| macro. Its `argument' is typeset in the same way as a syntactic |
| 2304 |
% item (centred over the baseline). The implementation goes to some effort |
| 2305 |
% to ensure that the text is not actually an argument, to allow category |
| 2306 |
% codes to change while the text is being typeset. |
| 2307 |
% |
| 2308 |
% \begin{macro}{\tok} |
| 2309 |
% |
| 2310 |
% We start a box, and make space and return do their normal jobs. We use |
| 2311 |
% |\aftergroup| to regain control once the box is finished. |\doafter| is |
| 2312 |
% used to get control after the group finishes. |
| 2313 |
% |
| 2314 |
% \begin{macrocode} |
| 2315 |
\def\tok#{% |
| 2316 |
\sdbox\sdtokskip% |
| 2317 |
\strut% |
| 2318 |
\enspace% |
| 2319 |
\syntaxShortcuts\relax\relax% |
| 2320 |
\doafter\sd@tok% |
| 2321 |
} |
| 2322 |
% \end{macrocode} |
| 2323 |
% |
| 2324 |
% The |\sd@tok| macro is similar to |\sd@tok@ii| above. |
| 2325 |
% |
| 2326 |
% \begin{macrocode} |
| 2327 |
\def\sd@tok{% |
| 2328 |
\enspace% |
| 2329 |
\endsdbox% |
| 2330 |
} |
| 2331 |
% \end{macrocode} |
| 2332 |
% |
| 2333 |
% \end{macro} |
| 2334 |
% |
| 2335 |
% \subsubsection{The \protect\env{stack} environment} |
| 2336 |
% |
| 2337 |
% The \env{stack} environment is used to present alternatives in a syntax |
| 2338 |
% diagram. The alternatives are separated by |\\| commands. |
| 2339 |
% |
| 2340 |
% \begin{macro}{\stack} |
| 2341 |
% |
| 2342 |
% The optional positioning argument is handled using \LaTeX's |\newcommand| |
| 2343 |
% mechanism. |
| 2344 |
% |
| 2345 |
% \begin{macrocode} |
| 2346 |
\newcommand\stack[1][t]{% |
| 2347 |
% \end{macrocode} |
| 2348 |
% |
| 2349 |
% First, we add some horizontal space. |
| 2350 |
% |
| 2351 |
% \begin{macrocode} |
| 2352 |
\sd@gap\sdmidskip% |
| 2353 |
% \end{macrocode} |
| 2354 |
% |
| 2355 |
% We're within a complex construction, so we need to clear the |\ifsd@base| |
| 2356 |
% flag. |
| 2357 |
% |
| 2358 |
% \begin{macrocode} |
| 2359 |
\begingroup\sd@basefalse% |
| 2360 |
% \end{macrocode} |
| 2361 |
% |
| 2362 |
% The top and bottom rows of the stack are different to the others, since |
| 2363 |
% the vertical rules mustn't extend all the way up the side of the item. |
| 2364 |
% The bottom row is handled separately by |\endstack| below. The top row |
| 2365 |
% must be handled via a flag, |\ifsd@toplayer|. |
| 2366 |
% |
| 2367 |
% Initially, the flag must be set true. |
| 2368 |
% |
| 2369 |
% \begin{macrocode} |
| 2370 |
\sd@toplayertrue% |
| 2371 |
% \end{macrocode} |
| 2372 |
% |
| 2373 |
% We set the |\\| command to separate the items in the |\halign|. |
| 2374 |
% |
| 2375 |
% \begin{macrocode} |
| 2376 |
\let\\\sd@stackcr% |
| 2377 |
% \end{macrocode} |
| 2378 |
% |
| 2379 |
% The actual structure must be set in vertical mode, so we must place it |
| 2380 |
% in a box. The position argument determines whether this must be a |
| 2381 |
% |\vbox| or a |\vtop|. We also insert a bit of rounding if the options say |
| 2382 |
% we must. |
| 2383 |
% |
| 2384 |
% \begin{macrocode} |
| 2385 |
\if#1t% |
| 2386 |
\let\@tempa\vtop% |
| 2387 |
\sd@toptrue% |
| 2388 |
\ifsd@round\llap{\sd@trcirc\kern\tw@\sdrulewidth}\fi% |
| 2389 |
\else\if#1b% |
| 2390 |
\let\@tempa\vbox% |
| 2391 |
\sd@topfalse% |
| 2392 |
\ifsd@round\llap{\sd@brcirc\kern\tw@\sdrulewidth}\fi% |
| 2393 |
\else% |
| 2394 |
\sd@err{Bad position argument passed to stack}% |
| 2395 |
{The positioning argument must be one of `t' or `b'. I% |
| 2396 |
have^^Jassumed you meant to type `t'.}% |
| 2397 |
\let\@tempa\vtop% |
| 2398 |
\fi\fi% |
| 2399 |
% \end{macrocode} |
| 2400 |
% |
| 2401 |
% Now we start the box, which we will complete at the end of the environment. |
| 2402 |
% |
| 2403 |
% \begin{macrocode} |
| 2404 |
\@tempa\bgroup% |
| 2405 |
% \end{macrocode} |
| 2406 |
% |
| 2407 |
% We must remove any extra space between rows of the table, since the rules |
| 2408 |
% will not join up correctly. We can use |\offinterlineskip| safely, since |
| 2409 |
% each individual row contains a strut. |
| 2410 |
% |
| 2411 |
% \begin{macrocode} |
| 2412 |
\offinterlineskip% |
| 2413 |
% \end{macrocode} |
| 2414 |
% |
| 2415 |
% Now we can start the alignment. We actually use \PlainTeX's |\ialign| |
| 2416 |
% macro, which also clears |\tabskip| for us. |
| 2417 |
% |
| 2418 |
% \begin{macrocode} |
| 2419 |
\ialign\bgroup% |
| 2420 |
% \end{macrocode} |
| 2421 |
% |
| 2422 |
% The preamble is trivial, since we must do all of the work ourselves |
| 2423 |
% |
| 2424 |
% \begin{macrocode} |
| 2425 |
##\cr% |
| 2426 |
% \end{macrocode} |
| 2427 |
% |
| 2428 |
% We can now start putting the text into a box ready for typesetting later. |
| 2429 |
% The strut makes the vertical spacing correct. |
| 2430 |
% |
| 2431 |
% \begin{macrocode} |
| 2432 |
\setbox\z@\hbox\bgroup% |
| 2433 |
\strut% |
| 2434 |
} |
| 2435 |
% \end{macrocode} |
| 2436 |
% |
| 2437 |
% \end{macro} |
| 2438 |
% |
| 2439 |
% \begin{macro}{\endstack} |
| 2440 |
% |
| 2441 |
% The first part of this is similar to the |\sd@stackcr| macro below, except |
| 2442 |
% that the vertical rules are different. We don't support rounded edges |
| 2443 |
% on single-row stacks, although this isn't a great loss to humanity. |
| 2444 |
% |
| 2445 |
% \begin{macrocode} |
| 2446 |
\def\endstack{% |
| 2447 |
\egroup% |
| 2448 |
\ifsd@toplayer% |
| 2449 |
\sd@dostack\sd@upper\sd@lower\relax\relax% |
| 2450 |
\else% |
| 2451 |
\ifsd@round% |
| 2452 |
\ifsd@top% |
| 2453 |
\sd@dostack{\ht\z@}\sd@botcirc\sd@blcirc\sd@brcirc% |
| 2454 |
\else% |
| 2455 |
\sd@dostack{\ht\z@}\sd@botcirc\relax\relax% |
| 2456 |
\fi% |
| 2457 |
\else% |
| 2458 |
\sd@dostack{\ht\z@}\sd@lower\relax\relax% |
| 2459 |
\fi% |
| 2460 |
\fi% |
| 2461 |
% \end{macrocode} |
| 2462 |
% |
| 2463 |
% We now close the |\halign| and the vbox we created. |
| 2464 |
% |
| 2465 |
% \begin{macrocode} |
| 2466 |
\egroup% |
| 2467 |
\egroup% |
| 2468 |
% \end{macrocode} |
| 2469 |
% |
| 2470 |
% Deal with any rounding we started off. |
| 2471 |
% |
| 2472 |
% \begin{macrocode} |
| 2473 |
\ifsd@round% |
| 2474 |
\ifsd@top |
| 2475 |
\rlap{\kern\tw@\sdrulewidth\sd@tlcirc}% |
| 2476 |
\else% |
| 2477 |
\rlap{\kern\tw@\sdrulewidth\sd@blcirc}% |
| 2478 |
\fi% |
| 2479 |
\fi% |
| 2480 |
% \end{macrocode} |
| 2481 |
% |
| 2482 |
% Finally, we add some horizontal glue to space the diagram out. |
| 2483 |
% |
| 2484 |
% \begin{macrocode} |
| 2485 |
\endgroup\sd@gap\sdmidskip% |
| 2486 |
} |
| 2487 |
% \end{macrocode} |
| 2488 |
% |
| 2489 |
% \end{macro} |
| 2490 |
% |
| 2491 |
% \begin{macro}{\sd@stackcr} |
| 2492 |
% |
| 2493 |
% The |\\| command is set to this macro during a \env{stack} environment. |
| 2494 |
% |
| 2495 |
% \begin{macrocode} |
| 2496 |
\def\sd@stackcr{% |
| 2497 |
% \end{macrocode} |
| 2498 |
% |
| 2499 |
% The first job is to close the box containing the previous item. |
| 2500 |
% |
| 2501 |
% \begin{macrocode} |
| 2502 |
\egroup% |
| 2503 |
% \end{macrocode} |
| 2504 |
% |
| 2505 |
% Now we typeset the vertical rules differently depending on whether this is |
| 2506 |
% the first item in the stack. This looks quite terrifying initially, but |
| 2507 |
% it's just an enumeration of the possible cases for the different values |
| 2508 |
% of |\ifsd@toplayer|, |\ifsd@top| and |\ifsd@round|, putting in appropriate |
| 2509 |
% rules and arcs in the right places. |
| 2510 |
% |
| 2511 |
% \begin{macrocode} |
| 2512 |
\ifsd@toplayer% |
| 2513 |
\ifsd@round% |
| 2514 |
\ifsd@top% |
| 2515 |
\sd@dostack\sd@topcirc{\dp\z@}\relax\relax% |
| 2516 |
\else% |
| 2517 |
\sd@dostack\sd@topcirc{\dp\z@}\sd@tlcirc\sd@trcirc% |
| 2518 |
\fi% |
| 2519 |
\else% |
| 2520 |
\sd@dostack\sd@upper{\dp\z@}\relax\relax% |
| 2521 |
\fi% |
| 2522 |
\else% |
| 2523 |
\ifsd@round% |
| 2524 |
\ifsd@top% |
| 2525 |
\sd@dostack{\ht\z@}{\dp\z@}\sd@blcirc\sd@brcirc% |
| 2526 |
\else% |
| 2527 |
\sd@dostack{\ht\z@}{\dp\z@}\sd@tlcirc\sd@trcirc% |
| 2528 |
\fi% |
| 2529 |
\else% |
| 2530 |
\sd@dostack{\ht\z@}{\dp\z@}\relax\relax% |
| 2531 |
\fi% |
| 2532 |
\fi% |
| 2533 |
% \end{macrocode} |
| 2534 |
% |
| 2535 |
% The next item won't be the first, so we clear the flag. |
| 2536 |
% |
| 2537 |
% \begin{macrocode} |
| 2538 |
\sd@toplayerfalse% |
| 2539 |
% \end{macrocode} |
| 2540 |
% |
| 2541 |
% Now we have to set up the next cell. We put the text into a box again. |
| 2542 |
% |
| 2543 |
% \begin{macrocode} |
| 2544 |
\setbox\z@\hbox\bgroup% |
| 2545 |
\strut% |
| 2546 |
} |
| 2547 |
% \end{macrocode} |
| 2548 |
% |
| 2549 |
% \end{macro} |
| 2550 |
% |
| 2551 |
% \begin{macro}{\sd@dostack} |
| 2552 |
% |
| 2553 |
% Actually typesetting the text in a cell is performed here. The macro is |
| 2554 |
% called as |
| 2555 |
% \begin{quote}\synshorts |
| 2556 |
% "\\sd@dostack{"<height>"}{"<depth>"}{"<left-arc>"}{"<right-arc>"}" |
| 2557 |
% \end{quote} |
| 2558 |
% where \<height> and \<depth> are the height and depth of the vertical |
| 2559 |
% rules to put around the item, and \<left-arc> and \<right-arc> are |
| 2560 |
% commands to draw rounded edges on the left and right hand sides of the |
| 2561 |
% item. |
| 2562 |
% |
| 2563 |
% The values for the height and depth are quite often going to be the height |
| 2564 |
% and depth of box~0. Since we empty box~0 in the course of typesetting the |
| 2565 |
% row, we need to cache the sizes on entry. |
| 2566 |
% |
| 2567 |
% \begin{macrocode} |
| 2568 |
\def\sd@dostack#1#2#3#4{% |
| 2569 |
\@tempdima#1% |
| 2570 |
\@tempdimb#2% |
| 2571 |
\kern-\tw@\sdrulewidth% |
| 2572 |
\vrule\@height\@tempdima\@depth\@tempdimb\@width\tw@\sdrulewidth% |
| 2573 |
#3% |
| 2574 |
\sd@rule\hfill% |
| 2575 |
\sd@gap\sdtokskip% |
| 2576 |
\unhbox\z@% |
| 2577 |
\sd@gap\sdtokskip% |
| 2578 |
\sd@rule\hfill% |
| 2579 |
#4% |
| 2580 |
\vrule\@height\@tempdima\@depth\@tempdimb\@width\tw@\sdrulewidth% |
| 2581 |
\kern-\tw@\sdrulewidth% |
| 2582 |
\cr% |
| 2583 |
} |
| 2584 |
% \end{macrocode} |
| 2585 |
% |
| 2586 |
% \end{macro} |
| 2587 |
% |
| 2588 |
% \subsubsection{The \protect\env{rep} environment} |
| 2589 |
% |
| 2590 |
% The \env{rep} environment is used for typesetting loops in the diagram. |
| 2591 |
% Again, we use |\halign| for the typesetting. Loops are simpler than |
| 2592 |
% stacks, however, since there are always two rows. We store both rows in |
| 2593 |
% box registers, and build the loop at the end. |
| 2594 |
% |
| 2595 |
% \begin{macro}{\rep} |
| 2596 |
% |
| 2597 |
% Again, we use |\newcommand| to process the optional argument. |
| 2598 |
% |
| 2599 |
% \begin{macrocode} |
| 2600 |
\newcommand\rep[1][t]{% |
| 2601 |
% \end{macrocode} |
| 2602 |
% |
| 2603 |
% First, leave a gap on the left side. |
| 2604 |
% |
| 2605 |
% \begin{macrocode} |
| 2606 |
\sd@gap\sdmidskip% |
| 2607 |
% \end{macrocode} |
| 2608 |
% |
| 2609 |
% We're not at base level any more, so disable linebreaking. |
| 2610 |
% |
| 2611 |
% \begin{macrocode} |
| 2612 |
\begingroup\sd@basefalse% |
| 2613 |
% \end{macrocode} |
| 2614 |
% |
| 2615 |
% Remember we're going backwards now. |
| 2616 |
% |
| 2617 |
% \begin{macrocode} |
| 2618 |
\ifsd@backwards\sd@backwardsfalse\else\sd@backwardstrue\fi% |
| 2619 |
% \end{macrocode} |
| 2620 |
% |
| 2621 |
% Define |\\| to separate the two parts of the loop. |
| 2622 |
% |
| 2623 |
% \begin{macrocode} |
| 2624 |
\let\\\sd@loop% |
| 2625 |
% \end{macrocode} |
| 2626 |
% |
| 2627 |
% Now check the argument, and use the appropriate type of box. In addition |
| 2628 |
% to changing the typesetting, we must remember which way up to typeset the |
| 2629 |
% loop, since the end code must always put the first argument on the |
| 2630 |
% baseline, with the loop either above or below. |
| 2631 |
% |
| 2632 |
% \begin{macrocode} |
| 2633 |
\if#1t% |
| 2634 |
\let\@tempa\vbox% |
| 2635 |
\sd@toptrue% |
| 2636 |
\else\if#1b% |
| 2637 |
\let\@tempa\vtop% |
| 2638 |
\sd@topfalse% |
| 2639 |
\else% |
| 2640 |
\sd@err{Bad position argument passed to loop}% |
| 2641 |
{The positioning argument must be `t' or `b'. I have^^J% |
| 2642 |
assumed you meant to type `t'.}% |
| 2643 |
\let\@tempa\vbox% |
| 2644 |
\sd@toptrue% |
| 2645 |
\fi\fi% |
| 2646 |
% \end{macrocode} |
| 2647 |
% |
| 2648 |
% Now we start the box. |
| 2649 |
% |
| 2650 |
% \begin{macrocode} |
| 2651 |
\@tempa\bgroup% |
| 2652 |
% \end{macrocode} |
| 2653 |
% |
| 2654 |
% The loop is by default empty, apart from a strut. This is put into box~1. |
| 2655 |
% |
| 2656 |
% \begin{macrocode} |
| 2657 |
\setbox\tw@\copy\strutbox% |
| 2658 |
% \end{macrocode} |
| 2659 |
% |
| 2660 |
% Now start typesetting the main text in box~0. |
| 2661 |
% |
| 2662 |
% \begin{macrocode} |
| 2663 |
\setbox\z@\hbox\bgroup\strut% |
| 2664 |
} |
| 2665 |
% \end{macrocode} |
| 2666 |
% |
| 2667 |
% \end{macro} |
| 2668 |
% |
| 2669 |
% \begin{macro}{\endrep} |
| 2670 |
% |
| 2671 |
% The final code must first close whatever box was open. |
| 2672 |
% |
| 2673 |
% \begin{macrocode} |
| 2674 |
\def\endrep{% |
| 2675 |
\egroup% |
| 2676 |
% \end{macrocode} |
| 2677 |
% |
| 2678 |
% Now we typeset the loop, depending on which way up it was meant to be. |
| 2679 |
% Again, this terrifying piece of code is a simple list of possibile values |
| 2680 |
% of our various flags. |
| 2681 |
% |
| 2682 |
% \begin{macrocode} |
| 2683 |
\ifsd@top% |
| 2684 |
\ifsd@round% |
| 2685 |
\sd@doloop\tw@\z@\relax\relax% |
| 2686 |
\sd@tlcirc\sd@trcirc{\sd@rlc\sd@blcirc}{\sd@llc\sd@brcirc}% |
| 2687 |
\else% |
| 2688 |
\sd@doloop\tw@\z@\relax\sd@downarr\relax\relax\relax\relax% |
| 2689 |
\fi% |
| 2690 |
\else% |
| 2691 |
\ifsd@round% |
| 2692 |
\sd@doloop\z@\tw@\relax\relax% |
| 2693 |
{\sd@rlc\sd@tlcirc}{\sd@llc\sd@trcirc}\sd@blcirc\sd@brcirc% |
| 2694 |
\else% |
| 2695 |
\sd@doloop\z@\tw@\sd@uparr\relax\relax\relax\relax\relax% |
| 2696 |
\fi% |
| 2697 |
\fi% |
| 2698 |
% \end{macrocode} |
| 2699 |
% |
| 2700 |
% Close the vbox we opened. |
| 2701 |
% |
| 2702 |
% \begin{macrocode} |
| 2703 |
\egroup% |
| 2704 |
% \end{macrocode} |
| 2705 |
% |
| 2706 |
% Finally, we leave a gap before the next structure. |
| 2707 |
% |
| 2708 |
% \begin{macrocode} |
| 2709 |
\endgroup\sd@gap\sdmidskip% |
| 2710 |
} |
| 2711 |
% \end{macrocode} |
| 2712 |
% |
| 2713 |
% \end{macro} |
| 2714 |
% |
| 2715 |
% \begin{macro}{\sd@loop} |
| 2716 |
% |
| 2717 |
% This macro handles the |\\| command within a loop environment. We close |
| 2718 |
% the current box, and start filling in box~1. We also redefine |\\| to |
| 2719 |
% raise an error when the |\\| command is used again. |
| 2720 |
% |
| 2721 |
% \begin{macrocode} |
| 2722 |
\def\sd@loop{% |
| 2723 |
\egroup% |
| 2724 |
\def\\{\sd@err{Too many \string\\\space commands in loop}\@ehc}% |
| 2725 |
\setbox\tw@\hbox\bgroup\strut% |
| 2726 |
} |
| 2727 |
% \end{macrocode} |
| 2728 |
% |
| 2729 |
% \end{macro} |
| 2730 |
% |
| 2731 |
% \begin{macro}{\sd@doloop} |
| 2732 |
% |
| 2733 |
% This is the macro which actually creates the |\halign| for the loop. It |
| 2734 |
% is called with four arguments, as: |
| 2735 |
% \begin{quote}\synshorts |
| 2736 |
% "\\sd@doloop{"<top-box>"}{"<bottom-box>"}"^^A |
| 2737 |
% "{"<top-arrow>"}{"<btm-arrow>"}" \\ |
| 2738 |
% \hbox{}\quad "{"<top-left-arc>"}{"<top-right-arc>"}"^^A |
| 2739 |
% "{"<bottom-left-arc>"}{"<btm-right-arc>"}"^^A |
| 2740 |
% \kern-1in ^^A It may be overfull, but it looks OK to me ;-) |
| 2741 |
% \end{quote} |
| 2742 |
% |
| 2743 |
% The two \<box> arguments give the numbers of boxes to extract in the top |
| 2744 |
% and bottom rows of the alignment. The \<arrow> arguments specify |
| 2745 |
% characters to typeset at the end of the top and bottom rows for arrows. |
| 2746 |
% The various \<arc> arguments are commands which typeset arcs around the |
| 2747 |
% various parts of the items. |
| 2748 |
% |
| 2749 |
% We calculate the height and depth of the two boxes, and store them in |
| 2750 |
% \<dimen> registers, because the boxes are emptied before the right-hand |
| 2751 |
% rules are typeset. |
| 2752 |
% |
| 2753 |
% Actually, the two rows of the alignment are typeset in a different macro: |
| 2754 |
% we just pass the correct information on. |
| 2755 |
% |
| 2756 |
% \begin{macrocode} |
| 2757 |
\def\sd@doloop#1#2#3#4#5#6#7#8{% |
| 2758 |
\@tempdima\dp#1\relax% |
| 2759 |
\@tempdimb\ht#2\relax% |
| 2760 |
\offinterlineskip% |
| 2761 |
\ialign{% |
| 2762 |
##\cr% |
| 2763 |
\ifsd@round% |
| 2764 |
\sd@doloop@i#1#3\sd@topcirc\@tempdima{#5}{#6}% |
| 2765 |
\sd@doloop@i#2#4\@tempdimb\sd@botcirc{#7}{#8}% |
| 2766 |
\else% |
| 2767 |
\sd@doloop@i#1#3\sd@upper\@tempdima{#5}{#6}% |
| 2768 |
\sd@doloop@i#2#4\@tempdimb\sd@lower{#7}{#8}% |
| 2769 |
\fi% |
| 2770 |
}% |
| 2771 |
} |
| 2772 |
% \end{macrocode} |
| 2773 |
% |
| 2774 |
% \end{macro} |
| 2775 |
% |
| 2776 |
% \begin{macro}{\sd@doloop@i} |
| 2777 |
% |
| 2778 |
% Here we do the actual job of typesetting the rows of a loop alignment. |
| 2779 |
% The four arguments are: |
| 2780 |
% \begin{quote}\synshorts |
| 2781 |
% "\\sd@doloop@i{"<box>"}{"<arrow>"}"^^A |
| 2782 |
% "{"<rule-height>"}{"<rule-depth>"}" \\ |
| 2783 |
% \hbox{}\quad "{"<left-arc>"}{"<right-arc>"}"^^A |
| 2784 |
% \end{quote} |
| 2785 |
% |
| 2786 |
% The arrow position is determined by the |\ifsd@backwards| flag. The rest |
| 2787 |
% is fairly simple. |
| 2788 |
% |
| 2789 |
% \begin{macrocode} |
| 2790 |
\def\sd@doloop@i#1#2#3#4#5#6{% |
| 2791 |
\ifsd@backwards#2\fi% |
| 2792 |
\kern-\tw@\sdrulewidth% |
| 2793 |
\vrule\@height#3\@depth#4\@width\tw@\sdrulewidth% |
| 2794 |
#5% |
| 2795 |
\sd@rule\hfill% |
| 2796 |
\sd@gap\sdtokskip% |
| 2797 |
\unhbox#1% |
| 2798 |
\sd@gap\sdtokskip% |
| 2799 |
\sd@rule\hfill% |
| 2800 |
#6% |
| 2801 |
\vrule\@height#3\@depth#4\@width\tw@\sdrulewidth% |
| 2802 |
\ifsd@backwards\else#2\fi% |
| 2803 |
\kern-\tw@\sdrulewidth% |
| 2804 |
\cr% |
| 2805 |
} |
| 2806 |
% \end{macrocode} |
| 2807 |
% |
| 2808 |
% \end{macro} |
| 2809 |
% |
| 2810 |
% \subsection{The end} |
| 2811 |
% |
| 2812 |
% Phew! That's all of it completed. I hope this collection of commands |
| 2813 |
% and environments is of some help to someone. |
| 2814 |
% |
| 2815 |
% \begin{macrocode} |
| 2816 |
%</package> |
| 2817 |
% \end{macrocode} |
| 2818 |
% |
| 2819 |
% \hfill Mark Wooding, \today |
| 2820 |
% |
| 2821 |
% \Finale |
| 2822 |
% |
| 2823 |
\endinput |