diff options
author | Karl Berry <karl@freefriends.org> | 2022-11-09 21:08:15 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2022-11-09 21:08:15 +0000 |
commit | c949ca4c9835a7dbde888cb4a1760cf4c0691b5b (patch) | |
tree | 1262725dbc8d3f3f6f5328d47a7474c6f58184d9 /Master/texmf-dist/tex/lualatex/piton | |
parent | d6a12751a6fbdefb749e6a31cc8934763b18ec7f (diff) |
piton (9nov22)
git-svn-id: svn://tug.org/texlive/trunk@64979 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/tex/lualatex/piton')
-rw-r--r-- | Master/texmf-dist/tex/lualatex/piton/piton.sty | 373 |
1 files changed, 263 insertions, 110 deletions
diff --git a/Master/texmf-dist/tex/lualatex/piton/piton.sty b/Master/texmf-dist/tex/lualatex/piton/piton.sty index 60f6f622742..ccfa3c3eb26 100644 --- a/Master/texmf-dist/tex/lualatex/piton/piton.sty +++ b/Master/texmf-dist/tex/lualatex/piton/piton.sty @@ -18,8 +18,8 @@ %% and version 1.3 or later is part of all distributions of %% LaTeX version 2005/12/01 or later. %% -\def\myfileversion{0.9} -\def\myfiledate{2022/10/31} +\def\myfileversion{0.95} +\def\myfiledate{2022/11/09} @@ -36,28 +36,33 @@ \RequirePackage { luatexbase } \bool_new:N \c__piton_footnotehyper_bool \bool_new:N \c__piton_footnote_bool +\bool_new:N \c__piton_math_comments_bool \keys_define:nn { piton / package } { footnote .bool_set:N = \c__piton_footnote_bool , footnotehyper .bool_set:N = \c__piton_footnotehyper_bool , escape-inside .tl_set:N = \c__piton_escape_inside_tl , escape-inside .initial:n = , + comment-latex .code:n = { \lua_now:n { comment_latex = "#1" } } , + comment-latex .value_required:n = true , + math-comments .bool_set:N = \c__piton_math_comments_bool , + math-comments .default:n = true , unknown .code:n = \msg_error:nn { piton } { unknown~key~for~package } } \msg_new:nnn { piton } { unknown~key~for~package } { Unknown~key.\\ You~have~used~the~key~'\l_keys_key_str'~but~the~only~keys~available~here~ - are~'escape-inside',~'footnote'~and~'footnotehyper'.~Other~keys~are~ - available~in~\token_to_str:N \PitonOptions.\\ + are~'comment-latex',~'escape-inside',~'footnote',~'footnotehyper'~and~ + 'math-comments'.~Other~keys~are~available~in~\token_to_str:N \PitonOptions.\\ That~key~will~be~ignored. } \ProcessKeysOptions { piton / package } \begingroup \cs_new_protected:Npn \__piton_set_escape_char:nn #1 #2 { - \lua_now:n { begin_escape = "#1" } - \lua_now:n { end_escape = "#2" } + \lua_now:n { piton_begin_escape = "#1" } + \lua_now:n { piton_end_escape = "#2" } } \cs_generate_variant:Nn \__piton_set_escape_char:nn { x x } \__piton_set_escape_char:xx @@ -117,6 +122,7 @@ } } \int_new:N \l__piton_nb_lines_int +\int_new:N \l__piton_nb_non_empty_lines_int \int_new:N \g__piton_line_int \tl_new:N \g__piton_aux_tl \int_new:N \l__piton_splittable_int @@ -135,23 +141,9 @@ { \tl_put_right:Nn \l__piton_tab_tl { ~ } } } \__piton_set_tab_tl:n { 4 } -\cs_new_protected:Npn \__piton_newline: - { - \int_gincr:N \g__piton_line_int - \int_compare:nNnT \g__piton_line_int > { \l__piton_splittable_int - 1 } - { - \int_compare:nNnT - { \l__piton_nb_lines_int - \g__piton_line_int } > \l__piton_splittable_int - { - \egroup - \bool_if:NT \c__piton_footnote_bool { \end { savenotes } } - \newline - \bool_if:NT \c__piton_footnote_bool { \begin { savenotes } } - \vtop \bgroup - } - } - } \int_new:N \l__piton_gobble_int +\tl_new:N \l__piton_space_tl +\tl_set:Nn \l__piton_space_tl { ~ } \cs_set_protected:Npn \__piton_begin_line: #1 \__piton_end_line: { \bool_if:NTF \l__piton_slim_bool @@ -199,6 +191,22 @@ } \vspace { - 2.5 pt } } +\cs_new_protected:Npn \__piton_newline: + { + \int_gincr:N \g__piton_line_int + \int_compare:nNnT \g__piton_line_int > { \l__piton_splittable_int - 1 } + { + \int_compare:nNnT + { \l__piton_nb_lines_int - \g__piton_line_int } > \l__piton_splittable_int + { + \egroup + \bool_if:NT \c__piton_footnote_bool { \end { savenotes } } + \newline + \bool_if:NT \c__piton_footnote_bool { \begin { savenotes } } + \vtop \bgroup + } + } + } \bool_new:N \l__piton_line_numbers_bool \bool_new:N \l__piton_all_line_numbers_bool \bool_new:N \l__piton_resume_bool @@ -224,10 +232,15 @@ background-color .value_required:n = true , slim .bool_set:N = \l__piton_slim_bool , slim .default:n = true , - left-margin .dim_set:N = \l__piton_left_margin_dim , + left-margin .code:n = + \str_if_eq:nnTF { #1 } { auto } + { \dim_set:Nn \l__piton_left_margin_dim { -1cm } } + { \dim_set:Nn \l__piton_left_margin_dim { #1 } } , left-margin .value_required:n = true , tab-size .code:n = \__piton_set_tab_tl:n { #1 } , tab-size .value_required:n = true , + show-spaces .code:n = \tl_set:Nn \l__piton_space_tl { ␣ } , % U+2423 + show-spaces .value_forbidden:n = true , unknown .code:n = \msg_error:nn { piton } { Unknown~key~for~PitonOptions } } @@ -236,7 +249,7 @@ Unknown~key. \\ The~key~'\l_keys_key_str'~is~unknown~for~\token_to_str:N \PitonOptions.~The~ available~keys~are:~all-line-numbers,~auto-gobble,~env-gobble,~gobble,~ - left-margin,~line-numbers,~resume,~slim~splittable~and~tab-size.\\ + left-margin,~line-numbers,~resume,~show-spaces,~slim,~splittable~and~tab-size.\\ If~you~go~on,~that~key~will~be~ignored. } \NewDocumentCommand \PitonOptions { } { \keys_set:nn { PitonOptions } } @@ -284,7 +297,7 @@ \ttfamily \cs_set_protected:Npn \__piton_begin_line: { } \cs_set_protected:Npn \__piton_end_line: { } - \lua_now:n { Parse(token.scan_argument()) } { #1 } + \lua_now:n { piton.Parse(token.scan_argument()) } { #1 } \group_end: } \cs_new_protected:Npn \__piton_piton:n #1 @@ -292,15 +305,14 @@ \group_begin: \cs_set_protected:Npn \__piton_begin_line: { } \cs_set_protected:Npn \__piton_end_line: { } - \lua_now:e { Parse(token.scan_argument()) } { #1 } + \lua_now:n { piton.Parse(token.scan_argument()) } { #1 } \group_end: } \cs_new:Npn \__piton_pre_env: { \int_gincr:N \g__piton_env_int \tl_gclear:N \g__piton_aux_tl - \tl_if_exist:cT { c__piton _ \int_use:N \g__piton_env_int _ tl } - { \use:c { c__piton _ \int_use:N \g__piton_env_int _ tl } } + \cs_if_exist_use:c { c__piton _ \int_use:N \g__piton_env_int _ tl } \dim_compare:nNnT \l__piton_width_on_aux_dim = \c_zero_dim { \dim_set_eq:NN \l__piton_width_on_aux_dim \linewidth } \bool_if:NF \l__piton_resume_bool { \int_gzero:N \g__piton_visual_line_int } @@ -309,16 +321,58 @@ \dim_zero:N \parindent \dim_zero:N \lineskip } -\NewDocumentCommand { \PitonInputFile } { m } +\keys_define:nn { PitonInputFile } + { + first-line .int_set:N = \l__piton_first_line_int , + first-line .value_required:n = true , + last-line .int_set:N = \l__piton_last_line_int , + last-line .value_required:n = true , + } +\NewDocumentCommand { \PitonInputFile } { O { } m } { \group_begin: + \int_zero_new:N \l__piton_first_line_int + \int_zero_new:N \l__piton_last_line_int + \int_set_eq:NN \l__piton_last_line_int \c_max_int + \keys_set:nn { PitonInputFile } { #1 } \__piton_pre_env: \mode_if_vertical:TF \mode_leave_vertical: \newline - \lua_now:n { CountLinesFile(token.scan_argument()) } { #1 } + \lua_now:n { piton.CountLinesFile(token.scan_argument()) } { #2 } + \dim_compare:nNnT \l__piton_left_margin_dim < \c_zero_dim + { + \bool_if:NT \l__piton_line_numbers_bool + { + \hbox_set:Nn \l_tmpa_box + { + \footnotesize + \bool_if:NTF \l__piton_all_line_numbers_bool + { + \int_to_arabic:n + { \g__piton_visual_line_int + \l__piton_nb_lines_int } + } + { + \lua_now:n + { piton.CountNonEmptyLinesFile(token.scan_argument()) } + { #2 } + \int_to_arabic:n + { + \g__piton_visual_line_int + + \l__piton_nb_non_empty_lines_int + } + } + } + \dim_set:Nn \l__piton_left_margin_dim { \box_wd:N \l_tmpa_box + 0.5em } + } + } \ttfamily \bool_if:NT \c__piton_footnote_bool { \begin { savenotes } } \vtop \bgroup - \lua_now:n { ParseFile(token.scan_argument()) } { #1 } + \lua_now:e + { piton.ParseFile(token.scan_argument(), + \int_use:N \l__piton_first_line_int , + \int_use:N \l__piton_last_line_int ) + } + { #2 } \egroup \bool_if:NT \c__piton_footnote_bool { \end { savenotes } } \__piton_width_to_aux: @@ -338,13 +392,44 @@ { \group_end: \mode_if_vertical:TF \mode_leave_vertical: \newline - \lua_now:n { CountLines(token.scan_argument()) } { ##1 } + \lua_now:n { piton.CountLines(token.scan_argument()) } { ##1 } + \dim_compare:nNnT \l__piton_left_margin_dim < \c_zero_dim + { + \bool_if:NT \l__piton_line_numbers_bool + { + \bool_if:NTF \l__piton_all_line_numbers_bool + { + \hbox_set:Nn \l_tmpa_box + { + \footnotesize + \int_to_arabic:n + { \g__piton_visual_line_int + \l__piton_nb_lines_int } + } + } + { + \lua_now:n + { piton.CountNonEmptyLines(token.scan_argument()) } + { ##1 } + \hbox_set:Nn \l_tmpa_box + { + \footnotesize + \int_to_arabic:n + { + \g__piton_visual_line_int + + \l__piton_nb_non_empty_lines_int + } + } + } + \dim_set:Nn \l__piton_left_margin_dim + { \box_wd:N \l_tmpa_box + 0.5 em } + } + } \ttfamily \bool_if:NT \c__piton_footnote_bool { \begin { savenotes } } \vtop \bgroup \lua_now:e { - GobbleParse + piton.GobbleParse ( \int_use:N \l__piton_gobble_int , token.scan_argument() ) } { ##1 } @@ -478,6 +563,8 @@ Interpol.Inside = \color{black}\__piton_piton:n , Post.Function = \__piton_piton:n , } +\bool_if:NT \c__piton_math_comments_bool + { \SetPitonStyle { Comment.Math } } \AddToHook { env / piton / begin } { \msg_fatal:nn { piton } { No~environment~piton } } @@ -491,7 +578,11 @@ \ExplSyntaxOff \RequirePackage{luacode} \begin{luacode*} -local P, S, V, C, Ct, Cc, Cf = lpeg.P, lpeg.S, lpeg.V, lpeg.C, lpeg.Ct, lpeg.Cc, lpeg.Cf +piton = piton or { } +if piton.comment_latex == nil then piton.comment_latex = ">" end +piton.comment_latex = "#" .. piton.comment_latex +local P, S, V, C, Ct, Cc = lpeg.P, lpeg.S, lpeg.V, lpeg.C, lpeg.Ct, lpeg.Cc +local Cf, Cs = lpeg.Cf, lpeg.Cs local function Q(pattern) return Ct ( Cc ( luatexbase.catcodetables.CatcodeTableOther ) * C ( pattern ) ) end @@ -502,15 +593,20 @@ local function Lc(string) return Cc ( { luatexbase.catcodetables.expl , string } ) end local function K(pattern, style) + if style + then return Lc ( "{\\PitonStyle{" .. style .. "}{" ) * Q ( pattern ) * Lc ( "}}" ) + else + return Q ( pattern ) + end end local Escape = - P(begin_escape) - * L ( ( 1 - P(end_escape) ) ^ 1 ) - * P(end_escape) + P(piton_begin_escape) + * L ( ( 1 - P(piton_end_escape) ) ^ 1 ) + * P(piton_end_escape) lpeg.locale(lpeg) local alpha, digit, space = lpeg.alpha, lpeg.digit, lpeg.space local letter = alpha + P "_" @@ -520,7 +616,7 @@ local letter = alpha + P "_" local alphanum = letter + digit local identifier = letter * alphanum ^ 0 -local Identifier = Q ( identifier ) +local Identifier = K ( identifier ) local Number = K ( ( digit^1 * P "." * digit^0 + digit^0 * P "." * digit^1 + digit^1 ) @@ -528,16 +624,17 @@ local Number = + digit^1 , 'Number' ) -if begin_escape ~= '' -then Word = Q ( ( ( 1 - space - P(begin_escape) - P(end_escape) ) +local Word +if piton_begin_escape ~= '' +then Word = K ( ( ( 1 - space - P(piton_begin_escape) - P(piton_end_escape) ) - S "'\"\r[()]" - digit ) ^ 1 ) -else Word = Q ( ( ( 1 - space ) - S "'\"\r[()]" - digit ) ^ 1 ) +else Word = K ( ( ( 1 - space ) - S "'\"\r[()]" - digit ) ^ 1 ) end -local Space = Q ( ( space - P "\r" ) ^ 1 ) +local Space = K ( ( space - P "\r" ) ^ 1 ) -local SkipSpace = Q ( ( space - P "\r" ) ^ 0 ) +local SkipSpace = K ( ( space - P "\r" ) ^ 0 ) -local Punct = Q ( S ".,:;!" ) +local Punct = K ( S ".,:;!" ) local Tab = P "\t" * Lc ( '\\l__piton_tab_tl' ) local EOL = P "\r" @@ -547,10 +644,13 @@ local EOL = + Lc ( '\\__piton_end_line: \\__piton_newline: \\__piton_begin_line:' ) ) -local Delim = Q ( S "[()]" ) - +local Delim = K ( S "[()]" ) local Operator = - K ( P "!=" + P "==" + P "<<" + P ">>" + S "-~+/*%=<>&.@|" , 'Operator') + K ( P "!=" + P "<>" + P "==" + P "<<" + P ">>" + P "<=" + P ">=" + P ":=" + + P "//" + P "**" + S "-~+/*%=<>&.@|" + , + 'Operator' + ) local OperatorWord = K ( P "in" + P "is" + P "and" + P "or" + P "not" , 'Operator.Word') @@ -602,9 +702,9 @@ local Exception = + P "StopAsyncIteration" + P "ModuleNotFoundError" + P "RecursionError" , 'Exception' ) -local RaiseException = K ( P "raise" , 'Keyword' ) * SkipSpace * Exception * Q ( P "(" ) +local RaiseException = K ( P "raise" , 'Keyword' ) * SkipSpace * Exception * K ( P "(" ) -local ExceptionInConsole = Exception * Q ( ( 1 - P "\r" ) ^ 0 ) * EOL +local ExceptionInConsole = Exception * K ( ( 1 - P "\r" ) ^ 0 ) * EOL local Decorator = K ( P "@" * letter^1 , 'Name.Decorator' ) local DefClass = K ( P "class" , 'Keyword' ) * Space * K ( identifier , 'Name.Class' ) @@ -617,7 +717,7 @@ local ImportAs = * ( ( Space * K ( P "as" , 'Keyword' ) * Space * K ( identifier , 'Name.Namespace' ) ) + - ( SkipSpace * Q ( P "," ) * SkipSpace * K ( identifier , 'Name.Namespace' ) ) ^ 0 + ( SkipSpace * K ( P "," ) * SkipSpace * K ( identifier , 'Name.Namespace' ) ) ^ 0 ) local FromImport = K ( P "from" , 'Keyword' ) @@ -626,58 +726,79 @@ local FromImport = local SingleShortInterpol = K ( P "{" , 'String.Interpol') * K ( ( 1 - S "}':" ) ^ 0 , 'Interpol.Inside' ) - * Q ( P ":" * (1 - S "}:'") ^ 0 ) ^ -1 + * K ( P ":" * (1 - S "}:'") ^ 0 ) ^ -1 * K ( P "}" , 'String.Interpol' ) local DoubleShortInterpol = K ( P "{" , 'String.Interpol' ) * K ( ( 1 - S "}\":" ) ^ 0 , 'Interpol.Inside' ) - * ( K ( P ":" , 'String.Interpol' ) * Q ( (1 - S "}:\"") ^ 0 ) ) ^ -1 + * ( K ( P ":" , 'String.Interpol' ) * K ( (1 - S "}:\"") ^ 0 ) ) ^ -1 * K ( P "}" , 'String.Interpol' ) local SingleLongInterpol = K ( P "{" , 'String.Interpol' ) * K ( ( 1 - S "}:\r" - P "'''" ) ^ 0 , 'Interpol.Inside' ) - * Q ( P ":" * (1 - S "}:\r" - P "'''" ) ^ 0 ) ^ -1 + * K ( P ":" * (1 - S "}:\r" - P "'''" ) ^ 0 ) ^ -1 * K ( P "}" , 'String.Interpol' ) local DoubleLongInterpol = K ( P "{" , 'String.Interpol' ) * K ( ( 1 - S "}:\r" - P "\"\"\"" ) ^ 0 , 'Interpol.Inside' ) - * Q ( P ":" * (1 - S "}:\r" - P "\"\"\"" ) ^ 0 ) ^ -1 + * K ( P ":" * (1 - S "}:\r" - P "\"\"\"" ) ^ 0 ) ^ -1 * K ( P "}" , 'String.Interpol' ) +local VisualSpace = P " " * Lc "\\l__piton_space_tl" local SingleShortPureString = - Q ( ( P "\\'" + P "{{" + P "}}" + 1 - S "{}'" ) ^ 1 ) + ( K ( ( P "\\'" + P "{{" + P "}}" + 1 - S " {}'" ) ^ 1 ) + VisualSpace ) ^ 1 local DoubleShortPureString = - Q ( ( P "\\\"" + P "{{" + P "}}" + 1 - S "{}\"" ) ^ 1 ) + ( K ( ( P "\\\"" + P "{{" + P "}}" + 1 - S " {}\"" ) ^ 1 ) + VisualSpace ) ^ 1 local SingleLongPureString = - Q ( ( 1 - P "'''" - S "{}'\r" ) ^ 1 ) + K ( ( 1 - P "'''" - S "{}'\r" ) ^ 1 ) local DoubleLongPureString = - Q ( ( 1 - P "\"\"\"" - S "{}\"\r" ) ^ 1 ) + K ( ( 1 - P "\"\"\"" - S " {}\"\r" ) ^ 1 ) +local PercentInterpol = + K ( P "%" + * ( P "(" * alphanum ^ 1 * P ")" ) ^ -1 + * ( S "-#0 +" ) ^ 0 + * ( digit ^ 1 + P "*" ) ^ -1 + * ( P "." * ( digit ^ 1 + P "*" ) ) ^ -1 + * ( S "HlL" ) ^ -1 + * S "sdfFeExXorgiGauc%" , + 'String.Interpol' + ) local SingleShortString = Lc ( "{\\PitonStyle{String.Short}{" ) * ( - Q ( P "f'" + P "F'" ) + K ( P "f'" + P "F'" ) * ( SingleShortInterpol + SingleShortPureString ) ^ 0 - * Q ( P "'" ) + * K ( P "'" ) + - Q ( ( P "'" + P "r'" + P "R'" ) - * ( P "\\'" + 1 - S "'\r" ) ^ 0 * P "'" ) + K ( P "'" + P "r'" + P "R'" ) + * ( K ( ( P "\\'" + 1 - S " '\r%" ) ^ 1 ) + + VisualSpace + + PercentInterpol + + K ( P "%" ) + ) ^ 0 + * K ( P "'" ) ) * Lc ( "}}" ) local DoubleShortString = Lc ( "{\\PitonStyle{String.Short}{" ) * ( - Q ( P "f\"" + P "F\"" ) + K ( P "f\"" + P "F\"" ) * ( DoubleShortInterpol + DoubleShortPureString ) ^ 0 - * Q ( P "\"" ) + * K ( P "\"" ) + - Q ( ( P "\"" + P "r\"" + P "R\"" ) - * ( P "\\\"" + 1 - S "\"\r" ) ^ 0 * P "\"" ) + K ( P "\"" + P "r\"" + P "R\"" ) + * ( K ( ( P "\\\"" + 1 - S " \"\r%" ) ^ 1 ) + + VisualSpace + + PercentInterpol + + K ( P "%" ) + ) ^ 0 + * K ( P "\"" ) ) * Lc ( "}}" ) @@ -685,7 +806,7 @@ local ShortString = SingleShortString + DoubleShortString local SingleLongString = Lc "{\\PitonStyle{String.Long}{" * ( - Q ( S "fF" * P "'''" ) + K ( S "fF" * P "'''" ) * ( SingleLongInterpol + SingleLongPureString ) ^ 0 * Lc "}}" * ( @@ -699,25 +820,25 @@ local SingleLongString = * Lc "{\\PitonStyle{String.Long}{" * ( SingleLongInterpol + SingleLongPureString ) ^ 0 + - Q ( ( S "rR" ) ^ -1 * P "'''" + K ( ( S "rR" ) ^ -1 * P "'''" * ( 1 - P "'''" - P "\r" ) ^ 0 ) * Lc "}}" * ( Lc "{\\PitonStyle{String.Long}{" - * Q ( ( 1 - P "'''" - P "\r" ) ^ 0 ) + * K ( ( 1 - P "'''" - P "\r" ) ^ 0 ) * Lc "}}" * EOL ) ^ 0 * Lc "{\\PitonStyle{String.Long}{" - * Q ( ( 1 - P "'''" - P "\r" ) ^ 0 ) + * K ( ( 1 - P "'''" - P "\r" ) ^ 0 ) ) - * Q ( P "'''" ) + * K ( P "'''" ) * Lc "}}" local DoubleLongString = Lc "{\\PitonStyle{String.Long}{" * ( - Q ( S "fF" * P "\"\"\"" ) + K ( S "fF" * P "\"\"\"" ) * ( DoubleLongInterpol + DoubleLongPureString ) ^ 0 * Lc "}}" * ( @@ -731,19 +852,19 @@ local DoubleLongString = * Lc "{\\PitonStyle{String.Long}{" * ( DoubleLongInterpol + DoubleLongPureString ) ^ 0 + - Q ( ( S "rR" ) ^ -1 * P "\"\"\"" + K ( ( S "rR" ) ^ -1 * P "\"\"\"" * ( 1 - P "\"\"\"" - P "\r" ) ^ 0 ) * Lc "}}" * ( Lc "{\\PitonStyle{String.Long}{" - * Q ( ( 1 - P "\"\"\"" - P "\r" ) ^ 0 ) + * K ( ( 1 - P "\"\"\"" - P "\r" ) ^ 0 ) * Lc "}}" * EOL ) ^ 0 * Lc "{\\PitonStyle{String.Long}{" - * Q ( ( 1 - P "\"\"\"" - P "\r" ) ^ 0 ) + * K ( ( 1 - P "\"\"\"" - P "\r" ) ^ 0 ) ) - * Q ( P "\"\"\"" ) + * K ( P "\"\"\"" ) * Lc "}}" local LongString = SingleLongString + DoubleLongString local StringDoc = @@ -755,13 +876,12 @@ local CommentMath = local Comment = Lc ( "{\\PitonStyle{Comment}{" ) - * Q ( P "#" ) - * ( CommentMath + Q ( ( 1 - S "$\r" ) ^ 1 ) ) ^ 0 + * K ( P "#" ) + * ( CommentMath + K ( ( 1 - S "$\r" ) ^ 1 ) ) ^ 0 * Lc ( "}}" ) * ( EOL + -1 ) - local CommentLaTeX = - P "##" + P(piton.comment_latex) * Lc "{\\PitonStyle{Comment.LaTeX}{\\ignorespaces" * L ( ( 1 - P "\r" ) ^ 0 ) * Lc "}}" @@ -787,18 +907,19 @@ local Param = SkipSpace * Identifier * SkipSpace * ( K ( P "=" * Expression , 'InitialValues' ) - + Q ( P ":" ) * SkipSpace * K ( letter^1 , 'Name.Type' ) + + K ( P ":" ) * SkipSpace * K ( letter^1 , 'Name.Type' ) ) ^ -1 -local Params = ( Param * ( Q "," * Param ) ^ 0 ) ^ -1 +local Params = ( Param * ( K "," * Param ) ^ 0 ) ^ -1 local DefFunction = K ( P "def" , 'Keyword' ) * Space * K ( identifier , 'Name.Function' ) * SkipSpace - * Q ( P "(" ) * Params * Q ( P ")" ) + * K ( P "(" ) * Params * K ( P ")" ) * SkipSpace + * ( K ( P "->" ) * SkipSpace * K ( identifier , 'Name.Type' ) ) ^ -1 * K ( ( 1 - S ":\r" )^0 , 'Post.Function' ) - * Q ( P ":" ) + * K ( P ":" ) * ( SkipSpace * ( EOL + CommentLaTeX + Comment ) -- in all cases, that contains an EOL * Tab ^ 0 @@ -806,15 +927,14 @@ local DefFunction = * StringDoc ^ 0 -- there may be additionnal docstrings ) ^ -1 local ItemDict = - ShortString * SkipSpace * Q ( P ":" ) * K ( Expression , 'Dict.Value' ) + ShortString * SkipSpace * K ( P ":" ) * K ( Expression , 'Dict.Value' ) local ItemOfSet = SkipSpace * ( ItemDict + ShortString ) * SkipSpace local Set = - Q ( P "{" ) - * ItemOfSet * ( Q ( P "," ) * ItemOfSet ) ^ 0 - * Q ( P "}" ) - + K ( P "{" ) + * ItemOfSet * ( K ( P "," ) * ItemOfSet ) ^ 0 + * K ( P "}" ) local SyntaxPythonAux = Lc ( '\\__piton_begin_line:' ) * ( ( space - P "\r" ) ^0 * P "\r" ) ^ -1 * @@ -846,16 +966,23 @@ local SyntaxPythonAux = + Word ) ^0 * -1 * Lc ( '\\__piton_end_line:' ) local SyntaxPython = Ct ( SyntaxPythonAux ) -function Parse(code) - local t = SyntaxPython : match ( code ) +function piton.Parse(code) + local t = SyntaxPython : match ( code ) -- match is a method of the LPEG for _ , s in ipairs(t) do tex.tprint(s) end end -function ParseFile(name) +function piton.ParseFile(name,first_line,last_line) s = '' - for line in io.lines(name) do s = s .. '\r' .. line end - Parse(s) + local i = 0 + for line in io.lines(name) + do i = i + 1 + if i >= first_line + then s = s .. '\r' .. line + end + if i >= last_line then break end + end + piton.Parse(s) end -function gobble(n,code) +local function gobble(n,code) function concat(acc,new_value) return acc .. new_value end @@ -873,19 +1000,10 @@ function gobble(n,code) ) : match ( code ) end end -function GobbleParse(n,code) - if n==-1 - then n = AutoGobbleLPEG : match(code) - else if n==-2 - then n = EnvGobbleLPEG : match(code) - end - end - Parse(gobble(n,code)) -end -function add(acc,new_value) +local function add(acc,new_value) return acc + new_value end -AutoGobbleLPEG = +local AutoGobbleLPEG = ( space ^ 0 * P "\r" ) ^ -1 * Cf ( ( @@ -899,23 +1017,58 @@ AutoGobbleLPEG = * ( 1 - P " " ) * ( 1 - P "\r" ) ^ 0 ) ^ -1 , math.min ) -EnvGobbleLPEG = +local EnvGobbleLPEG = ( ( 1 - P "\r" ) ^ 0 * P "\r" ) ^ 0 * Cf ( Cc(0) * ( P " " * Cc(1) ) ^ 0 , add ) * -1 -function CountLines(code) +function piton.GobbleParse(n,code) + if n==-1 + then n = AutoGobbleLPEG : match(code) + else if n==-2 + then n = EnvGobbleLPEG : match(code) + end + end + piton.Parse(gobble(n,code)) +end +function piton.CountLines(code) local count = 0 - for i in code:gmatch("\r") do count = count + 1 end + for i in code : gmatch ( "\r" ) do count = count + 1 end tex.sprint( luatexbase.catcodetables.expl , '\\int_set:Nn \\l__piton_nb_lines_int {' .. count .. '}' ) end -function CountLinesFile(name) +function piton.CountNonEmptyLines(code) + local count = 0 + count = + ( Cf ( Cc(0) * + ( + ( P " " ) ^ 0 * P "\r" + + ( 1 - P "\r" ) ^ 0 * P "\r" * Cc(1) + ) ^ 0 + * (1 - P "\r" ) ^ 0 , + add + ) * -1 ) : match (code) + tex.sprint( + luatexbase.catcodetables.expl , + '\\int_set:Nn \\l__piton_nb_non_empty_lines_int {' .. count .. '}' ) +end +function piton.CountLinesFile(name) local count = 0 for line in io.lines(name) do count = count + 1 end tex.sprint( luatexbase.catcodetables.expl , '\\int_set:Nn \\l__piton_nb_lines_int {' .. count .. '}' ) end +function piton.CountNonEmptyLinesFile(name) + local count = 0 + for line in io.lines(name) + do if not ( ( ( P " " ) ^ 0 * -1 ) : match ( line ) ) + then count = count + 1 + end + end + tex.sprint( + luatexbase.catcodetables.expl , + '\\int_set:Nn \\l__piton_nb_non_empty_lines_int {' .. count .. '}' ) +end \end{luacode*} \endinput |