summaryrefslogtreecommitdiff
path: root/support/splint/tex/yyinput.sty
blob: 4ca9a8617cbd1097f70113afbf70980234c4b80e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
% Copyright 2012-2024, Alexander Shibakov
% This file is part of SPLinT
%
% SPLinT is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% SPLinT is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with SPLinT.  If not, see <http://www.gnu.org/licenses/>.

%\def\mkpurebyte{\uccode`\@=\yycp@\uppercase{\yybytepure{@}}\uccode`\@=`\@}

% make all symbol characters category 12; most macros are indifferent to this
% however, if delimited macros are used to process the mathched text, the option 
% makes it easier to write such macros;

\def\mkpurebyte{\uccode`\.=\yycp@\uppercase{\yybytepure{.}}\uccode`\.=`\.}

\def\yyinput{\futurelet\next\yyinp@t} % get the code of the next character ...

\def\yyinp@t{% test the code and decide whether to continue lexing the
             % token or return to the parser
  \ifcat\noexpand\next\bgroup 
      \yybreak\yyinputgroup
  \else
      \if\noexpand\next\space % code 32 character token is assumed to
% be an ordinary space (while category 10 tokens are not necessarily);
% the reason for this choice is the way code 32 characters are treated
% by \string (turned into `real' spaces); `funny' spaces of both kinds
% (a character that differs from a `real' space (\catcode`\ ==10
% \number`\ ==32) in either category or character code) can be
% created, however, category 10 characters of character code other
% than 32 are very rare (and take some effort to produce), while
% category 12, character code 32 tokens are poduced as a result of
% `sanitizing' input all the time; note that the spaces are processed
% at the lowest level of the input routine and thus cannot be
% substituted by the switches below; this choice is not bullet proof
% but should suffice for most uses; in the case of more polluted
% input, an extra `sanitation' step can be performed first; if
% desired, category 10 charactes can be assumed to be spaces at this
% point, as well, although this will introduce an extra test, affecting
% the (already poor) efficiency of the input macros.
          \yycp@=`\ % 
          \yybyte={ }%
          \mkpurebyte
          \yybreak@\yyskipspace % return the space
      \else
          \if\noexpand\next\eolletter % cannot use ^^M here since TeX will simply drop the rest of the line
              \yycp@=\n
              \yybyte={\n}%
              \mkpurebyte
              \yybreak@@\yyskipspace % return the end of line character
          \else
              \yybreak@@\yy@np@t
          \fi
      \fi
  \yycontinue
}

% some \Cee\ escape characters; the rest are either silly (like \a and \b) or
% are already defined to have other important functions in \TeX\ (such as \v and \t)

\chardef\n=`\^^J
\chardef\r=`\^^M
\chardef\f=`\^^L
\chardef\HT=`\^^I % ASCII horizontal tab
\chardef\charseq1
\chardef\charac2
\chardef\chargroup3

\def\yyskipspace{\afterassignment\yyreturn\let\next= }

\def\yyinputgroup#1{%
  \yycp@=\chargroup 
  \yybyte={{#1}}%
  \mkpurebyte
  \yyreturn
}

\newif\ifyyinputdebug

\def\yy@np@t#1{% start lexing:
               % whitespace (category 10) tokens are skipped
               % automatically
  \yybyte{#1}%
  \ifyyinputdebug
      \immediate\write16{read: \the\yybyte\space after: \the\yytext@seen}%
  \fi
  \ifx#1\ % a space token
      \yybreak\returnexplicitspace
  \else
      \expandafter\ischar\string#100\end 
      \ifchar % a single character (not a control sequence)
          \ifcat\noexpand#1\noexpand~% is it an active character?
              \yycp@=\charac % yes, return it
              \mkpurebyte
              \yybreak@@\yyreturn
          \else % it is a non-active character ...
                % ... or a control sequence with an empty name (obtained by \csname\endcsname or
                % \enlinechar=-1 \toks0={\
                % } this case can be handled by the switch below but is ignored for the moment)
              \let\default\achardefault
              \yybreak@@{\switchon{\the\yybyte}\in\acharswitch}%
          \fi
      \else % it is a control sequence, return it
          \expandafter\ischar\string#10\end 
          \ifchar % it is a one-char control sequence
              \let\default\onechardefault
              \yybreak@@{\switchon{\the\yybyte}\in\onecharswitch}%
          \else
              \let\default\multichardefault
              \yybreak@@{\switchon{\the\yybyte}\in\multicharswitch}%
          \fi
      \fi
  \yycontinue
}

\chardef\explicitspacecode=`\ %

\def\returnexplicitspace{%
      \yycp@=\explicitspacecode 
      \mkpurebyte
      \yyreturn % keep looking for a non-whitespace token
}

\def\onecharswitch{
    \raw \n \raw {%
        \yycp@=\n
        \mkpurebyte
        \yyreturn
    }
    \raw \^^M\raw {%
        \returnexplicitspace
    }
}

\def\onechardefault{%
    \expandafter\yycp@\expandafter`\the\yybyte\relax
    \mkpurebyte
    \yyreturn
}%

\def\multicharswitch{
    \raw\vb\raw {%
        \vbunwrap
    }
    \raw\insertraw\raw {%
        \insertrawnext
    }
    \raw\stashed\raw {%
        \stashnext
    }
    \raw\format \formatlocal\raw {%
        \formatnext
    }
    \raw\formatbegin\raw {%
        \fmtbegin
    }
    \raw\formatp\raw {%
        \fmtparam
    }
    \raw\sflush\raw {%
        \sflushnext
    }
    \raw\yyeof\raw {%
        \yycp@=\YYENDOFBUFFERCHAR\relax
        \yybytepure={}%
        \yyreturn
    }
    \raw\inputboundary\raw {%
        \inputboundarynext
    }
    \raw\flatten \resetf \inline \skipheader\raw {%
        \expandafter\yyinput\expandafter\format\expandafter{\the\yybyte}%
    }
    \raw\fold \breakline\raw {% 
        \expandafter\yyinput\expandafter\formatlocal\expandafter{\the\yybyte}%
    }
    \raw\breakahead\raw {% 
        \expandafter\yyinput\expandafter\formatp\the\yybyte
    }
    \raw\break\raw {% for testing purposes
        \yycp@=`\ %
        \yybytepure={ }% 
        \yyreturn
    }
    \raw\squashtermstrue\raw {%
        \yycp@=`\ %
        \yybytepure={ }% 
        \squashtermstrue
        \yyreturn
    }
    \raw\endparse \endparseinput\raw {%
        \errmessage{internal error: reading past the end of the input buffer}%
    }
}

\def\multichardefault{%
    \yycp@\charseq
    \mkpurebyte
    \yyreturn
}%

% the single character switch `normalizes' the typesetting of underscores, as this
% is a particularly tricky character to get right; in typewriter fonts there is a
% `first class' underscore character so \_ may be defined as \char`\_ whereas in other fonts
% there is no `native' underscore character so \_ may be defined as a rule of an appropriate
% size
\def\acharswitch{
    \raw_\raw\_ {% the name parser will replace _ with \_ in subscripts
        \yycp@`\_\relax
        \yybyte{\_}%
        \mkpurebyte
        \yyreturn
    }
}

