| 1 |
% |
| 2 |
% This is derived from alltt.sty |
| 3 |
% |
| 4 |
\NeedsTeXFormat{LaTeX2e} |
| 5 |
\ProvidesPackage{sml}[1998/12/17 defines sml environment] |
| 6 |
|
| 7 |
% |
| 8 |
% Font for typesetting sml code, default is \tt |
| 9 |
% |
| 10 |
\newcommand{\smlFont}{\verbatim@font} |
| 11 |
\newcommand{\smlCommentSize}{\small} |
| 12 |
\newcommand{\smlCommentFont}{\it} |
| 13 |
\newcommand{\smlNumberFont}{\smlFont} |
| 14 |
\newcommand{\smlNumberStyle}[1]{\arabic{#1}} |
| 15 |
\newcommand{\sml@font}{\smlFont} |
| 16 |
|
| 17 |
|
| 18 |
% |
| 19 |
% Turn on and off dollar sign |
| 20 |
% |
| 21 |
\newif\ifsml@dollar |
| 22 |
\newcommand{\smlDollarOn}{\sml@dollartrue} |
| 23 |
\newcommand{\smlDollarOff}{\sml@dollarfalse} |
| 24 |
\smlDollarOff |
| 25 |
|
| 26 |
% |
| 27 |
% Font for typesetting sml type variables, default is \it |
| 28 |
% |
| 29 |
\newcommand{\smlTypeVarFont}{\it} |
| 30 |
|
| 31 |
% |
| 32 |
% Commands for typesetting type variables and keywords. |
| 33 |
% Can be overridden by user. |
| 34 |
% |
| 35 |
\newcommand{\makeSmlTypeVar}[1]{'{\smlTypeVarFont#1}} |
| 36 |
\newcommand{\makeSmlKeyword}[1]{{\bf#1}} |
| 37 |
\newcommand{\BeginSmlComment}{\begingroup\smlCommentSize\smlCommentFont} |
| 38 |
\newcommand{\EndSmlComment}{\endgroup} |
| 39 |
|
| 40 |
% |
| 41 |
% Command to declare a new type variable translation |
| 42 |
% and keywords |
| 43 |
% |
| 44 |
\newcommand{\smlTypeVar}[2]{ |
| 45 |
\expandafter\newcommand\csname smltv@#1\endcsname{#2} |
| 46 |
} |
| 47 |
\newcommand{\smlRemoveTypeVar}[1]{ |
| 48 |
\expandafter\newcommand\csname smltv@#1\endcsname\relax |
| 49 |
} |
| 50 |
\newcommand{\smlNewKeyword}[1]{ |
| 51 |
\expandafter\def\csname smlkw@#1\endcsname{\makeSmlKeyword{#1}} |
| 52 |
} |
| 53 |
\newcommand{\smlDefineNewKeyword}[2]{ |
| 54 |
\expandafter\def\csname smlkw@#1\endcsname{#2} |
| 55 |
} |
| 56 |
\newcommand{\smlRemoveKeyword}[1]{ |
| 57 |
\expandafter\newcommand\csname smlkw@#1\endcsname\relax |
| 58 |
} |
| 59 |
|
| 60 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 61 |
% |
| 62 |
|
| 63 |
% |
| 64 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 65 |
\newcommand{\sml@expandtv}[1]{% |
| 66 |
\if\csname smltv@#1\endcsname\relax\makeSmlTypeVar{#1}\else% |
| 67 |
\csname smltv@#1\endcsname\fi} |
| 68 |
|
| 69 |
\newcommand{\sml@expandkw}[1]{% |
| 70 |
\if\csname smlkw@#1\endcsname\relax{#1}\else\csname smlkw@#1\endcsname\fi} |
| 71 |
|
| 72 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 73 |
% This is a lexer that translates type variables and keywords |
| 74 |
% #1 is the eof character |
| 75 |
% #2 is the current state (actually a macro) |
| 76 |
% which can be: |
| 77 |
% 0 -- program text |
| 78 |
% 1 -- type variable |
| 79 |
% 2 -- program text found ( |
| 80 |
% 3 -- in comment |
| 81 |
% 4 -- in comment type variable |
| 82 |
% 5 -- in comment found * |
| 83 |
% #3 is the current string |
| 84 |
% #4 is the next token |
| 85 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 86 |
\newcommand{\sml@startlexer}[1]{\sml@lexer{#1}{0}{}} |
| 87 |
\newcommand{\sml@lexer}[4]{% |
| 88 |
\ifx#4a\let\sml@nexttok=\sml@getletter% |
| 89 |
\else\ifx#4b\let\sml@nexttok=\sml@getletter% |
| 90 |
\else\ifx#4c\let\sml@nexttok=\sml@getletter% |
| 91 |
\else\ifx#4d\let\sml@nexttok=\sml@getletter% |
| 92 |
\else\ifx#4e\let\sml@nexttok=\sml@getletter% |
| 93 |
\else\ifx#4f\let\sml@nexttok=\sml@getletter% |
| 94 |
\else\ifx#4g\let\sml@nexttok=\sml@getletter% |
| 95 |
\else\ifx#4h\let\sml@nexttok=\sml@getletter% |
| 96 |
\else\ifx#4i\let\sml@nexttok=\sml@getletter% |
| 97 |
\else\ifx#4j\let\sml@nexttok=\sml@getletter% |
| 98 |
\else\ifx#4k\let\sml@nexttok=\sml@getletter% |
| 99 |
\else\ifx#4l\let\sml@nexttok=\sml@getletter% |
| 100 |
\else\ifx#4m\let\sml@nexttok=\sml@getletter% |
| 101 |
\else\ifx#4n\let\sml@nexttok=\sml@getletter% |
| 102 |
\else\ifx#4o\let\sml@nexttok=\sml@getletter% |
| 103 |
\else\ifx#4p\let\sml@nexttok=\sml@getletter% |
| 104 |
\else\ifx#4q\let\sml@nexttok=\sml@getletter% |
| 105 |
\else\ifx#4r\let\sml@nexttok=\sml@getletter% |
| 106 |
\else\ifx#4s\let\sml@nexttok=\sml@getletter% |
| 107 |
\else\ifx#4t\let\sml@nexttok=\sml@getletter% |
| 108 |
\else\ifx#4u\let\sml@nexttok=\sml@getletter% |
| 109 |
\else\ifx#4v\let\sml@nexttok=\sml@getletter% |
| 110 |
\else\ifx#4w\let\sml@nexttok=\sml@getletter% |
| 111 |
\else\ifx#4x\let\sml@nexttok=\sml@getletter% |
| 112 |
\else\ifx#4y\let\sml@nexttok=\sml@getletter% |
| 113 |
\else\ifx#4z\let\sml@nexttok=\sml@getletter% |
| 114 |
\else\ifx#4_\let\sml@nexttok=\sml@getletter% |
| 115 |
\else% |
| 116 |
\let\sml@nexttok=\sml@transition% is not a letter |
| 117 |
\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi% |
| 118 |
\sml@nexttok{#1}{#2}{#3}{#4}% |
| 119 |
} |
| 120 |
|
| 121 |
\newcommand{\sml@getletter}[4]{\sml@lexer{#1}{#2}{#3#4}} % get more character |
| 122 |
|
| 123 |
% |
| 124 |
% Do something according to the lookahead character |
| 125 |
% |
| 126 |
\newcommand{\sml@transition}[4]{% |
| 127 |
%\message{|#3|}% |
| 128 |
\if\noexpand#4'% |
| 129 |
\let\sml@nextaction=\sml@tokenizetv% |
| 130 |
\else\if\noexpand#4#1% |
| 131 |
\let\sml@nextaction=\sml@endlexer% |
| 132 |
\else\ifx#4(% |
| 133 |
\let\sml@nextaction=\sml@lparen% |
| 134 |
\else\ifx#4)% |
| 135 |
\let\sml@nextaction=\sml@rparen% |
| 136 |
\else\ifx#4*% |
| 137 |
\let\sml@nextaction=\sml@star% |
| 138 |
\else% |
| 139 |
\let\sml@nextaction=\sml@tokenizekw% |
| 140 |
\fi\fi\fi\fi\fi% |
| 141 |
\sml@nextaction{#1}{#2}{#3}{#4}% |
| 142 |
} |
| 143 |
|
| 144 |
% 0 -- program text |
| 145 |
% 1 -- type variable |
| 146 |
% 2 -- program text found ( |
| 147 |
% 3 -- in comment |
| 148 |
% 4 -- in comment type variable |
| 149 |
% 5 -- in comment found * |
| 150 |
\newcommand{\sml@expand}[2]{% |
| 151 |
\ifcase#1\let\sml@process=\sml@expandkw\or% |
| 152 |
\let\sml@process=\sml@expandtv\or% |
| 153 |
\let\sml@process=\sml@expandkw\or% |
| 154 |
\let\sml@process=\sml@expandkw\or% |
| 155 |
\let\sml@process=\sml@expandtv\or% |
| 156 |
\let\sml@process=\sml@expandkw% |
| 157 |
\else\ERROR% |
| 158 |
\fi% |
| 159 |
\sml@process{#2}% |
| 160 |
} |
| 161 |
\newcommand{\sml@tokenizekw}[4]{\sml@expand{#2}{#3}#4\sml@lexer{#1}{\sml@deltakw{#2}}{}} |
| 162 |
\newcommand{\sml@tokenizetv}[4]{\sml@expand{#2}{#3}\sml@lexer{#1}{\sml@deltatv{#2}}{}} |
| 163 |
\newcommand{\sml@endlexer}[4]{\sml@expand{#2}{#3}} |
| 164 |
\newcommand{\sml@lparen}[4]{\sml@expand{#2}{#3}% |
| 165 |
(\sml@lexer{#1}{\sml@deltal{#2}}{}} |
| 166 |
\newcommand{\sml@rparen}[4]{\sml@expand{#2}{#3}% |
| 167 |
\sml@closecomment{#2})\sml@lexer{#1}{\sml@deltar{#2}}{}} |
| 168 |
\newcommand{\sml@star}[4]{\sml@expand{#2}{#3}*% |
| 169 |
\sml@opencomment{#2}\sml@lexer{#1}{\sml@deltas{#2}}{}} |
| 170 |
|
| 171 |
% 0 -- program text |
| 172 |
% 1 -- type variable |
| 173 |
% 2 -- program text found ( |
| 174 |
% 3 -- in comment |
| 175 |
% 4 -- in comment type variable |
| 176 |
% 5 -- in comment found * |
| 177 |
\newcommand{\sml@opencomment}[1]{\if2#1\BeginSmlComment\else\fi} |
| 178 |
\newcommand{\sml@closecomment}[1]{\if5#1\EndSmlComment\else\fi} |
| 179 |
\newcommand{\sml@deltakw}[1]{\ifcase#1 0\or0\or0\or3\or3\or3\else\ERROR\fi} |
| 180 |
\newcommand{\sml@deltatv}[1]{\ifcase#1 1\or1\or1\or4\or4\or4\else\ERROR\fi} |
| 181 |
\newcommand{\sml@deltal}[1]{\ifcase#1 2\or2\or2\or3\or3\or3\else\ERROR\fi} |
| 182 |
\newcommand{\sml@deltas}[1]{\ifcase#1 0\or0\or3\or5\or5\or5\else\ERROR\fi} |
| 183 |
\newcommand{\sml@deltar}[1]{\ifcase#1 0\or0\or0\or3\or3\or0\else\ERROR\fi} |
| 184 |
|
| 185 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 186 |
% |
| 187 |
% How to process type variables as active character: |
| 188 |
% |
| 189 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 190 |
\newcommand{\sml@processtypevar}{\sml@lexty{}} |
| 191 |
\newcommand{\sml@lexty}[2]{\ifcat\noexpand#2a\let\sml@nextty=\sml@morety\else\let\sml@nextty=\sml@donety\fi\sml@nextty{#1}#2} |
| 192 |
\newcommand{\sml@morety}[2]{\sml@lexty{#1#2}} |
| 193 |
\newcommand{\sml@donety}[1]{\sml@expandtv{#1}} |
| 194 |
\newcommand{\sml@aftertypevar}{} |
| 195 |
|
| 196 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 197 |
% |
| 198 |
% Macros for setting up verbatim mode |
| 199 |
% |
| 200 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 201 |
% |
| 202 |
% Set up catcodes for verbatim mode |
| 203 |
% |
| 204 |
\begingroup |
| 205 |
\lccode`\~=`\' |
| 206 |
\lowercase{\endgroup |
| 207 |
\newcommand{\sml@settick}{\catcode`\'=\active\let~=\sml@processtypevar} |
| 208 |
\newcommand{\sml@verbatimmode}{% |
| 209 |
\let\sml@origprime=~% |
| 210 |
\sml@font% |
| 211 |
\@noligs% |
| 212 |
\catcode`\^=12% |
| 213 |
\catcode`\_=12% |
| 214 |
\ifsml@dollar\else\catcode`\$=12\fi% |
| 215 |
\catcode`\#=12% |
| 216 |
\catcode`\&=12% |
| 217 |
\catcode`\~=12% |
| 218 |
\catcode`\%=12% |
| 219 |
\everymath\expandafter{\the\everymath\sml@mathmode}% |
| 220 |
\everydisplay\expandafter{\the\everydisplay\sml@mathmode}% |
| 221 |
\everypar\expandafter{\the\everypar\unpenalty}% |
| 222 |
\sml@settick% |
| 223 |
\frenchspacing\@vobeyspaces% Preserve all spaces |
| 224 |
} |
| 225 |
|
| 226 |
% |
| 227 |
% Set up catcodes for math mode |
| 228 |
% |
| 229 |
\newcommand{\sml@mathmode}{ |
| 230 |
\catcode`\^=7 % Superscript |
| 231 |
\catcode`\_=8 % Subscript |
| 232 |
\catcode`\'=12 % prime |
| 233 |
\let~=\sml@origprime% |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
% |
| 238 |
% Set up display format |
| 239 |
% |
| 240 |
\newcommand{\sml@begindisplay}{% |
| 241 |
\begingroup |
| 242 |
%\trivlist \item |
| 243 |
\if@minipage% Insert paragraph break |
| 244 |
\else |
| 245 |
\vskip\parskip |
| 246 |
\fi |
| 247 |
\leftskip\@totalleftmargin |
| 248 |
\rightskip\z@skip |
| 249 |
\parindent\z@ |
| 250 |
\parfillskip\@flushglue |
| 251 |
\parskip\z@skip |
| 252 |
\@@par |
| 253 |
\@tempswafalse |
| 254 |
\def\par{% |
| 255 |
\if@tempswa |
| 256 |
\leavevmode\null\@@par\penalty\interlinepenalty |
| 257 |
\else |
| 258 |
\@tempswatrue |
| 259 |
\ifhmode\@@par\penalty\interlinepenalty\fi |
| 260 |
\fi} |
| 261 |
\obeylines % Lines breaks are meaningful |
| 262 |
} |
| 263 |
|
| 264 |
% |
| 265 |
% End display format |
| 266 |
% |
| 267 |
%\newcommand{\sml@enddisplay}{\endtrivlist\endgroup} |
| 268 |
\newcommand{\sml@enddisplay}{\endgroup} |
| 269 |
|
| 270 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 271 |
% |
| 272 |
% The main macros |
| 273 |
% |
| 274 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 275 |
\newenvironment{smldisplay}{\sml@begindisplay\trivlist\item\sml@verbatimmode}{\endtrivlist\sml@enddisplay} |
| 276 |
|
| 277 |
\newcommand{\sml}{\begingroup\sml@verbatimmode\sml@verb} |
| 278 |
\newcommand{\sml@verb}[1]{#1\endgroup} |
| 279 |
|
| 280 |
\newsavebox{\sml@box} |
| 281 |
\newenvironment{sml@boxit}{\begin{lrbox}{\sml@box} |
| 282 |
\begin{minipage}[b]{\columnwidth}} |
| 283 |
{\end{minipage}\end{lrbox}\begin{center}\framebox[\columnwidth]{\usebox{\sml@box}}\end{center}} |
| 284 |
|
| 285 |
\newenvironment{smlboxeddisplay}{\begin{sml@boxit}\begin{smldisplay}}{\end{smldisplay}\end{sml@boxit}} |
| 286 |
|
| 287 |
\newcounter{sml@linecount} |
| 288 |
\newcounter{sml@increment} |
| 289 |
\newcommand{\sml@setuplineno}[2]{% |
| 290 |
\setcounter{sml@linecount}{#1}% |
| 291 |
\setcounter{sml@increment}{1}% |
| 292 |
\newcommand{\sml@showlinecount}% |
| 293 |
{\llap{\smlNumberFont\smlNumberStyle{sml@linecount}\ }}% |
| 294 |
\newcommand{\sml@inclineno}{\addtocounter{sml@increment}{-1}% |
| 295 |
\ifnum\value{sml@increment}=0\sml@showlinecount\setcounter{sml@increment}{#2}\else \fi% |
| 296 |
\addtocounter{sml@linecount}{1}}% |
| 297 |
\everypar{\sml@inclineno}% |
| 298 |
} |
| 299 |
|
| 300 |
\newenvironment{smllisting}[2]% |
| 301 |
{\sml@begindisplay\sml@verbatimmode\sml@setuplineno{#1}{#2}}% |
| 302 |
{\sml@enddisplay} |
| 303 |
\newenvironment{smlboxedlisting}[2]% |
| 304 |
{\begin{sml@boxit}\begin{smllisting}{#1}{#2}}{\end{smllisting}\end{sml@boxit}} |
| 305 |
\newenvironment{smldisplay*}[1]{% |
| 306 |
\sml@begindisplay |
| 307 |
\let\sml@settick=\relax |
| 308 |
\sml@verbatimmode |
| 309 |
\sml@startlexer{#1} |
| 310 |
}{\sml@enddisplay} |
| 311 |
\newenvironment{smlboxeddisplay*}[1]% |
| 312 |
{\begin{sml@boxit}\begin{smldisplay*}{#1}}{\end{smldisplay*}\end{sml@boxit}} |
| 313 |
|
| 314 |
|
| 315 |
\begingroup |
| 316 |
\catcode`\^^A=12% |
| 317 |
\catcode`\<=1% |
| 318 |
\catcode`\>=2% |
| 319 |
\catcode`\?=0% |
| 320 |
\catcode`\{=12% |
| 321 |
\catcode`\}=12% |
| 322 |
%\catcode`\\=12% |
| 323 |
\lowercase<\endgroup% |
| 324 |
\def\sml@glob#1\end<\sml@startlexer<^^A>#1^^A\sml@enddisplay\end>% |
| 325 |
\def\smldisp<% |
| 326 |
\sml@begindisplay% |
| 327 |
\let\sml@settick=\relax% |
| 328 |
\sml@verbatimmode% |
| 329 |
\catcode`\^^A=12% |
| 330 |
\sml@glob% |
| 331 |
> |
| 332 |
\def\endsmldisp<> |
| 333 |
> |
| 334 |
|
| 335 |
\begingroup |
| 336 |
\catcode`\^^A=12 |
| 337 |
\lowercase{\endgroup |
| 338 |
\newcommand{\Sml}{% |
| 339 |
\begingroup% |
| 340 |
\let\sml@settick=\relax% |
| 341 |
\sml@verbatimmode% |
| 342 |
\catcode`\^^A=12% |
| 343 |
\long\def\sml@grab##1{\sml@startlexer{^^A}##1^^A\endgroup}% |
| 344 |
\sml@grab% |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 349 |
% |
| 350 |
% Set up the default keywords |
| 351 |
% |
| 352 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 353 |
\newcommand{\smlSetupAllKeywords} |
| 354 |
{ \smlNewKeyword{functor} |
| 355 |
\smlNewKeyword{structure} |
| 356 |
\smlNewKeyword{signature} |
| 357 |
\smlNewKeyword{struct} |
| 358 |
\smlNewKeyword{sharing} |
| 359 |
\smlNewKeyword{sig} |
| 360 |
\smlNewKeyword{include} |
| 361 |
\smlNewKeyword{fun} |
| 362 |
\smlNewKeyword{val} |
| 363 |
\smlNewKeyword{rec} |
| 364 |
\smlNewKeyword{type} |
| 365 |
\smlNewKeyword{datatype} |
| 366 |
\smlNewKeyword{case} |
| 367 |
\smlNewKeyword{of} |
| 368 |
\smlNewKeyword{and} |
| 369 |
\smlNewKeyword{where} |
| 370 |
\smlNewKeyword{withtype} |
| 371 |
\smlNewKeyword{end} |
| 372 |
\smlNewKeyword{let} |
| 373 |
\smlNewKeyword{in} |
| 374 |
\smlNewKeyword{end} |
| 375 |
\smlNewKeyword{handle} |
| 376 |
\smlNewKeyword{raise} |
| 377 |
\smlNewKeyword{exception} |
| 378 |
\smlNewKeyword{local} |
| 379 |
\smlNewKeyword{open} |
| 380 |
\smlNewKeyword{if} |
| 381 |
\smlNewKeyword{then} |
| 382 |
\smlNewKeyword{else} |
| 383 |
\smlNewKeyword{infix} |
| 384 |
\smlNewKeyword{infixr} |
| 385 |
\smlNewKeyword{nonfix} |
| 386 |
} |
| 387 |
|
| 388 |
\smlSetupAllKeywords |
| 389 |
|
| 390 |
\endinput |