diff options
author | Karl Berry <karl@freefriends.org> | 2023-02-14 20:41:38 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2023-02-14 20:41:38 +0000 |
commit | 8a3494de6e75c6ec6795d0789d288054ff2fbcba (patch) | |
tree | a39a0d3e6c43c903de5dfffc3271b9ff3ed65c12 /Master/texmf-dist/tex/lualatex | |
parent | 1fd0caa8b0e74a823ff39f1cc0347cdd5f635885 (diff) |
piton (14feb23)
git-svn-id: svn://tug.org/texlive/trunk@65835 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/tex/lualatex')
-rw-r--r-- | Master/texmf-dist/tex/lualatex/piton/piton.sty | 598 |
1 files changed, 451 insertions, 147 deletions
diff --git a/Master/texmf-dist/tex/lualatex/piton/piton.sty b/Master/texmf-dist/tex/lualatex/piton/piton.sty index c630fd80755..6ec3460aeec 100644 --- a/Master/texmf-dist/tex/lualatex/piton/piton.sty +++ b/Master/texmf-dist/tex/lualatex/piton/piton.sty @@ -6,7 +6,7 @@ %% %% piton.dtx (with options: `package') %% -%% Copyright (C) 2023 by F. Pantigny +%% Copyright (C) 2022-2023 by F. Pantigny %% %% This file may be distributed and/or modified under the %% conditions of the LaTeX Project Public License, either @@ -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{1.3} -\def\myfiledate{2023/01/29} +\def\myfileversion{1.4} +\def\myfiledate{2023/02/14} \bigskip @@ -129,14 +129,16 @@ \bool_set_true:N \c__piton_footnote_bool } } +\str_new:N \l__piton_language_str +\str_set:Nn \l__piton_language_str { python } \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 \int_set:Nn \l__piton_splittable_int { 100 } -\str_new:N \l__piton_bg_color_tl -\str_new:N \l__piton_prompt_bg_color_tl +\clist_new:N \l__piton_bg_color_clist +\tl_new:N \l__piton_prompt_bg_color_tl \dim_new:N \g__piton_width_dim \dim_new:N \l__piton_width_on_aux_dim \int_new:N \g__piton_env_int @@ -193,7 +195,7 @@ \cs_new_protected:Npn \__piton_prompt: { \tl_gset:Nn \g__piton_begin_line_hook_tl - { \tl_set:NV \l__piton_bg_color_tl \l__piton_prompt_bg_color_tl } + { \clist_set:NV \l__piton_bg_color_clist \l__piton_prompt_bg_color_tl } } \cs_new_protected:Npn \__piton_replace_spaces:n #1 { @@ -220,7 +222,7 @@ \bool_if:NTF \l__piton_slim_bool { \hcoffin_set:Nn \l_tmpa_coffin } { - \str_if_empty:NTF \l__piton_bg_color_tl + \clist_if_empty:NTF \l__piton_bg_color_clist { \vcoffin_set:Nnn \l_tmpa_coffin { \dim_eval:n { \linewidth - \l__piton_left_margin_dim } } @@ -246,7 +248,7 @@ { \tl_if_empty:nF { #1 } } \__piton_print_number: } - \tl_if_empty:NF \l__piton_bg_color_tl + \clist_if_empty:NF \l__piton_bg_color_clist { \skip_horizontal:n { 0.5 em } } \coffin_typeset:Nnnnn \l_tmpa_coffin T l \c_zero_dim \c_zero_dim } @@ -254,14 +256,14 @@ { \dim_gset:Nn \g__piton_width_dim { \box_wd:N \l_tmpa_box } } \box_set_dp:Nn \l_tmpa_box { \box_dp:N \l_tmpa_box + 1.25 pt } \box_set_ht:Nn \l_tmpa_box { \box_ht:N \l_tmpa_box + 1.25 pt } - \str_if_empty:NTF \l__piton_bg_color_tl + \clist_if_empty:NTF \l__piton_bg_color_clist { \box_use_drop:N \l_tmpa_box } { \vbox_top:n { \hbox:n { - \__piton_color:V \l__piton_bg_color_tl + \__piton_color:N \l__piton_bg_color_clist \vrule height \box_ht:N \l_tmpa_box depth \box_dp:N \l_tmpa_box width \l__piton_width_on_aux_dim @@ -275,7 +277,16 @@ \group_end: \tl_gclear:N \g__piton_begin_line_hook_tl } -\cs_set_protected:Npn \__piton_color:n #1 +\cs_set_protected:Npn \__piton_color:N #1 + { + \int_set:Nn \l_tmpa_int { \clist_count:N #1 } + \int_set:Nn \l_tmpb_int { \int_mod:nn \g__piton_line_int \l_tmpa_int + 1 } + \tl_set:Nx \l_tmpa_tl { \clist_item:Nn #1 \l_tmpb_int } + \tl_if_eq:NnTF \l_tmpa_tl { none } + { \dim_zero:N \l__piton_width_on_aux_dim } + { \exp_args:NV \__piton_color_i:n \l_tmpa_tl } + } +\cs_set_protected:Npn \__piton_color_i:n #1 { \tl_if_head_eq_meaning:nNTF { #1 } [ { @@ -314,7 +325,7 @@ \l__piton_continuation_symbol_tl } \skip_horizontal:n { 0.3 em } - \tl_if_empty:NF \l__piton_bg_color_tl + \clist_if_empty:NF \l__piton_bg_color_clist { \skip_horizontal:n { 0.5 em } } } \bool_if:NT \l__piton_indent_broken_lines_bool @@ -333,6 +344,8 @@ \bool_new:N \l__piton_resume_bool \keys_define:nn { PitonOptions } { + language .str_set:N = \l__piton_language_str , + language .value_required:n = true , gobble .int_set:N = \l__piton_gobble_int , gobble .value_required:n = true , auto-gobble .code:n = \int_set:Nn \l__piton_gobble_int { -1 } , @@ -351,10 +364,10 @@ resume .value_forbidden:n = true , splittable .int_set:N = \l__piton_splittable_int , splittable .default:n = 1 , - background-color .str_set:N = \l__piton_bg_color_tl , + background-color .clist_set:N = \l__piton_bg_color_clist , background-color .value_required:n = true , - prompt-background-color .str_set:N = \l__piton_prompt_bg_color_tl , - prompt-background-color .value_required:n = true , + prompt-background-color .tl_set:N = \l__piton_prompt_bg_color_tl , + prompt-background-color .value_required:n = true , slim .bool_set:N = \l__piton_slim_bool , slim .default:n = true , left-margin .code:n = @@ -417,7 +430,7 @@ { \bool_if:NT \l__piton_slim_bool { - \tl_if_empty:NF \l__piton_bg_color_tl + \clist_if_empty:NF \l__piton_bg_color_clist { \tl_gput_right:Nx \g__piton_aux_tl { @@ -443,7 +456,11 @@ \cs_set_protected:Npn \__piton_begin_line: { } \cs_set_protected:Npn \__piton_end_line: { } \tl_set:Nx \l_tmpa_tl - { \lua_now:n { piton.ParseBis(token.scan_string()) } { #1 } } + { + \lua_now:e + { piton.ParseBis('\l__piton_language_str',token.scan_string()) } + { #1 } + } \bool_if:NTF \l__piton_show_spaces_bool { \regex_replace_all:nnN { \x20 } { ␣ } \l_tmpa_tl } % U+2423 { @@ -461,7 +478,11 @@ \cs_set_protected:Npn \__piton_begin_line: { } \cs_set_protected:Npn \__piton_end_line: { } \tl_set:Nx \l_tmpa_tl - { \lua_now:n { piton.Parse(token.scan_string()) } { #1 } } + { + \lua_now:e + { piton.Parse('\l__piton_language_str',token.scan_string()) } + { #1 } + } \bool_if:NT \l__piton_show_spaces_bool { \regex_replace_all:nnN { \x20 } { ␣ } \l_tmpa_tl } % U+2423 \l_tmpa_tl @@ -478,11 +499,19 @@ \l__piton_break_lines_in_Piton_bool { \tl_set:Nx \l_tmpa_tl - { \lua_now:n { piton.ParseTer(token.scan_string()) } { #1 } } + { + \lua_now:e + { piton.ParseTer('\l__piton_language_str',token.scan_string()) } + { #1 } + } } { \tl_set:Nx \l_tmpa_tl - { \lua_now:n { piton.Parse(token.scan_string()) } { #1 } } + { + \lua_now:e + { piton.Parse('\l__piton_language_str',token.scan_string()) } + { #1 } + } } \bool_if:NT \l__piton_show_spaces_bool { \regex_replace_all:nnN { \x20 } { ␣ } \l_tmpa_tl } % U+2423 @@ -501,11 +530,19 @@ \l__piton_break_lines_in_Piton_bool { \tl_set:Nx \l_tmpa_tl - { \lua_now:n { piton.ParseTer(token.scan_string()) } { #1 } } + { + \lua_now:e + { piton.ParseTer('\l__piton_language_str',token.scan_string()) } + { #1 } + } } { \tl_set:Nx \l_tmpa_tl - { \lua_now:n { piton.Parse(token.scan_string()) } { #1 } } + { + \lua_now:e + { piton.Parse('\l__piton_language_str',token.scan_string()) } + { #1 } + } } \bool_if:NT \l__piton_show_spaces_bool { \regex_replace_all:nnN { \x20 } { ␣ } \l_tmpa_tl } % U+2423 @@ -576,7 +613,7 @@ \vtop \bgroup \lua_now:e { - piton.ParseFile(token.scan_argument() , + piton.ParseFile('\l__piton_language_str',token.scan_argument() , \int_use:N \l__piton_first_line_int , \int_use:N \l__piton_last_line_int ) } @@ -633,7 +670,11 @@ \lua_now:e { piton.GobbleParse - ( \int_use:N \l__piton_gobble_int , token.scan_argument() ) + ( + '\l__piton_language_str' , + \int_use:N \l__piton_gobble_int , + token.scan_argument() + ) } { ##1 } \vspace { 2.5 pt } @@ -711,20 +752,26 @@ Name.Class .value_required:n = true , Name.Builtin .tl_set:c = pitonStyle Name.Builtin , Name.Builtin .value_required:n = true , + TypeParameter .tl_set:c = pitonStyle TypeParameter , + TypeParameter .value_required:n = true , Name.Type .tl_set:c = pitonStyle Name.Type , Name.Type .value_required:n = true , Operator .tl_set:c = pitonStyle Operator , Operator .value_required:n = true , Operator.Word .tl_set:c = pitonStyle Operator.Word , Operator.Word .value_required:n = true , - Post.Function .tl_set:c = pitonStyle Post.Function , - Post.Function .value_required:n = true , Exception .tl_set:c = pitonStyle Exception , Exception .value_required:n = true , Comment.LaTeX .tl_set:c = pitonStyle Comment.LaTeX , Comment.LaTeX .value_required:n = true , - Beamer .tl_set:c = pitonStyle Beamer , - Beamer .value_required:n = true , + Identifier .tl_set:c = pitonStyle Identifier , + Comment.LaTeX .value_required:n = true , + ParseAgain.noCR .tl_set:c = pitonStyle ParseAgain.noCR , + ParseAgain.noCR .value_required:n = true , + ParseAgain .tl_set:c = pitonStyle ParseAgain , + ParseAgain .value_required:n = true , + Prompt .tl_set:c = pitonStyle Prompt , + Prompt .value_required:n = true , unknown .code:n = \msg_error:nn { piton } { Unknown~key~for~SetPitonStyle } } @@ -737,6 +784,7 @@ Comment.LaTeX,~ Dict.Value,~ Exception,~ + Identifier,~ InitialValues,~ Keyword,~ Keyword.Constant,~ @@ -748,6 +796,7 @@ Number,~ Operator,~ Operator.Word,~ + Prompt,~ String,~ String.Doc,~ String.Long,~ @@ -776,11 +825,34 @@ InitialValues = \__piton_piton:n , Dict.Value = \__piton_piton:n , Interpol.Inside = \color{black}\__piton_piton:n , - Beamer = \__piton_piton_no_cr:n , - Post.Function = \__piton_piton:n , + TypeParameter = \color[HTML]{008800} \itshape , + Identifier = \__piton_identifier:n , + Prompt = , + ParseAgain.noCR = \__piton_piton_no_cr:n , + ParseAgain = \__piton_piton:n , + } +\bool_if:NT \c__piton_math_comments_bool { \SetPitonStyle { Comment.Math } } +\cs_new_protected:Npn \__piton_identifier:n #1 + { \cs_if_exist_use:c { PitonIdentifier #1 } { #1 } } +\keys_define:nn { PitonOptions } + { identifiers .code:n = \__piton_set_identifiers:n { #1 } } +\keys_define:nn { Piton / identifiers } + { + names .clist_set:N = \l__piton_identifiers_names_tl , + style .tl_set:N = \l__piton_style_tl , + } +\cs_new_protected:Npn \__piton_set_identifiers:n #1 + { + \clist_clear_new:N \l__piton_identifiers_names_tl + \tl_clear_new:N \l__piton_style_tl + \keys_set:nn { Piton / identifiers } { #1 } + \clist_map_inline:Nn \l__piton_identifiers_names_tl + { + \tl_set_eq:cN + { PitonIdentifier ##1 } + \l__piton_style_tl + } } -\bool_if:NT \c__piton_math_comments_bool - { \SetPitonStyle { Comment.Math } } \AddToHook { env / piton / begin } { \msg_fatal:nn { piton } { No~environment~piton } } @@ -811,7 +883,9 @@ end-of-broken-line,~ env-gobble,~ gobble,~ + identifiers,~ indent-broken-lines,~ + language,~ left-margin,~ line-numbers,~ prompt-background-color,~ @@ -853,7 +927,8 @@ 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 Cf, Cs , Cg , Cmt , Cb = lpeg.Cf, lpeg.Cs, lpeg.Cg , lpeg.Cmt , lpeg.Cb +local R = lpeg.R local function Q(pattern) return Ct ( Cc ( luatexbase.catcodetables.CatcodeTableOther ) * C ( pattern ) ) end @@ -888,7 +963,7 @@ local letter = alpha + P "_" local alphanum = letter + digit local identifier = letter * alphanum ^ 0 -local Identifier = K ( identifier ) +local Identifier = K ( identifier , 'Identifier' ) local Number = K ( ( digit^1 * P "." * digit^0 + digit^0 * P "." * digit^1 + digit^1 ) @@ -911,6 +986,7 @@ local Punct = K ( S ".,:;!" ) local Tab = P "\t" * Lc ( '\\l__piton_tab_tl' ) local SpaceIndentation = Lc ( '\\__piton_an_indentation_space:' ) * ( K " " ) local Delim = K ( S "[()]" ) +local VisualSpace = space * Lc "\\l__piton_space_tl" local Operator = K ( P "!=" + P "<>" + P "==" + P "<<" + P ">>" + P "<=" + P ">=" + P ":=" + P "//" + P "**" + S "-~+/*%=<>&.@|" @@ -946,7 +1022,7 @@ local Builtin = 'Name.Builtin' ) local Exception = - K ( "ArithmeticError" + P "AssertionError" + P "AttributeError" + K ( P "ArithmeticError" + P "AssertionError" + P "AttributeError" + P "BaseException" + P "BufferError" + P "BytesWarning" + P "DeprecationWarning" + P "EOFError" + P "EnvironmentError" + P "Exception" + P "FloatingPointError" + P "FutureWarning" + P "GeneratorExit" + P "IOError" + P "ImportError" @@ -990,41 +1066,6 @@ local FromImport = K ( P "from" , 'Keyword' ) * Space * K ( identifier , 'Name.Namespace' ) * Space * K ( P "import" , 'Keyword' ) -local SingleShortInterpol = - K ( P "{" , 'String.Interpol') - * K ( ( 1 - S "}':" ) ^ 0 , 'Interpol.Inside' ) - * 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' ) * K ( (1 - S "}:\"") ^ 0 ) ) ^ -1 - * K ( P "}" , 'String.Interpol' ) - -local SingleLongInterpol = - K ( P "{" , 'String.Interpol' ) - * K ( ( 1 - S "}:\r" - P "'''" ) ^ 0 , 'Interpol.Inside' ) - * 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' ) - * K ( P ":" * (1 - S "}:\r" - P "\"\"\"" ) ^ 0 ) ^ -1 - * K ( P "}" , 'String.Interpol' ) -local VisualSpace = space * Lc "\\l__piton_space_tl" -local SingleShortPureString = - ( VisualSpace + K ( ( P "\\'" + P "{{" + P "}}" + 1 - S " {}'" ) ^ 1 ) ) ^ 1 - -local DoubleShortPureString = - ( VisualSpace + K ( ( P "\\\"" + P "{{" + P "}}" + 1 - S " {}\"" ) ^ 1 ) ) ^ 1 - -local SingleLongPureString = - K ( ( 1 - P "'''" - S "{}'\r" ) ^ 1 ) - -local DoubleLongPureString = - K ( ( 1 - P "\"\"\"" - S " {}\"\r" ) ^ 1 ) local PercentInterpol = K ( P "%" * ( P "(" * alphanum ^ 1 * P ")" ) ^ -1 @@ -1039,7 +1080,16 @@ local SingleShortString = Lc ( "{\\PitonStyle{String.Short}{" ) * ( K ( P "f'" + P "F'" ) - * ( SingleShortInterpol + SingleShortPureString ) ^ 0 + * ( + K ( P "{" , 'String.Interpol') + * K ( ( 1 - S "}':" ) ^ 0 , 'Interpol.Inside' ) + * K ( P ":" * (1 - S "}:'") ^ 0 ) ^ -1 + * K ( P "}" , 'String.Interpol' ) + + + VisualSpace + + + K ( ( P "\\'" + P "{{" + P "}}" + 1 - S " {}'" ) ^ 1 ) + ) ^ 0 * K ( P "'" ) + K ( P "'" + P "r'" + P "R'" ) @@ -1056,7 +1106,16 @@ local DoubleShortString = Lc ( "{\\PitonStyle{String.Short}{" ) * ( K ( P "f\"" + P "F\"" ) - * ( DoubleShortInterpol + DoubleShortPureString ) ^ 0 + * ( + K ( P "{" , 'String.Interpol' ) + * K ( ( 1 - S "}\":" ) ^ 0 , 'Interpol.Inside' ) + * ( K ( P ":" , 'String.Interpol' ) * K ( (1 - S "}:\"") ^ 0 ) ) ^ -1 + * K ( P "}" , 'String.Interpol' ) + + + VisualSpace + + + K ( ( P "\\\"" + P "{{" + P "}}" + 1 - S " {}\"" ) ^ 1 ) + ) ^ 0 * K ( P "\"" ) + K ( P "\"" + P "r\"" + P "R\"" ) @@ -1072,11 +1131,13 @@ local DoubleShortString = local ShortString = SingleShortString + DoubleShortString local BalancedBraces = P { "E" , - E = ( ShortString + ( 1 - S "{}" ) ) ^ 0 - * + E = ( - P "{" * V "E" * P "}" - * ( ShortString + ( 1 - S "{}" ) ) ^ 0 + P "{" * V "E" * P "}" + + + ShortString + + + ( 1 - S "{}" ) ) ^ 0 } local Beamer = P ( false ) @@ -1084,6 +1145,7 @@ local BeamerBeginEnvironments = P ( true ) local BeamerEndEnvironments = P ( true ) local BeamerNamesEnvironments = P "uncoverenv" + P "onlyenv" + P "visibleenv" + P "invisibleenv" + + P "alertenv" + P "actionenv" if piton_beamer then @@ -1099,7 +1161,7 @@ then ) * L ( ( P "<" * (1 - P ">") ^ 0 * P ">" ) ^ -1 * P "{" ) - * K ( BalancedBraces , 'Beamer' ) + * K ( BalancedBraces , 'ParseAgain.noCR' ) * L ( P "}" ) + L ( @@ -1107,9 +1169,9 @@ then * P "<" * (1 - P ">") ^ 0 * P ">" * P "{" ) - * K ( BalancedBraces , 'Beamer' ) + * K ( BalancedBraces , 'ParseAgain.noCR' ) * L ( P "}{" ) - * K ( BalancedBraces , 'Beamer' ) + * K ( BalancedBraces , 'ParseAgain.noCR' ) * L ( P "}" ) + L ( @@ -1117,11 +1179,11 @@ then * P "<" * (1 - P ">") ^ 0 * P ">" * P "{" ) - * K ( BalancedBraces , 'Beamer' ) + * K ( BalancedBraces , 'ParseAgain.noCR' ) * L ( P "}{" ) - * K ( BalancedBraces , 'Beamer' ) + * K ( BalancedBraces , 'ParseAgain.noCR' ) * L ( P "}{" ) - * K ( BalancedBraces , 'Beamer' ) + * K ( BalancedBraces , 'ParseAgain.noCR' ) * L ( P "}" ) BeamerBeginEnvironments = ( space ^ 0 * @@ -1138,7 +1200,8 @@ then * P "\r" ) ^ 0 end -local Prompt = ( # ( P ">>>" + P "..." ) * Lc ( '\\__piton_prompt:' ) ) ^ -1 +local PromptHastyDetection = ( # ( P ">>>" + P "..." ) * Lc ( '\\__piton_prompt:' ) ) ^ -1 +local Prompt = K ( ( ( P ">>>" + P "..." ) * P " " ^ -1 ) ^ -1 , 'Prompt' ) local EOL if piton_beamer then @@ -1151,8 +1214,9 @@ EOL = Lc ( '\\__piton_end_line:' ) * BeamerEndEnvironments * BeamerBeginEnvironments - * Prompt + * PromptHastyDetection * Lc ( '\\__piton_newline: \\__piton_begin_line:' ) + * Prompt ) * SpaceIndentation ^ 0 @@ -1164,40 +1228,41 @@ EOL = ( space ^ 0 * -1 ) + Lc ( '\\__piton_end_line:' ) - * Prompt + * PromptHastyDetection * Lc ( '\\__piton_newline: \\__piton_begin_line:' ) + * Prompt ) * SpaceIndentation ^ 0 end +function EOL_for_style ( s ) + return Lc "}}" * EOL * Lc ( "{\\PitonStyle{" .. s .. "}{" ) +end local SingleLongString = Lc "{\\PitonStyle{String.Long}{" * ( K ( S "fF" * P "'''" ) - * ( SingleLongInterpol + SingleLongPureString ) ^ 0 - * Lc "}}" * ( - EOL + K ( P "{" , 'String.Interpol' ) + * K ( ( 1 - S "}:\r" - P "'''" ) ^ 0 , 'Interpol.Inside' ) + * K ( P ":" * (1 - S "}:\r" - P "'''" ) ^ 0 ) ^ -1 + * K ( P "}" , 'String.Interpol' ) + - Lc "{\\PitonStyle{String.Long}{" - * ( SingleLongInterpol + SingleLongPureString ) ^ 0 - * Lc "}}" - * EOL + K ( ( 1 - P "'''" - S "{}'\r" ) ^ 1 ) + + + EOL_for_style 'String.Long' ) ^ 0 - * Lc "{\\PitonStyle{String.Long}{" - * ( SingleLongInterpol + SingleLongPureString ) ^ 0 + - K ( ( S "rR" ) ^ -1 * P "'''" - * ( 1 - P "'''" - P "\r" ) ^ 0 ) - * Lc "}}" + K ( ( S "rR" ) ^ -1 * P "'''" ) * ( - Lc "{\\PitonStyle{String.Long}{" - * K ( ( 1 - P "'''" - P "\r" ) ^ 0 ) - * Lc "}}" - * EOL + K ( ( 1 - P "'''" - S "\r%" ) ^ 1 ) + + + PercentInterpol + + + P "%" + + + EOL_for_style 'String.Long' ) ^ 0 - * Lc "{\\PitonStyle{String.Long}{" - * K ( ( 1 - P "'''" - P "\r" ) ^ 0 ) ) * K ( P "'''" ) * Lc "}}" @@ -1206,30 +1271,27 @@ local DoubleLongString = Lc "{\\PitonStyle{String.Long}{" * ( K ( S "fF" * P "\"\"\"" ) - * ( DoubleLongInterpol + DoubleLongPureString ) ^ 0 - * Lc "}}" * ( - EOL + K ( P "{" , 'String.Interpol' ) + * K ( ( 1 - S "}:\r" - P "\"\"\"" ) ^ 0 , 'Interpol.Inside' ) + * K ( P ":" * (1 - S "}:\r" - P "\"\"\"" ) ^ 0 ) ^ -1 + * K ( P "}" , 'String.Interpol' ) + - Lc "{\\PitonStyle{String.Long}{" - * ( DoubleLongInterpol + DoubleLongPureString ) ^ 0 - * Lc "}}" - * EOL + K ( ( 1 - P "\"\"\"" - S "{}\"\r" ) ^ 1 ) + + + EOL_for_style 'String.Long' ) ^ 0 - * Lc "{\\PitonStyle{String.Long}{" - * ( DoubleLongInterpol + DoubleLongPureString ) ^ 0 + - K ( ( S "rR" ) ^ -1 * P "\"\"\"" - * ( 1 - P "\"\"\"" - P "\r" ) ^ 0 ) - * Lc "}}" + K ( ( S "rR" ) ^ -1 * P "\"\"\"" ) * ( - Lc "{\\PitonStyle{String.Long}{" - * K ( ( 1 - P "\"\"\"" - P "\r" ) ^ 0 ) - * Lc "}}" - * EOL + K ( ( 1 - P "\"\"\"" - S "%\r" ) ^ 1 ) + + + PercentInterpol + + + P "%" + + + EOL_for_style 'String.Long' ) ^ 0 - * Lc "{\\PitonStyle{String.Long}{" - * K ( ( 1 - P "\"\"\"" - P "\r" ) ^ 0 ) ) * K ( P "\"\"\"" ) * Lc "}}" @@ -1287,7 +1349,7 @@ local DefFunction = * K ( P "(" ) * Params * K ( P ")" ) * SkipSpace * ( K ( P "->" ) * SkipSpace * K ( identifier , 'Name.Type' ) ) ^ -1 - * K ( ( 1 - S ":\r" )^0 , 'Post.Function' ) + * K ( ( 1 - S ":\r" )^0 , 'ParseAgain' ) * K ( P ":" ) * ( SkipSpace * ( EOL + CommentLaTeX + Comment ) -- in all cases, that contains an EOL @@ -1322,7 +1384,6 @@ MainLoop = + ShortString + Punct + FromImport - + ImportAs + RaiseException + DefFunction + DefClass @@ -1334,28 +1395,271 @@ MainLoop = + Number + Word ) ^ 0 -local SyntaxPython = P ( true ) +local python = P ( true ) -function piton.defSyntaxPython() - SyntaxPython = - Ct ( - ( ( space - P "\r" ) ^0 * P "\r" ) ^ -1 - * BeamerBeginEnvironments - * Prompt - * Lc ( '\\__piton_begin_line:' ) - * SpaceIndentation ^ 0 - * MainLoop - * -1 - * Lc ( '\\__piton_end_line:' ) - ) +python = + Ct ( + ( ( space - P "\r" ) ^0 * P "\r" ) ^ -1 + * BeamerBeginEnvironments + * PromptHastyDetection + * Lc ( '\\__piton_begin_line:' ) + * Prompt + * SpaceIndentation ^ 0 + * MainLoop + * -1 + * Lc ( '\\__piton_end_line:' ) + ) +local languages = { } +languages['python'] = python +local Punct = K ( S ",:;!" ) +local identifier = + ( R "az" + R "AZ" + P "_") * ( R "az" + R "AZ" + S "_'" + digit ) ^ 0 + +local Identifier = K ( identifier ) + +local Operator = + K ( P "!=" + P "<>" + P "==" + P "<<" + P ">>" + P "<=" + P ">=" + P ":=" + + P "||" + P "&&" + P "//" + P "**" + P ";;" + P "::" + P "->" + + P "+." + P "-." + P "*." + P "/." + + S "-~+/*%=<>&@|" + , + 'Operator' + ) + +local OperatorWord = + K ( P "and" + P "asr" + P "land" + P "lor" + P "lsl" + P "lxor" + + P "mod" + P "or" , + 'Operator.Word') + +local Keyword = + K ( P "as" + P "assert" + P "begin" + P "class" + P "constraint" + P "done" + + P "do" + P "downto" + P "else" + P "end" + P "exception" + P "external" + + P "false" + P "for" + P "function" + P "fun" + P "functor" + P "if" + + P "in" + P "include" + P "inherit" + P "initializer" + P "lazy" + P "let" + + P "match" + P "method" + P "module" + P "mutable" + P "new" + P "object" + + P "of" + P "open" + P "private" + P "raise" + P "rec" + P "sig" + + P "struct" + P "then" + P "to" + P "true" + P "try" + P "type" + + P "value" + P "val" + P "virtual" + P "when" + P "while" + P "with" , + 'Keyword' ) + + K ( P "true" + P "false" , 'Keyword.Constant' ) + +local Builtin = + K ( P "not" + P "incr" + P "decr" + P "fst" + P "snd" + + P "String.length" + + P "List.tl" + P "List.hd" + P "List.mem" + P "List.exists" + + P "List.for_all" + P "List.filter" + P "List.length" + P "List.map" + + P "List.iter" + + P "Array.length" + P "Array.make" + P "Array.make_matrix" + + P "Array.init" + P "Array.copy" + P "Array.copy" + P "Array.mem" + + P "Array.exists" + P "Array.for_all" + P "Array.map" + P "Array.iter" + + P "Queue.create" + P "Queue.is_empty" + P "Queue.push" + P "Queue.pop" + + P "Stack.create" + P "Stack.is_empty" + P "Stack.push" + P "Stack.pop" + + P "Hashtbl.create" + P "Hashtbl.add" + P "Hashtbl.remove" + + P "Hashtbl.mem" + P "Hashtbl.find" + P "Hashtbl.find_opt" + + P "Hashtbl.iter" + , 'Name.Builtin' ) +local Exception = + K ( P "Division_by_zero" + P "End_of_File" + P "Failure" + + P "Invalid_argument" + P "Match_failure" + P "Not_found" + + P "Out_of_memory" + P "Stack_overflow" + P "Sys_blocked_io" + + P "Sys_error" + P "Undefined_recursive_module" , + 'Exception' ) +local Char = + K ( P "'" * ( ( 1 - P "'" ) ^ 0 + P "\\'" ) * P "'" , 'String.Short' ) +local BalancedBraces = + P { "E" , + E = + ( + P "{" * V "E" * P "}" + + + P "\"" * ( 1 - S "\"" ) ^ 0 * P "\"" -- OCaml strings + + + ( 1 - S "{}" ) + ) ^ 0 + } +if piton_beamer +then + Beamer = + L ( P "\\pause" * ( P "[" * (1 - P "]") ^ 0 * P "]" ) ^ -1 ) + + + ( P "\\uncover" * Lc ( '\\__piton_beamer_command:n{uncover}' ) + + P "\\only" * Lc ( '\\__piton_beamer_command:n{only}' ) + + P "\\alert" * Lc ( '\\__piton_beamer_command:n{alert}' ) + + P "\\visible" * Lc ( '\\__piton_beamer_command:n{visible}' ) + + P "\\invisible" * Lc ( '\\__piton_beamer_command:n{invisible}' ) + + P "\\action" * Lc ( '\\__piton_beamer_command:n{action}' ) + ) + * + L ( ( P "<" * (1 - P ">") ^ 0 * P ">" ) ^ -1 * P "{" ) + * K ( BalancedBraces , 'ParseAgain.noCR' ) + * L ( P "}" ) + + + L ( + ( P "\\alt" ) + * P "<" * (1 - P ">") ^ 0 * P ">" + * P "{" + ) + * K ( BalancedBraces , 'ParseAgain.noCR' ) + * L ( P "}{" ) + * K ( BalancedBraces , 'ParseAgain.noCR' ) + * L ( P "}" ) + + + L ( + ( P "\\temporal" ) + * P "<" * (1 - P ">") ^ 0 * P ">" + * P "{" + ) + * K ( BalancedBraces , 'ParseAgain.noCR' ) + * L ( P "}{" ) + * K ( BalancedBraces , 'ParseAgain.noCR' ) + * L ( P "}{" ) + * K ( BalancedBraces , 'ParseAgain.noCR' ) + * L ( P "}" ) + BeamerBeginEnvironments = + ( space ^ 0 * + L + ( + P "\\begin{" * BeamerNamesEnvironments * "}" + * ( P "<" * ( 1 - P ">") ^ 0 * P ">" ) ^ -1 + ) + * P "\r" + ) ^ 0 + BeamerEndEnvironments = + ( space ^ 0 * + L ( P "\\end{" * BeamerNamesEnvironments * P "}" ) + * P "\r" + ) ^ 0 +end +local EOL +if piton_beamer +then +EOL = + P "\r" + * + ( + ( space^0 * -1 ) + + + Lc ( '\\__piton_end_line:' ) + * BeamerEndEnvironments + * BeamerBeginEnvironments + * Lc ( '\\__piton_newline: \\__piton_begin_line:' ) + ) + * + SpaceIndentation ^ 0 +else +EOL = + P "\r" + * + ( + ( space ^ 0 * -1 ) + + + Lc ( '\\__piton_end_line:' ) + * Lc ( '\\__piton_newline: \\__piton_begin_line:' ) + ) + * + SpaceIndentation ^ 0 +end +function EOL_for_style ( s ) + return Lc "}}" * EOL * Lc ( "{\\PitonStyle{" .. s .. "}{" ) end +local String = + Lc "{\\PitonStyle{String.Long}{" + * K ( P "\"" ) + * ( + VisualSpace + + + K ( ( 1 - S " \"\r" ) ^ 1 ) + + + EOL_for_style 'String.Long' + ) ^ 0 + * K ( P "\"" ) + * Lc "}}" +local ext = ( R "az" + P "_" ) ^ 0 +local open = "{" * Cg(ext, 'init') * "|" +local close = "|" * C(ext) * "}" +local closeeq = + Cmt ( close * Cb('init'), + function (s, i, a, b) return a==b end ) +local QuotedStringBis = + Lc "{\\PitonStyle{String.Long}{" + * + ( + VisualSpace + + + K ( ( 1 - S " \r" ) ^ 1 ) + + + EOL_for_style 'String.Long' + ) ^ 0 + * Lc "}}" +local QuotedString = + C ( open * ( 1 - closeeq ) ^ 0 * close ) / + ( function (s) return QuotedStringBis : match(s) end ) +local Comment = + Lc "{\\PitonStyle{Comment}{" + * + P { + "A" , + A = K "(*" + * ( V "A" + + K ( ( 1 - P "(*" - P "*)" - S "\r$" ) ^ 1 ) -- $ + + P "$" * K ( ( 1 - S "$\r" ) ^ 1 , 'Comment.Math' ) * P "$" -- $ + + EOL_for_style 'Comment' + ) ^ 0 + * K "*)" + } + * + Lc "}}" +local DefFunction = + ( K ( P "let rec" + P "let" + P "and" , 'Keyword' ) ) + * Space + * K ( identifier , 'Name.Function' ) + * Space + * # ( 1 - P "=" ) +local TypeParameter = K ( P "'" * alpha * # ( 1 - P "'") , 'TypeParameter' ) +MainLoop = + ( ( space^1 * -1 ) + + EOL + + Space + + Tab + + Escape + + Beamer + + TypeParameter + + String + QuotedString + Char + + Comment + + Delim + + Operator + + Punct + + FromImport + + ImportAs + + Exception + + DefFunction + + Keyword * ( Space + Punct + Delim + EOL + -1 ) + + OperatorWord * ( Space + Punct + Delim + EOL + -1 ) + + Builtin * ( Space + Punct + Delim + EOL + -1 ) + + Identifier + + Number + + Word + ) ^ 0 +local ocaml = P ( true ) -piton.defSyntaxPython() -function piton.Parse(code) - local t = SyntaxPython : match ( code ) +ocaml = + Ct ( + ( ( space - P "\r" ) ^0 * P "\r" ) ^ -1 + * BeamerBeginEnvironments + * Lc ( '\\__piton_begin_line:' ) + * SpaceIndentation ^ 0 + * MainLoop + * -1 + * Lc ( '\\__piton_end_line:' ) + ) +languages['ocaml'] = ocaml +local MinimalSyntax = Ct ( ( (1 - P "\r" ) ^ 1 + EOL ) ^ 0 ) +function piton.Parse(language,code) + local t = languages[language] : match ( code ) + if t == nil then t = MinimalSyntax : match ( code ) end for _ , s in ipairs(t) do tex.tprint(s) end end -function piton.ParseFile(name,first_line,last_line) +function piton.ParseFile(language,name,first_line,last_line) s = '' local i = 0 for line in io.lines(name) @@ -1365,16 +1669,16 @@ function piton.ParseFile(name,first_line,last_line) end if i >= last_line then break end end - piton.Parse(s) + piton.Parse(language,s) end -function piton.ParseBis(code) +function piton.ParseBis(language,code) local s = ( Cs ( ( P '##' / '#' + 1 ) ^ 0 ) ) : match ( code ) - return piton.Parse(s) + return piton.Parse(language,s) end -function piton.ParseTer(code) +function piton.ParseTer(language,code) local s = ( Cs ( ( P '\\__piton_breakable_space:' / ' ' + 1 ) ^ 0 ) ) : match ( code ) - return piton.Parse(s) + return piton.Parse(language,s) end local function gobble(n,code) function concat(acc,new_value) @@ -1428,7 +1732,7 @@ local TabsAutoGobbleLPEG = local EnvGobbleLPEG = ( ( 1 - P "\r" ) ^ 0 * P "\r" ) ^ 0 * Cf ( Cc(0) * ( P " " * Cc(1) ) ^ 0 , add ) * -1 -function piton.GobbleParse(n,code) +function piton.GobbleParse(language,n,code) if n==-1 then n = AutoGobbleLPEG : match(code) else if n==-2 @@ -1438,7 +1742,7 @@ function piton.GobbleParse(n,code) end end end - piton.Parse(gobble(n,code)) + piton.Parse(language,gobble(n,code)) end function piton.CountLines(code) local count = 0 |