% This file is only intended to be input by Tcode_pycode.tex

%%%   Look for string prefixes and handle them   %%%

%Have these defined to anything. To check whether a word is any of L, u, u8 or U.
%We take advantage and define them to the format we want for each case.
%local-global in oder to avoid \csFormat defining anything
{
\xdef\FormatStrpyDefault{\csFormat{String}} %The default, for strings without prefix and with r prefix.
\xdef\StrpyPrefix@r{\csFormat{RawPrefix}}			\xdef\StrpyPrefix@R{\csFormat{RawPrefix}}
\xdef\StrpyPrefix@u{\csFormat{WidePrefix}}		\xdef\StrpyPrefix@U{\csFormat{WidePrefix}}
\xdef\StrpyPrefix@f{\csFormat{FormattedPrefix}}	\xdef\StrpyPrefix@F{\csFormat{FormattedPrefix}}
\xdef\StrpyPrefix@rf{\csFormat{FormattedPrefix}}	\xdef\StrpyPrefix@Rf{\csFormat{FormattedPrefix}}
\xdef\StrpyPrefix@rF{\csFormat{FormattedPrefix}}	\xdef\StrpyPrefix@RF{\csFormat{FormattedPrefix}}
\xdef\StrpyPrefix@fr{\csFormat{FormattedPrefix}}	\xdef\StrpyPrefix@fR{\csFormat{FormattedPrefix}}
\xdef\StrpyPrefix@Fr{\csFormat{FormattedPrefix}}	\xdef\StrpyPrefix@FR{\csFormat{FormattedPrefix}}
\xdef\StrpyPrefix@b{\csFormat{BytesPrefix}}		\xdef\StrpyPrefix@B{\csFormat{BytesPrefix}}
\xdef\StrpyPrefix@rb{\csFormat{BytesPrefix}}		\xdef\StrpyPrefix@Rb{\csFormat{BytesPrefix}}
\xdef\StrpyPrefix@rB{\csFormat{BytesPrefix}}		\xdef\StrpyPrefix@RB{\csFormat{BytesPrefix}}
\xdef\StrpyPrefix@br{\csFormat{BytesPrefix}}		\xdef\StrpyPrefix@bR{\csFormat{BytesPrefix}}
\xdef\StrpyPrefix@Br{\csFormat{BytesPrefix}}		\xdef\StrpyPrefix@BR{\csFormat{BytesPrefix}}
\xdef\StrpyPrefix@t{\csFormat{TemplatePrefix}}		\xdef\StrpyPrefix@T{\csFormat{TemplatePrefix}}
\xdef\StrpyPrefix@rt{\csFormat{TemplatePrefix}}	\xdef\StrpyPrefix@Rt{\csFormat{TemplatePrefix}}
\xdef\StrpyPrefix@rT{\csFormat{TemplatePrefix}}	\xdef\StrpyPrefix@RT{\csFormat{TemplatePrefix}}
\xdef\StrpyPrefix@tr{\csFormat{TemplatePrefix}}	\xdef\StrpyPrefix@tR{\csFormat{TemplatePrefix}}
\xdef\StrpyPrefix@Tr{\csFormat{TemplatePrefix}}	\xdef\StrpyPrefix@TR{\csFormat{TemplatePrefix}}
%Define the format of the string corresponding with each prefix
\xdef\py@NextString@r{\csFormat{String}}				\xdef\py@NextString@R{\csFormat{String}}
\xdef\py@NextString@u{\csFormat{StringWide}}		\xdef\py@NextString@U{\csFormat{StringWide}}
\xdef\py@NextString@f{\csFormat{StringFormatted}}		\xdef\py@NextString@F{\csFormat{StringFormatted}}
\xdef\py@NextString@rf{\csFormat{StringFormatted}}	\xdef\py@NextString@Rf{\csFormat{StringFormatted}}
\xdef\py@NextString@rF{\csFormat{StringFormatted}}	\xdef\py@NextString@RF{\csFormat{StringFormatted}}
\xdef\py@NextString@fr{\csFormat{StringFormatted}}	\xdef\py@NextString@fR{\csFormat{StringFormatted}}
\xdef\py@NextString@Fr{\csFormat{StringFormatted}}	\xdef\py@NextString@FR{\csFormat{StringFormatted}}
\xdef\py@NextString@b{\csFormat{StringBytes}}		\xdef\py@NextString@B{\csFormat{StringBytes}}
\xdef\py@NextString@rb{\csFormat{StringBytes}}		\xdef\py@NextString@Rb{\csFormat{StringBytes}}
\xdef\py@NextString@rB{\csFormat{StringBytes}}		\xdef\py@NextString@RB{\csFormat{StringBytes}}
\xdef\py@NextString@br{\csFormat{StringBytes}}		\xdef\py@NextString@bR{\csFormat{StringBytes}}
\xdef\py@NextString@Br{\csFormat{StringBytes}}		\xdef\py@NextString@BR{\csFormat{StringBytes}}
\xdef\py@NextString@t{\csFormat{StringTemplate}}		\xdef\py@NextString@T{\csFormat{StringTemplate}}
\xdef\py@NextString@rt{\csFormat{StringTemplate}}		\xdef\py@NextString@Rt{\csFormat{StringTemplate}}
\xdef\py@NextString@rT{\csFormat{StringTemplate}}	\xdef\py@NextString@RT{\csFormat{StringTemplate}}
\xdef\py@NextString@tr{\csFormat{StringTemplate}}		\xdef\py@NextString@tR{\csFormat{StringTemplate}}
\xdef\py@NextString@Tr{\csFormat{StringTemplate}}	\xdef\py@NextString@TR{\csFormat{StringTemplate}}
}
%Those that handle {} within the string
\let\py@FormatInString@f\relax	\let\py@FormatInString@F\relax
\let\py@FormatInString@t\relax	\let\py@FormatInString@T\relax
\let\py@FormatInString@fr\relax	\let\py@FormatInString@fR\relax	\let\py@FormatInString@Fr\relax	\let\py@FormatInString@FR\relax
\let\py@FormatInString@rf\relax	\let\py@FormatInString@Rf\relax	\let\py@FormatInString@rF\relax	\let\py@FormatInString@RF\relax
\let\py@FormatInString@tr\relax	\let\py@FormatInString@tR\relax	\let\py@FormatInString@Tr\relax	\let\py@FormatInString@TR\relax
\let\py@FormatInString@rt\relax	\let\py@FormatInString@Rt\relax	\let\py@FormatInString@rT\relax	\let\py@FormatInString@RT\relax

