On Wed, Jan 18, 2023 at 11:52:33AM +0100, Patrice Dumas wrote: > On Tue, Jan 17, 2023 at 09:26:54PM +0000, Gavin Smith wrote: > > Disclaimer: this message is not immediately practical. > > > > One thing I've noticed is that direct nesting of @ref commands is diagnosed, > > but indirect nesting is not: > > Indeed, this is a long-standing issue in texi2any, and should be, > nowadays relatvely easy to implement because there are commands stacks. > The reason why I did not start working on it is that I fear that it will > have an impact on the performance, as it would mean that for each > command, the stack of commands would be checked. I am not completely > convinced that the performance penalty is worth it.
I don't see why it should be a huge performance barrier. Suppose we want to forbid nested @footnote; then we would have a hash like $self->{'nesting_context'} equal to {'footnote' => 0}, then when we enter a @footnote, we increment $self->{'nesting_context'}->{'footnote'}, and decrement it on leaving. Whenever we see a @footnote, we check that $self->{'nesting_context'}->{'footnote'} is equal to zero. This approach would also work for nested cross-references. It would not require traversing the list of currently open commands for every command. For places where @anchor should be forbidden, we would use a counter like $self->{'nesting_context'}->{'simple_text'} to mark that @anchor is excluded throughout. Increment it on entering a @chapter argument and decrement it at the end. If there are other contexts where only a restricted subset of Texinfo commands is valid, these could expressed with similar counters. Obviously I haven't tried to implement this, so there would likely be complications. I would be willing to work on the XS parser code for this, as I have time, if it is agreed it is a good idea.