in reply to HOP::Lexer not doing what I expected
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:
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.['A','aaaa'], ['A','a'], 'b', ['A','aaaa'], 'b', ['A','a']
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: HOP::Lexer not doing what I expected
by bart (Canon) on Nov 11, 2006 at 22:48 UTC | |
by cmarcelo (Scribe) on Nov 12, 2006 at 00:49 UTC |