Debian Codegraph @

texmf-dist/doc/latex/prettytok/prettytok.tex

raw
1 %! TEX program = lualatex
2 \ProvidesFile{prettytok.tex} [2023/04/18 v0.2.0 ]
3 \RequirePackage{fvextra}
4 \documentclass{l3doc}
5 \usepackage[svgnames]{xcolor}
6 \EnableCrossrefs
7 \CodelineIndex
8 \fvset{breaklines=true,gobble=0,tabsize=4,frame=single,numbers=left,numbersep=3pt}
9 \AtBeginDocument{\DeleteShortVerb\"}
10 \MakeOuterQuote{"}
11 \usepackage{cleveref}
12 \begin{document}
13 \hfuzz=1pt
14 \GetFileInfo{\jobname.tex}
15
16 \title{\pkg{prettytok} --- Pretty-print token lists\thanks{This file
17 describes version \fileversion, last revised \filedate.}
18 }
19 \author{user202729%
20 %\thanks{E-mail: (not set)}
21 }
22 \date{Released \filedate}
23
24 \maketitle
25
26 \begin{abstract}
27 Pretty-print token lists for debugging purposes.
28
29 Can be used to replace |\tl_analysis_show:n|.
30 \end{abstract}
31
32 \section{Motivation}
33
34 \TeX/\LaTeX's default mechanism for debug-printing -- that is, the following:
35 \begin{itemize}
36 \item |\showtokens{...}|,
37 \item |\show...|,
38 \item |\typeout{\unexpanded{...}}|,
39 \item |\tl_show:n|,
40 \item etc.
41 \end{itemize}
42 has a few limitations:
43
44 \begin{itemize}
45 \item |\show| and similar is considered an error and stops the \TeX\ run. (this point is partially fixable, see the package writer's question \url{https://tex.stackexchange.com/q/621919/250119})
46 \item If there's some unprintable character in the output (for example, |^^J|, |^^M|, |^^I| -- literal tab character), it's not easy to distinguish between them.
47 \item If some token has unexpected catcode (most commonly, letter versus other), it's not easy to distinguish as well.
48 \item They does not work in expansion-only context.
49
50 (apart from |\msg_expandable_error:nn|, but this one suffers from the first problem as well)
51 \end{itemize}
52
53 |\tl_analysis_show:n| attempts to fix the third problem, but is very, very verbose and does not fix the other problems.
54
55 This package aims to fix all of them.
56
57 (although the expandable debug printing functions are \LuaTeX-only.)
58
59 And some more additional (expandable in \LuaTeX) functions to inspect the content of the input stream at a particular moment in time.
60
61
62 \section{Usage}
63
64 \subsection{A complete example}
65
66 For a full example, the following code, which prints several things, both using the \LaTeXe\ interface
67 and using the \pkg{expl3}-style interface, using both expandable and unexpandable command (the former requires \LuaTeX, as mentioned in \ref{subsec:expandable-interface}):
68 \VerbatimInput{prettytok-test.tex}
69 The corresponding output (in the current version) is:
70 \begingroup%
71 \ExplSyntaxOn
72
73 \definecolor{myyellow}{RGB}{173, 174, 0}
74 \definecolor{mygreen}{RGB}{52, 174, 0}
75
76 \let \\ \textbackslash
77 \def \cmd#1{\textcolor{myyellow}{\textbf{\textbackslash}#1}}
78 \def \spec#1 {\textcolor{red}{#1}}
79 \def \letr#1 {\textcolor{mygreen}{#1}}
80 \setlength{\parindent}{0pt}
81 \frenchspacing % no double space after dot
82
83 \catcode`␣\active \def␣{\textbf{\textcolor{gray!60}{\char32}}}
84
85 \ExplSyntaxOff
86 \small%
87 \obeylines%
88 \texttt{
89 > 123\spec\{4␣5\spec\}\cmd{6}
90 > \spec{\#\&\textasciicircum\_}:
91 > \cmd{error}
92 > \letr{very␣long␣long␣long␣long␣long␣long␣long␣long␣long␣long␣long␣long␣long}
93 > .. \letr{␣long␣argument}␣\cmd{argument} \cmd{argument} \cmd{test} 123456
94 > \letr{very␣long␣long␣long␣long␣long␣long␣long␣long␣long␣long␣long␣long␣long}
95 > .. \letr{␣long␣argument}␣\cmd{argument} \cmd{argument} \cmd{test} 123456
96 > \spec{\#\&\textasciicircum}\letr{\_:}
97 > \letr{abc}␣
98 }\endgroup
99
100 \subsection{Loading the package}
101
102 \begin{verbatim}
103 \usepackage{prettytok}
104 \end{verbatim}
105
106 \subsection{Options}
107
108 There are several options that can be passed to the package.
109
110 Usage example: |\usepackage[mode=term-shell]{prettytok}|.
111
112 \begin{variable}{mode=}
113 Specify the working mode of the package, that is, where the output is displayed. It can either be:
114 \begin{itemize}
115 \item |term-8bit|: this is the default.
116
117 Assume XTerm-compatible system, output to the terminal.
118
119 Requires |-8bit| option on engines other than \LuaTeX, see the following link: \\
120 \url{https://tex.stackexchange.com/q/168460/250119}.
121
122 Besides, this drops the distinction between the catcode of some tokens (for example \verb|{}$#&^_| are all shown as the same color as "special catcode"),
123 which is available in the tooltip in the HTML version.
124
125 This might work on Windows, although the package writer have not tested it.
126 Refer to \url{https://stackoverflow.com/q/2048509/5267751}
127 and \url{https://superuser.com/q/413073/577463}.
128
129 \item |term-shell|:
130 To output colored text to the terminal, |--8bit| flag is needed otherwise the terminal escape codes will be
131 changed to |^^[| etc.
132 A workaround, using \TeX\ Live's behavior when the file name has the form \verb+|...+,
133 is provided with this option.
134
135 Requires |--shell-escape| flag. May not be very reliable.
136
137 Refer to \url{https://tex.stackexchange.com/a/670572/250119} for more details.
138
139 \item |html|: output to a HTML file named for example |pretty-abc.html| (although this can be customized,
140 refer to \hyperref[opt:html-file-name]{|html-file-name=|}%
141 ) if the main \TeX\ file is named |abc.tex|.
142
143 Open the file in any browser to view the result.
144
145 Using this option, the output will not be cluttered with the traceback/other \TeX\ default output.
146
147 By default, the output refreshes whenever the \TeX\ file is recompiled. The behavior
148 can be customized with \hyperref[opt:html-refresh-strategy]{|html-refresh-strategy=|} and \hyperref[opt:html-refresh-duration]{|html-refresh-duration=|}.
149
150 \end{itemize}
151 \end{variable}
152
153 Currently, it's not supported to print the debug output to the PDF itself,
154 because if the \TeX\ program stops with error / has some error that corrupts the PDF output, the output will even with corrupted more by the debug print.
155
156 \subsubsection{HTML configuration}
157
158 These options are only meaningful if |mode=html|.
159
160 \begin{variable}{html-file-name=}\label{opt:html-file-name}
161 The output file name. Defaults to |pretty-|\meta{jobname}|.html|, as mentioned above.
162 \end{variable}
163
164 \begin{variable}{html-refresh-strategy=}\label{opt:html-refresh-strategy}
165 The auto-refresh strategy. Allowed values are 0-4. 0 is no refresh. \\
166 Which value works best depends on the particular browser. \\
167 If you're using Google Chrome to view the output HTML,
168 invoking the browser from the command-line with |--allow-file-access-from-files| flag may be useful.
169 \end{variable}
170
171 \begin{variable}{html-refresh-duration=}\label{opt:html-refresh-duration}
172 The duration between two consecutive refresh check, in milliseconds. Defaults to 1000.
173 \end{variable}
174
175 \subsubsection{Terminal configuration}
176
177 These options are only meaningful if |mode=term-8bit| or |mode=term-shell|.
178
179 \begin{variable}{term-prefix=,term-prefix-more=}
180 Strings consist of prefixes to be output before each terminal line.
181
182 This might be useful for log-filtering/output-filtering tools such as |texfot| to recognize the output line.
183
184 Defaults to \verb*|> | and \verb*|> .. | respectively.
185 \end{variable}
186
187 \begin{variable}{term-wrap-limit=}
188 Estimated line length limit. Set this to a little smaller than your terminal width.
189
190 Defaults to 70.
191 \end{variable}
192
193 \begin{variable}{term-shell-decode-cmd=}
194 Only meaningful with |mode=term-shell|.
195
196 Normally you would not need to explicitly pass this option, unless something does not work.
197
198 By default, a file named \file{prettytok-decode-8bit.py}
199 should be included in your \TeX\ distribution, and
200 the package runs |kpsewhich prettytok-decode-8bit.py| to find the location
201 of that file in order to execute it.
202 However, if by any reason this does not work, you can specify an explicit command such as
203 |python3 /full/path/to/prettytok-decode-8bit.py| to override it.
204
205 Passing blank value invokes the default behavior (runs |kpsewhich|).
206
207 Alternatively, you can also choose to explicitly pass the path in order to save a call to |kpsewhich|
208 to make the program a bit faster.
209
210 If you \emph{really} want to, special characters may be passed by prefixing them with |\|.
211 \DisableQuotes
212 But |\"| won't work anyway (as far as the package writer know, this is impossible in non-\LuaTeX\ engines).
213 \EnableQuotes
214 \begin{texnote} The path is interpreted by detokenizing the value in |escapechar=-1|. \end{texnote}
215 \end{variable}
216
217 \begin{variable}{term-shell-decode-cmd-print=}
218 If |mode=term-shell|, print out the command correspond to |term-shell-decode-cmd| on the console, for debugging purpose.
219
220 Example output:
221 |The value of term-shell-decode-cmd is: [[[./prettytok-decode-8bit.py]]]|
222 \end{variable}
223
224
225 \subsection{Main function}
226 \begin{function}{\pretty:n,\pretty:x,\pretty:o,\pretty:V}
227 \begin{syntax}
228 \cs{pretty:n} \Arg{token list}
229 \end{syntax}
230 Print the content of \meta{token list}.
231
232 This is a simple replacement of the functions above. (|\tl_analysis_show:n|, etc.)
233 \end{function}
234
235 \begin{function}{\pretty:w}
236 \begin{syntax}
237 \cs{pretty:w} \meta{token list} \cs{prettystop}
238 \end{syntax}
239 Print the content of \meta{token list}.
240
241 The purpose of this function is that
242 it can be inserted "anywhere" in order to \emph{inspect the input stream}
243 without affecting how the function works.
244
245 Note that the input stream will be tokenized and has catcode frozen.
246
247 For example
248 \begin{verbatim}
249 \ExplSyntaxOn
250 \def \f #1 {\prettye:w 789}
251 \f 123456 \prettystop
252 \end{verbatim}
253 will print out |78923456|.
254
255 For now, it must be brace-balanced. Use \cs{prettye:w} instead if this is a problem.
256 \end{function}
257
258 \begin{function}[EXP]{\prettystop}
259 \begin{syntax}
260 \cs{prettystop}
261 \end{syntax}
262 Only used as a delimiter for |:w| functions. For convenience, this function is defined to do nothing.
263 \end{function}
264
265 \begin{function}{\prettyshow:N,\prettyshow:c}
266 \begin{syntax}
267 \cs{prettyshow:N} \meta{token}
268 \cs{prettyshow:c} \Arg{control sequence name}
269 \end{syntax}
270 Show the meaning of a |N|-type argument.
271 \end{function}
272
273 \begin{function}{\pretty:N,\pretty:c}
274 \begin{syntax}
275 \cs{pretty:N} \meta{token}
276 \cs{pretty:c} \Arg{control sequence name}
277 \end{syntax}
278 Print \meta{token}.
279
280 It may also be useful to use |\pretty:V| to print a token list variable's value,
281 or |\prettyshow:N| to print a control sequence's meaning.
282 \end{function}
283
284 \begin{function}{\pretty:nn, \pretty:nnn}
285 \begin{syntax}
286 \cs{pretty:nn} \Arg{token list} \Arg{token list}
287 \cs{pretty:nnn} \Arg{token list} \Arg{token list} \Arg{token list}
288 \end{syntax}
289 Print multiple token lists. Its effect is similar to multiple consecutive calls to \cs{pretty:n}.
290 \end{function}
291
292
293
294
295
296
297 \subsection{Expandable interface (\LuaTeX\ only)}
298 \label{subsec:expandable-interface}
299
300 \begin{function}[EXP]{\prettye:n}
301 \begin{syntax}
302 \cs{prettye:n} \Arg{token list}
303 \end{syntax}
304 Print the token list, similar to \cs{pretty:n}, but is fully expandable.
305 \end{function}
306
307 \begin{function}[EXP]{\prettye:w}
308 \begin{syntax}
309 \cs{prettye:w} \meta{tokens} \cs{prettystop}
310 \end{syntax}
311 Print the tokens until \cs{prettystop} is seen. Useful for inspecting the content of the input stream.
312
313 As a debugging tool, it's possible to execute |\everyeof{\prettystop}| to avoid runaway printing in weird catcode environments.
314
315 Currently some implementation details (it can be fixed, but the package writer does not
316 have an immediate need for it, see \url{https://tex.stackexchange.com/q/335994/250119}) means control sequences not
317 in the hash table will be destroyed.
318 Use with care.
319
320 \end{function}
321
322 \begin{function}[EXP]{\prettye:nn, \prettye:nnn}
323 \begin{syntax}
324 \cs{prettye:nn} \Arg{token list} \Arg{token list}
325 \cs{prettye:nnn} \Arg{token list} \Arg{token list} \Arg{token list}
326 \end{syntax}
327 Similar to multiple consecutive calls to \cs{prettye:n}.
328 \end{function}
329
330
331
332 \begin{function}[EXP]{\prettye:nw}
333 \begin{syntax}
334 \cs{prettye:nw} \Arg{callback} \meta{tokens} \cs{prettystop}
335 \end{syntax}
336 Same as above, but has a callback, that is, code that is put in the input stream after the content is printed.
337
338 Useful if you want to fine-tune what is printed exactly.
339 (\texttt{\cs{prettye:w} \meta{callback} \meta{tokens}} is functionally the same, but the callback
340 is also printed, which will clutter the debug output)
341
342 For example
343 \begin{verbatim}
344 \prettye:nw {\somecode ...} 123 \prettystop
345 \end{verbatim}
346 will print |123|, then after some expansion steps results in the input stream contain
347 |\somecode ... 123 \prettystop|.
348 \end{function}
349
350 \begin{function}[EXP]{\prettye:nnw, \prettye:nnnw}
351 \begin{syntax}
352 \cs{prettye:nnw} \Arg{callback} \Arg{token list} \meta{tokens} \cs{prettystop}
353 \cs{prettye:nnnw} \Arg{callback} \Arg{token list} \Arg{token list} \meta{tokens} \cs{prettystop}
354 \end{syntax}
355 Similar to call(s) to \cs{prettye:n} followed by a call to \cs{prettye:nw}.
356
357 For example
358 \begin{verbatim}
359 \prettye:nnw {\somecode ...} {123} 456 \prettystop
360 \end{verbatim}
361 will print |123456|, then, after some expansion steps, |\somecode ... 456 \prettystop| remains in the input stream.
362 \end{function}
363
364 \subsection{Lua programming interface}
365
366 \begin{function}{prettyprint}
367 \begin{syntax}
368 |prettyprint(|\meta{content}|)|
369 \end{syntax}
370 Print the content, which should be a table of token objects.
371
372 For convenience, you can pass multiple arguments. Strings are also supported.
373 \end{function}
374
375
376
377
378 \subsection{\LaTeXe\ interface}
379
380 \begin{function}{\prettyN,\prettyX,\prettyO,\prettyV,\prettyW,\prettyshowN,\prettyshowC}
381 \begin{syntax}
382 \tn{prettyN} \Arg{token list}
383 \tn{prettyX} \Arg{token list}
384 \tn{prettyO} \Arg{token list}
385 \tn{prettyV} \meta{tl var}
386 \cs{prettyW} \meta{token list} \cs{prettystop}
387 \tn{prettyshowN} \meta{token}
388 \tn{prettyshowC} \Arg{control sequence name}
389 \end{syntax}
390 Alias of the correspondingly-named commands.
391 \end{function}
392
393 \begin{function}[EXP]{\prettyeN,\prettyeW}
394 \begin{syntax}
395 \cs{prettyeN} \Arg{token list}
396 \cs{prettyeW} \meta{tokens} \cs{prettystop}
397 \end{syntax}
398 Alias of the correspondingly-named commands. Only available in \LuaTeX.
399 \end{function}
400
401
402 \section{Implementation}
403
404 Unfortunately, the implementation is not typesetted in \TeX. Read the |.sty| file.
405
406 Remark: it's possible to do expandable printing in other engines as well by, for example, turning on |\tracingmacros|, parse the token list somehow
407 (and use some not-always-exact logic to distinguish normal character and active character with same meaning;
408 then grep the resulting log file for special markers.
409
410 But that would be very, very slow and slows down everything else by turning on logging. Just use \LuaTeX\ for debugging.
411
412 There's another option of recompiling the engine and adding some expandable primitive for debug logging...
413
414 \PrintChanges
415 \PrintIndex
416 \Finale
417 \end{document}