I have previosly written about speculative execution in the lexer. To exactly reproduce the behavior of the image links, not only one, but two speculations will be necessary. However, this is very complex and the use case is undocumented so I would like to simplify these.
The original beheviour is as follows: the option list is split on the '|' character; the caption is the _last_ non-option in the list, if any.
So, to reproduce this, a separate speculation has to be initiated for the caption. If another caption (non-option) is seen in the list, the speculation will fail.
Furthermore, media link may nest one level. If a MEDIA_LINK or INTERNAL_LINK appears in the caption of the second level, the production will completely fail.
I think that the following is a reasonable simplification: image links may not nest (although internal links and external links may appear in the caption of a media link), The _first_ non-option in the list is the caption and no options may appear after the caption. In this way, only one speculation is required for media links, and the lexer can handle the option list. This behavior seems consistent with the documentation at http://www.mediawiki.org/wiki/Help:Images.
Is there any known use for putting an image inside an image caption, or is the restriction I propose here sufficient?
Best regards,
Andreas Jonsson