Good points.

It's just that when balancing elegance & performance & dependencies, in some cases I end up favouring the "one big regex" approach - assuming I can work around its technical limitations.

For example, in one current use-case I want to search a large input stream for occurrences of any of several "things", and extract relevant information from each match. This is not exactly a "parser" use-case (since I don't care about most of the input stream, and don't want to build a unified AST from it) but it shares some similarities.

In my prototype design (which seems to be working out fine so far), I do it by defining an array entry for each "thing" like so:

my @things = ( { parse => qr/ regex0 /sx, action => sub { ... } }, { parse => qr/ regex1 /sx, action => sub { ... } }, ... );

And then when the program starts, it dynamically compiles all of those individual regexes into a big branching one of the form:

qr{ (?| regex0 (*MARK:0) | regex1 (*MARK:1) | regex2 (*MARK:2) | ... ) }sx

...and starts matching this big regex against the input stream using a sliding window approach inspired by this old post by BrowserUk.

And whenever a match is found, it calls the corresponding action function roughly like so:

$things[$main::REGMARK]{action}->();

This approach is attractive to me because:

Each branch/thing can have its own capture groups etc, and process them in its action function.
However, if their parsing regexes need more complex quantified parts, then the advanced techniques I described above become necessary in order to be able to keep this design.

If those techniques were built-in instead of requiring scary embedded code blocks, I think I would use this kind of approach more often.


In reply to Re^2: Advanced techniques with regex quantifiers [example use-case] by smls
in thread Advanced techniques with regex quantifiers by smls

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.