Acme-Pythonic
view release on metacpan or search on metacpan
lib/Acme/Pythonic.pm view on Meta::CPAN
# This regexp matches a 7-bit ASCII identifier. We use atomic grouping
# because an identifier cannot be backtracked.
my $id = qr/(?>[_a-zA-Z](?:[_a-zA-Z0-9']|::)*)/;
# Shorthand to put an eventual trailing comment in some regexps.
my $tc = qr/(?<!\$)#.*/;
# Tries its best at converting Pythonic code to Perl.
sub unpythonize {
# Sometimes Filter::Simple adds newlines blanking out stuff, which
# interferes with Pythonic conventions.
my %bos = (); # BlanketOutS
my $count = 0;
s<$Filter::Simple::placeholder>
<my $bo = "$;BLANKED_OUT_".$count++."$;";
$bos{$bo} = $&;
$bo>geo;
lib/Acme/Pythonic.pm view on Meta::CPAN
# Put an opening paren in the places we forgive parens. It will be later
# closed or removed as needed in the main subroutine.
sub left_parenthesize {
$_[0] =~ s/^(\s*\b(?:if|elsif|unless)\b\s*)/$1(/ ||
$_[0] =~ s/^(\s*(?:$id\s*:)?\s*\b(?:while|until)\b(\s*))/$2 eq '' ? "$1 (" : "$1("/eo ||
$_[0] =~ s/^(\s*(?:$id\s*:\s*)?\bfor(?:each)?\b\s*)(.*)/fortype_guesser($1,$2)/oxe
}
# Tries its best at guessing a for(each) type or, at least, where to put
# the opening paren.
#
# Returns a string which is a copy of the original with the paren
# inserted.
sub fortype_guesser {
my ($for, $rest) = @_;
my $guess = "";
# Try to match "for VAR in LIST", and "for VAR LIST"
if ($rest =~ m/^((?:my|our)? \s* \$ $id\s+) in\s* ((?: (?:[\$\@%&\\]) | (?:\b\w) ) .*)$/ox ||
( run in 1.280 second using v1.01-cache-2.11-cpan-4e96b696675 )