\def\achardefault{%
    \expandafter\yycp@\expandafter`\the\yybyte\relax
    \mkpurebyte
    \yyreturn
}

% the following commands are only here for debugging purposes
% they slow down the input

\setspecialcharsfrom\onecharswitch
\setspecialcharsfrom\multicharswitch
\setspecialcharsfrom\acharswitch

% care should be taken with using the next command; it will only be executed
% if the parser (rather the lexer) `sees' it as part of the input; thus if this
% command is inserted at the end of the parsed text and, say, a bootstrap parser
% terminates early, the command will not be executed; likewise, it may be executed 
% (more than) twice in case, for example, a parser stack is used and both (several)
% parsers see it before the parsing is terminated. 

\def\insertrawnext#1{% insert a command
    #1\yyinput
}

\def\vbunwrap#1#2\vb{%
    \yyinput#1\stashed{#2}\vb
}

% stash and format streams (lists)

\chardef\stashchar=`\ %
\chardef\formatchar=`\ %

\newcount\stashmarker
\newcount\formatmarker

\def\yystash{[stash]}
\def\yyformat{[format]}

\def\stashnextwithspace#1{%
    \yybytepure{ }\yycp@\stashchar
    \yybyte\expandafter{\the\yybyte{#1}}%
    \advance\stashmarker\@ne
    \ifnum\stashmarker<\showlistcounter\yystash\relax
    \else
        \appendtolist\yystash{#1}%
    \fi
    \yyreturn
}

% the mechanism for stash processing making stash invisible

\def\stashnextwithnothing#1{%
    \yybyte\expandafter{\the\yybyte{#1}}\concat\yysubtext\yybyte
    \advance\stashmarker\@ne
    \ifnum\stashmarker<\showlistcounter\yystash\relax
    \else
        \appendtolist\yystash{#1}%
    \fi
    \ifyyinputdebug
        \immediate\write16{mid text: \the\yysubtext}%
    \fi
    \yyinput
}

% collect the stash in the stash list (stream)

\let\stashnext\stashnextwithnothing

\def\formatnext#1{%
    \yybytepure{ }\yycp@\formatchar
    \toksa{#1}%
    \advance\formatmarker\@ne
    \ifnum\formatmarker<\showlistcounter\yyformat\relax
    \else
        \expandafter\appendtolisti\expandafter\yyformat\expandafter{\the\yybyte{#1}}% insert the iterator sequence, as well
    \fi
    \yybyte\expandafter{\the\yybyte{#1}}%
    \yyreturn
}

\def\fmtbegin#1\fmtend{\formatnext{#1}} % multiparameter format sequences
\def\fmtparam#1#2{\formatnext{#1{#2}}} % single parameter format sequences

\chardef\boundarychar=`\ %

% the following is a minimal setup of a parsing boundary

\def\inputboundarynext#1{% `l' for left boundary
    \yybytepure{ }\yycp@\boundarychar
    \yybyte\expandafter{\the\yybyte{#1}}%
    \yyreturn % inserting something here will effectively insert it into the input stream
}

% the next macro is not implemented correctly (since after a backup the flushed
% tokens may be reinserted; there needs to be a mechanism to remember that the
% tokens are already in the stash stream

\def\sflushnext#1#2{% #1 is the marker
                    % #2 is the contents
    \yybytepure{ }\yycp@\stashchar
    % `save the flushed code' goes here
    \yybyte\expandafter{\the\yybyte{#1}{#2}}%
    \concat\yysubtext\yybyte
    \ifyyinputdebug
        \immediate\write16{mid text: \the\yysubtext}%
    \fi
    \yyreturn
}

\def\z@rotest{0}
\newif\ifchar

\def\ischar#1#2#3\end{% three parameters because #1 can be an
                      % \escapechar
    \def\lastnamechar{#3}%
    \ifx\lastnamechar\z@rotest\chartrue\else\charfalse\fi
}

% trivial input routine

\def\yyinputtrivial{\futurelet\next\yyinp@ttrivial} % get the code of the next character ...

\def\yyinp@ttrivial{
    \ifcat\noexpand\next\space % category 10 token
        \yycp@=`\ % 
        \yybyte={ }%
        \mkpurebyte
        \yybreak\yyskipspace % return the space
    \else
        \yybreak\yy@np@ttrivial
    \yycontinue
}

\def\yy@np@ttrivial#1{%
    \ifcat\noexpand#1a%
        \yycp@`#1%
        \yybyte{#1}%
        \yybytepure{#1}%
    \else
        \if\noexpand#1\eolletter
            \yycp@=\n
            \yybyte\expandafter{\eolletter}%
            \mkpurebyte
        \else
            \ifx#1\yyeof
                \yycp@=\YYENDOFBUFFERCHAR\relax
                \yybyte{#1}%
                \yybytepure={}%
            \else
                \yycp@\charseq
                \mkpurebyte
            \fi
        \fi
    \fi
    \yyreturn
}