Acme-Pythonic
view release on metacpan or search on metacpan
lib/Acme/Pythonic.pm view on Meta::CPAN
@stack = ();
foreach my $line (@lines) {
# We remove any trailing comment so that we can assert stuff
# easily about the end of the code in this line. It is later
# appended back in the continue block below.
$comment = $line =~ s/(\s*$tc)//o ? $1 : '';
next if $line =~ /^\s*$/;
if (!$joining) {
$unbalanced_paren = left_parenthesize($line);
$might_be_modifier = $line =~ /^\s*(?:if|unless|while|until|for|foreach)\b/;
$line_with_modifier = \$line if $might_be_modifier;
($indent) = $line =~ /^(\s*)/;
$indent = length(expand($indent));
}
if ($line =~ /(?:,|=>)\s*$/ || $line =~ s/\\\s*$//) {
++$joining;
next if $joining > 1; # if 1 we need yet to handle indentation
} else {
lib/Acme/Pythonic.pm view on Meta::CPAN
# In addition, it guarantees make test works no matter the platform.
sub normalize_newlines {
s/\015\012/\n/g;
tr/\015/\n/ unless "\n" eq "\015";
tr/\012/\n/ unless "\n" eq "\012";
}
# 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
lib/Acme/Pythonic.pm view on Meta::CPAN
Acme::Pythonic provides I<grosso modo> these conventions:
=over 4
=item * Blocks are marked by indentation and an opening colon instead of
braces.
=item * Simple statements are separated by newlines instead of
semicolons.
=item * EXPRs in control flow structures do not need parentheses around.
=back
Additionally, the filter understands the keywords C<pass> and C<in>.
for my $n in 1..100:
while $n != 1:
if $n % 2:
$n = 3*$n + 1
else:
( run in 0.485 second using v1.01-cache-2.11-cpan-a5abf4f5562 )