What I want to do is replace {...} (token group) by \{...\} (curly braces) at any level of nesting inside an expression. I'm only able to do it for level = 0. There should be an extra curly brace around the second 'o' in the example given. Any suggestion?
\documentclass{article}
\ExplSyntaxOn
\cs_new:Nn
__erw_unbrace_scan:n
{ %\group_begin:
__erw_scan:n#1
%\group_end:
}
\cs_new:Nn
__erw_aux:n
{ \tl_if_single_token:nTF{#1}{#1}
{ {__erw_unbrace_scan:n{#1}}}}
\cs_new:Npn
__erw_scan:w #1 #2 #3 \q_recursion_stop
{\quark_if_recursion_tail_stop:n{#2}
__erw_aux:n{#1}
__erw_scan:w {#2} #3 \q_recursion_stop}
\cs_new:Nn
__erw_scan:n
{__erw_scan:w #1 {dummy}\q_recursion_tail\q_recursion_stop}
\ExplSyntaxOff
\begin{document}
\ExplSyntaxOn
\tl_if_single_token:nTF{x}{T}{F}\par
\tl_if_single_token:nTF{{x}}{T}{F}\par
__erw_scan:n{f{o{o}}bar} % expected: f{o{o}}bar
\ExplSyntaxOff
\end{document}




