Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PostfixDeref: direct dereference #15

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cpanfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
requires strictures => 2;
requires Moo => 2;
requires PPR => '0.000021';
requires PPR => '0.001004';
requires Mu => 0;
requires 'Module::Runtime' => 0;
174 changes: 133 additions & 41 deletions lib/Babble/Plugin/PostfixDeref.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,171 @@ package Babble::Plugin::PostfixDeref;

use Moo;

my $scalar_post = q{
(?:
(?>(?&PerlOWS))
(?:
(?:
(?>(?&PerlOWS)) -> (?>(?&PerlOWS))
(?&PerlParenthesesList)
|
(?>(?&PerlOWS)) (?: -> (?&PerlOWS) )?+
(?> \$\* | (?&PerlArrayIndexer) | (?&PerlHashIndexer) )
)
(?:
(?>(?&PerlOWS)) (?: -> (?&PerlOWS) )?+
(?> \$\* | (?&PerlArrayIndexer) | (?&PerlHashIndexer) | (?&PerlParenthesesList) )
)*+
)?+
(?:
(?>(?&PerlOWS)) -> (?>(?&PerlOWS))
[\@%]
(?> \* | (?&PerlArrayIndexer) | (?&PerlHashIndexer) )
)?+
)
my $term_derefable = q{
# Copied from <PerlTerm> rule in PPR::[email protected]
# The remaining alternatives can all take postfix dereferencers...
# ...
(?:
(?= \$ ) (?&PerlScalarAccess)
|
(?= \@ ) (?&PerlArrayAccess)
|
(?= % ) (?&PerlHashAccess)
|
(?&PerlAnonymousSubroutine)
|
(?>(?&PerlNullaryBuiltinFunction)) (?! (?>(?&PerlOWS)) \( )
|
(?&PerlDoBlock) | (?&PerlEvalBlock)
|
(?&PerlCall)
|
(?&PerlVariableDeclaration)
|
(?&PerlTypeglob)
|
(?>(?&PerlParenthesesList))

# Can optionally do a [...] lookup straight after the parens,
# followd by any number of other look-ups
(?:
(?>(?&PerlOWS)) (?&PerlArrayIndexer)
(?:
(?>(?&PerlOWS))
(?>
(?&PerlArrayIndexer)
| (?&PerlHashIndexer)
| (?&PerlParenthesesList)
)
)*+
)?+
|
(?&PerlAnonymousArray)
|
(?&PerlAnonymousHash)
|
(?&PerlDiamondOperator)
|
(?&PerlContextualMatch)
|
(?&PerlQuotelikeS)
|
(?&PerlQuotelikeTR)
|
(?&PerlQuotelikeQX)
|
(?&PerlLiteral)
)
};

my $scalarnospace_post = q{
# Copied from <PerlScalarAccessNoSpace> rule in PPR::[email protected]
# Then any nuber of arrowed accesses
# (this is an inlined subset of (?&PerlTermPostfixDereference))...
(?:
->
(?>
# A series of simple brackets can omit interstitial arrows...
(?: (?&PerlArrayIndexer)
| (?&PerlHashIndexer)
)++

| # An array or hash slice...
\@ (?> (?>(?&PerlArrayIndexer)) | (?>(?&PerlHashIndexer)) )
)
)*+

# Followed by at most one of these terminal arrowed dereferences...
(?:
->
(?>
# An array or scalar deref...
[\@\$] \*

| # An array count deref...
\$ \# \*
)
)?+
};

sub transform_to_plain {
my ($self, $top) = @_;
$top->remove_use_argument(experimental => 'postderef');
$top->remove_use_argument(feature => 'postderef');
for my $argument (qw(postderef postderef_qq)) {
$top->remove_use_argument(experimental => $argument);
$top->remove_use_argument(feature => $argument);
}
# TODO: cry about lvalues assignment to postfix derefs
my $tf = sub {
my ($m) = @_;
my ($m, $in_quotelike) = @_;
my $interpolate = defined $in_quotelike && $in_quotelike;
my ($term, $postfix) = $m->subtexts(qw(term postfix));
#warn "Term: $term"; warn "Postfix: $postfix";
my $grammar = $m->grammar_regexp;
my $strip_re = qr{
( (?>(?&PerlOWS)) -> (?>(?&PerlOWS))
(?>
(?> (?&PerlQualifiedIdentifier) | (?&PerlVariableScalar) )
(?: (?>(?&PerlOWS)) (?&PerlParenthesesList) )?+
| (?&PerlParenthesesList)
| (?&PerlArrayIndexer)
| (?&PerlHashIndexer)
\$\#\*
| \$\*
| (?> (?&PerlQualifiedIdentifier) | (?&PerlVariableScalar) )
(?: (?>(?&PerlOWS)) (?&PerlParenthesesList) )?+
| (?:
(?>(?&PerlOWS))
(?> (?&PerlParenthesesList) | (?&PerlArrayIndexer) | (?&PerlHashIndexer) )
)++
)
)
${grammar}
}x;
while ($postfix =~ s/^${strip_re}//) {
my $stripped = $1;
if ($stripped =~ /\$\*$/) {
$term = '(map $$_, '.$term.')[0]';
if ($stripped =~ /(\$\#?)\*$/) {
my $sigil = $1;
$term = $sigil.'{'.$term.'}';
if( $interpolate ) {
$term = "\@{[ $term ]}";
}
} else {
$term .= $stripped;
}
}
if ($postfix) {
my ($sigil, $rest) = ($postfix =~ /^\s*->\s*([\@%])(.*)$/);
$rest = '' if $rest eq '*';
$term = '(map '.$sigil.'{$_}'.$rest.', '.$term.')';
$term = $sigil.'{'.$term.'}'.$rest;
if( $interpolate ) {
# NOTE This can be interpolated safely
# because:
# 1. The delimiters are balanced so use inside of
# `qq{ ... }` or `qq[ ... ]` is safe.
# 2. The contents of $term can only contain expressions that
# have `$` and `@` sigils, so any expression contained in
# $term which is used within `qq@ ... @` will not have the
# `@` sigil (same with the `qq$ ... $` and `$` sigil).
$term = "\@{[ $term ]}";
}
}
$m->submatches->{term}->replace_text($term);
$m->submatches->{postfix}->replace_text('');
};
$top->each_match_within(PrefixPostfixTerm => [
'(?: (?>(?&PerlPrefixUnaryOperator)) (?&PerlOWS) )*+',
[ term => '(?>(?&PerlTerm))' ],
$top->each_match_within(Term => [
[ term => "(?> $term_derefable )" ],
[ postfix => '(?&PerlTermPostfixDereference)' ],
'(?: (?>(?&PerlOWS)) (?&PerlPostfixUnaryOperator) )?+'
] => $tf);
$top->each_match_within(ScalarAccess => [
[ term => '(?>(?&PerlVariableScalar))' ],
[ postfix => $scalar_post ],
] => $tf);

# NOTE ScalarAccessNoSpace is used within the
# ScalarAccessNoSpaceNoArrow rule, but any such
# matches here via that rule would be invalid input
# to begin with.
$top->each_match_within(ScalarAccessNoSpace => [
[ term => q{
(?>(?&PerlVariableScalarNoSpace))

# Optional arrowless access(es) to begin...
(?: (?&PerlArrayIndexer) | (?&PerlHashIndexer) )*+
} ],
[ postfix => $scalarnospace_post ],
] => sub { $tf->(shift, 1) });
# NOTE ArrayAccessNoSpace also needs to implemented.
}

1;
42 changes: 39 additions & 3 deletions t/plugin-postfixderef.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,47 @@ my $pd = Babble::Plugin::PostfixDeref->new;

my @cand = (
[ 'my $x = $foo->$*; my @y = $bar->baz->@*;',
'my $x = (map $$_, $foo)[0]; my @y = (map @{$_}, $bar->baz);' ],
'my $x = ${$foo}; my @y = @{$bar->baz};' ],
[ 'my $x = ($foo->bar->$*)->baz->@*;',
'my $x = (map @{$_}, ((map $$_, $foo->bar)[0])->baz);' ],
'my $x = @{(${$foo->bar})->baz};' ],
[ 'my @val = $foo->@{qw(key names)};',
'my @val = (map @{$_}{qw(key names)}, $foo);' ],
'my @val = @{$foo}{qw(key names)};' ],
[ 'my $val = $foo[0];',
'my $val = $foo[0];' ],
[ 'my $val = $foo[$idx];',
'my $val = $foo[$idx];' ],
[ '$bar->{key0}{key1}',
'$bar->{key0}{key1}' ],
[ '$bar->{key0}{key1}->@*',
'@{$bar->{key0}{key1}}' ],
[ '$bar->{key0}{key1}->@[@idx]',
'@{$bar->{key0}{key1}}[@idx]' ],
[ 'my %val = $foo->%[@idx];',
'my %val = %{$foo}[@idx];' ],
[ 'my %val = $foo->%{qw(key names)};',
'my %val = %{$foo}{qw(key names)};' ],
[ 'qq{ $foo->@* }',
'qq{ @{[ @{$foo} ]} }' ],
[ 'qq{ $foo->@{qw(key names)} }',
'qq{ @{[ @{$foo}{qw(key names)} ]} }' ],

[ 'qq{ $foo }',
'qq{ $foo }' ],
[ 'qq{ $foo $bar }',
'qq{ $foo $bar }' ],

[ 'qq{ $foo->%* }',
'qq{ $foo->%* }' ],
[ 'qq{ $foo->%* $bar->@* }',
'qq{ $foo->%* @{[ @{$bar} ]} }' ],

[ 'qq{ $foo->$* }',
'qq{ @{[ ${$foo} ]} }' ],

[ '$foo->$#*',
'$#{$foo}' ],
[ 'qq{ $foo->$#* }',
'qq{ @{[ $#{$foo} ]} }' ],
);

foreach my $cand (@cand) {
Expand Down