Indeed, my rule of thumb isn't that good after all :-(. This snippet illustrate what you said about string vs. comment:

use strict; my $sql = <<'--SQL--'; # this is a comment $a = "simple string"; $b = "string and comment"; # comment $c = "string with # thing"; # comment with "string" $d = "very hard" # dasdas . "to do"; --SQL-- use HOP::Lexer 'string_lexer'; my $lexer = string_lexer( $sql, [ STRING => qr/"[^"]+"/ ], [ COMMENT => qr/#[^\n]*/ ], [ SPACE => qr/\s+/, sub {} ], ); # (continues like the others...)

Here is ambiguous what to do, and both orders give a bad result. If STRING comes first, it finds strings inside comments, and if COMMENT comes first, it finds comments inside string.

(Well, there's a workaround similar to what the original article used to deal with parenthesis, which involves another parsing phase, but this is a little bit cheating I guess ;-)

use strict; my $sql = <<'--SQL--'; # this is a comment $a = "simple string"; $b = "string and comment"; # comment $c = "string with # thing"; # comment with "string" $d = "very hard" # dasdas . "to do"; --SQL-- use HOP::Lexer 'string_lexer'; my $lexer = string_lexer( $sql, [ STRING => qr/"[^"]+"/ ], [ COMMENT => qr/#/ ], [ NEWLINE => qr/\n/ ], [ SPACE => qr/\s+/, sub {} ], ); # parse my @out; my $comment = 0; while (defined (my $token = $lexer->())) { if (ref $token) { my ($label, $value) = @$token; $comment = 1 if $label eq 'COMMENT'; $comment = 0 if $label eq 'NEWLINE'; next if $label eq 'NEWLINE'; } push @out, $token if not $comment; } # Data::Dump the output (elaborated in order to produce compact result +s) use Data::Dumper; $Data::Dumper::Indent = 0; $Data::Dumper::Terse = 1; ($\, $,) = ("\n", ",\n"); print map { Dumper $_ } @out;

Which gives us:

'$a', '=', ['STRING','"simple string"'], ';', '$b', '=', ['STRING','"string and comment"'], ';', '$c', '=', ['STRING','"string with # thing"'], ';', '$d', '=', ['STRING','"very hard"'], '.', ['STRING','"to do"'], ';'

In reply to Re^3: 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.