summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/lualatex/piton/piton.sty
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/tex/lualatex/piton/piton.sty')
-rw-r--r--Master/texmf-dist/tex/lualatex/piton/piton.sty872
1 files changed, 490 insertions, 382 deletions
diff --git a/Master/texmf-dist/tex/lualatex/piton/piton.sty b/Master/texmf-dist/tex/lualatex/piton/piton.sty
index 6ec3460aeec..645a643e9ee 100644
--- a/Master/texmf-dist/tex/lualatex/piton/piton.sty
+++ b/Master/texmf-dist/tex/lualatex/piton/piton.sty
@@ -18,12 +18,8 @@
%% and version 1.3 or later is part of all distributions of
%% LaTeX version 2005/12/01 or later.
%%
-\def\myfileversion{1.4}
-\def\myfiledate{2023/02/14}
-
-\bigskip
-
-%%
+\def\myfileversion{1.5}
+\def\myfiledate{2023/04/04}
\NeedsTeXFormat{LaTeX2e}
@@ -34,7 +30,13 @@
{\myfileversion}
{Highlight Python codes with LPEG on LuaLaTeX}
\msg_new:nnn { piton } { LuaLaTeX~mandatory }
- { The~package~'piton'~must~be~used~with~LuaLaTeX.\\ It~won't~be~loaded. }
+ {
+ LuaLaTeX~is~mandatory.\\
+ The~package~'piton'~requires~the~engine~LuaLaTeX.\\
+ \str_if_eq:VnT \c_sys_jobname_str { output }
+ { If~you~use~Overleaf,~you~can~switch~to~LuaLaTeX~in~the~"Menu". \\}
+ If~you~go~on,~the~package~'piton'~won't~be~loaded.
+ }
\sys_if_engine_luatex:F { \msg_critical:nn { piton } { LuaLaTeX~mandatory } }
\RequirePackage { luatexbase }
\bool_new:N \c__piton_footnotehyper_bool
@@ -155,6 +157,8 @@
\bool_new:N \l__piton_slim_bool
\dim_new:N \l__piton_left_margin_dim
\bool_new:N \l__piton_left_margin_auto_bool
+\dim_new:N \l__piton_numbers_sep_dim
+\dim_set:Nn \l__piton_numbers_sep_dim { 0.7 em }
\tl_new:N \l__piton_tab_tl
\cs_new_protected:Npn \__piton_set_tab_tl:n #1
{
@@ -191,12 +195,60 @@
}
{ \msg_error:nn { piton } { label~with~lines~numbers } }
}
+\cs_new_protected:Npn \__piton_open_brace:
+ { \directlua { piton.open_brace() } }
+\cs_new_protected:Npn \__piton_close_brace:
+ { \directlua { piton.close_brace() } }
\tl_new:N \g__piton_begin_line_hook_tl
\cs_new_protected:Npn \__piton_prompt:
{
\tl_gset:Nn \g__piton_begin_line_hook_tl
{ \clist_set:NV \l__piton_bg_color_clist \l__piton_prompt_bg_color_tl }
}
+\clist_new:N \g__piton_current_style_clist
+\clist_set:Nn \g__piton_current_style_clist { __end }
+\cs_new_protected:Npn \__piton_close_current_styles:
+ {
+ \int_set:Nn \l_tmpa_int { \clist_count:N \g__piton_current_style_clist - 1 }
+ \exp_args:NV \__piton_close_n_styles:n \l_tmpa_int
+ }
+\cs_new_protected:Npn \__piton_close_n_styles:n #1
+ {
+ \int_compare:nNnT { #1 } > 0
+ {
+ \__piton_close_brace:
+ \__piton_close_brace:
+ \__piton_close_n_styles:n { #1 - 1 }
+ }
+ }
+\cs_new_protected:Npn \__piton_open_current_styles:
+ { \exp_last_unbraced:NV \__piton_open_styles:w \g__piton_current_style_clist , }
+\cs_new_protected:Npn \__piton_open_styles:w #1 ,
+ {
+ \tl_if_eq:nnF { #1 } { __end }
+ { \__piton_open_brace: #1 \__piton_open_brace: \__piton_open_styles:w }
+ }
+\cs_new_protected:Npn \__piton_pop_style:
+ {
+ \clist_greverse:N \g__piton_current_style_clist
+ \clist_gpop:NN \g__piton_current_style_clist \l_tmpa_tl
+ \clist_gpop:NN \g__piton_current_style_clist \l_tmpa_tl
+ \clist_gpush:Nn \g__piton_current_style_clist { __end }
+ \clist_greverse:N \g__piton_current_style_clist
+ }
+\cs_new_protected:Npn \__piton_push_style:n #1
+ {
+ \clist_greverse:N \g__piton_current_style_clist
+ \clist_gpop:NN \g__piton_current_style_clist \l_tmpa_tl
+ \clist_gpush:Nn \g__piton_current_style_clist { #1 }
+ \clist_gpush:Nn \g__piton_current_style_clist { __end }
+ \clist_greverse:N \g__piton_current_style_clist
+ }
+\cs_new_protected:Npn \__piton_push_and_exec:n #1
+ {
+ \__piton_push_style:n { #1 }
+ \__piton_open_brace: #1 \__piton_open_brace:
+ }
\cs_new_protected:Npn \__piton_replace_spaces:n #1
{
\tl_set:Nn \l_tmpa_tl { #1 }
@@ -249,7 +301,13 @@
\__piton_print_number:
}
\clist_if_empty:NF \l__piton_bg_color_clist
- { \skip_horizontal:n { 0.5 em } }
+ {
+ \dim_compare:nNnT \l__piton_left_margin_dim = \c_zero_dim
+ {
+ \bool_if:NF \l__piton_left_margin_auto_bool
+ { \skip_horizontal:n { 0.5 em } }
+ }
+ }
\coffin_typeset:Nnnnn \l_tmpa_coffin T l \c_zero_dim \c_zero_dim
}
\dim_compare:nNnT { \box_wd:N \l_tmpa_box } > \g__piton_width_dim
@@ -378,6 +436,8 @@
}
{ \dim_set:Nn \l__piton_left_margin_dim { #1 } } ,
left-margin .value_required:n = true ,
+ numbers-sep .dim_set:N = \l__piton_numbers_sep_dim ,
+ numbers-sep .value_required:n = true ,
tab-size .code:n = \__piton_set_tab_tl:n { #1 } ,
tab-size .value_required:n = true ,
show-spaces .bool_set:N = \l__piton_show_spaces_bool ,
@@ -409,7 +469,7 @@
\hbox_overlap_left:n
{
{ \color { gray } \footnotesize \int_to_arabic:n \g__piton_visual_line_int }
- \skip_horizontal:n { 0.4 em }
+ \skip_horizontal:N \l__piton_numbers_sep_dim
}
}
\cs_new_protected:Npn \__piton_write_aux:
@@ -606,7 +666,8 @@
{ \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 }
+ \dim_set:Nn \l__piton_left_margin_dim
+ { \box_wd:N \l_tmpa_box + \l__piton_numbers_sep_dim + 0.1 em }
}
\ttfamily
\bool_if:NT \c__piton_footnote_bool { \begin { savenotes } }
@@ -662,7 +723,7 @@
}
}
\dim_set:Nn \l__piton_left_margin_dim
- { \box_wd:N \l_tmpa_box + 0.5 em }
+ { \box_wd:N \l_tmpa_box + \l__piton_numbers_sep_dim + 0.1 em }
}
\ttfamily
\bool_if:NT \c__piton_footnote_bool { \begin { savenotes } }
@@ -714,65 +775,67 @@
{ \normalfont \scantextokens { $#1$ } }
\keys_define:nn { piton / Styles }
{
- String.Interpol .tl_set:c = pitonStyle String.Interpol ,
- String.Interpol .value_required:n = true ,
- FormattingType .tl_set:c = pitonStyle FormattingType ,
- FormattingType .value_required:n = true ,
- Dict.Value .tl_set:c = pitonStyle Dict.Value ,
- Dict.Value .value_required:n = true ,
- Name.Decorator .tl_set:c = pitonStyle Name.Decorator ,
- Name.Decorator .value_required:n = true ,
- Name.Function .tl_set:c = pitonStyle Name.Function ,
- Name.Function .value_required:n = true ,
- Keyword .tl_set:c = pitonStyle Keyword ,
- Keyword .value_required:n = true ,
- Keyword.Constant .tl_set:c = pitonStyle Keyword.Constant ,
- Keyword.constant .value_required:n = true ,
- String.Doc .tl_set:c = pitonStyle String.Doc ,
- String.Doc .value_required:n = true ,
- Interpol.Inside .tl_set:c = pitonStyle Interpol.Inside ,
- Interpol.Inside .value_required:n = true ,
- String.Long .tl_set:c = pitonStyle String.Long ,
- String.Long .value_required:n = true ,
- String.Short .tl_set:c = pitonStyle String.Short ,
- String.Short .value_required:n = true ,
- String .meta:n = { String.Long = #1 , String.Short = #1 } ,
- Comment.Math .tl_set:c = pitonStyle Comment.Math ,
- Comment.Math .default:n = \__piton_math_scantokens:n ,
- Comment.Math .initial:n = ,
- Comment .tl_set:c = pitonStyle Comment ,
- Comment .value_required:n = true ,
- InitialValues .tl_set:c = pitonStyle InitialValues ,
- InitialValues .value_required:n = true ,
- Number .tl_set:c = pitonStyle Number ,
- Number .value_required:n = true ,
- Name.Namespace .tl_set:c = pitonStyle Name.Namespace ,
- Name.Namespace .value_required:n = true ,
- Name.Class .tl_set:c = pitonStyle Name.Class ,
- 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 ,
- 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 ,
- 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 =
+ String.Interpol .tl_set:c = pitonStyle String.Interpol ,
+ String.Interpol .value_required:n = true ,
+ FormattingType .tl_set:c = pitonStyle FormattingType ,
+ FormattingType .value_required:n = true ,
+ Dict.Value .tl_set:c = pitonStyle Dict.Value ,
+ Dict.Value .value_required:n = true ,
+ Name.Decorator .tl_set:c = pitonStyle Name.Decorator ,
+ Name.Decorator .value_required:n = true ,
+ Name.Function .tl_set:c = pitonStyle Name.Function ,
+ Name.Function .value_required:n = true ,
+ Name.UserFunction .tl_set:c = pitonStyle Name.UserFunction ,
+ Name.UserFunction .value_required:n = true ,
+ Keyword .tl_set:c = pitonStyle Keyword ,
+ Keyword .value_required:n = true ,
+ Keyword.Constant .tl_set:c = pitonStyle Keyword.Constant ,
+ Keyword.constant .value_required:n = true ,
+ String.Doc .tl_set:c = pitonStyle String.Doc ,
+ String.Doc .value_required:n = true ,
+ Interpol.Inside .tl_set:c = pitonStyle Interpol.Inside ,
+ Interpol.Inside .value_required:n = true ,
+ String.Long .tl_set:c = pitonStyle String.Long ,
+ String.Long .value_required:n = true ,
+ String.Short .tl_set:c = pitonStyle String.Short ,
+ String.Short .value_required:n = true ,
+ String .meta:n = { String.Long = #1 , String.Short = #1 } ,
+ Comment.Math .tl_set:c = pitonStyle Comment.Math ,
+ Comment.Math .default:n = \__piton_math_scantokens:n ,
+ Comment.Math .initial:n = ,
+ Comment .tl_set:c = pitonStyle Comment ,
+ Comment .value_required:n = true ,
+ InitialValues .tl_set:c = pitonStyle InitialValues ,
+ InitialValues .value_required:n = true ,
+ Number .tl_set:c = pitonStyle Number ,
+ Number .value_required:n = true ,
+ Name.Namespace .tl_set:c = pitonStyle Name.Namespace ,
+ Name.Namespace .value_required:n = true ,
+ Name.Class .tl_set:c = pitonStyle Name.Class ,
+ 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 ,
+ 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 ,
+ 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 }
}
\msg_new:nnn { piton } { Unknown~key~for~SetPitonStyle }
@@ -805,35 +868,36 @@
}
\SetPitonStyle
{
- Comment = \color[HTML]{0099FF} \itshape ,
- Exception = \color[HTML]{CC0000} ,
- Keyword = \color[HTML]{006699} \bfseries ,
- Keyword.Constant = \color[HTML]{006699} \bfseries ,
- Name.Builtin = \color[HTML]{336666} ,
- Name.Decorator = \color[HTML]{9999FF},
- Name.Class = \color[HTML]{00AA88} \bfseries ,
- Name.Function = \color[HTML]{CC00FF} ,
- Name.Namespace = \color[HTML]{00CCFF} ,
- Number = \color[HTML]{FF6600} ,
- Operator = \color[HTML]{555555} ,
- Operator.Word = \bfseries ,
- String = \color[HTML]{CC3300} ,
- String.Doc = \color[HTML]{CC3300} \itshape ,
- String.Interpol = \color[HTML]{AA0000} ,
- Comment.LaTeX = \normalfont \color[rgb]{.468,.532,.6} ,
- Name.Type = \color[HTML]{336666} ,
- InitialValues = \__piton_piton:n ,
- Dict.Value = \__piton_piton:n ,
- Interpol.Inside = \color{black}\__piton_piton:n ,
- TypeParameter = \color[HTML]{008800} \itshape ,
- Identifier = \__piton_identifier:n ,
- Prompt = ,
- ParseAgain.noCR = \__piton_piton_no_cr:n ,
- ParseAgain = \__piton_piton:n ,
+ Comment = \color[HTML]{0099FF} \itshape ,
+ Exception = \color[HTML]{CC0000} ,
+ Keyword = \color[HTML]{006699} \bfseries ,
+ Keyword.Constant = \color[HTML]{006699} \bfseries ,
+ Name.Builtin = \color[HTML]{336666} ,
+ Name.Decorator = \color[HTML]{9999FF},
+ Name.Class = \color[HTML]{00AA88} \bfseries ,
+ Name.Function = \color[HTML]{CC00FF} ,
+ Name.Namespace = \color[HTML]{00CCFF} ,
+ Number = \color[HTML]{FF6600} ,
+ Operator = \color[HTML]{555555} ,
+ Operator.Word = \bfseries ,
+ String = \color[HTML]{CC3300} ,
+ String.Doc = \color[HTML]{CC3300} \itshape ,
+ String.Interpol = \color[HTML]{AA0000} ,
+ Comment.LaTeX = \normalfont \color[rgb]{.468,.532,.6} ,
+ Name.Type = \color[HTML]{336666} ,
+ InitialValues = \__piton_piton:n ,
+ Dict.Value = \__piton_piton:n ,
+ Interpol.Inside = \color{black}\__piton_piton:n ,
+ TypeParameter = \color[HTML]{008800} \itshape ,
+ Identifier = \__piton_identifier:n ,
+ Name.UserFunction = ,
+ 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 } }
+ { \cs_if_exist_use:c { PitonIdentifier _ \l__piton_language_str _ #1 } { #1 } }
\keys_define:nn { PitonOptions }
{ identifiers .code:n = \__piton_set_identifiers:n { #1 } }
\keys_define:nn { Piton / identifiers }
@@ -849,10 +913,28 @@
\clist_map_inline:Nn \l__piton_identifiers_names_tl
{
\tl_set_eq:cN
- { PitonIdentifier ##1 }
+ { PitonIdentifier _ \l__piton_language_str _ ##1 }
\l__piton_style_tl
}
}
+\cs_new_protected:cpn { pitonStyle Name.Function.Internal } #1
+ {
+ { \PitonStyle { Name.Function } { #1 } }
+ \cs_gset_protected:cpn { PitonIdentifier _ \l__piton_language_str _ #1 }
+ { \PitonStyle{ Name.UserFunction } }
+ \seq_if_exist:cF { g__piton_functions _ \l__piton_language_str _ seq }
+ { \seq_new:c { g__piton_functions _ \l__piton_language_str _ seq } }
+ \seq_gput_right:cn { g__piton_functions _ \l__piton_language_str _ seq } { #1 }
+ }
+\NewDocumentCommand \PitonClearUserFunctions { ! O { \l__piton_language_str } }
+ {
+ \seq_if_exist:cT { g__piton_functions _ #1 _ seq }
+ {
+ \seq_map_inline:cn { g__piton_functions _ #1 _ seq }
+ { \cs_undefine:c { PitonIdentifier _ #1 _ ##1} }
+ \seq_gclear:c { g__piton_functions _ #1 _ seq }
+ }
+ }
\AddToHook { env / piton / begin }
{ \msg_fatal:nn { piton } { No~environment~piton } }
@@ -920,12 +1002,20 @@
in~Beamer.\\
If~you~go~on,~that~argument~will~be~ignored.
}
+\msg_new:nnn { Piton } { Python~error }
+ { A~Python~error~has~been~detected. }
\ExplSyntaxOff
\RequirePackage{luacode}
\begin{luacode*}
piton = piton or { }
if piton.comment_latex == nil then piton.comment_latex = ">" end
piton.comment_latex = "#" .. piton.comment_latex
+function piton.open_brace ()
+ tex.sprint("{")
+end
+function piton.close_brace ()
+ tex.sprint("}")
+end
local P, S, V, C, Ct, Cc = lpeg.P, lpeg.S, lpeg.V, lpeg.C, lpeg.Ct, lpeg.Cc
local Cf, Cs , Cg , Cmt , Cb = lpeg.Cf, lpeg.Cs, lpeg.Cg , lpeg.Cmt , lpeg.Cb
local R = lpeg.R
@@ -938,16 +1028,17 @@ end
local function Lc(string)
return Cc ( { luatexbase.catcodetables.expl , string } )
end
-local function K(pattern, style)
- if style
- then
+local function K(style, pattern)
return
Lc ( "{\\PitonStyle{" .. style .. "}{" )
* Q ( pattern )
* Lc ( "}}" )
- else
- return Q ( pattern )
- end
+end
+local function WithStyle(style,pattern)
+ return
+ Ct ( Cc "Open" * Cc ( "{\\PitonStyle{" .. style .. "}{" ) * Cc "}}" )
+ * pattern
+ * Ct ( Cc "Close" )
end
local Escape =
P(piton_begin_escape)
@@ -963,51 +1054,50 @@ local letter = alpha + P "_"
local alphanum = letter + digit
local identifier = letter * alphanum ^ 0
-local Identifier = K ( identifier , 'Identifier' )
+local Identifier = K ( 'Identifier' , identifier)
local Number =
- K (
+ K ( 'Number' ,
( digit^1 * P "." * digit^0 + digit^0 * P "." * digit^1 + digit^1 )
* ( S "eE" * S "+-" ^ -1 * digit^1 ) ^ -1
- + digit^1 ,
- 'Number'
+ + digit^1
)
local Word
if piton_begin_escape ~= ''
-then Word = K ( ( ( 1 - space - P(piton_begin_escape) - P(piton_end_escape) )
+then Word = Q ( ( ( 1 - space - P(piton_begin_escape) - P(piton_end_escape) )
- S "'\"\r[()]" - digit ) ^ 1 )
-else Word = K ( ( ( 1 - space ) - S "'\"\r[()]" - digit ) ^ 1 )
+else Word = Q ( ( ( 1 - space ) - S "'\"\r[()]" - digit ) ^ 1 )
end
-local Space = ( K " " ) ^ 1
+local Space = ( Q " " ) ^ 1
-local SkipSpace = ( K " " ) ^ 0
+local SkipSpace = ( Q " " ) ^ 0
-local Punct = K ( S ".,:;!" )
+local Punct = Q ( S ".,:;!" )
local Tab = P "\t" * Lc ( '\\l__piton_tab_tl' )
-local SpaceIndentation = Lc ( '\\__piton_an_indentation_space:' ) * ( K " " )
-local Delim = K ( S "[()]" )
+local SpaceIndentation = Lc ( '\\__piton_an_indentation_space:' ) * ( Q " " )
+local Delim = Q ( S "[()]" )
local VisualSpace = space * Lc "\\l__piton_space_tl"
local Operator =
- K ( P "!=" + P "<>" + P "==" + P "<<" + P ">>" + P "<=" + P ">=" + P ":="
+ K ( 'Operator' ,
+ 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')
+ K ( 'Operator.Word' ,P "in" + P "is" + P "and" + P "or" + P "not" )
local Keyword =
- K ( P "as" + P "assert" + P "break" + P "case" + P "class" + P "continue"
+ K ( 'Keyword' ,
+ P "as" + P "assert" + P "break" + P "case" + P "class" + P "continue"
+ P "def" + P "del" + P "elif" + P "else" + P "except" + P "exec"
+ P "finally" + P "for" + P "from" + P "global" + P "if" + P "import"
+ P "lambda" + P "non local" + P "pass" + P "return" + P "try"
- + P "while" + P "with" + P "yield" + P "yield from" ,
- 'Keyword' )
- + K ( P "True" + P "False" + P "None" , 'Keyword.Constant' )
+ + P "while" + P "with" + P "yield" + P "yield from" )
+ + K ( 'Keyword.Constant' ,P "True" + P "False" + P "None" )
local Builtin =
- K ( P "__import__" + P "abs" + P "all" + P "any" + P "bin" + P "bool"
+ K ( 'Name.Builtin' ,
+ P "__import__" + P "abs" + P "all" + P "any" + P "bin" + P "bool"
+ P "bytearray" + P "bytes" + P "chr" + P "classmethod" + P "compile"
+ P "complex" + P "delattr" + P "dict" + P "dir" + P "divmod"
+ P "enumerate" + P "eval" + P "filter" + P "float" + P "format"
@@ -1018,11 +1108,11 @@ local Builtin =
+ P "ord" + P "pow" + P "print" + P "property" + P "range" + P "repr"
+ P "reversed" + P "round" + P "set" + P "setattr" + P "slice" + P "sorted"
+ P "staticmethod" + P "str" + P "sum" + P "super" + P "tuple" + P "type"
- + P "vars" + P "zip" ,
- 'Name.Builtin' )
+ + P "vars" + P "zip" )
local Exception =
- K ( P "ArithmeticError" + P "AssertionError" + P "AttributeError"
+ K ( 'Exception' ,
+ 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"
@@ -1041,92 +1131,84 @@ local Exception =
+ P "ConnectionResetError" + P "FileExistsError" + P "FileNotFoundError"
+ P "InterruptedError" + P "IsADirectoryError" + P "NotADirectoryError"
+ P "PermissionError" + P "ProcessLookupError" + P "TimeoutError"
- + P "StopAsyncIteration" + P "ModuleNotFoundError" + P "RecursionError" ,
- 'Exception' )
+ + P "StopAsyncIteration" + P "ModuleNotFoundError" + P "RecursionError" )
-local RaiseException = K ( P "raise" , 'Keyword' ) * SkipSpace * Exception * K ( P "(" )
+local RaiseException = K ( 'Keyword' , P "raise" ) * SkipSpace * Exception * Q ( P "(" )
-local Decorator = K ( P "@" * letter^1 , 'Name.Decorator' )
+local Decorator = K ( 'Name.Decorator' , P "@" * letter^1 )
local DefClass =
- K ( P "class" , 'Keyword' ) * Space * K ( identifier , 'Name.Class' )
+ K ( 'Keyword' , P "class" ) * Space * K ( 'Name.Class' , identifier )
local ImportAs =
- K ( P "import" , 'Keyword' )
+ K ( 'Keyword' , P "import" )
* Space
- * K ( identifier * ( P "." * identifier ) ^ 0 ,
- 'Name.Namespace'
- )
+ * K ( 'Name.Namespace' ,
+ identifier * ( P "." * identifier ) ^ 0 )
* (
- ( Space * K ( P "as" , 'Keyword' ) * Space
- * K ( identifier , 'Name.Namespace' ) )
+ ( Space * K ( 'Keyword' , P "as" ) * Space
+ * K ( 'Name.Namespace' , identifier ) )
+
- ( SkipSpace * K ( P "," ) * SkipSpace
- * K ( identifier , 'Name.Namespace' ) ) ^ 0
+ ( SkipSpace * Q ( P "," ) * SkipSpace
+ * K ( 'Name.Namespace' , identifier ) ) ^ 0
)
local FromImport =
- K ( P "from" , 'Keyword' )
- * Space * K ( identifier , 'Name.Namespace' )
- * Space * K ( P "import" , 'Keyword' )
+ K ( 'Keyword' , P "from" )
+ * Space * K ( 'Name.Namespace' , identifier )
+ * Space * K ( 'Keyword' , P "import" )
local PercentInterpol =
- K ( P "%"
+ K ( 'String.Interpol' ,
+ 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'
+ * S "sdfFeExXorgiGauc%"
)
local SingleShortString =
- Lc ( "{\\PitonStyle{String.Short}{" )
- * (
- K ( P "f'" + P "F'" )
+ WithStyle ( 'String.Short' ,
+ Q ( P "f'" + P "F'" )
* (
- K ( P "{" , 'String.Interpol')
- * K ( ( 1 - S "}':" ) ^ 0 , 'Interpol.Inside' )
- * K ( P ":" * (1 - S "}:'") ^ 0 ) ^ -1
- * K ( P "}" , 'String.Interpol' )
+ K ( 'String.Interpol' , P "{" )
+ * K ( 'Interpol.Inside' , ( 1 - S "}':" ) ^ 0 )
+ * Q ( P ":" * (1 - S "}:'") ^ 0 ) ^ -1
+ * K ( 'String.Interpol' , P "}" )
+
VisualSpace
+
- K ( ( P "\\'" + P "{{" + P "}}" + 1 - S " {}'" ) ^ 1 )
+ Q ( ( P "\\'" + P "{{" + P "}}" + 1 - S " {}'" ) ^ 1 )
) ^ 0
- * K ( P "'" )
+ * Q ( P "'" )
+
- K ( P "'" + P "r'" + P "R'" )
- * ( K ( ( P "\\'" + 1 - S " '\r%" ) ^ 1 )
+ Q ( P "'" + P "r'" + P "R'" )
+ * ( Q ( ( P "\\'" + 1 - S " '\r%" ) ^ 1 )
+ VisualSpace
+ PercentInterpol
- + K ( P "%" )
+ + Q ( P "%" )
) ^ 0
- * K ( P "'" )
- )
- * Lc ( "}}" )
+ * Q ( P "'" ) )
local DoubleShortString =
- Lc ( "{\\PitonStyle{String.Short}{" )
- * (
- K ( P "f\"" + P "F\"" )
+ WithStyle ( 'String.Short' ,
+ Q ( P "f\"" + P "F\"" )
* (
- K ( P "{" , 'String.Interpol' )
- * K ( ( 1 - S "}\":" ) ^ 0 , 'Interpol.Inside' )
- * ( K ( P ":" , 'String.Interpol' ) * K ( (1 - S "}:\"") ^ 0 ) ) ^ -1
- * K ( P "}" , 'String.Interpol' )
+ K ( 'String.Interpol' , P "{" )
+ * Q ( ( 1 - S "}\":" ) ^ 0 , 'Interpol.Inside' )
+ * ( K ( 'String.Interpol' , P ":" ) * Q ( (1 - S "}:\"") ^ 0 ) ) ^ -1
+ * K ( 'String.Interpol' , P "}" )
+
VisualSpace
+
- K ( ( P "\\\"" + P "{{" + P "}}" + 1 - S " {}\"" ) ^ 1 )
+ Q ( ( P "\\\"" + P "{{" + P "}}" + 1 - S " {}\"" ) ^ 1 )
) ^ 0
- * K ( P "\"" )
+ * Q ( P "\"" )
+
- K ( P "\"" + P "r\"" + P "R\"" )
- * ( K ( ( P "\\\"" + 1 - S " \"\r%" ) ^ 1 )
+ Q ( P "\"" + P "r\"" + P "R\"" )
+ * ( Q ( ( P "\\\"" + 1 - S " \"\r%" ) ^ 1 )
+ VisualSpace
+ PercentInterpol
- + K ( P "%" )
+ + Q ( P "%" )
) ^ 0
- * K ( P "\"" )
- )
- * Lc ( "}}" )
+ * Q ( P "\"" ) )
local ShortString = SingleShortString + DoubleShortString
local BalancedBraces =
@@ -1147,31 +1229,59 @@ local BeamerNamesEnvironments =
P "uncoverenv" + P "onlyenv" + P "visibleenv" + P "invisibleenv"
+ P "alertenv" + P "actionenv"
+UserCommands =
+ Ct ( Cc "Open" * C ( "\\emph{" ) * Cc "}" )
+ * ( C ( BalancedBraces ) / (function (s) return MainLoopPython:match(s) end ) )
+ * P "}" * Ct ( Cc "Close" )
+function OneBeamerEnvironment(name)
+ return
+ Ct ( Cc "Open"
+ * C (
+ P ( "\\begin{" .. name .. "}" )
+ * ( P "<" * (1 - P ">") ^ 0 * P ">" ) ^ -1
+ )
+ * Cc ( "\\end{" .. name .. "}" )
+ )
+ * (
+ C ( ( 1 - P ( "\\end{" .. name .. "}" ) ) ^ 0 )
+ / (function (s) return MainLoopPython:match(s) end )
+ )
+ * P ( "\\end{" .. name .. "}" ) * Ct ( Cc "Close" )
+end
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 "}" )
+ Ct ( Cc "Open"
+ * C (
+ (
+ P "\\uncover" + P "\\only" + P "\\alert" + P "\\visible"
+ + P "\\invisible" + P "\\action"
+ )
+ * ( P "<" * (1 - P ">") ^ 0 * P ">" ) ^ -1
+ * P "{"
+ )
+ * Cc "}"
+ )
+ * ( C ( BalancedBraces ) / (function (s) return MainLoopPython:match(s) end ) )
+ * P "}" * Ct ( Cc "Close" )
+ +
+ OneBeamerEnvironment "uncoverenv"
+ + OneBeamerEnvironment "onlyenv"
+ + OneBeamerEnvironment "visibleenv"
+ + OneBeamerEnvironment "invisibleenv"
+ + OneBeamerEnvironment "alertenv"
+ + OneBeamerEnvironment "actionenv"
+
L (
( P "\\alt" )
* P "<" * (1 - P ">") ^ 0 * P ">"
* P "{"
)
- * K ( BalancedBraces , 'ParseAgain.noCR' )
+ * K ( 'ParseAgain.noCR' , BalancedBraces )
* L ( P "}{" )
- * K ( BalancedBraces , 'ParseAgain.noCR' )
+ * K ( 'ParseAgain.noCR' , BalancedBraces )
* L ( P "}" )
+
L (
@@ -1179,11 +1289,11 @@ then
* P "<" * (1 - P ">") ^ 0 * P ">"
* P "{"
)
- * K ( BalancedBraces , 'ParseAgain.noCR' )
+ * K ( 'ParseAgain.noCR' , BalancedBraces )
* L ( P "}{" )
- * K ( BalancedBraces , 'ParseAgain.noCR' )
+ * K ( 'ParseAgain.noCR' , BalancedBraces )
* L ( P "}{" )
- * K ( BalancedBraces , 'ParseAgain.noCR' )
+ * K ( 'ParseAgain.noCR' , BalancedBraces )
* L ( P "}" )
BeamerBeginEnvironments =
( space ^ 0 *
@@ -1201,122 +1311,104 @@ then
) ^ 0
end
local PromptHastyDetection = ( # ( P ">>>" + P "..." ) * Lc ( '\\__piton_prompt:' ) ) ^ -1
-local Prompt = K ( ( ( P ">>>" + P "..." ) * P " " ^ -1 ) ^ -1 , 'Prompt' )
-local EOL
-if piton_beamer
-then
-EOL =
+local Prompt = K ( 'Prompt' , ( ( P ">>>" + P "..." ) * P " " ^ -1 ) ^ -1 )
+local EOL =
P "\r"
*
(
( space^0 * -1 )
+
- Lc ( '\\__piton_end_line:' )
- * BeamerEndEnvironments
- * BeamerBeginEnvironments
- * PromptHastyDetection
- * Lc ( '\\__piton_newline: \\__piton_begin_line:' )
- * Prompt
- )
- *
- SpaceIndentation ^ 0
-else
-EOL =
- P "\r"
- *
- (
- ( space ^ 0 * -1 )
- +
- Lc ( '\\__piton_end_line:' )
- * PromptHastyDetection
- * Lc ( '\\__piton_newline: \\__piton_begin_line:' )
- * Prompt
+ Ct (
+ Cc "EOL"
+ *
+ Ct (
+ Lc "\\__piton_end_line:"
+ * BeamerEndEnvironments
+ * BeamerBeginEnvironments
+ * 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 "'''" )
+ WithStyle ( 'String.Long' ,
+ ( Q ( S "fF" * P "'''" )
* (
- K ( P "{" , 'String.Interpol' )
- * K ( ( 1 - S "}:\r" - P "'''" ) ^ 0 , 'Interpol.Inside' )
- * K ( P ":" * (1 - S "}:\r" - P "'''" ) ^ 0 ) ^ -1
- * K ( P "}" , 'String.Interpol' )
+ K ( 'String.Interpol' , P "{" )
+ * K ( 'Interpol.Inside' , ( 1 - S "}:\r" - P "'''" ) ^ 0 )
+ * Q ( P ":" * (1 - S "}:\r" - P "'''" ) ^ 0 ) ^ -1
+ * K ( 'String.Interpol' , P "}" )
+
- K ( ( 1 - P "'''" - S "{}'\r" ) ^ 1 )
+ Q ( ( 1 - P "'''" - S "{}'\r" ) ^ 1 )
+
- EOL_for_style 'String.Long'
+ EOL
) ^ 0
+
- K ( ( S "rR" ) ^ -1 * P "'''" )
+ Q ( ( S "rR" ) ^ -1 * P "'''" )
* (
- K ( ( 1 - P "'''" - S "\r%" ) ^ 1 )
+ Q ( ( 1 - P "'''" - S "\r%" ) ^ 1 )
+
PercentInterpol
+
P "%"
+
- EOL_for_style 'String.Long'
+ EOL
) ^ 0
)
- * K ( P "'''" )
- * Lc "}}"
+ * Q ( P "'''" ) )
local DoubleLongString =
- Lc "{\\PitonStyle{String.Long}{"
- * (
- K ( S "fF" * P "\"\"\"" )
- * (
- K ( P "{" , 'String.Interpol' )
- * K ( ( 1 - S "}:\r" - P "\"\"\"" ) ^ 0 , 'Interpol.Inside' )
- * K ( P ":" * (1 - S "}:\r" - P "\"\"\"" ) ^ 0 ) ^ -1
- * K ( P "}" , 'String.Interpol' )
- +
- K ( ( 1 - P "\"\"\"" - S "{}\"\r" ) ^ 1 )
- +
- EOL_for_style 'String.Long'
- ) ^ 0
- +
- K ( ( S "rR" ) ^ -1 * P "\"\"\"" )
- * (
- K ( ( 1 - P "\"\"\"" - S "%\r" ) ^ 1 )
- +
- PercentInterpol
- +
- P "%"
- +
- EOL_for_style 'String.Long'
- ) ^ 0
- )
- * K ( P "\"\"\"" )
- * Lc "}}"
+ WithStyle ( 'String.Long' ,
+ (
+ Q ( S "fF" * P "\"\"\"" )
+ * (
+ K ( 'String.Interpol', P "{" )
+ * K ( 'Interpol.Inside' , ( 1 - S "}:\r" - P "\"\"\"" ) ^ 0 )
+ * Q ( P ":" * (1 - S "}:\r" - P "\"\"\"" ) ^ 0 ) ^ -1
+ * K ( 'String.Interpol' , P "}" )
+ +
+ Q ( ( 1 - P "\"\"\"" - S "{}\"\r" ) ^ 1 )
+ +
+ EOL
+ ) ^ 0
+ +
+ Q ( ( S "rR" ) ^ -1 * P "\"\"\"" )
+ * (
+ Q ( ( 1 - P "\"\"\"" - S "%\r" ) ^ 1 )
+ +
+ PercentInterpol
+ +
+ P "%"
+ +
+ EOL
+ ) ^ 0
+ )
+ * Q ( P "\"\"\"" )
+ )
local LongString = SingleLongString + DoubleLongString
local StringDoc =
- K ( P "\"\"\"" , 'String.Doc' )
- * ( K ( (1 - P "\"\"\"" - P "\r" ) ^ 0 , 'String.Doc' ) * EOL
+ K ( 'String.Doc' , P "\"\"\"" )
+ * ( K ( 'String.Doc' , (1 - P "\"\"\"" - P "\r" ) ^ 0 ) * EOL
* Tab ^ 0
) ^ 0
- * K ( ( 1 - P "\"\"\"" - P "\r" ) ^ 0 * P "\"\"\"" , 'String.Doc' )
+ * K ( 'String.Doc' , ( 1 - P "\"\"\"" - P "\r" ) ^ 0 * P "\"\"\"" )
local CommentMath =
- P "$" * K ( ( 1 - S "$\r" ) ^ 1 , 'Comment.Math' ) * P "$"
+ P "$" * K ( 'Comment.Math' , ( 1 - S "$\r" ) ^ 1 ) * P "$"
local Comment =
- Lc ( "{\\PitonStyle{Comment}{" )
- * K ( P "#" )
- * ( CommentMath + K ( ( 1 - S "$\r" ) ^ 1 ) ) ^ 0
- * Lc ( "}}" )
+ WithStyle ( 'Comment' ,
+ Q ( P "#" )
+ * ( CommentMath + Q ( ( 1 - S "$\r" ) ^ 1 ) ) ^ 0 )
* ( EOL + -1 )
local CommentLaTeX =
P(piton.comment_latex)
* Lc "{\\PitonStyle{Comment.LaTeX}{\\ignorespaces"
* L ( ( 1 - P "\r" ) ^ 0 )
* Lc "}}"
- * ( EOL + -1 )
+ * ( EOL + -1 ) -- you could put EOL instead of EOL
local Expression =
P { "E" ,
E = ( 1 - S "{}()[]\r," ) ^ 0
@@ -1337,20 +1429,20 @@ local Expression =
local Param =
SkipSpace * Identifier * SkipSpace
* (
- K ( P "=" * Expression , 'InitialValues' )
- + K ( P ":" ) * SkipSpace * K ( letter^1 , 'Name.Type' )
+ K ( 'InitialValues' , P "=" * Expression )
+ + Q ( P ":" ) * SkipSpace * K ( 'Name.Type' , letter^1 )
) ^ -1
-local Params = ( Param * ( K "," * Param ) ^ 0 ) ^ -1
+local Params = ( Param * ( Q "," * Param ) ^ 0 ) ^ -1
local DefFunction =
- K ( P "def" , 'Keyword' )
+ K ( 'Keyword' , P "def" )
* Space
- * K ( identifier , 'Name.Function' )
+ * K ( 'Name.Function.Internal' , identifier )
* SkipSpace
- * K ( P "(" ) * Params * K ( P ")" )
+ * Q ( P "(" ) * Params * Q ( P ")" )
* SkipSpace
- * ( K ( P "->" ) * SkipSpace * K ( identifier , 'Name.Type' ) ) ^ -1
- * K ( ( 1 - S ":\r" )^0 , 'ParseAgain' )
- * K ( P ":" )
+ * ( Q ( P "->" ) * SkipSpace * K ( 'Name.Type' , identifier ) ) ^ -1
+ * K ( 'ParseAgain' , ( 1 - S ":\r" )^0 )
+ * Q ( P ":" )
* ( SkipSpace
* ( EOL + CommentLaTeX + Comment ) -- in all cases, that contains an EOL
* Tab ^ 0
@@ -1358,16 +1450,16 @@ local DefFunction =
* StringDoc ^ 0 -- there may be additionnal docstrings
) ^ -1
local ItemDict =
- ShortString * SkipSpace * K ( P ":" ) * K ( Expression , 'Dict.Value' )
+ ShortString * SkipSpace * Q ( P ":" ) * K ( 'Dict.Value' , Expression )
local ItemOfSet = SkipSpace * ( ItemDict + ShortString ) * SkipSpace
local Set =
- K ( P "{" )
- * ItemOfSet * ( K ( P "," ) * ItemOfSet ) ^ 0
- * K ( P "}" )
-local ExceptionInConsole = Exception * K ( ( 1 - P "\r" ) ^ 0 ) * EOL
-MainLoop =
+ Q ( P "{" )
+ * ItemOfSet * ( Q ( P "," ) * ItemOfSet ) ^ 0
+ * Q ( P "}" )
+local ExceptionInConsole = Exception * Q ( ( 1 - P "\r" ) ^ 0 ) * EOL
+MainLoopPython =
( ( space^1 * -1 )
+ EOL
+ Space
@@ -1375,6 +1467,7 @@ MainLoop =
+ Escape
+ CommentLaTeX
+ Beamer
+ + UserCommands
+ LongString
+ Comment
+ ExceptionInConsole
@@ -1387,10 +1480,10 @@ MainLoop =
+ RaiseException
+ DefFunction
+ DefClass
- + Keyword * ( Space + Punct + Delim + EOL + -1 )
+ + Keyword * ( Space + Punct + Delim + EOL+ -1 )
+ Decorator
- + OperatorWord * ( Space + Punct + Delim + EOL + -1 )
- + Builtin * ( Space + Punct + Delim + EOL + -1 )
+ + OperatorWord * ( Space + Punct + Delim + EOL+ -1 )
+ + Builtin * ( Space + Punct + Delim + EOL+ -1 )
+ Identifier
+ Number
+ Word
@@ -1402,49 +1495,50 @@ python =
( ( space - P "\r" ) ^0 * P "\r" ) ^ -1
* BeamerBeginEnvironments
* PromptHastyDetection
- * Lc ( '\\__piton_begin_line:' )
+ * Lc '\\__piton_begin_line:'
* Prompt
* SpaceIndentation ^ 0
- * MainLoop
+ * MainLoopPython
* -1
- * Lc ( '\\__piton_end_line:' )
+ * Lc '\\__piton_end_line:'
)
local languages = { }
languages['python'] = python
-local Punct = K ( S ",:;!" )
+local Delim = Q ( P "[|" + P "|]" + S "[()]" )
+local Punct = Q ( S ",:;!" )
local identifier =
( R "az" + R "AZ" + P "_") * ( R "az" + R "AZ" + S "_'" + digit ) ^ 0
-local Identifier = K ( identifier )
+local Identifier = K ( 'Identifier' , identifier )
local Operator =
- K ( P "!=" + P "<>" + P "==" + P "<<" + P ">>" + P "<=" + P ">=" + P ":="
+ K ( 'Operator' ,
+ 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')
+ K ( 'Operator.Word' ,
+ P "and" + P "asr" + P "land" + P "lor" + P "lsl" + P "lxor"
+ + P "mod" + P "or" )
local Keyword =
- K ( P "as" + P "assert" + P "begin" + P "class" + P "constraint" + P "done"
+ K ( 'Keyword' ,
+ 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' )
+ + P "value" + P "val" + P "virtual" + P "when" + P "while" + P "with" )
+ + K ( 'Keyword.Constant' , P "true" + P "false" )
local Builtin =
- K ( P "not" + P "incr" + P "decr" + P "fst" + P "snd"
+ K ( 'Name.Builtin' ,
+ 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"
@@ -1456,16 +1550,15 @@ local Builtin =
+ 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' )
+ + P "Hashtbl.iter" )
local Exception =
- K ( P "Division_by_zero" + P "End_of_File" + P "Failure"
+ K ( 'Exception' ,
+ 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' )
+ + P "Sys_error" + P "Undefined_recursive_module" )
local Char =
- K ( P "'" * ( ( 1 - P "'" ) ^ 0 + P "\\'" ) * P "'" , 'String.Short' )
+ K ( 'String.Short' , P "'" * ( ( 1 - P "'" ) ^ 0 + P "\\'" ) * P "'" )
local BalancedBraces =
P { "E" ,
E =
@@ -1491,7 +1584,7 @@ then
)
*
L ( ( P "<" * (1 - P ">") ^ 0 * P ">" ) ^ -1 * P "{" )
- * K ( BalancedBraces , 'ParseAgain.noCR' )
+ * K ( 'ParseAgain.noCR' , BalancedBraces )
* L ( P "}" )
+
L (
@@ -1499,9 +1592,9 @@ then
* P "<" * (1 - P ">") ^ 0 * P ">"
* P "{"
)
- * K ( BalancedBraces , 'ParseAgain.noCR' )
+ * K ( 'ParseAgain.noCR' , BalancedBraces )
* L ( P "}{" )
- * K ( BalancedBraces , 'ParseAgain.noCR' )
+ * K ( 'ParseAgain.noCR' , BalancedBraces )
* L ( P "}" )
+
L (
@@ -1509,11 +1602,11 @@ then
* P "<" * (1 - P ">") ^ 0 * P ">"
* P "{"
)
- * K ( BalancedBraces , 'ParseAgain.noCR' )
+ * K ( 'ParseAgain.noCR' , BalancedBraces )
* L ( P "}{" )
- * K ( BalancedBraces , 'ParseAgain.noCR' )
+ * K ( 'ParseAgain.noCR' , BalancedBraces )
* L ( P "}{" )
- * K ( BalancedBraces , 'ParseAgain.noCR' )
+ * K ( 'ParseAgain.noCR' , BalancedBraces )
* L ( P "}" )
BeamerBeginEnvironments =
( space ^ 0 *
@@ -1530,50 +1623,38 @@ then
* P "\r"
) ^ 0
end
-local EOL
-if piton_beamer
-then
-EOL =
+local 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:' )
+ Ct (
+ Cc "EOL"
+ *
+ Ct (
+ Lc "\\__piton_end_line:"
+ * BeamerEndEnvironments
+ * BeamerBeginEnvironments
+ * PromptHastyDetection
+ * Lc "\\__piton_newline: \\__piton_begin_line:"
+ * Prompt
+ )
+ )
)
*
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 string =
+ Q ( P "\"" )
+ * (
+ VisualSpace
+ +
+ Q ( ( 1 - S " \"\r" ) ^ 1 )
+ +
+ EOL
+ ) ^ 0
+ * Q ( P "\"" )
+local String = WithStyle ( 'String.Long' , string )
local ext = ( R "az" + P "_" ) ^ 0
local open = "{" * Cg(ext, 'init') * "|"
local close = "|" * C(ext) * "}"
@@ -1581,42 +1662,39 @@ local closeeq =
Cmt ( close * Cb('init'),
function (s, i, a, b) return a==b end )
local QuotedStringBis =
- Lc "{\\PitonStyle{String.Long}{"
- *
- (
+ WithStyle ( 'String.Long' ,
+ (
VisualSpace
+
- K ( ( 1 - S " \r" ) ^ 1 )
+ Q ( ( 1 - S " \r" ) ^ 1 )
+
- EOL_for_style 'String.Long'
- ) ^ 0
- * Lc "}}"
+ EOL
+ ) ^ 0 )
+
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 "}}"
+ WithStyle ( 'Comment' ,
+ P {
+ "A" ,
+ A = Q "(*"
+ * ( V "A"
+ + Q ( ( 1 - P "(*" - P "*)" - S "\r$\"" ) ^ 1 ) -- $
+ + string
+ + P "$" * K ( 'Comment.Math' , ( 1 - S "$\r" ) ^ 1 ) * P "$" -- $
+ + EOL
+ ) ^ 0
+ * Q "*)"
+ } )
local DefFunction =
- ( K ( P "let rec" + P "let" + P "and" , 'Keyword' ) )
+ K ( 'Keyword' , P "let rec" + P "let" + P "and" )
* Space
- * K ( identifier , 'Name.Function' )
+ * K ( 'Name.Function.Internal' , identifier )
* Space
- * # ( 1 - P "=" )
-local TypeParameter = K ( P "'" * alpha * # ( 1 - P "'") , 'TypeParameter' )
-MainLoop =
+ * # ( P "=" * space * P "function" + ( 1 - P "=" ) )
+local TypeParameter = K ( 'TypeParameter' , P "'" * alpha * # ( 1 - P "'" ) )
+MainLoopOCaml =
( ( space^1 * -1 )
+ EOL
+ Space
@@ -1648,16 +1726,46 @@ ocaml =
* BeamerBeginEnvironments
* Lc ( '\\__piton_begin_line:' )
* SpaceIndentation ^ 0
- * MainLoop
+ * MainLoopOCaml
* -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
+ local left_stack = {}
+ local right_stack = {}
+ for _ , one_item in ipairs(t)
+ do
+ if one_item[1] == "EOL"
+ then
+ for _ , s in ipairs(right_stack)
+ do tex.sprint( s )
+ end
+ for _ , s in ipairs(one_item[2])
+ do tex.tprint(s)
+ end
+ for _ , s in ipairs(left_stack)
+ do tex.sprint( s )
+ end
+ else
+ if one_item[1] == "Open"
+ then
+ tex.sprint( one_item[2] )
+ table.insert(left_stack,one_item[2])
+ table.insert(right_stack,one_item[3])
+ else
+ if one_item[1] == "Close"
+ then
+ tex.sprint( right_stack[#right_stack] )
+ left_stack[#left_stack] = nil
+ right_stack[#right_stack] = nil
+ else
+ tex.tprint(one_item)
+ end
+ end
+ end
+ end
end
function piton.ParseFile(language,name,first_line,last_line)
s = ''