Eliminate tabs from TeX input files.
[mdwtools] / syntax.dtx
index 627f8c8..ee0f4e3 100644 (file)
@@ -4,14 +4,14 @@
 %
 % Syntax typesetting package for LaTeX 2e
 %
-% (c) 1996 Mark Wooding
+% (c) 2003 Mark Wooding
 %
 % \end{meta-comment}
 %
 % \begin{meta-comment} <general public licence>
 %%
 %% syntax package -- typesetting syntax descriptions
-%% Copyright (c) 1996 Mark Wooding
+%% Copyright (c) 2003 Mark Wooding
 %%
 %% This program is free software; you can redistribute it and/or modify
 %% it under the terms of the GNU General Public License as published by
 % \begin{meta-comment} <Package preamble>
 %<+package>\NeedsTeXFormat{LaTeX2e}
 %<+package>\ProvidesPackage{syntax}
-%<+package>                [1996/05/17 1.07 Syntax typesetting (MDW)]
+%<+package>                [2003/08/25 1.08 Syntax typesetting (MDW)]
 % \end{meta-comment}
 %
-% ^^A \CheckSum{1504}
+% \CheckSum{1627}
 %% \CharacterTable
 %%  {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
 %%   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
 % \begin{listing}
 %\def\<#1>{\synt{#1}}
 % \end{listing}
-% to allow you to type "\\<"<text>">" as an alternative to 
+% to allow you to type "\\<"<text>">" as an alternative to
 % "\\synt{"<text>"}".
 %
 % \DescribeMacro\lit
 % blank lines.  You can use the normal |\\| command to perform line-breaking
 % of a production rule.  Note that a production rule must begin with a
 % nonterminal name enclosed in angle brackets (|<| \dots |>|), followed by
-% whitespace, then some kind of production operator (usually `::=') and then
-% some more whitespace.  You can control how this text is actually typeset,
-% however.
+% any decorative material, whitespace, some kind of production operator
+% (usually `::=') and then some more whitespace.  You can control how this
+% text is actually typeset, however.
 %
 % \DescribeMacro{\[[}
 % \DescribeMacro{\]]}
 % You can use syntax diagrams (see below) instead of a straight piece of BNF
-% by enclosing it in a |\[[| \dots |\]]| pair.  Note that you can't mix 
+% by enclosing it in a |\[[| \dots |\]]| pair.  Note that you can't mix
 % syntax diagrams and BNF in a production rule, and you will get something
 % which looks very strange if you try.
 %
 %
 % \end{description}
 %
-% \DescribeMacro\grammarlabel
+% \DescribeMacro\grammarlabelx
 % You can also control how the `label' is typeset by redefining the
-% |\grammarlabel| command.  The command is given two arguments: the name of
-% the nonterminal (which was enclosed in angle brackets), and the `production
-% operator'.  The command is expected to produce the label.  By default, it
-% typesets the nonterminal name using |\synt| and the operator at opposite
-% ends of the label, separated by an |\hfill|.
+% |\grammarlabelx| command.  The command is given three arguments: the name
+% of the nonterminal (which was enclosed in angle brackets), the following
+% decorative material, and the `production operator'.  The command is
+% expected to produce the label.  By default, it typesets the nonterminal
+% name using |\synt| followed by the decoration, and the operator, at
+% opposite ends of the label, separated by an |\hfill|.
+%
+% \DescribeMacro\grammarlabel
+% For compatibility, if there is no decorative material, the macro
+% |\grammarlabel| is called instead, with just two arguments: the nonterminal
+% name and the operator.  The default implementation of |\grammarlabel| just
+% calls |\grammarlabelx| with empty decoration.
 %
 % \subsection{Syntax diagrams}
 %
 % ^^A took to get the table below to look right: __6.  Hmm... not as bad
 % ^^A as I expected.  Most of them were fine-tuning things.
 %
-% \medskip                                     ^^A Leave a vertical gap
-% \hbox to\columnwidth{\hfil\vbox{\tabskip=0pt ^^A Centre it horizontally
-% \sdsize \csname sd@setsize\endcsname         ^^A Position syntdiag arrows
-% \halign to .5\columnwidth{                   ^^A Set the table width
+% \medskip                                      ^^A Leave a vertical gap
+% \hbox to\columnwidth{\hfil\vbox{\tabskip=0pt  ^^A Centre it horizontally
+% \sdsize \csname sd@setsize\endcsname          ^^A Position syntdiag arrows
+% \halign to .5\columnwidth{                    ^^A Set the table width
 %   &\ttfamily\ignorespaces#\unskip\hfil\tabskip=0pt ^^A Typeset the name
-%   &\quad\csname sd@arr@#\endcsname\hfil      ^^A Typeset the arrow
-%   &\setbox0=\hbox{#}\tabskip=0pt plus 1fil\cr        ^^A Stretch between columns
+%   &\quad\csname sd@arr@#\endcsname\hfil       ^^A Typeset the arrow
+%   &\setbox0=\hbox{#}\tabskip=0pt plus 1fil\cr ^^A Stretch between columns
 %   >>-&>>-&   &>-&>-&   &->&->\cr
 %   -><&-><&   &...&...& &-&-\cr
-% }}\hfil}                                     ^^A Close the boxing
-% \medskip                                     ^^A And leave another gap
+% }}\hfil}                                      ^^A Close the boxing
+% \medskip                                      ^^A And leave another gap
 %
 % These declarations should be used only in the optional argument to the
 % \env{syntdiag$*$} command.  The second optional argument to the
 %
 % \DescribeMacro\tok
 % You can also include text using the |\tok| command.  The argument of this
-% command is typeset in \LaTeX's LR~mode and inserted into the diagram. 
+% command is typeset in \LaTeX's LR~mode and inserted into the diagram.
 % Syntax abbreviations are allowed within the argument, so you can, for
 % example, include textual descriptions like
 % \begin{listing}
 %
 % Note that items on the backwards arrow of a \env{rep} construction should
 % be displayed \emph{backwards}.  You must put the individual items in
-% reverse order when building this part of your diagrams.  \syn\ will 
+% reverse order when building this part of your diagrams.  \syn\ will
 % correctly reverse the arrows on \env{rep} structures, but apart from
 % this, you must cope on your own.  You are recommended to keep these parts
 % of your diagrams as simple as possible to avoid confusing readers.
 %
 % You can also alter the appearance of \env{stack}s and \env{rep}s by using
 % their optional positioning arguments.  By default, \env{stack}s descend
-% below the main line of the diagram, and \env{rep}s extend above it.  
+% below the main line of the diagram, and \env{rep}s extend above it.
 % Specifying an optional argument of |[b]| for either environment reverses
 % this, putting \env{stack}s above and \env{rep}s below the line.
 %
 % |\lit| and |\lit*|) has two style commands associated with it, as shown
 % in the table below.
 %
-% \begin{tab}{lll}                                                \hline
-% \bf Syntax item      & \bf Left command & \bf Right command  \\ \hline
-% Nonterminals         & |\syntleft|      & |\syntright|       \\
-% Quoted terminals     & |\litleft|       & |\litright|        \\
-% Unquoted terminals   & |\ulitleft|      & |\ulitright|       \\ \hline
+% \begin{tab}{lll}                                                 \hline
+% \bf Syntax item       & \bf Left command & \bf Right command  \\ \hline
+% Nonterminals          & |\syntleft|      & |\syntright|       \\
+% Quoted terminals      & |\litleft|       & |\litright|        \\
+% Unquoted terminals    & |\ulitleft|      & |\ulitright|       \\ \hline
 % \end{tab}
 %
 % It's not too hard to see how this works.  For example, if you look at
 % commands, in \env{grammar} environments, and in syntax diagrams.
 %
 %
-% \section{Change history}
-%
-% \subsection*{Version 1.07}
-%
-% \begin{itemize}
-% \item Fixed problem with underscore hacking in a \env{tabbing} environment.
-% \end{itemize}
-%
-% \subsection*{Version 1.06}
-%
-% \begin{itemize}
-% \item Added style hooks for syntax items.
-% \item Improved colour handling in syntax diagrams, thanks to the |\doafter|
-%       package.
-% \item Fixed some nasty bugs in the \env{grammar} environment which confused
-%       other lists and ruined the spacing.  The \env{grammar} handling is
-%       now much tidier in general.
-% \end{itemize}
-%
-% \subsection*{Version 1.05}
-%
-% \begin{itemize}
-% \item Fixed `the bug' in the syntax diagram typesetting.  It now breaks
-%       lines almost psychically, and doesn't break in the wrong places.
-% \item Almost rewrote the \env{grammar} environment.  It now does lots of
-%       the list handling itself, to allow more versatile typesetting of the
-%       left hand sides.  There's lots of evil in there now.
-% \item Added some more configurability.  In particular, two new settings
-%       have been added to control \env{grammar} environments, and a neat
-%       way of adding new syntax diagram structures has been introduced.
-% \end{itemize}
-%
-% \subsection*{Version 1.04}
-%
-% \begin{itemize}
-% \item Changed the vertical positioning of the rules, to make all the text
-%       line up properly.  While the old version was elegant and simple, it
-%       had the drawback of looking nasty.
-% \item Allow line breaks at underscores, but don't if there's another one
-%       afterwards.  Also, prevent losing following space if underscore is
-%       written to a file.
-% \end{itemize}
-%
-% \subsection*{Version 1.02}
-%
-% \begin{itemize}
-% \item Added support for rounded corners in syntax diagrams.
-% \item Changed lots of |\hskip| commands to |\kern|s, to prevent possible
-%       line breaks.
-% \end{itemize}
-%
-% \subsection*{Version 1.01}
-%
-% \begin{itemize}
-% \item Allowed disabling of underscore active character, to avoid messing
-%       up filenames.
-% \item Added |\grammarparsep| and |\grammarindent| length parameters to
-%       control the appearance of grammars.
-% \end{itemize}
-%
 % \implementation
 %
 % \section{Implementation of \syn}
 % characters through.  By default, we re-enable |\|, and make `\verb*" "'
 % typeset some space glue, rather than a space character.  A macro
 % `\verb*"\ "' is defined to actually print a space character, which yield
-% `\verb*" "' in the `|\tt|' font. 
+% `\verb*" "' in the `|\tt|' font.
 %
 % Finally, it defines a |\ch| command, which, given a single-character
 % control sequence as its argument, typesets the character.  This is useful,
   \addspecial\|%
   \addspecial\"%
   \catcode`\|\active%
-  \catcode`\<\active% 
+  \catcode`\<\active%
   \catcode`\"\active%
   \catcode`\`\active%
 }
 % commands.  Well, almost \dots
 %
 %    \begin{macrocode}
-\newcommand{\grammarlabel}[2]{%
-  \synt{#1} \hfill#2%
-}
+\newcommand{\grammarlabel}[2]{\grammarlabelx{#1}{}{#2}}
+\newcommand{\grammarlabelx}[3]{\synt{#1}#2 \hfill#3}
 %    \end{macrocode}
 %
 % Now for a bit of hacking to make the item stuff work properly.  This gets
 % strut just makes sure that I know exactly how high the line is.
 %
 %    \begin{macrocode}
-\def\gr@implitem<#1> #2 {%
-  \sbox\z@{\hskip\labelsep\grammarlabel{#1}{#2}}%
+\def\gr@implitem<#1>#2 #3 {%
+  \sbox\z@{%
+    \hskip\labelsep%
+    \def\@tempa{#2}%
+    \ifx\@tempa\@empty\grammarlabel{#1}{#3}%
+    \else\grammarlabelx{#1}{#2}{#3}\fi%
+  }%
   \strut\@@par%
   \vskip-\parskip%
   \vskip-\baselineskip%
 }
 \def\sd@leftarr{%
   \bgroup%
-  \raise\sd@mid\hbox{\@linefnt\char'33\kern-6\p@}%
+  \setbox\tw@\hbox{\@linefnt\char'33\kern-6\p@}%
   \sd@arrow%
 }
 %    \end{macrocode}