Acme-Pythonic
view release on metacpan or search on metacpan
lib/Acme/Pythonic.pm view on Meta::CPAN
s/$Filter::Simple::placeholder/BLANKED_OUT/g;
print;
$_ = '1;';
}
};
# 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;
# In addition, we can now normalize newlines without breaking
# Filter::Simple's identifiers.
normalize_newlines();
my @lines = split /\n/;
return unless @lines;
# If unsure about the ending indentation level, add an extra
# non-indented line to ensure the stack gets emptied.
push @lines, '1; # added by Acme::Pythonic' if $lines[-1] =~ /^(?:\s|\s*#)/;
my ($comment, # comment in the current line, if any
$indent, # indentation of the current logical line
$id_at_sob, # identifier at StartOfBlock, for instance "else", or "eval"
$prev_line_with_code, # previous line with code
$might_be_modifier, # flag: current logical line might be a modifier
$line_with_modifier, # physical line which started the current modifier
$joining, # flag: are we joining lines?
$unbalanced_paren, # flag: we opened a paren that remains to be closed
@stack, # keeps track of indentation stuff
);
@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 {
$joining = 0;
}
# Handle trailing colons, which can be Pythonic, mark a labeled
# block, mean some map, or &-sub call, etc.
#
# We check the parity of the number of ending colons to try to
# avoid breaking things like
#
# print for keys %main::
#
my $bracket_opened_by = '';
if ($line =~ /(:+)$/ && length($1) % 2) {
$might_be_modifier = 0;
# We perform some checks because labels have to keep their colon.
if ($line !~ /^\s*$id:$/o ||
$line =~ /[[:lower:]]/ || # labels are not allowed to have lower-case letters
$line =~ /^\s*(?:BEGIN|CHECK|INIT|END):$/) {
chop $line;
if ($unbalanced_paren) {
$line .= ")";
$unbalanced_paren = 0;
} else {
($bracket_opened_by) = $line =~ /($id)\s*$/o;
}
}
} elsif (!$joining) {
$$line_with_modifier =~ s/\(// if $might_be_modifier;
$unbalanced_paren = 0;
$line .= ';';
}
# Handle indentation. Language::Pythonesque was the basis of
# this code.
my $prev_indent = @stack ? $stack[-1]{indent} : 0;
if ($prev_indent < $indent) {
push @stack, {indent => $indent, id_at_sob => $id_at_sob};
$$prev_line_with_code .= " {" unless $$prev_line_with_code =~ s/(?=\s*$tc)/ {/o;
} elsif ($prev_indent > $indent) {
do {
my $prev_id_at_sob = $stack[-1]{id_at_sob};
pop @stack;
$prev_indent = @stack ? $stack[-1]{indent} : 0;
$$prev_line_with_code .= "\n" . ((' ' x $prev_indent) . "}");
$$prev_line_with_code .= ";" if needs_semicolon($prev_id_at_sob);
} while $prev_indent > $indent;
$$prev_line_with_code =~ s/;$/ / if $might_be_modifier;
}
$id_at_sob = $bracket_opened_by;
} continue {
$line =~ s/^\s*pass;?\s*$//;
$prev_line_with_code = \$line if !$joining && $line =~ /\S/;
$line .= $comment;
}
( run in 0.594 second using v1.01-cache-2.11-cpan-5b529ec07f3 )