Future of scScript Patterns
The current Match algorithm has a very hard time going backwards in the text buffer. If a
particular Pat starts at the end of the buffer and jumps (perhaps N chars/lines) backwards,
it will eventually go past the beginning of the buffer (or permissible area) and fail. But
Match is not smart enough to just stop, instead it advances +1 position from
the previous start and tries again, and again.... just as it would for a forward match
going through a gap area.
While not quite as dramatic, mindlessly advancing +1 position can also be
inefficient for forward matches. If the particular word does not match, it makes no sense
to advance +1 char into the word and try again. It would be much better
to jump directly to the next word instead. The same logic may apply to lines or paragraphs,
depending on the particular pattern definitions.
There are a few possible solutions, but the most appealing general one is to also provide a
FailMFunc. The regular MFunc (if provided) is normally called
only when the Pat matches. It tells the Match apparatus whether to continue and
where to go next. A similar FailMFunc could perform that function when the Pat
does not match. So instead of mindlessly advancing +1 in search of the
next match, it could advance backwards or even jump to the next word or line. This
mechanism can enable proper backwards traversals, but also make forward matching more
efficient. Given the more advanced functionality, it might be best to limit its use to a
new RMatch function, one that takes an extra FailMFunc arg right
after MFunc.
(An expedient step would be to supply a #SkipWord option for the current Match!)