Debian Codegraph @

texmf-dist/source/lualatex/piton/piton-code.dtx

raw
1 % \iffalse -*- coding: utf-8 ; -*- \fi
2 % \iffalse meta-comment
3 %
4 % Copyright (C) 2022-2026 by F. Pantigny
5 % -----------------------------------
6 %
7 % This file may be distributed and/or modified under the
8 % conditions of the LaTeX Project Public License, either version 1.3
9 % of this license or (at your option) any later version.
10 % The latest version of this license is in:
11 %
12 % http://www.latex-project.org/lppl.txti
13 %
14 % and version 1.3 or later is part of all distributions of LaTeX
15 % version 2005/12/01 or later.
16 %
17 % \fi
18 % \iffalse
19 %
20 %<*batchfile>
21 \begingroup
22 \input l3docstrip.tex
23 \endgroup
24 %</batchfile>
25 %
26 %<@@=piton>
27 %<*driver>
28 \documentclass{l3doc}
29 \VerbatimFootnotes
30 \usepackage{geometry}
31 \geometry{left=2.8cm,right=2.8cm,top=2.5cm,bottom=2.5cm,papersize={21cm,29.7cm}}
32 \usepackage{fontspec}
33 \usepackage[dvipsnames,svgnames]{xcolor}
34 \usepackage{luacolor,lua-ul,upquote}
35 \fvset{commandchars=\~\#\@,formatcom=\color{gray}}
36 \usepackage[footnotehyper]{piton}
37 \PitonOptions
38 {
39 splittable = 4 ,
40 math-comments ,
41 begin-escape = ! ,
42 end-escape = ! ,
43 begin-escape-math = \( ,
44 end-escape-math = \) ,
45 }
46
47 \parindent 0pt
48 \skip\footins = 2\bigskipamount
49
50
51 \PitonOptions{gobble=2}
52
53 \EnableCrossrefs
54
55 \begin{document}
56 \DocInput{piton-code.dtx}
57 \end{document}
58 %</driver>
59 % \fi
60 % \iffalse
61 %<*STY>
62 % \fi
63 \def\PitonFileVersion{4.13}
64 \def\PitonFileDate{2026/05/25}
65 % \iffalse
66 %</STY>
67 %<*LUA>
68 piton_version = "4.13" -- 2026/05/25
69 %</LUA>
70 %\fi
71 %
72 % \catcode`\" = 11
73 %
74 % \title{The package \pkg{piton}\thanks{This document corresponds to the
75 % version~\PitonFileVersion\space of \pkg{piton}, at the date of~\PitonFileDate.}}
76 % \author{F. Pantigny \\ \texttt{[email protected]}}
77 %
78 % \maketitle
79 %
80 % \begin{abstract}
81 % This document is the documented code of the LuaLaTeX package \pkg{piton}. It
82 % is \emph{not} its user's guide. The guide of utilisation is the document
83 % |piton.pdf| (with a French translation: |piton-french.pdf|).
84 % \end{abstract}
85 %
86 %
87 %
88 %
89 %
90 % \medskip
91 % The development of the extension \pkg{piton} is done on the following GitHub
92 % depot:
93 %
94 % \verb|https://github.com/fpantigny/piton|
95 %
96 % \section{Introduction}
97 %
98 % The main job of the package \pkg{piton} is to take in as input a computer
99 % listing and to send back to LaTeX as output that code \emph{with interlaced LaTeX
100 % instructions of formatting}.
101 %
102 % In fact, all that job is done by a \textsc{lpeg} called |LPEG1[<language>]|
103 % where |<language>| is a Lua string which is the name of the computer language.
104 % That \textsc{lpeg}, when matched against the string of a computer listing,
105 % returns as capture a Lua table containing data to send to LaTeX. The only
106 % thing to do after will be to apply |tex.tprint| to each element of that
107 % table.\footnote{Recall that |tex.tprint| takes in as argument a Lua table
108 % whose first component is a ``catcode table'' and the second element a string.
109 % The string will be sent to LaTeX with the regime of catcodes specified by the
110 % catcode table. If no catcode table is provided, the standard catcodes of LaTeX
111 % will be used.}
112 %
113 % In fact, there is a variant of the \textsc{lpeg} |LPEG1[<language>]|, called
114 % |LPEG2[<language>]|. The latter uses the first one and will be used to format
115 % the whole content of an environment |{Piton}| (with, in particular, small tuning
116 % for the beginning and the end).
117 %
118 % \bigskip
119 % Consider, for example, the following Python code:
120 %
121 % \begin{Piton}
122 % def parity(x):
123 % return x%2
124 % \end{Piton}
125 %
126 % The capture returned by the \textsc{lpeg} |LPEG1['python']| (in Lua, this may
127 % also be written |LPEG1.python|) against that code is the Lua table containing
128 % the following elements :
129 %
130 % \bigskip
131 % \begin{minipage}{\linewidth}
132 % \color{gray}
133 %
134 % |{ "\\__piton_begin_line:" }|\footnote{Each line of the computer listings will
135 % be encapsulated in a pair: \texttt{\textbackslash_@@_begin_line:} --
136 % \texttt{\textbackslash@@_end_line:}. The token
137 % \texttt{\textbackslash@@_end_line:} must be explicit because it will be used as
138 % marker in order to delimit the argument of the command \texttt{\textbackslash
139 % @@\_begin\_line:}. Both tokens \texttt{\textbackslash_@@_begin_line:} and
140 % \texttt{\textbackslash@@_end_line:} will be nullified in the command
141 % \texttt{\textbackslash piton} (since there can't be lines breaks in the
142 % argument of a command \texttt{\textbackslash piton}).}
143 %
144 % \texttt{\{ "\{\textbackslash PitonStyle\{Keyword\}\{" \}}\footnote{The
145 % lexical elements for which we have a \pkg{piton} style will be
146 % formatted via the use of the command \texttt{\textbackslash PitonStyle}.
147 % Such an element is typeset in LaTeX via the syntax \texttt{\{\textbackslash
148 % PitonStyle\{\textsl{style}\}\{...\}\}} because the instructions inside an \texttt{\textbackslash
149 % PitonStyle} may be both semi-global declarations like
150 % \texttt{\textbackslash bfseries} and commands with one argument like
151 % \texttt{\textbackslash fbox}.}
152 %
153 % \texttt{\{
154 % luatexbase.catcodetables.other\footnote{\texttt{luatexbase.catcodetables.other} is a mere number which corresponds to the ``catcode table'' whose all characters have the catcode ``other'' (which means that they will be typeset by LaTeX verbatim).}, "def" \} }
155 %
156 % |{ "}}" }|
157 %
158 % |{ luatexbase.catcodetables.other, " " }|
159 %
160 % |{ "{\PitonStyle{Name.Function}{" }|
161 %
162 % |{ luatexbase.catcodetables.other, "parity" }|
163 %
164 % |{ "}}" }|
165 %
166 % |{ luatexbase.catcodetables.other, "(" }|
167 %
168 % |{ luatexbase.catcodetables.other, "x" }|
169 %
170 % |{ luatexbase.catcodetables.other, ")" }|
171 %
172 % |{ luatexbase.catcodetables.other, ":" }|
173 %
174 % |{ "\\__piton_end_line: \\__piton_par: \\__piton_begin_line:" }|
175 %
176 % |{ luatexbase.catcodetables.other, " " }|
177 %
178 % |{ "{\PitonStyle{Keyword}{" }|
179 %
180 % |{ luatexbase.catcodetables.other, "return" }|
181 %
182 % |{ "}}" }|
183 %
184 % |{ luatexbase.catcodetables.other, " " }|
185 %
186 % |{ luatexbase.catcodetables.other, "x" }|
187 %
188 % |{ "{\PitonStyle{Operator}{" }|
189 %
190 % |{ luatexbase.catcodetables.other, "%" }|
191 %
192 % |{ "}}" }|
193 %
194 % |{ "{\PitonStyle{Number}{" }|
195 %
196 % |{ luatexbase.catcodetables.other, "2" }|
197 %
198 % |{ "}}" }|
199 %
200 % |{ "\\__piton_end_line:" }|
201 %
202 % \end{minipage}
203 %
204 % \bigskip
205 % We give now the LaTeX code which is sent back by Lua to TeX (we have written
206 % on several lines for legibility but no character |\r| will be sent to LaTeX). The
207 % characters which are greyed-out are sent to LaTeX with the catcode ``other''
208 % (=12). All the others characters are sent with the regime of catcodes of L3
209 % (as set by |\ExplSyntaxOn|).
210 %
211 %
212 % \begingroup
213 % \def\gbox#1{\colorbox{gray!20}{\strut #1}}
214 % \setlength{\fboxsep}{1pt}
215 %
216 % \begin{Verbatim*}[formatcom = \color{black}]
217 % \__piton_begin_line:{\PitonStyle{Keyword}{~gbox#def@}}
218 % ~gbox# @{\PitonStyle{Name.Function}{~gbox#parity@}}~gbox#(x):@\__piton_end_line:\__piton_par:
219 % \__piton_begin_line:~gbox# @{\PitonStyle{Keyword}{~gbox#return@}}
220 % ~gbox# x@{\PitonStyle{Operator}{~gbox#%@}}{\PitonStyle{Number}{~gbox#2@}}\__piton_end_line:
221 % \end{Verbatim*}
222 % \endgroup
223 %
224 %
225 %
226 %
227 % \section{The L3 part of the implementation}
228 %
229 % \subsection{Declaration of the package}
230 % \begin{macrocode}
231 %<*STY>
232 \NeedsTeXFormat{LaTeX2e}
233 \ProvidesExplPackage
234 {piton}
235 {\PitonFileDate}
236 {\PitonFileVersion}
237 {Highlight computer listings with LPEG on LuaLaTeX}
238 % \end{macrocode}
239 %
240 % \begin{macrocode}
241 \msg_new:nnn { piton } { latex-too-old }
242 {
243 Your~LaTeX~release~is~too~old. \\
244 You~need~at~least~the~version~of~2025-06-01. \\
245 If~you~use~Overleaf,~you~need~at~least~"TeX~Live~2025".\\
246 The~package~'piton'~won't~be~loaded.
247 }
248 % \end{macrocode}
249 %
250 % \begin{macrocode}
251 \providecommand { \IfFormatAtLeastTF } { \@ifl@t@r \fmtversion }
252 \IfFormatAtLeastTF
253 { 2025-06-01 }
254 { }
255 { \msg_critical:nn { piton } { latex-too-old } }
256 % \end{macrocode}
257 % \bigskip
258 % The command |\text| provided by the package \pkg{amstext} will be used to
259 % allow the use of the command |\piton{...}| (with the standard syntax) in
260 % mathematical mode.
261 % \begin{macrocode}
262 \RequirePackage { amstext }
263 % \end{macrocode}
264 %
265 % \bigskip
266 % The command |\marginalia| of the package \pkg{marginalia} will be used
267 % for the margin notes created by the keys |paperclip| and |annotation|.
268 % \begin{macrocode}
269 \RequirePackage { marginalia }
270 % \end{macrocode}
271 %
272 % \bigskip
273 % The package \pkg{transparent} is compatible with \pkg{pdfmanagement} (which
274 % is not loaded by \pkg{piton} but which is used for the key |join| when it is
275 % loaded).
276 % \begin{macrocode}
277 \RequirePackage { transparent }
278 % \end{macrocode}
279 %
280 %
281 % \bigskip
282 % \begin{macrocode}
283 \cs_new_protected:Npn \@@_error:n { \msg_error:nn { piton } }
284 \cs_new_protected:Npn \@@_warning:n { \msg_warning:nn { piton } }
285 \cs_new_protected:Npn \@@_warning:nn { \msg_warning:nnn { piton } }
286 \cs_new_protected:Npn \@@_error:nn { \msg_error:nnn { piton } }
287 \cs_new_protected:Npn \@@_error:nnn { \msg_error:nnnn { piton } }
288 \cs_new_protected:Npn \@@_fatal:n { \msg_fatal:nn { piton } }
289 \cs_new_protected:Npn \@@_fatal:nn { \msg_fatal:nnn { piton } }
290 \cs_new_protected:Npn \@@_msg_new:nn { \msg_new:nnn { piton } }
291 % \end{macrocode}
292 %
293 % \bigskip
294 % With Overleaf (and also TeXPage), by default, a document is compiled in
295 % non-stop mode. When there is an error, there is no way to the user to use the
296 % key H in order to have more information. That's why we decide to put that
297 % piece of information (for the messages with such information) in the main part
298 % of the message when the key |messages-for-Overleaf| is used (at load-time).
299 % \begin{macrocode}
300 \cs_new_protected:Npn \@@_msg_new:nnn #1 #2 #3
301 {
302 \bool_if:NTF \g_@@_messages_for_Overleaf_bool
303 { \msg_new:nnn { piton } { #1 } { #2 \\ #3 } }
304 { \msg_new:nnnn { piton } { #1 } { #2 } { #3 } }
305 }
306 % \end{macrocode}
307 %
308 % \bigskip
309 % We also create commands which will generate usually an error but only a
310 % warning on Overleaf. The argument is given by curryfication.
311 % \begin{macrocode}
312 \cs_new_protected:Npn \@@_error_or_warning:n
313 { \bool_if:NTF \g_@@_messages_for_Overleaf_bool \@@_warning:n \@@_error:n }
314 \cs_new_protected:Npn \@@_error_or_warning:nn
315 { \bool_if:NTF \g_@@_messages_for_Overleaf_bool \@@_warning:nn \@@_error:nn }
316 % \end{macrocode}
317 %
318 % We try to detect whether the compilation is done on Overleaf. We use
319 % |\c_sys_jobname_str| because, with Overleaf, the value of |\c_sys_jobname_str|
320 % is always ``|output|''.
321 % \begin{macrocode}
322 \bool_new:N \g_@@_messages_for_Overleaf_bool
323 \bool_gset:Nn \g_@@_messages_for_Overleaf_bool
324 {
325 \str_if_eq_p:on \c_sys_jobname_str { _region_ } % for Emacs
326 || \str_if_eq_p:on \c_sys_jobname_str { output } % for Overleaf
327 }
328 % \end{macrocode}
329 %
330 % \bigskip
331 % \begin{macrocode}
332 \@@_msg_new:nn { LuaLaTeX~mandatory }
333 {
334 LuaLaTeX~is~mandatory.\\
335 The~package~'piton'~requires~the~engine~LuaLaTeX.\\
336 \str_if_eq:onT \c_sys_jobname_str { output }
337 { If~you~use~Overleaf,~you~can~switch~to~LuaLaTeX~in~
338 "File~>~Settings~>~Compiler"~and~if~you~use~TeXPage,
339 ~you~should~go~in~"Settings". \\ }
340 \IfClassLoadedT { beamer }
341 {
342 Since~you~use~Beamer,~don't~forget~to~use~piton~in~frames~with~
343 the~key~'fragile'.\\
344 }
345 \IfClassLoadedT { ltx-talk }
346 {
347 Since~you~use~'ltx-talk',~don't~forget~to~use~piton~in~
348 environments~'frame*'.\\
349 }
350 }
351 \sys_if_engine_luatex:F { \@@_fatal:n { LuaLaTeX~mandatory } }
352 % \end{macrocode}
353 %
354 % \bigskip
355 % \begin{macrocode}
356 \RequirePackage { luacode }
357 % \end{macrocode}
358 %
359 % \bigskip
360 % \begin{macrocode}
361 \@@_msg_new:nnn { piton.lua~not~found }
362 {
363 The~file~'piton.lua'~can't~be~found.~
364 If~you~want~to~know~how~to~retrieve~the~file~'piton.lua',~type~H~<return>.
365 }
366 {
367 On~the~site~CTAN,~go~to~the~page~of~'piton':~https://ctan.org/pkg/piton.~
368 The~file~'README.md'~explains~how~to~retrieve~the~files~'piton.sty'~and~
369 'piton.lua'.
370 }
371 % \end{macrocode}
372 %
373 % \bigskip
374 % \begin{macrocode}
375 \file_if_exist:nF { piton.lua } { \@@_fatal:n { piton.lua~not~found } }
376 % \end{macrocode}
377 %
378 %
379 % \bigskip
380 % We define a set of keys for the options at load-time.
381 % \begin{macrocode}
382 \keys_define:nn { piton }
383 {
384 footnote .bool_gset:N = \g_@@_footnote_bool ,
385 footnotehyper .bool_gset:N = \g_@@_footnotehyper_bool ,
386 footnote .usage:n = load ,
387 footnotehyper .usage:n = load ,
388
389 beamer .bool_gset:N = \g_@@_beamer_bool ,
390 beamer .usage:n = load ,
391
392 unknown .code:n = \@@_error:n { Unknown~key~for~package }
393 }
394 % \end{macrocode}
395 %
396 % \begin{macrocode}
397 \@@_msg_new:nn { Unknown~key~for~package }
398 {
399 Unknown~key.\\
400 You~have~used~the~key~'\l_keys_key_str'~when~loading~piton~
401 but~the~only~keys~available~here~are~'beamer',~'footnote'~
402 and~'footnotehyper'.~Other~keys~are~available~in~
403 \token_to_str:N \PitonOptions.\\
404 That~key~will~be~ignored.
405 }
406 % \end{macrocode}
407 %
408 %
409 % \bigskip
410 % We process the options provided by the user at load-time.
411 % \begin{macrocode}
412 \ProcessKeyOptions
413 % \end{macrocode}
414 %
415 %
416 % \bigskip
417 % \begin{macrocode}
418 \IfClassLoadedT { beamer } { \bool_gset_true:N \g_@@_beamer_bool }
419 \IfClassLoadedT { ltx-talk } { \bool_gset_true:N \g_@@_beamer_bool }
420 \IfPackageLoadedT { beamerarticle } { \bool_gset_true:N \g_@@_beamer_bool }
421 % \end{macrocode}
422 %
423 % \begin{macrocode}
424 \lua_now:e
425 {
426 piton = piton~or~{ }
427 piton.last_code = ''
428 piton.last_language = ''
429 piton.join = ''
430 piton.write = ''
431 piton.path_write = ''
432 \bool_if:NT \g_@@_beamer_bool { piton.beamer = true }
433 }
434 % \end{macrocode}
435 %
436 %
437 % \bigskip
438 % \begin{macrocode}
439 \RequirePackage { xcolor }
440 % \end{macrocode}
441 %
442 %
443 % \begin{macrocode}
444 \@@_msg_new:nn { footnote~with~footnotehyper~package }
445 {
446 Footnote~forbidden.\\
447 You~can't~use~the~option~'footnote'~because~the~package~
448 footnotehyper~has~already~been~loaded.~
449 If~you~want,~you~can~use~the~option~'footnotehyper'~and~the~footnotes~
450 within~the~environments~of~piton~will~be~extracted~with~the~tools~
451 of~the~package~footnotehyper.\\
452 If~you~go~on,~the~package~footnote~won't~be~loaded.
453 }
454 % \end{macrocode}
455 %
456 % \begin{macrocode}
457 \@@_msg_new:nn { footnotehyper~with~footnote~package }
458 {
459 You~can't~use~the~option~'footnotehyper'~because~the~package~
460 footnote~has~already~been~loaded.~
461 If~you~want,~you~can~use~the~option~'footnote'~and~the~footnotes~
462 within~the~environments~of~piton~will~be~extracted~with~the~tools~
463 of~the~package~footnote.\\
464 If~you~go~on,~the~package~footnotehyper~won't~be~loaded.
465 }
466 % \end{macrocode}
467 %
468 % \medskip
469 % \begin{macrocode}
470 \bool_if:NT \g_@@_footnote_bool
471 {
472 % \end{macrocode}
473 % The class \cls{beamer} has its own system to extract footnotes and that's why
474 % we have nothing to do if \cls{beamer} is used.
475 % \begin{macrocode}
476 \IfClassLoadedTF { beamer }
477 { \bool_gset_false:N \g_@@_footnote_bool }
478 {
479 \IfPackageLoadedTF { footnotehyper }
480 { \@@_error:n { footnote~with~footnotehyper~package } }
481 { \usepackage { footnote } }
482 }
483 }
484 % \end{macrocode}
485 %
486 % \begin{macrocode}
487 \bool_if:NT \g_@@_footnotehyper_bool
488 {
489 % \end{macrocode}
490 % The class \cls{beamer} has its own system to extract footnotes and that's why
491 % we have nothing to do if \cls{beamer} is used.
492 % \begin{macrocode}
493 \IfClassLoadedTF { beamer }
494 { \bool_gset_false:N \g_@@_footnote_bool }
495 {
496 \IfPackageLoadedTF { footnote }
497 { \@@_error:n { footnotehyper~with~footnote~package } }
498 { \usepackage { footnotehyper } }
499 \bool_gset_true:N \g_@@_footnote_bool
500 }
501 }
502 % \end{macrocode}
503 % The flag |\g_@@_footnote_bool| is raised and so, we will only have to test
504 % |\g_@@_footnote_bool| in order to know if we have to insert an environment
505 % |{savenotes}|.
506 %
507 %
508 % \bigskip
509 % \subsection{Parameters and technical definitions}
510 %
511 % \begin{macrocode}
512 \dim_new:N \l_@@_rounded_corners_dim
513 % \end{macrocode}
514 %
515 % \begin{macrocode}
516 \bool_new:N \l_@@_in_label_bool
517 % \end{macrocode}
518 %
519 % \begin{macrocode}
520 \dim_new:N \l_@@_tmpc_dim
521 % \end{macrocode}
522 % \medskip
523 %
524 % The listing that we have to format will be stored in |\l_@@_listing_tl|.
525 % That applies both for the command |\PitonInputFile| and the environment |{Piton}|
526 % (or another environment defined by |\NewPitonEnvironment|).
527 % \begin{macrocode}
528 \tl_new:N \l_@@_listing_tl
529 % \end{macrocode}
530 %
531 % \medskip
532 % The content of an environment such as |{Piton}| will be composed first in the
533 % following box, but that box will (sometimes) be \emph{unvboxed} at the end.
534 %
535 % We need a global variable (see |\@@_add_bg_and_right_nb_to_output_box:|).
536 % \begin{macrocode}
537 \box_new:N \g_@@_output_box
538 % \end{macrocode}
539 %
540 %
541 % The following string will contain the name of the computer language
542 % considered (the initial value is |python|).
543 %
544 % \begin{macrocode}
545 \str_new:N \l_piton_language_str
546 \str_set:Nn \l_piton_language_str { python }
547 % \end{macrocode}
548 %
549 % \medskip
550 % Each time an environment of \pkg{piton} is used, the computer listing in the
551 % body of that environment will be stored in the following global string.
552 % \begin{macrocode}
553 \tl_new:N \g_piton_last_code_tl
554 % \end{macrocode}
555 %
556 % \medskip
557 % The following parameter corresponds to the key |path| (which is the path used
558 % to include files by |\PitonInputFile|). Each component of that sequence will
559 % be a string (type |str|).
560 % \begin{macrocode}
561 \seq_new:N \l_@@_path_seq
562 % \end{macrocode}
563 %
564 % \medskip
565 % The names of all the join files will be stored in the following sequence:
566 % \begin{macrocode}
567 \seq_new:N \g_@@_join_seq
568 % \end{macrocode}
569 %
570 %
571 % \medskip
572 % \begin{macrocode}
573 \str_new:N \l_@@_join_str
574 % \end{macrocode}
575 %
576 % \medskip
577 % When the key |tcolorbox| is used, you will have to take into account the width
578 % of the graphical elements added by |tcolorbox| on both sides of the listing.
579 % We will put that quantity in the following variable.
580 % \begin{macrocode}
581 \dim_new:N \l_@@_tcb_margins_dim
582 % \end{macrocode}
583 %
584 % \medskip
585 % The following parameter corresponds to the key |box|.
586 % \begin{macrocode}
587 \str_new:N \l_@@_box_str
588 % \end{macrocode}
589 %
590 % \medskip
591 % In order to have a better control over the keys.
592 % \begin{macrocode}
593 \bool_new:N \l_@@_in_PitonOptions_bool
594 \bool_new:N \l_@@_in_PitonInputFile_bool
595 % \end{macrocode}
596 %
597 %
598 % \medskip
599 % We will compute (with Lua) the numbers of lines of the listings (or
600 % \emph{chunks} of listings when |split-on-empty-lines| is in force) and store
601 % it in the following counter.
602 % \begin{macrocode}
603 \int_new:N \g_@@_nb_lines_int
604 % \end{macrocode}
605 %
606 % The same for the number of non-empty lines of the listings.
607 % \begin{macrocode}
608 \int_new:N \l_@@_nb_non_empty_lines_int
609 % \end{macrocode}
610 %
611 %
612 % \medskip
613 % The following counter will be used to count the lines during the composition.
614 % It will take into account all the lines, empty or not empty. It won't be used
615 % to print the numbers of the lines but will be used to allow or disallow line
616 % breaks (when |splittable| is in force) and for the color of the background
617 % (when |background-color| is used with a \emph{list} of colors or when
618 % |\rowcolor| is used).
619 % \begin{macrocode}
620 \int_new:N \g_@@_line_int
621 % \end{macrocode}
622 %
623 %
624 % \medskip
625 % The following string corresponds to the key |background-color| of |\PitonOptions|.
626 % \begin{macrocode}
627 \clist_new:N \l_@@_bg_color_clist
628 % \end{macrocode}
629 %
630 % We will also keep in memory the length of the previous |clist| (for efficiency).
631 % \begin{macrocode}
632 \int_new:N \l_@@_bg_colors_int
633 % \end{macrocode}
634 %
635 % \medskip
636 % \begin{macrocode}
637 \tl_new:N \l_@@_space_in_string_tl
638 % \end{macrocode}
639 %
640 % \medskip
641 % The following parameters correspond to the keys |begin-range| and |end-range| of
642 % the command |\PitonInputFile|.
643 % \begin{macrocode}
644 \str_new:N \l_@@_begin_range_str
645 \str_new:N \l_@@_end_range_str
646 % \end{macrocode}
647 %
648 % \medskip
649 % The following boolean corresponds to the key |math-comments| (available only
650 % in the preamble of the LaTeX document).
651 % \begin{macrocode}
652 \bool_new:N \g_@@_math_comments_bool
653 % \end{macrocode}
654 %
655 % \medskip
656 % The argument of |\PitonInputFile|.
657 % \begin{macrocode}
658 \str_new:N \l_@@_file_name_str
659 % \end{macrocode}
660 %
661 % \medskip
662 % The following line can't be deleted.
663 % \begin{macrocode}
664 \bool_new:N \l_@@_tcolorbox_bool
665 % \end{macrocode}
666 %
667 % \medskip
668 % The following boolean corresponds to the keys |paperclip| and |annotation|.
669 % \begin{macrocode}
670 \bool_new:N \l_@@_paperclip_bool
671 \str_new:N \l_@@_paperclip_str
672 % \end{macrocode}
673 %
674 % \medskip
675 % The listings embedded in the \textsc{pdf} by the key |paperclip| will be numbered by
676 % the following counter.
677 % \begin{macrocode}
678 \int_new:N \g_@@_paperclip_int
679 % \end{macrocode}
680 %
681 % \medskip
682 % The following boolean corresponds to the key |show-spaces|.
683 % \begin{macrocode}
684 \bool_new:N \l_@@_show_spaces_bool
685 % \end{macrocode}
686 %
687 %
688 % \medskip
689 % The following boolean corresponds to the key |break-lines-in-piton|.
690 % \begin{macrocode}
691 \bool_new:N \l_@@_break_lines_in_piton_bool
692 % \end{macrocode}
693 %
694 % \medskip
695 % The following flag will be raised when the key |max-width| is used (and
696 % when |width| is used with the key |min|, which is equivalent to
697 % |max-width=\linewidth|). Note also that the key |box| sets |width=min| (except
698 % if |min| is used with a numerical value).
699 % \begin{macrocode}
700 \bool_new:N \l_@@_minimize_width_bool
701 % \end{macrocode}
702 %
703 % \bigskip
704 % The following dimension corresponds to the key |width|. It's meant to be the whole width of
705 % the environment (for instance, the width of the box of \pkg{tcolorbox} when the key
706 % |tcolorbox| is used). The initial value is $0$~pt which means that the end user has not
707 % used the key. In that case, it will be set equal to the current value of |\linewidth|
708 % in |\@@_pre_composition:|.
709 %
710 % However if |max-width| is used (or |width=min| which is equivalent to
711 % |max-width=\linewidth|), the actual width of the final environment in the \textsc{pdf}
712 % may (potentially) be smaller.
713 % \begin{macrocode}
714 \dim_new:N \l_@@_width_dim
715 % \end{macrocode}
716 %
717 % \medskip
718 % |\l_@@_listing_width_dim| will be the width of the listing taking into account the
719 % lines of code (of course) but also:
720 % \begin{itemize}
721 % \item |l_@@_left_margin_dim| (for the numbers of lines);
722 % \item a small margin when |background-color| is in force\footnote{Remark that the mere
723 % use of |\rowcolor| does not add those small margins.}).
724 % \end{itemize}
725 % \begin{macrocode}
726 \dim_new:N \l_@@_listing_width_dim
727 % \end{macrocode}
728 % However, if |max-width| is used (or |width=min| which is equivalent to
729 % |max-width=\linewidth|), that length will be computed once again in
730 % |\@@_create_output_box:|
731 %
732 % \medskip
733 % |\l_@@_code_width_dim| will be the length of the lines of code, without the potential
734 % margins (for the backgrounds and for |length-margin| for the number of lines).
735 %
736 % It will be computed in |\@@_compute_code_width:|.
737 % \begin{macrocode}
738 \dim_new:N \l_@@_code_width_dim
739 % \end{macrocode}
740 %
741 % \medskip
742 % \begin{macrocode}
743 \box_new:N \l_@@_line_box
744 % \end{macrocode}
745 %
746 % \medskip
747 % The following dimension corresponds to the keys |left-margin| and |right-margin|.
748 % \begin{macrocode}
749 \dim_new:N \l_@@_left_margin_dim
750 \dim_new:N \l_@@_right_margin_dim
751 % \end{macrocode}
752 %
753 % \medskip
754 % The following boolean will be set when the key |left-margin=auto| is used.
755 % \begin{macrocode}
756 \bool_new:N \l_@@_left_margin_auto_bool
757 \bool_new:N \l_@@_right_margin_auto_bool
758 % \end{macrocode}
759 %
760 %
761 % \medskip
762 % The following boolean corresponds to the key |line-numbers/lmmono10-draw|.
763 % \begin{macrocode}
764 \bool_new:N \l_@@_lmodern_drawn_bool
765 % \end{macrocode}
766 %
767 % \medskip
768 % When the key |line-numbers/position| is set to |right|, we will have
769 % to keep in memory the numbers of the lines in the following sequence.
770 % \begin{macrocode}
771 \seq_new:N \g_@@_visual_line_numbers_seq
772 % \end{macrocode}
773 %
774 % \medskip
775 % Be careful. The following sequence |\g_@@_languages_seq| is not the list of
776 % the languages supported by \pkg{piton}. It's the list of the languages for
777 % which at least a user function has been defined. We need that sequence only
778 % for the command |\PitonClearUserFunctions| when it is used without its
779 % optional argument: it must clear the whole list of languages for which at least
780 % a user function has been defined.
781 % \begin{macrocode}
782 \seq_new:N \g_@@_languages_seq
783 % \end{macrocode}
784 %
785 %
786 % \begin{macrocode}
787 \cs_new_protected:Npn \@@_tab:
788 {
789 \bool_if:NTF \l_@@_show_spaces_bool
790 {
791 \hbox_set:Nn \l_tmpa_box
792 { \prg_replicate:nn \l_@@_tab_size_int { ~ } }
793 \dim_set:Nn \l_tmpa_dim { \box_wd:N \l_tmpa_box }
794 \( \mathcolor { gray }
795 { \hbox_to_wd:nn \l_tmpa_dim { \rightarrowfill } } \)
796 }
797 { \hbox:n { \prg_replicate:nn \l_@@_tab_size_int { ~ } } }
798 \int_gadd:Nn \g_@@_indentation_int \l_@@_tab_size_int
799 }
800 % \end{macrocode}
801 %
802 % \medskip
803 % The following token list will be used only for the spaces in the strings.
804 % \begin{macrocode}
805 \tl_set_eq:NN \l_@@_space_in_string_tl \nobreakspace
806 % \end{macrocode}
807 % When the key |break-lines-in-piton| is set, that parameter will be replaced by
808 % |\space| (in |\piton| with the standard syntax) and when the key
809 % |show-spaces-in-strings| is set, it will be replaced by ␣ (U+2423).
810 %
811 %
812 % \medskip
813 % At each line, the following counter will count the spaces at the beginning.
814 % \begin{macrocode}
815 \int_new:N \g_@@_indentation_int
816 % \end{macrocode}
817 %
818 %
819 %
820 % \bigskip
821 % In the environment |{Piton}|, the command |\label| will be linked to the
822 % following command.
823 % \begin{macrocode}
824 \cs_new_protected:Npn \@@_label:n #1
825 {
826 \bool_if:NTF \l_@@_line_numbers_bool
827 {
828 \@bsphack
829 \protected@write \@auxout { }
830 {
831 \string \newlabel { #1 }
832 {
833 { \int_use:N \g_@@_visual_line_int }
834 { \thepage }
835 { }
836 { line.#1 }
837 { }
838 }
839 }
840 \@esphack
841 \IfPackageLoadedT { hyperref }
842 { \Hy@raisedlink { \hyper@anchorstart { line.#1 } \hyper@anchorend } }
843 }
844 { \@@_error:n { label~with~lines~numbers } }
845 }
846 % \end{macrocode}
847 %
848 % \bigskip
849 %
850 % The same goes for the command |\zlabel| if the |zref| package is loaded.
851 % Note that |\label| will also be linked to |\@@_zlabel:n| if the key
852 % |label-as-zlabel| is set to |true|.
853 % \begin{macrocode}
854 \cs_new_protected:Npn \@@_zlabel:n #1
855 {
856 \bool_if:NTF \l_@@_line_numbers_bool
857 {
858 \@bsphack
859 \protected@write \@auxout { }
860 {
861 \string \zref@newlabel { #1 }
862 {
863 \string \default { \int_use:N \g_@@_visual_line_int }
864 \string \page { \thepage }
865 \string \zc@type { line }
866 \string \anchor { line.#1 }
867 }
868 }
869 \@esphack
870 \IfPackageLoadedT { hyperref }
871 { \Hy@raisedlink { \hyper@anchorstart { line.#1 } \hyper@anchorend } }
872 }
873 { \@@_error:n { label~with~lines~numbers } }
874 }
875 % \end{macrocode}
876 %
877 % \bigskip
878 % In the environments |{Piton}| the command |\rowcolor| will be linked to the
879 % following one.
880 % \begin{macrocode}
881 \NewDocumentCommand { \@@_rowcolor:n } { o m }
882 {
883 \tl_gset:ce
884 { g_@@_color_ \int_eval:n { \g_@@_line_int + 1 } _ tl }
885 { \tl_if_novalue:nTF { #1 } { #2 } { [ #1 ] { #2 } } }
886 \bool_gset_true:N \g_@@_rowcolor_inside_bool
887 }
888 % \end{macrocode}
889 %
890 % In the command |piton| (in fact in |\@@_piton_standard| and |\@@_piton_verbatim|,
891 % the command |\rowcolor| will be linked to the following one (in order to nullify
892 % its effect).
893 % \begin{macrocode}
894 \NewDocumentCommand { \@@_noop_rowcolor } { o m } { }
895 % \end{macrocode}
896 %
897 % \bigskip
898 % The following commands correspond to the keys |marker/beginning| and
899 % |marker/end|. The values of that keys are functions that will be applied to
900 % the ``\emph{range}'' specified by the end user in an individual
901 % |\PitonInputFile|. They will construct the markers used to find textually in
902 % the external file loaded by \pkg{piton} the part which must be included (and
903 % formatted).
904 %
905 % These macros must \emph{not} be protected.
906 % \begin{macrocode}
907 \cs_new:Npn \@@_marker_beginning:n #1 { }
908 \cs_new:Npn \@@_marker_end:n #1 { }
909 % \end{macrocode}
910 %
911 %
912 %
913 % \bigskip
914 % The following token list will be evaluated at the end of
915 % |\@@_begin_line:|... |\@@_end_line:| and cleared at the end. It will be used
916 % by LPEG acting between the lines of the Python code in order to add
917 % instructions to be executed in vertical mode between the lines.
918 % \begin{macrocode}
919 \tl_new:N \g_@@_after_line_tl
920 % \end{macrocode}
921 %
922 %
923 % \bigskip
924 % The spaces at the end of a line of code are deleted by \pkg{piton}.
925 % However, it's not actually true: they are replace by |\@@_trailing_space:|.
926 % \begin{macrocode}
927 \cs_new_protected:Npn \@@_trailing_space: { }
928 % \end{macrocode}
929 % When we have to rescan some pieces of code, we will use |\@@_piton:n| and that
930 % command |\@@_piton:n| will set |\@@_trailing_space:| equal to |\space|.
931 %
932 % \bigskip
933 % \begin{macrocode}
934 \bool_new:N \g_@@_color_is_none_bool
935 \bool_new:N \g_@@_next_color_is_none_bool
936 % \end{macrocode}
937 %
938 % \bigskip
939 % \begin{macrocode}
940 \bool_new:N \g_@@_rowcolor_inside_bool
941 % \end{macrocode}
942 %
943 %
944 % \bigskip
945 % \subsection{Detected commands}
946 %
947 % There are four keys for ``detected commands and environments'':
948 % |detected-commands|, |raw-detected-commands|, |beamer-commands| and
949 % |beamer-environments|.
950 %
951 % In fact, there is also |vertical-detected-commands| but has a special treatment.
952 %
953 % For each of those keys, we keep a clist of the names of such detected
954 % commands and environments. For the commands, the corresponding |clist| will
955 % contain the name of the commands \emph{wihtout} the backlash.
956 %
957 % \begin{macrocode}
958 \clist_new:N \l_@@_detected_commands_clist
959 \clist_new:N \l_@@_raw_detected_commands_clist
960 \clist_new:N \l_@@_beamer_commands_clist
961 \clist_set:Nn \l_@@_beamer_commands_clist
962 { uncover, only , visible , invisible , alert , action}
963 \clist_new:N \l_@@_beamer_environments_clist
964 \clist_set:Nn \l_@@_beamer_environments_clist
965 { uncoverenv , onlyenv , visibleenv , invisibleenv , alertenv , actionenv }
966 % \end{macrocode}
967 % Remark that, since we have used clists, these clists, as token lists are
968 % ``purified'': there is no empty component and for each component, there is no
969 % space on both sides.
970 %
971 % \bigskip
972 % Of course, the value of those clists may be modified during the preamble of
973 % the document by using the corresponding key (|detected-commands|, etc.).
974 %
975 % However, after the |\begin{document}|, it's no longer possible to modify those
976 % clists because their contents will be used in the construction of the main
977 % \textsc{lpeg} for each computer language.
978 %
979 % \bigskip
980 % However, in a |\AtBeginDocument|, we will convert those clists into
981 % ``toks registers'' of TeX.
982 % \begin{macrocode}
983 \hook_gput_code:nnn { begindocument } { . }
984 {
985 \newtoks \PitonDetectedCommands
986 \newtoks \PitonRawDetectedCommands
987 \newtoks \PitonBeamerCommands
988 \newtoks \PitonBeamerEnvironments
989 % \end{macrocode}
990 % L3 does \emph{not} support those ``toks registers'' but it's still possible to
991 % affect to the ``toks registers'' the content of the clists with a L3-like syntax.
992 % \begin{macrocode}
993 \exp_args:NV \PitonDetectedCommands \l_@@_detected_commands_clist
994 \exp_args:NV \PitonRawDetectedCommands \l_@@_raw_detected_commands_clist
995 \exp_args:NV \PitonBeamerCommands \l_@@_beamer_commands_clist
996 \exp_args:NV \PitonBeamerEnvironments \l_@@_beamer_environments_clist
997 }
998 % \end{macrocode}
999 %
1000 % Then at the beginning of the document, when we will load the Lua file
1001 % |piton.lua|, we will read those ``toks registers'' within Lua (with
1002 % |tex.toks|) and convert them into Lua tables (and, then, use those tables to
1003 % construct \textsc{lpeg}).
1004 %
1005 %
1006 % \bigskip
1007 % When the key |vertical-detected-commands| is used, we will have to redefine
1008 % the corresponding commands in |\@@_pre_composition:|.
1009 %
1010 % The instructions for these redefinitions will be put in the following token list.
1011 % \begin{macrocode}
1012 \tl_new:N \g_@@_def_vertical_commands_tl
1013 % \end{macrocode}
1014 %
1015 % \bigskip
1016 % The following commands must \emph{not} be protected.
1017 % \begin{macrocode}
1018 \cs_new:Npn \@@_retrieve_backslash:n #1
1019 {
1020 \str_if_eq:eeTF \c_backslash_str { \str_head:n { #1 } }
1021 { \str_tail:n { #1 } }
1022 { #1 }
1023 }
1024 % \end{macrocode}
1025 %
1026 % \bigskip
1027 % \begin{macrocode}
1028 \cs_new_protected:Npn \@@_detected_commands:n #1
1029 {
1030 \clist_set:Nn \l_tmpa_clist { #1 }
1031 \clist_clear:N \l_tmpb_clist
1032 \clist_map_inline:Nn \l_tmpa_clist
1033 { \clist_put_right:Ne \l_tmpb_clist { \@@_retrieve_backslash:n { ##1 } } }
1034 \clist_if_in:NnTF \l_tmpb_clist { rowcolor }
1035 {
1036 \@@_error:n { rowcolor~in~detected-commands }
1037 \clist_remove_all:Nn \l_tmpb_clist { rowcolor }
1038 }
1039 \clist_put_right:No \l_@@_detected_commands_clist \l_tmpb_clist
1040 }
1041 % \end{macrocode}
1042 %
1043 % \bigskip
1044 % \begin{macrocode}
1045 \cs_new_protected:Npn \@@_raw_detected_commands:n #1
1046 {
1047 \clist_set:Nn \l_tmpa_clist { #1 }
1048 \clist_clear:N \l_tmpb_clist
1049 \clist_map_inline:Nn \l_tmpa_clist
1050 { \clist_put_right:Ne \l_tmpb_clist { \@@_retrieve_backslash:n { ##1 } } }
1051 \clist_put_right:No \l_@@_raw_detected_commands_clist \l_tmpb_clist
1052 }
1053 % \end{macrocode}
1054 %
1055 % \bigskip
1056 % \begin{macrocode}
1057 \cs_new_protected:Npn \@@_vertical_detected_commands:n #1
1058 {
1059 \clist_set:Nn \l_tmpa_clist { #1 }
1060 \clist_clear:N \l_tmpb_clist
1061 \clist_map_inline:Nn \l_tmpa_clist
1062 { \clist_put_right:Ne \l_tmpb_clist { \@@_retrieve_backslash:n { ##1 } } }
1063 \clist_put_right:No \l_@@_raw_detected_commands_clist \l_tmpb_clist
1064 \clist_map_inline:Nn \l_tmpb_clist
1065 {
1066 \cs_set_eq:cc { @@ _ old _ ##1 : } { ##1 }
1067 \cs_new_protected:cn { @@ _ new _ ##1 : n }
1068 {
1069 \bool_if:nTF
1070 { \l_@@_tcolorbox_bool || ! \str_if_empty_p:N \l_@@_box_str }
1071 {
1072 \tl_gput_right:Nn \g_@@_after_line_tl
1073 { \use:c { @@ _old _ ##1 : } { ####1 } }
1074 }
1075 {
1076 \cs_if_exist:cTF { g_@@_after_line _ \int_use:N \g_@@_line_int }
1077 { \tl_gput_right:cn }
1078 { \tl_gset:cn }
1079 { g_@@_after_line _ \int_eval:n { \g_@@_line_int + 1 }_tl }
1080 { \use:c { @@ _old _ ##1 : } { ####1 } }
1081 }
1082 }
1083 \tl_gput_right:Nn \g_@@_def_vertical_commands_tl
1084 { \cs_set_eq:cc { ##1 } { @@ _ new _ ##1 : n } }
1085 }
1086 }
1087 % \end{macrocode}
1088 %
1089 %
1090 %
1091 % \bigskip
1092 % \subsection{Treatment of a line of code}
1093 %
1094 %
1095 % \begin{macrocode}
1096 \cs_new_protected:Npn \@@_replace_spaces:n #1
1097 {
1098 \tl_set:Nn \l_tmpa_tl { #1 }
1099 \bool_if:NTF \l_@@_show_spaces_bool
1100 {
1101 \tl_set:Nn \l_@@_space_in_string_tl { ␣ } % U+2423
1102 \tl_replace_all:NVn \l_tmpa_tl \c_catcode_other_space_tl { ␣ } % U+2423
1103 }
1104 {
1105 % \end{macrocode}
1106 % If the key |break-lines-in-Piton| is in force, we replace all the characters
1107 % U+0020 (that is to say the spaces) by |\@@_breakable_space:|. Remark that,
1108 % except the spaces inserted in the LaTeX comments (and maybe in the math
1109 % comments), all these spaces are of catcode ``other'' (=12) and are
1110 % unbreakable.
1111 % \begin{macrocode}
1112 \bool_if:NT \l_@@_break_lines_in_Piton_bool
1113 {
1114 \tl_if_eq:NnF \l_@@_space_in_string_tl { ␣ }
1115 { \tl_set_eq:NN \l_@@_space_in_string_tl \@@_breakable_space: }
1116 % \end{macrocode}
1117 %
1118 % In the following code, we have to replace all the spaces in the token list
1119 % |\l_tmpa_tl|. That means that this replacement must be ``recursive'': even the
1120 % spaces which are within brace groups (|{...}|) must be replaced. For instance,
1121 % the spaces in long strings of Python are within such groups since there are
1122 % within a command |\PitonStyle{String.Long}{...}|. That's why the
1123 % use of |\tl_replace_all:Nnn| is not enough.
1124 %
1125 % The first implementation was using |\tl_regex_replace_all:nnN|
1126 %
1127 % |\tl_regex_replace_all:nnN { \x20 } { \c { @@_breakable_space: } } \l_tmpa_tl|
1128 %
1129 % but that programming was certainly slow.
1130 %
1131 % Now, we use |\tl_replace_all:NVn| \emph{but}, in the styles
1132 % |String.Long.Internal| we replace the spaces with
1133 % |\@@_breakable_space:| by another use of the same technic with
1134 % |\tl_replace_all:NVn|. We do the same job for the \emph{doc strings} of Python and
1135 % for the comments.
1136 % \begin{macrocode}
1137 \tl_replace_all:NVn \l_tmpa_tl
1138 \c_catcode_other_space_tl
1139 \@@_breakable_space:
1140 }
1141 }
1142 \l_tmpa_tl
1143 }
1144 \cs_generate_variant:Nn \@@_replace_spaces:n { o }
1145 % \end{macrocode}
1146 %
1147 % \bigskip
1148 % In the contents provided by Lua, each line of the Python code will be
1149 % surrounded by |\@@_begin_line:| and |\@@_end_line:|.
1150
1151 % |\@@_begin_line:| is a TeX command with a delimited argument (|\@@_end_line:|
1152 % is the marker for the end of the argument).
1153 %
1154 % However, we define also |\@@_end_line:| as no-op, because, when the last line
1155 % of the listing is the end of an environment of Beamer (eg |\end{uncoverenv}|),
1156 % we will have a token |\@@_end_line:| added at the end without any
1157 % corresponding |\@@_begin_line:|).
1158 % \begin{macrocode}
1159 \cs_set_protected:Npn \@@_end_line: { }
1160 % \end{macrocode}
1161 %
1162 % \bigskip
1163 % \begin{macrocode}
1164 \cs_set_protected:Npn \@@_begin_line: #1 \@@_end_line:
1165 {
1166 \group_begin:
1167 \int_gzero:N \g_@@_indentation_int
1168 % \end{macrocode}
1169 %
1170 % \medskip
1171 % We put the potential number of line, the potential left and right margins.
1172 % \begin{macrocode}
1173 \hbox_set:Nn \l_@@_line_box
1174 {
1175 \skip_horizontal:N \l_@@_left_margin_dim
1176 \bool_if:NT \l_@@_line_numbers_bool
1177 {
1178 % \end{macrocode}
1179 % |\l_tmpa_int| will be equal to $1$ when the current line is not empty.
1180 % \begin{macrocode}
1181 \int_set:Nn \l_tmpa_int
1182 {
1183 \lua_now:e
1184 {
1185 tex.sprint
1186 (
1187 % \end{macrocode}
1188 % The following expression gives an integer of Lua (\emph{integer} is a
1189 % sub-type of \emph{number} introduced in Lua 5.3), the output will be of the
1190 % form |"3"| (and not |"3.0"|) which is what we want for |\int_set:Nn|.
1191 % \begin{macrocode}
1192 piton.empty_lines
1193 [ \int_eval:n { \g_@@_line_int + 1 } ]
1194 )
1195 }
1196 }
1197 \bool_lazy_or:nnT
1198 { \int_compare_p:nNn \l_tmpa_int = \c_one_int }
1199 { ! \l_@@_skip_empty_lines_bool }
1200 { \int_gincr:N \g_@@_visual_line_int }
1201
1202 \bool_lazy_or:nnTF
1203 { \int_compare_p:nNn \l_tmpa_int = \c_one_int }
1204 { ! \l_@@_skip_empty_lines_bool && \l_@@_label_empty_lines_bool }
1205 {
1206 \bool_lazy_or:nnTF
1207 { \int_compare_p:nNn { \l_@@_numbers_step_int } = 1 }
1208 {
1209 \int_compare_p:nNn
1210 {
1211 \int_mod:nn
1212 { \g_@@_visual_line_int }
1213 { \l_@@_numbers_step_int }
1214 }
1215 = \c_one_int
1216 }
1217 {
1218 \str_if_eq:eeTF \l_@@_line_numbers_position_str { left }
1219 \@@_print_number_left:
1220 {
1221 \seq_gput_right:Ne \g_@@_visual_line_numbers_seq
1222 { \int_use:N \g_@@_visual_line_int }
1223 }
1224 }
1225 {
1226 \str_if_eq:eeT \l_@@_line_numbers_position_str { right }
1227 { \seq_gput_right:Nn \g_@@_visual_line_numbers_seq { } }
1228 }
1229 }
1230 {
1231 \str_if_eq:eeT \l_@@_line_numbers_position_str { right }
1232 { \seq_gput_right:Nn \g_@@_visual_line_numbers_seq { } }
1233 }
1234 }
1235 % \end{macrocode}
1236 % If there is a background, we must remind that there is a left margin of 0.5~em
1237 % for the background (which will be added later).
1238 % \begin{macrocode}
1239 \int_compare:nNnT \l_@@_bg_colors_int > { \c_zero_int }
1240 {
1241 % \end{macrocode}
1242 % ... but if only if the key |left-margin| is not used !
1243 % \begin{macrocode}
1244 \dim_compare:nNnT \l_@@_left_margin_dim = \c_zero_dim
1245 { \skip_horizontal:n { 0.5 em } }
1246 }
1247 % \end{macrocode}
1248 %
1249 % \bigskip
1250 % \begin{macrocode}
1251 \bool_if:NTF \l_@@_minimize_width_bool
1252 {
1253 \hbox_set:Nn \l_tmpa_box
1254 {
1255 \language = -1
1256 \raggedright
1257 \strut
1258 \@@_replace_spaces:n { #1 }
1259 \strut \hfil
1260 }
1261 \dim_compare:nNnTF { \box_wd:N \l_tmpa_box } < \l_@@_code_width_dim
1262 { \box_use:N \l_tmpa_box }
1263 { \@@_vtop_of_code:n { #1 } }
1264 }
1265 { \@@_vtop_of_code:n { #1 } }
1266 }
1267 % \end{macrocode}
1268 % Now, the line of code is composed in the box |\l_@@_line_box|.
1269 %
1270 % \bigskip
1271 % \begin{macrocode}
1272 \box_set_dp:Nn \l_@@_line_box { \box_dp:N \l_@@_line_box + 1.25 pt }
1273 \box_set_ht:Nn \l_@@_line_box { \box_ht:N \l_@@_line_box + 1.25 pt }
1274 % \end{macrocode}
1275 %
1276 % \begin{macrocode}
1277 \box_use_drop:N \l_@@_line_box
1278 % \end{macrocode}
1279 %
1280 % \begin{macrocode}
1281 \group_end:
1282 \g_@@_after_line_tl
1283 \tl_gclear:N \g_@@_after_line_tl
1284 }
1285 % \end{macrocode}
1286 %
1287 % \bigskip
1288 % The following command will be used in |\@@_begin_line:| … |\@@_end_line:|.
1289 % \begin{macrocode}
1290 \cs_new_protected:Npn \@@_vtop_of_code:n #1
1291 {
1292 \vbox_top:n
1293 {
1294 \hsize = \l_@@_code_width_dim
1295 \language = -1
1296 \raggedright
1297 \strut
1298 \@@_replace_spaces:n { #1 }
1299 \strut \hfil
1300 }
1301 }
1302 % \end{macrocode}
1303 %
1304 % \bigskip
1305 % The following command will be used when the key |background-color| is used or when
1306 % the key |line-numbers| is used in conjunction with |line-numbers/position=right|.
1307 %
1308 % The content of the line has been previously set in |\l_@@_line_box|.
1309 %
1310 % That command is used only once, in |\@@_add_bg_and_right_nb_to_output_box:|.
1311 % \begin{macrocode}
1312 \cs_new_protected:Npn \@@_add_bg_and_right_nb_to_line_and_use:
1313 {
1314 \vtop
1315 {
1316 \offinterlineskip
1317 \hbox
1318 {
1319 % \end{macrocode}
1320 % The command |\@@_compute_and_set_color:| sets the current color but also sets the booleans
1321 % |\g_@@_color_is_none_bool| and |\g_@@_next_color_is_none_bool|. It uses the
1322 % current value of |\l_@@_bg_color_clist|, the value of |\g_@@_line_int| (the number of the
1323 % current line) but also potential token lists of the form |\g_@@_color_12_tl| if the end
1324 % user has used the command |\rowcolor|.
1325 % \begin{macrocode}
1326 \group_begin:
1327 \@@_compute_and_set_color:
1328 % \end{macrocode}
1329 % The colored panels are overlapping. However, if the special color |none| is used
1330 % we must not put such overlapping.
1331 % \begin{macrocode}
1332 \dim_set:Nn \l_tmpa_dim { \box_dp:N \l_@@_line_box }
1333 \bool_if:NT \g_@@_next_color_is_none_bool
1334 { \dim_sub:Nn \l_tmpa_dim { 2.5 pt } }
1335 % \end{macrocode}
1336 % When |\g_@@_color_is_none_bool| is in force, we will compose a |\vrule| of
1337 % width 0~pt. We need that |\vrule| because it will be a strut.
1338 % \begin{macrocode}
1339 \bool_if:NTF \g_@@_color_is_none_bool
1340 { \dim_zero:N \l_tmpb_dim }
1341 { \dim_set_eq:NN \l_tmpb_dim \l_@@_listing_width_dim }
1342 \dim_set:Nn \l_@@_tmpc_dim { \box_ht:N \l_@@_line_box }
1343 % \end{macrocode}
1344 % Now, the colored panel.
1345 % \begin{macrocode}
1346 \dim_compare:nNnTF \l_@@_rounded_corners_dim > \c_zero_dim
1347 {
1348 \int_compare:nNnTF \g_@@_line_int = \c_one_int
1349 {
1350 \begin{tikzpicture}[baseline = 0cm]
1351 \fill (0,0)
1352 [rounded~corners = \l_@@_rounded_corners_dim]
1353 -- (0,\l_@@_tmpc_dim)
1354 -- (\l_tmpb_dim,\l_@@_tmpc_dim)
1355 [sharp~corners] -- (\l_tmpb_dim,-\l_tmpa_dim)
1356 -- (0,-\l_tmpa_dim)
1357 -- cycle ;
1358 \end{tikzpicture}
1359 }
1360 {
1361 \int_compare:nNnTF \g_@@_line_int = \g_@@_nb_lines_int
1362 {
1363 \begin{tikzpicture}[baseline = 0cm]
1364 \fill (0,0) -- (0,\l_@@_tmpc_dim)
1365 -- (\l_tmpb_dim,\l_@@_tmpc_dim)
1366 [rounded~corners = \l_@@_rounded_corners_dim]
1367 -- (\l_tmpb_dim,-\l_tmpa_dim)
1368 -- (0,-\l_tmpa_dim)
1369 -- cycle ;
1370 \end{tikzpicture}
1371 }
1372 {
1373 \vrule height \l_@@_tmpc_dim
1374 depth \l_tmpa_dim
1375 width \l_tmpb_dim
1376 % \end{macrocode}
1377 % For the case when |line-numbers/position=right| is in force with |line-numbers|.
1378 % \begin{macrocode}
1379 \dim_compare:nNnT \l_tmpb_dim = \c_zero_dim
1380 { \skip_horizontal:N \l_@@_listing_width_dim }
1381 }
1382 }
1383 }
1384 {
1385 \vrule height \l_@@_tmpc_dim
1386 depth \l_tmpa_dim
1387 width \l_tmpb_dim
1388 % \end{macrocode}
1389 % For the case when |line-numbers/position=right| is in force with |line-numbers|.
1390 % \begin{macrocode}
1391 \dim_compare:nNnT \l_tmpb_dim = \c_zero_dim
1392 { \skip_horizontal:N \l_@@_listing_width_dim }
1393 }
1394 % \end{macrocode}
1395 % The group is for the color of the background.
1396 % \begin{macrocode}
1397 \group_end:
1398 \bool_if:NT \l_@@_line_numbers_bool
1399 {
1400 \str_if_eq:eeT \l_@@_line_numbers_position_str { right }
1401 {
1402 \seq_gpop_right:NN \g_@@_visual_line_numbers_seq \l_tmpa_tl
1403 \@@_print_number_right:
1404 }
1405 }
1406 }
1407 \bool_if:NT \g_@@_next_color_is_none_bool
1408 { \skip_vertical:n { 2.5 pt } }
1409 \skip_vertical:n { - \box_ht_plus_dp:N \l_@@_line_box }
1410 \box_use_drop:N \l_@@_line_box
1411 }
1412 }
1413 % \end{macrocode}
1414 % End of |\@@_add_bg_and_right_nb_to_line_and_use:|
1415 %
1416 %
1417 % \bigskip
1418 % The command |\@@_compute_and_set_color:| sets the current color but also sets the booleans
1419 % |\g_@@_color_is_none_bool| and |\g_@@_next_color_is_none_bool|. It uses the
1420 % current value of |\l_@@_bg_color_clist|, the value of
1421 % |\g_@@_line_int| (the number of the current line) but also potential token lists
1422 % of the form |\g_@@_color_12_tl| if the end user has used the command |\rowcolor|.
1423 % \begin{macrocode}
1424 \cs_set_protected:Npn \@@_compute_and_set_color:
1425 {
1426 \int_compare:nNnTF \l_@@_bg_colors_int = \c_zero_int
1427 { \tl_set:Nn \l_tmpa_tl { none } }
1428 {
1429 \int_set:Nn \l_tmpb_int
1430 { \int_mod:nn \g_@@_line_int \l_@@_bg_colors_int + 1 }
1431 \tl_set:Ne \l_tmpa_tl { \clist_item:Nn \l_@@_bg_color_clist \l_tmpb_int }
1432 }
1433 % \end{macrocode}
1434 % The row may have a color specified by the command |\rowcolor|. We check that point now.
1435 % \begin{macrocode}
1436 \cs_if_exist:cT { g_@@_color_ \int_use:N \g_@@_line_int _ tl }
1437 {
1438 \tl_set_eq:Nc \l_tmpa_tl { g_@@_color_ \int_use:N \g_@@_line_int _ tl}
1439 % \end{macrocode}
1440 % We don't need any longer the variable and that's why we delete it (it must be free
1441 % for the next environment of \pkg{piton}).
1442 % \begin{macrocode}
1443 \cs_undefine:c { g_@@_color_ \int_use:N \g_@@_line_int _ tl}
1444 }
1445 \tl_if_eq:NnTF \l_tmpa_tl { none }
1446 { \bool_gset_true:N \g_@@_color_is_none_bool }
1447 {
1448 \bool_gset_false:N \g_@@_color_is_none_bool
1449 \@@_color:o \l_tmpa_tl
1450 }
1451 % \end{macrocode}
1452 % We are looking for the next color because we have to know whether that
1453 % color is the special color |none| (for the vertical adjustment of the background color).
1454 % \begin{macrocode}
1455 \int_compare:nNnTF { \g_@@_line_int + 1 } = \g_@@_nb_lines_int
1456 { \bool_gset_false:N \g_@@_next_color_is_none_bool }
1457 {
1458 \int_compare:nNnTF \l_@@_bg_colors_int = \c_zero_int
1459 { \tl_set:Nn \l_tmpa_tl { none } }
1460 {
1461 \int_set:Nn \l_tmpb_int
1462 { \int_mod:nn { \g_@@_line_int + 1 } \l_@@_bg_colors_int + 1 }
1463 \tl_set:Ne \l_tmpa_tl { \clist_item:Nn \l_@@_bg_color_clist \l_tmpb_int }
1464 }
1465 \cs_if_exist:cT { g_@@_color_ \int_eval:n { \g_@@_line_int + 1 } _ tl }
1466 {
1467 \tl_set_eq:Nc \l_tmpa_tl
1468 { g_@@_color_ \int_eval:n { \g_@@_line_int + 1 } _ tl }
1469 }
1470 \tl_if_eq:NnTF \l_tmpa_tl { none }
1471 { \bool_gset_true:N \g_@@_next_color_is_none_bool }
1472 { \bool_gset_false:N \g_@@_next_color_is_none_bool }
1473 }
1474 }
1475 % \end{macrocode}
1476 %
1477 %
1478 % The following command |\@@_color:n| will accept both the instruction
1479 % |\@@_color:n { red!15 }| and the instruction |\@@_color:n { [rgb]{0.9,0.9,0} }|.
1480 % \begin{macrocode}
1481 \cs_set_protected:Npn \@@_color:n #1
1482 {
1483 \tl_if_head_eq_meaning:nNTF { #1 } [
1484 {
1485 \tl_set:Nn \l_tmpa_tl { #1 }
1486 \tl_set_rescan:Nno \l_tmpa_tl { } \l_tmpa_tl
1487 \exp_last_unbraced:No \color \l_tmpa_tl
1488 }
1489 { \color { #1 } }
1490 }
1491 \cs_generate_variant:Nn \@@_color:n { o }
1492 % \end{macrocode}
1493 %
1494 % \bigskip
1495 % The command |\@@_par:| will be inserted by Lua between two lines of the
1496 % computer listing.
1497 % \begin{itemize}
1498 % \item In fact, it will be inserted between two commands
1499 % |\@@_begin_line:|...|\@@_end_of_line:|.
1500 % \item When the key |break-lines-in-Piton| is in force, a line of the
1501 % computer listing (the \emph{input}) may result in several lines in the
1502 % \textsc{pdf} (the \emph{output}).
1503 % \item Remind that |\@@_par:| has a rather complex behaviour because it will
1504 % finish and start paragraphs.
1505 % \end{itemize}
1506 % \begin{macrocode}
1507 \cs_new_protected:Npn \@@_par:
1508 {
1509 % \end{macrocode}
1510 % We recall that |\g_@@_line_int| is \emph{not} used for the number of line
1511 % printed in the \textsc{pdf} (when |line-numbers| is in force)...
1512 % \begin{macrocode}
1513 \int_gincr:N \g_@@_line_int
1514 % \end{macrocode}
1515 % ... it will be used to allow or disallow page breaks, and also by the command
1516 % |\rowcolor|.
1517 %
1518 %
1519 % Each line in the listing is composed in a box of TeX (which may contain
1520 % several lines when the key |break-lines-in-Piton| is in force) put in a
1521 % paragraph.
1522 % \begin{macrocode}
1523 \par
1524 % \end{macrocode}
1525 % We now add a |\kern| because each line of code is overlapping vertically by a
1526 % quantity of 2.5~pt in order to have a good background (when |background-color|
1527 % is in force). We need to use a |\kern| (in fact |\par\kern...|) and not a
1528 % |\vskip| because page breaks should \emph{not} be allowed on that kern.
1529 % \begin{macrocode}
1530 \kern -2.5 pt
1531 % \end{macrocode}
1532 % Now, we control page breaks after the paragraph.
1533 % \begin{macrocode}
1534 \@@_add_penalty_for_the_line:
1535 }
1536 % \end{macrocode}
1537 % After the command |\@@_par:|, we will usually have a command |\@@_begin_line:|.
1538 %
1539 % \bigskip
1540 % The following command |\@@_breakable_space:| is for breakable spaces in the
1541 % environments |{Piton}| and the listings of |\PitonInputFile| and \emph{not} for the
1542 % commands |\piton|.
1543 % \begin{macrocode}
1544 \cs_set_protected:Npn \@@_breakable_space:
1545 {
1546 \discretionary
1547 { \hbox:n { \color { gray } \l_@@_end_of_broken_line_tl } }
1548 {
1549 \hbox_overlap_left:n
1550 {
1551 {
1552 \normalfont \footnotesize \color { gray }
1553 \l_@@_continuation_symbol_tl
1554 }
1555 \skip_horizontal:n { 0.3 em }
1556 \int_compare:nNnT \l_@@_bg_colors_int > \c_zero_int
1557 { \skip_horizontal:n { 0.5 em } }
1558 }
1559 \bool_if:NT \l_@@_indent_broken_lines_bool
1560 {
1561 \hbox:n
1562 {
1563 \prg_replicate:nn { \g_@@_indentation_int } { ~ }
1564 { \color { gray } \l_@@_csoi_tl }
1565 }
1566 }
1567 }
1568 { \hbox { ~ } }
1569 }
1570 % \end{macrocode}
1571 %
1572 % \bigskip
1573 % \subsection{PitonOptions}
1574 %
1575 %
1576 %
1577 % \medskip
1578 % \begin{macrocode}
1579 \bool_new:N \l_@@_line_numbers_bool
1580 \bool_new:N \l_@@_skip_empty_lines_bool
1581 \bool_set_true:N \l_@@_skip_empty_lines_bool
1582 \bool_new:N \l_@@_line_numbers_absolute_bool
1583 \tl_new:N \l_@@_line_numbers_format_tl
1584 \tl_set:Nn \l_@@_line_numbers_format_tl { \footnotesize \color { gray } }
1585 \bool_new:N \l_@@_label_empty_lines_bool
1586 \bool_set_true:N \l_@@_label_empty_lines_bool
1587 \int_new:N \l_@@_number_lines_start_int
1588 \str_new:N \l_@@_line_numbers_position_str
1589 \str_set:Nn \l_@@_line_numbers_position_str { left }
1590 \bool_new:N \l_@@_resume_bool
1591 \bool_new:N \g_@@_label_as_zlabel_bool
1592 % \end{macrocode}
1593 %
1594 %
1595 % \bigskip
1596 % \begin{macrocode}
1597 \keys_define:nn { PitonOptions / marker }
1598 {
1599 beginning .cs_set:Np = \@@_marker_beginning:n #1 ,
1600 beginning .value_required:n = true ,
1601 end .cs_set:Np = \@@_marker_end:n #1 ,
1602 end .value_required:n = true ,
1603 include-lines .bool_set:N = \l_@@_marker_include_lines_bool ,
1604 unknown .code:n = \@@_error:n { Unknown~key~for~marker }
1605 }
1606 % \end{macrocode}
1607 %
1608 % \bigskip
1609 % \begin{macrocode}
1610 \keys_define:nn { PitonOptions / line-numbers }
1611 {
1612 true .code:n = \bool_set_true:N \l_@@_line_numbers_bool ,
1613 false .code:n = \bool_set_false:N \l_@@_line_numbers_bool ,
1614
1615 start .code:n =
1616 \bool_set_true:N \l_@@_line_numbers_bool
1617 \int_set:Nn \l_@@_number_lines_start_int { #1 } ,
1618 start .value_required:n = true ,
1619
1620 skip-empty-lines .code:n =
1621 \bool_if:NF \l_@@_in_PitonOptions_bool
1622 { \bool_set_true:N \l_@@_line_numbers_bool }
1623 \str_if_eq:nnTF { #1 } { false }
1624 { \bool_set_false:N \l_@@_skip_empty_lines_bool }
1625 { \bool_set_true:N \l_@@_skip_empty_lines_bool } ,
1626
1627 label-empty-lines .code:n =
1628 \bool_if:NF \l_@@_in_PitonOptions_bool
1629 { \bool_set_true:N \l_@@_line_numbers_bool }
1630 \str_if_eq:nnTF { #1 } { false }
1631 { \bool_set_false:N \l_@@_label_empty_lines_bool }
1632 { \bool_set_true:N \l_@@_label_empty_lines_bool } ,
1633
1634 absolute .code:n =
1635 \bool_if:NTF \l_@@_in_PitonOptions_bool
1636 { \bool_set_true:N \l_@@_line_numbers_absolute_bool }
1637 { \bool_set_true:N \l_@@_line_numbers_bool }
1638 \bool_if:NT \l_@@_in_PitonInputFile_bool
1639 {
1640 \bool_set_true:N \l_@@_line_numbers_absolute_bool
1641 \bool_set_false:N \l_@@_skip_empty_lines_bool
1642 } ,
1643 absolute .value_forbidden:n = true ,
1644
1645 resume .code:n =
1646 \bool_set_true:N \l_@@_resume_bool
1647 \bool_if:NF \l_@@_in_PitonOptions_bool
1648 { \bool_set_true:N \l_@@_line_numbers_bool } ,
1649 resume .value_forbidden:n = true ,
1650
1651 sep .dim_set:N = \l_@@_numbers_sep_dim ,
1652 sep .value_required:n = true ,
1653 sep .initial:n = 0.7 em ,
1654
1655 step .int_set:N = \l_@@_numbers_step_int ,
1656 step .initial:n = 1 ,
1657 step .value_required:n = true ,
1658
1659 position .choices:nn = { left , right }
1660 { \str_set_eq:NN \l_@@_line_numbers_position_str \l_keys_choice_tl } ,
1661 position .value_required:n = true ,
1662
1663 format .tl_set:N = \l_@@_line_numbers_format_tl ,
1664 format .value_required:n = true ,
1665
1666 lmmono10-drawn .bool_set:N = \l_@@_lmodern_drawn_bool ,
1667 lmmono10-drawn .default:n = true ,
1668
1669 unknown .code:n =
1670 \@@_unknown_key:nn
1671 { PitonOptions / line-numbers }
1672 { Unknown~key~for~line-numbers }
1673
1674 }
1675 % \end{macrocode}
1676 %
1677 %
1678 % \bigskip
1679 % Be careful! The name of the following set of keys must be considered as
1680 % public! Hence, it should \emph{not} be changed.
1681 %
1682 % \begin{macrocode}
1683 \keys_define:nn { PitonOptions }
1684 {
1685 indentations-for-Foxit .choices:nn = { true , false }
1686 {
1687 \tl_if_eq:VnTF \l_keys_value_tl { true }
1688 { \@@_define_leading_space_Foxit: }
1689 { \@@_define_leading_space_normal: }
1690 } ,
1691 box .choices:nn = { c , t , b , m }
1692 { \str_set_eq:NN \l_@@_box_str \l_keys_choice_tl } ,
1693 box .default:n = c ,
1694 break-strings-anywhere .bool_set:N = \l_@@_break_strings_anywhere_bool ,
1695 break-numbers-anywhere .bool_set:N = \l_@@_break_numbers_anywhere_bool ,
1696 % \end{macrocode}
1697 % First, we put keys that should be available only in the preamble.
1698 % \begin{macrocode}
1699 detected-commands .code:n = \@@_detected_commands:n { #1 } ,
1700 detected-commands .value_required:n = true ,
1701 detected-commands .usage:n = preamble ,
1702 vertical-detected-commands .code:n = \@@_vertical_detected_commands:n { #1 } ,
1703 vertical-detected-commands .value_required:n = true ,
1704 vertical-detected-commands .usage:n = preamble ,
1705 raw-detected-commands .code:n = \@@_raw_detected_commands:n { #1 } ,
1706 raw-detected-commands .value_required:n = true ,
1707 raw-detected-commands .usage:n = preamble ,
1708 detected-beamer-commands .code:n =
1709 \@@_error_if_not_in_beamer:
1710 \clist_put_right:Nn \l_@@_beamer_commands_clist { #1 } ,
1711 detected-beamer-commands .value_required:n = true ,
1712 detected-beamer-commands .usage:n = preamble ,
1713 detected-beamer-environments .code:n =
1714 \@@_error_if_not_in_beamer:
1715 \clist_put_right:Nn \l_@@_beamer_environments_clist { #1 } ,
1716 detected-beamer-environments .value_required:n = true ,
1717 detected-beamer-environments .usage:n = preamble ,
1718 % \end{macrocode}
1719 %
1720 %
1721 % Remark that the command |\lua_escape:n| is fully expandable. That's why we use
1722 % |\lua_now:e|.
1723 % \begin{macrocode}
1724 begin-escape .code:n =
1725 \lua_now:e { piton.begin_escape = "\lua_escape:n{#1}" } ,
1726 begin-escape .value_required:n = true ,
1727 begin-escape .usage:n = preamble ,
1728
1729 end-escape .code:n =
1730 \lua_now:e { piton.end_escape = "\lua_escape:n{#1}" } ,
1731 end-escape .value_required:n = true ,
1732 end-escape .usage:n = preamble ,
1733
1734 begin-escape-math .code:n =
1735 \lua_now:e { piton.begin_escape_math = "\lua_escape:n{#1}" } ,
1736 begin-escape-math .value_required:n = true ,
1737 begin-escape-math .usage:n = preamble ,
1738
1739 end-escape-math .code:n =
1740 \lua_now:e { piton.end_escape_math = "\lua_escape:n{#1}" } ,
1741 end-escape-math .value_required:n = true ,
1742 end-escape-math .usage:n = preamble ,
1743
1744 comment-latex .code:n = \lua_now:n { comment_latex = "#1" } ,
1745 comment-latex .value_required:n = true ,
1746 comment-latex .usage:n = preamble ,
1747
1748 label-as-zlabel .bool_gset:N = \g_@@_label_as_zlabel_bool ,
1749 label-as-zlabel .usage:n = preamble ,
1750
1751 math-comments .bool_gset:N = \g_@@_math_comments_bool ,
1752 math-comments .usage:n = preamble ,
1753 % \end{macrocode}
1754 %
1755 % \bigskip
1756 % Now, general keys.
1757 % \begin{macrocode}
1758 language .code:n =
1759 \str_set:Ne \l_piton_language_str { \str_lowercase:n { #1 } } ,
1760 language .value_required:n = true ,
1761 path .code:n =
1762 \seq_clear:N \l_@@_path_seq
1763 \clist_map_inline:nn { #1 }
1764 {
1765 \str_set:Nn \l_tmpa_str { ##1 }
1766 \seq_put_right:No \l_@@_path_seq { \l_tmpa_str }
1767 } ,
1768 path .value_required:n = true ,
1769 % \end{macrocode}
1770 % The initial value of the key |path| is not empty: it's |.|, that is to say a
1771 % comma separated list with only one component which is |.|, the current directory.
1772 % \begin{macrocode}
1773 path .initial:n = . ,
1774 path-write .str_set:N = \l_@@_path_write_str ,
1775 path-write .value_required:n = true ,
1776 font-command .tl_set:N = \l_@@_font_command_tl ,
1777 font-command .initial:n = \ttfamily ,
1778 font-command .value_required:n = true ,
1779 font-command+ .code:n
1780 = { \tl_put_right:Nn \l_@@_font_command_tl { #1 } } ,
1781 font-command+ .value_required:n = true ,
1782 font-command~+ .meta:n = { font-command+ = #1 } ,
1783 font-command~+ .value_required:n = true ,
1784 gobble .int_set:N = \l_@@_gobble_int ,
1785 gobble .default:n = -1 ,
1786 auto-gobble .code:n = \int_set:Nn \l_@@_gobble_int { -1 } ,
1787 auto-gobble .value_forbidden:n = true ,
1788 env-gobble .code:n = \int_set:Nn \l_@@_gobble_int { -2 } ,
1789 env-gobble .value_forbidden:n = true ,
1790 tabs-auto-gobble .code:n = \int_set:Nn \l_@@_gobble_int { -3 } ,
1791 tabs-auto-gobble .value_forbidden:n = true ,
1792
1793 splittable-on-empty-lines .bool_set:N = \l_@@_splittable_on_empty_lines_bool ,
1794
1795 split-on-empty-lines .bool_set:N = \l_@@_split_on_empty_lines_bool ,
1796 % \end{macrocode}
1797 % When the key |split-on-empty-lines| is in force, the correspondint token
1798 % list will be inserted between the chunks of code (the computer listing provided
1799 % by the end user is split in chunks on the empty lines in the code).
1800 % % That parameter must contain elements to be inserted in \emph{vertical} mode by
1801 % TeX.
1802 % \begin{macrocode}
1803 split-separation .tl_set:N = \l_@@_split_separation_tl ,
1804 split-separation .value_required:n = true ,
1805 split-separation .initial:n = \vspace { \baselineskip } \vspace { -1.25pt } ,
1806
1807 split-separation+ .code:n =
1808 \tl_put_right:Nn \l_@@_split_separation_tl { #1 } ,
1809 split-separation+ .value_required:n = true ,
1810 split-separation~+ .meta:n = { split-separation+ = #1 } ,
1811 add-to-split-separation .meta:n = { split-separation+ = #1 } ,
1812
1813 marker .code:n =
1814 \bool_lazy_or:nnTF
1815 \l_@@_in_PitonInputFile_bool
1816 \l_@@_in_PitonOptions_bool
1817 { \keys_set:nn { PitonOptions / marker } { #1 } }
1818 { \@@_error:n { Invalid~key } } ,
1819 marker .value_required:n = true ,
1820
1821 line-numbers .code:n =
1822 \keys_set:nn { PitonOptions / line-numbers } { #1 } ,
1823 % \end{macrocode}
1824 % The following line is mandatory.
1825 % \begin{macrocode}
1826 line-numbers .default:n = true ,
1827 % \end{macrocode}
1828 %
1829 % \medskip
1830 % The following counter corresponds to the key |splittable| of |\PitonOptions|.
1831 % If the value of |\l_@@_splittable_int| is equal to $n$, then no line break can
1832 % occur within the first $n$~lines or the last $n$~lines of a listing (or a
1833 % \emph{chunk} of listings when the key |split-on-empty-lines| is in force).
1834 % \begin{macrocode}
1835 splittable .int_set:N = \l_@@_splittable_int ,
1836 splittable .default:n = 1 ,
1837 splittable .initial:n = 100 ,
1838 background-color .code:n =
1839 \clist_set:Nn \l_@@_bg_color_clist { #1 }
1840 % \end{macrocode}
1841 % We keep the lenght of the clist |\l_@@_bg_color_clist| in a counter for efficiency only.
1842 % \begin{macrocode}
1843 \int_set:Nn \l_@@_bg_colors_int { \clist_count:N \l_@@_bg_color_clist } ,
1844 background-color .value_required:n = true ,
1845 % \end{macrocode}
1846 % The package \pkg{piton} will also detect the lines of code which correspond to
1847 % the user input in a Python console, that is to say the lines of code beginning
1848 % with |>>>| and |...|. It's possible, with the key |prompt-background-color|,
1849 % to require a background for these lines of code (and the other lines of code
1850 % will have the standard background color specified by |background-color|).
1851 % \begin{macrocode}
1852 prompt-background-color .tl_set:N = \l_@@_prompt_bg_color_tl ,
1853 prompt-background-color .value_required:n = true ,
1854 prompt-background-color .initial:n = gray!15 ,
1855 % \end{macrocode}
1856 % With the tuning |write=false|, the content of the environment won't be parsed
1857 % and won't be printed on the \textsc{pdf}. However, the Lua variables |piton.last_code|
1858 % and |piton.last_language| will be set (and, hence, |piton.get_last_code| will be
1859 % operational). The keys |join| and |write| will be honoured.
1860 % \begin{macrocode}
1861 print .bool_set:N = \l_@@_print_bool ,
1862 print .value_required:n = true ,
1863 print .initial:n = true ,
1864
1865 width .code:n =
1866 \str_if_eq:nnTF { #1 } { min }
1867 {
1868 \bool_set_true:N \l_@@_minimize_width_bool
1869 \dim_zero:N \l_@@_width_dim
1870 }
1871 {
1872 \bool_set_false:N \l_@@_minimize_width_bool
1873 \dim_set:Nn \l_@@_width_dim { #1 }
1874 } ,
1875 width .value_required:n = true ,
1876
1877 max-width .code:n =
1878 \bool_set_true:N \l_@@_minimize_width_bool
1879 \dim_set:Nn \l_@@_width_dim { #1 } ,
1880 max-width .value_required:n = true ,
1881
1882 paperclip .code:n =
1883 \bool_set_true:N \l_@@_paperclip_bool
1884 \tl_if_novalue:nTF { #1 }
1885 { \str_set:Nn \l_@@_paperclip_str { } }
1886 { \str_set:Nn \l_@@_paperclip_str { #1 } } ,
1887
1888 annotation .bool_set:N = \l_@@_annotation_bool ,
1889
1890 write .str_set:N = \l_@@_write_str ,
1891 write .value_required:n = true ,
1892 no-write .code:n = \str_set_eq:NN \l_@@_write_str \c_empty_str ,
1893 no-write .value_forbidden:n = true ,
1894 join .code:n =
1895 \str_set:Nn \l_@@_join_str { #1 }
1896 \seq_if_in:NnF \g_@@_join_seq { #1 }
1897 { \seq_gput_right:No \g_@@_join_seq { #1 } } ,
1898 join .value_required:n = true ,
1899 join-separation .str_set:N = \l_@@_join_separation_str ,
1900 join-separation .value_required:n = true ,
1901 no-join .code:n = \str_set_eq:NN \l_@@_join_str \c_empty_str ,
1902 no-join .value_forbidden:n = true ,
1903
1904 left-margin .code:n =
1905 \str_if_eq:nnTF { #1 } { auto }
1906 {
1907 \dim_zero:N \l_@@_left_margin_dim
1908 \bool_set_true:N \l_@@_left_margin_auto_bool
1909 }
1910 {
1911 \dim_set:Nn \l_@@_left_margin_dim { #1 }
1912 \bool_set_false:N \l_@@_left_margin_auto_bool
1913 } ,
1914 left-margin .value_required:n = true ,
1915
1916 right-margin .code:n =
1917 \str_if_eq:nnTF { #1 } { auto }
1918 {
1919 \dim_zero:N \l_@@_right_margin_dim
1920 \bool_set_true:N \l_@@_right_margin_auto_bool
1921 }
1922 {
1923 \dim_set:Nn \l_@@_right_margin_dim { #1 }
1924 \bool_set_false:N \l_@@_right_margin_auto_bool
1925 } ,
1926 right-margin .value_required:n = true ,
1927
1928 tab-size .int_set:N = \l_@@_tab_size_int ,
1929 tab-size .value_required:n = true ,
1930 tab-size .initial:n = 4 ,
1931
1932 show-spaces .bool_set:N = \l_@@_show_spaces_bool ,
1933 show-spaces .value_forbidden:n = true ,
1934
1935 show-spaces-in-strings .code:n =
1936 \tl_set:Nn \l_@@_space_in_string_tl { ␣ } , % U+2423
1937 show-spaces-in-strings .value_forbidden:n = true ,
1938
1939 break-lines-in-Piton .bool_set:N = \l_@@_break_lines_in_Piton_bool ,
1940 break-lines-in-Piton .initial:n = true ,
1941
1942 break-lines-in-piton .bool_set:N = \l_@@_break_lines_in_piton_bool ,
1943
1944 break-lines .meta:n = { break-lines-in-piton , break-lines-in-Piton } ,
1945 break-lines .value_forbidden:n = true ,
1946
1947 indent-broken-lines .bool_set:N = \l_@@_indent_broken_lines_bool ,
1948
1949 end-of-broken-line .tl_set:N = \l_@@_end_of_broken_line_tl ,
1950 end-of-broken-line .value_required:n = true ,
1951 end-of-broken-line .initial:n = \hspace* { 0.5em } \textbackslash ,
1952
1953 continuation-symbol .tl_set:N = \l_@@_continuation_symbol_tl ,
1954 continuation-symbol .value_required:n = true ,
1955 continuation-symbol .initial:n = + ,
1956
1957 continuation-symbol-on-indentation .tl_set:N = \l_@@_csoi_tl ,
1958 continuation-symbol-on-indentation .value_required:n = true ,
1959 continuation-symbol-on-indentation .initial:n = $\hookrightarrow \;$ ,
1960
1961 first-line .code:n = \@@_in_PitonInputFile:n
1962 { \int_set:Nn \l_@@_first_line_int { #1 } } ,
1963 first-line .value_required:n = true ,
1964
1965 last-line .code:n = \@@_in_PitonInputFile:n
1966 { \int_set:Nn \l_@@_last_line_int { #1 } } ,
1967 last-line .value_required:n = true ,
1968
1969 begin-range .code:n = \@@_in_PitonInputFile:n
1970 { \str_set:Nn \l_@@_begin_range_str { #1 } } ,
1971 begin-range .value_required:n = true ,
1972
1973 end-range .code:n = \@@_in_PitonInputFile:n
1974 { \str_set:Nn \l_@@_end_range_str { #1 } } ,
1975 end-range .value_required:n = true ,
1976
1977 range .code:n = \@@_in_PitonInputFile:n
1978 {
1979 \str_set:Nn \l_@@_begin_range_str { #1 }
1980 \str_set:Nn \l_@@_end_range_str { #1 }
1981 } ,
1982 range .value_required:n = true ,
1983
1984 env-used-by-split .code:n =
1985 \lua_now:n { piton.env_used_by_split = '#1' } ,
1986 env-used-by-split .initial:n = Piton ,
1987
1988 resume .meta:n = line-numbers/resume ,
1989
1990 unknown .code:n =
1991 \@@_unknown_key:nn
1992 { PitonOptions }
1993 { Unknown~key~for~PitonOptions } ,
1994
1995 % deprecated
1996 all-line-numbers .code:n =
1997 \bool_set_true:N \l_@@_line_numbers_bool
1998 \bool_set_false:N \l_@@_skip_empty_lines_bool ,
1999 % \end{macrocode}
2000 %
2001 % \begin{macrocode}
2002 rounded-corners .code:n =
2003 \AtBeginDocument
2004 {
2005 \IfPackageLoadedTF { tikz }
2006 { \dim_set:Nn \l_@@_rounded_corners_dim { #1 } }
2007 { \@@_err_rounded_corners_without_Tikz: }
2008 } ,
2009 rounded-corners .default:n = 4 pt
2010 }
2011 % \end{macrocode}
2012 %
2013 % \begin{macrocode}
2014 \hook_gput_code:nnn { begindocument } { . }
2015 {
2016 \IfPackageLoadedTF { tcolorbox }
2017 {
2018 \pgfkeysifdefined { / tcb / libload / breakable }
2019 {
2020 \keys_define:nn { PitonOptions }
2021 {
2022 tcolorbox .bool_set:N = \l_@@_tcolorbox_bool ,
2023 }
2024 }
2025 {
2026 \keys_define:nn { PitonOptions }
2027 { tcolorbox .code:n = \@@_error:n { library~breakable~not~loaded } }
2028 }
2029 }
2030 {
2031 \keys_define:nn { PitonOptions }
2032 { tcolorbox .code:n = \@@_error:n { tcolorbox~not~loaded } }
2033 }
2034 }
2035 % \end{macrocode}
2036 %
2037 % \bigskip
2038 % \begin{macrocode}
2039 \cs_new_protected:Npn \@@_err_rounded_corners_without_Tikz:
2040 {
2041 \@@_error:n { rounded-corners~without~Tikz }
2042 \cs_gset:Npn \@@_err_rounded_corners_without_Tikz: { }
2043 }
2044 % \end{macrocode}
2045 %
2046 % \bigskip
2047 % \begin{macrocode}
2048 \cs_new_protected:Npn \@@_in_PitonInputFile:n #1
2049 {
2050 \bool_if:NTF \l_@@_in_PitonInputFile_bool
2051 { #1 }
2052 { \@@_error:n { Invalid~key } }
2053 }
2054 % \end{macrocode}
2055 %
2056 %
2057 %
2058 % \bigskip
2059 % \begin{macrocode}
2060 \NewDocumentCommand \PitonOptions { m }
2061 {
2062 \bool_set_true:N \l_@@_in_PitonOptions_bool
2063 \keys_set:nn { PitonOptions } { #1 }
2064 \bool_set_false:N \l_@@_in_PitonOptions_bool
2065 }
2066 % \end{macrocode}
2067 %
2068 %
2069 %
2070 % \bigskip
2071 % When using |\NewPitonEnvironment| a user may use |\PitonOptions| inside.
2072 % However, the set of keys available should be different that in standard
2073 % |\PitonOptions|. That's why we define a version of |\PitonOptions| with no
2074 % restriction on the set of available keys and we will link that version to
2075 % |\PitonOptions| in such environment.
2076 % \begin{macrocode}
2077 \NewDocumentCommand \@@_fake_PitonOptions { }
2078 { \keys_set:nn { PitonOptions } }
2079 % \end{macrocode}
2080 %
2081 %
2082 %
2083 % \bigskip
2084 % \subsection{The numbers of the lines}
2085 %
2086 % \medskip
2087 % The following counter will be used to count the lines in the code when the
2088 % user requires the numbers of the lines to be printed (with |line-numbers|)
2089 % whereas the counter |\g_@@_line_int| previously defined is \emph{not} used for
2090 % that functionality.
2091 %
2092 % \begin{macrocode}
2093 \int_new:N \g_@@_visual_line_int
2094 % \end{macrocode}
2095 %
2096 % \begin{macrocode}
2097 \cs_new_protected:Npn \@@_incr_visual_line:
2098 { \bool_if:NF \l_@@_skip_empty_lines_bool { \int_gincr:N \g_@@_visual_line_int } }
2099 % \end{macrocode}
2100 %
2101 % \bigskip
2102 % The following command will be used when the numbers of lines are printed
2103 % on the left (|line-numbers/position=left|). The number of line is in the counter
2104 % |\g_@@_visual_line_int|.
2105 % \begin{macrocode}
2106 \cs_new_protected:Npn \@@_print_number_left:
2107 {
2108 \hbox_overlap_left:n
2109 {
2110 \@@_actually_print_number:n { \int_to_arabic:n { \g_@@_visual_line_int } }
2111 \skip_horizontal:N \l_@@_numbers_sep_dim
2112 }
2113 }
2114 % \end{macrocode}
2115 %
2116 % \bigskip
2117 % The following command will be used when the numbers of lines are printed
2118 % on the right (|line-numbers/position=right|). The number of line is in |\l_tmpa_tl|.
2119 % \begin{macrocode}
2120 \cs_new_protected:Npn \@@_print_number_right:
2121 {
2122 \hbox_overlap_left:n
2123 {
2124 \@@_actually_print_number:n { \l_tmpa_tl }
2125 \int_compare:nNnT \l_@@_bg_colors_int > 0
2126 { \skip_horizontal:n { 0.1 em } }
2127 }
2128 }
2129 % \end{macrocode}
2130 %
2131 % |\@@_actually_print_number:| itself prints the number without the
2132 % |\hbox_overlap_left:n|. It is used by both |\@@_print_number_left:| and
2133 % |\@@_print_number_right:|
2134 % \begin{macrocode}
2135 \cs_new_protected:Npn \@@_actually_print_number:n #1
2136 {
2137 \group_begin:
2138 \bool_if:NTF \l_@@_lmodern_drawn_bool
2139 % \end{macrocode}
2140 % We put braces after |\l_@@_line_numbers_format_tl|.
2141 % Thus, the user may use the key |line-numbers/format| with a value such as |\fbox|.
2142 % \begin{macrocode}
2143 { \l_@@_line_numbers_format_tl { \@@_draw_number:e { #1 } } }
2144 {
2145 % \end{macrocode}
2146 % |\space| is mandatory in the ``PostScript string'' |(\space)| here.
2147 % \begin{macrocode}
2148 \pdfextension literal { /Artifact << /ActualText (\space) >> BDC }
2149 \l_@@_line_numbers_format_tl { #1 }
2150 \pdfextension literal { EMC }
2151 }
2152 \group_end:
2153 }
2154 % \end{macrocode}
2155 %
2156 %
2157 %
2158 %
2159 % \bigskip
2160 % \subsection{The main commands and environments for the end user}
2161 %
2162 % \bigskip
2163 % \begin{macrocode}
2164 \NewDocumentCommand { \NewPitonLanguage } { O { } m ! o }
2165 {
2166 \tl_if_novalue:nTF { #3 }
2167 % \end{macrocode}
2168 % The last argument is provided by curryfication.
2169 % \begin{macrocode}
2170 { \@@_NewPitonLanguage:nnn { #1 } { #2 } }
2171 % \end{macrocode}
2172 % The two last arguments are provided by curryfication.
2173 % \begin{macrocode}
2174 { \@@_NewPitonLanguage:nnnnn { #1 } { #2 } { #3 } }
2175 }
2176 % \end{macrocode}
2177 %
2178 % \bigskip
2179 % The following property list will contain the definitions of the computer
2180 % languages as provided by the end user. However, if a language is defined
2181 % over another base language, the corresponding list will contain the \emph{whole}
2182 % definition of the language.
2183 % \begin{macrocode}
2184 \prop_new:N \g_@@_languages_prop
2185 % \end{macrocode}
2186 %
2187 % \bigskip
2188 % \begin{macrocode}
2189 \keys_define:nn { NewPitonLanguage }
2190 {
2191 morekeywords .code:n = ,
2192 otherkeywords .code:n = ,
2193 sensitive .code:n = ,
2194 keywordsprefix .code:n = ,
2195 moretexcs .code:n = ,
2196 morestring .code:n = ,
2197 morecomment .code:n = ,
2198 moredelim .code:n = ,
2199 moredirectives .code:n = ,
2200 tag .code:n = ,
2201 alsodigit .code:n = ,
2202 alsoletter .code:n = ,
2203 alsoother .code:n = ,
2204 unknown .code:n = \@@_error:n { Unknown~key~NewPitonLanguage }
2205 }
2206 % \end{macrocode}
2207 %
2208 % \bigskip
2209 % The function |\@@_NewPitonLanguage:nnn| will be used when the language is
2210 % \emph{not} defined above a base language (and a base dialect).
2211 % \begin{macrocode}
2212 \cs_new_protected:Npn \@@_NewPitonLanguage:nnn #1 #2 #3
2213 {
2214 % \end{macrocode}
2215 % We store in |\l_tmpa_tl| the name of the language with the potential dialect,
2216 % that is to say, for example : |[AspectJ]{Java}|. We use |\tl_if_blank:nF|
2217 % because the end user may have written |\NewPitonLanguage[ ]{Java}{...}|.
2218 % \begin{macrocode}
2219 \tl_set:Ne \l_tmpa_tl
2220 {
2221 \tl_if_blank:nF { #1 } { [ \str_lowercase:n { #1 } ] }
2222 \str_lowercase:n { #2 }
2223 }
2224 % \end{macrocode}
2225 %
2226 % The following set of keys is only used to raise an error when a key in unknown!
2227 % \begin{macrocode}
2228 \keys_set:nn { NewPitonLanguage } { #3 }
2229 % \end{macrocode}
2230 %
2231 % We store in LaTeX the definition of the language because some languages may be
2232 % defined with that language as base language.
2233 % \begin{macrocode}
2234 \prop_gput:Non \g_@@_languages_prop \l_tmpa_tl { #3 }
2235 % \end{macrocode}
2236 % The Lua part of the package \pkg{piton} will be loaded in a
2237 % |\AtBeginDocument|. Hence, we will put also in a |\AtBeginDocument| the
2238 % use of the Lua function |piton.new_language| (which does the main job).
2239 % \begin{macrocode}
2240 \@@_NewPitonLanguage:on \l_tmpa_tl { #3 }
2241 }
2242 % \end{macrocode}
2243 %
2244 % \begin{macrocode}
2245 \cs_new_protected:Npn \@@_NewPitonLanguage:nn #1 #2
2246 {
2247 \hook_gput_code:nnn { begindocument } { . }
2248 { \lua_now:e { piton.new_language("#1","\lua_escape:n{#2}") } }
2249 }
2250 \cs_generate_variant:Nn \@@_NewPitonLanguage:nn { o }
2251 % \end{macrocode}
2252 %
2253 % \bigskip
2254 % Now the case when the language is defined upon a base language.
2255 % \begin{macrocode}
2256 \cs_new_protected:Npn \@@_NewPitonLanguage:nnnnn #1 #2 #3 #4 #5
2257 {
2258 % \end{macrocode}
2259 % We store in |\l_tmpa_tl| the name of the base language with the dialect, that
2260 % is to say, for example : |[AspectJ]{Java}|. We use |\tl_if_blank:nF| because
2261 % the end user may have used |\NewPitonLanguage[Handel]{C}[ ]{C}{...}|
2262 % \begin{macrocode}
2263 \tl_set:Ne \l_tmpa_tl
2264 {
2265 \tl_if_blank:nF { #3 } { [ \str_lowercase:n { #3 } ] }
2266 \str_lowercase:n { #4 }
2267 }
2268 % \end{macrocode}
2269 % We retrieve in |\l_tmpb_tl| the definition (as provided by the end user) of
2270 % that base language. Caution: |\g_@@_languages_prop| does not contain all the
2271 % languages provided by \pkg{piton} but only those defined by using
2272 % |\NewPitonLanguage|.
2273 % \begin{macrocode}
2274 \prop_get:NoNTF \g_@@_languages_prop \l_tmpa_tl \l_tmpb_tl
2275 % \end{macrocode}
2276 % We can now define the new language by using the previous function.
2277 % \begin{macrocode}
2278 { \@@_NewPitonLanguage:nnno { #1 } { #2 } { #5 } \l_tmpb_tl }
2279 { \@@_error:n { Language~not~defined } }
2280 }
2281 % \end{macrocode}
2282 %
2283 % \bigskip
2284 % \begin{macrocode}
2285 \cs_new_protected:Npn \@@_NewPitonLanguage:nnnn #1 #2 #3 #4
2286 % \end{macrocode}
2287 % In the following line, we write |#4,#3| and not |#3,#4| because we want that the
2288 % keys which correspond to base language appear before the keys which are added
2289 % in the language we define.
2290 % \begin{macrocode}
2291 { \@@_NewPitonLanguage:nnn { #1 } { #2 } { #4 , #3 } }
2292 \cs_generate_variant:Nn \@@_NewPitonLanguage:nnnn { n n n o }
2293 % \end{macrocode}
2294 %
2295 % \bigskip
2296 % \begin{macrocode}
2297 \NewDocumentCommand { \piton } { }
2298 { \peek_meaning:NTF \bgroup { \@@_piton_standard } { \@@_piton_verbatim } }
2299 % \end{macrocode}
2300 %
2301 % \begin{macrocode}
2302 \NewDocumentCommand { \@@_piton_standard } { m }
2303 {
2304 \group_begin:
2305 \tl_if_eq:NnF \l_@@_space_in_string_tl { ␣ }
2306 {
2307 % \end{macrocode}
2308 % Remind that, when |break-strings-anywhere| is in force, multiple commands |\-|
2309 % will be inserted between the characters of the string to allow the breaks. The
2310 % |\exp_not:N| before |\space| is mandatory.
2311 % \begin{macrocode}
2312 \bool_lazy_or:nnT
2313 \l_@@_break_lines_in_piton_bool
2314 \l_@@_break_strings_anywhere_bool
2315 { \tl_set:Nn \l_@@_space_in_string_tl { \exp_not:N \space } }
2316 }
2317 % \end{macrocode}
2318 %
2319 % \bigskip
2320 % The following tuning of LuaTeX in order to avoid all breaks of lines on the
2321 % hyphens.
2322 % \begin{macrocode}
2323 \automatichyphenmode = 1
2324 % \end{macrocode}
2325 % Remark that the argument of |\piton| (with the normal syntax) is expanded in
2326 % the TeX sens, (see the |\tl_set:Ne| below) and that's why we can provide the
2327 % following escapes to the end user:
2328 % \begin{macrocode}
2329 \cs_set_eq:NN \\ \c_backslash_str
2330 \cs_set_eq:NN \% \c_percent_str
2331 \cs_set_eq:NN \{ \c_left_brace_str
2332 \cs_set_eq:NN \} \c_right_brace_str
2333 \cs_set_eq:NN \$ \c_dollar_str
2334 % \end{macrocode}
2335 % The standard command |\␣| is \emph{not} expandable and we need here expandable
2336 % commands. With the following code, we define an expandable command.
2337 % \begin{macrocode}
2338 \cs_set_eq:cN { ~ } \space
2339 % \end{macrocode}
2340 %
2341 % \begin{macrocode}
2342 \cs_set_eq:NN \@@_begin_line: \prg_do_nothing:
2343 % \end{macrocode}
2344 %
2345 % \bigskip
2346 % We redefine |\rowcolor| inside of |\piton| commands to do nothing.
2347 % \begin{macrocode}
2348 \cs_set_eq:NN \rowcolor \@@_noop_rowcolor
2349 % \end{macrocode}
2350 %
2351 % \begin{macrocode}
2352 \tl_set:Ne \l_tmpa_tl
2353 {
2354 \lua_now:e
2355 { piton.ParseBis('\l_piton_language_str',token.scan_string()) }
2356 { #1 }
2357 }
2358 \bool_if:NTF \l_@@_show_spaces_bool
2359 { \tl_replace_all:NVn \l_tmpa_tl \c_catcode_other_space_tl { ␣ } } % U+2423
2360 {
2361 \bool_if:NT \l_@@_break_lines_in_piton_bool
2362 % \end{macrocode}
2363 % With the following line, the spaces of catacode 12 (which were not breakable)
2364 % are replaced by |\space|, and, thus, become breakable.
2365 % \begin{macrocode}
2366 { \tl_replace_all:NVn \l_tmpa_tl \c_catcode_other_space_tl \space }
2367 }
2368 % \end{macrocode}
2369 % The command |\text| is provided by the package \pkg{amstext} (loaded by
2370 % \pkg{piton}).
2371 % \begin{macrocode}
2372 \if_mode_math:
2373 \text { \l_@@_font_command_tl \l_tmpa_tl }
2374 \else:
2375 \l_@@_font_command_tl \l_tmpa_tl
2376 \fi:
2377 \group_end:
2378 }
2379 % \end{macrocode}
2380 %
2381 %
2382 % \bigskip
2383 % \begin{macrocode}
2384 \NewDocumentCommand { \@@_piton_verbatim } { v }
2385 {
2386 \group_begin:
2387 \automatichyphenmode = 1
2388 \cs_set_eq:NN \@@_begin_line: \prg_do_nothing:
2389 % \end{macrocode}
2390 %
2391 % \bigskip
2392 % We redefine |\rowcolor| inside of |\piton| commands to do nothing.
2393 % \begin{macrocode}
2394 \cs_set_eq:NN \rowcolor \@@_noop_rowcolor
2395 % \end{macrocode}
2396 %
2397 % \begin{macrocode}
2398 \tl_set:Ne \l_tmpa_tl
2399 {
2400 \lua_now:e
2401 { piton.Parse('\l_piton_language_str',token.scan_string()) }
2402 { #1 }
2403 }
2404 \bool_if:NT \l_@@_show_spaces_bool
2405 { \tl_replace_all:NVn \l_tmpa_tl \c_catcode_other_space_tl { ␣ } } % U+2423
2406 \if_mode_math:
2407 \text { \l_@@_font_command_tl \l_tmpa_tl }
2408 \else:
2409 \l_@@_font_command_tl \l_tmpa_tl
2410 \fi:
2411 \group_end:
2412 }
2413 % \end{macrocode}
2414 %
2415
2416 % \bigskip
2417 %
2418 %
2419 % \bigskip
2420 % The following command does \emph{not} correspond to a user command. It will
2421 % be used when we will have to ``rescan'' some chunks of computer code. For
2422 % example, it will be the initial value of the Piton style |InitialValues| (the
2423 % default values of the arguments of a Python function).
2424 % \begin{macrocode}
2425 \cs_new_protected:Npn \@@_piton:n #1
2426 { \tl_if_blank:nF { #1 } { \@@_piton_i:n { #1 } } }
2427
2428 \cs_new_protected:Npn \@@_piton_i:n #1
2429 {
2430 \group_begin:
2431 \cs_set_eq:NN \@@_begin_line: \prg_do_nothing:
2432 \cs_set:cpn { pitonStyle _ \l_piton_language_str _ Prompt } { }
2433 \cs_set:cpn { pitonStyle _ Prompt } { }
2434 \cs_set_eq:NN \@@_leading_space: \space
2435 \cs_set_eq:NN \@@_trailing_space: \space
2436 \tl_set:Ne \l_tmpa_tl
2437 {
2438 \lua_now:e
2439 { piton.ParseTer('\l_piton_language_str',token.scan_string()) }
2440 { #1 }
2441 }
2442 \bool_if:NT \l_@@_show_spaces_bool
2443 { \tl_replace_all:NVn \l_tmpa_tl \c_catcode_other_space_tl { ␣ } } % U+2423
2444 \@@_replace_spaces:o \l_tmpa_tl
2445 \group_end:
2446 }
2447 % \end{macrocode}
2448 %
2449 %
2450 % \bigskip
2451 % |\@@_pre_composition:| will be used both in |\PitonInputFile| and
2452 % in the environments such as |{Piton}|.
2453 % \begin{macrocode}
2454 \cs_new_protected:Npn \@@_pre_composition:
2455 {
2456 \dim_compare:nNnT \l_@@_width_dim = \c_zero_dim
2457 {
2458 \dim_set_eq:NN \l_@@_width_dim \linewidth
2459 % \end{macrocode}
2460 % When the key |box| is used, |width=min| is activated (except when
2461 % |width| has been used with a numerical value).
2462 % \begin{macrocode}
2463 \str_if_empty:NF \l_@@_box_str
2464 { \bool_set_true:N \l_@@_minimize_width_bool }
2465 }
2466 % \end{macrocode}
2467 % We compute |\l_@@_listing_width_dim|. However, if |max-width| is used (or
2468 % |width=min| which uses |max-width|), that length will be computed
2469 % again in |\@@_create_output_box:| but \textbf{even
2470 % in the case}, we have to compute that value now (because the maximal width set
2471 % by |max-width| may be reached by some lines of the listing---and those lines
2472 % would be wrapped).
2473 % \begin{macrocode}
2474 \dim_set:Nn \l_@@_listing_width_dim
2475 {
2476 \bool_if:NTF \l_@@_tcolorbox_bool
2477 {
2478 \l_@@_width_dim -
2479 ( \kvtcb@left@rule
2480 + \kvtcb@leftupper
2481 + \kvtcb@boxsep * 2
2482 + \kvtcb@rightupper
2483 + \kvtcb@right@rule )
2484 }
2485 { \l_@@_width_dim }
2486 }
2487 % \end{macrocode}
2488 % \begin{macrocode}
2489 \legacy_if:nT { @inlabel } { \bool_set_true:N \l_@@_in_label_bool }
2490 \automatichyphenmode = 1
2491 \bool_if:NF \l_@@_resume_bool { \int_gzero:N \g_@@_visual_line_int }
2492 \g_@@_def_vertical_commands_tl
2493 \int_gzero:N \g_@@_line_int
2494 \int_gzero:N \g_@@_nb_lines_int
2495 \dim_zero:N \parindent
2496 \dim_zero:N \lineskip
2497 \dim_zero:N \parskip
2498
2499 \seq_gclear:N \g_@@_visual_line_numbers_seq
2500
2501 \cs_set_eq:NN \rowcolor \@@_rowcolor:n
2502 % \end{macrocode}
2503 %
2504 % For efficiency, we keep in |\l_@@_bg_colors_int| the length of |\l_@@_bg_color_clist|.
2505 % \begin{macrocode}
2506 \int_compare:nNnT \l_@@_bg_colors_int > { \c_zero_int }
2507 { \bool_set_true:N \l_@@_bg_bool }
2508 \bool_gset_false:N \g_@@_rowcolor_inside_bool
2509 \IfPackageLoadedTF { zref-base }
2510 {
2511 \bool_if:NTF \g_@@_label_as_zlabel_bool
2512 { \cs_set_eq:NN \label \@@_zlabel:n }
2513 { \cs_set_eq:NN \label \@@_label:n }
2514 \cs_set_eq:NN \zlabel \@@_zlabel:n
2515 }
2516 { \cs_set_eq:NN \label \@@_label:n }
2517 \l_@@_font_command_tl
2518 }
2519 % \end{macrocode}
2520 %
2521 %
2522 % \bigskip
2523 % When the parameters |line-numbers|, |line-numbers/position=left| and
2524 % |left-margin| are in force (or if |line-numbers|, |line-numbers=right| and
2525 % |right-margin| are in force), we have to compute the width of the maximal
2526 % number of lines at the end of the environment to fix the correct value to
2527 % |left-margin| (or |right-margin|).
2528 %
2529 % The command |\@@_compute_margin:N| will do that job.
2530
2531 % It's argument must be either |\l_@@_left_margin_dim| either |\l_@@_right_margin_dim|.
2532 % \begin{macrocode}
2533 \cs_new_protected:Npn \@@_compute_margin:N #1
2534 {
2535 \use:e
2536 {
2537 \bool_if:NTF \l_@@_skip_empty_lines_bool
2538 { \lua_now:n { piton.CountNonEmptyLines(token.scan_argument()) } }
2539 { \lua_now:n { piton.CountLines(token.scan_argument()) } }
2540 { \l_@@_listing_tl }
2541 }
2542 \hbox_set:Nn \l_tmpa_box
2543 {
2544 \l_@@_line_numbers_format_tl
2545 \int_to_arabic:n
2546 {
2547 \g_@@_visual_line_int
2548 +
2549 \bool_if:NTF \l_@@_skip_empty_lines_bool
2550 { \l_@@_nb_non_empty_lines_int }
2551 { \g_@@_nb_lines_int }
2552 }
2553 }
2554 \dim_set:Nn #1 { \box_wd:N \l_tmpa_box + \l_@@_numbers_sep_dim + 0.1 em }
2555 }
2556 % \end{macrocode}
2557 %
2558 %
2559 %
2560 % \bigskip
2561 % The following command computes |\l_@@_code_width_dim|.
2562 %
2563 % It will be used only once (in |\@@_create_output_box:|).
2564 %
2565 % If there is a background (even a background with the color |none|), we
2566 % subtract 0.5~em on both sides. However, if there is a left margin or a right
2567 % margin, we use those margins. If the key |left-margin| has been used with the
2568 % special value |auto| (this is meaningful only in conjonction with the key
2569 % |line-numbers| and a value of |line-numbers/position| equal to |left|), the
2570 % actual value for the left margin has yet computed (and stored in
2571 % |left-margin|). Idem for the right margin.
2572 % \begin{macrocode}
2573 \cs_new_protected:Npn \@@_compute_code_width:
2574 {
2575 \dim_set:Nn \l_@@_code_width_dim
2576 {
2577 \l_@@_listing_width_dim
2578 -
2579 (
2580 \int_compare:nNnTF \l_@@_bg_colors_int > { \c_zero_int }
2581 {
2582 \dim_compare:nNnTF \l_@@_left_margin_dim > \c_zero_dim
2583 { \l_@@_left_margin_dim }
2584 { 0.5 em }
2585 +
2586 \dim_compare:nNnTF \l_@@_right_margin_dim > \c_zero_dim
2587 { \l_@@_right_margin_dim }
2588 { 0.5 em }
2589 }
2590 { \l_@@_left_margin_dim + \l_@@_right_margin_dim }
2591 )
2592 }
2593 }
2594 % \end{macrocode}
2595 %
2596 % \bigskip
2597 % The following command computes |\l_@@_listing_width_dim| and it will be used
2598 % when |max-width| (or |width=min|) is used. Remind that the key |box| sets |width=min|
2599 % (except when |width| is used with a numerical value).
2600 %
2601 % It will be used only once (in |\@@_create_output_box:|).
2602 %
2603 % The computation is the inverse of the computation done in |\@@_compute_code_width:|.
2604 % \begin{macrocode}
2605 \cs_new_protected:Npn \@@_recompute_listing_width:
2606 {
2607 \dim_set:Nn \l_@@_listing_width_dim
2608 {
2609 \box_wd:N \g_@@_output_box
2610 +
2611 \int_compare:nNnTF \l_@@_bg_colors_int > \c_zero_int
2612 {
2613 \dim_compare:nNnTF \l_@@_left_margin_dim > \c_zero_dim
2614 { \l_@@_left_margin_dim }
2615 { 0.5 em }
2616 +
2617 \dim_compare:nNnTF \l_@@_right_margin_dim > \c_zero_dim
2618 { \l_@@_right_margin_dim }
2619 { 0.5 em }
2620 }
2621 { \l_@@_left_margin_dim + \l_@@_right_margin_dim }
2622 }
2623 }
2624 % \end{macrocode}
2625 %
2626 % \bigskip
2627 % \begin{macrocode}
2628 \cs_new_protected:Npn \@@_store_body:n #1
2629 {
2630 % \end{macrocode}
2631 % Now, we have to replace all the occurrences of |\obeyedline| by a character of
2632 % end of line (|\r| in the strings of Lua).
2633 % \begin{macrocode}
2634 \tl_set:Ne \obeyedline { \char_generate:nn { 13 } { 11 } }
2635 \tl_set:Ne \l_@@_listing_tl { #1 }
2636 \tl_set_eq:NN \ProcessedArgument \l_@@_listing_tl
2637 }
2638 % \end{macrocode}
2639 %
2640 % The first argument of the following macro is one of the four strings:
2641 % |New|, |Renew|, |Provide| and |Declare|.
2642 % \begin{macrocode}
2643 \cs_new_protected:Nn \@@_DefinePitonEnvironment:nnnnn
2644 {
2645 \use:c { #1 DocumentEnvironment } { #2 } { #3 > { \@@_store_body:n } c }
2646 {
2647 \cs_set_eq:NN \PitonOptions \@@_fake_PitonOptions
2648 #4
2649 \@@_pre_composition:
2650 \int_compare:nNnT { \l_@@_number_lines_start_int } > { \c_zero_int }
2651 {
2652 \int_gset:Nn \g_@@_visual_line_int
2653 { \l_@@_number_lines_start_int - 1 }
2654 }
2655 \bool_if:NT \g_@@_beamer_bool
2656 { \@@_translate_beamer_env:o { \l_@@_listing_tl } }
2657 \bool_if:NT \g_@@_footnote_bool \savenotes
2658 \@@_composition:
2659 \bool_lazy_or:nnT { \l_@@_paperclip_bool } { \l_@@_annotation_bool }
2660 { \@@_create_paperclip_annotation: }
2661 \bool_if:NT \g_@@_footnote_bool \endsavenotes
2662 #5
2663 }
2664 { \ignorespacesafterend }
2665 }
2666 % \end{macrocode}
2667 %
2668 %
2669 % \bigskip
2670 % |\marginalia| is a command of the package \pkg{marginalia} (loaded by \pkg{piton}).
2671 % \begin{macrocode}
2672 \cs_new_protected:Npn \@@_create_paperclip_annotation:
2673 {
2674 \marginalia
2675 {
2676 \vspace* { - 0.8 em }
2677 \hbox:n
2678 {
2679 \vrule~height~0~pt~depth~12~pt~width~0~pt
2680 \bool_if:NT \l_@@_annotation_bool
2681 {
2682 \lua_now:n
2683 {
2684 % \end{macrocode}
2685 % The function |piton.utf16| does a conversion from utf8 to utf16 big endian encoded in
2686 % hexadecimal (with the \textsc{bom} of big endian), which is suitable to be put in
2687 % a string between angular brackets of the \textsc{pdf}. It's easier for a stream!
2688 % \begin{macrocode}
2689 pdf.immediateobj
2690 ( "<" .. piton.utf16 ( piton.get_last_code ( ) ) .. ">" )
2691 }
2692 \pdfextension annot~width~5pt~height~10pt~depth~0pt
2693 {
2694 /Subtype /Text
2695 /Contents~\pdf_object_ref_last:
2696 /Name /Note
2697 /Subj (Computer~listing)
2698 % \end{macrocode}
2699 % The following tries to specify that the note should not receive answers (since it is meant
2700 % for an easy copy-past of the computer listing).
2701 % \begin{macrocode}
2702 /ReplyType /Group
2703 % \end{macrocode}
2704 % Adds the bit 10 which means |LockedContents|.
2705 % \begin{macrocode}
2706 /F~512
2707 /C [0.8~0.8~0.8]
2708 }
2709 \hspace* { 7 mm }
2710 }
2711 \bool_if:NT \l_@@_paperclip_bool { \@@_create_paperclip: }
2712 }
2713 }
2714 }
2715 % \end{macrocode}
2716 %
2717 % \bigskip
2718 % \begin{macrocode}
2719 \cs_new_protected:Npn \@@_create_paperclip:
2720 {
2721 \str_if_empty:NT \l_@@_paperclip_str
2722 {
2723 \int_gincr:N \g_@@_paperclip_int
2724 \str_set:Ne \l_@@_paperclip_str { listing_\int_use:N \g_@@_paperclip_int .txt }
2725 }
2726 % \end{macrocode}
2727 % Here, we don't understand why the |tostring| is mandatory.
2728 % \begin{macrocode}
2729 \lua_now:n { pdf.immediateobj ( "stream" , tostring ( piton.get_last_code() ) ) }
2730 \box_move_down:nn
2731 { 10 pt }
2732 {
2733 \hbox:n
2734 {
2735 \pdfextension annot~width~10pt~height~20pt~depth~0pt
2736 {
2737 /Subtype /FileAttachment
2738 /Name /Paperclip
2739 /F~8 % no zoom
2740 % \end{macrocode}
2741 % |/Contents| will be used as info-bulle and description of the file in the panel of the
2742 % embedded files.
2743 % \begin{macrocode}
2744 /Contents (The~computer~listing)
2745 /FS <<
2746 /Type /Filespec
2747 /F (\l_@@_paperclip_str)
2748 /EF << /F~\pdf_object_ref_last: >>
2749 /AFRelationship /Supplement
2750 >>
2751 }
2752 }
2753 }
2754 }
2755 % \end{macrocode}
2756 %
2757 %
2758 % \bigskip
2759 % For the following commands, the arguments are provided by curryfication.
2760 % \begin{macrocode}
2761 \NewDocumentCommand { \NewPitonEnvironment } { }
2762 { \@@_DefinePitonEnvironment:nnnnn { New } }
2763 % \end{macrocode}
2764 %
2765 % \begin{macrocode}
2766 \NewDocumentCommand { \DeclarePitonEnvironment } { }
2767 { \@@_DefinePitonEnvironment:nnnnn { Declare } }
2768 % \end{macrocode}
2769 %
2770 % \begin{macrocode}
2771 \NewDocumentCommand { \RenewPitonEnvironment } { }
2772 { \@@_DefinePitonEnvironment:nnnnn { Renew } }
2773 % \end{macrocode}
2774 %
2775 % \begin{macrocode}
2776 \NewDocumentCommand { \ProvidePitonEnvironment } { }
2777 { \@@_DefinePitonEnvironment:nnnnn { Provide } }
2778 % \end{macrocode}
2779 %
2780 %
2781 % \bigskip
2782 % \begin{macrocode}
2783 \cs_new_protected:Npn \@@_translate_beamer_env:n
2784 { \lua_now:e { piton.TranslateBeamerEnv(token.scan_argument ( ) ) } }
2785 \cs_generate_variant:Nn \@@_translate_beamer_env:n { o }
2786 % \end{macrocode}
2787 %
2788 % \bigskip
2789 % \begin{macrocode}
2790 \cs_new_protected:Npn \@@_composition:
2791 {
2792 \str_if_empty:NT \l_@@_box_str
2793 {
2794 \mode_if_vertical:F
2795 { \bool_if:NF \l_@@_in_PitonInputFile_bool { \newline } }
2796 }
2797 % \end{macrocode}
2798 %
2799 % \begin{macrocode}
2800 \bool_if:NT \l_@@_line_numbers_bool
2801 {
2802 \bool_lazy_and:nnT
2803 { \l_@@_left_margin_auto_bool }
2804 { \str_if_eq_p:ee \l_@@_line_numbers_position_str { left } }
2805 { \@@_compute_margin:N \l_@@_left_margin_dim }
2806 \bool_lazy_and:nnT
2807 { \l_@@_right_margin_auto_bool }
2808 { \str_if_eq_p:ee \l_@@_line_numbers_position_str { right } }
2809 { \@@_compute_margin:N \l_@@_right_margin_dim }
2810 }
2811 % \end{macrocode}
2812 %
2813 % \begin{macrocode}
2814 \lua_now:e
2815 {
2816 piton.join_separation = "\l_@@_join_separation_str"
2817 piton.join = "\l_@@_join_str"
2818 piton.write = "\l_@@_write_str"
2819 piton.path_write = "\l_@@_path_write_str"
2820 }
2821 \noindent
2822 \bool_if:NTF \l_@@_print_bool
2823 {
2824 % \end{macrocode}
2825 % When |split-on-empty-lines| is in force, each chunk will be formated by an
2826 % environment |{Piton}| (or the environment specified by |env-used-by-split|).
2827 % Within each of these environments, we will come back here (but, of course,
2828 % |split-on-empty-line| will have been set to |false|). The mechanism ``|retrieve|''
2829 % is mandatory.
2830 % \begin{macrocode}
2831 \bool_if:NTF \l_@@_split_on_empty_lines_bool
2832 { \par \@@_retrieve_gobble_split_parse:o \l_@@_listing_tl }
2833 {
2834 \@@_create_output_box:
2835 % \end{macrocode}
2836 % Now, the listing has been composed in |\g_@@_output_box| and |\l_@@_listing_width_dim|
2837 % contains the width of the listing (with the potential margin for the numbers of lines).
2838 % \begin{macrocode}
2839 \bool_if:NTF \l_@@_tcolorbox_bool
2840 {
2841 \str_if_empty:NTF \l_@@_box_str
2842 \@@_composition_iii:
2843 \@@_composition_iv:
2844 }
2845 {
2846 \str_if_empty:NTF \l_@@_box_str
2847 \@@_composition_i:
2848 \@@_composition_ii:
2849 }
2850 }
2851 }
2852 { \@@_gobble_parse_no_print:o \l_@@_listing_tl }
2853 }
2854 % \end{macrocode}
2855 %
2856 % \bigskip
2857 % |\@@_composition_i:| is for the main case: the key |tcolorbox| is not used, nor
2858 % the key |box|.
2859 %
2860 % We can't do a mere |\vbox_unpack:N \g_@@_output_box| because that would not work
2861 % inside a list of LaTeX (|{itemize}| or |{enumerate}|).
2862 %
2863 % The composition in the box |\g_@@_output_box| was mandatory to be able to deal
2864 % with the case of a conjunction of the keys |width=min| and |background-color=...|.
2865 % \begin{macrocode}
2866 \cs_new_protected:Npn \@@_composition_i:
2867 {
2868 % \end{macrocode}
2869 % First, we ``reverse'' the box |\g_@@_output_box|: we put in the box
2870 % |\g_tmpa_box| the boxes present in |\g_@@_output_box|, but in reversed order.
2871 % The vertical spaces and the penalties are discarded.
2872 % \begin{macrocode}
2873 \box_clear:N \g_tmpa_box
2874 % \end{macrocode}
2875 % The box |\g_@@_line_box| will be used as an auxiliary box.
2876 % \begin{macrocode}
2877 \box_clear_new:N \g_@@_line_box
2878 % \end{macrocode}
2879 % We unpack |\g_@@_output_box| in |\l_tmpa_box| used as a scratched box.
2880 % \begin{macrocode}
2881 \vbox_set:Nn \l_tmpa_box
2882 {
2883 \vbox_unpack_drop:N \g_@@_output_box
2884 \bool_gset_false:N \g_tmpa_bool
2885 \unskip \unskip
2886 \bool_gset_false:N \g_tmpa_bool
2887 \bool_do_until:nn \g_tmpa_bool
2888 {
2889 \unskip \unskip \unskip
2890 \unpenalty \unkern
2891 \box_set_to_last:N \l_@@_line_box
2892 \box_if_empty:NTF \l_@@_line_box
2893 { \bool_gset_true:N \g_tmpa_bool }
2894 {
2895 \vbox_gset:Nn \g_tmpa_box
2896 {
2897 \vbox_unpack:N \g_tmpa_box
2898 \box_use:N \l_@@_line_box
2899 }
2900 }
2901 }
2902 }
2903 % \end{macrocode}
2904 % Now, we will loop over the boxes in |\g_tmpa_box| and compose the
2905 % boxes in the TeX flow.
2906 % \begin{macrocode}
2907 \bool_gset_false:N \g_tmpa_bool
2908 \int_zero:N \g_@@_line_int
2909 \bool_do_until:nn \g_tmpa_bool
2910 {
2911 % \end{macrocode}
2912 % We retrieve the last box of |\g_tmpa_box| (and store it in |\g_@@_line_box|)
2913 % and keep the other boxes in |\g_tmpa_box|.
2914 % \begin{macrocode}
2915 \vbox_gset:Nn \g_tmpa_box
2916 {
2917 \vbox_unpack_drop:N \g_tmpa_box
2918 \box_gset_to_last:N \g_@@_line_box
2919 }
2920 % \end{macrocode}
2921 % If the box that we have retrieved is void, that means that, in fact, there is no
2922 % longer boxes in |\g_tmpa_box| and we will exit the loop.
2923 % \begin{macrocode}
2924 \box_if_empty:NTF \g_@@_line_box
2925 { \bool_gset_true:N \g_tmpa_bool }
2926 {
2927 \box_use:N \g_@@_line_box
2928 \int_gincr:N \g_@@_line_int
2929 \par
2930 \kern -2.5 pt
2931 % \end{macrocode}
2932 % We will determine the penalty by reading the Lua table |piton.lines_status|. That
2933 % will use the current value of |\g_@@_line_int|.
2934 % \begin{macrocode}
2935 \@@_add_penalty_for_the_line:
2936 % \end{macrocode}
2937 % We now add the instructions corresponding to the \emph{vertical detected commands}
2938 % that are potentially used in the corresponding line of the listing.
2939 % \begin{macrocode}
2940 \cs_if_exist_use:cT { g_@@_after_line _ \int_use:N \g_@@_line_int }
2941 { \cs_undefine:c { g_@@_after_line _ \int_use:N \g_@@_line_int } }
2942 \int_compare:nNnT \g_@@_line_int < \g_@@_nb_lines_int
2943 { \mode_leave_vertical: }
2944 }
2945 }
2946 \skip_vertical:n { 2.5 pt }
2947 }
2948 % \end{macrocode}
2949 %
2950 % \bigskip
2951 % |\@@_composition_ii:| will be used when the key |box| is in force but \emph{not} the key
2952 % |tcolorbox|.
2953 % \begin{macrocode}
2954 \cs_new_protected:Npn \@@_composition_ii:
2955 {
2956 \use:e { \begin { minipage } [ \l_@@_box_str ] }
2957 { \l_@@_listing_width_dim }
2958 % \end{macrocode}
2959 % Here, |\vbox_unpack:N|, instead of |\box_use:N| is mandatory for the vertical
2960 % position of the box.
2961 % \begin{macrocode}
2962 \vbox_unpack:N \g_@@_output_box
2963 % \end{macrocode}
2964 % |\kern| is mandatory here (|\skip_vertical:n| won't work).
2965 % \begin{macrocode}
2966 \kern 2.5 pt
2967 \end { minipage }
2968 }
2969 % \end{macrocode}
2970 %
2971 % \bigskip
2972 % |\@@_composition_iii:| will be used when the key |tcolorbox| is in force but
2973 % \emph{not} the key |box|.
2974 % \begin{macrocode}
2975 \cs_new_protected:Npn \@@_composition_iii:
2976 {
2977 \use:e
2978 {
2979 \begin { tcolorbox }
2980 % \end{macrocode}
2981 % Even though we use the key |breakable| of |{tcolorbox}|, our environment will be
2982 % breakable only when the key |splittable| of \pkg{piton} is used.
2983 % \begin{macrocode}
2984 [ breakable , text~width = \l_@@_listing_width_dim ]
2985 }
2986 \par
2987 \vbox_unpack:N \g_@@_output_box
2988 \end { tcolorbox }
2989 }
2990 % \end{macrocode}
2991 %
2992 % \bigskip
2993 % |\@@_composition_iv:| will be used when both keys |tcolorbox| and |box| are in force.
2994 % \begin{macrocode}
2995 \cs_new_protected:Npn \@@_composition_iv:
2996 {
2997 \use:e
2998 {
2999 \begin { tcolorbox }
3000 [
3001 hbox ,
3002 text~width = \l_@@_listing_width_dim ,
3003 nobeforeafter ,
3004 box~align =
3005 \str_case:Nn \l_@@_box_str
3006 {
3007 t { top }
3008 b { bottom }
3009 c { center }
3010 m { center }
3011 }
3012 ]
3013 }
3014 \box_use:N \g_@@_output_box
3015 \end { tcolorbox }
3016 }
3017 % \end{macrocode}
3018 %
3019 %
3020 % \bigskip
3021 % The following function will add the correct vertical penalty after
3022 % a line of code in order to control the breaks of the pages. We use the Lua table
3023 % |piton.lines_status| which has been written by |piton.ComputeLinesStatus| for
3024 % this aim. Each line has a ``status`` (equal to 0, 1 or 2) and that status
3025 % directly says whether a break is allowed.
3026 % \begin{macrocode}
3027 \cs_new_protected:Npn \@@_add_penalty_for_the_line:
3028 {
3029 \int_case:nn
3030 {
3031 \lua_now:e
3032 {
3033 tex.sprint
3034 ( piton.lines_status [ \int_use:N \g_@@_line_int ] )
3035 }
3036 }
3037 { 1 { \penalty 100 } 2 \nobreak }
3038 }
3039 % \end{macrocode}
3040 %
3041 % \bigskip
3042 % |\@@_create_output_box:| is used only once, in |\@@_composition:|.
3043 %
3044 % It creates (and modifies when there are backgrounds or numbers of the lines
3045 % on the right) |\g_@@_output_box|.
3046 % \begin{macrocode}
3047 \cs_new_protected:Npn \@@_create_output_box:
3048 {
3049 \@@_compute_code_width:
3050 \vbox_gset:Nn \g_@@_output_box
3051 { \@@_retrieve_gobble_parse:o \l_@@_listing_tl }
3052 \bool_if:NT \l_@@_minimize_width_bool { \@@_recompute_listing_width: }
3053 \bool_lazy_any:nT
3054 {
3055 { \int_compare_p:nNn \l_@@_bg_colors_int > \c_zero_int }
3056 { \g_@@_rowcolor_inside_bool }
3057 {
3058 \l_@@_line_numbers_bool
3059 &&
3060 \str_if_eq_p:ee \l_@@_line_numbers_position_str { right }
3061 }
3062 }
3063 \@@_add_bg_and_right_nb_to_output_box:
3064 }
3065 % \end{macrocode}
3066 %
3067 %
3068 % \bigskip
3069 % We add the backgrounds after the composition of the box |\g_@@_output_box| by a
3070 % loop over the lines in that box. Idem when the key |line-numbers| is used in
3071 % conjunction with |line-numbers/position=right|.
3072 %
3073 % The backgrounds will have a width equal to |\l_@@_listing_width_dim|.
3074 %
3075 % That command will be used only once, in |\@@_create_output_box:|.
3076 % \begin{macrocode}
3077 \cs_new_protected:Npn \@@_add_bg_and_right_nb_to_output_box:
3078 {
3079 \int_gset_eq:NN \g_@@_line_int \g_@@_nb_lines_int
3080 % \end{macrocode}
3081 % |\l_tmpa_box| is only used to \emph{unpack} the vertical box |\g_@@_output_box|.
3082 % \begin{macrocode}
3083 \vbox_set:Nn \l_tmpa_box
3084 {
3085 \vbox_unpack_drop:N \g_@@_output_box
3086 % \end{macrocode}
3087 % We will raise |\g_tmpa_bool| to exit the loop |\bool_do_until:nn| below.
3088 % \begin{macrocode}
3089 \bool_gset_false:N \g_tmpa_bool
3090 \unskip \unskip
3091 % \end{macrocode}
3092 % We begin the loop.
3093 % \begin{macrocode}
3094 \bool_do_until:nn \g_tmpa_bool
3095 {
3096 \unskip \unskip \unskip
3097 \int_set_eq:NN \l_tmpa_int \lastpenalty
3098 \unpenalty \unkern
3099 % \end{macrocode}
3100 % In standard TeX (not LuaTeX), the only way to loop over the sub-boxes of a given
3101 % box is to use the TeX primitive |\lastbox| (via |\box_set_to_last:N| of L3).
3102 % Of course, it would be interesting to replace that programming by a programming
3103 % in Lua of LuaTeX...
3104 % \begin{macrocode}
3105 \box_set_to_last:N \l_@@_line_box
3106 \box_if_empty:NTF \l_@@_line_box
3107 { \bool_gset_true:N \g_tmpa_bool }
3108 {
3109 % \end{macrocode}
3110 % |\g_@@_line_int| will be used in |\@@_add_bg_and_right_nb_to_line_and_use:|.
3111 % \begin{macrocode}
3112 \vbox_gset:Nn \g_@@_output_box
3113 {
3114 % \end{macrocode}
3115 % The command |\@@_add_bg_and_right_nb_to_line_and_use:| will add a background to
3116 % the line (in |\l_@@_line_box|) but will also put the line in the current box.
3117 % The background will have a width equal to |\l_@@_listing_width_dim|.
3118 % \begin{macrocode}
3119 \@@_add_bg_and_right_nb_to_line_and_use:
3120 \kern -2.5 pt
3121 \penalty \l_tmpa_int
3122 \vbox_unpack:N \g_@@_output_box
3123 }
3124 }
3125 \int_gdecr:N \g_@@_line_int
3126 }
3127 }
3128 }
3129 % \end{macrocode}
3130 %
3131 % \bigskip
3132 % The following will be used when the end user has used |print=false|.
3133 % \begin{macrocode}
3134 \cs_new_protected:Npn \@@_gobble_parse_no_print:n
3135 {
3136 \lua_now:e
3137 {
3138 piton.GobbleParseNoPrint
3139 (
3140 '\l_piton_language_str' ,
3141 \int_use:N \l_@@_gobble_int ,
3142 token.scan_argument ( )
3143 )
3144 }
3145 }
3146 \cs_generate_variant:Nn \@@_gobble_parse_no_print:n { o }
3147 % \end{macrocode}
3148 %
3149 % \bigskip
3150 % The following function will be used when the key |split-on-empty-lines| is not
3151 % in force. It will retrieve the first empty line, gobble the spaces at the
3152 % beginning of the lines and parse the code. The argument is provided by
3153 % curryfication.
3154 % \begin{macrocode}
3155 \cs_new_protected:Npn \@@_retrieve_gobble_parse:n
3156 {
3157 \lua_now:e
3158 {
3159 piton.RetrieveGobbleParse
3160 (
3161 '\l_piton_language_str' ,
3162 \int_use:N \l_@@_gobble_int ,
3163 \bool_if:NTF \l_@@_splittable_on_empty_lines_bool
3164 { \int_eval:n { - \l_@@_splittable_int } }
3165 { \int_use:N \l_@@_splittable_int } ,
3166 token.scan_argument ( )
3167 )
3168 }
3169 }
3170 \cs_generate_variant:Nn \@@_retrieve_gobble_parse:n { o }
3171 % \end{macrocode}
3172 %
3173 % \bigskip
3174 % The following function will be used when the key |split-on-empty-lines| is in
3175 % force. It will gobble the spaces at the beginning of the lines (if the key
3176 % |gobble| is in force), then split the code at the empty lines and, eventually,
3177 % parse the code. The argument is provided by curryfication.
3178 % \begin{macrocode}
3179 \cs_new_protected:Npn \@@_retrieve_gobble_split_parse:n
3180 {
3181 \lua_now:e
3182 {
3183 piton.RetrieveGobbleSplitParse
3184 (
3185 '\l_piton_language_str' ,
3186 \int_use:N \l_@@_gobble_int ,
3187 \int_use:N \l_@@_splittable_int ,
3188 token.scan_argument ( )
3189 )
3190 }
3191 }
3192 \cs_generate_variant:Nn \@@_retrieve_gobble_split_parse:n { o }
3193 % \end{macrocode}
3194 %
3195 % \bigskip
3196 % Now, we define the environment |{Piton}|, which is the main environment
3197 % provided by the package \pkg{piton}. Of course, you use |\NewPitonEnvironment|.
3198 % \begin{macrocode}
3199 \bool_if:NTF \g_@@_beamer_bool
3200 {
3201 \NewPitonEnvironment { Piton } { D < > { .- } O { } }
3202 {
3203 \keys_set:nn { PitonOptions } { #2 }
3204 \begin { actionenv } < #1 >
3205 }
3206 { \end { actionenv } }
3207 }
3208 {
3209 \NewPitonEnvironment { Piton } { O { } }
3210 { \keys_set:nn { PitonOptions } { #1 } }
3211 { }
3212 }
3213 % \end{macrocode}
3214 %
3215 %
3216 % \bigskip
3217 % \begin{macrocode}
3218 \NewDocumentCommand { \PitonInputFileTF } { d < > O { } m m m }
3219 {
3220 \mode_if_vertical:F { \par }
3221 \group_begin:
3222 \seq_concat:NNN
3223 \l_file_search_path_seq
3224 \l_@@_path_seq
3225 \l_file_search_path_seq
3226 \file_get_full_name:nNTF { #3 } \l_@@_file_name_str
3227 {
3228 \@@_input_file:nn { #1 } { #2 }
3229 #4
3230 }
3231 { #5 }
3232 \group_end:
3233 }
3234 % \end{macrocode}
3235 %
3236 % \medskip
3237 % \begin{macrocode}
3238 \cs_new_protected:Npn \@@_unknown_file:n #1
3239 { \msg_error:nnn { piton } { Unknown~file } { #1 } }
3240 % \end{macrocode}
3241 %
3242 % \begin{macrocode}
3243 \NewDocumentCommand { \PitonInputFile } { d < > O { } m }
3244 {
3245 \PitonInputFileTF < #1 > [ #2 ] { #3 } { }
3246 {
3247 % \end{macrocode}
3248 % The following line is for |latexmk| (suggestion of Y. Salmon).
3249 % \begin{macrocode}
3250 \iow_log:n { No~file~#3 }
3251 \@@_unknown_file:n { #3 }
3252 }
3253 }
3254 \NewDocumentCommand { \PitonInputFileT } { d < > O { } m m }
3255 {
3256 \PitonInputFileTF < #1 > [ #2 ] { #3 } { #4 }
3257 {
3258 % \end{macrocode}
3259 % The following line is for |latexmk| (suggestion of Y. Salmon).
3260 % \begin{macrocode}
3261 \iow_log:n { No~file~#3 }
3262 \@@_unknown_file:n { #3 }
3263 }
3264 }
3265 \NewDocumentCommand { \PitonInputFileF } { d < > O { } m m }
3266 { \PitonInputFileTF < #1 > [ #2 ] { #3 } { } { #4 } }
3267 % \end{macrocode}
3268 %
3269 % The following command uses as implicit argument the name of the file in
3270 % |\l_@@_file_name_str|.
3271 % \begin{macrocode}
3272 \cs_new_protected:Npn \@@_input_file:nn #1 #2
3273 {
3274 % \end{macrocode}
3275 % We recall that, if we are in Beamer, the command |\PitonInputFile| is
3276 % ``overlay-aware'' and that's why there is an optional argument between angular
3277 % brackets (|<| and |>|).
3278 % \begin{macrocode}
3279 \tl_if_novalue:nF { #1 }
3280 {
3281 \bool_if:NTF \g_@@_beamer_bool
3282 { \begin { uncoverenv } < #1 > }
3283 { \@@_error_or_warning:n { overlay~without~beamer } }
3284 }
3285 \group_begin:
3286 % \end{macrocode}
3287 % The following line is to allow tools such as |latexmk| to be aware that the
3288 % file read by |\PitonInputFile| is loaded during the compilation of the LaTeX
3289 % document.
3290 % \begin{macrocode}
3291 \iow_log:e { (\l_@@_file_name_str) }
3292 \int_zero_new:N \l_@@_first_line_int
3293 \int_zero_new:N \l_@@_last_line_int
3294 \int_set_eq:NN \l_@@_last_line_int \c_max_int
3295 \bool_set_true:N \l_@@_in_PitonInputFile_bool
3296 \keys_set:nn { PitonOptions } { #2 }
3297 \bool_if:NT \l_@@_line_numbers_absolute_bool
3298 { \bool_set_false:N \l_@@_skip_empty_lines_bool }
3299 \bool_if:nTF
3300 {
3301 (
3302 \int_compare_p:nNn \l_@@_first_line_int > \c_zero_int
3303 || \int_compare_p:nNn \l_@@_last_line_int < \c_max_int
3304 )
3305 && ! \str_if_empty_p:N \l_@@_begin_range_str
3306 }
3307 {
3308 \@@_error_or_warning:n { bad~range~specification }
3309 \int_zero:N \l_@@_first_line_int
3310 \int_set_eq:NN \l_@@_last_line_int \c_max_int
3311 }
3312 {
3313 \str_if_empty:NF \l_@@_begin_range_str
3314 {
3315 \@@_compute_range:
3316 \bool_lazy_or:nnT
3317 \l_@@_marker_include_lines_bool
3318 { ! \str_if_eq_p:NN \l_@@_begin_range_str \l_@@_end_range_str }
3319 {
3320 \int_decr:N \l_@@_first_line_int
3321 \int_incr:N \l_@@_last_line_int
3322 }
3323 }
3324 }
3325 \@@_pre_composition:
3326 \bool_if:NT \l_@@_line_numbers_absolute_bool
3327 { \int_gset:Nn \g_@@_visual_line_int { \l_@@_first_line_int - 1 } }
3328 \int_compare:nNnT \l_@@_number_lines_start_int > \c_zero_int
3329 {
3330 \int_gset:Nn \g_@@_visual_line_int
3331 { \l_@@_number_lines_start_int - 1 }
3332 }
3333 % \end{macrocode}
3334 % The following case arises when the code |line-numbers/absolute| is in force
3335 % without the use of a marked range.
3336 % \begin{macrocode}
3337 \int_compare:nNnT \g_@@_visual_line_int < \c_zero_int
3338 { \int_gzero:N \g_@@_visual_line_int }
3339 % \end{macrocode}
3340 %
3341 % \begin{macrocode}
3342 \lua_now:e
3343 {
3344 % \end{macrocode}
3345 % The following command will store the content of the file (or only a part of that file)
3346 % in |\l_@@_listing_tl|.
3347 % \begin{macrocode}
3348 piton.ReadFile(
3349 '\l_@@_file_name_str' ,
3350 \int_use:N \l_@@_first_line_int ,
3351 \int_use:N \l_@@_last_line_int )
3352 }
3353 \@@_composition:
3354 \group_end:
3355 % \end{macrocode}
3356 %
3357 %
3358 % We recall that, if we are in Beamer, the command |\PitonInputFile| is
3359 % ``overlay-aware'' and that's why we close now an environment |{uncoverenv}|
3360 % that we have opened at the beginning of the command.
3361 % \begin{macrocode}
3362 \tl_if_novalue:nF { #1 }
3363 { \bool_if:NT \g_@@_beamer_bool { \end { uncoverenv } } }
3364 }
3365 % \end{macrocode}
3366 %
3367 % \bigskip
3368 % The following command computes the values of |\l_@@_first_line_int| and
3369 % |\l_@@_last_line_int| when |\PitonInputFile| is used with textual markers.
3370 % \begin{macrocode}
3371 \cs_new_protected:Npn \@@_compute_range:
3372 {
3373 % \end{macrocode}
3374 % We store the markers in L3 strings (|str|) in order to do safely the following
3375 % replacement of |\#|.
3376 % \begin{macrocode}
3377 \str_set:Ne \l_tmpa_str { \@@_marker_beginning:n { \l_@@_begin_range_str } }
3378 \str_set:Ne \l_tmpb_str { \@@_marker_end:n { \l_@@_end_range_str } }
3379 % \end{macrocode}
3380 % We replace the sequences |\#| which may be present in the prefixes and
3381 % suffixes added to the markers by the functions |\@@_marker_beginning:n| and
3382 % |\@@_marker_end:n|.
3383 % \begin{macrocode}
3384 \tl_replace_all:Nee \l_tmpa_str { \c_backslash_str \c_hash_str } \c_hash_str
3385 \tl_replace_all:Nee \l_tmpb_str { \c_backslash_str \c_hash_str } \c_hash_str
3386 % \end{macrocode}
3387 %
3388 % \begin{macrocode}
3389 \lua_now:e
3390 {
3391 piton.ComputeRange
3392 ( '\l_tmpa_str' , '\l_tmpb_str' , '\l_@@_file_name_str' )
3393 }
3394 }
3395 % \end{macrocode}
3396 %
3397 % \bigskip
3398 % \subsection{The styles}
3399 %
3400 % \medskip
3401 % The following command is fundamental: it will be used by the Lua code.
3402 % \begin{macrocode}
3403 \NewDocumentCommand { \PitonStyle } { m }
3404 {
3405 \cs_if_exist_use:cF { pitonStyle _ \l_piton_language_str _ #1 }
3406 { \use:c { pitonStyle _ #1 } }
3407 }
3408 % \end{macrocode}
3409 %
3410 % \medskip
3411 % The following variant will be rarely used. It applies only a local style
3412 % and only when that style exists (no error will be raised when the style does not
3413 % exist). That command will be used in particular for the language ``|expl|''.
3414 % \begin{macrocode}
3415 \NewDocumentCommand { \OptionalLocalPitonStyle } { m }
3416 { \cs_if_exist_use:c { pitonStyle _ \l_piton_language_str _ #1 } }
3417 % \end{macrocode}
3418 %
3419 % \medskip
3420 % \begin{macrocode}
3421 \NewDocumentCommand { \SetPitonStyle } { O { } m }
3422 {
3423 \str_clear_new:N \l_@@_SetPitonStyle_option_str
3424 \str_set:Ne \l_@@_SetPitonStyle_option_str { \str_lowercase:n { #1 } }
3425 \str_if_eq:onT { \l_@@_SetPitonStyle_option_str } { current-language }
3426 { \str_set_eq:NN \l_@@_SetPitonStyle_option_str \l_piton_language_str }
3427 \keys_set:nn { piton / Styles } { #2 }
3428 }
3429 % \end{macrocode}
3430 %
3431 %
3432 % \medskip
3433 % \begin{macrocode}
3434 \cs_new_protected:Npn \@@_math_scantokens:n #1
3435 { \normalfont \scantextokens { \begin{math} #1 \end{math} } }
3436 % \end{macrocode}
3437 %
3438 % \medskip
3439 % \begin{macrocode}
3440 \clist_new:N \g_@@_styles_clist
3441 \clist_gset:Nn \g_@@_styles_clist
3442 {
3443 Comment ,
3444 Comment.Internal ,
3445 Comment.LaTeX ,
3446 Discard ,
3447 Delim ,
3448 Exception ,
3449 FormattingType ,
3450 Identifier.Internal ,
3451 Identifier ,
3452 InitialValues ,
3453 Interpol.Inside ,
3454 Keyword ,
3455 Keyword.Governing ,
3456 Keyword.Constant ,
3457 Keyword2 ,
3458 Keyword3 ,
3459 Keyword4 ,
3460 Keyword5 ,
3461 Keyword6 ,
3462 Keyword7 ,
3463 Keyword8 ,
3464 Keyword9 ,
3465 Name.Builtin ,
3466 Name.Class ,
3467 Name.Constructor ,
3468 Name.Decorator ,
3469 Name.Field ,
3470 Name.Function ,
3471 Name.Module ,
3472 Name.Namespace ,
3473 Name.Table ,
3474 Name.Type ,
3475 Number ,
3476 Number.Internal ,
3477 Operator ,
3478 Operator.Word ,
3479 Preproc ,
3480 Prompt ,
3481 Punct ,
3482 String.Doc ,
3483 String.Doc.Internal ,
3484 String.Interpol ,
3485 String.Long ,
3486 String.Long.Internal ,
3487 String.Short ,
3488 String.Short.Internal ,
3489 Tag ,
3490 TypeParameter ,
3491 UserFunction ,
3492 % \end{macrocode}
3493 % |TypeExpression| is an internal style for expressions which defines types in OCaml.
3494 % \begin{macrocode}
3495 TypeExpression ,
3496 % \end{macrocode}
3497 % Now, specific styles for the languages created with |\NewPitonLanguage| with
3498 % the syntax of \pkg{listings}.
3499 % \begin{macrocode}
3500 Directive
3501 }
3502 % \end{macrocode}
3503 %
3504 %
3505 %
3506 % \begin{macrocode}
3507 \clist_map_inline:Nn \g_@@_styles_clist
3508 {
3509 \keys_define:nn { piton / Styles }
3510 {
3511 #1 .value_required:n = true ,
3512 #1 .code:n =
3513 \tl_set:cn
3514 {
3515 pitonStyle _
3516 \str_if_empty:NF \l_@@_SetPitonStyle_option_str
3517 { \l_@@_SetPitonStyle_option_str _ }
3518 #1
3519 }
3520 { ##1 }
3521 }
3522 }
3523
3524 \keys_define:nn { piton / Styles }
3525 {
3526 String .meta:n = { String.Long = #1 , String.Short = #1 } ,
3527 String .value_required:n = true ,
3528 Comment.Math .tl_set:c = pitonStyle _ Comment.Math ,
3529 Comment.Math .value_required:n = true ,
3530 unknown .code:n = \@@_unknown_style:
3531 }
3532 % \end{macrocode}
3533 %
3534 % \bigskip
3535 % For the langage |expl|, it's possible to create ``on the fly'' some styles
3536 % of the form \texttt{Module.\textsl{name}} or \texttt{Type.\textsl{name}}.
3537 % For the other languages, it's not possible.
3538 % \begin{macrocode}
3539 \cs_new_protected:Npn \@@_unknown_style:
3540 {
3541 \str_if_eq:eeTF \l_@@_SetPitonStyle_option_str { expl }
3542 {
3543 \seq_set_split:Nne \l_tmpa_seq { . } \l_keys_key_str
3544 \seq_get_left:NN \l_tmpa_seq \l_tmpa_str
3545 % \end{macrocode}
3546 % Now, the first part of the key (before the first period) is stored in |\l_tmpa_str|.
3547 % \begin{macrocode}
3548 \bool_lazy_and:nnTF
3549 { \int_compare_p:nNn { \seq_count:N \l_tmpa_seq } > { 1 } }
3550 {
3551 \str_if_eq_p:Vn \l_tmpa_str { Module }
3552 ||
3553 \str_if_eq_p:Vn \l_tmpa_str { Type }
3554 }
3555 % \end{macrocode}
3556 % Now, we will create a new style.
3557 % \begin{macrocode}
3558 { \tl_set:co { pitonStyle _ expl _ \l_keys_key_str } \l_keys_value_tl }
3559 { \@@_error:n { Unknown~key~for~SetPitonStyle } }
3560 }
3561 { \@@_error:n { Unknown~key~for~SetPitonStyle } }
3562 }
3563 % \end{macrocode}
3564 %
3565 % \bigskip
3566 % \begin{macrocode}
3567 \SetPitonStyle[OCaml]
3568 {
3569 TypeExpression =
3570 {
3571 \SetPitonStyle [ OCaml ]
3572 {
3573 Identifier = \PitonStyle { Name.Type } ,
3574 Name.Builtin = \PitonStyle { Name.Type}
3575 }
3576 \@@_piton:n
3577 }
3578 }
3579 % \end{macrocode}
3580 %
3581 % \bigskip
3582 % We add the word |String| to the list of the styles because we will use that
3583 % list in the error message for an unknown key in |\SetPitonStyle|.
3584 %
3585 % \begin{macrocode}
3586 \clist_gput_left:Nn \g_@@_styles_clist { String }
3587 % \end{macrocode}
3588 %
3589 % \bigskip
3590 % Of course, we sort that clist.
3591 % \begin{macrocode}
3592 \clist_gsort:Nn \g_@@_styles_clist
3593 {
3594 \str_compare:nNnTF { #1 } < { #2 }
3595 \sort_return_same:
3596 \sort_return_swapped:
3597 }
3598 % \end{macrocode}
3599 %
3600 % \bigskip
3601 % \begin{macrocode}
3602 \cs_set_eq:NN \@@_break_strings_anywhere:n \prg_do_nothing:
3603
3604 \cs_set_eq:NN \@@_break_numbers_anywhere:n \prg_do_nothing:
3605
3606 \cs_new_protected:Npn \@@_actually_break_anywhere:n #1
3607 {
3608 \tl_set:Nn \l_tmpa_tl { #1 }
3609 % \end{macrocode}
3610 % We have to begin by a substitution for the spaces. Otherwise, they would be
3611 % gobbled in the |\tl_map_inline:Nn|.
3612 % \begin{macrocode}
3613 \tl_replace_all:NVn \l_tmpa_tl \c_catcode_other_space_tl \space
3614 \seq_clear:N \l_tmpa_seq
3615 \tl_map_inline:Nn \l_tmpa_tl { \seq_put_right:Nn \l_tmpa_seq { ##1 } }
3616 \seq_use:Nn \l_tmpa_seq { \- }
3617 }
3618 % \end{macrocode}
3619 %
3620 % \bigskip
3621 % \begin{macrocode}
3622 \cs_new_protected:Npn \@@_comment:n #1
3623 { \PitonStyle { Comment } { \PitonSpaceSubstitute { #1 } } }
3624 % \end{macrocode}
3625 %
3626 % \bigskip
3627 % We use a standard name for the following command (and not a internal name of L3
3628 % such as |\@@_space_substitute:n| because it will be inserted in the main LaTeX stream
3629 % by Lua when |morecomment| is used in |\NewPitonLanguage|.
3630 %
3631 % We should probably change that with an ``internal style''.
3632 % \begin{macrocode}
3633 \cs_new_protected:Npn \PitonSpaceSubstitute #1 % noqa
3634 {
3635 \bool_if:NTF \l_@@_break_lines_in_Piton_bool
3636 {
3637 \tl_set:Nn \l_tmpa_tl { #1 }
3638 \tl_replace_all:NVn \l_tmpa_tl
3639 \c_catcode_other_space_tl
3640 \@@_breakable_space:
3641 \l_tmpa_tl
3642 }
3643 { #1 }
3644 }
3645 % \end{macrocode}
3646 %
3647 % \bigskip
3648 % \begin{macrocode}
3649 \cs_new_protected:Npn \@@_string_long:n #1
3650 {
3651 \PitonStyle { String.Long }
3652 {
3653 \bool_if:NTF \l_@@_break_strings_anywhere_bool
3654 { \@@_actually_break_anywhere:n { #1 } }
3655 {
3656 % \end{macrocode}
3657 % We have, when |break-lines-in-Piton| is in force, to replace the spaces by
3658 % |\@@_breakable_space:| because, when we have done a similar job in
3659 % |\@@_replace_spaces:n| used in |\@@_begin_line:|, that job was not able to do
3660 % the replacement in the brace group |{...}| of |\PitonStyle{String.Long}{...}|
3661 % because we used a |\tl_replace_all:NVn|. At that time, it would have been
3662 % possible to use a |\tl_regex_replace_all:Nnn| but it is notoriously slow.
3663 % \begin{macrocode}
3664 \bool_if:NTF \l_@@_break_lines_in_Piton_bool
3665 {
3666 \tl_set:Nn \l_tmpa_tl { #1 }
3667 \tl_replace_all:NVn \l_tmpa_tl
3668 \c_catcode_other_space_tl
3669 \@@_breakable_space:
3670 \l_tmpa_tl
3671 }
3672 { #1 }
3673 }
3674 }
3675 }
3676 \cs_new_protected:Npn \@@_string_short:n #1
3677 {
3678 \PitonStyle { String.Short }
3679 {
3680 \bool_if:NT \l_@@_break_strings_anywhere_bool
3681 { \@@_actually_break_anywhere:n }
3682 { #1 }
3683 }
3684 }
3685 \cs_new_protected:Npn \@@_string_doc:n #1
3686 {
3687 \PitonStyle { String.Doc }
3688 {
3689 \bool_if:NTF \l_@@_break_lines_in_Piton_bool
3690 {
3691 \tl_set:Nn \l_tmpa_tl { #1 }
3692 \tl_replace_all:NVn \l_tmpa_tl
3693 \c_catcode_other_space_tl
3694 \@@_breakable_space:
3695 \l_tmpa_tl
3696 }
3697 { #1 }
3698 }
3699 }
3700 \cs_new_protected:Npn \@@_number:n #1
3701 {
3702 \PitonStyle { Number }
3703 {
3704 \bool_if:NT \l_@@_break_numbers_anywhere_bool
3705 { \@@_actually_break_anywhere:n }
3706 { #1 }
3707 }
3708 }
3709 % \end{macrocode}
3710 %
3711 % \bigskip
3712 % \subsection{The initial styles}
3713 %
3714 % The initial styles are inspired by the style ``manni'' of Pygments.
3715 %
3716 % \medskip
3717 % \begin{macrocode}
3718 \SetPitonStyle
3719 {
3720 Comment = \color [ HTML ] { 0099FF } \itshape ,
3721 Comment.Internal = \@@_comment:n ,
3722 Exception = \color [ HTML ] { CC0000 } ,
3723 Keyword = \color [ HTML ] { 006699 } \bfseries ,
3724 Keyword.Governing = \color [ HTML ] { 006699 } \bfseries ,
3725 Keyword.Constant = \color [ HTML ] { 006699 } \bfseries ,
3726 Name.Builtin = \color [ HTML ] { 336666 } ,
3727 Name.Decorator = \color [ HTML ] { 9999FF },
3728 Name.Class = \color [ HTML ] { 00AA88 } \bfseries ,
3729 Name.Function = \color [ HTML ] { CC00FF } ,
3730 Name.Namespace = \color [ HTML ] { 00CCFF } ,
3731 Name.Constructor = \color [ HTML ] { 006000 } \bfseries ,
3732 Name.Field = \color [ HTML ] { AA6600 } ,
3733 Name.Module = \color [ HTML ] { 0060A0 } \bfseries ,
3734 Name.Table = \color [ HTML ] { 309030 } ,
3735 Number = \color [ HTML ] { FF6600 } ,
3736 Number.Internal = \@@_number:n ,
3737 Operator = \color [ HTML ] { 555555 } ,
3738 Operator.Word = \bfseries ,
3739 String = \color [ HTML ] { CC3300 } ,
3740 String.Long.Internal = \@@_string_long:n ,
3741 String.Short.Internal = \@@_string_short:n ,
3742 String.Doc.Internal = \@@_string_doc:n ,
3743 String.Doc = \color [ HTML ] { CC3300 } \itshape ,
3744 String.Interpol = \color [ HTML ] { AA0000 } ,
3745 Comment.LaTeX = \normalfont \color [ rgb ] { .468, .532, .6 } ,
3746 Name.Type = \color [ HTML ] { 336666 } ,
3747 InitialValues = \@@_piton:n ,
3748 Interpol.Inside = { \l_@@_font_command_tl \@@_piton:n } ,
3749 TypeParameter = \color [ HTML ] { 336666} \itshape ,
3750 Preproc = \color [ HTML ] { AA6600} \slshape ,
3751 % \end{macrocode}
3752 % We need the command |\@@_identifier:n| because of the command
3753 % |\SetPitonIdentifier|. The command |\@@_identifier:n| will potentially call
3754 % the style |Identifier| (which is a user-style, not an internal style).
3755 % \begin{macrocode}
3756 Identifier.Internal = \@@_identifier:n ,
3757 Identifier = ,
3758 Directive = \color [ HTML ] { AA6600} ,
3759 Tag = \colorbox { gray!10 } ,
3760 UserFunction = \PitonStyle { Identifier } ,
3761 Prompt = ,
3762 Discard = \use_none:n
3763 }
3764 % \end{macrocode}
3765 % \bigskip
3766 % \subsection{Styles specific to the language expl}
3767 %
3768 % \begin{macrocode}
3769 \clist_new:N \g_@@_expl_styles_clist
3770 \clist_gset:Nn \g_@@_expl_styles_clist
3771 {
3772 Scope.l ,
3773 Scope.g ,
3774 Scope.c
3775 }
3776 % \end{macrocode}
3777 %
3778 % \begin{macrocode}
3779 \clist_map_inline:Nn \g_@@_expl_styles_clist
3780 {
3781 \keys_define:nn { piton / Styles }
3782 {
3783 #1 .value_required:n = true ,
3784 #1 .code:n =
3785 \tl_set:cn
3786 {
3787 pitonStyle _
3788 \str_if_empty:NF \l_@@_SetPitonStyle_option_str
3789 { \l_@@_SetPitonStyle_option_str _ }
3790 #1
3791 }
3792 { ##1 }
3793 }
3794 }
3795 % \end{macrocode}
3796 %
3797 % \begin{macrocode}
3798 \SetPitonStyle [ expl ]
3799 {
3800 Scope.l = ,
3801 Scope.g = \bfseries ,
3802 Scope.c = \slshape ,
3803 Type.bool = \color [ HTML ] { AA6600} ,
3804 Type.box = \color [ HTML ] { 267910 } ,
3805 Type.clist = \color [ HTML ] { 309030 } ,
3806 Type.fp = \color [ HTML ] { FF3300 } ,
3807 Type.int = \color [ HTML ] { FF6600 } ,
3808 Type.seq = \color [ HTML ] { 309030 } ,
3809 Type.skip = \color [ HTML ] { 0CC060 } ,
3810 Type.str = \color [ HTML ] { CC3300 } ,
3811 Type.tl = \color [ HTML ] { AA2200 } ,
3812 Module.bool = \color [ HTML ] { AA6600} ,
3813 Module.box = \color [ HTML ] { 267910 } ,
3814 Module.cs = \bfseries \color [ HTML ] { 006699 } ,
3815 Module.exp = \bfseries \color [ HTML ] { 404040 } ,
3816 Module.hbox = \color [ HTML ] { 267910 } ,
3817 Module.prg = \bfseries ,
3818 Module.clist = \color [ HTML ] { 309030 } ,
3819 Module.fp = \color [ HTML ] { FF3300 } ,
3820 Module.int = \color [ HTML ] { FF6600 } ,
3821 Module.seq = \color [ HTML ] { 309030 } ,
3822 Module.skip = \color [ HTML ] { 0CC060 } ,
3823 Module.str = \color [ HTML ] { CC3300 } ,
3824 Module.tl = \color [ HTML ] { AA2200 } ,
3825 Module.vbox = \color [ HTML ] { 267910 }
3826 }
3827 % \end{macrocode}
3828 %
3829 % \medskip
3830 % If the key |math-comments| has been used in the preamble of the LaTeX
3831 % document, we change the style |Comment.Math| which should be considered only
3832 % at an ``internal style''. However, maybe we will document in a future version
3833 % the possibility to write change the style \emph{locally} in a document)].
3834 % \begin{macrocode}
3835 \hook_gput_code:nnn { begindocument } { . }
3836 {
3837 \bool_if:NT \g_@@_math_comments_bool
3838 { \SetPitonStyle { Comment.Math = \@@_math_scantokens:n } }
3839 }
3840 % \end{macrocode}
3841 %
3842 %
3843 % \bigskip
3844 % \subsection{Highlighting some identifiers}
3845 %
3846 %
3847 % \medskip
3848 % \begin{macrocode}
3849 \NewDocumentCommand { \SetPitonIdentifier } { o m m }
3850 {
3851 \clist_set:Nn \l_tmpa_clist { #2 }
3852 \tl_if_novalue:nTF { #1 }
3853 {
3854 \clist_map_inline:Nn \l_tmpa_clist
3855 { \cs_set:cpn { PitonIdentifier _ ##1 } { #3 } }
3856 }
3857 {
3858 \str_set:Ne \l_tmpa_str { \str_lowercase:n { #1 } }
3859 \str_if_eq:onT \l_tmpa_str { current-language }
3860 { \str_set_eq:NN \l_tmpa_str \l_piton_language_str }
3861 \clist_map_inline:Nn \l_tmpa_clist
3862 { \cs_set:cpn { PitonIdentifier _ \l_tmpa_str _ ##1 } { #3 } }
3863 }
3864 }
3865 % \end{macrocode}
3866 %
3867 % \begin{macrocode}
3868 \cs_new_protected:Npn \@@_identifier:n #1
3869 {
3870 \str_set:Nn \l_tmpa_str { #1 }
3871 \cs_if_exist_use:cF { PitonIdentifier _ \l_piton_language_str _ \l_tmpa_str }
3872 {
3873 \cs_if_exist_use:cF { PitonIdentifier _ \l_tmpa_str }
3874 { \PitonStyle { Identifier } }
3875 }
3876 { #1 }
3877 }
3878 % \end{macrocode}
3879 %
3880 %
3881 % \bigskip
3882 % In particular, we have an highlighting of the identifiers which are the
3883 % names of Python functions previously defined by the user. Indeed, when a
3884 % Python function is defined, the style |Name.Function.Internal| is applied to
3885 % that name. We define now that style (we define it directly and we short-cut
3886 % the function |\SetPitonStyle|).
3887 % \begin{macrocode}
3888 \cs_new_protected:cpn { pitonStyle _ Name.Function.Internal } #1
3889 {
3890 % \end{macrocode}
3891 % First, the element is composed in the TeX flow with the style |Name.Function|
3892 % which is provided to the end user.
3893 % \begin{macrocode}
3894 { \PitonStyle { Name.Function } { #1 } }
3895 \str_set:Nn \l_tmpa_str { #1 }
3896 % \end{macrocode}
3897 % Now, we specify that the name of the new Python function is a known identifier
3898 % that will be formatted with the Piton style |UserFunction|. Of course,
3899 % here the affectation is global because we have to exit many groups and even
3900 % the environments |{Piton}|).
3901 % \begin{macrocode}
3902 \cs_gset_protected:cpn { PitonIdentifier _ \l_piton_language_str _ \l_tmpa_str }
3903 { \PitonStyle { UserFunction } }
3904 % \end{macrocode}
3905 % Now, we put the name of that new user function in the dedicated sequence
3906 % (specific of the current language). {\bfseries That sequence will be used only
3907 % by |\PitonClearUserFunctions|}.
3908 % \begin{macrocode}
3909 \seq_if_exist:cF { g_@@_functions _ \l_piton_language_str _ seq }
3910 { \seq_new:c { g_@@_functions _ \l_piton_language_str _ seq } }
3911 \seq_gput_right:co { g_@@_functions _ \l_piton_language_str _ seq } \l_tmpa_str
3912 % \end{macrocode}
3913 % We update |\g_@@_languages_seq| which is used only by the command
3914 % |\PitonClearUserFunctions| when it's used without its optional argument.
3915 % \begin{macrocode}
3916 \seq_if_in:NoF \g_@@_languages_seq { \l_piton_language_str }
3917 { \seq_gput_left:No \g_@@_languages_seq { \l_piton_language_str } }
3918 }
3919 % \end{macrocode}
3920 %
3921 % \bigskip
3922 % \begin{macrocode}
3923 \NewDocumentCommand \PitonClearUserFunctions { ! o }
3924 {
3925 \tl_if_novalue:nTF { #1 }
3926 % \end{macrocode}
3927 % If the command is used without its optional argument, we will deleted the
3928 % user language for all the computer languages.
3929 % \begin{macrocode}
3930 { \@@_clear_all_functions: }
3931 { \@@_clear_list_functions:n { #1 } }
3932 }
3933 % \end{macrocode}
3934 %
3935 % \bigskip
3936 % \begin{macrocode}
3937 \cs_new_protected:Npn \@@_clear_list_functions:n #1
3938 {
3939 \clist_set:Nn \l_tmpa_clist { #1 }
3940 \clist_map_function:NN \l_tmpa_clist \@@_clear_functions_i:n
3941 \clist_map_inline:nn { #1 }
3942 { \seq_gremove_all:Nn \g_@@_languages_seq { ##1 } }
3943 }
3944 % \end{macrocode}
3945 %
3946 % \bigskip
3947 % \begin{macrocode}
3948 \cs_new_protected:Npn \@@_clear_functions_i:n #1
3949 { \@@_clear_functions_ii:n { \str_lowercase:n { #1 } } }
3950 % \end{macrocode}
3951 %
3952 % The following command clears the list of the user-defined functions for the
3953 % language provided in argument (mandatory in lower case).
3954 % \begin{macrocode}
3955 \cs_new_protected:Npn \@@_clear_functions_ii:n #1
3956 {
3957 \seq_if_exist:cT { g_@@_functions _ #1 _ seq }
3958 {
3959 \seq_map_inline:cn { g_@@_functions _ #1 _ seq }
3960 { \cs_undefine:c { PitonIdentifier _ #1 _ ##1} }
3961 \seq_gclear:c { g_@@_functions _ #1 _ seq }
3962 }
3963 }
3964 \cs_generate_variant:Nn \@@_clear_functions_ii:n { e }
3965 % \end{macrocode}
3966 %
3967 % \bigskip
3968 % \begin{macrocode}
3969 \cs_new_protected:Npn \@@_clear_functions:n #1
3970 {
3971 \@@_clear_functions_i:n { #1 }
3972 \seq_gremove_all:Nn \g_@@_languages_seq { #1 }
3973 }
3974 % \end{macrocode}
3975 %
3976 % \bigskip
3977 % The following command clears all the user-defined functions for all the
3978 % computer languages.
3979 % \begin{macrocode}
3980 \cs_new_protected:Npn \@@_clear_all_functions:
3981 {
3982 \seq_map_function:NN \g_@@_languages_seq \@@_clear_functions_i:n
3983 \seq_gclear:N \g_@@_languages_seq
3984 }
3985 % \end{macrocode}
3986 %
3987 %
3988 % \bigskip
3989 % \begin{macrocode}
3990 \AtEndDocument
3991 {
3992 % \end{macrocode}
3993 % For the files written on the disk (with the key |write|), all the job is done by
3994 % Lua.
3995 % \begin{macrocode}
3996 \lua_now:n { piton.write_files_now ( ) }
3997 % \end{macrocode}
3998 % For the files joined in the \textsc{pdf}, we have a modern version which uses
3999 % the package \pkg{pdfmanagement} of LaTeX and a legacy mechanism.
4000 % \begin{macrocode}
4001 \IfPDFManagementActiveTF
4002 { \@@_join_files: }
4003 { \@@_join_files_legacy: }
4004 }
4005 % \end{macrocode}
4006 %
4007 %
4008 % \bigskip
4009 % If the new pakcage \pkg{pdfmanagement} is used, we insert the file directly in the
4010 % catalog of the \textsc{pdf} file.
4011 % \begin{macrocode}
4012 \cs_new_protected:Npn \@@_join_files:
4013 {
4014 \seq_map_inline:Nn \g_@@_join_seq
4015 {
4016 \lua_now:n { pdf.immediateobj ( "stream" , piton.join_files["##1"] ) }
4017 \str_set_convert:Nnnn \l_tmpa_str { ##1 } { } { utf16/hex }
4018 \pdfmanagement_add:nne { Catalog / Names } { EmbeddedFiles }
4019 {
4020 <<
4021 /Type /Filespec
4022 /UF <\l_tmpa_str>
4023 /EF << /F~\pdf_object_ref_last: >>
4024 /Desc (Computer~listing)
4025 /AFRelationship /Supplement
4026 >>
4027 }
4028 }
4029 }
4030 % \end{macrocode}
4031 %
4032 %
4033 % \bigskip
4034 % The legacy version of |\@@_join_files:| will be used when the new
4035 % package \pkg{pdfmanagement} is \emph{not} used. It that case, we can't
4036 % insert the file directly in the catalog of the \text{pdf} file. Therefore, we
4037 % insert the file linked to a annotation in a page of the \textsc{pdf} file. We
4038 % try to make the annotation itself invisible with several technics.
4039 % \begin{macrocode}
4040 \cs_new_protected:Npn \@@_join_files_legacy:
4041 {
4042 \seq_map_inline:Nn \g_@@_join_seq
4043 {
4044 \str_set_convert:Nnnn \l_tmpa_str { ##1 } { } { utf16/hex }
4045 \lua_now:n { pdf.immediateobj ( "stream" , piton.join_files["##1"] ) }
4046 \pdfextension annot~width~0pt~height~0pt~depth~0pt
4047 % \end{macrocode}
4048 % The entry |/F| in the \textsc{pdf} dictionnary of the annotation is an
4049 % unsigned 32-bit integer containing flags specifying various characteristics of
4050 % the annotation. The bit in position 2 means \emph{Hidden}.
4051 % However, despite that bit which means \emph{Hidden}, some \textsc{pdf} readers
4052 % show the annotation. That's why we have used |width 0pt height 0pt depth 0pt|.
4053 % \begin{macrocode}
4054 {
4055 /Subtype /FileAttachment
4056 /F~2
4057 /Name /Paperclip
4058 /Contents (Computer~listing)
4059 /FS <<
4060 /Type /Filespec
4061 % \end{macrocode}
4062 % We have previously converted the name of the embedded file in |utf16/hex| with
4063 % the \textsc{bom} of big endian and now we can write a \textsc{pdf} string
4064 % between |<| and |>| (with that encoding).
4065 % \begin{macrocode}
4066 /UF <\l_tmpa_str>
4067 % \end{macrocode}
4068 % It would have been possible to write |\pdffeedback lastobj~0~R| instead
4069 % |\pdf_object_ref_last:| since LuaTeX is the only engine allowed by \pkg{piton}. Remark
4070 % that |\pdf_object_ref_last:| is in the LaTeX kernel (not in the package |pdfmanagement|).
4071 % \begin{macrocode}
4072 /EF << /F~\pdf_object_ref_last: >>
4073 /AFRelationship /Supplement
4074 >>
4075 }
4076 }
4077 }
4078 % \end{macrocode}
4079 %
4080 %
4081 % \bigskip
4082 %
4083 % \subsection{Spaces of indentation}
4084 %
4085 % \bigskip
4086 % \begin{macrocode}
4087 \cs_new_protected:Npn \@@_define_leading_space_normal:
4088 {
4089 \cs_set_protected:Npn \@@_leading_space:
4090 {
4091 \int_gincr:N \g_@@_indentation_int
4092 % \end{macrocode}
4093 % Be careful: the |\hbox:n| is mandatory.
4094 % \begin{macrocode}
4095 \hbox:n { ~ }
4096 }
4097 }
4098 % \end{macrocode}
4099 %
4100 % \begin{macrocode}
4101 \cs_new_protected:Npn \@@_define_leading_space_Foxit:
4102 {
4103 \cs_set_protected:Npn \@@_leading_space:
4104 {
4105 \int_gincr:N \g_@@_indentation_int
4106 \pdfextension literal { /Artifact << /ActualText (\space) >> BDC }
4107 {
4108 \color { white }
4109 \transparent { 0 }
4110 . % previously : ␣ U+2423
4111 }
4112 \pdfextension literal { EMC }
4113 }
4114 }
4115 \@@_define_leading_space_Foxit:
4116 % \end{macrocode}
4117 %
4118 %
4119 % \bigskip
4120 % \subsection{Security}
4121 %
4122 % \begin{macrocode}
4123 \AddToHook { env / piton / before }
4124 { \@@_fatal:n { No~environment~piton } }
4125 % \end{macrocode}
4126 %
4127 %
4128 % \bigskip
4129 % \subsection{The error messages of the package}
4130 %
4131 %
4132 % When there is a unknown key, we try a ``normal form'' of the key and, when
4133 % that normal form exists, we add that information in the error message.
4134 %
4135 % The normal form is the lower case form of the key, with all the spaces
4136 % replaced by hyphens (there is never spaces in the keys of \pkg{piton}).
4137 %
4138 % |#1| is a clist of names of sets of keys and |#2| is the error message
4139 % to send.
4140 % \begin{macrocode}
4141 \cs_new_protected:Npn \@@_unknown_key:nn #1 #2
4142 {
4143 \str_set_eq:NN \l_tmpa_str \l_keys_key_str
4144 \str_replace_all:Nnn \l_tmpa_str { ~ } { - }
4145 \str_set:Ne \l_tmpa_str { \str_lowercase:f { \l_tmpa_str } }
4146 \bool_set_false:N \l_tmpa_bool
4147 \clist_map_inline:nn { #1 }
4148 {
4149 \keys_if_exist:neT { ##1 } { \l_tmpa_str }
4150 {
4151 \@@_error:n { key~with~normal~form~exists }
4152 \bool_set_true:N \l_tmpa_bool
4153 \clist_map_break:
4154 }
4155 }
4156 \bool_if:NF \l_tmpa_bool { \@@_error:n { #2 } }
4157 }
4158 % \end{macrocode}
4159 %
4160 % \begin{macrocode}
4161 \@@_msg_new:nn { key~with~normal~form~exists }
4162 {
4163 The~key~'\l_keys_key_str'~does~not~exists.~It~will~be~ignored.\\
4164 Maybe~you~want~to~use~the~key~'\l_tmpa_str'.
4165 }
4166 % \end{macrocode}
4167 %
4168 %
4169 % \begin{macrocode}
4170 \@@_msg_new:nn { No~environment~piton }
4171 {
4172 There~is~no~environment~piton!\\
4173 There~is~an~environment~{Piton}~and~a~command~
4174 \token_to_str:N \piton\ but~there~is~no~environment~
4175 {piton}.
4176 }
4177 % \end{macrocode}
4178 %
4179 % \begin{macrocode}
4180 \@@_msg_new:nn { rounded-corners~without~Tikz }
4181 {
4182 TikZ~not~used \\
4183 You~can't~use~the~key~'rounded-corners'~because~
4184 you~have~not~loaded~the~package~TikZ.~
4185 If~you~go~on,~your~key~will~be~ignored.~
4186 You~won't~have~similar~error~till~the~end~of~the~document.
4187 }
4188 % \end{macrocode}
4189 %
4190 % \begin{macrocode}
4191 \@@_msg_new:nn { tcolorbox~not~loaded }
4192 {
4193 tcolorbox~not~loaded \\
4194 You~can't~use~the~key~'tcolorbox'~because~
4195 you~have~not~loaded~the~package~tcolorbox.~
4196 Use~\token_to_str:N \usepackage[breakable]{tcolorbox}.~
4197 If~you~go~on,~that~key~will~be~ignored.
4198 }
4199 % \end{macrocode}
4200 %
4201 % \begin{macrocode}
4202 \@@_msg_new:nn { library~breakable~not~loaded }
4203 {
4204 breakable~not~loaded \\
4205 You~can't~use~the~key~'tcolorbox'~because~
4206 you~have~not~loaded~the~library~'breakable'~of~tcolorbox'.~
4207 Use~\token_to_str:N \tcbuselibrary{breakable}~in~the~preamble~
4208 of~your~document.~
4209 If~you~go~on,~that~key~will~be~ignored.
4210 }
4211 % \end{macrocode}
4212 %
4213 % \begin{macrocode}
4214 \@@_msg_new:nn { Language~not~defined }
4215 {
4216 Language~not~defined \\
4217 The~language~'\l_tmpa_tl'~has~not~been~defined~previously.~
4218 If~you~go~on,~your~command~\token_to_str:N \NewPitonLanguage\
4219 will~be~ignored.
4220 }
4221 % \end{macrocode}
4222 %
4223 %
4224 % \begin{macrocode}
4225 \@@_msg_new:nn { bad~version~of~piton.lua }
4226 {
4227 Bad~number~version~of~'piton.lua'\\
4228 The~file~'piton.lua'~loaded~has~not~the~same~number~of~
4229 version~as~the~file~'piton.sty'.~You~can~go~on~but~you~should~
4230 address~that~issue.
4231 }
4232 % \end{macrocode}
4233 %
4234 %
4235 % \begin{macrocode}
4236 \@@_msg_new:nn { Unknown~key~NewPitonLanguage }
4237 {
4238 Unknown~key~for~\token_to_str:N \NewPitonLanguage.\\
4239 The~key~'\l_keys_key_str'~is~unknown.~
4240 This~key~will~be~ignored.
4241 }
4242 % \end{macrocode}
4243 %
4244 % \begin{macrocode}
4245 \@@_msg_new:nn { Unknown~key~for~SetPitonStyle }
4246 {
4247 The~style~'\l_keys_key_str'~is~unknown.\\
4248 This~setting~will~be~ignored.~
4249 The~available~styles~are~(in~alphabetic~order):~
4250 \clist_use:Nnnn \g_@@_styles_clist { ~and~ } { ,~ } { ~and~ }.
4251 }
4252 % \end{macrocode}
4253 %
4254 % \begin{macrocode}
4255 \@@_msg_new:nn { Invalid~key }
4256 {
4257 Wrong~use~of~key.\\
4258 You~can't~use~the~key~'\l_keys_key_str'~here.~
4259 Your~key~will~be~ignored.
4260 }
4261 % \end{macrocode}
4262 %
4263 % \begin{macrocode}
4264 \@@_msg_new:nn { Unknown~key~for~line-numbers }
4265 {
4266 Unknown~key. \\
4267 The~key~'line-numbers / \l_keys_key_str'~is~unknown.~
4268 The~available~keys~of~the~family~'line-numbers'~are~(in~
4269 alphabetic~order):~
4270 absolute,~false,~label-empty-lines,~lmmono10-drawn,~
4271 ~position,~resume,~skip-empty-lines,~
4272 sep,~start~and~true.~
4273 Your~key~will~be~ignored.
4274 }
4275 % \end{macrocode}
4276 %
4277 % \begin{macrocode}
4278 \@@_msg_new:nn { Unknown~key~for~marker }
4279 {
4280 Unknown~key. \\
4281 The~key~'marker / \l_keys_key_str'~is~unknown.~
4282 The~available~keys~of~the~family~'marker'~are~(in~
4283 alphabetic~order):~ beginning,~end~and~include-lines.~
4284 Your~key~will~be~ignored.
4285 }
4286 % \end{macrocode}
4287 %
4288 % \begin{macrocode}
4289 \@@_msg_new:nn { bad~range~specification }
4290 {
4291 Incompatible~keys.\\
4292 You~can't~specify~the~range~of~lines~to~include~by~using~both~
4293 markers~and~explicit~number~of~lines.~
4294 Your~whole~file~'\l_@@_file_name_str'~will~be~included.
4295 }
4296 % \end{macrocode}
4297 %
4298 % \begin{macrocode}
4299 \cs_new_nopar:Nn \@@_thepage:
4300 {
4301 \thepage
4302 \cs_if_exist:NT \insertframenumber
4303 {
4304 ~(frame~\insertframenumber
4305 \cs_if_exist:NT \beamer@slidenumber { ,~slide~\insertslidenumber }
4306 )
4307 }
4308 }
4309 % \end{macrocode}
4310 %
4311 % We don't give the name |syntax error| for the following error because you
4312 % should not give a name with a space because such space could be replaced by
4313 % U+2423 when the key |show-spaces| is in force in the command |\piton|.
4314 % \begin{macrocode}
4315 \@@_msg_new:nn { SyntaxError }
4316 {
4317 Syntax~Error~on~page~\@@_thepage:.\\
4318 Your~code~of~the~language~'\l_piton_language_str'~is~not~
4319 syntactically~correct.~
4320 It~won't~be~printed~in~the~PDF~file.
4321 }
4322 % \end{macrocode}
4323 %
4324 % \begin{macrocode}
4325 \@@_msg_new:nn { FileError }
4326 {
4327 File~Error.\\
4328 It's~not~possible~to~write~on~the~file~'#1' \\
4329 \sys_if_shell_unrestricted:F
4330 { (try~to~compile~with~'lualatex~-shell-escape').\\ }
4331 If~you~go~on,~nothing~will~be~written~on~that~file.
4332 }
4333 % \end{macrocode}
4334 %
4335 % \begin{macrocode}
4336 \@@_msg_new:nn { InexistentDirectory }
4337 {
4338 Inexistent~directory.\\
4339 The~directory~'\l_@@_path_write_str'~
4340 given~in~the~key~'path-write'~does~not~exist.~
4341 Nothing~will~be~written~on~'\l_@@_write_str'.
4342 }
4343 % \end{macrocode}
4344 %
4345 % \begin{macrocode}
4346 \@@_msg_new:nn { begin~marker~not~found }
4347 {
4348 Marker~not~found.\\
4349 The~range~'\l_@@_begin_range_str'~provided~to~the~
4350 command~\token_to_str:N \PitonInputFile\ has~not~been~found.~
4351 The~whole~file~'\l_@@_file_name_str'~will~be~inserted.
4352 }
4353 % \end{macrocode}
4354 %
4355 %
4356 % \begin{macrocode}
4357 \@@_msg_new:nn { end~marker~not~found }
4358 {
4359 Marker~not~found.\\
4360 The~marker~of~end~of~the~range~'\l_@@_end_range_str'~
4361 provided~to~the~command~\token_to_str:N \PitonInputFile\
4362 has~not~been~found.~The~file~'\l_@@_file_name_str'~will~
4363 be~inserted~till~the~end.
4364 }
4365 % \end{macrocode}
4366 %
4367 %
4368 % \begin{macrocode}
4369 \@@_msg_new:nn { Unknown~file }
4370 {
4371 Unknown~file. \\
4372 The~file~'#1'~is~unknown.~
4373 Your~command~\token_to_str:N \PitonInputFile\ will~be~ignored.
4374 }
4375 % \end{macrocode}
4376 %
4377 % \begin{macrocode}
4378 \cs_new_protected:Npn \@@_error_if_not_in_beamer:
4379 {
4380 \bool_if:NF \g_@@_beamer_bool
4381 { \@@_error_or_warning:n { Without~beamer } }
4382 }
4383 % \end{macrocode}
4384 %
4385 % \begin{macrocode}
4386 \@@_msg_new:nn { Without~beamer }
4387 {
4388 Key~'\l_keys_key_str'~without~Beamer.\\
4389 You~should~not~use~the~key~'\l_keys_key_str'~since~you~
4390 are~not~in~Beamer.~However,~you~can~go~on.
4391 }
4392 % \end{macrocode}
4393 %
4394 % \begin{macrocode}
4395 \@@_msg_new:nn { rowcolor~in~detected-commands }
4396 {
4397 'rowcolor'~forbidden~in~'detected-commands'.\\
4398 You~should~put~'rowcolor'~in~'raw-detected-commands'.~
4399 That~key~will~be~ignored.
4400 }
4401 % \end{macrocode}
4402 %
4403 % \begin{macrocode}
4404 \@@_msg_new:nnn { Unknown~key~for~PitonOptions }
4405 {
4406 Unknown~key. \\
4407 The~key~'\l_keys_key_str'~is~unknown~for~\token_to_str:N \PitonOptions.~
4408 It~will~be~ignored.~
4409 For~a~list~of~the~available~keys,~type~H~<return>.
4410 }
4411 {
4412 The~available~keys~are~(in~alphabetic~order):~
4413 annotation,~
4414 add-to-split-separation,~
4415 auto-gobble,~
4416 background-color,~
4417 begin-range,~
4418 box,~
4419 break-lines,~
4420 break-lines-in-piton,~
4421 break-lines-in-Piton,~
4422 break-numbers-anywhere,~
4423 break-strings-anywhere,~
4424 continuation-symbol,~
4425 continuation-symbol-on-indentation,~
4426 detected-beamer-commands,~
4427 detected-beamer-environments,~
4428 detected-commands,~
4429 end-of-broken-line,~
4430 end-range,~
4431 env-gobble,~
4432 env-used-by-split,~
4433 font-command(+),~
4434 gobble,~
4435 indent-broken-lines,~
4436 join,~
4437 label-as-zlabel,~
4438 language,~
4439 left-margin,~
4440 line-numbers/,~
4441 marker/,~
4442 math-comments,~
4443 no-join,~
4444 no-write,~
4445 path,~
4446 path-write,~
4447 print,~
4448 prompt-background-color,~
4449 raw-detected-commands,~
4450 resume,~
4451 right-margin,~
4452 rounded-corners,~
4453 show-spaces,~
4454 show-spaces-in-strings,~
4455 splittable,~
4456 splittable-on-empty-lines,~
4457 split-on-empty-lines,~
4458 split-separation,~
4459 tabs-auto-gobble,~
4460 tab-size,~
4461 tcolorbox,~
4462 varwidth,~
4463 vertical-detected-commands,~
4464 width~and~write.
4465 }
4466 % \end{macrocode}
4467 %
4468 % \bigskip
4469 % \begin{macrocode}
4470 \@@_msg_new:nn { label~with~lines~numbers }
4471 {
4472 You~can't~use~the~command~\token_to_str:N \label\
4473 or~\token_to_str:N \zlabel\
4474 because~the~key~'line-numbers'~
4475 is~not~active.~If~you~go~on,~that~command~will~ignored.
4476 }
4477 % \end{macrocode}
4478 %
4479 %
4480 % \bigskip
4481 % \begin{macrocode}
4482 \@@_msg_new:nn { overlay~without~beamer }
4483 {
4484 You~can't~use~an~argument~<...>~for~your~command~
4485 \token_to_str:N \PitonInputFile\ because~you~are~not~
4486 in~Beamer.~If~you~go~on,~that~argument~will~be~ignored.
4487 }
4488 % \end{macrocode}
4489 %
4490 % \bigskip
4491 %
4492 % \begin{macrocode}
4493 \@@_msg_new:nn { label~as~zlabel~needs~zref~package }
4494 {
4495 The~key~'label-as-zlabel'~requires~the~package~'zref'.~
4496 Please~load~the~package~'zref'~before~setting~the~key.
4497 }
4498 \hook_gput_code:nnn { begindocument } { . }
4499 {
4500 \bool_if:NT \g_@@_label_as_zlabel_bool
4501 {
4502 \IfPackageLoadedF { zref-base }
4503 { \@@_fatal:n { label~as~zlabel~needs~zref~package } }
4504 }
4505 }
4506 % \end{macrocode}
4507 %
4508 % \bigskip
4509 % \subsection{The glyphs of the 10 arabic numbers}
4510 %
4511 % \begin{macrocode}
4512 \cs_new_protected:Npn \@@_draw_number:n #1
4513 { \tl_map_inline:nn { #1 } { \use:c { c_@@_glyph_ ##1 _tl } } }
4514 \cs_generate_variant:Nn \@@_draw_number:n { e }
4515 % \end{macrocode}
4516 %
4517 %
4518 % \begin{macrocode}
4519 \cs_set_eq:NN \tlconstcn \tl_const:cn
4520 % \end{macrocode}
4521 %
4522 % \begin{macrocode}
4523 \ExplSyntaxOff
4524 \tlconstcn{c_@@_glyph_0_tl}
4525 {%
4526 \hbox to 5pt
4527 {%
4528 \hfill
4529 \pdfextension literal
4530 {
4531 4.24 3.05 m
4532 4.24 4.91 3.22 6.22 2.12 6.22 c
4533 1.00 6.22 0 4.88 0 3.06 c
4534 0 1.20 1.02 -0.11 2.12 -0.11 c
4535 3.24 -0.11 4.24 1.23 4.24 3.05 c
4536 3.55 3.16 m
4537 3.55 1.68 2.90 0.50 2.12 0.50 c
4538 1.34 0.50 0.69 1.68 0.69 3.16 c
4539 0.69 4.62 1.38 5.61 2.12 5.61 c
4540 2.85 5.61 3.55 4.63 3.55 3.16 c
4541 h f
4542 }%
4543 \hfill
4544 }%
4545 }
4546 % \end{macrocode}
4547 %
4548 %
4549 % \begin{macrocode}
4550 \tlconstcn{c_@@_glyph_1_tl}
4551 {%
4552 \hbox to 5pt
4553 {%
4554 \hfill \kern 1 pt
4555 \pdfextension literal
4556 {
4557 3.37 0.3 m
4558 3.37 0.61 3.13 0.61 2.97 0.61 c
4559 2.06 0.61 l
4560 2.06 5.81 l
4561 2.06 5.97 2.06 6.22 1.76 6.22 c
4562 1.57 6.22 1.51 6.1 1.46 5.98 c
4563 1.08 5.13 0.56 5.02 0.37 5.0 c
4564 0.21 4.99 0.0 4.97 0.0 4.69 c
4565 0.0 4.44 0.18 4.39 0.33 4.39 c
4566 0.52 4.39 0.93 4.45 1.37 4.83 c
4567 1.37 0.61 l
4568 0.46 0.61 l
4569 0.3 0.61 0.06 0.61 0.06 0.3 c
4570 0.06 0.0 0.31 0.0 0.46 0.0 c
4571 2.97 0.0 l
4572 3.12 0.0 3.37 0.0 3.37 0.3 c
4573 h f
4574 }%
4575 \hfill
4576 }%
4577 }
4578 % \end{macrocode}
4579 %
4580 % \begin{macrocode}
4581 \tlconstcn{c_@@_glyph_2_tl}
4582 {%
4583 \hbox to 5pt
4584 {%
4585 \hfill
4586 \pdfextension literal
4587 {
4588 4.2 0.41 m
4589 4.2 0.67 l
4590 4.2 0.86 4.2 1.08 3.86 1.08 c
4591 3.51 1.08 3.51 0.89 3.51 0.61 c
4592 1.13 0.61 l
4593 1.72 1.12 2.68 1.87 3.11 2.27 c
4594 3.74 2.83 4.2 3.47 4.2 4.27 c
4595 4.2 5.47 3.19 6.22 1.97 6.22 c
4596 0.79 6.22 0.0 5.4 0.0 4.55 c
4597 0.0 4.18 0.28 4.07 0.45 4.07 c
4598 0.66 4.07 0.89 4.24 0.89 4.52 c
4599 0.89 4.64 0.84 4.77 0.75 4.84 c
4600 0.9 5.3 1.37 5.61 1.92 5.61 c
4601 2.74 5.61 3.51 5.15 3.51 4.27 c
4602 3.51 3.57 3.02 2.99 2.36 2.44 c
4603 0.15 0.58 l
4604 0.06 0.5 0.0 0.45 0.0 0.31 c
4605 0.0 0.0 0.25 0.0 0.41 0.0 c
4606 3.8 0.0 l
4607 4.13 0.0 4.2 0.09 4.2 0.41 c
4608 h f
4609 }%
4610 \hfill
4611 }%
4612 }
4613 % \end{macrocode}
4614 %
4615 % \begin{macrocode}
4616 \tlconstcn{c_@@_glyph_3_tl}
4617 {%
4618 \hbox to 5pt
4619 {%
4620 \hfill
4621 \pdfextension literal
4622 {
4623 4.36 1.74 m
4624 4.36 2.45 3.89 3.04 3.23 3.34 c
4625 3.79 3.7 4.07 4.27 4.07 4.81 c
4626 4.07 5.55 3.34 6.22 2.19 6.22 c
4627 0.99 6.22 0.29 5.74 0.29 5.05 c
4628 0.29 4.72 0.54 4.58 0.74 4.58 c
4629 0.95 4.58 1.18 4.75 1.18 5.03 c
4630 1.18 5.17 1.12 5.27 1.09 5.3 c
4631 1.4 5.61 2.11 5.61 2.2 5.61 c
4632 2.88 5.61 3.38 5.25 3.38 4.8 c
4633 3.38 4.5 3.23 4.15 2.96 3.93 c
4634 2.64 3.67 2.39 3.65 2.03 3.63 c
4635 1.46 3.59 1.31 3.59 1.31 3.3 c
4636 1.31 2.99 1.55 2.99 1.71 2.99 c
4637 2.17 2.99 l
4638 3.16 2.99 3.67 2.32 3.67 1.74 c
4639 3.67 1.13 3.11 0.5 2.2 0.5 c
4640 1.8 0.5 1.03 0.61 0.77 1.08 c
4641 0.82 1.13 0.89 1.19 0.89 1.39 c
4642 0.89 1.63 0.7 1.83 0.45 1.83 c
4643 0.22 1.83 0.0 1.68 0.0 1.36 c
4644 0.0 0.47 0.97 -0.11 2.2 -0.11 c
4645 3.51 -0.11 4.36 0.81 4.36 1.74 c
4646 h f
4647 }%
4648 \hfill
4649 }%
4650 }
4651 % \end{macrocode}
4652 %
4653 % \begin{macrocode}
4654 \tlconstcn{c_@@_glyph_4_tl}
4655 {%
4656 \hbox to 5pt
4657 {%
4658 \hfill
4659 \pdfextension literal
4660 {
4661 4.66 1.99 m
4662 4.66 2.3 4.42 2.3 4.26 2.3 c
4663 3.48 2.3 l
4664 3.48 5.82 l
4665 3.48 6.15 3.41 6.23 3.07 6.23 c
4666 2.79 6.23 l
4667 2.55 6.23 2.5 6.22 2.37 6.02 c
4668 0.09 2.44 l
4669 0.0 2.31 0.0 2.29 0.0 2.09 c
4670 0.0 1.76 0.09 1.69 0.4 1.69 c
4671 2.92 1.69 l
4672 2.92 0.61 l
4673 2.3 0.61 l
4674 2.14 0.61 1.9 0.61 1.9 0.3 c
4675 1.9 0.0 2.15 0.0 2.3 0.0 c
4676 4.1 0.0 l
4677 4.25 0.0 4.5 0.0 4.5 0.3 c
4678 4.5 0.61 4.26 0.61 4.1 0.61 c
4679 3.48 0.61 l
4680 3.48 1.69 l
4681 4.26 1.69 l
4682 4.41 1.69 4.66 1.69 4.66 1.99 c
4683 2.92 2.3 m
4684 0.7 2.3 l
4685 2.92 5.79 l
4686 h f
4687 }%
4688 \hfill
4689 }%
4690 }
4691 % \end{macrocode}
4692 %
4693 % \begin{macrocode}
4694 \tlconstcn{c_@@_glyph_5_tl}
4695 {%
4696 \hbox to 5pt
4697 {%
4698 \hfill
4699 \pdfextension literal
4700 {
4701 4.2 1.9 m
4702 4.2 2.91 3.43 3.89 2.25 3.89 c
4703 1.9 3.89 1.46 3.82 1.05 3.6 c
4704 1.05 5.5 l
4705 3.45 5.5 l
4706 3.6 5.5 3.85 5.5 3.85 5.8 c
4707 3.85 6.11 3.61 6.11 3.45 6.11 c
4708 0.76 6.11 l
4709 0.43 6.11 0.36 6.02 0.36 5.7 c
4710 0.36 3.04 l
4711 0.36 2.86 0.36 2.63 0.68 2.63 c
4712 0.86 2.63 0.9 2.68 0.98 2.78 c
4713 1.25 3.1 1.66 3.28 2.24 3.28 c
4714 3.06 3.28 3.51 2.55 3.51 1.9 c
4715 3.51 1.1 2.8 0.5 1.96 0.5 c
4716 1.68 0.5 1.04 0.58 0.76 1.13 c
4717 0.81 1.18 0.89 1.25 0.89 1.45 c
4718 0.89 1.74 0.66 1.9 0.45 1.9 c
4719 0.3 1.9 0.0 1.81 0.0 1.42 c
4720 0.0 0.59 0.84 -0.11 1.96 -0.11 c
4721 3.21 -0.11 4.2 0.79 4.2 1.9 c
4722 h f
4723 }%
4724 \hfill
4725 }%
4726 }
4727 % \end{macrocode}
4728 %
4729 % \begin{macrocode}
4730 \tlconstcn{c_@@_glyph_6_tl}
4731 {%
4732 \hbox to 5pt
4733 {%
4734 \hfill
4735 \pdfextension literal
4736 {
4737 4.18 1.93 m
4738 4.18 3.07 3.3 3.96 2.2 3.96 c
4739 1.68 3.96 1.16 3.79 0.71 3.38 c
4740 0.85 4.79 1.79 5.61 2.67 5.61 c
4741 3.01 5.61 3.17 5.5 3.22 5.45 c
4742 3.17 5.4 3.12 5.34 3.12 5.16 c
4743 3.12 4.92 3.3 4.72 3.56 4.72 c
4744 3.81 4.72 4.01 4.89 4.01 5.19 c
4745 4.01 5.68 3.65 6.22 2.68 6.22 c
4746 1.34 6.22 0.0 5.01 0.0 2.99 c
4747 0.0 0.62 1.12 -0.11 2.11 -0.11 c
4748 3.2 -0.11 4.18 0.73 4.18 1.93 c
4749 3.49 1.93 m
4750 3.49 1.07 2.83 0.5 2.11 0.5 c
4751 1.46 0.5 1.07 0.99 0.87 1.6 c
4752 0.77 1.84 0.79 2.08 0.79 2.22 c
4753 0.79 2.84 1.39 3.34 2.15 3.34 c
4754 2.96 3.34 3.49 2.67 3.49 1.93 c
4755 h f
4756 }%
4757 \hfill
4758 }%
4759 }
4760 % \end{macrocode}
4761 %
4762 % \begin{macrocode}
4763 \tlconstcn{c_@@_glyph_7_tl}
4764 {%
4765 \hbox to 5pt
4766 {%
4767 \hfill
4768 \pdfextension literal
4769 {
4770 4.36 5.8 m
4771 4.36 6.11 4.12 6.11 3.96 6.11 c
4772 0.66 6.11 l
4773 0.6 6.27 0.42 6.27 0.34 6.27 c
4774 0.0 6.27 0.0 6.05 0.0 5.86 c
4775 0.0 5.44 l
4776 0.0 5.25 0.0 5.03 0.34 5.03 c
4777 0.69 5.03 0.69 5.22 0.69 5.5 c
4778 3.33 5.5 l
4779 1.6 3.66 1.26 1.46 1.26 0.36 c
4780 1.26 0.22 1.26 -0.11 1.61 -0.11 c
4781 1.78 -0.11 1.95 0.0 1.95 0.29 c
4782 2.0 3.13 3.53 4.87 4.14 5.46 c
4783 4.34 5.64 4.36 5.66 4.36 5.8 c
4784 h f
4785 }%
4786 \hfill
4787 }%
4788 }
4789 % \end{macrocode}
4790 %
4791 % \begin{macrocode}
4792 \tlconstcn{c_@@_glyph_8_tl}
4793 {%
4794 \hbox to 5pt
4795 {%
4796 \hfill
4797 \pdfextension literal
4798 {
4799 4.36 1.74 m
4800 4.36 2.49 3.71 3.08 2.97 3.29 c
4801 3.77 3.56 4.22 4.05 4.22 4.62 c
4802 4.22 5.44 3.37 6.22 2.18 6.22 c
4803 0.98 6.22 0.14 5.43 0.14 4.62 c
4804 0.14 4.05 0.6 3.55 1.39 3.29 c
4805 0.64 3.08 0.0 2.49 0.0 1.74 c
4806 0.0 0.77 0.93 -0.11 2.18 -0.11 c
4807 3.44 -0.11 4.36 0.77 4.36 1.74 c
4808 3.53 4.61 m
4809 3.53 4.04 2.91 3.6 2.18 3.6 c
4810 1.45 3.6 0.83 4.05 0.83 4.61 c
4811 0.83 5.13 1.39 5.61 2.18 5.61 c
4812 2.96 5.61 3.53 5.13 3.53 4.61 c
4813 3.67 1.75 m
4814 3.67 1.06 3.01 0.5 2.18 0.5 c
4815 1.35 0.5 0.69 1.06 0.69 1.75 c
4816 0.69 2.36 1.27 2.99 2.18 2.99 c
4817 3.1 2.99 3.67 2.36 3.67 1.75 c
4818 h f
4819 }%
4820 \hfill
4821 }%
4822 }
4823 % \end{macrocode}
4824 %
4825 % \begin{macrocode}
4826 \tlconstcn{c_@@_glyph_9_tl}
4827 {%
4828 \hbox to 5pt
4829 {%
4830 \hfill
4831 \pdfextension literal
4832 {
4833 4.18 3.12 m
4834 4.18 5.53 3.07 6.22 2.12 6.22 c
4835 1.01 6.22 0.0 5.39 0.0 4.18 c
4836 0.0 3.04 0.88 2.15 1.98 2.15 c
4837 2.5 2.15 3.02 2.32 3.47 2.73 c
4838 3.37 1.49 2.59 0.5 1.63 0.5 c
4839 1.54 0.5 1.18 0.51 0.97 0.67 c
4840 1.01 0.72 1.06 0.78 1.06 0.95 c
4841 1.06 1.19 0.88 1.39 0.62 1.39 c
4842 0.37 1.39 0.17 1.22 0.17 0.92 c
4843 0.17 0.6 0.35 -0.11 1.63 -0.11 c
4844 2.95 -0.11 4.18 1.14 4.18 3.12 c
4845 3.4 3.89 m
4846 3.4 3.34 2.86 2.77 2.03 2.77 c
4847 1.22 2.77 0.69 3.44 0.69 4.18 c
4848 0.69 5.05 1.4 5.61 2.12 5.61 c
4849 2.55 5.61 2.83 5.38 2.99 5.18 c
4850 3.31 4.79 3.4 4.6 3.4 3.89 c
4851 h f
4852 }%
4853 \hfill
4854 }%
4855 }
4856 \ExplSyntaxOn
4857 % \end{macrocode}
4858 %
4859 % \bigskip
4860 % \subsection{We load piton.lua}
4861 %
4862 %
4863 % \bigskip
4864 % \begin{macrocode}
4865 \cs_new_protected:Npn \@@_test_version:n #1
4866 {
4867 \str_if_eq:onF \PitonFileVersion { #1 }
4868 { \@@_error:n { bad~version~of~piton.lua } }
4869 }
4870 % \end{macrocode}
4871 %
4872 % \bigskip
4873 % \begin{macrocode}
4874 \hook_gput_code:nnn { begindocument } { . }
4875 {
4876 \lua_load_module:n { piton }
4877 \lua_now:n
4878 {
4879 tex.sprint ( luatexbase.catcodetables.expl ,
4880 [[\@@_test_version:n {]] .. piton_version .. "}" )
4881 }
4882 }
4883 % \end{macrocode}
4884 %
4885 %
4886 %</STY>
4887 %
4888 %
4889 % \bigskip
4890 % \section{The Lua part of the implementation}
4891 %
4892 % \bigskip
4893 % The Lua code will be loaded via a |{luacode*}| environment. The environment
4894 % is by itself a Lua block and the local declarations will be local to that
4895 % block. All the global functions (used by the L3 parts of the implementation)
4896 % will be put in a Lua table called |piton|.
4897 %
4898 %
4899 % \begin{macrocode}
4900 %<*LUA>
4901 piton.comment_latex = piton.comment_latex or ">"
4902 piton.comment_latex = "#" .. piton.comment_latex
4903 % \end{macrocode}
4904 %
4905 % \bigskip
4906 % The table |piton.write_files| will contain the contents of all the files that we
4907 % will write on the disk in the |\AtEndDocument| (if the user has used the key
4908 % |write-file|). The table |piton.join_files| is similar for the key |join|.
4909 % \begin{macrocode}
4910 piton.write_files = { }
4911 piton.join_files = { }
4912 % \end{macrocode}
4913 %
4914 % \bigskip
4915 % \begin{macrocode}
4916 local sprintL3
4917 function sprintL3 ( s )
4918 tex.sprint ( luatexbase.catcodetables.expl , s )
4919 end
4920 % \end{macrocode}
4921 %
4922 % \bigskip
4923 % \subsection{Special functions dealing with LPEG}
4924 %
4925 % \medskip
4926 % We will use the Lua library \pkg{lpeg} which is built in LuaTeX. That's why we
4927 % define first aliases for several functions of that library.
4928 % \begin{macrocode}
4929 local P, S, V, C, Ct, Cc = lpeg.P, lpeg.S, lpeg.V, lpeg.C, lpeg.Ct, lpeg.Cc
4930 local Cg , Cmt , Cb = lpeg.Cg , lpeg.Cmt , lpeg.Cb
4931 local B , R = lpeg.B , lpeg.R
4932 % \end{macrocode}
4933 %
4934 % \vspace{1cm}
4935 % The following line is mandatory.
4936 % \begin{macrocode}
4937 lpeg.locale(lpeg)
4938 % \end{macrocode}
4939 %
4940 %
4941 % \subsection{The functions Q, K, WithStyle, etc.}
4942 %
4943 % \bigskip
4944 % The function |Q| takes in as argument a pattern and returns a \textsc{lpeg}
4945 % \emph{which does a capture} of the pattern. That capture will be sent to LaTeX
4946 % with the catcode ``other'' for all the characters: it's suitable for elements
4947 % of the computer listings that \pkg{piton} will typeset verbatim (thanks to the
4948 % catcode ``other'').
4949 % \begin{macrocode}
4950 local Q
4951 function Q ( pattern )
4952 return Ct ( Cc ( luatexbase.catcodetables.other ) * C ( pattern ) )
4953 end
4954 % \end{macrocode}
4955 %
4956 %
4957 % \bigskip
4958 % The function |L| takes in as argument a pattern and returns a \textsc{lpeg}
4959 % \emph{which does a capture} of the pattern. That capture will be sent to LaTeX
4960 % with standard LaTeX catcodes for all the characters: the elements captured
4961 % will be formatted as normal LaTeX codes. It's suitable for the ``LaTeX
4962 % comments'' in the environments |{Piton}| and the elements between
4963 % |begin-escape| and |end-escape|. That function won't be much used.
4964 % \begin{macrocode}
4965 local L
4966 function L ( pattern ) return
4967 Ct ( C ( pattern ) )
4968 end
4969 % \end{macrocode}
4970 %
4971 % \bigskip
4972 % The function |Lc| (the c is for \emph{constant}) takes in as argument a string
4973 % and returns a \textsc{lpeg} \emph{with does a constant capture} which returns
4974 % that string. The elements captured will be formatted as L3 code. It will be
4975 % used to send to LaTeX all the formatting LaTeX instructions we have to insert
4976 % in order to do the syntactic highlighting (that's the main job of
4977 % \pkg{piton}). That function, unlike the previous one, will be widely used.
4978 % \begin{macrocode}
4979 local Lc
4980 function Lc ( string ) return
4981 Cc ( { luatexbase.catcodetables.expl , string } )
4982 end
4983 % \end{macrocode}
4984 %
4985 % \bigskip
4986 % The function |K| creates a \textsc{lpeg} which will return as capture the
4987 % whole LaTeX code corresponding to a Python chunk (that is to say with the
4988 % LaTeX formatting instructions corresponding to the syntactic nature of that
4989 % Python chunk). The first argument is a Lua string corresponding to the name of
4990 % a \pkg{piton} style and the second element is a pattern (that is to say a
4991 % \textsc{lpeg} without capture)
4992 % \begin{macrocode}
4993 local K
4994 function K ( style , pattern ) return
4995 Lc ( [[ {\PitonStyle{ ]] .. style .. "}{" )
4996 * Q ( pattern )
4997 * Lc "}}"
4998 end
4999 % \end{macrocode}
5000 % The formatting commands in a given \pkg{piton} style (eg. the style |Keyword|)
5001 % may be semi-global declarations (such as |\bfseries| or |\slshape|) or LaTeX
5002 % macros with an argument (such as |\fbox| or |\colorbox{yellow}|). In order to
5003 % deal with both syntaxes, we have used two pairs of braces:
5004 % |{\PitonStyle{Keyword}{|\texttt{\slshape text to format}|}}|.
5005 %
5006 %
5007 % \bigskip
5008 % The following function |WithStyle| is similar to the function |K| but should
5009 % be used for multi-lines elements.
5010 % \begin{macrocode}
5011 local WithStyle
5012 function WithStyle ( style , pattern ) return
5013 Ct ( Cc "Open" * Cc ( [[{\PitonStyle{]] .. style .. "}{" ) * Cc "}}" )
5014 * pattern
5015 * Ct ( Cc "Close" )
5016 end
5017 % \end{macrocode}
5018 %
5019 % \bigskip
5020 % The following \textsc{lpeg} catches the Python chunks which are in LaTeX
5021 % escapes (and that chunks will be considered as normal LaTeX constructions).
5022 % \begin{macrocode}
5023 Escape = P ( false )
5024 EscapeClean = P ( false )
5025 if piton.begin_escape then
5026 Escape =
5027 P ( piton.begin_escape )
5028 * L ( ( 1 - P ( piton.end_escape ) ) ^ 1 )
5029 * P ( piton.end_escape )
5030 % \end{macrocode}
5031 % The LPEG |EscapeClean| will be used in the LPEG Clean (and that LPEG is used
5032 % to ``clean'' the code by removing the formatting elements).
5033 % \begin{macrocode}
5034 EscapeClean =
5035 P ( piton.begin_escape )
5036 * ( 1 - P ( piton.end_escape ) ) ^ 1
5037 * P ( piton.end_escape )
5038 end
5039 % \end{macrocode}
5040 %
5041 % \begin{macrocode}
5042 EscapeMath = P ( false )
5043 if piton.begin_escape_math then
5044 EscapeMath =
5045 P ( piton.begin_escape_math )
5046 * Lc "$"
5047 * L ( ( 1 - P(piton.end_escape_math) ) ^ 1 )
5048 * Lc "$"
5049 * P ( piton.end_escape_math )
5050 end
5051 % \end{macrocode}
5052 %
5053 %
5054 % \bigskip
5055 % \paragraph{The basic syntactic LPEG}
5056 %
5057 % \begin{macrocode}
5058 local alpha , digit = lpeg.alpha , lpeg.digit
5059 local space = P " "
5060 % \end{macrocode}
5061 %
5062 % Remember that, for \textsc{lpeg}, the Unicode characters such as |à|, |â|,
5063 % |ç|, etc. are in fact strings of length 2 (2 bytes) because \pkg{lpeg} is not
5064 % Unicode-aware.
5065 % \begin{macrocode}
5066 local letter = alpha + "_" + "â" + "à" + "ç" + "é" + "è" + "ê" + "ë" + "ï" + "î"
5067 + "ô" + "û" + "ü" + "Â" + "À" + "Ç" + "É" + "È" + "Ê" + "Ë"
5068 + "Ï" + "Î" + "Ô" + "Û" + "Ü"
5069
5070 local alphanum = letter + digit
5071 % \end{macrocode}
5072 %
5073 % \bigskip
5074 % The following \textsc{lpeg} |identifier| is a mere pattern (that is to say
5075 % more or less a regular expression) which matches the Python identifiers (hence
5076 % the name).
5077 % \begin{macrocode}
5078 local identifier = letter * alphanum ^ 0
5079 % \end{macrocode}
5080 %
5081 % \medskip
5082 % On the other hand, the \textsc{lpeg} |Identifier| (with a capital) also returns
5083 % a \emph{capture}.
5084 % \begin{macrocode}
5085 local Identifier = K ( 'Identifier.Internal' , identifier )
5086 % \end{macrocode}
5087 %
5088 % \bigskip
5089 % \textbf{By convention, we will use names with an initial capital for LPEG
5090 % which return captures.}
5091 %
5092 % \bigskip
5093 %
5094 % The following functions allow to recognize numbers that contains |_| among
5095 % their digits, for example |1_000_000|, but also floating point numbers, numbers
5096 % with exponents and numbers with different bases.\footnote{The edge cases such as }
5097 % \begin{macrocode}
5098 local allow_underscores_except_first
5099 function allow_underscores_except_first ( p )
5100 return p * (P "_" + p)^0
5101 end
5102 local allow_underscores
5103 function allow_underscores ( p )
5104 return (P "_" + p)^0
5105 end
5106 local digits_to_number
5107 function digits_to_number(prefix, digits)
5108 -- The edge cases of what is allowed in number litterals is modelled after
5109 -- OCaml numbers, which seems to be the most permissive language
5110 -- in this regard (among C, OCaml, Python & SQL).
5111 return prefix
5112 * allow_underscores_except_first(digits^1)
5113 * (P "." * #(1 - P ".") * allow_underscores(digits))^-1
5114 * (S "eE" * S "+-"^-1 * allow_underscores_except_first(digits^1))^-1
5115 end
5116 % \end{macrocode}
5117 %
5118 % \bigskip
5119 % Here is the first use of our function~|K|. That function will be used to
5120 % construct \textsc{lpeg} which capture Python chunks for which we have a
5121 % dedicated \pkg{piton} style. For example, for the numbers, \pkg{piton}
5122 % provides a style which is called |Number|. The name of the style is provided
5123 % as a Lua string in the second argument of the function~|K|. By convention, we
5124 % use single quotes for delimiting the Lua strings which are names of
5125 % \pkg{piton} styles (but this is only a convention).
5126 % \begin{macrocode}
5127 local Number =
5128 K ( 'Number.Internal' ,
5129 digits_to_number (P "0x" + P "0X", R "af" + R "AF" + digit)
5130 + digits_to_number (P "0o" + P "0O", R "07")
5131 + digits_to_number (P "0b" + P "0B", R "01")
5132 + digits_to_number ( "" , digit )
5133 )
5134 % \end{macrocode}
5135 %
5136 % \bigskip
5137 % We will now define the LPEG |Word|.
5138 %
5139 % We have a problem in the following LPEG because, obviously, we should adjust
5140 % the list of symbols with the delimiters of the current language (no?).
5141 % \begin{macrocode}
5142 local lpeg_central = 1 - S " '\"\r[({})]" - digit
5143 % \end{macrocode}
5144 % We recall that |piton.begin_escape| and |piton_end_escape| are Lua strings
5145 % corresponding to the keys |begin-escape| and |end-escape|.
5146 % \begin{macrocode}
5147 if piton.begin_escape then
5148 lpeg_central = lpeg_central - piton.begin_escape
5149 end
5150 if piton.begin_escape_math then
5151 lpeg_central = lpeg_central - piton.begin_escape_math
5152 end
5153 local Word = Q ( lpeg_central ^ 1 )
5154 % \end{macrocode}
5155 %
5156 % \bigskip
5157 % \begin{macrocode}
5158 local Space = Q " " ^ 1
5159 local SkipSpace = Q " " ^ 0
5160
5161 local Punct = Q ( S ".,:;!" )
5162
5163 local Tab = "\t" * Lc [[ \@@_tab: ]]
5164 % \end{macrocode}
5165 %
5166 % \bigskip
5167 % \begin{macrocode}
5168 local LeadingSpace = Lc [[ \@@_leading_space: ]] * P " "
5169 % \end{macrocode}
5170 %
5171 % \bigskip
5172 % \begin{macrocode}
5173 local Delim = Q ( S "[({})]" )
5174 % \end{macrocode}
5175 %
5176 % \bigskip
5177 % The following \textsc{lpeg} catches a space (U+0020) and replaces it by
5178 % |\l_@@_space_in_string_tl|. It will be used in the strings. Usually,
5179 % |\l_@@_space_in_string_tl| will contain a space and therefore there won't be any
5180 % difference. However, when the key |show-spaces-in-strings| is in force,
5181 % |\\l_@@_space_in_string_tl| will contain ␣ (U+2423) in order to visualize the
5182 % spaces.
5183 % \begin{macrocode}
5184 local SpaceInString = space * Lc [[ \l_@@_space_in_string_tl ]]
5185 % \end{macrocode}
5186 %
5187 %
5188 % \bigskip
5189 % \subsection{The option 'detected-commands' and al.}
5190 %
5191 % We create four Lua tables called |detected_commands|, |raw_detected_commands|,
5192 % |beamer_commands| and |beamer_environments|.
5193 %
5194 % On the TeX side, the corresponding data have first been stored as clists.
5195
5196 % Then, in a |\AtBeginDocument|, they have been converted in ``toks registers''
5197 % of TeX.
5198 %
5199 % Now, on the Lua side, we are able to access to those ``toks registers'' with
5200 % the special pseudo-table |tex.toks| of LuaTeX.
5201 %
5202 % Remark that we can safely use |explode(',')| to convert such ``toks
5203 % registers'' in Lua tables since, in a clist of L3, there is no empty
5204 % component and, for each component, there is no space on both sides (the
5205 % |explode| of the Lua of LuaTeX is unable to do itself such purification of the
5206 % components).
5207 % \begin{macrocode}
5208 local detected_commands = tex.toks.PitonDetectedCommands : explode ( ',' )
5209 local raw_detected_commands = tex.toks.PitonRawDetectedCommands : explode ( ',' )
5210 local beamer_commands = tex.toks.PitonBeamerCommands : explode ( ',' )
5211 local beamer_environments = tex.toks.PitonBeamerEnvironments : explode ( ',' )
5212 % \end{macrocode}
5213 %
5214 % We will also create some \textsc{lpeg}.
5215 %
5216 % According to our conventions, a \textsc{lpeg} with a name in camelCase is a
5217 % \textsc{lpeg} which doesn't do any capture.
5218 % \begin{macrocode}
5219 local detectedCommands = P ( false )
5220 for _ , x in ipairs ( detected_commands ) do
5221 detectedCommands = detectedCommands + P ( "\\" .. x )
5222 end
5223 % \end{macrocode}
5224 % Further, we will have a \textsc{lpeg} called |DetectedCommands| (in
5225 % PascalCase) which will be a \textsc{lpeg} \emph{with} captures.
5226 %
5227 % \bigskip
5228 % \begin{macrocode}
5229 local rawDetectedCommands = P ( false )
5230 for _ , x in ipairs ( raw_detected_commands ) do
5231 rawDetectedCommands = rawDetectedCommands + P ( "\\" .. x )
5232 end
5233 % \end{macrocode}
5234 %
5235 %
5236 % \begin{macrocode}
5237 local beamerCommands = P ( false )
5238 for _ , x in ipairs ( beamer_commands ) do
5239 beamerCommands = beamerCommands + P ( "\\" .. x )
5240 end
5241 % \end{macrocode}
5242 %
5243 % \begin{macrocode}
5244 local beamerEnvironments = P ( false )
5245 for _ , x in ipairs ( beamer_environments ) do
5246 beamerEnvironments = beamerEnvironments + P ( x )
5247 end
5248 % \end{macrocode}
5249 %
5250 %
5251 % \bigskip
5252 % \paragraph{Several tools for the construction of the main LPEG}
5253 %
5254 % \begin{macrocode}
5255 local LPEG0 = { }
5256 local LPEG1 = { }
5257 local LPEG2 = { }
5258 local LPEG_cleaner = { }
5259 % \end{macrocode}
5260 %
5261 % \bigskip
5262 % For each language, we will need a pattern to match expressions with balanced
5263 % braces. Those balanced braces must \emph{not} take into account the braces
5264 % present in strings of the language. However, the syntax for the strings is
5265 % language-dependent. That's why we write a Lua function |Compute_braces| which
5266 % will compute the pattern by taking in as argument a pattern for the strings of
5267 % the language (at least the shorts strings). The argument of |Compute_braces|
5268 % must be a pattern \emph{which does no captures}.
5269 % \begin{macrocode}
5270 local Compute_braces
5271 function Compute_braces ( lpeg_string ) return
5272 P { "E" ,
5273 E =
5274 (
5275 "{" * V "E" * "}"
5276 +
5277 lpeg_string
5278 +
5279 ( 1 - S "{}" )
5280 ) ^ 0
5281 }
5282 end
5283 % \end{macrocode}
5284 %
5285 %
5286 %
5287 % \bigskip
5288 % The following Lua function will compute the \text{lpeg} |DetectedCommands|
5289 % which is a \textsc{lpeg} with captures.
5290 % \begin{macrocode}
5291 local Compute_DetectedCommands
5292 function Compute_DetectedCommands ( lang , braces ) return
5293 Ct (
5294 Cc "Open"
5295 * C ( detectedCommands * space ^ 0 * P "{" )
5296 * Cc "}"
5297 )
5298 * ( braces
5299 / ( function ( s )
5300 if s ~= '' then return
5301 LPEG1[lang] : match ( s )
5302 end
5303 end )
5304 )
5305 * P "}"
5306 * Ct ( Cc "Close" )
5307 end
5308 % \end{macrocode}
5309 %
5310 % \begin{macrocode}
5311 local Compute_RawDetectedCommands
5312 function Compute_RawDetectedCommands ( lang , braces ) return
5313 Ct ( C ( rawDetectedCommands * space ^ 0 * P "{" * braces * P "}" ) )
5314 end
5315 % \end{macrocode}
5316 %
5317 % \bigskip
5318 % \begin{macrocode}
5319 local Compute_LPEG_cleaner
5320 function Compute_LPEG_cleaner ( lang , braces ) return
5321 Ct ( ( ( detectedCommands + rawDetectedCommands ) * "{"
5322 * ( braces
5323 / ( function ( s )
5324 if s ~= '' then return
5325 LPEG_cleaner[lang] : match ( s )
5326 end
5327 end )
5328 )
5329 * "}"
5330 + EscapeClean
5331 + C ( P ( 1 ) )
5332 ) ^ 0 ) / table.concat
5333 end
5334 % \end{macrocode}
5335 %
5336 % \bigskip
5337 % The following function |ParseAgain| will be used in the definitions of the
5338 % LPEG of the different computer languages when we will need to
5339 % \emph{parse again} a small chunk of code. It's a way to avoid the use of a actual
5340 % \emph{grammar} of LPEG (in a sens, a recursive regular expression).
5341 %
5342 % Remark that there is no \pkg{piton} style associated to a chunk of code which
5343 % is analyzed by |ParseAgain|. If we wish a \pkg{piton} style available to the
5344 % end user (if he wish to format that element with a uniform font instead of
5345 % an analyze by |ParseAgain|), we have to use |\@@_piton:n|.
5346 % \begin{macrocode}
5347 local ParseAgain
5348 function ParseAgain ( code )
5349 if code ~= '' then return
5350 % \end{macrocode}
5351 % The variable |piton.language| is set in the function |piton.Parse|.
5352 % \begin{macrocode}
5353 LPEG1[piton.language] : match ( code )
5354 end
5355 end
5356 % \end{macrocode}
5357 %
5358 %
5359 %
5360 % \bigskip
5361 % \paragraph{Constructions for Beamer}
5362 %
5363 % \bigskip
5364 % If the class Beamer is used, some environments and commands of Beamer are
5365 % automatically detected in the listings of \pkg{piton}.
5366 %
5367 % \begin{macrocode}
5368 local Beamer = P ( false )
5369 % \end{macrocode}
5370 %
5371 % \bigskip
5372 % The following Lua function will be used to compute the \textsc{lpeg} |Beamer|
5373 % for each computer language.
5374 %
5375 % According to our conventions, the \textsc{lpeg} |Beamer|, with its name in
5376 % PascalCase does captures.
5377 % \begin{macrocode}
5378 local Compute_Beamer
5379 function Compute_Beamer ( lang , braces )
5380 % \end{macrocode}
5381 %
5382 % \smallskip
5383 % We will compute in |lpeg| the \textsc{lpeg} that we will return.
5384 % \begin{macrocode}
5385 local lpeg = L ( P [[\pause]] * ( "[" * ( 1 - P "]" ) ^ 0 * "]" ) ^ -1 )
5386 lpeg = lpeg +
5387 Ct ( Cc "Open"
5388 * C ( beamerCommands
5389 * ( "<" * ( 1 - P ">" ) ^ 0 * ">" ) ^ -1
5390 * P "{"
5391 )
5392 * Cc "}"
5393 )
5394 * ( braces /
5395 ( function ( s ) if s ~= '' then return LPEG1[lang] : match ( s ) end end ) )
5396 * "}"
5397 * Ct ( Cc "Close" )
5398 % \end{macrocode}
5399 %
5400 %
5401 % \bigskip
5402 % For the command |\alt|, the specification of the overlays (between angular
5403 % brackets) is mandatory.
5404 % \begin{macrocode}
5405 lpeg = lpeg +
5406 L ( P [[\alt]] * "<" * ( 1 - P ">" ) ^ 0 * ">{" )
5407 * ( braces /
5408 ( function ( s ) if s ~= '' then return LPEG1[lang] : match ( s ) end end ) )
5409 * L ( P "}{" )
5410 * ( braces /
5411 ( function ( s ) if s ~= '' then return LPEG1[lang] : match ( s ) end end ) )
5412 * L ( P "}" )
5413 % \end{macrocode}
5414 %
5415 % \bigskip
5416 % For |\temporal|, the specification of the overlays (between angular brackets)
5417 % is mandatory.
5418 % \begin{macrocode}
5419 lpeg = lpeg +
5420 L ( P [[\temporal]] * "<" * ( 1 - P ">" ) ^ 0 * ">{" )
5421 * ( braces
5422 / ( function ( s )
5423 if s ~= '' then return LPEG1[lang] : match ( s ) end end ) )
5424 * L ( P "}{" )
5425 * ( braces
5426 / ( function ( s )
5427 if s ~= '' then return LPEG1[lang] : match ( s ) end end ) )
5428 * L ( P "}{" )
5429 * ( braces
5430 / ( function ( s )
5431 if s ~= '' then return LPEG1[lang] : match ( s ) end end ) )
5432 * L ( P "}" )
5433 % \end{macrocode}
5434 %
5435 % \bigskip
5436 % Now, the environments of Beamer.
5437 % \begin{macrocode}
5438 for _ , x in ipairs ( beamer_environments ) do
5439 lpeg = lpeg +
5440 Ct ( Cc "Open"
5441 * C (
5442 P ( [[\begin{]] .. x .. "}" )
5443 * ( "<" * ( 1 - P ">") ^ 0 * ">" ) ^ -1
5444 )
5445 * space ^ 0 * ( P "\r" ) ^ 1 -- added 25/08/23
5446 * Cc ( [[\end{]] .. x .. "}" )
5447 )
5448 * (
5449 % \end{macrocode}
5450 % We catch all the content of the Beamer environment which a \textsc{lpeg} which is
5451 % a grammar because t here may be nested environments of the same type
5452 % (added 2025/11/14).
5453 % \begin{macrocode}
5454 (
5455 P { "E" ,
5456 E = (
5457 P ( [[\begin{]] .. x .. "}" )
5458 * V "E"
5459 * P ( [[\end{]] .. x .. "}" )
5460 +
5461 (
5462 1
5463 - P ( [[\begin{]] .. x .. "}" )
5464 - P ( [[\end{]] .. x .. "}" )
5465 )
5466 ) ^ 0
5467 }
5468 )
5469 / ( function ( s )
5470 if s ~= '' then return
5471 LPEG1[lang] : match ( s )
5472 end
5473 end )
5474 )
5475 * P ( [[\end{]] .. x .. "}" )
5476 * Ct ( Cc "Close" )
5477 end
5478 % \end{macrocode}
5479 %
5480 % \bigskip
5481 % Now, you can return the value we have computed.
5482 % \begin{macrocode}
5483 return lpeg
5484 end
5485 % \end{macrocode}
5486 %
5487 %
5488 % \bigskip
5489 % The following LPEG is in relation with the key |math-comments|. It will be
5490 % used in all the languages.
5491 % \begin{macrocode}
5492 local CommentMath =
5493 P "$" * K ( 'Comment.Math' , ( 1 - S "$\r" ) ^ 1 ) * P "$" -- $
5494 % \end{macrocode}
5495 %
5496 %
5497 % \bigskip
5498 % \paragraph{EOL}
5499 %
5500 % There may be empty lines in the transcription of the prompt, \emph{id est} lines of
5501 % the form |...| without space after and that's why we need |P " " ^ -1| with the |^ -1|.
5502 % \begin{macrocode}
5503 local Prompt =
5504 K ( 'Prompt' , ( P ">>>" + "..." ) * P " " ^ -1 )
5505 * Lc [[ \rowcolor { \l_@@_prompt_bg_color_tl } ]]
5506 % \end{macrocode}
5507 %
5508 %
5509 % \bigskip
5510 % The following \textsc{lpeg} |EOL| is for the end of lines.
5511 % \begin{macrocode}
5512 local EOL =
5513 P "\r"
5514 *
5515 (
5516 space ^ 0 * -1
5517 +
5518 Cc "EOL"
5519 )
5520 * ( LeadingSpace ^ 0 * # ( 1 - S " \r" ) ) ^ -1
5521 % \end{macrocode}
5522 %
5523 %
5524 % \bigskip
5525 % The following \textsc{lpeg} |CommentLaTeX| is for what is called in that
5526 % document the ``LaTeX comments''.
5527 % \begin{macrocode}
5528 local CommentLaTeX =
5529 P ( piton.comment_latex )
5530 * Lc [[{\PitonStyle{Comment.LaTeX}{\ignorespaces]]
5531 * L ( ( 1 - P "\r" ) ^ 0 )
5532 * Lc "}}"
5533 * ( EOL + -1 )
5534 % \end{macrocode}
5535 %
5536 %
5537 % \bigskip
5538 % \subsection{The language Python}
5539 %
5540 % We open a Lua local scope for the language Python (of course, there will be
5541 % also global definitions).
5542 % \begin{macrocode}
5543 --python Python
5544 do
5545 % \end{macrocode}
5546 %
5547 % \bigskip
5548 % Some strings of length 2 are explicit because we want the corresponding
5549 % ligatures available in some fonts such as \emph{Fira Code} to be active.
5550 % \begin{macrocode}
5551 local Operator =
5552 K ( 'Operator' ,
5553 P "!=" + "<>" + "==" + "<<" + ">>" + "<=" + ">=" + ":=" + "//" + "**"
5554 + S "-~+/*%=<>&.@|" )
5555
5556 local OperatorWord =
5557 K ( 'Operator.Word' , P "in" + "is" + "and" + "or" + "not" )
5558 % \end{macrocode}
5559 %
5560 % \smallskip
5561 % The keyword |in| in a construction such as ``|for i in range(n)|'' must be
5562 % formatted as a keyword and not as an |Operator.Word| and that's why we write
5563 % the following LPEG |For|.
5564 % \begin{macrocode}
5565 local For = K ( 'Keyword' , P "for" )
5566 * Space
5567 * Identifier
5568 * Space
5569 * K ( 'Keyword' , P "in" )
5570
5571 local Keyword =
5572 K ( 'Keyword' ,
5573 P "assert" + "as" + "break" + "case" + "class" + "continue" + "def" +
5574 "del" + "elif" + "else" + "except" + "exec" + "finally" + "for" + "from" +
5575 "global" + "if" + "import" + "lambda" + "non local" + "pass" + "return" +
5576 "try" + "while" + "with" + "yield" + "yield from" )
5577 + K ( 'Keyword.Constant' , P "True" + "False" + "None" )
5578
5579 local Builtin =
5580 K ( 'Name.Builtin' ,
5581 P "__import__" + "abs" + "all" + "any" + "bin" + "bool" + "bytearray" +
5582 "bytes" + "chr" + "classmethod" + "compile" + "complex" + "delattr" +
5583 "dict" + "dir" + "divmod" + "enumerate" + "eval" + "filter" + "float" +
5584 "format" + "frozenset" + "getattr" + "globals" + "hasattr" + "hash" +
5585 "hex" + "id" + "input" + "int" + "isinstance" + "issubclass" + "iter" +
5586 "len" + "list" + "locals" + "map" + "max" + "memoryview" + "min" + "next"
5587 + "object" + "oct" + "open" + "ord" + "pow" + "print" + "property" +
5588 "range" + "repr" + "reversed" + "round" + "set" + "setattr" + "slice" +
5589 "sorted" + "staticmethod" + "str" + "sum" + "super" + "tuple" + "type" +
5590 "vars" + "zip" )
5591
5592 local Exception =
5593 K ( 'Exception' ,
5594 P "ArithmeticError" + "AssertionError" + "AttributeError" +
5595 "BaseException" + "BufferError" + "BytesWarning" + "DeprecationWarning" +
5596 "EOFError" + "EnvironmentError" + "Exception" + "FloatingPointError" +
5597 "FutureWarning" + "GeneratorExit" + "IOError" + "ImportError" +
5598 "ImportWarning" + "IndentationError" + "IndexError" + "KeyError" +
5599 "KeyboardInterrupt" + "LookupError" + "MemoryError" + "NameError" +
5600 "NotImplementedError" + "OSError" + "OverflowError" +
5601 "PendingDeprecationWarning" + "ReferenceError" + "ResourceWarning" +
5602 "RuntimeError" + "RuntimeWarning" + "StopIteration" + "SyntaxError" +
5603 "SyntaxWarning" + "SystemError" + "SystemExit" + "TabError" + "TypeError"
5604 + "UnboundLocalError" + "UnicodeDecodeError" + "UnicodeEncodeError" +
5605 "UnicodeError" + "UnicodeTranslateError" + "UnicodeWarning" +
5606 "UserWarning" + "ValueError" + "VMSError" + "Warning" + "WindowsError" +
5607 "ZeroDivisionError" + "BlockingIOError" + "ChildProcessError" +
5608 "ConnectionError" + "BrokenPipeError" + "ConnectionAbortedError" +
5609 "ConnectionRefusedError" + "ConnectionResetError" + "FileExistsError" +
5610 "FileNotFoundError" + "InterruptedError" + "IsADirectoryError" +
5611 "NotADirectoryError" + "PermissionError" + "ProcessLookupError" +
5612 "TimeoutError" + "StopAsyncIteration" + "ModuleNotFoundError" +
5613 "RecursionError" )
5614
5615 local RaiseException = K ( 'Keyword' , P "raise" ) * SkipSpace * Exception * Q "("
5616 % \end{macrocode}
5617 %
5618 % \bigskip
5619 % In Python, a ``decorator'' is a statement whose begins by |@| which patches
5620 % the function defined in the following statement.
5621 % \begin{macrocode}
5622 local Decorator = K ( 'Name.Decorator' , P "@" * letter ^ 1 )
5623 % \end{macrocode}
5624 %
5625 % \bigskip
5626 % The following \textsc{lpeg} |DefClass| will be used to detect the definition of a
5627 % new class (the name of that new class will be formatted with the \pkg{piton}
5628 % style |Name.Class|).
5629 %
5630 % \smallskip
5631 % Example:\enskip \piton{class myclass:}
5632 % \begin{macrocode}
5633 local DefClass =
5634 K ( 'Keyword' , "class" ) * Space * K ( 'Name.Class' , identifier )
5635 % \end{macrocode}
5636 %
5637 % If the word |class| is not followed by a identifier, it will be caught as
5638 % keyword by the \textsc{lpeg} |Keyword| (useful if we want to type a
5639 % list of keywords).
5640 %
5641 % \bigskip
5642 % The following \textsc{lpeg} |ImportAs| is used for the lines beginning by |import|.
5643 % % We have to detect the potential keyword |as| because both the name of the
5644 % module and its alias must be formatted with the \pkg{piton} style |Name.Namespace|.
5645 %
5646 % \smallskip
5647 % Example:\enskip \piton{import numpy as np}
5648 %
5649 % \smallskip
5650 % Moreover, after the keyword |import|, it's possible to have a comma-separated
5651 % list of modules (if the keyword |as| is not used).
5652 %
5653 % \smallskip
5654 % Example:\enskip \piton{import math, numpy}
5655 % \begin{macrocode}
5656 local ImportAs =
5657 K ( 'Keyword' , "import" )
5658 * Space
5659 * K ( 'Name.Namespace' , identifier * ( "." * identifier ) ^ 0 )
5660 * (
5661 ( Space * K ( 'Keyword' , "as" ) * Space
5662 * K ( 'Name.Namespace' , identifier ) )
5663 +
5664 ( SkipSpace * Q "," * SkipSpace
5665 * K ( 'Name.Namespace' , identifier ) ) ^ 0
5666 )
5667 % \end{macrocode}
5668 % Be careful: there is no commutativity of |+| in the previous expression.
5669 %
5670 % \bigskip
5671 % The \textsc{lpeg} |FromImport| is used for the lines beginning by |from|. We
5672 % need a special treatment because the identifier following the keyword |from|
5673 % must be formatted with the \pkg{piton} style |Name.Namespace| and the
5674 % following keyword |import| must be formatted with the \pkg{piton} style
5675 % |Keyword| and must \emph{not} be caught by the \textsc{lpeg} |ImportAs|.
5676 %
5677 % \smallskip
5678 % Example:\enskip \piton{from math import pi}
5679 %
5680 % \smallskip
5681 % \begin{macrocode}
5682 local FromImport =
5683 K ( 'Keyword' , "from" )
5684 * Space * K ( 'Name.Namespace' , identifier )
5685 * Space * K ( 'Keyword' , "import" )
5686 % \end{macrocode}
5687 %
5688 % \bigskip
5689 % \paragraph{The strings of Python}
5690 %
5691 % For the strings in Python, there are four categories of delimiters (without
5692 % counting the prefixes for f-strings and raw strings). We will use, in the
5693 % names of our \textsc{lpeg}, prefixes to distinguish the \textsc{lpeg} dealing
5694 % with that categories of strings, as presented in the following tabular.
5695 % \begin{center}
5696 % \begin{tabular}{@{}lcc@{}}
5697 % \toprule
5698 % & |Single| & |Double| \\
5699 % \midrule
5700 % |Short| & |'text'| & |"text"| \\
5701 % |Long| & |'''test'''| & |"""text"""| \\
5702 % \bottomrule
5703 % \end{tabular}
5704 % \end{center}
5705 %
5706 %
5707 % \bigskip
5708 % We have also to deal with the interpolations in the f-strings. Here
5709 % is an example of a f-string with an interpolation and a format
5710 % instruction\footnote{There is no special \pkg{piton} style for the formatting
5711 % instruction (after the colon): the style which will be applied will be the
5712 % style of the encompassing string, that is to say |String.Short| or
5713 % |String.Long|.} in that interpolation:
5714 %
5715 % |\piton{f'Total price: {total+1:.2f} €'}|
5716 %
5717 %
5718 % \bigskip
5719 % The interpolations beginning by |%| (even though there is more modern
5720 % techniques now in Python).
5721 % \begin{macrocode}
5722 local PercentInterpol =
5723 K ( 'String.Interpol' ,
5724 P "%"
5725 * ( "(" * alphanum ^ 1 * ")" ) ^ -1
5726 * ( S "-#0 +" ) ^ 0
5727 * ( digit ^ 1 + "*" ) ^ -1
5728 * ( "." * ( digit ^ 1 + "*" ) ) ^ -1
5729 * ( S "HlL" ) ^ -1
5730 * S "sdfFeExXorgiGauc%"
5731 )
5732 % \end{macrocode}
5733 %
5734 % \bigskip
5735 % We can now define the \textsc{lpeg} for the four kinds of strings. It's not
5736 % possible to use our function~|K| because of the interpolations which must be
5737 % formatted with another \pkg{piton} style that the rest of the
5738 % string.\footnote{The interpolations are formatted with the \pkg{piton} style
5739 % |Interpol.Inside|. The initial value of that style is \texttt{\textbackslash
5740 % @@\_piton:n} which means that the interpolations are parsed once again by \pkg{piton}.}
5741 % \begin{macrocode}
5742 local SingleShortString =
5743 WithStyle ( 'String.Short.Internal' ,
5744 % \end{macrocode}
5745 % First, we deal with the f-strings of Python, which are prefixed by |f| or |F|.
5746 % \begin{macrocode}
5747 Q ( P "f'" + "F'" )
5748 * (
5749 K ( 'String.Interpol' , "{" )
5750 * K ( 'Interpol.Inside' , ( 1 - S "}':" ) ^ 0 )
5751 * Q ( P ":" * ( 1 - S "}:'" ) ^ 0 ) ^ -1
5752 * K ( 'String.Interpol' , "}" )
5753 +
5754 SpaceInString
5755 +
5756 Q ( ( P "\\'" + "\\\\" + "{{" + "}}" + 1 - S " {}'" ) ^ 1 )
5757 ) ^ 0
5758 * Q "'"
5759 +
5760 % \end{macrocode}
5761 % Now, we deal with the standard strings of Python, but also the ``raw strings''.
5762 % \begin{macrocode}
5763 Q ( P "'" + "r'" + "R'" )
5764 * ( Q ( ( P "\\'" + "\\\\" + 1 - S " '\r%" ) ^ 1 )
5765 + SpaceInString
5766 + PercentInterpol
5767 + Q "%"
5768 ) ^ 0
5769 * Q "'" )
5770 % \end{macrocode}
5771 %
5772 % \begin{macrocode}
5773 local DoubleShortString =
5774 WithStyle ( 'String.Short.Internal' ,
5775 Q ( P "f\"" + "F\"" )
5776 * (
5777 K ( 'String.Interpol' , "{" )
5778 * K ( 'Interpol.Inside' , ( 1 - S "}\":" ) ^ 0 )
5779 * ( K ( 'String.Interpol' , ":" ) * Q ( (1 - S "}:\"") ^ 0 ) ) ^ -1
5780 * K ( 'String.Interpol' , "}" )
5781 +
5782 SpaceInString
5783 +
5784 Q ( ( P "\\\"" + "\\\\" + "{{" + "}}" + 1 - S " {}\"" ) ^ 1 )
5785 ) ^ 0
5786 * Q "\""
5787 +
5788 Q ( P "\"" + "r\"" + "R\"" )
5789 * ( Q ( ( P "\\\"" + "\\\\" + 1 - S " \"\r%" ) ^ 1 )
5790 + SpaceInString
5791 + PercentInterpol
5792 + Q "%"
5793 ) ^ 0
5794 * Q "\"" )
5795
5796 local ShortString = SingleShortString + DoubleShortString
5797 % \end{macrocode}
5798 %
5799 % \bigskip
5800 % \paragraph{Beamer}
5801 %
5802 % The argument of |Compute_braces| must be a pattern \emph{which does no
5803 % catching} corresponding to the strings of the language.
5804 %
5805 % \begin{macrocode}
5806 local braces =
5807 Compute_braces
5808 (
5809 ( P "\"" + "r\"" + "R\"" + "f\"" + "F\"" )
5810 * ( P '\\\"' + 1 - S "\"" ) ^ 0 * "\""
5811 +
5812 ( P '\'' + 'r\'' + 'R\'' + 'f\'' + 'F\'' )
5813 * ( P '\\\'' + 1 - S '\'' ) ^ 0 * '\''
5814 )
5815
5816 if piton.beamer then Beamer = Compute_Beamer ( 'python' , braces ) end
5817 % \end{macrocode}
5818 %
5819 % \bigskip
5820 % \paragraph{Detected commands}
5821 %
5822 % \begin{macrocode}
5823 DetectedCommands = Compute_DetectedCommands ( 'python' , braces )
5824 + Compute_RawDetectedCommands ( 'python' , braces )
5825 % \end{macrocode}
5826 %
5827 % \bigskip
5828 %
5829 % \paragraph{LPEG_cleaner}
5830 %
5831 % \begin{macrocode}
5832 LPEG_cleaner.python = Compute_LPEG_cleaner ( 'python' , braces )
5833 % \end{macrocode}
5834 %
5835 % \bigskip
5836 % \paragraph{The long strings}
5837 %
5838 %
5839 % \begin{macrocode}
5840 local SingleLongString =
5841 WithStyle ( 'String.Long.Internal' ,
5842 ( Q ( S "fF" * P "'''" )
5843 * (
5844 K ( 'String.Interpol' , "{" )
5845 * K ( 'Interpol.Inside' , ( 1 - S "}:\r" - "'''" ) ^ 0 )
5846 * Q ( P ":" * (1 - S "}:\r" - "'''" ) ^ 0 ) ^ -1
5847 * K ( 'String.Interpol' , "}" )
5848 +
5849 Q ( ( 1 - P "'''" - S "{}'\r" ) ^ 1 )
5850 +
5851 EOL
5852 ) ^ 0
5853 +
5854 Q ( ( S "rR" ) ^ -1 * "'''" )
5855 * (
5856 Q ( ( 1 - P "'''" - S "\r%" ) ^ 1 )
5857 +
5858 PercentInterpol
5859 +
5860 P "%"
5861 +
5862 EOL
5863 ) ^ 0
5864 )
5865 * Q "'''" )
5866 % \end{macrocode}
5867 %
5868 % \begin{macrocode}
5869 local DoubleLongString =
5870 WithStyle ( 'String.Long.Internal' ,
5871 (
5872 Q ( S "fF" * "\"\"\"" )
5873 * (
5874 K ( 'String.Interpol', "{" )
5875 * K ( 'Interpol.Inside' , ( 1 - S "}:\r" - "\"\"\"" ) ^ 0 )
5876 * Q ( ":" * (1 - S "}:\r" - "\"\"\"" ) ^ 0 ) ^ -1
5877 * K ( 'String.Interpol' , "}" )
5878 +
5879 Q ( ( 1 - S "{}\"\r" - "\"\"\"" ) ^ 1 )
5880 +
5881 EOL
5882 ) ^ 0
5883 +
5884 Q ( S "rR" ^ -1 * "\"\"\"" )
5885 * (
5886 Q ( ( 1 - P "\"\"\"" - S "%\r" ) ^ 1 )
5887 +
5888 PercentInterpol
5889 +
5890 P "%"
5891 +
5892 EOL
5893 ) ^ 0
5894 )
5895 * Q "\"\"\""
5896 )
5897 % \end{macrocode}
5898 %
5899 % \begin{macrocode}
5900 local LongString = SingleLongString + DoubleLongString
5901 % \end{macrocode}
5902 %
5903 % \bigskip
5904 % We have a \textsc{lpeg} for the Python docstrings. That \textsc{lpeg} will
5905 % be used in the \textsc{lpeg} |DefFunction| which deals with the whole preamble
5906 % of a function definition (which begins with |def|).
5907 % \begin{macrocode}
5908 local StringDoc =
5909 K ( 'String.Doc.Internal' , P "r" ^ -1 * "\"\"\"" )
5910 * ( K ( 'String.Doc.Internal' , (1 - P "\"\"\"" - "\r" ) ^ 0 ) * EOL
5911 * Tab ^ 0
5912 ) ^ 0
5913 * K ( 'String.Doc.Internal' , ( 1 - P "\"\"\"" - "\r" ) ^ 0 * "\"\"\"" )
5914 % \end{macrocode}
5915 %
5916 % \bigskip
5917 % \paragraph{The comments in the Python listings}
5918 %
5919 % We define different \textsc{lpeg} dealing with comments in the Python
5920 % listings.
5921 % \begin{macrocode}
5922 local Comment =
5923 WithStyle
5924 ( 'Comment.Internal' ,
5925 Q "#" * ( CommentMath + Q ( ( 1 - S "$\r" ) ^ 1 ) ) ^ 0 -- $
5926 )
5927 * ( EOL + -1 )
5928 % \end{macrocode}
5929 %
5930 %
5931 % \bigskip
5932 % \paragraph{DefFunction}
5933 %
5934 % The following \textsc{lpeg} |expression| will be used for the parameters in
5935 % the \emph{argspec} of a Python function. It's necessary to use a \emph{grammar}
5936 % because that pattern mainly checks the correct nesting of the delimiters
5937 % (and it's known in the theory of formal languages that this can't be done with
5938 % regular expressions \emph{stricto sensu} only).
5939 % \begin{macrocode}
5940 local expression =
5941 P { "E" ,
5942 E = ( "'" * ( P "\\'" + 1 - S "'\r" ) ^ 0 * "'"
5943 + "\"" * ( P "\\\"" + 1 - S "\"\r" ) ^ 0 * "\""
5944 + "{" * V "F" * "}"
5945 + "(" * V "F" * ")"
5946 + "[" * V "F" * "]"
5947 + ( 1 - S "{}()[]\r," ) ) ^ 0 ,
5948 F = ( "{" * V "F" * "}"
5949 + "(" * V "F" * ")"
5950 + "[" * V "F" * "]"
5951 + ( 1 - S "{}()[]\r\"'" ) ) ^ 0
5952 }
5953 % \end{macrocode}
5954 %
5955 % \bigskip
5956 % We will now define a \textsc{lpeg} |Params| that will catch the list of
5957 % parameters (that is to say the \emph{argspec}) in the definition of a Python
5958 % function. For example, in the line of code
5959 % \begin{center}
5960 % \piton{def MyFunction(a,b,x=10,n:int): return n}
5961 % \end{center}
5962 % the \textsc{lpeg} |Params| will be used to catch the chunk\enskip |a,b,x=10,n:int|.
5963 %
5964 % \medskip
5965 % \begin{macrocode}
5966 local Params =
5967 P { "E" ,
5968 E = ( V "F" * ( Q "," * V "F" ) ^ 0 ) ^ -1 ,
5969 F = SkipSpace * ( Identifier + Q "*args" + Q "**kwargs" ) * SkipSpace
5970 * ( Q ":" * SkipSpace * K ( 'Name.Type' , identifier ) ) ^ -1
5971 * ( SkipSpace * K ( 'InitialValues' , "=" * SkipSpace * expression ) ) ^ -1
5972 }
5973 % \end{macrocode}
5974 %
5975 %
5976 % \bigskip
5977 % The following \textsc{lpeg} |DefFunction| catches a keyword |def| and the
5978 % following name of function \emph{but also everything else until a potential
5979 % docstring}. That's why this definition of \textsc{lpeg} must occur (in the file
5980 % |piton.sty|) after the definition of several other \textsc{lpeg} such as
5981 % |Comment|, |CommentLaTeX|, |Params|, |StringDoc|...
5982 % \begin{macrocode}
5983 local DefFunction =
5984 K ( 'Keyword' , "def" )
5985 * Space
5986 * K ( 'Name.Function.Internal' , identifier )
5987 * SkipSpace
5988 * Q "(" * Params * Q ")"
5989 * SkipSpace
5990 * ( Q "->" * SkipSpace * K ( 'Name.Type' , identifier ) ) ^ -1
5991 % \end{macrocode}
5992 %
5993 % \begin{macrocode}
5994 * ( C ( ( 1 - S ":\r" ) ^ 0 ) / ParseAgain )
5995 * Q ":"
5996 * ( SkipSpace
5997 * ( EOL + CommentLaTeX + Comment ) -- in all cases, that contains an EOL
5998 * Tab ^ 0
5999 * SkipSpace
6000 * StringDoc ^ 0 -- there may be additional docstrings
6001 ) ^ -1
6002 % \end{macrocode}
6003 % Remark that, in the previous code, |CommentLaTeX| \emph{must} appear
6004 % before |Comment|: there is no commutativity of the addition for the
6005 % \emph{parsing expression grammars} (\textsc{peg}).
6006 %
6007 % \smallskip
6008 % If the word |def| is not followed by an identifier and parenthesis, it will be
6009 % caught as keyword by the \textsc{lpeg} |Keyword| (useful if, for example, the
6010 % end user wants to speak of the keyword \piton{def}).
6011 %
6012 %
6013 % \paragraph{Miscellaneous}
6014 %
6015 % \begin{macrocode}
6016 local ExceptionInConsole = Exception * Q ( ( 1 - P "\r" ) ^ 0 ) * EOL
6017 % \end{macrocode}
6018 %
6019 %
6020 % \bigskip
6021 % \paragraph{The main LPEG for the language Python}
6022 %
6023 % \begin{macrocode}
6024 local EndKeyword
6025 = Space + Punct + Delim + EOL + Beamer + DetectedCommands + Escape +
6026 EscapeMath + -1
6027 % \end{macrocode}
6028 %
6029 % First, the main loop :
6030 % \begin{macrocode}
6031 local Main =
6032 space ^ 0 * EOL
6033 + Space
6034 + Tab
6035 + Escape + EscapeMath
6036 + Beamer
6037 + CommentLaTeX
6038 + DetectedCommands
6039 + Prompt
6040 + LongString
6041 + Comment
6042 + ExceptionInConsole
6043 + Delim
6044 + Operator
6045 + OperatorWord * EndKeyword
6046 + ShortString
6047 + Punct
6048 + FromImport
6049 + RaiseException
6050 + DefFunction
6051 + DefClass
6052 + For
6053 + Keyword * EndKeyword
6054 + Decorator
6055 + Builtin * EndKeyword
6056 + Identifier
6057 + Number
6058 + Word
6059 % \end{macrocode}
6060 %
6061 % \bigskip
6062 % Here, we must not put |local|, of course.
6063 % \begin{macrocode}
6064 LPEG1.python = Main ^ 0
6065 % \end{macrocode}
6066 %
6067 % \bigskip
6068 % We recall that each line in the Python code to parse will be sent back to
6069 % LaTeX between a pair |\@@_begin_line:| -- |\@@_end_line:|\footnote{Remember
6070 % that the \texttt{\textbackslash @@\_end\_line:} must be explicit because it
6071 % will be used as marker in order to delimit the argument of the command
6072 % \texttt{\textbackslash @@\_begin\_line:}}.
6073 % \begin{macrocode}
6074 LPEG2.python =
6075 Ct (
6076 ( space ^ 0 * "\r" ) ^ -1
6077 * Lc [[ \@@_begin_line: ]]
6078 * LeadingSpace ^ 0
6079 * ( space ^ 1 * -1 + Main ) ^ 0
6080 * -1
6081 * Lc [[ \@@_end_line: ]]
6082 )
6083 % \end{macrocode}
6084 %
6085 % \bigskip
6086 % End of the Lua scope for the language Python.
6087 % \begin{macrocode}
6088 end
6089 % \end{macrocode}
6090 %
6091 %
6092 % \subsection{The language OCaml}
6093 %
6094 % We open a Lua local scope for the language OCaml (of course, there will be also
6095 % global definitions).
6096 % \begin{macrocode}
6097 --ocaml Ocaml OCaml
6098 do
6099 % \end{macrocode}
6100 %
6101 % \bigskip
6102 % \begin{macrocode}
6103 local SkipSpace = ( Q " " + EOL ) ^ 0
6104 local Space = ( Q " " + EOL ) ^ 1
6105 % \end{macrocode}
6106 %
6107 % \bigskip
6108 % \begin{macrocode}
6109 local braces = Compute_braces ( '\"' * ( 1 - S "\"" ) ^ 0 * '\"' )
6110 % \end{macrocode}
6111 %
6112 % \bigskip
6113 % \begin{macrocode}
6114 if piton.beamer then Beamer = Compute_Beamer ( 'ocaml' , braces ) end
6115 DetectedCommands =
6116 Compute_DetectedCommands ( 'ocaml' , braces )
6117 + Compute_RawDetectedCommands ( 'ocaml' , braces )
6118 local Q
6119 % \end{macrocode}
6120 % Usually, the following version of the function |Q| will be used without the
6121 % second arguemnt (|strict|), that is to say in a loosy way. However, in some
6122 % circunstancies, we will a need the ``strict'' version, for instance in
6123 % |DefFunction|.
6124 % \begin{macrocode}
6125 function Q ( pattern, strict )
6126 if strict ~= nil then
6127 return Ct ( Cc ( luatexbase.catcodetables.CatcodeTableOther ) * C ( pattern ) )
6128 else
6129 return Ct ( Cc ( luatexbase.catcodetables.CatcodeTableOther ) * C ( pattern ) )
6130 + Beamer + DetectedCommands + EscapeMath + Escape
6131 end
6132 end
6133 % \end{macrocode}
6134 %
6135 % \bigskip
6136 % \begin{macrocode}
6137 local K
6138 function K ( style , pattern, strict ) return
6139 Lc ( [[ {\PitonStyle{ ]] .. style .. "}{" )
6140 * Q ( pattern, strict )
6141 * Lc "}}"
6142 end
6143 % \end{macrocode}
6144 %
6145 % \bigskip
6146 % \begin{macrocode}
6147 local WithStyle
6148 function WithStyle ( style , pattern ) return
6149 Ct ( Cc "Open" * Cc ( [[{\PitonStyle{]] .. style .. "}{" ) * Cc "}}" )
6150 * (pattern + Beamer + DetectedCommands + EscapeMath + Escape)
6151 * Ct ( Cc "Close" )
6152 end
6153 % \end{macrocode}
6154 %
6155 % \bigskip
6156 % The following LPEG corresponds to the balanced expressions (balanced according
6157 % to the parenthesis). Of course, we must write |(1 - S "()")| with outer parenthesis.
6158 % \begin{macrocode}
6159 local balanced_parens =
6160 P { "E" , E = ( "(" * V "E" * ")" + ( 1 - S "()" ) ) ^ 0 }
6161 % \end{macrocode}
6162 %
6163 % \paragraph{The strings of OCaml}
6164 % \begin{macrocode}
6165 local ocaml_string =
6166 P "\""
6167 * (
6168 P " "
6169 +
6170 P ( ( P '\\\"' + 1 - S " \"\r" ) ^ 1 )
6171 +
6172 EOL -- ?
6173 ) ^ 0
6174 * P "\""
6175 % \end{macrocode}
6176 %
6177 % \begin{macrocode}
6178 local String =
6179 WithStyle
6180 ( 'String.Long.Internal' ,
6181 Q "\""
6182 * (
6183 SpaceInString
6184 +
6185 Q ( ( P '\\\"' + 1 - S " \"\r" ) ^ 1 )
6186 +
6187 EOL
6188 ) ^ 0
6189 * Q "\""
6190 )
6191 % \end{macrocode}
6192 %
6193 % \bigskip
6194 % Now, the ``quoted strings'' of OCaml (for example \verb+{ext|Essai|ext}+).
6195 %
6196 % For those strings, we will do two consecutive analysis. First an analysis to
6197 % determine the whole string and, then, an analysis for the potential visual
6198 % spaces and the EOL in the string.
6199 %
6200 % The first analysis require a match-time capture. For explanations about that
6201 % programmation, see the paragraphe \emph{Lua's long strings} in
6202 % |www.inf.puc-rio.br/~roberto/lpeg|.
6203 %
6204 % \begin{macrocode}
6205 local ext = ( R "az" + "_" ) ^ 0
6206 local open = "{" * Cg ( ext , 'init' ) * "|"
6207 local close = "|" * C ( ext ) * "}"
6208 local closeeq =
6209 Cmt ( close * Cb ( 'init' ) ,
6210 function ( s , i , a , b ) return a == b end )
6211 % \end{macrocode}
6212 %
6213 % \medskip
6214 % The \textsc{lpeg} |QuotedStringBis| will do the second analysis.
6215 % \begin{macrocode}
6216 local QuotedStringBis =
6217 WithStyle ( 'String.Long.Internal' ,
6218 (
6219 Space
6220 +
6221 Q ( ( 1 - S " \r" ) ^ 1 )
6222 +
6223 EOL
6224 ) ^ 0 )
6225 % \end{macrocode}
6226 %
6227 % \medskip
6228 % We use a ``function capture'' (as called in the official documentation of the
6229 % \textsc{lpeg}) in order to do the second analysis on the result of the first one.
6230 % \begin{macrocode}
6231 local QuotedString =
6232 C ( open * ( 1 - closeeq ) ^ 0 * close ) /
6233 ( function ( s ) return QuotedStringBis : match ( s ) end )
6234 % \end{macrocode}
6235 %
6236 % \bigskip
6237 % In OCaml, the delimiters for the comments are |(*| and |*)|. There are
6238 % unsymmetrical and OCaml allows those comments to be nested. That's why we need a
6239 % grammar.
6240 %
6241 % In these comments, we embed the math comments (between |$| and |$|) and we
6242 % embed also a treatment for the end of lines (since the comments may be multi-lines).
6243 %
6244 % \begin{macrocode}
6245 local comment =
6246 P {
6247 "A" ,
6248 A = Q "(*"
6249 * ( V "A"
6250 + Q ( ( 1 - S "\r$\"" - "(*" - "*)" ) ^ 1 ) -- $
6251 + Q ( ocaml_string )
6252 + "$" * K ( 'Comment.Math' , ( 1 - S "$\r" ) ^ 1 ) * "$" -- $
6253 + EOL
6254 ) ^ 0
6255 * Q "*)"
6256 }
6257 local Comment = WithStyle ( 'Comment.Internal' , comment )
6258 % \end{macrocode}
6259 %
6260 %
6261 % \paragraph{Some standard LPEG}
6262 %
6263 % \begin{macrocode}
6264 local Delim = K ( 'Delim' , P "[|" + "|]" + S "[()]" )
6265 local Punct = K ( 'Punct' , S ",:;!" )
6266 % \end{macrocode}
6267 %
6268 % \bigskip
6269 % The identifiers caught by |cap_identifier| begin with a capital. In OCaml,
6270 % it's used for the constructors of types and for the names of the modules.
6271 %
6272 % \begin{macrocode}
6273 local cap_identifier = R "AZ" * ( R "az" + R "AZ" + S "_'" + digit ) ^ 0
6274 % \end{macrocode}
6275 %
6276 % \bigskip
6277 % We consider |::| and |[]| as constructors (of the lists) as does the Tuareg
6278 % mode of Emacs.
6279 % \begin{macrocode}
6280 local Constructor =
6281 P "::"
6282 % \end{macrocode}
6283 % Don't use |\hspace| instead of |\kern|
6284 % \begin{macrocode}
6285 * Lc [[{\PitonStyle{Name.Constructor}{\kern0.1em:\kern-0.2em:\kern0.1em}}]]
6286 +
6287 P "[]"
6288 * Lc ([[{\PitonStyle{Name.Constructor}{\kern-0.1em[\kern0.1em]}}]])
6289 K ( 'Name.Constructor' ,
6290 Q "`" ^ -1 * cap_identifier
6291 + Q ( "[" , true ) * SkipSpace * Q ( "]" , true) )
6292 % \end{macrocode}
6293 %
6294 % \bigskip
6295 % \begin{macrocode}
6296 local ModuleType = K ( 'Name.Type' , cap_identifier )
6297 % \end{macrocode}
6298 %
6299 % \bigskip
6300 % \begin{macrocode}
6301 local OperatorWord =
6302 K ( 'Operator.Word' ,
6303 P "asr" + "land" + "lor" + "lsl" + "lxor" + "mod" + "or" + "not" )
6304 % \end{macrocode}
6305 %
6306 % \bigskip
6307 % In OCaml, some keywords are considered as \emph{governing keywords} with some
6308 % special syntactic characteristics.
6309 % \begin{macrocode}
6310 local governing_keyword = P "and" + "begin" + "class" + "constraint" +
6311 "end" + "external" + "functor" + "include" + "inherit" + "initializer" +
6312 "in" + "let" + "method" + "module" + "object" + "open" + "rec" + "sig" +
6313 "struct" + "type" + "val"
6314 % \end{macrocode}
6315 %
6316 % \bigskip
6317 % \begin{macrocode}
6318 local Keyword =
6319 K ( 'Keyword' ,
6320 P "assert" + "as" + "done" + "downto" + "do" + "else" + "exception"
6321 + "for" + "function" + "fun" + "if" + "lazy" + "match" + "mutable"
6322 + "new" + "of" + "private" + "raise" + "then" + "to" + "try"
6323 + "virtual" + "when" + "while" + "with" )
6324 + K ( 'Keyword.Constant' , P "true" + "false" )
6325 + K ( 'Keyword.Governing', governing_keyword )
6326 % \end{macrocode}
6327 %
6328 % \bigskip
6329 % \begin{macrocode}
6330 local EndKeyword
6331 = Space + Punct + Delim + EOL + Beamer + DetectedCommands + Escape
6332 + EscapeMath + -1
6333 % \end{macrocode}
6334 %
6335 % \bigskip
6336 % Now, the identifier. Recall that we have also a LPEG |cap_identifier| for the
6337 % indentifiers beginning with a capital letter.
6338 % \begin{macrocode}
6339 local identifier = ( R "az" + "_" ) * ( R "az" + R "AZ" + S "_'" + digit ) ^ 0
6340 - ( OperatorWord + Keyword ) * EndKeyword
6341 % \end{macrocode}
6342 %
6343 % \bigskip
6344 % We have the internal style |Identifier.Internal| in order to be able to
6345 % implement the mechanism |\SetPitonIdentifier|. The final user has access to a
6346 % style called |Identifier|.
6347 % \begin{macrocode}
6348 local Identifier = K ( 'Identifier.Internal' , identifier )
6349 % \end{macrocode}
6350 %
6351 % \bigskip
6352 % In OCmal, \emph{character} is a type different of the type |string|.
6353 % \begin{macrocode}
6354 local ocaml_char =
6355 P "'" *
6356 (
6357 ( 1 - S "'\\" )
6358 + "\\"
6359 * ( S "\\'ntbr \""
6360 + digit * digit * digit
6361 + P "x" * ( digit + R "af" + R "AF" )
6362 * ( digit + R "af" + R "AF" )
6363 * ( digit + R "af" + R "AF" )
6364 + P "o" * R "03" * R "07" * R "07" )
6365 )
6366 * "'"
6367 local Char =
6368 K ( 'String.Short.Internal', ocaml_char )
6369 % \end{macrocode}
6370 %
6371 % \bigskip
6372 % For the parameter of the types (for example : |`\a| as in |`a list|).
6373 % \begin{macrocode}
6374 local TypeParameter =
6375 K ( 'TypeParameter' ,
6376 "'" * Q "_" ^ -1 * alpha ^ 1 * digit ^ 0 * ( # ( 1 - P "'" ) + -1 ) )
6377 % \end{macrocode}
6378 %
6379 % \paragraph{DotNotation}
6380 %
6381 % Now, we deal with the notations with points (eg: |List.length|). In OCaml,
6382 % such notation is used for the fields of the records and for the modules.
6383 % \begin{macrocode}
6384 local DotNotation =
6385 (
6386 K ( 'Name.Module' , cap_identifier )
6387 * Q "."
6388 * ( Identifier + Constructor + Q "(" + Q "[" + Q "{" ) ^ -1
6389 +
6390 Identifier
6391 * Q "."
6392 * K ( 'Name.Field' , identifier )
6393 )
6394 * ( Q "." * K ( 'Name.Field' , identifier ) ) ^ 0
6395 % \end{macrocode}
6396 %
6397 % \paragraph{The records}
6398 %
6399 % \begin{macrocode}
6400 local expression_for_fields_type =
6401 P { "E" ,
6402 E = ( "{" * V "F" * "}"
6403 + "(" * V "F" * ")"
6404 + TypeParameter
6405 + ( 1 - S "{}()[]\r;" ) ) ^ 0 ,
6406 F = ( "{" * V "F" * "}"
6407 + "(" * V "F" * ")"
6408 + ( 1 - S "{}()[]\r\"'" ) + TypeParameter ) ^ 0
6409 }
6410 % \end{macrocode}
6411 %
6412 % \bigskip
6413 % \begin{macrocode}
6414 local expression_for_fields_value =
6415 P { "E" ,
6416 E = ( "{" * V "F" * "}"
6417 + "(" * V "F" * ")"
6418 + "[" * V "F" * "]"
6419 + ocaml_string + ocaml_char
6420 + ( 1 - S "{}()[];" ) ) ^ 0 ,
6421 F = ( "{" * V "F" * "}"
6422 + "(" * V "F" * ")"
6423 + "[" * V "F" * "]"
6424 + ocaml_string + ocaml_char
6425 + ( 1 - S "{}()[]\"'" )) ^ 0
6426 }
6427 % \end{macrocode}
6428 %
6429 % \bigskip
6430 % \begin{macrocode}
6431 local OneFieldDefinition =
6432 ( K ( 'Keyword' , "mutable" ) * SkipSpace ) ^ -1
6433 * K ( 'Name.Field' , identifier ) * SkipSpace
6434 * Q ":" * SkipSpace
6435 * K ( 'TypeExpression' , expression_for_fields_type )
6436 * SkipSpace
6437 % \end{macrocode}
6438 %
6439 % \bigskip
6440 % \begin{macrocode}
6441 local OneField =
6442 K ( 'Name.Field' , identifier ) * SkipSpace
6443 * Q "=" * SkipSpace
6444 % \end{macrocode}
6445 % Don't forget the parentheses!
6446 % \begin{macrocode}
6447 * ( C ( expression_for_fields_value ) / ParseAgain )
6448 * SkipSpace
6449 % \end{macrocode}
6450 %
6451 % \bigskip
6452 % The \emph{records}.
6453 % \begin{macrocode}
6454 local RecordVal =
6455 Q "{" * SkipSpace
6456 *
6457 (
6458 (Identifier + DotNotation) * Space * K('Keyword', "with") * Space
6459 ) ^-1
6460 *
6461 (
6462 OneField * ( Q ";" * SkipSpace * ( Comment * SkipSpace ) ^ 0 * OneField ) ^ 0
6463 )
6464 * SkipSpace
6465 * Q ";" ^ -1
6466 * SkipSpace
6467 * Comment ^ -1
6468 * SkipSpace
6469 * Q "}"
6470 local RecordType =
6471 Q "{" * SkipSpace
6472 *
6473 (
6474 OneFieldDefinition
6475 * ( Q ";" * SkipSpace * ( Comment * SkipSpace ) ^ 0 * OneFieldDefinition ) ^ 0
6476 )
6477 * SkipSpace
6478 * Q ";" ^ -1
6479 * SkipSpace
6480 * Comment ^ -1
6481 * SkipSpace
6482 * Q "}"
6483 local Record = RecordType + RecordVal
6484 % \end{macrocode}
6485 %
6486 % \bigskip
6487 % \begin{macrocode}
6488 local Operator =
6489 P "||" *
6490 % \end{macrocode}
6491 % Don't use |\hspace| instead of |\kern|!
6492 % \begin{macrocode}
6493 Lc([[{\PitonStyle{Operator}{\kern0.1em|\kern-0.2em|\kern0.1em}}]])
6494 +
6495 K ( 'Operator' ,
6496 P "!=" + "<>" + "==" + "<<" + ">>" + "<=" + ">=" + ":=" + "&&" +
6497 "//" + "**" + ";;" + "->" + "+." + "-." + "*." + "/."
6498 + S "-~+/*%=<>&@|" )
6499 % \end{macrocode}
6500 %
6501 % \bigskip
6502 % \begin{macrocode}
6503 local Builtin =
6504 K ( 'Name.Builtin' , P "incr" + "decr" + "fst" + "snd" + "ref" )
6505 % \end{macrocode}
6506 %
6507 % \bigskip
6508 % \begin{macrocode}
6509 local Exception =
6510 K ( 'Exception' ,
6511 P "Division_by_zero" + "End_of_File" + "Failure" + "Invalid_argument" +
6512 "Match_failure" + "Not_found" + "Out_of_memory" + "Stack_overflow" +
6513 "Sys_blocked_io" + "Sys_error" + "Undefined_recursive_module" )
6514 % \end{macrocode}
6515 %
6516 % \bigskip
6517 % \begin{macrocode}
6518 LPEG_cleaner.ocaml = Compute_LPEG_cleaner ( 'ocaml' , braces )
6519 % \end{macrocode}
6520 %
6521 % \bigskip
6522 % An argument in the definition of a OCaml function may be of the form
6523 % |(pattern:type)|. |pattern| may be a single identifier but it's not mandatory.
6524 % First instance, it's possible to write in OCaml:
6525 %
6526 % |let head (a::q) = a|
6527 %
6528 % First, we write a pattern (in the LPEG sens!) to match what will be the
6529 % pattern (in the OCaml sens).
6530 % \begin{macrocode}
6531 local pattern_part =
6532 ( P "(" * balanced_parens * ")" + ( 1 - S ":()" ) + P "::" ) ^ 0
6533 % \end{macrocode}
6534 % For the ``type'' part, the LPEG-pattern will merely be |balanced_parens|.
6535 %
6536 % \bigskip
6537 % We can now write a LPEG |Argument| which catches a argument of function (in
6538 % the definition of the function).
6539 % \begin{macrocode}
6540 local Argument =
6541 % \end{macrocode}
6542 % The following line is for the labels of the labeled arguments. Maybe we will,
6543 % in the future, create a style for those elements.
6544 % \begin{macrocode}
6545 ( Q "~" * Identifier * Q ":" * SkipSpace ) ^ -1
6546 *
6547 % \end{macrocode}
6548 % Now, the argument itself, either a single identifier, or a construction
6549 % between parentheses
6550 % \begin{macrocode}
6551 (
6552 K ( 'Identifier.Internal' , identifier )
6553 +
6554 Q "(" * SkipSpace
6555 * ( C ( pattern_part ) / ParseAgain )
6556 * SkipSpace
6557 % \end{macrocode}
6558 % Of course, the specification of type is optional.
6559 % \begin{macrocode}
6560 * ( Q ":" * #(1- P"=")
6561 * K ( 'TypeExpression' , balanced_parens ) * SkipSpace
6562 ) ^ -1
6563 * Q ")"
6564 )
6565 % \end{macrocode}
6566 %
6567 % \bigskip
6568 % Despite its name, the \textsc{lpeg} |DefFunction| deals also with |let open|
6569 % which opens locally a module.
6570 % \begin{macrocode}
6571 local DefFunction =
6572 K ( 'Keyword.Governing' , "let open" )
6573 * Space
6574 * K ( 'Name.Module' , cap_identifier )
6575 +
6576 K ( 'Keyword.Governing' , P "let rec" + "let" + "and" )
6577 * Space
6578 * K ( 'Name.Function.Internal' , identifier )
6579 * Space
6580 * (
6581 % \end{macrocode}
6582 % We use here the argument |strict| in order to allow a correct analyse of
6583 % |let x = \uncover<2->{y}| (elsewhere, it's interpreted as a definition of a OCaml
6584 % function).
6585 % \begin{macrocode}
6586 Q "=" * SkipSpace * K ( 'Keyword' , "function" , true )
6587 +
6588 Argument * ( SkipSpace * Argument ) ^ 0
6589 * (
6590 SkipSpace
6591 * Q ":" * # ( 1 - P "=" )
6592 * K ( 'TypeExpression' , ( 1 - P "=" ) ^ 0 )
6593 ) ^ -1
6594 )
6595 % \end{macrocode}
6596 %
6597 % \paragraph{DefModule}
6598 %
6599 % \begin{macrocode}
6600 local DefModule =
6601 K ( 'Keyword.Governing' , "module" ) * Space
6602 *
6603 (
6604 K ( 'Keyword.Governing' , "type" ) * Space
6605 * K ( 'Name.Type' , cap_identifier )
6606 +
6607 K ( 'Name.Module' , cap_identifier ) * SkipSpace
6608 *
6609 (
6610 Q "(" * SkipSpace
6611 * K ( 'Name.Module' , cap_identifier ) * SkipSpace
6612 * Q ":" * # ( 1 - P "=" ) * SkipSpace
6613 * K ( 'Name.Type' , cap_identifier ) * SkipSpace
6614 *
6615 (
6616 Q "," * SkipSpace
6617 * K ( 'Name.Module' , cap_identifier ) * SkipSpace
6618 * Q ":" * # ( 1 - P "=" ) * SkipSpace
6619 * K ( 'Name.Type' , cap_identifier ) * SkipSpace
6620 ) ^ 0
6621 * Q ")"
6622 ) ^ -1
6623 *
6624 (
6625 Q "=" * SkipSpace
6626 * K ( 'Name.Module' , cap_identifier ) * SkipSpace
6627 * Q "("
6628 * K ( 'Name.Module' , cap_identifier ) * SkipSpace
6629 *
6630 (
6631 Q ","
6632 *
6633 K ( 'Name.Module' , cap_identifier ) * SkipSpace
6634 ) ^ 0
6635 * Q ")"
6636 ) ^ -1
6637 )
6638 +
6639 K ( 'Keyword.Governing' , P "include" + "open" )
6640 * Space
6641 * K ( 'Name.Module' , cap_identifier )
6642 % \end{macrocode}
6643 %
6644 % \paragraph{DefType}
6645 %
6646 % \begin{macrocode}
6647 local DefType =
6648 K ( 'Keyword.Governing' , "type" )
6649 * Space
6650 * K ( 'TypeExpression' , Q ( 1 - P "=" - P "+=" ) ^ 1 )
6651 * SkipSpace
6652 * ( Q "+=" + Q "=" )
6653 * SkipSpace
6654 * (
6655 RecordType
6656 +
6657 % \end{macrocode}
6658 % The following lines are a suggestion of Y. Salmon.
6659 % \begin{macrocode}
6660 WithStyle
6661 (
6662 'TypeExpression' ,
6663 (
6664 (
6665 EOL
6666 + comment
6667 + Q ( 1
6668 - P ";;"
6669 - P "type"
6670 - ( ( Space + EOL ) * governing_keyword * EndKeyword )
6671 )
6672 ) ^ 0
6673 *
6674 (
6675 # ( P "type" + ( Space + EOL ) * governing_keyword * EndKeyword )
6676 + Q ";;"
6677 + -1
6678 )
6679 )
6680 )
6681 )
6682 % \end{macrocode}
6683 %
6684 % \bigskip
6685 % \begin{macrocode}
6686 local prompt =
6687 Q "utop[" * digit^1 * Q "]> "
6688 local start_of_line = P(function(subject, position)
6689 if position == 1 or subject:sub(position - 1, position - 1) == "\r" then
6690 return position
6691 end
6692 return nil
6693 end)
6694 local Prompt = #start_of_line * K( 'Prompt', prompt )
6695 local Answer = #start_of_line * (Q "-" + Q "val" * Space * Identifier )
6696 * SkipSpace * Q ":" * #(1- P"=") * SkipSpace
6697 * (K ( 'TypeExpression' , Q ( 1 - P "=") ^ 1 ) ) * SkipSpace * Q "="
6698 % \end{macrocode}
6699 %
6700 % \bigskip
6701 % \paragraph{The main LPEG for the language OCaml}
6702 %
6703 % \begin{macrocode}
6704 local Main =
6705 space ^ 0 * EOL
6706 + Space
6707 + Tab
6708 + Escape + EscapeMath
6709 + Beamer
6710 + DetectedCommands
6711 + TypeParameter
6712 + String + QuotedString + Char
6713 + Comment
6714 + Prompt + Answer
6715 % \end{macrocode}
6716 % For the labels (maybe we will write in the future a dedicated LPEG pour those tokens).
6717 % \begin{macrocode}
6718 + Q "~" * Identifier * ( Q ":" ) ^ -1
6719 + Q ":" * # (1 - P ":") * SkipSpace
6720 * K ( 'TypeExpression' , balanced_parens ) * SkipSpace * Q ")"
6721 + Exception
6722 + DefType
6723 + DefFunction
6724 + DefModule
6725 + Record
6726 + Keyword * EndKeyword
6727 + OperatorWord * EndKeyword
6728 + Builtin * EndKeyword
6729 + DotNotation * EndKeyword
6730 + Constructor
6731 + Identifier
6732 + Punct
6733 + Delim -- Delim is before Operator for a correct analysis of [| et |]
6734 + Operator
6735 + Number
6736 + Word
6737 % \end{macrocode}
6738 %
6739 % \bigskip
6740 % % Here, we must not put |local|, of course.
6741 % \begin{macrocode}
6742 LPEG1.ocaml = Main ^ 0
6743 % \end{macrocode}
6744 %
6745 % \bigskip
6746 % \begin{macrocode}
6747 LPEG2.ocaml =
6748 Ct (
6749 % \end{macrocode}
6750 % The following lines are in order to allow, in |\piton| (and not in |{Piton}|),
6751 % judgments of type (such as |f : my_type -> 'a list|) or single expressions of
6752 % type such as |my_type -> 'a list| (in that case, the argument of |\piton|
6753 % \emph{must} begin by a colon).
6754 % \begin{macrocode}
6755 (
6756 (
6757 P ":"
6758 +
6759 (
6760 ( K ( 'Name.Module' , cap_identifier ) * Q "." ) ^ -1
6761 * Identifier
6762 * SkipSpace
6763 * Q ":"
6764 )
6765 )
6766 * # ( 1 - S ":=" )
6767 * SkipSpace
6768 * K ( 'TypeExpression' , ( 1 - P "\r" ) ^ 0 ) * -1
6769 )
6770 +
6771 (
6772 ( space ^ 0 * "\r" ) ^ -1
6773 * Lc [[ \@@_begin_line: ]]
6774 * LeadingSpace ^ 0
6775 * ( ( space * Lc [[ \@@_trailing_space: ]] ) ^ 1 * -1
6776 + space ^ 0 * EOL
6777 + Main
6778 ) ^ 0
6779 * -1
6780 * Lc [[ \@@_end_line: ]]
6781 )
6782 )
6783 % \end{macrocode}
6784 %
6785 % \bigskip
6786 % End of the Lua scope for the language OCaml.
6787 % \begin{macrocode}
6788 end
6789 % \end{macrocode}
6790 %
6791 %
6792 % \bigskip
6793 % \subsection{The language C}
6794 %
6795 % We open a Lua local scope for the language C (of course, there will be also
6796 % global definitions).
6797 % \begin{macrocode}
6798 --c C c++ C++
6799 do
6800 % \end{macrocode}
6801 %
6802 % \bigskip
6803 % \begin{macrocode}
6804 local Delim = Q ( S "{[()]}" )
6805 % \end{macrocode}
6806 %
6807 % \begin{macrocode}
6808 local Punct = Q ( S ",:;!" )
6809 % \end{macrocode}
6810 %
6811 % \bigskip
6812 % Some strings of length 2 are explicit because we want the corresponding
6813 % ligatures available in some fonts such as \emph{Fira Code} to be active.
6814 % \begin{macrocode}
6815 local identifier = letter * alphanum ^ 0
6816
6817 local Operator =
6818 K ( 'Operator' ,
6819 P "!=" + "==" + "<<" + ">>" + "<=" + ">=" + "||" + "&&"
6820 + S "-~+/*%=<>&.@|!" )
6821
6822 local Keyword =
6823 K ( 'Keyword' ,
6824 P "alignas" + "asm" + "auto" + "break" + "case" + "catch" + "class" +
6825 "const" + "constexpr" + "continue" + "decltype" + "do" + "else" + "enum" +
6826 "extern" + "for" + "goto" + "if" + "nexcept" + "private" + "public" +
6827 "register" + "restricted" + "return" + "static" + "static_assert" +
6828 "struct" + "switch" + "thread_local" + "throw" + "try" + "typedef" +
6829 "union" + "using" + "virtual" + "volatile" + "while"
6830 )
6831 + K ( 'Keyword.Constant' , P "default" + "false" + "NULL" + "nullptr" + "true" )
6832
6833 local Builtin =
6834 K ( 'Name.Builtin' ,
6835 P "alignof" + "malloc" + "printf" + "scanf" + "sizeof" )
6836
6837 local Type =
6838 K ( 'Name.Type' ,
6839 P "bool" + "char" + "char16_t" + "char32_t" + "double" + "float" +
6840 "int8_t" + "int16_t" + "int32_t" + "int64_t" + "uint8_t" + "uint16_t" +
6841 "uint32_t" + "uint64_t" + "int" + "long" + "short" + "signed" + "unsigned" +
6842 "void" + "wchar_t" ) * Q "*" ^ 0
6843
6844 local DefFunction =
6845 Type
6846 * Space
6847 * Q "*" ^ -1
6848 * K ( 'Name.Function.Internal' , identifier )
6849 * SkipSpace
6850 * # P "("
6851 % \end{macrocode}
6852 % We remind that the marker |#| of \textsc{lpeg} specifies that the pattern will be
6853 % detected but won't consume any character.
6854 %
6855 % \bigskip
6856 % The following \textsc{lpeg} |DefClass| will be used to detect the definition of a
6857 % new class (the name of that new class will be formatted with the \pkg{piton}
6858 % style |Name.Class|).
6859 %
6860 % \smallskip
6861 % Example:\enskip \piton{class myclass:}
6862 % \begin{macrocode}
6863 local DefClass =
6864 K ( 'Keyword' , "class" ) * Space * K ( 'Name.Class' , identifier )
6865 % \end{macrocode}
6866 %
6867 % If the word |class| is not followed by a identifier, it will be caught as
6868 % keyword by the \textsc{lpeg} |Keyword| (useful if we want to type a
6869 % list of keywords).
6870 %
6871 % \bigskip
6872 % \begin{macrocode}
6873 local Character =
6874 K ( 'String.Short' ,
6875 P [['\'']] + P "'" * ( 1 - P "'" ) ^ 0 * P "'" )
6876 % \end{macrocode}
6877 %
6878 % \bigskip
6879 % \paragraph{The strings of C}
6880 %
6881 % \begin{macrocode}
6882 String =
6883 WithStyle ( 'String.Long.Internal' ,
6884 Q "\""
6885 * ( SpaceInString
6886 + K ( 'String.Interpol' ,
6887 "%" * ( S "difcspxXou" + "ld" + "li" + "hd" + "hi" )
6888 )
6889 + Q ( ( P "\\\"" + 1 - S " \"" ) ^ 1 )
6890 ) ^ 0
6891 * Q "\""
6892 )
6893 % \end{macrocode}
6894 %
6895 % \bigskip
6896 % \paragraph{Beamer}
6897 %
6898 % \bigskip
6899 % The argument of |Compute_braces| must be a pattern \emph{which does no
6900 % catching} corresponding to the strings of the language.
6901 % \begin{macrocode}
6902 local braces = Compute_braces ( "\"" * ( 1 - S "\"" ) ^ 0 * "\"" )
6903 if piton.beamer then Beamer = Compute_Beamer ( 'c' , braces ) end
6904 % \end{macrocode}
6905 %
6906 % \begin{macrocode}
6907 DetectedCommands =
6908 Compute_DetectedCommands ( 'c' , braces )
6909 + Compute_RawDetectedCommands ( 'c' , braces )
6910 % \end{macrocode}
6911 %
6912 % \begin{macrocode}
6913 LPEG_cleaner.c = Compute_LPEG_cleaner ( 'c' , braces )
6914 % \end{macrocode}
6915 %
6916 % \bigskip
6917 % \paragraph{The directives of the preprocessor}
6918 %
6919 % \begin{macrocode}
6920 local Preproc = K ( 'Preproc' , "#" * ( 1 - P "\r" ) ^ 0 ) * ( EOL + -1 )
6921 % \end{macrocode}
6922 %
6923 %
6924 % \bigskip
6925 % \paragraph{The comments in the C listings}
6926 %
6927 % We define different \textsc{lpeg} dealing with comments in the C listings.
6928 % \begin{macrocode}
6929 local Comment =
6930 WithStyle ( 'Comment.Internal' ,
6931 Q "//" * ( CommentMath + Q ( ( 1 - S "$\r" ) ^ 1 ) ) ^ 0 ) -- $
6932 * ( EOL + -1 )
6933
6934 local LongComment =
6935 WithStyle ( 'Comment.Internal' ,
6936 Q "/*"
6937 * ( CommentMath + Q ( ( 1 - P "*/" - S "$\r" ) ^ 1 ) + EOL ) ^ 0
6938 * Q "*/"
6939 ) -- $
6940 % \end{macrocode}
6941 %
6942 %
6943 %
6944 %
6945 % \bigskip
6946 % \paragraph{The main LPEG for the language C}
6947 %
6948 % \begin{macrocode}
6949 local EndKeyword
6950 = Space + Punct + Delim + EOL + Beamer + DetectedCommands + Escape +
6951 EscapeMath + -1
6952 % \end{macrocode}
6953 %
6954 % First, the main loop :
6955 % \begin{macrocode}
6956 local Main =
6957 space ^ 0 * EOL
6958 + Space
6959 + Tab
6960 + Escape + EscapeMath
6961 + CommentLaTeX
6962 + Beamer
6963 + DetectedCommands
6964 + Preproc
6965 + Comment + LongComment
6966 + Delim
6967 + Operator
6968 + Character
6969 + String
6970 + Punct
6971 + DefFunction
6972 + DefClass
6973 + Type * ( Q "*" ^ -1 + EndKeyword )
6974 + Keyword * EndKeyword
6975 + Builtin * EndKeyword
6976 + Identifier
6977 + Number
6978 + Word
6979 % \end{macrocode}
6980 %
6981 % \bigskip
6982 % Here, we must not put |local|, of course.
6983 % \begin{macrocode}
6984 LPEG1.c = Main ^ 0
6985 % \end{macrocode}
6986 %
6987 % \bigskip
6988 % We recall that each line in the C code to parse will be sent back to
6989 % LaTeX between a pair |\@@_begin_line:| -- |\@@_end_line:|\footnote{Remember
6990 % that the \texttt{\textbackslash @@\_end\_line:} must be explicit because it
6991 % will be used as marker in order to delimit the argument of the command
6992 % \texttt{\textbackslash @@\_begin\_line:}}.
6993 % \begin{macrocode}
6994 LPEG2.c =
6995 Ct (
6996 ( space ^ 0 * P "\r" ) ^ -1
6997 * Lc [[ \@@_begin_line: ]]
6998 * LeadingSpace ^ 0
6999 * ( space ^ 1 * -1 + Main ) ^ 0
7000 * -1
7001 * Lc [[ \@@_end_line: ]]
7002 )
7003 % \end{macrocode}
7004 %
7005 % \bigskip
7006 % End of the Lua scope for the language C.
7007 % \begin{macrocode}
7008 end
7009 % \end{macrocode}
7010 %
7011 % \bigskip
7012 % \subsection{The language SQL}
7013 %
7014 % We open a Lua local scope for the language SQL (of course, there will be also
7015 % global definitions).
7016 % \begin{macrocode}
7017 --sql SQL
7018 do
7019 % \end{macrocode}
7020 %
7021 % \bigskip
7022 % \begin{macrocode}
7023 local LuaKeyword
7024 function LuaKeyword ( name ) return
7025 Lc [[ {\PitonStyle{Keyword}{ ]]
7026 * Q ( Cmt (
7027 C ( letter * alphanum ^ 0 ) ,
7028 function ( _ , _ , a ) return a : upper ( ) == name end
7029 )
7030 )
7031 * Lc "}}"
7032 end
7033 % \end{macrocode}
7034 %
7035 % \bigskip
7036 % In the identifiers, we will be able to catch those contening spaces, that is
7037 % to say like |"last name"|.
7038 % \begin{macrocode}
7039 local identifier =
7040 letter * ( alphanum + "-" ) ^ 0
7041 + P '"' * ( ( 1 - P '"' ) ^ 1 ) * '"'
7042 % \end{macrocode}
7043 %
7044 % \begin{macrocode}
7045 local Operator =
7046 K ( 'Operator' , P "=" + "!=" + "<>" + ">=" + ">" + "<=" + "<" + S "*+/" )
7047 % \end{macrocode}
7048 %
7049 % In SQL, the keywords are case-insensitive. That's why we have a little
7050 % complication. We will catch the keywords with the identifiers and, then,
7051 % distinguish the keywords with a Lua function. However, some keywords will be
7052 % caught in special LPEG because we want to detect the names of the SQL tables.
7053 %
7054 % \bigskip
7055 % The following function converts a comma-separated list in a ``set'', that is
7056 % to say a Lua table with a fast way to test whether a string belongs to that
7057 % set (eventually, the indexation of the components of the table is no longer
7058 % done by integers but by the strings themselves).
7059 % \begin{macrocode}
7060 local Set
7061 function Set ( list )
7062 local set = { }
7063 for _ , l in ipairs ( list ) do set[l] = true end
7064 return set
7065 end
7066 % \end{macrocode}
7067 %
7068 % \bigskip
7069 % We now use the previous function |Set| to creates the ``sets'' |set_keywords|
7070 % and |set_builtin|. That list of keywords comes from
7071 % \url{https://sqlite.org/lang_keywords.html}.
7072 % \begin{macrocode}
7073 local set_keywords = Set
7074 {
7075 "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ALWAYS", "ANALYZE",
7076 "AND", "AS", "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN",
7077 "BY", "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT",
7078 "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT", "CURRENT_DATE",
7079 "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE",
7080 "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DO", "DROP", "EACH",
7081 "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUDE", "EXCLUSIVE", "EXISTS",
7082 "EXPLAIN", "FAIL", "FILTER", "FIRST", "FOLLOWING", "FOR", "FOREIGN", "FROM",
7083 "FULL", "GENERATED", "GLOB", "GROUP", "GROUPS", "HAVING", "IF", "IGNORE",
7084 "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER", "INSERT",
7085 "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY", "LAST",
7086 "LEFT", "LIKE", "LIMIT", "MATCH", "MATERIALIZED", "NATURAL", "NO", "NOT",
7087 "NOTHING", "NOTNULL", "NULL", "NULLS", "OF", "OFFSET", "ON", "OR", "ORDER",
7088 "OTHERS", "OUTER", "OVER", "PARTITION", "PLAN", "PRAGMA", "PRECEDING",
7089 "PRIMARY", "QUERY", "RAISE", "RANGE", "RECURSIVE", "REFERENCES", "REGEXP",
7090 "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RETURNING", "RIGHT",
7091 "ROLLBACK", "ROW", "ROWS", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP",
7092 "TEMPORARY", "THEN", "TIES", "TO", "TRANSACTION", "TRIGGER", "UNBOUNDED",
7093 "UNION", "UNIQUE", "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL",
7094 "WHEN", "WHERE", "WINDOW", "WITH", "WITHOUT"
7095 }
7096 % \end{macrocode}
7097 %
7098 % \begin{macrocode}
7099 local set_builtins = Set
7100 {
7101 "AVG" , "COUNT" , "CHAR_LENGTH" , "CONCAT" , "CURDATE" , "CURRENT_DATE" ,
7102 "DATE_FORMAT" , "DAY" , "LOWER" , "LTRIM" , "MAX" , "MIN" , "MONTH" , "NOW" ,
7103 "RANK" , "ROUND" , "RTRIM" , "SUBSTRING" , "SUM" , "UPPER" , "YEAR"
7104 }
7105 % \end{macrocode}
7106 %
7107 % The \textsc{lpeg} |Identifier| will catch the identifiers of the fields
7108 % but also the keywords and the built-in functions of SQL. If will \emph{not}
7109 % catch the names of the SQL tables.
7110 % \begin{macrocode}
7111 local Identifier =
7112 C ( identifier ) /
7113 (
7114 function ( s )
7115 if set_keywords [ s : upper ( ) ] then return
7116 % \end{macrocode}
7117 % Remind that, in Lua, it's possible to return \emph{several} values.
7118 % \begin{macrocode}
7119 { [[{\PitonStyle{Keyword}{]] } ,
7120 { luatexbase.catcodetables.other , s } ,
7121 { "}}" }
7122 else
7123 if set_builtins [ s : upper ( ) ] then return
7124 { [[{\PitonStyle{Name.Builtin}{]] } ,
7125 { luatexbase.catcodetables.other , s } ,
7126 { "}}" }
7127 else return
7128 { [[{\PitonStyle{Name.Field}{]] } ,
7129 { luatexbase.catcodetables.other , s } ,
7130 { "}}" }
7131 end
7132 end
7133 end
7134 )
7135 % \end{macrocode}
7136 %
7137 % \bigskip
7138 % \paragraph{The strings of SQL}
7139 %
7140 % \begin{macrocode}
7141 local String = K ( 'String.Long.Internal' , "'" * ( 1 - P "'" ) ^ 1 * "'" )
7142 % \end{macrocode}
7143 %
7144 % \bigskip
7145 % \paragraph{Beamer}
7146 %
7147 % \bigskip
7148 % The argument of |Compute_braces| must be a pattern \emph{which does no
7149 % catching} corresponding to the strings of the language.
7150 %
7151 % \begin{macrocode}
7152 local braces = Compute_braces ( "'" * ( 1 - P "'" ) ^ 1 * "'" )
7153 if piton.beamer then Beamer = Compute_Beamer ( 'sql' , braces ) end
7154 % \end{macrocode}
7155 %
7156 % \begin{macrocode}
7157 DetectedCommands =
7158 Compute_DetectedCommands ( 'sql' , braces )
7159 + Compute_RawDetectedCommands ( 'sql' , braces )
7160 % \end{macrocode}
7161 %
7162 % \begin{macrocode}
7163 LPEG_cleaner.sql = Compute_LPEG_cleaner ( 'sql' , braces )
7164 % \end{macrocode}
7165 %
7166 %
7167 %
7168 % \bigskip
7169 % \paragraph{The comments in the SQL listings}
7170 %
7171 % We define different \textsc{lpeg} dealing with comments in the SQL listings.
7172 % \begin{macrocode}
7173 local Comment =
7174 WithStyle ( 'Comment.Internal' ,
7175 Q "--" -- syntax of SQL92
7176 * ( CommentMath + Q ( ( 1 - S "$\r" ) ^ 1 ) ) ^ 0 ) -- $
7177 * ( EOL + -1 )
7178
7179 local LongComment =
7180 WithStyle ( 'Comment.Internal' ,
7181 Q "/*"
7182 * ( CommentMath + Q ( ( 1 - P "*/" - S "$\r" ) ^ 1 ) + EOL ) ^ 0
7183 * Q "*/"
7184 ) -- $
7185 % \end{macrocode}
7186 %
7187 %
7188 %
7189 % \bigskip
7190 % \paragraph{The main LPEG for the language SQL}
7191 %
7192 % \begin{macrocode}
7193 local EndKeyword
7194 = Space + Punct + Delim + EOL + Beamer + DetectedCommands + Escape +
7195 EscapeMath + -1
7196 % \end{macrocode}
7197 %
7198 % \begin{macrocode}
7199 local TableField =
7200 K ( 'Name.Table' , identifier )
7201 * Q "."
7202 * ( DetectedCommands + ( K ( 'Name.Field' , identifier ) ) ^ 0 )
7203
7204 local OneField =
7205 (
7206 Q ( "(" * ( 1 - P ")" ) ^ 0 * ")" )
7207 +
7208 K ( 'Name.Table' , identifier )
7209 * Q "."
7210 * K ( 'Name.Field' , identifier )
7211 +
7212 K ( 'Name.Field' , identifier )
7213 )
7214 * (
7215 Space * LuaKeyword "AS" * Space * K ( 'Name.Field' , identifier )
7216 ) ^ -1
7217 * ( Space * ( LuaKeyword "ASC" + LuaKeyword "DESC" ) ) ^ -1
7218
7219 local OneTable =
7220 K ( 'Name.Table' , identifier )
7221 * (
7222 Space
7223 * LuaKeyword "AS"
7224 * Space
7225 * K ( 'Name.Table' , identifier )
7226 ) ^ -1
7227
7228 local WeCatchTableNames =
7229 LuaKeyword "FROM"
7230 * ( Space + EOL )
7231 * OneTable * ( SkipSpace * Q "," * SkipSpace * OneTable ) ^ 0
7232 + (
7233 LuaKeyword "JOIN" + LuaKeyword "INTO" + LuaKeyword "UPDATE"
7234 + LuaKeyword "TABLE"
7235 )
7236 * ( Space + EOL ) * OneTable
7237 % \end{macrocode}
7238 %
7239 % \begin{macrocode}
7240 local EndKeyword
7241 = Space + Punct + Delim + EOL + Beamer
7242 + DetectedCommands + Escape + EscapeMath + -1
7243 % \end{macrocode}
7244 %
7245 %
7246 % First, the main loop :
7247 % \begin{macrocode}
7248 local Main =
7249 space ^ 0 * EOL
7250 + Space
7251 + Tab
7252 + Escape + EscapeMath
7253 + CommentLaTeX
7254 + Beamer
7255 + DetectedCommands
7256 + Comment + LongComment
7257 + Delim
7258 + Operator
7259 + String
7260 + Punct
7261 + WeCatchTableNames
7262 + ( TableField + Identifier ) * ( Space + Operator + Punct + Delim + EOL + -1 )
7263 + Number
7264 + Word
7265 % \end{macrocode}
7266 %
7267 % \bigskip
7268 % Here, we must not put |local|, of course.
7269 % \begin{macrocode}
7270 LPEG1.sql = Main ^ 0
7271 % \end{macrocode}
7272 %
7273 % \bigskip
7274 % We recall that each line in the code to parse will be sent back to
7275 % LaTeX between a pair |\@@_begin_line:| -- |\@@_end_line:|\footnote{Remember
7276 % that the \texttt{\textbackslash @@\_end\_line:} must be explicit because it
7277 % will be used as marker in order to delimit the argument of the command
7278 % \texttt{\textbackslash @@\_begin\_line:}}.
7279 % \begin{macrocode}
7280 LPEG2.sql =
7281 Ct (
7282 ( space ^ 0 * "\r" ) ^ -1
7283 * Lc [[ \@@_begin_line: ]]
7284 * LeadingSpace ^ 0
7285 * ( space ^ 1 * -1 + Main ) ^ 0
7286 * -1
7287 * Lc [[ \@@_end_line: ]]
7288 )
7289 % \end{macrocode}
7290 %
7291 % \bigskip
7292 % End of the Lua scope for the language SQL.
7293 % \begin{macrocode}
7294 end
7295 % \end{macrocode}
7296 %
7297 % \subsection{The language ``Minimal''}
7298 %
7299 % We open a Lua local scope for the language ``Minimal'' (of course, there
7300 % will be also global definitions).
7301 % \begin{macrocode}
7302 --minimal Minimal
7303 do
7304 % \end{macrocode}
7305 %
7306 % \begin{macrocode}
7307 local Punct = Q ( S ",:;!\\" )
7308
7309 local Comment =
7310 WithStyle ( 'Comment.Internal' ,
7311 Q "#"
7312 * ( CommentMath + Q ( ( 1 - S "$\r" ) ^ 1 ) ) ^ 0 -- $
7313 )
7314 * ( EOL + -1 )
7315
7316 local String =
7317 WithStyle ( 'String.Short.Internal' ,
7318 Q "\""
7319 * ( SpaceInString
7320 + Q ( ( P [[\"]] + 1 - S " \"" ) ^ 1 )
7321 ) ^ 0
7322 * Q "\""
7323 )
7324 % \end{macrocode}
7325 %
7326 % \bigskip
7327 % The argument of |Compute_braces| must be a pattern \emph{which does no
7328 % catching} corresponding to the strings of the language.
7329 % \begin{macrocode}
7330 local braces = Compute_braces ( P "\"" * ( P "\\\"" + 1 - P "\"" ) ^ 1 * "\"" )
7331
7332 if piton.beamer then Beamer = Compute_Beamer ( 'minimal' , braces ) end
7333
7334 DetectedCommands =
7335 Compute_DetectedCommands ( 'minimal' , braces )
7336 + Compute_RawDetectedCommands ( 'minimal' , braces )
7337
7338 LPEG_cleaner.minimal = Compute_LPEG_cleaner ( 'minimal' , braces )
7339
7340 local identifier = letter * alphanum ^ 0
7341
7342 local Identifier = K ( 'Identifier.Internal' , identifier )
7343
7344 local Delim = Q ( S "{[()]}" )
7345
7346 local Main =
7347 space ^ 0 * EOL
7348 + Space
7349 + Tab
7350 + Escape + EscapeMath
7351 + CommentLaTeX
7352 + Beamer
7353 + DetectedCommands
7354 + Comment
7355 + Delim
7356 + String
7357 + Punct
7358 + Identifier
7359 + Number
7360 + Word
7361 % \end{macrocode}
7362 %
7363 % \bigskip
7364 % Here, we must not put |local|, of course.
7365 % \begin{macrocode}
7366 LPEG1.minimal = Main ^ 0
7367
7368 LPEG2.minimal =
7369 Ct (
7370 ( space ^ 0 * "\r" ) ^ -1
7371 * Lc [[ \@@_begin_line: ]]
7372 * LeadingSpace ^ 0
7373 * ( space ^ 1 * -1 + Main ) ^ 0
7374 * -1
7375 * Lc [[ \@@_end_line: ]]
7376 )
7377 % \end{macrocode}
7378 %
7379 %
7380 % \bigskip
7381 % End of the Lua scope for the language ``Minimal''.
7382 % \begin{macrocode}
7383 end
7384 % \end{macrocode}
7385 %
7386 % \subsection{The language ``Verbatim''}
7387 %
7388 % We open a Lua local scope for the language ``Verbatim'' (of course, there
7389 % will be also global definitions).
7390 % \begin{macrocode}
7391 --verbatim Verbatim
7392 do
7393 % \end{macrocode}
7394
7395 % \bigskip
7396 % Here, we don't use |braces| as done with the other languages because we don't
7397 % have have to take into account the strings (there is no string in the langage
7398 % ``Verbatim'').
7399 % \begin{macrocode}
7400 local braces =
7401 P { "E" ,
7402 E = ( "{" * V "E" * "}" + ( 1 - S "{}" ) ) ^ 0
7403 }
7404
7405 if piton.beamer then Beamer = Compute_Beamer ( 'verbatim' , braces ) end
7406
7407 DetectedCommands =
7408 Compute_DetectedCommands ( 'verbatim' , braces )
7409 + Compute_RawDetectedCommands ( 'verbatim' , braces )
7410
7411 LPEG_cleaner.verbatim = Compute_LPEG_cleaner ( 'verbatim' , braces )
7412 % \end{macrocode}
7413 %
7414 % Now, you will construct the LPEG Word.
7415 % \begin{macrocode}
7416 local lpeg_central = 1 - S " \\\r"
7417 if piton.begin_escape then
7418 lpeg_central = lpeg_central - piton.begin_escape
7419 end
7420 if piton.begin_escape_math then
7421 lpeg_central = lpeg_central - piton.begin_escape_math
7422 end
7423 local Word = Q ( lpeg_central ^ 1 )
7424
7425 local Main =
7426 space ^ 0 * EOL
7427 + Space
7428 + Tab
7429 + Escape + EscapeMath
7430 + Beamer
7431 + DetectedCommands
7432 + Q [[\]]
7433 + Word
7434 % \end{macrocode}
7435 %
7436 % \bigskip
7437 % Here, we must not put |local|, of course.
7438 % \begin{macrocode}
7439 LPEG1.verbatim = Main ^ 0
7440
7441 LPEG2.verbatim =
7442 Ct (
7443 ( space ^ 0 * "\r" ) ^ -1
7444 * Lc [[ \@@_begin_line: ]]
7445 * LeadingSpace ^ 0
7446 * ( space ^ 1 * -1 + Main ) ^ 0
7447 * -1
7448 * Lc [[ \@@_end_line: ]]
7449 )
7450 % \end{macrocode}
7451 %
7452 %
7453 % \bigskip
7454 % End of the Lua scope for the language ``|verbatim|''.
7455 % \begin{macrocode}
7456 end
7457 % \end{macrocode}
7458 %
7459 %
7460 % \subsection{The language expl}
7461 %
7462 % We open a Lua local scope for the language |expl| of LaTeX3 (of course, there
7463 % will be also global definitions).
7464 % \begin{macrocode}
7465 --EXPL expl
7466 do
7467 % \end{macrocode}
7468 %
7469 % \begin{macrocode}
7470 local Comment =
7471 WithStyle
7472 ( 'Comment.Internal' ,
7473 Q "%" * ( CommentMath + Q ( ( 1 - S "$\r" ) ^ 1 ) ) ^ 0 -- $
7474 )
7475 * ( EOL + -1 )
7476 % \end{macrocode}
7477 %
7478 % \medskip
7479 % First, we begin with a special function to analyse the ``keywords'', that is to
7480 % say the control sequences beginning by ``|\|''.
7481 % \begin{macrocode}
7482 local analyze_cs
7483 function analyze_cs ( s )
7484 local i = s : find ( ":" )
7485 if i then
7486 % \end{macrocode}
7487 % First, the case of what might be called a ``function'' in |expl|, for instance,
7488 % |\tl_set:Nn| or |\int_compare:nNnTF|.
7489 % \begin{macrocode}
7490 local name = s : sub ( 2 , i - 1 )
7491 local parts = name : explode ( "_" )
7492 local module = parts[1]
7493 if module == "" then module = parts[3] end
7494 % \end{macrocode}
7495 % Remind that, in Lua, we can return \emph{several} values.
7496 % \begin{macrocode}
7497 return
7498 { [[{\OptionalLocalPitonStyle{Module.]] .. module .. "}{" } ,
7499 { luatexbase.catcodetables.other , s } ,
7500 { "}}" }
7501 else
7502 % \end{macrocode}
7503 % \begin{macrocode}
7504 local p = s : sub ( 1 , 3 )
7505 if p == [[\l_]] or p == [[\g_]] or p == [[\c_]] then
7506 % \end{macrocode}
7507 % The case of what might be called a ``variable'', for instance,
7508 % |\l_tmpa_int| or |\g__module_text_tl|.
7509 % \begin{macrocode}
7510 local scope = s : sub(2,2)
7511 local parts = s : explode ( "_" )
7512 local module = parts[2]
7513 if module == "" then module = parts[3] end
7514 local type = parts[#parts]
7515 return
7516 { [[{\OptionalLocalPitonStyle{Scope.]] .. scope .. "}{" } ,
7517 { [[{\OptionalLocalPitonStyle{Module.]] .. module .. "}{" } ,
7518 { [[{\OptionalLocalPitonStyle{Type.]] .. type .. "}{" } ,
7519 { luatexbase.catcodetables.other , s } ,
7520 { "}}}}}}" }
7521 else
7522 % \end{macrocode}
7523 % We have a control sequence which is neither a ``function'' neither a ``variable''
7524 % of |expl|. It's a control sequence of standard LaTeX and we don't format it.
7525 % \begin{macrocode}
7526 return { luatexbase.catcodetables.other , s }
7527 end
7528 end
7529 end
7530 % \end{macrocode}
7531 %
7532 % \bigskip
7533 % Here, we don't use |braces| as done with the other languages because we don't
7534 % have have to take into account the strings (there is no string in the langage
7535 % |expl|).
7536 % \begin{macrocode}
7537 local braces =
7538 P { "E" ,
7539 E = ( "{" * V "E" * "}" + ( 1 - S "{}" ) ) ^ 0
7540 }
7541
7542 if piton.beamer then Beamer = Compute_Beamer ( 'expl' , braces ) end
7543
7544 DetectedCommands =
7545 Compute_DetectedCommands ( 'expl' , braces )
7546 + Compute_RawDetectedCommands ( 'expl' , braces )
7547
7548 LPEG_cleaner.expl = Compute_LPEG_cleaner ( 'expl' , braces )
7549 % \end{macrocode}
7550 % \begin{macrocode}
7551 local control_sequence = P "\\" * ( R "Az" + "_" + ":" + "@" ) ^ 1
7552 local ControlSequence = C ( control_sequence ) / analyze_cs
7553 % \end{macrocode}
7554 %
7555 % \begin{macrocode}
7556 local def_function
7557 = P [[\cs_]]
7558 * ( P "set" + "new")
7559 * ( P "_protected" ) ^ -1
7560 * P ":N" * ( P "p" ) ^ -1 * "n"
7561 % \end{macrocode}
7562 %
7563 % \begin{macrocode}
7564 local DefFunction =
7565 C ( def_function ) / analyze_cs
7566 * Space
7567 * Lc ( [[ {\PitonStyle{Name.Function}{ ]] )
7568 * ControlSequence -- Q ( ControlSequence ) ?
7569 * Lc "}}"
7570 % \end{macrocode}
7571 %
7572 % \begin{macrocode}
7573 local Word = Q ( ( 1 - S " \r" ) ^ 1 )
7574
7575 local Main =
7576 space ^ 0 * EOL
7577 + Space
7578 + Tab
7579 + Escape + EscapeMath
7580 + Beamer
7581 + Comment
7582 + DetectedCommands
7583 + DefFunction
7584 + ControlSequence
7585 + Word
7586 % \end{macrocode}
7587 %
7588 % \bigskip
7589 % Here, we must not put |local|, of course.
7590 % \begin{macrocode}
7591 LPEG1.expl = Main ^ 0
7592
7593 LPEG2.expl =
7594 Ct (
7595 ( space ^ 0 * "\r" ) ^ -1
7596 * Lc [[ \@@_begin_line: ]]
7597 * LeadingSpace ^ 0
7598 * ( space ^ 1 * -1 + Main ) ^ 0
7599 * -1
7600 * Lc [[ \@@_end_line: ]]
7601 )
7602 % \end{macrocode}
7603 %
7604 %
7605 % \bigskip
7606 % End of the Lua scope for the language |expl| of LaTeX3.
7607 % \begin{macrocode}
7608 end
7609 % \end{macrocode}
7610 %
7611 %
7612 % \bigskip
7613 % \subsection{The function Parse}
7614 %
7615 % \medskip
7616 % The function |Parse| is the main function of the package \pkg{piton}. It
7617 % parses its argument and sends back to LaTeX the code with interlaced
7618 % formatting LaTeX instructions. In fact, everything is done by the
7619 % \textsc{lpeg} corresponding to the considered language (|LPEG2[language]|)
7620 % which returns as capture a Lua table containing data to send to LaTeX.
7621 %
7622 % \bigskip
7623 % \begin{macrocode}
7624 function piton.Parse ( language , code )
7625 % \end{macrocode}
7626 % The variable |piton.language| will be used by the function |ParseAgain|.
7627 % \begin{macrocode}
7628 piton.language = language
7629 local t = LPEG2[language] : match ( code )
7630 if not t then
7631 sprintL3 [[ \@@_error_or_warning:n { SyntaxError } ]]
7632 return -- to exit in force the function
7633 end
7634 local left_stack = {}
7635 local right_stack = {}
7636 for _ , one_item in ipairs ( t ) do
7637 if one_item == "EOL" then
7638 for i = #right_stack, 1, -1 do
7639 tex.sprint ( right_stack[i] )
7640 end
7641 % \end{macrocode}
7642 % We remind that the |\@@_end_line:| must be explicit since it's the marker
7643 % of end of the command |\@@_begin_line:|.
7644 % \begin{macrocode}
7645 sprintL3 ( [[ \@@_end_line: \@@_par: \@@_begin_line: ]] )
7646 tex.sprint ( table.concat ( left_stack ) )
7647 else
7648 % \end{macrocode}
7649 %
7650 % Here is an example of an item beginning with |"Open"|.
7651 %
7652 % |{ "Open" , "\begin{uncoverenv}<2>" , "\end{uncoverenv}" }|
7653 %
7654 % In order to deal with the ends of lines, we have to close the environment
7655 % (|{uncoverenv}| in this example) at the end of each line and reopen it at the
7656 % beginning of the new line. That's why we use two Lua stacks, called
7657 % |left_stack| and |right_stack|. |left_stack| will be for the elements like
7658 % |\begin{uncoverenv}<2>| and |right_stack| will be for the elements like
7659 % |\end{uncoverenv}|.
7660 % \begin{macrocode}
7661 if one_item[1] == "Open" then
7662 tex.sprint ( one_item[2] )
7663 table.insert ( left_stack , one_item[2] )
7664 table.insert ( right_stack , one_item[3] )
7665 else
7666 if one_item[1] == "Close" then
7667 tex.sprint ( right_stack[#right_stack] )
7668 left_stack[#left_stack] = nil
7669 right_stack[#right_stack] = nil
7670 else
7671 tex.tprint ( one_item )
7672 end
7673 end
7674 end
7675 end
7676 end
7677 % \end{macrocode}
7678 %
7679 % \bigskip
7680 % There is the problem of the conventions of end of lines (|\n| in Unix and
7681 % Linux but |\r\n| in Windows). The function |my_file_lines| will read a file
7682 % line by line after replacement of the potential |\r\n| by |\n| (that means that
7683 % we go the convention \textsc{unix}).
7684 % \begin{macrocode}
7685 local my_file_lines
7686 function my_file_lines ( filename )
7687 local f = io.open ( filename , 'rb' )
7688 local s = f : read ( '*a' )
7689 f : close ( )
7690 % \end{macrocode}
7691 % À la fin, on doit bien mettre |(.-)| et pas |(.*)|.
7692 % \begin{macrocode}
7693 return ( s .. '\n' ) : gsub( '\r\n?' , '\n') : gmatch ( '(.-)\n' )
7694 end
7695 % \end{macrocode}
7696 % Recall that, in Lua, |gmatch| returns an \emph{iterator}.
7697 %
7698 %
7699 % \bigskip
7700 % \begin{macrocode}
7701 function piton.ReadFile ( name , first_line , last_line )
7702 local s = ''
7703 local i = 0
7704 for line in my_file_lines ( name ) do
7705 i = i + 1
7706 if i >= first_line then
7707 s = s .. '\r' .. line
7708 end
7709 if i >= last_line then break end
7710 end
7711 % \end{macrocode}
7712 %
7713 % \bigskip
7714 % We extract the BOM of utf-8, if present.
7715 % \begin{macrocode}
7716 if s : sub ( 1 , 4 ) == string.char ( 13 , 239 , 187 , 191 ) then
7717 s = s : sub ( 5 , -1 )
7718 end
7719 % \end{macrocode}
7720 %
7721 % \begin{macrocode}
7722 sprintL3 ( [[ \tl_set:Nn \l_@@_listing_tl { ]])
7723 tex.sprint ( luatexbase.catcodetables.other , s )
7724 sprintL3 ( "}" )
7725 end
7726 % \end{macrocode}
7727 %
7728 % \bigskip
7729 % \begin{macrocode}
7730 function piton.RetrieveGobbleParse ( lang , n , splittable , code )
7731 local s
7732 s = ( ( P " " ^ 0 * "\r" ) ^ -1 * C ( P ( 1 ) ^ 0 ) * -1 ) : match ( code )
7733 piton.GobbleParse ( lang , n , splittable , s )
7734 end
7735 % \end{macrocode}
7736 %
7737 % \bigskip
7738 % \subsection{Two variants of the function Parse with integrated preprocessors}
7739 %
7740 % The following command will be used by the user command |\piton|.
7741 % For that command, we have to undo the duplication of the symbols |#|.
7742 % \begin{macrocode}
7743 function piton.ParseBis ( lang , code )
7744 return piton.Parse ( lang , code : gsub ( '##' , '#' ) )
7745 end
7746 % \end{macrocode}
7747 % Of course, |gsub| spans the string only once for the substitutions, which
7748 % means that |####| will be replaced by |##| as expected and not by |#|.
7749 %
7750 % \bigskip
7751 % The following command will be used when we have to parse some small chunks of
7752 % code that have yet been parsed. They are re-scanned by LaTeX because it has
7753 % been required by |\@@_piton:n| in the \pkg{piton} style of the syntaxic
7754 % element. In that case, you have to remove the potential \verb|\@@_breakable_space:|
7755 % that have been inserted when the key |break-lines| is in force.
7756 % \begin{macrocode}
7757 function piton.ParseTer ( lang , code )
7758 % \end{macrocode}
7759 % Be careful: we have to write |[[\@@_breakable_space: ]]| with a space after
7760 % the name of the LaTeX command |\@@_breakable_space:|.
7761 % \begin{macrocode}
7762 return piton.Parse
7763 (
7764 lang ,
7765 code : gsub ( [[\@@_breakable_space: ]] , ' ' )
7766 )
7767 end
7768 % \end{macrocode}
7769 %
7770 %
7771 % \bigskip
7772 % \subsection{Preprocessors of the function Parse for gobble}
7773 %
7774 % We deal now with preprocessors of the function |Parse| which are needed when
7775 % the ``gobble mechanism'' is used.
7776 %
7777 %
7778 % \bigskip
7779 % The following \textsc{lpeg} returns as capture the minimal number of spaces at
7780 % the beginning of the lines of code.
7781 % \begin{macrocode}
7782 local AutoGobbleLPEG =
7783 ( (
7784 P " " ^ 0 * "\r"
7785 +
7786 Ct ( C " " ^ 0 ) / table.getn
7787 * ( 1 - P " " ) * ( 1 - P "\r" ) ^ 0 * "\r"
7788 ) ^ 0
7789 * ( Ct ( C " " ^ 0 ) / table.getn
7790 * ( 1 - P " " ) * ( 1 - P "\r" ) ^ 0 ) ^ -1
7791 ) / math.min
7792 % \end{macrocode}
7793 %
7794 % \bigskip
7795 % The following \textsc{lpeg} is similar but works with the tabulations.
7796 % \begin{macrocode}
7797 local TabsAutoGobbleLPEG =
7798 (
7799 (
7800 P "\t" ^ 0 * "\r"
7801 +
7802 Ct ( C "\t" ^ 0 ) / table.getn
7803 * ( 1 - P "\t" ) * ( 1 - P "\r" ) ^ 0 * "\r"
7804 ) ^ 0
7805 * ( Ct ( C "\t" ^ 0 ) / table.getn
7806 * ( 1 - P "\t" ) * ( 1 - P "\r" ) ^ 0 ) ^ -1
7807 ) / math.min
7808 % \end{macrocode}
7809 %
7810 % \bigskip
7811 % The following \textsc{lpeg} returns as capture the number of spaces at the
7812 % last line, that is to say before the |\end{Piton}| (and usually it's also the
7813 % number of spaces before the corresponding |\begin{Piton}| because that's the
7814 % traditional way to indent in LaTeX).
7815 % \begin{macrocode}
7816 local EnvGobbleLPEG =
7817 ( ( 1 - P "\r" ) ^ 0 * "\r" ) ^ 0
7818 * Ct ( C " " ^ 0 * -1 ) / table.getn
7819 % \end{macrocode}
7820 %
7821 %
7822 % \bigskip
7823 % The function |gobble| gobbles $n$ characters on the left of the code. The
7824 % negative values of $n$ have special significations.
7825 % \begin{macrocode}
7826 function piton.Gobble ( n , code )
7827 if n == 0 then return
7828 code
7829 else
7830 if n == -1 then
7831 n = AutoGobbleLPEG : match ( code )
7832 % \end{macrocode}
7833 % for the case of an empty environment (only blank lines)
7834 % \begin{macrocode}
7835 if tonumber(n) then else n = 0 end
7836 else
7837 if n == -2 then
7838 n = EnvGobbleLPEG : match ( code )
7839 else
7840 if n == -3 then
7841 n = TabsAutoGobbleLPEG : match ( code )
7842 if tonumber(n) then else n = 0 end
7843 end
7844 end
7845 end
7846 % \end{macrocode}
7847 % We have a second test |if n == 0| because, even if the key like
7848 % |auto-gobble| is in force, it's possible that, in fact, there is no space to
7849 % gobble...
7850 % \begin{macrocode}
7851 if n == 0 then return
7852 code
7853 else return
7854 % \end{macrocode}
7855 % We will now use a \textsc{lpeg} that we have to compute dynamically because it
7856 % depends on the value of~$n$.
7857 % \begin{macrocode}
7858 ( Ct (
7859 ( 1 - P "\r" ) ^ (-n) * C ( ( 1 - P "\r" ) ^ 0 )
7860 * ( C "\r" * ( 1 - P "\r" ) ^ (-n) * C ( ( 1 - P "\r" ) ^ 0 )
7861 ) ^ 0 )
7862 / table.concat
7863 ) : match ( code )
7864 end
7865 end
7866 end
7867 % \end{macrocode}
7868 %
7869 %
7870 %
7871 % \bigskip
7872 % In the following code, |n| is the value of |\l_@@_gobble_int|.
7873 %
7874 % |splittable| is the value of |\l_@@_splittable_int|.
7875 % \begin{macrocode}
7876 function piton.GobbleParse ( lang , n , splittable , code )
7877 piton.ComputeLinesStatus ( code , splittable )
7878 piton.last_code = piton.Gobble ( n , code )
7879 piton.last_language = lang
7880 % \end{macrocode}
7881 % We count the number of lines of the computer listing. The result will be stored
7882 % by Lua in |\g_@@_nb_lines_int|.
7883 % \begin{macrocode}
7884 piton.CountLines ( piton.last_code )
7885 piton.Parse ( lang , piton.last_code )
7886 piton.join_and_write ( )
7887 end
7888 % \end{macrocode}
7889 %
7890 % \bigskip
7891 % The following function will be used when the end user has used the key |join|
7892 % or the key |write|.
7893 % The value of the key |join| has been written in the Lua variable |piton.join|.
7894 % \begin{macrocode}
7895 function piton.join_and_write ( )
7896 if piton.join ~= '' then
7897 if not piton.join_files [ piton.join ] then
7898 piton.join_files [ piton.join ] = piton.get_last_code ( )
7899 else
7900 if piton.join_separation == '' then
7901 piton.join_files [ piton.join ] =
7902 piton.join_files [ piton.join ]
7903 .. "\r\n"
7904 .. piton.get_last_code ( )
7905 else
7906 piton.join_files [ piton.join ] =
7907 piton.join_files [ piton.join ]
7908 .. "\r\n"
7909 .. ( piton.join_separation : gsub ( '##' , '#' ) )
7910 .. "\r\n"
7911 .. piton.get_last_code ( )
7912 end
7913 end
7914 end
7915 % \end{macrocode}
7916 %
7917 % Now, if the end user has used the key |write| to write the listing of the
7918 % environment on an external file (on the disk).
7919 %
7920 % We have written the values of the keys |write| and |path-write| in the Lua
7921 % variables |piton.write| and |piton.path_write|.
7922 %
7923 % If |piton.write| is not empty, that means that the key |write| has been used
7924 % for the current environment and, hence, we have to write the content of the
7925 % listing on the corresponding external file.
7926 % \begin{macrocode}
7927 if piton.write ~= '' then
7928 % \end{macrocode}
7929 % We will write on |file_name| the full name (with the path) of the file in
7930 % which we will write.
7931 % \begin{macrocode}
7932 local file_name = ''
7933 if piton.path_write == '' then
7934 file_name = piton.write
7935 else
7936 % \end{macrocode}
7937 % If |piton.path-write| is not empty, that means that we will not write on a
7938 % file in the current directory but in another directory. First, we verify that that
7939 % directory actually exists.
7940 % \begin{macrocode}
7941 local attr = lfs.attributes ( piton.path_write )
7942 if attr and attr.mode == "directory" then
7943 file_name = piton.path_write .. "/" .. piton.write
7944 else
7945 % \end{macrocode}
7946 % If the directory does \emph{not} exist, you raise an (non-fatal) error since
7947 % TeX is not able to create a new directory.
7948 % \begin{macrocode}
7949 sprintL3 [[ \@@_error_or_warning:n { InexistentDirectory } ]]
7950 end
7951 end
7952 if file_name ~= '' then
7953 % \end{macrocode}
7954 % Now, |file_name| contains the complete name of the file on which we will have
7955 % to write. Maybe the file does not exist but we are sure that the directory
7956 % exist.
7957 %
7958 % The Lua table |piton.write_files| is a table of Lua strings corresponding to all the
7959 % files that we will write on the disk in the |\AtEndDocument|. They
7960 % correspond to the use of the key |write| (and |path-write|).
7961 % \begin{macrocode}
7962 if not piton.write_files [ file_name ] then
7963 piton.write_files [ file_name ] = piton.get_last_code ( )
7964 else
7965 piton.write_files [ file_name ] =
7966 piton.write_files [ file_name ] .. "\n" .. piton.get_last_code ( )
7967 end
7968 end
7969 end
7970 end
7971 % \end{macrocode}
7972 %
7973 % \bigskip
7974 % The following command will be used when the end user has set |print=false|.
7975 % \begin{macrocode}
7976 function piton.GobbleParseNoPrint ( lang , n , code )
7977 piton.last_code = piton.Gobble ( n , code )
7978 piton.last_language = lang
7979 piton.join_and_write ( )
7980 end
7981 % \end{macrocode}
7982 %
7983 % \bigskip
7984 % The following function will be used when the key |split-on-empty-lines| is in
7985 % force. With that key, the computer listing is split in chunks at the empty
7986 % lines (usually between the abstract functions defined in the computer
7987 % code). LaTeX will be able to change the page between the chunks. The second
7988 % argument |n| corresponds to the value of the key |gobble| (number of spaces to
7989 % gobble).
7990 % \begin{macrocode}
7991 function piton.GobbleSplitParse ( lang , n , splittable , code )
7992 local chunks
7993 chunks =
7994 (
7995 Ct (
7996 (
7997 P " " ^ 0 * "\r"
7998 +
7999 C ( ( ( 1 - P "\r" ) ^ 1 * ( P "\r" + -1 )
8000 - ( P " " ^ 0 * ( P "\r" + -1 ) )
8001 ) ^ 1
8002 )
8003 ) ^ 0
8004 )
8005 ) : match ( piton.Gobble ( n , code ) )
8006 sprintL3 [[ \begingroup ]]
8007 sprintL3
8008 (
8009 [[ \PitonOptions { split-on-empty-lines = false, gobble = 0, ]]
8010 .. "language = " .. lang .. ","
8011 .. "splittable = " .. splittable .. "}"
8012 )
8013 for k , v in pairs ( chunks ) do
8014 if k > 1 then
8015 sprintL3 ( [[ \l_@@_split_separation_tl ]] )
8016 end
8017 tex.print
8018 (
8019 [[\begin{]] .. piton.env_used_by_split .. "}\r"
8020 .. v
8021 .. [[\end{]] .. piton.env_used_by_split .. "}\r"
8022 )
8023 end
8024 sprintL3 [[ \endgroup ]]
8025 end
8026 % \end{macrocode}
8027 %
8028 % \bigskip
8029 % \begin{macrocode}
8030 function piton.RetrieveGobbleSplitParse ( lang , n , splittable , code )
8031 local s
8032 s = ( ( P " " ^ 0 * "\r" ) ^ -1 * C ( P ( 1 ) ^ 0 ) * -1 ) : match ( code )
8033 piton.GobbleSplitParse ( lang , n , splittable , s )
8034 end
8035 % \end{macrocode}
8036 %
8037 % \bigskip
8038 % The following Lua string will be inserted between the chunks of code created
8039 % when the key |split-on-empty-lines| is in force. It's used only once: you have
8040 % given a name to that Lua string only for legibility. The token list
8041 % |\l_@@_split_separation_tl| corresponds to the key |split-separation|. That
8042 % token list must contain elements inserted in \emph{vertical mode} of TeX.
8043 % \begin{macrocode}
8044 piton.string_between_chunks =
8045 [[ \par \l_@@_split_separation_tl \mode_leave_vertical: ]]
8046 .. [[ \global \g_@@_line_int = 0 ]]
8047 % \end{macrocode}
8048 % The counter |\g_@@_line_int| will be used to control the points where the code
8049 % may be broken by a change of page (see the key |splittable|).
8050 %
8051 % \bigskip
8052 % The following public Lua function is provided to the developer.
8053 % \begin{macrocode}
8054 function piton.get_last_code ( )
8055 return LPEG_cleaner[piton.last_language] : match ( piton.last_code )
8056 : gsub ( '\r\n?' , '\n' )
8057 end
8058 % \end{macrocode}
8059 %
8060 %
8061 % \subsection{To count the number of lines}
8062 %
8063 % \begin{macrocode}
8064 local CountBeamerEnvironments
8065 function CountBeamerEnvironments ( code ) return
8066 (
8067 Ct (
8068 (
8069 P "\\begin{" * beamerEnvironments * ( 1 - P "\r" ) ^ 0 * C "\r"
8070 +
8071 ( 1 - P "\r" ) ^ 0 * "\r"
8072 ) ^ 0
8073 * ( 1 - P "\r" ) ^ 0
8074 * -1
8075 ) / table.getn
8076 ) : match ( code )
8077 end
8078 % \end{macrocode}
8079 %
8080 % \bigskip
8081 % The following function counts the lines of |code| except the lines which
8082 % contains only instructions for the environements of Beamer.
8083 %
8084 % It is used in |GobbleParse| and at the beginning of |\@@_composition:| (in some
8085 % rare circumstancies).
8086 %
8087 % Be careful. We have tried a version with |string.gsub| without success.
8088 % \begin{macrocode}
8089 function piton.CountLines ( code )
8090 local count
8091 count =
8092 ( Ct ( ( ( 1 - P "\r" ) ^ 0 * C "\r" ) ^ 0
8093 *
8094 (
8095 space ^ 0 * ( 1 - P "\r" - space ) * ( 1 - P "\r" ) ^ 0 * Cc "\r"
8096 + space ^ 0
8097 ) ^ -1
8098 * -1
8099 ) / table.getn
8100 ) : match ( code )
8101 if piton.beamer then
8102 count = count - 2 * CountBeamerEnvironments ( code )
8103 end
8104 sprintL3 ( [[ \int_gset:Nn \g_@@_nb_lines_int { ]] .. count .. "}" )
8105 end
8106 % \end{macrocode}
8107 %
8108 %
8109 % \bigskip
8110 % The following function is only used once (in |piton.GobbleParse|). We have
8111 % written an autonomous function only for legibility. The number of lines of the
8112 % code will be stored in |\l_@@_nb_non_empty_lines_int|. It will be used to
8113 % compute the largest number of lines to write (when |line-numbers| is in force).
8114 % \begin{macrocode}
8115 function piton.CountNonEmptyLines ( code )
8116 local count = 0
8117 % \end{macrocode}
8118 % The following code is not clear. We should try to replace it by use of the |string|
8119 % library of Lua.
8120 % \begin{macrocode}
8121 count =
8122 ( Ct ( ( P " " ^ 0 * "\r"
8123 + ( 1 - P "\r" ) ^ 0 * C "\r" ) ^ 0
8124 * ( 1 - P "\r" ) ^ 0
8125 * -1
8126 ) / table.getn
8127 ) : match ( code )
8128 count = count + 1
8129 % \end{macrocode}
8130 %
8131 % \begin{macrocode}
8132 if piton.beamer then
8133 count = count - 2 * CountBeamerEnvironments ( code )
8134 end
8135 sprintL3
8136 ( [[ \int_set:Nn \l_@@_nb_non_empty_lines_int { ]] .. count .. "}" )
8137 end
8138 % \end{macrocode}
8139 %
8140 %
8141 %
8142 % \bigskip
8143 % The following function stores in |\l_@@_first_line_int| and
8144 % |\l_@@_last_line_int| the numbers of lines of the file |file_name|
8145 % corresponding to the strings |marker_beginning| and |marker_end|.
8146 %
8147 % |s| is the marker of the beginning and |t| is the marker of the end.
8148 % \begin{macrocode}
8149 function piton.ComputeRange ( s , t , file_name )
8150 local first_line = -1
8151 local count = 0
8152 local last_found = false
8153 for line in io.lines ( file_name ) do
8154 if first_line == -1 then
8155 if line : sub ( 1 , #s ) == s then
8156 first_line = count
8157 end
8158 else
8159 if line : sub ( 1 , #t ) == t then
8160 last_found = true
8161 break
8162 end
8163 end
8164 count = count + 1
8165 end
8166 if first_line == -1 then
8167 sprintL3 [[ \@@_error_or_warning:n { begin~marker~not~found } ]]
8168 else
8169 if not last_found then
8170 sprintL3 [[ \@@_error_or_warning:n { end~marker~not~found } ]]
8171 end
8172 end
8173 sprintL3 (
8174 [[ \int_set:Nn \l_@@_first_line_int { ]] .. first_line .. ' + 2 }'
8175 .. [[ \global \l_@@_last_line_int = ]] .. count )
8176 end
8177 % \end{macrocode}
8178 %
8179 % \bigskip
8180 % \subsection{To determine the empty lines of the listings}
8181 %
8182 % Despite its name, the Lua function |ComputeLinesStatus| computes
8183 % |piton.lines_status| but also |piton.empty_lines|.
8184 %
8185 % \medskip
8186 % In |piton.empty_lines|, a line will have the number 0 if it's a empty line (in
8187 % fact a blank line, with only spaces) and 1 elsewhere.
8188 %
8189 % \medskip
8190 % In |piton.lines_status|, each line will have a status with regard the
8191 % breaking points allowed (for the changes of pages).
8192 % \begin{itemize}
8193 % \item 0 if the line is empty and a page break is allowed;
8194 % \item 1 if the line is not empty but a page break is allowed after that line;
8195 % \item 2 if a page break is \emph{not} allowed after that line (empty or not empty).
8196 % \end{itemize}
8197 %
8198 % \medskip
8199 % |splittable| is the value of |\l_@@_splittable_int|.
8200 % However, if |splittable-on-empty-lines| is in force, |splittable| is the
8201 % opposite of |\l_@@_splittable_int|.
8202 % \begin{macrocode}
8203 function piton.ComputeLinesStatus ( code , splittable )
8204 % \end{macrocode}
8205 % The lines in the listings which correspond to the beginning or the end of an
8206 % environment of Beamer (eg. |\begin{uncoverenv}|) must be retrieved (those
8207 % lines have \emph{no} number and therefore, \emph{no} status).
8208 % \begin{macrocode}
8209 local lpeg_line_beamer
8210 if piton.beamer then
8211 lpeg_line_beamer =
8212 space ^ 0
8213 * P [[\begin{]] * beamerEnvironments * "}"
8214 * ( "<" * ( 1 - P ">" ) ^ 0 * ">" ) ^ -1
8215 +
8216 space ^ 0
8217 * P [[\end{]] * beamerEnvironments * "}"
8218 else
8219 lpeg_line_beamer = P ( false )
8220 end
8221 % \end{macrocode}
8222 % \begin{macrocode}
8223 local lpeg_empty_lines =
8224 Ct (
8225 ( lpeg_line_beamer * "\r"
8226 +
8227 P " " ^ 0 * "\r" * Cc ( 0 )
8228 +
8229 ( 1 - P "\r" ) ^ 0 * "\r" * Cc ( 1 )
8230 ) ^ 0
8231 *
8232 ( lpeg_line_beamer + ( 1 - P "\r" ) ^ 1 * Cc ( 1 ) ) ^ -1
8233 )
8234 * -1
8235 % \end{macrocode}
8236 % \begin{macrocode}
8237 local lpeg_all_lines =
8238 Ct (
8239 ( lpeg_line_beamer * "\r"
8240 +
8241 ( 1 - P "\r" ) ^ 0 * "\r" * Cc ( 1 )
8242 ) ^ 0
8243 *
8244 ( lpeg_line_beamer + ( 1 - P "\r" ) ^ 1 * Cc ( 1 ) ) ^ -1
8245 )
8246 * -1
8247 % \end{macrocode}
8248 % We begin with the computation of |piton.empty_lines|. It will be used in
8249 % conjonction with |line-numbers|.
8250 % \begin{macrocode}
8251 piton.empty_lines = lpeg_empty_lines : match ( code )
8252 % \end{macrocode}
8253 %
8254 % Now, we compute |piton.lines_status|. It will be used in conjonction with
8255 % |splittable| and |splittable-on-empty-lines|.
8256 %
8257 % Now, we will take into account the current value of |\l_@@_splittable_int|
8258 % (provided by the \emph{absolute value} of the argument |splittable|).
8259 % \begin{macrocode}
8260 local lines_status
8261 local s = splittable
8262 if splittable < 0 then s = - splittable end
8263 % \end{macrocode}
8264 %
8265 % \begin{macrocode}
8266 if splittable > 0 then
8267 lines_status = lpeg_all_lines : match ( code )
8268 else
8269 % \end{macrocode}
8270 % Here, we should try to copy |piton.empty_lines| but it's not easy.
8271 % \begin{macrocode}
8272 lines_status = lpeg_empty_lines : match ( code )
8273 for i , x in ipairs ( lines_status ) do
8274 if x == 0 then
8275 for j = 1 , s - 1 do
8276 if i + j > #lines_status then break end
8277 if lines_status[i+j] == 0 then break end
8278 lines_status[i+j] = 2
8279 end
8280 for j = 1 , s - 1 do
8281 if i - j == 1 then break end
8282 if lines_status[i-j-1] == 0 then break end
8283 lines_status[i-j-1] = 2
8284 end
8285 end
8286 end
8287 end
8288 % \end{macrocode}
8289 %
8290 % In all cases (whatever is the value of |splittable-on-empty-lines|) we have to
8291 % deal with both extremities of the listing to format.
8292 %
8293 % First from the beginning of the code.
8294 % \begin{macrocode}
8295 for j = 1 , s - 1 do
8296 if j > #lines_status then break end
8297 if lines_status[j] == 0 then break end
8298 lines_status[j] = 2
8299 end
8300 % \end{macrocode}
8301 % Now, from the end of the code.
8302 % \begin{macrocode}
8303 for j = 1 , s - 1 do
8304 if #lines_status - j == 0 then break end
8305 if lines_status[#lines_status - j] == 0 then break end
8306 lines_status[#lines_status - j] = 2
8307 end
8308 % \end{macrocode}
8309 %
8310 % \bigskip
8311 % \begin{macrocode}
8312 piton.lines_status = lines_status
8313 end
8314 % \end{macrocode}
8315 %
8316 % \begin{macrocode}
8317 function piton.TranslateBeamerEnv ( code )
8318 local s
8319 s =
8320 (
8321 Ct (
8322 (
8323 space ^ 0
8324 * C (
8325 ( P "\\begin{" + "\\end{" )
8326 * beamerEnvironments * "}" * ( 1 - P "\r" ) ^ 0 * "\r"
8327 )
8328 + C ( ( 1 - P "\r" ) ^ 0 * "\r" )
8329 ) ^ 0
8330 *
8331 (
8332 (
8333 space ^ 0
8334 * C (
8335 ( P "\\begin{" + "\\end{" )
8336 * beamerEnvironments * "}" * ( 1 - P "\r" ) ^ 0 * -1
8337 )
8338 + C ( ( 1 - P "\r" ) ^ 1 ) * -1
8339 ) ^ -1
8340 )
8341 ) ^ -1 / table.concat
8342 ) : match ( code )
8343 sprintL3 ( [[ \tl_set:Nn \l_@@_listing_tl { ]] )
8344 tex.sprint ( luatexbase.catcodetables.other , s )
8345 sprintL3 ( "}" )
8346 end
8347 % \end{macrocode}
8348 %
8349 % \bigskip
8350 % \subsection{To create new languages with the syntax of listings}
8351 %
8352 % \begin{macrocode}
8353 function piton.new_language ( lang , definition )
8354 lang = lang : lower ( )
8355 % \end{macrocode}
8356 %
8357 % \bigskip
8358 % \begin{macrocode}
8359 local alpha , digit = lpeg.alpha , lpeg.digit
8360 local extra_letters = { "@" , "_" , "$" } --
8361 % \end{macrocode}
8362 %
8363 %
8364 % \bigskip
8365 % The command |add_to_letter| (triggered by the key ||) don't write right away
8366 % in the \textsc{lpeg} pattern of the letters in an intermediate |extra_letters|
8367 % because we may have to retrieve letters from that ``list'' if there appear in
8368 % a key |alsoother|.
8369 % \begin{macrocode}
8370 function add_to_letter ( c )
8371 if c ~= " " then table.insert ( extra_letters , c ) end
8372 end
8373 % \end{macrocode}
8374 %
8375 % For the digits, it's straitforward.
8376 % \begin{macrocode}
8377 function add_to_digit ( c )
8378 if c ~= " " then digit = digit + c end
8379 end
8380 % \end{macrocode}
8381 %
8382 % \bigskip
8383 % The main use of the key |alsoother| is, for the language LaTeX, when you have
8384 % to retrieve some characters from the list of letters, in particular |@| and
8385 % |_| (which, by default, are not allowed in the name of a control sequence in
8386 % TeX).
8387 %
8388 % \medskip
8389 % (In the following \textsc{lpeg} we have a problem when we try to add |{| and
8390 % |}|).
8391 % \begin{macrocode}
8392 local other = S ":_@+-*/<>!?;.()[]~^=#&\"\'\\$" --
8393 local extra_others = { }
8394 % \end{macrocode}
8395 %
8396 % \begin{macrocode}
8397 function add_to_other ( c )
8398 if c ~= " " then
8399 % \end{macrocode}
8400 % We will use |extra_others| to retrieve further these characters from the list
8401 % of the letters.
8402 % \begin{macrocode}
8403 extra_others[c] = true
8404 % \end{macrocode}
8405 % The \textsc{lpeg} pattern |other| will be used in conjunction with the key
8406 % |tag| (mainly for languages such as \textsc{html} and \textsc{xml}) for the
8407 % character |/| in the closing tags |</....>|).
8408 % \begin{macrocode}
8409 other = other + P ( c )
8410 end
8411 end
8412 % \end{macrocode}
8413 %
8414 %
8415 % \bigskip
8416 % Now, the first transformation of the definition of the language, as provided
8417 % by the end user in the argument |definition| of |piton.new_language|.
8418 % \begin{macrocode}
8419 local def_table
8420 if ( S ", " ^ 0 * -1 ) : match ( definition ) then
8421 def_table = {}
8422 else
8423 local strict_braces =
8424 P { "E" ,
8425 E = ( "{" * V "F" * "}" + ( 1 - S ",{}" ) ) ^ 0 ,
8426 F = ( "{" * V "F" * "}" + ( 1 - S "{}" ) ) ^ 0
8427 }
8428 local cut_definition =
8429 P { "E" ,
8430 E = Ct ( V "F" * ( "," * V "F" ) ^ 0 ) ,
8431 F = Ct ( space ^ 0 * C ( alpha ^ 1 ) * space ^ 0
8432 * ( "=" * space ^ 0 * C ( strict_braces ) ) ^ -1 )
8433 }
8434 def_table = cut_definition : match ( definition )
8435 end
8436 % \end{macrocode}
8437 % The definition of the language, provided by the end user of \pkg{piton} is
8438 % now in the Lua table |def_table|. We will use it \emph{several times}.
8439 %
8440 % \medskip
8441 % The following \textsc{lpeg} will be used to extract arguments in the values of
8442 % the keys (|morekeywords|, |morecomment|, |morestring|, etc.).
8443 % \begin{macrocode}
8444 local tex_braced_arg = "{" * C ( ( 1 - P "}" ) ^ 0 ) * "}"
8445 local tex_arg = tex_braced_arg + C ( 1 )
8446 local tex_option_arg = "[" * C ( ( 1 - P "]" ) ^ 0 ) * "]" + Cc ( nil )
8447 % \end{macrocode}
8448 %
8449 % \begin{macrocode}
8450 local args_for_tag
8451 = tex_option_arg
8452 * space ^ 0
8453 * tex_arg
8454 * space ^ 0
8455 * tex_arg
8456 % \end{macrocode}
8457 %
8458 % \begin{macrocode}
8459 local args_for_morekeywords
8460 = "[" * C ( ( 1 - P "]" ) ^ 0 ) * "]"
8461 * space ^ 0
8462 * tex_option_arg
8463 * space ^ 0
8464 * tex_arg
8465 * space ^ 0
8466 * ( tex_braced_arg + Cc ( nil ) )
8467 % \end{macrocode}
8468 %
8469 % \begin{macrocode}
8470 local args_for_moredelims
8471 = ( C ( P "*" ^ -2 ) + Cc ( nil ) ) * space ^ 0
8472 * args_for_morekeywords
8473 % \end{macrocode}
8474 %
8475 % \begin{macrocode}
8476 local args_for_morecomment
8477 = "[" * C ( ( 1 - P "]" ) ^ 0 ) * "]"
8478 * space ^ 0
8479 * tex_option_arg
8480 * space ^ 0
8481 * C ( P ( 1 ) ^ 0 * -1 )
8482 % \end{macrocode}
8483 %
8484 %
8485 % \bigskip
8486 % We scan the definition of the language (i.e. the table |def_table|) in order
8487 % to detect the potential key |sensitive|. Indeed, we have to catch that key
8488 % before the treatment of the keywords of the language. We will also look for
8489 % the potential keys |alsodigit|, |alsoletter| and |tag|.
8490 % \begin{macrocode}
8491 local sensitive = true
8492 local style_tag , left_tag , right_tag
8493 for _ , x in ipairs ( def_table ) do
8494 if x[1] == "sensitive" then
8495 if x[2] == nil or ( P "true" ) : match ( x[2] ) then
8496 sensitive = true
8497 else
8498 if ( P "false" + P "f" ) : match ( x[2] ) then sensitive = false end
8499 end
8500 end
8501 if x[1] == "alsodigit" then x[2] : gsub ( "." , add_to_digit ) end
8502 if x[1] == "alsoletter" then x[2] : gsub ( "." , add_to_letter ) end
8503 if x[1] == "alsoother" then x[2] : gsub ( "." , add_to_other ) end
8504 if x[1] == "tag" then
8505 style_tag , left_tag , right_tag = args_for_tag : match ( x[2] )
8506 style_tag = style_tag or [[\PitonStyle{Tag}]]
8507 end
8508 end
8509 % \end{macrocode}
8510 % Now, the \textsc{lpeg} for the numbers. Of course, it uses |digit| previously
8511 % computed.
8512 % \begin{macrocode}
8513 local Number =
8514 K ( 'Number.Internal' ,
8515 ( digit ^ 1 * "." * # ( 1 - P "." ) * digit ^ 0
8516 + digit ^ 0 * "." * digit ^ 1
8517 + digit ^ 1 )
8518 * ( S "eE" * S "+-" ^ -1 * digit ^ 1 ) ^ -1
8519 + digit ^ 1
8520 )
8521 % \end{macrocode}
8522 %
8523 % \begin{macrocode}
8524 local string_extra_letters = ""
8525 for _ , x in ipairs ( extra_letters ) do
8526 if not ( extra_others[x] ) then
8527 string_extra_letters = string_extra_letters .. x
8528 end
8529 end
8530 local letter = alpha + S ( string_extra_letters )
8531 + P "â" + "à" + "ç" + "é" + "è" + "ê" + "ë" + "ï" + "î"
8532 + "ô" + "û" + "ü" + "Â" + "À" + "Ç" + "É" + "È" + "Ê" + "Ë"
8533 + "Ï" + "Î" + "Ô" + "Û" + "Ü"
8534 % \end{macrocode}
8535 %
8536 % \begin{macrocode}
8537 local alphanum = letter + digit
8538 local identifier = letter * alphanum ^ 0
8539 local Identifier = K ( 'Identifier.Internal' , identifier )
8540 % \end{macrocode}
8541 %
8542 %
8543 % \bigskip
8544 % Now, we scan the definition of the language (i.e. the table |def_table|) for
8545 % the keywords.
8546 %
8547 %
8548 % The following LPEG does \emph{not} catch the optional argument between square
8549 % brackets in first position.
8550 % \begin{macrocode}
8551 local split_clist =
8552 P { "E" ,
8553 E = ( "[" * ( 1 - P "]" ) ^ 0 * "]" ) ^ -1
8554 * ( P "{" ) ^ 1
8555 * Ct ( V "F" * ( "," * V "F" ) ^ 0 )
8556 * ( P "}" ) ^ 1 * space ^ 0 ,
8557 F = space ^ 0 * C ( letter * alphanum ^ 0 + other ^ 1 ) * space ^ 0
8558 }
8559 % \end{macrocode}
8560 % The following function will be used if the keywords are not case-sensitive.
8561 % \begin{macrocode}
8562 local keyword_to_lpeg
8563 function keyword_to_lpeg ( name ) return
8564 Q ( Cmt (
8565 C ( identifier ) ,
8566 function ( _ , _ , a ) return a : upper ( ) == name : upper ( )
8567 end
8568 )
8569 )
8570 end
8571 local Keyword = P ( false )
8572 local PrefixedKeyword = P ( false )
8573 % \end{macrocode}
8574 % Now, we actually treat all the keywords and also the key |moredirectives|.
8575 % \begin{macrocode}
8576 for _ , x in ipairs ( def_table )
8577 do if x[1] == "morekeywords"
8578 or x[1] == "otherkeywords"
8579 or x[1] == "moredirectives"
8580 or x[1] == "moretexcs"
8581 then
8582 local keywords = P ( false )
8583 % \end{macrocode}
8584 % \begin{macrocode}
8585 local style = [[\PitonStyle{Keyword}]]
8586 if x[1] == "moredirectives" then style = [[\PitonStyle{Directive}]] end
8587 style = tex_option_arg : match ( x[2] ) or style
8588 local n = tonumber ( style )
8589 if n then
8590 if n > 1 then style = [[\PitonStyle{Keyword]] .. style .. "}" end
8591 end
8592 % \end{macrocode}
8593 % \begin{macrocode}
8594 for _ , word in ipairs ( split_clist : match ( x[2] ) ) do
8595 if x[1] == "moretexcs" then
8596 keywords = Q ( [[\]] .. word ) + keywords
8597 else
8598 if sensitive
8599 % \end{macrocode}
8600 % The documentation of \pkg{lstlistings} specifies that, for the key
8601 % |morekeywords|, if a keyword is a prefix of another keyword, then the prefix
8602 % must appear first. However, for the \text{lpeg}, it's rather the contrary.
8603 % That's why, here, we add the new element \emph{on the left}.
8604 % \begin{macrocode}
8605 then keywords = Q ( word ) + keywords
8606 else keywords = keyword_to_lpeg ( word ) + keywords
8607 end
8608 end
8609 end
8610 Keyword = Keyword +
8611 Lc ( "{" .. style .. "{" ) * keywords * Lc "}}"
8612 end
8613 % \end{macrocode}
8614 % Of course, the feature with the key |keywordsprefix| is designed for the
8615 % languages TeX, LaTeX, et \emph{al}. In that case, there is two kinds of
8616 % keywords (= control sequences).
8617 % \begin{itemize}
8618 % \item those beginning with |\| and a sequence of characters of catcode
8619 % ``|letter|'';
8620 % \item those beginning by |\| followed by one character of catcode ``|other|''.
8621 % \end{itemize}
8622 % The following code addresses both cases. Of course, the \textsc{lpeg} pattern
8623 % |letter| must catch only characters of catcode ``|letter|''. That's why we
8624 % have a key |alsoletter| to add new characters in that category (e.g. |:| when
8625 % we want to format L3 code). However, the \textsc{lpeg} pattern is allowed to
8626 % catch \emph{more} than only the characters of catcode ``other'' in TeX.
8627 % \begin{macrocode}
8628 if x[1] == "keywordsprefix" then
8629 local prefix = ( ( C ( 1 - P " " ) ^ 1 ) * P " " ^ 0 ) : match ( x[2] )
8630 PrefixedKeyword = PrefixedKeyword
8631 + K ( 'Keyword' , P ( prefix ) * ( letter ^ 1 + other ) )
8632 end
8633 end
8634 % \end{macrocode}
8635 %
8636 %
8637 % \bigskip
8638 % Now, we scan the definition of the language (i.e. the table |def_table|) for
8639 % the strings.
8640 % \begin{macrocode}
8641 local long_string = P ( false )
8642 local Long_string = P ( false )
8643 local LongString = P (false )
8644 local central_pattern = P ( false )
8645 for _ , x in ipairs ( def_table ) do
8646 if x[1] == "morestring" then
8647 arg1 , arg2 , arg3 , arg4 = args_for_morekeywords : match ( x[2] )
8648 arg2 = arg2 or [[\PitonStyle{String.Long}]]
8649 if arg1 ~= "s" then
8650 arg4 = arg3
8651 end
8652 central_pattern = 1 - S ( " \r" .. arg4 )
8653 if arg1 : match "b" then
8654 central_pattern = P ( [[\]] .. arg3 ) + central_pattern
8655 end
8656 % \end{macrocode}
8657 % In fact, the specifier |d| is point-less: when it is not in force, it's still
8658 % possible to double the delimiter with a correct behaviour of \pkg{piton}
8659 % since, in that case, \pkg{piton} will compose \emph{two} contiguous strings...
8660 % \begin{macrocode}
8661 if arg1 : match "d" or arg1 == "m" then
8662 central_pattern = P ( arg3 .. arg3 ) + central_pattern
8663 end
8664 if arg1 == "m"
8665 then prefix = B ( 1 - letter - ")" - "]" )
8666 else prefix = P ( true )
8667 end
8668 % \end{macrocode}
8669 % First, a pattern \emph{without captures} (needed to compute |braces|).
8670 % \begin{macrocode}
8671 long_string = long_string +
8672 prefix
8673 * arg3
8674 * ( space + central_pattern ) ^ 0
8675 * arg4
8676 % \end{macrocode}
8677 % Now a pattern \emph{with captures}.
8678 % \begin{macrocode}
8679 local pattern =
8680 prefix
8681 * Q ( arg3 )
8682 * ( SpaceInString + Q ( central_pattern ^ 1 ) + EOL ) ^ 0
8683 * Q ( arg4 )
8684 % \end{macrocode}
8685 % We will need |Long_string| in the nested comments.
8686 % \begin{macrocode}
8687 Long_string = Long_string + pattern
8688 LongString = LongString +
8689 Ct ( Cc "Open" * Cc ( "{" .. arg2 .. "{" ) * Cc "}}" )
8690 * pattern
8691 * Ct ( Cc "Close" )
8692 end
8693 end
8694 % \end{macrocode}
8695 % The argument of |Compute_braces| must be a pattern \emph{which does no
8696 % catching} corresponding to the strings of the language.
8697 % \begin{macrocode}
8698 local braces = Compute_braces ( long_string )
8699 if piton.beamer then Beamer = Compute_Beamer ( lang , braces ) end
8700
8701 DetectedCommands =
8702 Compute_DetectedCommands ( lang , braces )
8703 + Compute_RawDetectedCommands ( lang , braces )
8704
8705 LPEG_cleaner[lang] = Compute_LPEG_cleaner ( lang , braces )
8706 % \end{macrocode}
8707 %
8708 % \bigskip
8709 % Now, we deal with the comments and the delims.
8710 %
8711 % \medskip
8712 % |sCmment| is for the comments (of |morecomment|) of type |s| (\emph{standard}) or
8713 % |n| (\emph{nested}).
8714 % \begin{macrocode}
8715 local sComment = P ( false )
8716 % \end{macrocode}
8717 %
8718 % \medskip
8719 % |lComment| is for the comments (of |morecomment|) of type |l| (\emph{line}).
8720 % \begin{macrocode}
8721 local lComment = P ( false )
8722 % \end{macrocode}
8723 %
8724 % \medskip
8725 % |fCmment| is for the comments (of |morecomment|) of type |f| (\emph{first}).
8726 % \begin{macrocode}
8727 local fComment = P ( false )
8728 % \end{macrocode}
8729 %
8730 % \begin{macrocode}
8731 local fComment = P ( false )
8732
8733 for _ , x in ipairs ( def_table ) do
8734 if x[1] == "morecomment" then
8735 local arg1 , arg2 , other_args = args_for_morecomment : match ( x[2] )
8736 arg2 = arg2 or [[\PitonStyle{Comment}]]
8737 % \end{macrocode}
8738 % If the letter |i| is present in the first argument (eg:
8739 % |morecomment = [si]{(*}{*)}|, then the corresponding comments are discarded.
8740 % \begin{macrocode}
8741 if arg1 : match "i" then arg2 = [[\PitonStyle{Discard}]] end
8742 if arg1 : match "l" or arg1 : match "f" then
8743 local arg3 = ( tex_braced_arg + C ( P ( 1 ) ^ 0 * -1 ) )
8744 : match ( other_args )
8745 if arg3 == [[\#]] then arg3 = "#" end -- mandatory
8746 if arg3 == [[\%]] then arg3 = "%" end -- mandatory
8747 local MyLPEG =
8748 Ct ( Cc "Open"
8749 * Cc ( "{" .. arg2 .. [[{\PitonSpaceSubstitute{]] )
8750 * Cc "}}}"
8751 )
8752 * Q ( arg3 )
8753 * ( CommentMath + Q ( ( 1 - S "$\r" ) ^ 1 ) ) ^ 0 -- $ noqa
8754 * Ct ( Cc "Close" )
8755 * ( EOL + -1 )
8756 if arg1 : match "l" then
8757 lComment = lComment + MyLPEG
8758 else
8759 fComment = fComment + MyLPEG
8760 end
8761 else
8762 local arg3 , arg4 =
8763 ( tex_arg * space ^ 0 * tex_arg ) : match ( other_args )
8764 if arg1 : match "s" then
8765 sComment = sComment +
8766 Ct ( Cc "Open" * Cc ( "{" .. arg2 .. "{" ) * Cc "}}" )
8767 * Q ( arg3 )
8768 * (
8769 CommentMath
8770 + Q ( ( 1 - P ( arg4 ) - S "$\r" ) ^ 1 ) -- $
8771 + EOL
8772 ) ^ 0
8773 * Q ( arg4 )
8774 * Ct ( Cc "Close" )
8775 end
8776 if arg1 : match "n" then
8777 sComment = sComment +
8778 Ct ( Cc "Open" * Cc ( "{" .. arg2 .. "{" ) * Cc "}}" )
8779 * P { "A" ,
8780 A = Q ( arg3 )
8781 * ( V "A"
8782 + Q ( ( 1 - P ( arg3 ) - P ( arg4 )
8783 - S "\r$\"" ) ^ 1 ) -- $
8784 + long_string
8785 + "$" -- $
8786 * K ( 'Comment.Math' , ( 1 - S "$\r" ) ^ 1 ) --$
8787 * "$" -- $
8788 + EOL
8789 ) ^ 0
8790 * Q ( arg4 )
8791 }
8792 * Ct ( Cc "Close" )
8793 end
8794 end
8795 end
8796 % \end{macrocode}
8797 % For the keys |moredelim|, we have to add another argument in first position,
8798 % equal to |*| or |**|.
8799 % \begin{macrocode}
8800 if x[1] == "moredelim" then
8801 local arg1 , arg2 , arg3 , arg4 , arg5
8802 = args_for_moredelims : match ( x[2] )
8803 local MyFun = Q
8804 if arg1 == "*" or arg1 == "**" then
8805 function MyFun ( x )
8806 if x ~= '' then return
8807 LPEG1[lang] : match ( x )
8808 end
8809 end
8810 end
8811 local left_delim
8812 if arg2 : match "i" then
8813 left_delim = P ( arg4 )
8814 else
8815 left_delim = Q ( arg4 )
8816 end
8817 if arg2 : match "l" then
8818 sComment = sComment +
8819 Ct ( Cc "Open" * Cc ( "{" .. arg3 .. "{" ) * Cc "}}" )
8820 * left_delim
8821 * ( MyFun ( ( 1 - P "\r" ) ^ 1 ) ) ^ 0
8822 * Ct ( Cc "Close" )
8823 * ( EOL + -1 )
8824 end
8825 if arg2 : match "s" then
8826 local right_delim
8827 if arg2 : match "i" then
8828 right_delim = P ( arg5 )
8829 else
8830 right_delim = Q ( arg5 )
8831 end
8832 sComment = sComment +
8833 Ct ( Cc "Open" * Cc ( "{" .. arg3 .. "{" ) * Cc "}}" )
8834 * left_delim
8835 * ( MyFun ( ( 1 - P ( arg5 ) - "\r" ) ^ 1 ) + EOL ) ^ 0
8836 * right_delim
8837 * Ct ( Cc "Close" )
8838 end
8839 end
8840 end
8841
8842 local Delim = Q ( S "{[()]}" )
8843 local Punct = Q ( S "=,:;!\\'\"" )
8844 % \end{macrocode}
8845 %
8846 % \begin{macrocode}
8847 local EOL =
8848 P "\r"
8849 *
8850 (
8851 space ^ 0 * -1
8852 +
8853 Cc "EOL"
8854 )
8855 * ( ( fComment + lComment ) ^ 0 * LeadingSpace ^ 0 * # ( 1 - S " \r" ) ) ^ -1
8856 % \end{macrocode}
8857 %
8858 % \begin{macrocode}
8859 local Main =
8860 space ^ 0 * EOL * ( fComment + lComment ) ^ 0
8861 + Space
8862 + Tab
8863 + Escape + EscapeMath
8864 + CommentLaTeX
8865 + Beamer
8866 + DetectedCommands
8867 + sComment
8868 + lComment
8869 % \end{macrocode}
8870 % We must put |LongString| before |Delim| because, in PostScript, the strings
8871 % are delimited by parenthesis and those parenthesis would be caught by |Delim|.
8872 % \begin{macrocode}
8873 + LongString
8874 + Delim
8875 + PrefixedKeyword
8876 + Keyword * ( -1 + # ( 1 - alphanum ) )
8877 + Punct
8878 + K ( 'Identifier.Internal' , letter * alphanum ^ 0 )
8879 + Number
8880 + Word
8881 % \end{macrocode}
8882 %
8883 % The \textsc{lpeg} |LPEG1[lang]| is used to reformat small elements, for
8884 % example the arguments of the ``detected commands''.
8885 %
8886 % Of course, here, we must not put |local|, of course.
8887 % \begin{macrocode}
8888 LPEG1[lang] = Main ^ 0
8889 % \end{macrocode}
8890 %
8891 %
8892 % The \textsc{lpeg} |LPEG2[lang]| is used to format general chunks of code.
8893 % \begin{macrocode}
8894 LPEG2[lang] =
8895 Ct (
8896 ( space ^ 0 * P "\r" ) ^ -1
8897 * Lc [[ \@@_begin_line: ]]
8898 * ( fComment + lComment ) ^ 0 -- 2026-05-12
8899 * LeadingSpace ^ 0
8900 * ( space ^ 1 * -1 + Main ) ^ 0
8901 * -1
8902 * Lc [[ \@@_end_line: ]]
8903 )
8904 % \end{macrocode}
8905 %
8906 % If the key |tag| has been used. Of course, this feature is designed for the
8907 % languages such as \textsc{html} and \textsc{xml}.
8908 % \begin{macrocode}
8909 if left_tag then
8910 local Tag = Ct ( Cc "Open" * Cc ( "{" .. style_tag .. "{" ) * Cc "}}" )
8911 * Q ( left_tag * other ^ 0 ) -- $
8912 * ( ( ( 1 - P ( right_tag ) ) ^ 0 )
8913 / ( function ( x ) return LPEG0[lang] : match ( x ) end ) )
8914 * Q ( right_tag )
8915 * Ct ( Cc "Close" )
8916 MainWithoutTag
8917 = space ^ 1 * -1
8918 + space ^ 0 * EOL
8919 + Space
8920 + Tab
8921 + Escape + EscapeMath
8922 + CommentLaTeX
8923 + Beamer
8924 + DetectedCommands
8925 + sComment
8926 + Delim
8927 + LongString
8928 + PrefixedKeyword
8929 + Keyword * ( -1 + # ( 1 - alphanum ) )
8930 + Punct
8931 + K ( 'Identifier.Internal' , letter * alphanum ^ 0 )
8932 + Number
8933 + Word
8934 LPEG0[lang] = MainWithoutTag ^ 0
8935 local LPEGaux = Tab + Escape + EscapeMath + CommentLaTeX
8936 + Beamer + DetectedCommands + sComment + Tag
8937 MainWithTag
8938 = space ^ 1 * -1
8939 + space ^ 0 * EOL
8940 + Space
8941 + LPEGaux
8942 + Q ( ( 1 - EOL - LPEGaux ) ^ 1 )
8943 LPEG1[lang] = MainWithTag ^ 0
8944 LPEG2[lang] =
8945 Ct (
8946 ( space ^ 0 * P "\r" ) ^ -1
8947 * Lc [[ \@@_begin_line: ]]
8948 * Beamer
8949 * LeadingSpace ^ 0
8950 * LPEG1[lang]
8951 * -1
8952 * Lc [[ \@@_end_line: ]]
8953 )
8954 end
8955 end
8956 % \end{macrocode}
8957 %
8958 % \subsection{We write the files (key 'write') and join the files in the PDF (key 'join')}
8959 %
8960 % \begin{macrocode}
8961 function piton.write_files_now ( )
8962 for file_name , file_content in pairs ( piton.write_files ) do
8963 local file = io.open ( file_name , "w" )
8964 if file then
8965 file : write ( file_content )
8966 file : close ( )
8967 else
8968 sprintL3
8969 ( [[ \@@_error_or_warning:nn { FileError } { ]] .. file_name .. "}" )
8970 end
8971 end
8972 end
8973 % \end{macrocode}
8974 %
8975 % \bigskip
8976 % \subsection{Conversion from utf8 to utf16}
8977 %
8978 % Caution: the following function should be considered as public.
8979 % \begin{macrocode}
8980 function piton.utf16 ( str )
8981 local hex = { "FEFF" } -- BOM UTF-16BE
8982 for _, codepoint in utf8.codes(str) do
8983 table.insert(hex, string.format("%04X", codepoint))
8984 end
8985 return table.concat(hex)
8986 end
8987 %</LUA>
8988 % \end{macrocode}
8989 %
8990 %
8991 % \end{document}
8992 %
8993 % Local Variables:
8994 % fill-column: 80
8995 % End: