In TeX, various kinds of groups must match properly, for instance, the following is wrong:
$ \begingroup $ \endgroup
I would like macros which allow such mismatched grouping. They would keep certain marked assignments "local" to their grouping. For instance,
% Define \openA, \openB, \localA, \localB.
\def\foo{}
\def\baz{}
\openA
\localA\def\foo{Hello}
\openB
\localB\def\baz{Hello}
\message{\foo,\baz} % "Hello,Hello"
\closeA
\message{\foo,\baz} % ",Hello"
\closeB
\message{\foo,\baz} % ","
I am open to variations on the precise syntax, and to a solution in any engine with any package. Try be as close as possible to supporting all assignments.
EDIT: What I really want is a way of keeping some assignments local to groups which are independent of TeX's own. It just seemed easier to phrase the question in this way, and I think that solutions to both questions are identical.
To make things concrete, imagine that I want to convert (*-abc*+def(*?gh(i*-jk)lm)no) into a-b-c-d+e+f+g?h?i?j-k-l?m?n+o+. The rule would be that * defines the next character as the "current character", locally to parenthesized groups, and that the "current character" should be inserted after each other normal character. The easiest way to do that is to use TeX's grouping to keep the value of the "current character" local. For instance,
\def\convert#1{%
\def\storage{}%
\def\currentchar{}%
\readone#1\relax
}
\def\readone#1{%
\global\let\next\readone
\ifx#1\relax \global\let\next\relax
\else
\ifx#1(\begingroup
\else
\ifx#1)\endgroup
\else
\ifx#1*\let\next\star
\else \xdef\storage{\storage #1\currentchar}%
\fi
\fi
\fi
\fi
\next
}
The problem with that approach is that \storage must be modified globally, since the assignments happen within TeX's groups. In that particular application, it is not a problem. In my application (to the construction of an automaton for regular expression matching), I would end up doing global assignments to arbitrary token registers, which is very bad. Two solutions: either carry the value of \storage (all token registers) past each \endgroup, or restore the \currentchar at each \endgroup. In my true application, that second option is probably better, hence my question.