Looking at code from HOP::Lexer I found some interesting things. First an example (most of the code is the same from original post, I only change the data and lexer rules):

use strict; my $sql = <<'--SQL--'; aaaa a baaaab a --SQL-- use HOP::Lexer 'make_lexer'; my @sql = $sql; my $lexer = make_lexer( sub { shift @sql }, # iterator [ A => qr/a+/i ], [ BAB => qr/ba+b/i ], [ SPACE => qr/\s+/, sub {} ], );

This gives us:

['A','aaaa'], ['A','a'], 'b', ['A','aaaa'], 'b', ['A','a']
which seems wrong but: note that the rule A matches everytime the rule B matches (not exactly the same match but both match something) and, here's the surprise, HOP::Lexer uses split instead of matching the start of the string. This makes sense because you can have garbage or non-matched data at the start of the buffer, e.g. in original post example there's = which isn't matched by any rule.

Now it's easy to see why the rules work like that, for example with:

[ WORD => qr/\w+/i ], [ DQWORD => qr/"\w+"/ ],

So, considering that split is used and WORD has precedence, always will happen that " will be considered what I called garbage. And that's why giving higher priority to DQWORD works (as I replied in the thread), because otherwise WORD would match the \w+ inside of the double quoted one.

As a rule of thumb: if a rule has other rule inside it, give it higher priority.


In reply to Re: HOP::Lexer not doing what I expected by cmarcelo
in thread HOP::Lexer not doing what I expected by bart

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.