pegex-parser / pegex-pm

Pegex Parser for Perl
62 stars 22 forks source link

Add a handy line() method in Pegex::Receiver #36

Open agentzh opened 9 years ago

agentzh commented 9 years ago

Right now, we need ($self->parser->line_column)[0] to get line numbers in custom receiver class's methods. It'll be great if the Pegex::Receiver class (or anything along the inheritance line) provides a handy line() method so that each receiver method can just call $self->line to get the line number. Also this method can avoid the dynamic Perl array creation in line_column because only the line number is requested.

ingydotnet commented 9 years ago

See #42.

I think it might end up like:

my $receiver = Some::AST->new(need_line_number => 1);
…
package Some::AST;
sub got_foo {
    my $line = ${$self->{line}};
}
agentzh commented 9 years ago

@ingydotnet Not too bad, though a bit ugly, especially as part of the Pegex API. Started missing macros in Perl 5 :)

ingydotnet commented 9 years ago

@agentzh better ideas welcome. :)

agentzh commented 9 years ago

@ingydotnet Not really if you want the speed :) My original proposal looks cleaner though definitely a little bit slower :)