Debian Codegraph @

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

raw
1 %%! TEX program = lualatex
2 \ProvidesFile{precattl.tex} [2022/07/07 v0.0.0 ]
3 \documentclass{l3doc}
4 \EnableCrossrefs
5 \CodelineIndex
6 \RecordChanges
7 \fvset{gobble=0,tabsize=4,frame=single,numbers=left,numbersep=3pt}
8 \usepackage{hyperref}
9 \usepackage{csquotes}
10
11 %\MakeOuterQuote{"}
12 \begin{document}
13 \GetFileInfo{\jobname.tex}
14
15 \title{\textsf{precattl} --- Prepare special catcodes from token list
16 \thanks{This file describes version \fileversion, last revised \filedate.}
17 }
18 \author{user202729%
19 %\thanks{E-mail: (not set)}
20 }
21 \date{Released \filedate}
22
23 \maketitle
24
25 \begin{abstract}
26 Allow users to write code that contains tokens with unusual catcodes.
27 \end{abstract}
28
29 \section{Motivation}
30
31 This package allows developers to quickly prototype writing code by specifying the exact token list to be executed.
32
33 For example, classically if you want to define a space gobbler you would do this
34 \begin{verbatim}
35 \lowercase{\def\spacegobbler} {}
36 \end{verbatim}
37 which is equivalent to the token list |\def\spacegobbler|\meta{explicit space token}\verb+{}+.
38
39 In such simple cases there's negligible benefit in using this package; nevertheless in some more complex cases such as the following...
40 \begin{verbatim}
41 \begingroup
42 \lccode `?=`_
43 \lowercase{
44 \endgroup
45 \peek_analysis_map_inline:n {
46 \expandafter \string #1
47 \peek_analysis_map_break:
48 }
49 \c_begin_group_token ?
50 }
51
52 \lowercase{\lowercase{\def\twospacegobbler} } {}
53
54 \catcode`\^^M\active
55 \edef^^M{\string^^M}
56 \end{verbatim}
57 it would be beneficial to have simpler construct to express the code.
58
59 A side benefit is that there's no need to do explicit |\begingroup| ... |\endgroup| (and keep track of when |\endgroup| should be executed),
60 and no risk of accidentally pretokenizing the token list/it can be used inside argument of anything.
61
62 \section{Limitation}
63
64 \begin{itemize}
65 \item There's some performance hit. I think it's about 5 times slower than |\tl_analysis_map_inline:nn| on the same token list.
66
67 Nevertheless this is not a big problem, as it's possible to precompile the token list and include it in the generated \texttt{.sty} file.
68 \item It's not allowed to pass |\outer| tokens into the macro directly; however, generating |\outer| tokens with |\cC| and |\cA| is supported.
69 \end{itemize}
70
71
72 \section{Syntax}
73
74
75 \begin{function}{\precattl_exec:n}
76 \begin{syntax}
77 \cs{precattl_exec:n} \Arg{token list}
78 \end{syntax}
79 Executes the \Arg{token list} after preprocessing it in the following manner:
80
81 \begin{itemize}
82 \item |\cC|\Arg{control sequence name} is replaced by that control sequence.
83 \item |\cFrozenRelax| is replaced with the frozen relax control sequence.
84 \item |\cA|\Arg{tokens}, |\cO|\Arg{tokens}, etc. are replaced with sequence of tokens with catcode active/other respectively.
85 \end{itemize}
86 \end{function}
87 In particular, \Arg{tokens} or \Arg{control sequence name} might consist of normal \enquote{characters} or control sequences.
88 Control sequences will have its csname appended. (nevertheless don't use the null control sequence here)
89
90 The full list of supported catcodes is the same as \pkg{l3regex} package:
91 |\cB|, |\cE|, |\cM|, |\cT|, |\cP|, |\cU|, |\cD|, |\cS|, |\cL|, |\cO|, |\cA|.
92
93 As an example, the examples above can be executed as following:
94 \begin{verbatim}
95 \precattl_exec:n {
96 \def\spacegobbler\cS\ {}
97
98 \peek_analysis_map_inline:n {
99 \expandafter \string #1
100 \peek_analysis_map_break:
101 }
102 \c_begin_group_token \cO\_
103
104 \def\twospacegobbler\cS\ \cS\ {}
105
106 \def \cA\^^M {\cO\^^M}
107 }
108 \end{verbatim}
109 More details: the token list following one of the |\c|\meta{character} token might either be a single token, or a braced group. In the latter case their string representation without any |\escapechar| will be concatenated together.
110
111 For example:
112
113 \begin{itemize}
114 \item |\cL{12~34}| in |\ExplSyntaxOn| mode results in the 5 tokens with catcode letter and char code 1, 2, \meta{space}, 3, 4 respectively.
115 \item |\cO\abc| results in 3 tokens |abc| with catcode other.
116 \item |\cO{\ab\cd\ef}| results in 6 tokens |abcdef|. Note that this is different from the |\detokenized| value (that is, the spaces are removed) even when |\escapechar=-1|.
117 \item |\cC{\ab\cd\ef}| results in the control sequence |\abcdef|.
118 \item |\cO\\| results in a single token with char code |\| and catcode other.
119 \item |\cA\^^M| results in a single token with catcode active and char code 13. (because normal \TeX\ processing rules transform the token |\^^M| to a control sequence with name length = 1 before passing it to the function \cs{precattl_exec:n})
120 \end{itemize}
121
122 Remark: don't put explicit space token right after a |\cO| or similar. (nevertheless this is nontrivial to trigger because of how \TeX\ works normally)
123
124 Warning: a \verb*|\ | at the end of a line will be interpreted as an escape sequence containing the |\endlinechar|.
125
126
127 \begin{function}{\precattlExec}
128 \LaTeX2-style synonym for the function above.
129 \end{function}
130
131 \begin{function}{\precattl_set:Nn}
132 \begin{syntax}
133 \cs{precattl_set:Nn} \meta{tl var} \Arg{token list}
134 \end{syntax}
135 Same as above, but instead of executing the processed \Arg{token list}, the result is stored into \meta{tl var}.
136
137 Mostly equivalent to |\precattl_exec:n {\tl_set:Nn| \meta{tl var} \Arg{token list}|}|.
138 \end{function}
139
140 \StopEventually{%
141 \PrintChanges
142 \PrintIndex
143 }
144 \Finale
145 \end{document}
146