On Sat Mar 19 09:21:24 2011, pauloscustodio@gmail.com wrote:
Show quoted text> Thanks for your answer, and for the MFC module!
>
> I went for the more complex implementation just because it seamed
> silly to grep on a list of keys of a hash (inside token_kw) when
> calling exists on the hash key is much quicker. I will try your
> solution and profile to see if the impact justifies the additional
> code.
Ah yes, that is a point... An exists test would be nicer than a grep.
Show quoted text> In the application I was toying with I had to override token_int,
> because I need to accept some more integer formats like 0FFh and $FF.
> And again I needed to do the steps:
> - skip_ws()
> - grap pos()
> - check at_eos()
> - do my stuff
> - if error go back to saved pos() and fail()
>
> It seams convenient to factor out all this code in a maybe-like
> method, to be called by application specific token_xxx methods, e.g.
> doit($code). I know the name is not good, just poped out of the head.
> Any comments?
It's been a TODO comment of mine for a while now:
# Easy ability for subclasses to define more token types
I've observed that most of the token_foo methods are basically
achievable by:
return $convert->( $self->expect( $pattern ) );
My idea was to create a "token_generic" method, which takes such a
matching pattern and conversion function. You could then implement your
hex parser by:
sub token_hex
{
my $self = shift;
return $self->token_generic(
pattern => qr/([0-9A-F]+)h/i,
convert => sub { hex $_[1] },
);
}
In fact, this could then be further neatened by a class method that
creates the appropriate method sub and installs it in the package,
wrapping it thus:
__PACKAGE__->has_token(
name => "hex",
pattern => qr/([0-9A-F]+)h/i,
convert => sub { hex $_[1] },
);
and it would create the method for you.
Show quoted text> Also, any reason to call skip_ws() inside at_eos() after grabbing
> pos() instead of before? The effect is that the same white space is
> skipped twice.
Ah yes. The reason that at_eos() pulls the pos() so early on, is that
then it ensures that merely calling at_eos() doesn't skip the
whitespace. This is essential for substring_before() to work correctly,
or else it might miss whitespace contained within the substring.
It wasn't intentional for the effect of skip_ws to be done twice though;
I'll have a shuffle around of the code to see if that can be fixed.
--
Paul Evans