Debian Codegraph @

texmf-dist/tex/lualatex/piton/piton.lua

raw
1 --
2 -- This is file `piton.lua',
3 -- generated with the docstrip utility.
4 --
5 -- The original source files were:
6 --
7 -- piton-code.dtx (with options: `LUA')
8 -- ---------------------------------------------
9 -- Copyright (C) 2022-2026 by F. Pantigny
10 --
11 -- This file may be distributed and/or modified under the
12 -- conditions of the LaTeX Project Public License, either
13 -- version 1.3 of this license or (at your option) any later
14 -- version. The latest version of this license is in:
15 --
16 -- http://www.latex-project.org/lppl.txt
17 --
18 -- and version 1.3 or later is part of all distributions of
19 -- LaTeX version 2005/12/01 or later.
20 -- -------------------------------------------
21 --
22 -- This file is part of the LuaLaTeX package 'piton'.
23 piton_version = "4.13" -- 2026/05/25
24 piton.comment_latex = piton.comment_latex or ">"
25 piton.comment_latex = "#" .. piton.comment_latex
26 piton.write_files = { }
27 piton.join_files = { }
28 local sprintL3
29 function sprintL3 ( s )
30 tex.sprint ( luatexbase.catcodetables.expl , s )
31 end
32 local P, S, V, C, Ct, Cc = lpeg.P, lpeg.S, lpeg.V, lpeg.C, lpeg.Ct, lpeg.Cc
33 local Cg , Cmt , Cb = lpeg.Cg , lpeg.Cmt , lpeg.Cb
34 local B , R = lpeg.B , lpeg.R
35 lpeg.locale(lpeg)
36 local Q
37 function Q ( pattern )
38 return Ct ( Cc ( luatexbase.catcodetables.other ) * C ( pattern ) )
39 end
40 local L
41 function L ( pattern ) return
42 Ct ( C ( pattern ) )
43 end
44 local Lc
45 function Lc ( string ) return
46 Cc ( { luatexbase.catcodetables.expl , string } )
47 end
48 local K
49 function K ( style , pattern ) return
50 Lc ( [[ {\PitonStyle{ ]] .. style .. "}{" )
51 * Q ( pattern )
52 * Lc "}}"
53 end
54 local WithStyle
55 function WithStyle ( style , pattern ) return
56 Ct ( Cc "Open" * Cc ( [[{\PitonStyle{]] .. style .. "}{" ) * Cc "}}" )
57 * pattern
58 * Ct ( Cc "Close" )
59 end
60 Escape = P ( false )
61 EscapeClean = P ( false )
62 if piton.begin_escape then
63 Escape =
64 P ( piton.begin_escape )
65 * L ( ( 1 - P ( piton.end_escape ) ) ^ 1 )
66 * P ( piton.end_escape )
67 EscapeClean =
68 P ( piton.begin_escape )
69 * ( 1 - P ( piton.end_escape ) ) ^ 1
70 * P ( piton.end_escape )
71 end
72 EscapeMath = P ( false )
73 if piton.begin_escape_math then
74 EscapeMath =
75 P ( piton.begin_escape_math )
76 * Lc "$"
77 * L ( ( 1 - P(piton.end_escape_math) ) ^ 1 )
78 * Lc "$"
79 * P ( piton.end_escape_math )
80 end
81 local alpha , digit = lpeg.alpha , lpeg.digit
82 local space = P " "
83 local letter = alpha + "_" + "â" + "à" + "ç" + "é" + "è" + "ê" + "ë" + "ï" + "î"
84 + "ô" + "û" + "ü" + "Â" + "À" + "Ç" + "É" + "È" + "Ê" + "Ë"
85 + "Ï" + "Î" + "Ô" + "Û" + "Ü"
86
87 local alphanum = letter + digit
88 local identifier = letter * alphanum ^ 0
89 local Identifier = K ( 'Identifier.Internal' , identifier )
90 local allow_underscores_except_first
91 function allow_underscores_except_first ( p )
92 return p * (P "_" + p)^0
93 end
94 local allow_underscores
95 function allow_underscores ( p )
96 return (P "_" + p)^0
97 end
98 local digits_to_number
99 function digits_to_number(prefix, digits)
100 -- The edge cases of what is allowed in number litterals is modelled after
101 -- OCaml numbers, which seems to be the most permissive language
102 -- in this regard (among C, OCaml, Python & SQL).
103 return prefix
104 * allow_underscores_except_first(digits^1)
105 * (P "." * #(1 - P ".") * allow_underscores(digits))^-1
106 * (S "eE" * S "+-"^-1 * allow_underscores_except_first(digits^1))^-1
107 end
108 local Number =
109 K ( 'Number.Internal' ,
110 digits_to_number (P "0x" + P "0X", R "af" + R "AF" + digit)
111 + digits_to_number (P "0o" + P "0O", R "07")
112 + digits_to_number (P "0b" + P "0B", R "01")
113 + digits_to_number ( "" , digit )
114 )
115 local lpeg_central = 1 - S " '\"\r[({})]" - digit
116 if piton.begin_escape then
117 lpeg_central = lpeg_central - piton.begin_escape
118 end
119 if piton.begin_escape_math then
120 lpeg_central = lpeg_central - piton.begin_escape_math
121 end
122 local Word = Q ( lpeg_central ^ 1 )
123 local Space = Q " " ^ 1
124 local SkipSpace = Q " " ^ 0
125
126 local Punct = Q ( S ".,:;!" )
127
128 local Tab = "\t" * Lc [[ \__piton_tab: ]]
129 local LeadingSpace = Lc [[ \__piton_leading_space: ]] * P " "
130 local Delim = Q ( S "[({})]" )
131 local SpaceInString = space * Lc [[ \l__piton_space_in_string_tl ]]
132
133 local detected_commands = tex.toks.PitonDetectedCommands : explode ( ',' )
134 local raw_detected_commands = tex.toks.PitonRawDetectedCommands : explode ( ',' )
135 local beamer_commands = tex.toks.PitonBeamerCommands : explode ( ',' )
136 local beamer_environments = tex.toks.PitonBeamerEnvironments : explode ( ',' )
137 local detectedCommands = P ( false )
138 for _ , x in ipairs ( detected_commands ) do
139 detectedCommands = detectedCommands + P ( "\\" .. x )
140 end
141 local rawDetectedCommands = P ( false )
142 for _ , x in ipairs ( raw_detected_commands ) do
143 rawDetectedCommands = rawDetectedCommands + P ( "\\" .. x )
144 end
145 local beamerCommands = P ( false )
146 for _ , x in ipairs ( beamer_commands ) do
147 beamerCommands = beamerCommands + P ( "\\" .. x )
148 end
149 local beamerEnvironments = P ( false )
150 for _ , x in ipairs ( beamer_environments ) do
151 beamerEnvironments = beamerEnvironments + P ( x )
152 end
153 local LPEG0 = { }
154 local LPEG1 = { }
155 local LPEG2 = { }
156 local LPEG_cleaner = { }
157 local Compute_braces
158 function Compute_braces ( lpeg_string ) return
159 P { "E" ,
160 E =
161 (
162 "{" * V "E" * "}"
163 +
164 lpeg_string
165 +
166 ( 1 - S "{}" )
167 ) ^ 0
168 }
169 end
170 local Compute_DetectedCommands
171 function Compute_DetectedCommands ( lang , braces ) return
172 Ct (
173 Cc "Open"
174 * C ( detectedCommands * space ^ 0 * P "{" )
175 * Cc "}"
176 )
177 * ( braces
178 / ( function ( s )
179 if s ~= '' then return
180 LPEG1[lang] : match ( s )
181 end
182 end )
183 )
184 * P "}"
185 * Ct ( Cc "Close" )
186 end
187 local Compute_RawDetectedCommands
188 function Compute_RawDetectedCommands ( lang , braces ) return
189 Ct ( C ( rawDetectedCommands * space ^ 0 * P "{" * braces * P "}" ) )
190 end
191 local Compute_LPEG_cleaner
192 function Compute_LPEG_cleaner ( lang , braces ) return
193 Ct ( ( ( detectedCommands + rawDetectedCommands ) * "{"
194 * ( braces
195 / ( function ( s )
196 if s ~= '' then return
197 LPEG_cleaner[lang] : match ( s )
198 end
199 end )
200 )
201 * "}"
202 + EscapeClean
203 + C ( P ( 1 ) )
204 ) ^ 0 ) / table.concat
205 end
206 local ParseAgain
207 function ParseAgain ( code )
208 if code ~= '' then return
209 LPEG1[piton.language] : match ( code )
210 end
211 end
212 local Beamer = P ( false )
213 local Compute_Beamer
214 function Compute_Beamer ( lang , braces )
215 local lpeg = L ( P [[\pause]] * ( "[" * ( 1 - P "]" ) ^ 0 * "]" ) ^ -1 )
216 lpeg = lpeg +
217 Ct ( Cc "Open"
218 * C ( beamerCommands
219 * ( "<" * ( 1 - P ">" ) ^ 0 * ">" ) ^ -1
220 * P "{"
221 )
222 * Cc "}"
223 )
224 * ( braces /
225 ( function ( s ) if s ~= '' then return LPEG1[lang] : match ( s ) end end ) )
226 * "}"
227 * Ct ( Cc "Close" )
228 lpeg = lpeg +
229 L ( P [[\alt]] * "<" * ( 1 - P ">" ) ^ 0 * ">{" )
230 * ( braces /
231 ( function ( s ) if s ~= '' then return LPEG1[lang] : match ( s ) end end ) )
232 * L ( P "}{" )
233 * ( braces /
234 ( function ( s ) if s ~= '' then return LPEG1[lang] : match ( s ) end end ) )
235 * L ( P "}" )
236 lpeg = lpeg +
237 L ( P [[\temporal]] * "<" * ( 1 - P ">" ) ^ 0 * ">{" )
238 * ( braces
239 / ( function ( s )
240 if s ~= '' then return LPEG1[lang] : match ( s ) end end ) )
241 * L ( P "}{" )
242 * ( braces
243 / ( function ( s )
244 if s ~= '' then return LPEG1[lang] : match ( s ) end end ) )
245 * L ( P "}{" )
246 * ( braces
247 / ( function ( s )
248 if s ~= '' then return LPEG1[lang] : match ( s ) end end ) )
249 * L ( P "}" )
250 for _ , x in ipairs ( beamer_environments ) do
251 lpeg = lpeg +
252 Ct ( Cc "Open"
253 * C (
254 P ( [[\begin{]] .. x .. "}" )
255 * ( "<" * ( 1 - P ">") ^ 0 * ">" ) ^ -1
256 )
257 * space ^ 0 * ( P "\r" ) ^ 1 -- added 25/08/23
258 * Cc ( [[\end{]] .. x .. "}" )
259 )
260 * (
261 (
262 P { "E" ,
263 E = (
264 P ( [[\begin{]] .. x .. "}" )
265 * V "E"
266 * P ( [[\end{]] .. x .. "}" )
267 +
268 (
269 1
270 - P ( [[\begin{]] .. x .. "}" )
271 - P ( [[\end{]] .. x .. "}" )
272 )
273 ) ^ 0
274 }
275 )
276 / ( function ( s )
277 if s ~= '' then return
278 LPEG1[lang] : match ( s )
279 end
280 end )
281 )
282 * P ( [[\end{]] .. x .. "}" )
283 * Ct ( Cc "Close" )
284 end
285 return lpeg
286 end
287 local CommentMath =
288 P "$" * K ( 'Comment.Math' , ( 1 - S "$\r" ) ^ 1 ) * P "$" -- $
289 local Prompt =
290 K ( 'Prompt' , ( P ">>>" + "..." ) * P " " ^ -1 )
291 * Lc [[ \rowcolor { \l__piton_prompt_bg_color_tl } ]]
292 local EOL =
293 P "\r"
294 *
295 (
296 space ^ 0 * -1
297 +
298 Cc "EOL"
299 )
300 * ( LeadingSpace ^ 0 * # ( 1 - S " \r" ) ) ^ -1
301 local CommentLaTeX =
302 P ( piton.comment_latex )
303 * Lc [[{\PitonStyle{Comment.LaTeX}{\ignorespaces]]
304 * L ( ( 1 - P "\r" ) ^ 0 )
305 * Lc "}}"
306 * ( EOL + -1 )
307 --python Python
308 do
309 local Operator =
310 K ( 'Operator' ,
311 P "!=" + "<>" + "==" + "<<" + ">>" + "<=" + ">=" + ":=" + "//" + "**"
312 + S "-~+/*%=<>&.@|" )
313
314 local OperatorWord =
315 K ( 'Operator.Word' , P "in" + "is" + "and" + "or" + "not" )
316 local For = K ( 'Keyword' , P "for" )
317 * Space
318 * Identifier
319 * Space
320 * K ( 'Keyword' , P "in" )
321
322 local Keyword =
323 K ( 'Keyword' ,
324 P "assert" + "as" + "break" + "case" + "class" + "continue" + "def" +
325 "del" + "elif" + "else" + "except" + "exec" + "finally" + "for" + "from" +
326 "global" + "if" + "import" + "lambda" + "non local" + "pass" + "return" +
327 "try" + "while" + "with" + "yield" + "yield from" )
328 + K ( 'Keyword.Constant' , P "True" + "False" + "None" )
329
330 local Builtin =
331 K ( 'Name.Builtin' ,
332 P "__import__" + "abs" + "all" + "any" + "bin" + "bool" + "bytearray" +
333 "bytes" + "chr" + "classmethod" + "compile" + "complex" + "delattr" +
334 "dict" + "dir" + "divmod" + "enumerate" + "eval" + "filter" + "float" +
335 "format" + "frozenset" + "getattr" + "globals" + "hasattr" + "hash" +
336 "hex" + "id" + "input" + "int" + "isinstance" + "issubclass" + "iter" +
337 "len" + "list" + "locals" + "map" + "max" + "memoryview" + "min" + "next"
338 + "object" + "oct" + "open" + "ord" + "pow" + "print" + "property" +
339 "range" + "repr" + "reversed" + "round" + "set" + "setattr" + "slice" +
340 "sorted" + "staticmethod" + "str" + "sum" + "super" + "tuple" + "type" +
341 "vars" + "zip" )
342
343 local Exception =
344 K ( 'Exception' ,
345 P "ArithmeticError" + "AssertionError" + "AttributeError" +
346 "BaseException" + "BufferError" + "BytesWarning" + "DeprecationWarning" +
347 "EOFError" + "EnvironmentError" + "Exception" + "FloatingPointError" +
348 "FutureWarning" + "GeneratorExit" + "IOError" + "ImportError" +
349 "ImportWarning" + "IndentationError" + "IndexError" + "KeyError" +
350 "KeyboardInterrupt" + "LookupError" + "MemoryError" + "NameError" +
351 "NotImplementedError" + "OSError" + "OverflowError" +
352 "PendingDeprecationWarning" + "ReferenceError" + "ResourceWarning" +
353 "RuntimeError" + "RuntimeWarning" + "StopIteration" + "SyntaxError" +
354 "SyntaxWarning" + "SystemError" + "SystemExit" + "TabError" + "TypeError"
355 + "UnboundLocalError" + "UnicodeDecodeError" + "UnicodeEncodeError" +
356 "UnicodeError" + "UnicodeTranslateError" + "UnicodeWarning" +
357 "UserWarning" + "ValueError" + "VMSError" + "Warning" + "WindowsError" +
358 "ZeroDivisionError" + "BlockingIOError" + "ChildProcessError" +
359 "ConnectionError" + "BrokenPipeError" + "ConnectionAbortedError" +
360 "ConnectionRefusedError" + "ConnectionResetError" + "FileExistsError" +
361 "FileNotFoundError" + "InterruptedError" + "IsADirectoryError" +
362 "NotADirectoryError" + "PermissionError" + "ProcessLookupError" +
363 "TimeoutError" + "StopAsyncIteration" + "ModuleNotFoundError" +
364 "RecursionError" )
365
366 local RaiseException = K ( 'Keyword' , P "raise" ) * SkipSpace * Exception * Q "("
367 local Decorator = K ( 'Name.Decorator' , P "@" * letter ^ 1 )
368 local DefClass =
369 K ( 'Keyword' , "class" ) * Space * K ( 'Name.Class' , identifier )
370 local ImportAs =
371 K ( 'Keyword' , "import" )
372 * Space
373 * K ( 'Name.Namespace' , identifier * ( "." * identifier ) ^ 0 )
374 * (
375 ( Space * K ( 'Keyword' , "as" ) * Space
376 * K ( 'Name.Namespace' , identifier ) )
377 +
378 ( SkipSpace * Q "," * SkipSpace
379 * K ( 'Name.Namespace' , identifier ) ) ^ 0
380 )
381 local FromImport =
382 K ( 'Keyword' , "from" )
383 * Space * K ( 'Name.Namespace' , identifier )
384 * Space * K ( 'Keyword' , "import" )
385 local PercentInterpol =
386 K ( 'String.Interpol' ,
387 P "%"
388 * ( "(" * alphanum ^ 1 * ")" ) ^ -1
389 * ( S "-#0 +" ) ^ 0
390 * ( digit ^ 1 + "*" ) ^ -1
391 * ( "." * ( digit ^ 1 + "*" ) ) ^ -1
392 * ( S "HlL" ) ^ -1
393 * S "sdfFeExXorgiGauc%"
394 )
395 local SingleShortString =
396 WithStyle ( 'String.Short.Internal' ,
397 Q ( P "f'" + "F'" )
398 * (
399 K ( 'String.Interpol' , "{" )
400 * K ( 'Interpol.Inside' , ( 1 - S "}':" ) ^ 0 )
401 * Q ( P ":" * ( 1 - S "}:'" ) ^ 0 ) ^ -1
402 * K ( 'String.Interpol' , "}" )
403 +
404 SpaceInString
405 +
406 Q ( ( P "\\'" + "\\\\" + "{{" + "}}" + 1 - S " {}'" ) ^ 1 )
407 ) ^ 0
408 * Q "'"
409 +
410 Q ( P "'" + "r'" + "R'" )
411 * ( Q ( ( P "\\'" + "\\\\" + 1 - S " '\r%" ) ^ 1 )
412 + SpaceInString
413 + PercentInterpol
414 + Q "%"
415 ) ^ 0
416 * Q "'" )
417 local DoubleShortString =
418 WithStyle ( 'String.Short.Internal' ,
419 Q ( P "f\"" + "F\"" )
420 * (
421 K ( 'String.Interpol' , "{" )
422 * K ( 'Interpol.Inside' , ( 1 - S "}\":" ) ^ 0 )
423 * ( K ( 'String.Interpol' , ":" ) * Q ( (1 - S "}:\"") ^ 0 ) ) ^ -1
424 * K ( 'String.Interpol' , "}" )
425 +
426 SpaceInString
427 +
428 Q ( ( P "\\\"" + "\\\\" + "{{" + "}}" + 1 - S " {}\"" ) ^ 1 )
429 ) ^ 0
430 * Q "\""
431 +
432 Q ( P "\"" + "r\"" + "R\"" )
433 * ( Q ( ( P "\\\"" + "\\\\" + 1 - S " \"\r%" ) ^ 1 )
434 + SpaceInString
435 + PercentInterpol
436 + Q "%"
437 ) ^ 0
438 * Q "\"" )
439
440 local ShortString = SingleShortString + DoubleShortString
441 local braces =
442 Compute_braces
443 (
444 ( P "\"" + "r\"" + "R\"" + "f\"" + "F\"" )
445 * ( P '\\\"' + 1 - S "\"" ) ^ 0 * "\""
446 +
447 ( P '\'' + 'r\'' + 'R\'' + 'f\'' + 'F\'' )
448 * ( P '\\\'' + 1 - S '\'' ) ^ 0 * '\''
449 )
450
451 if piton.beamer then Beamer = Compute_Beamer ( 'python' , braces ) end
452 DetectedCommands = Compute_DetectedCommands ( 'python' , braces )
453 + Compute_RawDetectedCommands ( 'python' , braces )
454 LPEG_cleaner.python = Compute_LPEG_cleaner ( 'python' , braces )
455 local SingleLongString =
456 WithStyle ( 'String.Long.Internal' ,
457 ( Q ( S "fF" * P "'''" )
458 * (
459 K ( 'String.Interpol' , "{" )
460 * K ( 'Interpol.Inside' , ( 1 - S "}:\r" - "'''" ) ^ 0 )
461 * Q ( P ":" * (1 - S "}:\r" - "'''" ) ^ 0 ) ^ -1
462 * K ( 'String.Interpol' , "}" )
463 +
464 Q ( ( 1 - P "'''" - S "{}'\r" ) ^ 1 )
465 +
466 EOL
467 ) ^ 0
468 +
469 Q ( ( S "rR" ) ^ -1 * "'''" )
470 * (
471 Q ( ( 1 - P "'''" - S "\r%" ) ^ 1 )
472 +
473 PercentInterpol
474 +
475 P "%"
476 +
477 EOL
478 ) ^ 0
479 )
480 * Q "'''" )
481 local DoubleLongString =
482 WithStyle ( 'String.Long.Internal' ,
483 (
484 Q ( S "fF" * "\"\"\"" )
485 * (
486 K ( 'String.Interpol', "{" )
487 * K ( 'Interpol.Inside' , ( 1 - S "}:\r" - "\"\"\"" ) ^ 0 )
488 * Q ( ":" * (1 - S "}:\r" - "\"\"\"" ) ^ 0 ) ^ -1
489 * K ( 'String.Interpol' , "}" )
490 +
491 Q ( ( 1 - S "{}\"\r" - "\"\"\"" ) ^ 1 )
492 +
493 EOL
494 ) ^ 0
495 +
496 Q ( S "rR" ^ -1 * "\"\"\"" )
497 * (
498 Q ( ( 1 - P "\"\"\"" - S "%\r" ) ^ 1 )
499 +
500 PercentInterpol
501 +
502 P "%"
503 +
504 EOL
505 ) ^ 0
506 )
507 * Q "\"\"\""
508 )
509 local LongString = SingleLongString + DoubleLongString
510 local StringDoc =
511 K ( 'String.Doc.Internal' , P "r" ^ -1 * "\"\"\"" )
512 * ( K ( 'String.Doc.Internal' , (1 - P "\"\"\"" - "\r" ) ^ 0 ) * EOL
513 * Tab ^ 0
514 ) ^ 0
515 * K ( 'String.Doc.Internal' , ( 1 - P "\"\"\"" - "\r" ) ^ 0 * "\"\"\"" )
516 local Comment =
517 WithStyle
518 ( 'Comment.Internal' ,
519 Q "#" * ( CommentMath + Q ( ( 1 - S "$\r" ) ^ 1 ) ) ^ 0 -- $
520 )
521 * ( EOL + -1 )
522 local expression =
523 P { "E" ,
524 E = ( "'" * ( P "\\'" + 1 - S "'\r" ) ^ 0 * "'"
525 + "\"" * ( P "\\\"" + 1 - S "\"\r" ) ^ 0 * "\""
526 + "{" * V "F" * "}"
527 + "(" * V "F" * ")"
528 + "[" * V "F" * "]"
529 + ( 1 - S "{}()[]\r," ) ) ^ 0 ,
530 F = ( "{" * V "F" * "}"
531 + "(" * V "F" * ")"
532 + "[" * V "F" * "]"
533 + ( 1 - S "{}()[]\r\"'" ) ) ^ 0
534 }
535 local Params =
536 P { "E" ,
537 E = ( V "F" * ( Q "," * V "F" ) ^ 0 ) ^ -1 ,
538 F = SkipSpace * ( Identifier + Q "*args" + Q "**kwargs" ) * SkipSpace
539 * ( Q ":" * SkipSpace * K ( 'Name.Type' , identifier ) ) ^ -1
540 * ( SkipSpace * K ( 'InitialValues' , "=" * SkipSpace * expression ) ) ^ -1
541 }
542 local DefFunction =
543 K ( 'Keyword' , "def" )
544 * Space
545 * K ( 'Name.Function.Internal' , identifier )
546 * SkipSpace
547 * Q "(" * Params * Q ")"
548 * SkipSpace
549 * ( Q "->" * SkipSpace * K ( 'Name.Type' , identifier ) ) ^ -1
550 * ( C ( ( 1 - S ":\r" ) ^ 0 ) / ParseAgain )
551 * Q ":"
552 * ( SkipSpace
553 * ( EOL + CommentLaTeX + Comment ) -- in all cases, that contains an EOL
554 * Tab ^ 0
555 * SkipSpace
556 * StringDoc ^ 0 -- there may be additional docstrings
557 ) ^ -1
558 local ExceptionInConsole = Exception * Q ( ( 1 - P "\r" ) ^ 0 ) * EOL
559 local EndKeyword
560 = Space + Punct + Delim + EOL + Beamer + DetectedCommands + Escape +
561 EscapeMath + -1
562 local Main =
563 space ^ 0 * EOL
564 + Space
565 + Tab
566 + Escape + EscapeMath
567 + Beamer
568 + CommentLaTeX
569 + DetectedCommands
570 + Prompt
571 + LongString
572 + Comment
573 + ExceptionInConsole
574 + Delim
575 + Operator
576 + OperatorWord * EndKeyword
577 + ShortString
578 + Punct
579 + FromImport
580 + RaiseException
581 + DefFunction
582 + DefClass
583 + For
584 + Keyword * EndKeyword
585 + Decorator
586 + Builtin * EndKeyword
587 + Identifier
588 + Number
589 + Word
590 LPEG1.python = Main ^ 0
591 LPEG2.python =
592 Ct (
593 ( space ^ 0 * "\r" ) ^ -1
594 * Lc [[ \__piton_begin_line: ]]
595 * LeadingSpace ^ 0
596 * ( space ^ 1 * -1 + Main ) ^ 0
597 * -1
598 * Lc [[ \__piton_end_line: ]]
599 )
600 end
601 --ocaml Ocaml OCaml
602 do
603 local SkipSpace = ( Q " " + EOL ) ^ 0
604 local Space = ( Q " " + EOL ) ^ 1
605 local braces = Compute_braces ( '\"' * ( 1 - S "\"" ) ^ 0 * '\"' )
606 if piton.beamer then Beamer = Compute_Beamer ( 'ocaml' , braces ) end
607 DetectedCommands =
608 Compute_DetectedCommands ( 'ocaml' , braces )
609 + Compute_RawDetectedCommands ( 'ocaml' , braces )
610 local Q
611 function Q ( pattern, strict )
612 if strict ~= nil then
613 return Ct ( Cc ( luatexbase.catcodetables.CatcodeTableOther ) * C ( pattern ) )
614 else
615 return Ct ( Cc ( luatexbase.catcodetables.CatcodeTableOther ) * C ( pattern ) )
616 + Beamer + DetectedCommands + EscapeMath + Escape
617 end
618 end
619 local K
620 function K ( style , pattern, strict ) return
621 Lc ( [[ {\PitonStyle{ ]] .. style .. "}{" )
622 * Q ( pattern, strict )
623 * Lc "}}"
624 end
625 local WithStyle
626 function WithStyle ( style , pattern ) return
627 Ct ( Cc "Open" * Cc ( [[{\PitonStyle{]] .. style .. "}{" ) * Cc "}}" )
628 * (pattern + Beamer + DetectedCommands + EscapeMath + Escape)
629 * Ct ( Cc "Close" )
630 end
631 local balanced_parens =
632 P { "E" , E = ( "(" * V "E" * ")" + ( 1 - S "()" ) ) ^ 0 }
633 local ocaml_string =
634 P "\""
635 * (
636 P " "
637 +
638 P ( ( P '\\\"' + 1 - S " \"\r" ) ^ 1 )
639 +
640 EOL -- ?
641 ) ^ 0
642 * P "\""
643 local String =
644 WithStyle
645 ( 'String.Long.Internal' ,
646 Q "\""
647 * (
648 SpaceInString
649 +
650 Q ( ( P '\\\"' + 1 - S " \"\r" ) ^ 1 )
651 +
652 EOL
653 ) ^ 0
654 * Q "\""
655 )
656 local ext = ( R "az" + "_" ) ^ 0
657 local open = "{" * Cg ( ext , 'init' ) * "|"
658 local close = "|" * C ( ext ) * "}"
659 local closeeq =
660 Cmt ( close * Cb ( 'init' ) ,
661 function ( s , i , a , b ) return a == b end )
662 local QuotedStringBis =
663 WithStyle ( 'String.Long.Internal' ,
664 (
665 Space
666 +
667 Q ( ( 1 - S " \r" ) ^ 1 )
668 +
669 EOL
670 ) ^ 0 )
671 local QuotedString =
672 C ( open * ( 1 - closeeq ) ^ 0 * close ) /
673 ( function ( s ) return QuotedStringBis : match ( s ) end )
674 local comment =
675 P {
676 "A" ,
677 A = Q "(*"
678 * ( V "A"
679 + Q ( ( 1 - S "\r$\"" - "(*" - "*)" ) ^ 1 ) -- $
680 + Q ( ocaml_string )
681 + "$" * K ( 'Comment.Math' , ( 1 - S "$\r" ) ^ 1 ) * "$" -- $
682 + EOL
683 ) ^ 0
684 * Q "*)"
685 }
686 local Comment = WithStyle ( 'Comment.Internal' , comment )
687 local Delim = K ( 'Delim' , P "[|" + "|]" + S "[()]" )
688 local Punct = K ( 'Punct' , S ",:;!" )
689 local cap_identifier = R "AZ" * ( R "az" + R "AZ" + S "_'" + digit ) ^ 0
690 local Constructor =
691 P "::"
692 * Lc [[{\PitonStyle{Name.Constructor}{\kern0.1em:\kern-0.2em:\kern0.1em}}]]
693 +
694 P "[]"
695 * Lc ([[{\PitonStyle{Name.Constructor}{\kern-0.1em[\kern0.1em]}}]])
696 K ( 'Name.Constructor' ,
697 Q "`" ^ -1 * cap_identifier
698 + Q ( "[" , true ) * SkipSpace * Q ( "]" , true) )
699 local ModuleType = K ( 'Name.Type' , cap_identifier )
700 local OperatorWord =
701 K ( 'Operator.Word' ,
702 P "asr" + "land" + "lor" + "lsl" + "lxor" + "mod" + "or" + "not" )
703 local governing_keyword = P "and" + "begin" + "class" + "constraint" +
704 "end" + "external" + "functor" + "include" + "inherit" + "initializer" +
705 "in" + "let" + "method" + "module" + "object" + "open" + "rec" + "sig" +
706 "struct" + "type" + "val"
707 local Keyword =
708 K ( 'Keyword' ,
709 P "assert" + "as" + "done" + "downto" + "do" + "else" + "exception"
710 + "for" + "function" + "fun" + "if" + "lazy" + "match" + "mutable"
711 + "new" + "of" + "private" + "raise" + "then" + "to" + "try"
712 + "virtual" + "when" + "while" + "with" )
713 + K ( 'Keyword.Constant' , P "true" + "false" )
714 + K ( 'Keyword.Governing', governing_keyword )
715 local EndKeyword
716 = Space + Punct + Delim + EOL + Beamer + DetectedCommands + Escape
717 + EscapeMath + -1
718 local identifier = ( R "az" + "_" ) * ( R "az" + R "AZ" + S "_'" + digit ) ^ 0
719 - ( OperatorWord + Keyword ) * EndKeyword
720 local Identifier = K ( 'Identifier.Internal' , identifier )
721 local ocaml_char =
722 P "'" *
723 (
724 ( 1 - S "'\\" )
725 + "\\"
726 * ( S "\\'ntbr \""
727 + digit * digit * digit
728 + P "x" * ( digit + R "af" + R "AF" )
729 * ( digit + R "af" + R "AF" )
730 * ( digit + R "af" + R "AF" )
731 + P "o" * R "03" * R "07" * R "07" )
732 )
733 * "'"
734 local Char =
735 K ( 'String.Short.Internal', ocaml_char )
736 local TypeParameter =
737 K ( 'TypeParameter' ,
738 "'" * Q "_" ^ -1 * alpha ^ 1 * digit ^ 0 * ( # ( 1 - P "'" ) + -1 ) )
739 local DotNotation =
740 (
741 K ( 'Name.Module' , cap_identifier )
742 * Q "."
743 * ( Identifier + Constructor + Q "(" + Q "[" + Q "{" ) ^ -1
744 +
745 Identifier
746 * Q "."
747 * K ( 'Name.Field' , identifier )
748 )
749 * ( Q "." * K ( 'Name.Field' , identifier ) ) ^ 0
750 local expression_for_fields_type =
751 P { "E" ,
752 E = ( "{" * V "F" * "}"
753 + "(" * V "F" * ")"
754 + TypeParameter
755 + ( 1 - S "{}()[]\r;" ) ) ^ 0 ,
756 F = ( "{" * V "F" * "}"
757 + "(" * V "F" * ")"
758 + ( 1 - S "{}()[]\r\"'" ) + TypeParameter ) ^ 0
759 }
760 local expression_for_fields_value =
761 P { "E" ,
762 E = ( "{" * V "F" * "}"
763 + "(" * V "F" * ")"
764 + "[" * V "F" * "]"
765 + ocaml_string + ocaml_char
766 + ( 1 - S "{}()[];" ) ) ^ 0 ,
767 F = ( "{" * V "F" * "}"
768 + "(" * V "F" * ")"
769 + "[" * V "F" * "]"
770 + ocaml_string + ocaml_char
771 + ( 1 - S "{}()[]\"'" )) ^ 0
772 }
773 local OneFieldDefinition =
774 ( K ( 'Keyword' , "mutable" ) * SkipSpace ) ^ -1
775 * K ( 'Name.Field' , identifier ) * SkipSpace
776 * Q ":" * SkipSpace
777 * K ( 'TypeExpression' , expression_for_fields_type )
778 * SkipSpace
779 local OneField =
780 K ( 'Name.Field' , identifier ) * SkipSpace
781 * Q "=" * SkipSpace
782 * ( C ( expression_for_fields_value ) / ParseAgain )
783 * SkipSpace
784 local RecordVal =
785 Q "{" * SkipSpace
786 *
787 (
788 (Identifier + DotNotation) * Space * K('Keyword', "with") * Space
789 ) ^-1
790 *
791 (
792 OneField * ( Q ";" * SkipSpace * ( Comment * SkipSpace ) ^ 0 * OneField ) ^ 0
793 )
794 * SkipSpace
795 * Q ";" ^ -1
796 * SkipSpace
797 * Comment ^ -1
798 * SkipSpace
799 * Q "}"
800 local RecordType =
801 Q "{" * SkipSpace
802 *
803 (
804 OneFieldDefinition
805 * ( Q ";" * SkipSpace * ( Comment * SkipSpace ) ^ 0 * OneFieldDefinition ) ^ 0
806 )
807 * SkipSpace
808 * Q ";" ^ -1
809 * SkipSpace
810 * Comment ^ -1
811 * SkipSpace
812 * Q "}"
813 local Record = RecordType + RecordVal
814 local Operator =
815 P "||" *
816 Lc([[{\PitonStyle{Operator}{\kern0.1em|\kern-0.2em|\kern0.1em}}]])
817 +
818 K ( 'Operator' ,
819 P "!=" + "<>" + "==" + "<<" + ">>" + "<=" + ">=" + ":=" + "&&" +
820 "//" + "**" + ";;" + "->" + "+." + "-." + "*." + "/."
821 + S "-~+/*%=<>&@|" )
822 local Builtin =
823 K ( 'Name.Builtin' , P "incr" + "decr" + "fst" + "snd" + "ref" )
824 local Exception =
825 K ( 'Exception' ,
826 P "Division_by_zero" + "End_of_File" + "Failure" + "Invalid_argument" +
827 "Match_failure" + "Not_found" + "Out_of_memory" + "Stack_overflow" +
828 "Sys_blocked_io" + "Sys_error" + "Undefined_recursive_module" )
829 LPEG_cleaner.ocaml = Compute_LPEG_cleaner ( 'ocaml' , braces )
830 local pattern_part =
831 ( P "(" * balanced_parens * ")" + ( 1 - S ":()" ) + P "::" ) ^ 0
832 local Argument =
833 ( Q "~" * Identifier * Q ":" * SkipSpace ) ^ -1
834 *
835 (
836 K ( 'Identifier.Internal' , identifier )
837 +
838 Q "(" * SkipSpace
839 * ( C ( pattern_part ) / ParseAgain )
840 * SkipSpace
841 * ( Q ":" * #(1- P"=")
842 * K ( 'TypeExpression' , balanced_parens ) * SkipSpace
843 ) ^ -1
844 * Q ")"
845 )
846 local DefFunction =
847 K ( 'Keyword.Governing' , "let open" )
848 * Space
849 * K ( 'Name.Module' , cap_identifier )
850 +
851 K ( 'Keyword.Governing' , P "let rec" + "let" + "and" )
852 * Space
853 * K ( 'Name.Function.Internal' , identifier )
854 * Space
855 * (
856 Q "=" * SkipSpace * K ( 'Keyword' , "function" , true )
857 +
858 Argument * ( SkipSpace * Argument ) ^ 0
859 * (
860 SkipSpace
861 * Q ":" * # ( 1 - P "=" )
862 * K ( 'TypeExpression' , ( 1 - P "=" ) ^ 0 )
863 ) ^ -1
864 )
865 local DefModule =
866 K ( 'Keyword.Governing' , "module" ) * Space
867 *
868 (
869 K ( 'Keyword.Governing' , "type" ) * Space
870 * K ( 'Name.Type' , cap_identifier )
871 +
872 K ( 'Name.Module' , cap_identifier ) * SkipSpace
873 *
874 (
875 Q "(" * SkipSpace
876 * K ( 'Name.Module' , cap_identifier ) * SkipSpace
877 * Q ":" * # ( 1 - P "=" ) * SkipSpace
878 * K ( 'Name.Type' , cap_identifier ) * SkipSpace
879 *
880 (
881 Q "," * SkipSpace
882 * K ( 'Name.Module' , cap_identifier ) * SkipSpace
883 * Q ":" * # ( 1 - P "=" ) * SkipSpace
884 * K ( 'Name.Type' , cap_identifier ) * SkipSpace
885 ) ^ 0
886 * Q ")"
887 ) ^ -1
888 *
889 (
890 Q "=" * SkipSpace
891 * K ( 'Name.Module' , cap_identifier ) * SkipSpace
892 * Q "("
893 * K ( 'Name.Module' , cap_identifier ) * SkipSpace
894 *
895 (
896 Q ","
897 *
898 K ( 'Name.Module' , cap_identifier ) * SkipSpace
899 ) ^ 0
900 * Q ")"
901 ) ^ -1
902 )
903 +
904 K ( 'Keyword.Governing' , P "include" + "open" )
905 * Space
906 * K ( 'Name.Module' , cap_identifier )
907 local DefType =
908 K ( 'Keyword.Governing' , "type" )
909 * Space
910 * K ( 'TypeExpression' , Q ( 1 - P "=" - P "+=" ) ^ 1 )
911 * SkipSpace
912 * ( Q "+=" + Q "=" )
913 * SkipSpace
914 * (
915 RecordType
916 +
917 WithStyle
918 (
919 'TypeExpression' ,
920 (
921 (
922 EOL
923 + comment
924 + Q ( 1
925 - P ";;"
926 - P "type"
927 - ( ( Space + EOL ) * governing_keyword * EndKeyword )
928 )
929 ) ^ 0
930 *
931 (
932 # ( P "type" + ( Space + EOL ) * governing_keyword * EndKeyword )
933 + Q ";;"
934 + -1
935 )
936 )
937 )
938 )
939 local prompt =
940 Q "utop[" * digit^1 * Q "]> "
941 local start_of_line = P(function(subject, position)
942 if position == 1 or subject:sub(position - 1, position - 1) == "\r" then
943 return position
944 end
945 return nil
946 end)
947 local Prompt = #start_of_line * K( 'Prompt', prompt )
948 local Answer = #start_of_line * (Q "-" + Q "val" * Space * Identifier )
949 * SkipSpace * Q ":" * #(1- P"=") * SkipSpace
950 * (K ( 'TypeExpression' , Q ( 1 - P "=") ^ 1 ) ) * SkipSpace * Q "="
951 local Main =
952 space ^ 0 * EOL
953 + Space
954 + Tab
955 + Escape + EscapeMath
956 + Beamer
957 + DetectedCommands
958 + TypeParameter
959 + String + QuotedString + Char
960 + Comment
961 + Prompt + Answer
962 + Q "~" * Identifier * ( Q ":" ) ^ -1
963 + Q ":" * # (1 - P ":") * SkipSpace
964 * K ( 'TypeExpression' , balanced_parens ) * SkipSpace * Q ")"
965 + Exception
966 + DefType
967 + DefFunction
968 + DefModule
969 + Record
970 + Keyword * EndKeyword
971 + OperatorWord * EndKeyword
972 + Builtin * EndKeyword
973 + DotNotation * EndKeyword
974 + Constructor
975 + Identifier
976 + Punct
977 + Delim -- Delim is before Operator for a correct analysis of [| et |]
978 + Operator
979 + Number
980 + Word
981 LPEG1.ocaml = Main ^ 0
982 LPEG2.ocaml =
983 Ct (
984 (
985 (
986 P ":"
987 +
988 (
989 ( K ( 'Name.Module' , cap_identifier ) * Q "." ) ^ -1
990 * Identifier
991 * SkipSpace
992 * Q ":"
993 )
994 )
995 * # ( 1 - S ":=" )
996 * SkipSpace
997 * K ( 'TypeExpression' , ( 1 - P "\r" ) ^ 0 ) * -1
998 )
999 +
1000 (
1001 ( space ^ 0 * "\r" ) ^ -1
1002 * Lc [[ \__piton_begin_line: ]]
1003 * LeadingSpace ^ 0
1004 * ( ( space * Lc [[ \__piton_trailing_space: ]] ) ^ 1 * -1
1005 + space ^ 0 * EOL
1006 + Main
1007 ) ^ 0
1008 * -1
1009 * Lc [[ \__piton_end_line: ]]
1010 )
1011 )
1012 end
1013 --c C c++ C++
1014 do
1015 local Delim = Q ( S "{[()]}" )
1016 local Punct = Q ( S ",:;!" )
1017 local identifier = letter * alphanum ^ 0
1018
1019 local Operator =
1020 K ( 'Operator' ,
1021 P "!=" + "==" + "<<" + ">>" + "<=" + ">=" + "||" + "&&"
1022 + S "-~+/*%=<>&.@|!" )
1023
1024 local Keyword =
1025 K ( 'Keyword' ,
1026 P "alignas" + "asm" + "auto" + "break" + "case" + "catch" + "class" +
1027 "const" + "constexpr" + "continue" + "decltype" + "do" + "else" + "enum" +
1028 "extern" + "for" + "goto" + "if" + "nexcept" + "private" + "public" +
1029 "register" + "restricted" + "return" + "static" + "static_assert" +
1030 "struct" + "switch" + "thread_local" + "throw" + "try" + "typedef" +
1031 "union" + "using" + "virtual" + "volatile" + "while"
1032 )
1033 + K ( 'Keyword.Constant' , P "default" + "false" + "NULL" + "nullptr" + "true" )
1034
1035 local Builtin =
1036 K ( 'Name.Builtin' ,
1037 P "alignof" + "malloc" + "printf" + "scanf" + "sizeof" )
1038
1039 local Type =
1040 K ( 'Name.Type' ,
1041 P "bool" + "char" + "char16_t" + "char32_t" + "double" + "float" +
1042 "int8_t" + "int16_t" + "int32_t" + "int64_t" + "uint8_t" + "uint16_t" +
1043 "uint32_t" + "uint64_t" + "int" + "long" + "short" + "signed" + "unsigned" +
1044 "void" + "wchar_t" ) * Q "*" ^ 0
1045
1046 local DefFunction =
1047 Type
1048 * Space
1049 * Q "*" ^ -1
1050 * K ( 'Name.Function.Internal' , identifier )
1051 * SkipSpace
1052 * # P "("
1053 local DefClass =
1054 K ( 'Keyword' , "class" ) * Space * K ( 'Name.Class' , identifier )
1055 local Character =
1056 K ( 'String.Short' ,
1057 P [['\'']] + P "'" * ( 1 - P "'" ) ^ 0 * P "'" )
1058 String =
1059 WithStyle ( 'String.Long.Internal' ,
1060 Q "\""
1061 * ( SpaceInString
1062 + K ( 'String.Interpol' ,
1063 "%" * ( S "difcspxXou" + "ld" + "li" + "hd" + "hi" )
1064 )
1065 + Q ( ( P "\\\"" + 1 - S " \"" ) ^ 1 )
1066 ) ^ 0
1067 * Q "\""
1068 )
1069 local braces = Compute_braces ( "\"" * ( 1 - S "\"" ) ^ 0 * "\"" )
1070 if piton.beamer then Beamer = Compute_Beamer ( 'c' , braces ) end
1071 DetectedCommands =
1072 Compute_DetectedCommands ( 'c' , braces )
1073 + Compute_RawDetectedCommands ( 'c' , braces )
1074 LPEG_cleaner.c = Compute_LPEG_cleaner ( 'c' , braces )
1075 local Preproc = K ( 'Preproc' , "#" * ( 1 - P "\r" ) ^ 0 ) * ( EOL + -1 )
1076 local Comment =
1077 WithStyle ( 'Comment.Internal' ,
1078 Q "//" * ( CommentMath + Q ( ( 1 - S "$\r" ) ^ 1 ) ) ^ 0 ) -- $
1079 * ( EOL + -1 )
1080
1081 local LongComment =
1082 WithStyle ( 'Comment.Internal' ,
1083 Q "/*"
1084 * ( CommentMath + Q ( ( 1 - P "*/" - S "$\r" ) ^ 1 ) + EOL ) ^ 0
1085 * Q "*/"
1086 ) -- $
1087 local EndKeyword
1088 = Space + Punct + Delim + EOL + Beamer + DetectedCommands + Escape +
1089 EscapeMath + -1
1090 local Main =
1091 space ^ 0 * EOL
1092 + Space
1093 + Tab
1094 + Escape + EscapeMath
1095 + CommentLaTeX
1096 + Beamer
1097 + DetectedCommands
1098 + Preproc
1099 + Comment + LongComment
1100 + Delim
1101 + Operator
1102 + Character
1103 + String
1104 + Punct
1105 + DefFunction
1106 + DefClass
1107 + Type * ( Q "*" ^ -1 + EndKeyword )
1108 + Keyword * EndKeyword
1109 + Builtin * EndKeyword
1110 + Identifier
1111 + Number
1112 + Word
1113 LPEG1.c = Main ^ 0
1114 LPEG2.c =
1115 Ct (
1116 ( space ^ 0 * P "\r" ) ^ -1
1117 * Lc [[ \__piton_begin_line: ]]
1118 * LeadingSpace ^ 0
1119 * ( space ^ 1 * -1 + Main ) ^ 0
1120 * -1
1121 * Lc [[ \__piton_end_line: ]]
1122 )
1123 end
1124 --sql SQL
1125 do
1126 local LuaKeyword
1127 function LuaKeyword ( name ) return
1128 Lc [[ {\PitonStyle{Keyword}{ ]]
1129 * Q ( Cmt (
1130 C ( letter * alphanum ^ 0 ) ,
1131 function ( _ , _ , a ) return a : upper ( ) == name end
1132 )
1133 )
1134 * Lc "}}"
1135 end
1136 local identifier =
1137 letter * ( alphanum + "-" ) ^ 0
1138 + P '"' * ( ( 1 - P '"' ) ^ 1 ) * '"'
1139 local Operator =
1140 K ( 'Operator' , P "=" + "!=" + "<>" + ">=" + ">" + "<=" + "<" + S "*+/" )
1141 local Set
1142 function Set ( list )
1143 local set = { }
1144 for _ , l in ipairs ( list ) do set[l] = true end
1145 return set
1146 end
1147 local set_keywords = Set
1148 {
1149 "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ALWAYS", "ANALYZE",
1150 "AND", "AS", "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN",
1151 "BY", "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT",
1152 "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT", "CURRENT_DATE",
1153 "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE",
1154 "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DO", "DROP", "EACH",
1155 "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUDE", "EXCLUSIVE", "EXISTS",
1156 "EXPLAIN", "FAIL", "FILTER", "FIRST", "FOLLOWING", "FOR", "FOREIGN", "FROM",
1157 "FULL", "GENERATED", "GLOB", "GROUP", "GROUPS", "HAVING", "IF", "IGNORE",
1158 "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER", "INSERT",
1159 "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY", "LAST",
1160 "LEFT", "LIKE", "LIMIT", "MATCH", "MATERIALIZED", "NATURAL", "NO", "NOT",
1161 "NOTHING", "NOTNULL", "NULL", "NULLS", "OF", "OFFSET", "ON", "OR", "ORDER",
1162 "OTHERS", "OUTER", "OVER", "PARTITION", "PLAN", "PRAGMA", "PRECEDING",
1163 "PRIMARY", "QUERY", "RAISE", "RANGE", "RECURSIVE", "REFERENCES", "REGEXP",
1164 "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RETURNING", "RIGHT",
1165 "ROLLBACK", "ROW", "ROWS", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP",
1166 "TEMPORARY", "THEN", "TIES", "TO", "TRANSACTION", "TRIGGER", "UNBOUNDED",
1167 "UNION", "UNIQUE", "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL",
1168 "WHEN", "WHERE", "WINDOW", "WITH", "WITHOUT"
1169 }
1170 local set_builtins = Set
1171 {
1172 "AVG" , "COUNT" , "CHAR_LENGTH" , "CONCAT" , "CURDATE" , "CURRENT_DATE" ,
1173 "DATE_FORMAT" , "DAY" , "LOWER" , "LTRIM" , "MAX" , "MIN" , "MONTH" , "NOW" ,
1174 "RANK" , "ROUND" , "RTRIM" , "SUBSTRING" , "SUM" , "UPPER" , "YEAR"
1175 }
1176 local Identifier =
1177 C ( identifier ) /
1178 (
1179 function ( s )
1180 if set_keywords [ s : upper ( ) ] then return
1181 { [[{\PitonStyle{Keyword}{]] } ,
1182 { luatexbase.catcodetables.other , s } ,
1183 { "}}" }
1184 else
1185 if set_builtins [ s : upper ( ) ] then return
1186 { [[{\PitonStyle{Name.Builtin}{]] } ,
1187 { luatexbase.catcodetables.other , s } ,
1188 { "}}" }
1189 else return
1190 { [[{\PitonStyle{Name.Field}{]] } ,
1191 { luatexbase.catcodetables.other , s } ,
1192 { "}}" }
1193 end
1194 end
1195 end
1196 )
1197 local String = K ( 'String.Long.Internal' , "'" * ( 1 - P "'" ) ^ 1 * "'" )
1198 local braces = Compute_braces ( "'" * ( 1 - P "'" ) ^ 1 * "'" )
1199 if piton.beamer then Beamer = Compute_Beamer ( 'sql' , braces ) end
1200 DetectedCommands =
1201 Compute_DetectedCommands ( 'sql' , braces )
1202 + Compute_RawDetectedCommands ( 'sql' , braces )
1203 LPEG_cleaner.sql = Compute_LPEG_cleaner ( 'sql' , braces )
1204 local Comment =
1205 WithStyle ( 'Comment.Internal' ,
1206 Q "--" -- syntax of SQL92
1207 * ( CommentMath + Q ( ( 1 - S "$\r" ) ^ 1 ) ) ^ 0 ) -- $
1208 * ( EOL + -1 )
1209
1210 local LongComment =
1211 WithStyle ( 'Comment.Internal' ,
1212 Q "/*"
1213 * ( CommentMath + Q ( ( 1 - P "*/" - S "$\r" ) ^ 1 ) + EOL ) ^ 0
1214 * Q "*/"
1215 ) -- $
1216 local EndKeyword
1217 = Space + Punct + Delim + EOL + Beamer + DetectedCommands + Escape +
1218 EscapeMath + -1
1219 local TableField =
1220 K ( 'Name.Table' , identifier )
1221 * Q "."
1222 * ( DetectedCommands + ( K ( 'Name.Field' , identifier ) ) ^ 0 )
1223
1224 local OneField =
1225 (
1226 Q ( "(" * ( 1 - P ")" ) ^ 0 * ")" )
1227 +
1228 K ( 'Name.Table' , identifier )
1229 * Q "."
1230 * K ( 'Name.Field' , identifier )
1231 +
1232 K ( 'Name.Field' , identifier )
1233 )
1234 * (
1235 Space * LuaKeyword "AS" * Space * K ( 'Name.Field' , identifier )
1236 ) ^ -1
1237 * ( Space * ( LuaKeyword "ASC" + LuaKeyword "DESC" ) ) ^ -1
1238
1239 local OneTable =
1240 K ( 'Name.Table' , identifier )
1241 * (
1242 Space
1243 * LuaKeyword "AS"
1244 * Space
1245 * K ( 'Name.Table' , identifier )
1246 ) ^ -1
1247
1248 local WeCatchTableNames =
1249 LuaKeyword "FROM"
1250 * ( Space + EOL )
1251 * OneTable * ( SkipSpace * Q "," * SkipSpace * OneTable ) ^ 0
1252 + (
1253 LuaKeyword "JOIN" + LuaKeyword "INTO" + LuaKeyword "UPDATE"
1254 + LuaKeyword "TABLE"
1255 )
1256 * ( Space + EOL ) * OneTable
1257 local EndKeyword
1258 = Space + Punct + Delim + EOL + Beamer
1259 + DetectedCommands + Escape + EscapeMath + -1
1260 local Main =
1261 space ^ 0 * EOL
1262 + Space
1263 + Tab
1264 + Escape + EscapeMath
1265 + CommentLaTeX
1266 + Beamer
1267 + DetectedCommands
1268 + Comment + LongComment
1269 + Delim
1270 + Operator
1271 + String
1272 + Punct
1273 + WeCatchTableNames
1274 + ( TableField + Identifier ) * ( Space + Operator + Punct + Delim + EOL + -1 )
1275 + Number
1276 + Word
1277 LPEG1.sql = Main ^ 0
1278 LPEG2.sql =
1279 Ct (
1280 ( space ^ 0 * "\r" ) ^ -1
1281 * Lc [[ \__piton_begin_line: ]]
1282 * LeadingSpace ^ 0
1283 * ( space ^ 1 * -1 + Main ) ^ 0
1284 * -1
1285 * Lc [[ \__piton_end_line: ]]
1286 )
1287 end
1288 --minimal Minimal
1289 do
1290 local Punct = Q ( S ",:;!\\" )
1291
1292 local Comment =
1293 WithStyle ( 'Comment.Internal' ,
1294 Q "#"
1295 * ( CommentMath + Q ( ( 1 - S "$\r" ) ^ 1 ) ) ^ 0 -- $
1296 )
1297 * ( EOL + -1 )
1298
1299 local String =
1300 WithStyle ( 'String.Short.Internal' ,
1301 Q "\""
1302 * ( SpaceInString
1303 + Q ( ( P [[\"]] + 1 - S " \"" ) ^ 1 )
1304 ) ^ 0
1305 * Q "\""
1306 )
1307 local braces = Compute_braces ( P "\"" * ( P "\\\"" + 1 - P "\"" ) ^ 1 * "\"" )
1308
1309 if piton.beamer then Beamer = Compute_Beamer ( 'minimal' , braces ) end
1310
1311 DetectedCommands =
1312 Compute_DetectedCommands ( 'minimal' , braces )
1313 + Compute_RawDetectedCommands ( 'minimal' , braces )
1314
1315 LPEG_cleaner.minimal = Compute_LPEG_cleaner ( 'minimal' , braces )
1316
1317 local identifier = letter * alphanum ^ 0
1318
1319 local Identifier = K ( 'Identifier.Internal' , identifier )
1320
1321 local Delim = Q ( S "{[()]}" )
1322
1323 local Main =
1324 space ^ 0 * EOL
1325 + Space
1326 + Tab
1327 + Escape + EscapeMath
1328 + CommentLaTeX
1329 + Beamer
1330 + DetectedCommands
1331 + Comment
1332 + Delim
1333 + String
1334 + Punct
1335 + Identifier
1336 + Number
1337 + Word
1338 LPEG1.minimal = Main ^ 0
1339
1340 LPEG2.minimal =
1341 Ct (
1342 ( space ^ 0 * "\r" ) ^ -1
1343 * Lc [[ \__piton_begin_line: ]]
1344 * LeadingSpace ^ 0
1345 * ( space ^ 1 * -1 + Main ) ^ 0
1346 * -1
1347 * Lc [[ \__piton_end_line: ]]
1348 )
1349 end
1350 --verbatim Verbatim
1351 do
1352
1353 local braces =
1354 P { "E" ,
1355 E = ( "{" * V "E" * "}" + ( 1 - S "{}" ) ) ^ 0
1356 }
1357
1358 if piton.beamer then Beamer = Compute_Beamer ( 'verbatim' , braces ) end
1359
1360 DetectedCommands =
1361 Compute_DetectedCommands ( 'verbatim' , braces )
1362 + Compute_RawDetectedCommands ( 'verbatim' , braces )
1363
1364 LPEG_cleaner.verbatim = Compute_LPEG_cleaner ( 'verbatim' , braces )
1365 local lpeg_central = 1 - S " \\\r"
1366 if piton.begin_escape then
1367 lpeg_central = lpeg_central - piton.begin_escape
1368 end
1369 if piton.begin_escape_math then
1370 lpeg_central = lpeg_central - piton.begin_escape_math
1371 end
1372 local Word = Q ( lpeg_central ^ 1 )
1373
1374 local Main =
1375 space ^ 0 * EOL
1376 + Space
1377 + Tab
1378 + Escape + EscapeMath
1379 + Beamer
1380 + DetectedCommands
1381 + Q [[\]]
1382 + Word
1383 LPEG1.verbatim = Main ^ 0
1384
1385 LPEG2.verbatim =
1386 Ct (
1387 ( space ^ 0 * "\r" ) ^ -1
1388 * Lc [[ \__piton_begin_line: ]]
1389 * LeadingSpace ^ 0
1390 * ( space ^ 1 * -1 + Main ) ^ 0
1391 * -1
1392 * Lc [[ \__piton_end_line: ]]
1393 )
1394 end
1395 --EXPL expl
1396 do
1397 local Comment =
1398 WithStyle
1399 ( 'Comment.Internal' ,
1400 Q "%" * ( CommentMath + Q ( ( 1 - S "$\r" ) ^ 1 ) ) ^ 0 -- $
1401 )
1402 * ( EOL + -1 )
1403 local analyze_cs
1404 function analyze_cs ( s )
1405 local i = s : find ( ":" )
1406 if i then
1407 local name = s : sub ( 2 , i - 1 )
1408 local parts = name : explode ( "_" )
1409 local module = parts[1]
1410 if module == "" then module = parts[3] end
1411 return
1412 { [[{\OptionalLocalPitonStyle{Module.]] .. module .. "}{" } ,
1413 { luatexbase.catcodetables.other , s } ,
1414 { "}}" }
1415 else
1416 local p = s : sub ( 1 , 3 )
1417 if p == [[\l_]] or p == [[\g_]] or p == [[\c_]] then
1418 local scope = s : sub(2,2)
1419 local parts = s : explode ( "_" )
1420 local module = parts[2]
1421 if module == "" then module = parts[3] end
1422 local type = parts[#parts]
1423 return
1424 { [[{\OptionalLocalPitonStyle{Scope.]] .. scope .. "}{" } ,
1425 { [[{\OptionalLocalPitonStyle{Module.]] .. module .. "}{" } ,
1426 { [[{\OptionalLocalPitonStyle{Type.]] .. type .. "}{" } ,
1427 { luatexbase.catcodetables.other , s } ,
1428 { "}}}}}}" }
1429 else
1430 return { luatexbase.catcodetables.other , s }
1431 end
1432 end
1433 end
1434 local braces =
1435 P { "E" ,
1436 E = ( "{" * V "E" * "}" + ( 1 - S "{}" ) ) ^ 0
1437 }
1438
1439 if piton.beamer then Beamer = Compute_Beamer ( 'expl' , braces ) end
1440
1441 DetectedCommands =
1442 Compute_DetectedCommands ( 'expl' , braces )
1443 + Compute_RawDetectedCommands ( 'expl' , braces )
1444
1445 LPEG_cleaner.expl = Compute_LPEG_cleaner ( 'expl' , braces )
1446 local control_sequence = P "\\" * ( R "Az" + "_" + ":" + "@" ) ^ 1
1447 local ControlSequence = C ( control_sequence ) / analyze_cs
1448 local def_function
1449 = P [[\cs_]]
1450 * ( P "set" + "new")
1451 * ( P "_protected" ) ^ -1
1452 * P ":N" * ( P "p" ) ^ -1 * "n"
1453 local DefFunction =
1454 C ( def_function ) / analyze_cs
1455 * Space
1456 * Lc ( [[ {\PitonStyle{Name.Function}{ ]] )
1457 * ControlSequence -- Q ( ControlSequence ) ?
1458 * Lc "}}"
1459 local Word = Q ( ( 1 - S " \r" ) ^ 1 )
1460
1461 local Main =
1462 space ^ 0 * EOL
1463 + Space
1464 + Tab
1465 + Escape + EscapeMath
1466 + Beamer
1467 + Comment
1468 + DetectedCommands
1469 + DefFunction
1470 + ControlSequence
1471 + Word
1472 LPEG1.expl = Main ^ 0
1473
1474 LPEG2.expl =
1475 Ct (
1476 ( space ^ 0 * "\r" ) ^ -1
1477 * Lc [[ \__piton_begin_line: ]]
1478 * LeadingSpace ^ 0
1479 * ( space ^ 1 * -1 + Main ) ^ 0
1480 * -1
1481 * Lc [[ \__piton_end_line: ]]
1482 )
1483 end
1484 function piton.Parse ( language , code )
1485 piton.language = language
1486 local t = LPEG2[language] : match ( code )
1487 if not t then
1488 sprintL3 [[ \__piton_error_or_warning:n { SyntaxError } ]]
1489 return -- to exit in force the function
1490 end
1491 local left_stack = {}
1492 local right_stack = {}
1493 for _ , one_item in ipairs ( t ) do
1494 if one_item == "EOL" then
1495 for i = #right_stack, 1, -1 do
1496 tex.sprint ( right_stack[i] )
1497 end
1498 sprintL3 ( [[ \__piton_end_line: \__piton_par: \__piton_begin_line: ]] )
1499 tex.sprint ( table.concat ( left_stack ) )
1500 else
1501 if one_item[1] == "Open" then
1502 tex.sprint ( one_item[2] )
1503 table.insert ( left_stack , one_item[2] )
1504 table.insert ( right_stack , one_item[3] )
1505 else
1506 if one_item[1] == "Close" then
1507 tex.sprint ( right_stack[#right_stack] )
1508 left_stack[#left_stack] = nil
1509 right_stack[#right_stack] = nil
1510 else
1511 tex.tprint ( one_item )
1512 end
1513 end
1514 end
1515 end
1516 end
1517 local my_file_lines
1518 function my_file_lines ( filename )
1519 local f = io.open ( filename , 'rb' )
1520 local s = f : read ( '*a' )
1521 f : close ( )
1522 return ( s .. '\n' ) : gsub( '\r\n?' , '\n') : gmatch ( '(.-)\n' )
1523 end
1524 function piton.ReadFile ( name , first_line , last_line )
1525 local s = ''
1526 local i = 0
1527 for line in my_file_lines ( name ) do
1528 i = i + 1
1529 if i >= first_line then
1530 s = s .. '\r' .. line
1531 end
1532 if i >= last_line then break end
1533 end
1534 if s : sub ( 1 , 4 ) == string.char ( 13 , 239 , 187 , 191 ) then
1535 s = s : sub ( 5 , -1 )
1536 end
1537 sprintL3 ( [[ \tl_set:Nn \l__piton_listing_tl { ]])
1538 tex.sprint ( luatexbase.catcodetables.other , s )
1539 sprintL3 ( "}" )
1540 end
1541 function piton.RetrieveGobbleParse ( lang , n , splittable , code )
1542 local s
1543 s = ( ( P " " ^ 0 * "\r" ) ^ -1 * C ( P ( 1 ) ^ 0 ) * -1 ) : match ( code )
1544 piton.GobbleParse ( lang , n , splittable , s )
1545 end
1546 function piton.ParseBis ( lang , code )
1547 return piton.Parse ( lang , code : gsub ( '##' , '#' ) )
1548 end
1549 function piton.ParseTer ( lang , code )
1550 return piton.Parse
1551 (
1552 lang ,
1553 code : gsub ( [[\__piton_breakable_space: ]] , ' ' )
1554 )
1555 end
1556 local AutoGobbleLPEG =
1557 ( (
1558 P " " ^ 0 * "\r"
1559 +
1560 Ct ( C " " ^ 0 ) / table.getn
1561 * ( 1 - P " " ) * ( 1 - P "\r" ) ^ 0 * "\r"
1562 ) ^ 0
1563 * ( Ct ( C " " ^ 0 ) / table.getn
1564 * ( 1 - P " " ) * ( 1 - P "\r" ) ^ 0 ) ^ -1
1565 ) / math.min
1566 local TabsAutoGobbleLPEG =
1567 (
1568 (
1569 P "\t" ^ 0 * "\r"
1570 +
1571 Ct ( C "\t" ^ 0 ) / table.getn
1572 * ( 1 - P "\t" ) * ( 1 - P "\r" ) ^ 0 * "\r"
1573 ) ^ 0
1574 * ( Ct ( C "\t" ^ 0 ) / table.getn
1575 * ( 1 - P "\t" ) * ( 1 - P "\r" ) ^ 0 ) ^ -1
1576 ) / math.min
1577 local EnvGobbleLPEG =
1578 ( ( 1 - P "\r" ) ^ 0 * "\r" ) ^ 0
1579 * Ct ( C " " ^ 0 * -1 ) / table.getn
1580 function piton.Gobble ( n , code )
1581 if n == 0 then return
1582 code
1583 else
1584 if n == -1 then
1585 n = AutoGobbleLPEG : match ( code )
1586 if tonumber(n) then else n = 0 end
1587 else
1588 if n == -2 then
1589 n = EnvGobbleLPEG : match ( code )
1590 else
1591 if n == -3 then
1592 n = TabsAutoGobbleLPEG : match ( code )
1593 if tonumber(n) then else n = 0 end
1594 end
1595 end
1596 end
1597 if n == 0 then return
1598 code
1599 else return
1600 ( Ct (
1601 ( 1 - P "\r" ) ^ (-n) * C ( ( 1 - P "\r" ) ^ 0 )
1602 * ( C "\r" * ( 1 - P "\r" ) ^ (-n) * C ( ( 1 - P "\r" ) ^ 0 )
1603 ) ^ 0 )
1604 / table.concat
1605 ) : match ( code )
1606 end
1607 end
1608 end
1609 function piton.GobbleParse ( lang , n , splittable , code )
1610 piton.ComputeLinesStatus ( code , splittable )
1611 piton.last_code = piton.Gobble ( n , code )
1612 piton.last_language = lang
1613 piton.CountLines ( piton.last_code )
1614 piton.Parse ( lang , piton.last_code )
1615 piton.join_and_write ( )
1616 end
1617 function piton.join_and_write ( )
1618 if piton.join ~= '' then
1619 if not piton.join_files [ piton.join ] then
1620 piton.join_files [ piton.join ] = piton.get_last_code ( )
1621 else
1622 if piton.join_separation == '' then
1623 piton.join_files [ piton.join ] =
1624 piton.join_files [ piton.join ]
1625 .. "\r\n"
1626 .. piton.get_last_code ( )
1627 else
1628 piton.join_files [ piton.join ] =
1629 piton.join_files [ piton.join ]
1630 .. "\r\n"
1631 .. ( piton.join_separation : gsub ( '##' , '#' ) )
1632 .. "\r\n"
1633 .. piton.get_last_code ( )
1634 end
1635 end
1636 end
1637 if piton.write ~= '' then
1638 local file_name = ''
1639 if piton.path_write == '' then
1640 file_name = piton.write
1641 else
1642 local attr = lfs.attributes ( piton.path_write )
1643 if attr and attr.mode == "directory" then
1644 file_name = piton.path_write .. "/" .. piton.write
1645 else
1646 sprintL3 [[ \__piton_error_or_warning:n { InexistentDirectory } ]]
1647 end
1648 end
1649 if file_name ~= '' then
1650 if not piton.write_files [ file_name ] then
1651 piton.write_files [ file_name ] = piton.get_last_code ( )
1652 else
1653 piton.write_files [ file_name ] =
1654 piton.write_files [ file_name ] .. "\n" .. piton.get_last_code ( )
1655 end
1656 end
1657 end
1658 end
1659 function piton.GobbleParseNoPrint ( lang , n , code )
1660 piton.last_code = piton.Gobble ( n , code )
1661 piton.last_language = lang
1662 piton.join_and_write ( )
1663 end
1664 function piton.GobbleSplitParse ( lang , n , splittable , code )
1665 local chunks
1666 chunks =
1667 (
1668 Ct (
1669 (
1670 P " " ^ 0 * "\r"
1671 +
1672 C ( ( ( 1 - P "\r" ) ^ 1 * ( P "\r" + -1 )
1673 - ( P " " ^ 0 * ( P "\r" + -1 ) )
1674 ) ^ 1
1675 )
1676 ) ^ 0
1677 )
1678 ) : match ( piton.Gobble ( n , code ) )
1679 sprintL3 [[ \begingroup ]]
1680 sprintL3
1681 (
1682 [[ \PitonOptions { split-on-empty-lines = false, gobble = 0, ]]
1683 .. "language = " .. lang .. ","
1684 .. "splittable = " .. splittable .. "}"
1685 )
1686 for k , v in pairs ( chunks ) do
1687 if k > 1 then
1688 sprintL3 ( [[ \l__piton_split_separation_tl ]] )
1689 end
1690 tex.print
1691 (
1692 [[\begin{]] .. piton.env_used_by_split .. "}\r"
1693 .. v
1694 .. [[\end{]] .. piton.env_used_by_split .. "}\r"
1695 )
1696 end
1697 sprintL3 [[ \endgroup ]]
1698 end
1699 function piton.RetrieveGobbleSplitParse ( lang , n , splittable , code )
1700 local s
1701 s = ( ( P " " ^ 0 * "\r" ) ^ -1 * C ( P ( 1 ) ^ 0 ) * -1 ) : match ( code )
1702 piton.GobbleSplitParse ( lang , n , splittable , s )
1703 end
1704 piton.string_between_chunks =
1705 [[ \par \l__piton_split_separation_tl \mode_leave_vertical: ]]
1706 .. [[ \global \g__piton_line_int = 0 ]]
1707 function piton.get_last_code ( )
1708 return LPEG_cleaner[piton.last_language] : match ( piton.last_code )
1709 : gsub ( '\r\n?' , '\n' )
1710 end
1711 local CountBeamerEnvironments
1712 function CountBeamerEnvironments ( code ) return
1713 (
1714 Ct (
1715 (
1716 P "\\begin{" * beamerEnvironments * ( 1 - P "\r" ) ^ 0 * C "\r"
1717 +
1718 ( 1 - P "\r" ) ^ 0 * "\r"
1719 ) ^ 0
1720 * ( 1 - P "\r" ) ^ 0
1721 * -1
1722 ) / table.getn
1723 ) : match ( code )
1724 end
1725 function piton.CountLines ( code )
1726 local count
1727 count =
1728 ( Ct ( ( ( 1 - P "\r" ) ^ 0 * C "\r" ) ^ 0
1729 *
1730 (
1731 space ^ 0 * ( 1 - P "\r" - space ) * ( 1 - P "\r" ) ^ 0 * Cc "\r"
1732 + space ^ 0
1733 ) ^ -1
1734 * -1
1735 ) / table.getn
1736 ) : match ( code )
1737 if piton.beamer then
1738 count = count - 2 * CountBeamerEnvironments ( code )
1739 end
1740 sprintL3 ( [[ \int_gset:Nn \g__piton_nb_lines_int { ]] .. count .. "}" )
1741 end
1742 function piton.CountNonEmptyLines ( code )
1743 local count = 0
1744 count =
1745 ( Ct ( ( P " " ^ 0 * "\r"
1746 + ( 1 - P "\r" ) ^ 0 * C "\r" ) ^ 0
1747 * ( 1 - P "\r" ) ^ 0
1748 * -1
1749 ) / table.getn
1750 ) : match ( code )
1751 count = count + 1
1752 if piton.beamer then
1753 count = count - 2 * CountBeamerEnvironments ( code )
1754 end
1755 sprintL3
1756 ( [[ \int_set:Nn \l__piton_nb_non_empty_lines_int { ]] .. count .. "}" )
1757 end
1758 function piton.ComputeRange ( s , t , file_name )
1759 local first_line = -1
1760 local count = 0
1761 local last_found = false
1762 for line in io.lines ( file_name ) do
1763 if first_line == -1 then
1764 if line : sub ( 1 , #s ) == s then
1765 first_line = count
1766 end
1767 else
1768 if line : sub ( 1 , #t ) == t then
1769 last_found = true
1770 break
1771 end
1772 end
1773 count = count + 1
1774 end
1775 if first_line == -1 then
1776 sprintL3 [[ \__piton_error_or_warning:n { begin~marker~not~found } ]]
1777 else
1778 if not last_found then
1779 sprintL3 [[ \__piton_error_or_warning:n { end~marker~not~found } ]]
1780 end
1781 end
1782 sprintL3 (
1783 [[ \int_set:Nn \l__piton_first_line_int { ]] .. first_line .. ' + 2 }'
1784 .. [[ \global \l__piton_last_line_int = ]] .. count )
1785 end
1786 function piton.ComputeLinesStatus ( code , splittable )
1787 local lpeg_line_beamer
1788 if piton.beamer then
1789 lpeg_line_beamer =
1790 space ^ 0
1791 * P [[\begin{]] * beamerEnvironments * "}"
1792 * ( "<" * ( 1 - P ">" ) ^ 0 * ">" ) ^ -1
1793 +
1794 space ^ 0
1795 * P [[\end{]] * beamerEnvironments * "}"
1796 else
1797 lpeg_line_beamer = P ( false )
1798 end
1799 local lpeg_empty_lines =
1800 Ct (
1801 ( lpeg_line_beamer * "\r"
1802 +
1803 P " " ^ 0 * "\r" * Cc ( 0 )
1804 +
1805 ( 1 - P "\r" ) ^ 0 * "\r" * Cc ( 1 )
1806 ) ^ 0
1807 *
1808 ( lpeg_line_beamer + ( 1 - P "\r" ) ^ 1 * Cc ( 1 ) ) ^ -1
1809 )
1810 * -1
1811 local lpeg_all_lines =
1812 Ct (
1813 ( lpeg_line_beamer * "\r"
1814 +
1815 ( 1 - P "\r" ) ^ 0 * "\r" * Cc ( 1 )
1816 ) ^ 0
1817 *
1818 ( lpeg_line_beamer + ( 1 - P "\r" ) ^ 1 * Cc ( 1 ) ) ^ -1
1819 )
1820 * -1
1821 piton.empty_lines = lpeg_empty_lines : match ( code )
1822 local lines_status
1823 local s = splittable
1824 if splittable < 0 then s = - splittable end
1825 if splittable > 0 then
1826 lines_status = lpeg_all_lines : match ( code )
1827 else
1828 lines_status = lpeg_empty_lines : match ( code )
1829 for i , x in ipairs ( lines_status ) do
1830 if x == 0 then
1831 for j = 1 , s - 1 do
1832 if i + j > #lines_status then break end
1833 if lines_status[i+j] == 0 then break end
1834 lines_status[i+j] = 2
1835 end
1836 for j = 1 , s - 1 do
1837 if i - j == 1 then break end
1838 if lines_status[i-j-1] == 0 then break end
1839 lines_status[i-j-1] = 2
1840 end
1841 end
1842 end
1843 end
1844 for j = 1 , s - 1 do
1845 if j > #lines_status then break end
1846 if lines_status[j] == 0 then break end
1847 lines_status[j] = 2
1848 end
1849 for j = 1 , s - 1 do
1850 if #lines_status - j == 0 then break end
1851 if lines_status[#lines_status - j] == 0 then break end
1852 lines_status[#lines_status - j] = 2
1853 end
1854 piton.lines_status = lines_status
1855 end
1856 function piton.TranslateBeamerEnv ( code )
1857 local s
1858 s =
1859 (
1860 Ct (
1861 (
1862 space ^ 0
1863 * C (
1864 ( P "\\begin{" + "\\end{" )
1865 * beamerEnvironments * "}" * ( 1 - P "\r" ) ^ 0 * "\r"
1866 )
1867 + C ( ( 1 - P "\r" ) ^ 0 * "\r" )
1868 ) ^ 0
1869 *
1870 (
1871 (
1872 space ^ 0
1873 * C (
1874 ( P "\\begin{" + "\\end{" )
1875 * beamerEnvironments * "}" * ( 1 - P "\r" ) ^ 0 * -1
1876 )
1877 + C ( ( 1 - P "\r" ) ^ 1 ) * -1
1878 ) ^ -1
1879 )
1880 ) ^ -1 / table.concat
1881 ) : match ( code )
1882 sprintL3 ( [[ \tl_set:Nn \l__piton_listing_tl { ]] )
1883 tex.sprint ( luatexbase.catcodetables.other , s )
1884 sprintL3 ( "}" )
1885 end
1886 function piton.new_language ( lang , definition )
1887 lang = lang : lower ( )
1888 local alpha , digit = lpeg.alpha , lpeg.digit
1889 local extra_letters = { "@" , "_" , "$" } --
1890 function add_to_letter ( c )
1891 if c ~= " " then table.insert ( extra_letters , c ) end
1892 end
1893 function add_to_digit ( c )
1894 if c ~= " " then digit = digit + c end
1895 end
1896 local other = S ":_@+-*/<>!?;.()[]~^=#&\"\'\\$" --
1897 local extra_others = { }
1898 function add_to_other ( c )
1899 if c ~= " " then
1900 extra_others[c] = true
1901 other = other + P ( c )
1902 end
1903 end
1904 local def_table
1905 if ( S ", " ^ 0 * -1 ) : match ( definition ) then
1906 def_table = {}
1907 else
1908 local strict_braces =
1909 P { "E" ,
1910 E = ( "{" * V "F" * "}" + ( 1 - S ",{}" ) ) ^ 0 ,
1911 F = ( "{" * V "F" * "}" + ( 1 - S "{}" ) ) ^ 0
1912 }
1913 local cut_definition =
1914 P { "E" ,
1915 E = Ct ( V "F" * ( "," * V "F" ) ^ 0 ) ,
1916 F = Ct ( space ^ 0 * C ( alpha ^ 1 ) * space ^ 0
1917 * ( "=" * space ^ 0 * C ( strict_braces ) ) ^ -1 )
1918 }
1919 def_table = cut_definition : match ( definition )
1920 end
1921 local tex_braced_arg = "{" * C ( ( 1 - P "}" ) ^ 0 ) * "}"
1922 local tex_arg = tex_braced_arg + C ( 1 )
1923 local tex_option_arg = "[" * C ( ( 1 - P "]" ) ^ 0 ) * "]" + Cc ( nil )
1924 local args_for_tag
1925 = tex_option_arg
1926 * space ^ 0
1927 * tex_arg
1928 * space ^ 0
1929 * tex_arg
1930 local args_for_morekeywords
1931 = "[" * C ( ( 1 - P "]" ) ^ 0 ) * "]"
1932 * space ^ 0
1933 * tex_option_arg
1934 * space ^ 0
1935 * tex_arg
1936 * space ^ 0
1937 * ( tex_braced_arg + Cc ( nil ) )
1938 local args_for_moredelims
1939 = ( C ( P "*" ^ -2 ) + Cc ( nil ) ) * space ^ 0
1940 * args_for_morekeywords
1941 local args_for_morecomment
1942 = "[" * C ( ( 1 - P "]" ) ^ 0 ) * "]"
1943 * space ^ 0
1944 * tex_option_arg
1945 * space ^ 0
1946 * C ( P ( 1 ) ^ 0 * -1 )
1947 local sensitive = true
1948 local style_tag , left_tag , right_tag
1949 for _ , x in ipairs ( def_table ) do
1950 if x[1] == "sensitive" then
1951 if x[2] == nil or ( P "true" ) : match ( x[2] ) then
1952 sensitive = true
1953 else
1954 if ( P "false" + P "f" ) : match ( x[2] ) then sensitive = false end
1955 end
1956 end
1957 if x[1] == "alsodigit" then x[2] : gsub ( "." , add_to_digit ) end
1958 if x[1] == "alsoletter" then x[2] : gsub ( "." , add_to_letter ) end
1959 if x[1] == "alsoother" then x[2] : gsub ( "." , add_to_other ) end
1960 if x[1] == "tag" then
1961 style_tag , left_tag , right_tag = args_for_tag : match ( x[2] )
1962 style_tag = style_tag or [[\PitonStyle{Tag}]]
1963 end
1964 end
1965 local Number =
1966 K ( 'Number.Internal' ,
1967 ( digit ^ 1 * "." * # ( 1 - P "." ) * digit ^ 0
1968 + digit ^ 0 * "." * digit ^ 1
1969 + digit ^ 1 )
1970 * ( S "eE" * S "+-" ^ -1 * digit ^ 1 ) ^ -1
1971 + digit ^ 1
1972 )
1973 local string_extra_letters = ""
1974 for _ , x in ipairs ( extra_letters ) do
1975 if not ( extra_others[x] ) then
1976 string_extra_letters = string_extra_letters .. x
1977 end
1978 end
1979 local letter = alpha + S ( string_extra_letters )
1980 + P "â" + "à" + "ç" + "é" + "è" + "ê" + "ë" + "ï" + "î"
1981 + "ô" + "û" + "ü" + "Â" + "À" + "Ç" + "É" + "È" + "Ê" + "Ë"
1982 + "Ï" + "Î" + "Ô" + "Û" + "Ü"
1983 local alphanum = letter + digit
1984 local identifier = letter * alphanum ^ 0
1985 local Identifier = K ( 'Identifier.Internal' , identifier )
1986 local split_clist =
1987 P { "E" ,
1988 E = ( "[" * ( 1 - P "]" ) ^ 0 * "]" ) ^ -1
1989 * ( P "{" ) ^ 1
1990 * Ct ( V "F" * ( "," * V "F" ) ^ 0 )
1991 * ( P "}" ) ^ 1 * space ^ 0 ,
1992 F = space ^ 0 * C ( letter * alphanum ^ 0 + other ^ 1 ) * space ^ 0
1993 }
1994 local keyword_to_lpeg
1995 function keyword_to_lpeg ( name ) return
1996 Q ( Cmt (
1997 C ( identifier ) ,
1998 function ( _ , _ , a ) return a : upper ( ) == name : upper ( )
1999 end
2000 )
2001 )
2002 end
2003 local Keyword = P ( false )
2004 local PrefixedKeyword = P ( false )
2005 for _ , x in ipairs ( def_table )
2006 do if x[1] == "morekeywords"
2007 or x[1] == "otherkeywords"
2008 or x[1] == "moredirectives"
2009 or x[1] == "moretexcs"
2010 then
2011 local keywords = P ( false )
2012 local style = [[\PitonStyle{Keyword}]]
2013 if x[1] == "moredirectives" then style = [[\PitonStyle{Directive}]] end
2014 style = tex_option_arg : match ( x[2] ) or style
2015 local n = tonumber ( style )
2016 if n then
2017 if n > 1 then style = [[\PitonStyle{Keyword]] .. style .. "}" end
2018 end
2019 for _ , word in ipairs ( split_clist : match ( x[2] ) ) do
2020 if x[1] == "moretexcs" then
2021 keywords = Q ( [[\]] .. word ) + keywords
2022 else
2023 if sensitive
2024 then keywords = Q ( word ) + keywords
2025 else keywords = keyword_to_lpeg ( word ) + keywords
2026 end
2027 end
2028 end
2029 Keyword = Keyword +
2030 Lc ( "{" .. style .. "{" ) * keywords * Lc "}}"
2031 end
2032 if x[1] == "keywordsprefix" then
2033 local prefix = ( ( C ( 1 - P " " ) ^ 1 ) * P " " ^ 0 ) : match ( x[2] )
2034 PrefixedKeyword = PrefixedKeyword
2035 + K ( 'Keyword' , P ( prefix ) * ( letter ^ 1 + other ) )
2036 end
2037 end
2038 local long_string = P ( false )
2039 local Long_string = P ( false )
2040 local LongString = P (false )
2041 local central_pattern = P ( false )
2042 for _ , x in ipairs ( def_table ) do
2043 if x[1] == "morestring" then
2044 arg1 , arg2 , arg3 , arg4 = args_for_morekeywords : match ( x[2] )
2045 arg2 = arg2 or [[\PitonStyle{String.Long}]]
2046 if arg1 ~= "s" then
2047 arg4 = arg3
2048 end
2049 central_pattern = 1 - S ( " \r" .. arg4 )
2050 if arg1 : match "b" then
2051 central_pattern = P ( [[\]] .. arg3 ) + central_pattern
2052 end
2053 if arg1 : match "d" or arg1 == "m" then
2054 central_pattern = P ( arg3 .. arg3 ) + central_pattern
2055 end
2056 if arg1 == "m"
2057 then prefix = B ( 1 - letter - ")" - "]" )
2058 else prefix = P ( true )
2059 end
2060 long_string = long_string +
2061 prefix
2062 * arg3
2063 * ( space + central_pattern ) ^ 0
2064 * arg4
2065 local pattern =
2066 prefix
2067 * Q ( arg3 )
2068 * ( SpaceInString + Q ( central_pattern ^ 1 ) + EOL ) ^ 0
2069 * Q ( arg4 )
2070 Long_string = Long_string + pattern
2071 LongString = LongString +
2072 Ct ( Cc "Open" * Cc ( "{" .. arg2 .. "{" ) * Cc "}}" )
2073 * pattern
2074 * Ct ( Cc "Close" )
2075 end
2076 end
2077 local braces = Compute_braces ( long_string )
2078 if piton.beamer then Beamer = Compute_Beamer ( lang , braces ) end
2079
2080 DetectedCommands =
2081 Compute_DetectedCommands ( lang , braces )
2082 + Compute_RawDetectedCommands ( lang , braces )
2083
2084 LPEG_cleaner[lang] = Compute_LPEG_cleaner ( lang , braces )
2085 local sComment = P ( false )
2086 local lComment = P ( false )
2087 local fComment = P ( false )
2088 local fComment = P ( false )
2089
2090 for _ , x in ipairs ( def_table ) do
2091 if x[1] == "morecomment" then
2092 local arg1 , arg2 , other_args = args_for_morecomment : match ( x[2] )
2093 arg2 = arg2 or [[\PitonStyle{Comment}]]
2094 if arg1 : match "i" then arg2 = [[\PitonStyle{Discard}]] end
2095 if arg1 : match "l" or arg1 : match "f" then
2096 local arg3 = ( tex_braced_arg + C ( P ( 1 ) ^ 0 * -1 ) )
2097 : match ( other_args )
2098 if arg3 == [[\#]] then arg3 = "#" end -- mandatory
2099 if arg3 == [[\%]] then arg3 = "%" end -- mandatory
2100 local MyLPEG =
2101 Ct ( Cc "Open"
2102 * Cc ( "{" .. arg2 .. [[{\PitonSpaceSubstitute{]] )
2103 * Cc "}}}"
2104 )
2105 * Q ( arg3 )
2106 * ( CommentMath + Q ( ( 1 - S "$\r" ) ^ 1 ) ) ^ 0 -- $ noqa
2107 * Ct ( Cc "Close" )
2108 * ( EOL + -1 )
2109 if arg1 : match "l" then
2110 lComment = lComment + MyLPEG
2111 else
2112 fComment = fComment + MyLPEG
2113 end
2114 else
2115 local arg3 , arg4 =
2116 ( tex_arg * space ^ 0 * tex_arg ) : match ( other_args )
2117 if arg1 : match "s" then
2118 sComment = sComment +
2119 Ct ( Cc "Open" * Cc ( "{" .. arg2 .. "{" ) * Cc "}}" )
2120 * Q ( arg3 )
2121 * (
2122 CommentMath
2123 + Q ( ( 1 - P ( arg4 ) - S "$\r" ) ^ 1 ) -- $
2124 + EOL
2125 ) ^ 0
2126 * Q ( arg4 )
2127 * Ct ( Cc "Close" )
2128 end
2129 if arg1 : match "n" then
2130 sComment = sComment +
2131 Ct ( Cc "Open" * Cc ( "{" .. arg2 .. "{" ) * Cc "}}" )
2132 * P { "A" ,
2133 A = Q ( arg3 )
2134 * ( V "A"
2135 + Q ( ( 1 - P ( arg3 ) - P ( arg4 )
2136 - S "\r$\"" ) ^ 1 ) -- $
2137 + long_string
2138 + "$" -- $
2139 * K ( 'Comment.Math' , ( 1 - S "$\r" ) ^ 1 ) --$
2140 * "$" -- $
2141 + EOL
2142 ) ^ 0
2143 * Q ( arg4 )
2144 }
2145 * Ct ( Cc "Close" )
2146 end
2147 end
2148 end
2149 if x[1] == "moredelim" then
2150 local arg1 , arg2 , arg3 , arg4 , arg5
2151 = args_for_moredelims : match ( x[2] )
2152 local MyFun = Q
2153 if arg1 == "*" or arg1 == "**" then
2154 function MyFun ( x )
2155 if x ~= '' then return
2156 LPEG1[lang] : match ( x )
2157 end
2158 end
2159 end
2160 local left_delim
2161 if arg2 : match "i" then
2162 left_delim = P ( arg4 )
2163 else
2164 left_delim = Q ( arg4 )
2165 end
2166 if arg2 : match "l" then
2167 sComment = sComment +
2168 Ct ( Cc "Open" * Cc ( "{" .. arg3 .. "{" ) * Cc "}}" )
2169 * left_delim
2170 * ( MyFun ( ( 1 - P "\r" ) ^ 1 ) ) ^ 0
2171 * Ct ( Cc "Close" )
2172 * ( EOL + -1 )
2173 end
2174 if arg2 : match "s" then
2175 local right_delim
2176 if arg2 : match "i" then
2177 right_delim = P ( arg5 )
2178 else
2179 right_delim = Q ( arg5 )
2180 end
2181 sComment = sComment +
2182 Ct ( Cc "Open" * Cc ( "{" .. arg3 .. "{" ) * Cc "}}" )
2183 * left_delim
2184 * ( MyFun ( ( 1 - P ( arg5 ) - "\r" ) ^ 1 ) + EOL ) ^ 0
2185 * right_delim
2186 * Ct ( Cc "Close" )
2187 end
2188 end
2189 end
2190
2191 local Delim = Q ( S "{[()]}" )
2192 local Punct = Q ( S "=,:;!\\'\"" )
2193 local EOL =
2194 P "\r"
2195 *
2196 (
2197 space ^ 0 * -1
2198 +
2199 Cc "EOL"
2200 )
2201 * ( ( fComment + lComment ) ^ 0 * LeadingSpace ^ 0 * # ( 1 - S " \r" ) ) ^ -1
2202 local Main =
2203 space ^ 0 * EOL * ( fComment + lComment ) ^ 0
2204 + Space
2205 + Tab
2206 + Escape + EscapeMath
2207 + CommentLaTeX
2208 + Beamer
2209 + DetectedCommands
2210 + sComment
2211 + lComment
2212 + LongString
2213 + Delim
2214 + PrefixedKeyword
2215 + Keyword * ( -1 + # ( 1 - alphanum ) )
2216 + Punct
2217 + K ( 'Identifier.Internal' , letter * alphanum ^ 0 )
2218 + Number
2219 + Word
2220 LPEG1[lang] = Main ^ 0
2221 LPEG2[lang] =
2222 Ct (
2223 ( space ^ 0 * P "\r" ) ^ -1
2224 * Lc [[ \__piton_begin_line: ]]
2225 * ( fComment + lComment ) ^ 0 -- 2026-05-12
2226 * LeadingSpace ^ 0
2227 * ( space ^ 1 * -1 + Main ) ^ 0
2228 * -1
2229 * Lc [[ \__piton_end_line: ]]
2230 )
2231 if left_tag then
2232 local Tag = Ct ( Cc "Open" * Cc ( "{" .. style_tag .. "{" ) * Cc "}}" )
2233 * Q ( left_tag * other ^ 0 ) -- $
2234 * ( ( ( 1 - P ( right_tag ) ) ^ 0 )
2235 / ( function ( x ) return LPEG0[lang] : match ( x ) end ) )
2236 * Q ( right_tag )
2237 * Ct ( Cc "Close" )
2238 MainWithoutTag
2239 = space ^ 1 * -1
2240 + space ^ 0 * EOL
2241 + Space
2242 + Tab
2243 + Escape + EscapeMath
2244 + CommentLaTeX
2245 + Beamer
2246 + DetectedCommands
2247 + sComment
2248 + Delim
2249 + LongString
2250 + PrefixedKeyword
2251 + Keyword * ( -1 + # ( 1 - alphanum ) )
2252 + Punct
2253 + K ( 'Identifier.Internal' , letter * alphanum ^ 0 )
2254 + Number
2255 + Word
2256 LPEG0[lang] = MainWithoutTag ^ 0
2257 local LPEGaux = Tab + Escape + EscapeMath + CommentLaTeX
2258 + Beamer + DetectedCommands + sComment + Tag
2259 MainWithTag
2260 = space ^ 1 * -1
2261 + space ^ 0 * EOL
2262 + Space
2263 + LPEGaux
2264 + Q ( ( 1 - EOL - LPEGaux ) ^ 1 )
2265 LPEG1[lang] = MainWithTag ^ 0
2266 LPEG2[lang] =
2267 Ct (
2268 ( space ^ 0 * P "\r" ) ^ -1
2269 * Lc [[ \__piton_begin_line: ]]
2270 * Beamer
2271 * LeadingSpace ^ 0
2272 * LPEG1[lang]
2273 * -1
2274 * Lc [[ \__piton_end_line: ]]
2275 )
2276 end
2277 end
2278 function piton.write_files_now ( )
2279 for file_name , file_content in pairs ( piton.write_files ) do
2280 local file = io.open ( file_name , "w" )
2281 if file then
2282 file : write ( file_content )
2283 file : close ( )
2284 else
2285 sprintL3
2286 ( [[ \__piton_error_or_warning:nn { FileError } { ]] .. file_name .. "}" )
2287 end
2288 end
2289 end
2290 function piton.utf16 ( str )
2291 local hex = { "FEFF" } -- BOM UTF-16BE
2292 for _, codepoint in utf8.codes(str) do
2293 table.insert(hex, string.format("%04X", codepoint))
2294 end
2295 return table.concat(hex)
2296 end