\Tcodepy@Reload@S{
  \let\FormatNextString\FormatStrpyDefault
  \let\ifStrPrefix\iffalse

  \def\StrPrefixTasks{\let\ifStrPrefix\iffalse\exP\PrePrintStrPrefixHook} %Put things here so that inside the hook there is just one token
  \def\PrePrintStrPrefixHook{
    \ifcsname py@FormatInString@\the\WordToPrint\endcsname \let\SetupStrpyCodes\SetupFormattedpyString\fi
    \PrePrintHook
  }
}
%\the\Tcodepy@Reload@S %This goes in Tcode_pycode.tex

% String setup and { } escape.
\def\SetupNormalStrpyCodes{\catcode`\{=12 \catcode`\}=12 }
\def\restoreSetupStrpyCodes{\let\SetupStrpyCodes\SetupNormalStrpyCodes}
\restoreSetupStrpyCodes

{
\catcode`\{=12 \catcode`\}=12 \catcode`\[=1 \catcode`\]=2
\xdef\temp#1}[[\csFormat[FormatInString]{#1}]] % Consume the argument up to, and including, }
%We do not allow nested {}. It could be done by making { and } modify a counter and resetting the format
] % to \FormatString when a } puts the counter to zero.
\let\OpenBraceInpyString\temp
{
\catcode`\{=13 \catcode`\[=1
\gdef\temp[\aftergroup\restoreSetupStrpyCodes %Restore the normal behaviour
   \catcode`{=\active \let{=\OpenBraceInpyString \catcode`\}=\cat@xii}
}
\let\SetupFormattedpyString\temp

% Strings
\def\pyStringSimple{\bgroup\SetupStrpyCodes\futurelet\firstchar\pyDoStringS} %If the first char is ', we'll check for a third '.
\def\pyStringDouble{\bgroup\SetupStrpyCodes\futurelet\firstchar\pyDoStringD} %If the first char is ", we'll check for a third ".
\def\pyEndString{\egroup\let\FormatNextString\FormatStrpyDefault\@parsecode}
\def\pyDoStringS#1'{\SetEscapeChars\FormatNextString '#1'\ifx\firstchar'\exP\pyCheckThreeS \else \exP\pyEndString\fi}
\def\pyDoStringD#1"{\SetEscapeChars\FormatNextString "#1"\ifx\firstchar"\exP\pyCheckThreeD \else \exP\pyEndString\fi}
\def\pyCheckThreeS#1{\ifx'#1\exP\pyDoStrSThree\else\exP\pyEndString\fi #1}%The first two '' have already been printed. The setup is still active
\def\pyDoStrSThree'#1'''{'#1'''\pyEndString}
\def\pyCheckThreeD#1{\ifx"#1\exP\pyDoStrDThree\else\exP\pyEndString\fi #1}
\def\pyDoStrDThree"#1"""{"#1"""\pyEndString}

%%%   Strings in Code  %%%

\catcode`\^^M=13 %

% Typically, a ^^M or \^^M does not appear inside a string. For this reason we defer the somewhat long setup for
%the hypothetical next line til we meet an actual ^^M or \^^M. The last ^^M here will use the original definition, and the other gets forgotten
\def\EOLinpyString{\xdef\CContinueInTheMiddle{\expandonce\SetupStrpyCodes\expandonce\FormatNextString}\egroup\bgroup^^M}
\def\CodepyDoStringSimple#1'{\let^^M\EOLinpyString\FormatNextString '#1'\ifx\firstchar'\exP\pyCheckThreeS \else \exP\pyEndString\fi}
\def\CodepyDoStringDouble#1"{\let^^M\EOLinpyString\FormatNextString '#1'\ifx\firstchar"\exP\pyCheckThreeD \else \exP\pyEndString\fi}

\catcode`\^^M=5 %
