Fix incorrect handling of user-defined environments introduced in #856 (#1137) #1138
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes the problem with user-defined environments that was reported in #1137 that was introduced by PR #856, that was trying to fix a potential infinite recursion. It changed how the environments end macros were processed by inserting them into the parser string followed by second
\end{...}
macro. Aparser.stack.env
value (processing
) was used to track when the second\end{...}
was processed and skip the insertion of the end macros.The problem with that is that the environment where
processing
was added might be cleared by the ed macros. For the example in #1137, the\begin{array}
in the begin macros and the\end{array}
in the end macros for theboxed
environment means that theprocessing
environment value is in the environment in effect within thearray
environment, and that environment is ended and removed when\end{array}
is processed. So theprocessing
value is also lost, and the end macros are inserted again, leading to a new\hline
being inserted outside thearray
environment, and the associated error about a misplaced\hline
.Because of this, and other similar situations, the
parser.stack.env
value can't be used to track the second\end{...}
. So I've gone back to the original process of parsing the end macros, but needed a new way to avoid the potential recursion that #856 was meant to fix.The infinite recursion was possible when the end macros included
\end{...}
for the user-defined environment. This was a problem because when processing the end macros that contain\end{...}
that\end
would cause the end macros to be inserted again, and then that is related over and over. This is because the testing for a matching\begin{...}
for the\end{...}
isn't done until after the end macros are processed (so that the user-defined environment can include\begin
and\end
as in the example from #1137), when theend
stack item is pushed.The solution introduced here is use the
beginEnv
stack items to form a linked list of the open (user-defined) environments, with the the top-mostbeginEnv
stack item recorded in aparser.stack.global
variable. We only insert the end macro string if there is an activebeginEnv
on the stack, and we remove that from the linked list once we add the end macros the first time. That way, we only add the end macros once for any\begin{...}
that is on the stack, so even if the end macros include\end{...}
, they can't be inserted infinitely, and eventually we get theend
stack item being pushed, producing the mismatched begin/end message.This allows
and also
to work, while having
produce an appropriate error message.
Other test cases include
that should work while
and
and
and
and
should all produce errors.
The definitions
should produce
x...yxy
,while
should error.