Acme-Syntax-Python

 view release on metacpan or  search on metacpan

lib/Acme/Syntax/Python.pm  view on Meta::CPAN

    s{^\s*import (.+);$}
     {use $1;}gmx;
    s{^\s*from (.+) import (.+);$}
     {use $1 ($2);}gmx;

    s{True}{1}gmx;
    s{False}{0}gmx;

    if(/class (.+) inherits (.+):/) {
        s{class (.+) inherits (.+):}{\{\npackage $1;\nuse $2; our \@ISA = qw($2);\n}gmx;
        _start_block($self);
    }

    if(/class (.+):/) {
        s{class (.+):}{\{\npackage $1;\n}gmx;
        _start_block($self);
    }

    #Handle def with Params
    if(/lambda\((.+)\):/) {
        s{lambda\((.+)\):}{sub \{ my($1) = \@_;}gmx;
        _start_block($self, "_lambda_block");
    }

    #Handle def with no Params
    if(/lambda:/) {
        s{lambda:}{sub \{};
        _start_block($self, "_lambda_block");
    }

    #Handle def with Params
    if(/def (.+)\((.+)\):/) {
        if($1 eq "__init__") {
            s{def (.+)\((.+)\):}{sub $1 \{ my(\$class, $2) = \@_; my \$self = \{\};}gmx;
            $self->{_class_block}->{($self->{_block_depth} + 1)} = 1;
        } else {
            s{def (.+)\((.+)\):}{sub $1 \{ my($2) = \@_;}gmx;
        }
        _start_block($self);
    }

    #Handle def with no Params
    if(/def (.+):/) {
        if($1 eq "__init__") {
            s{def (.+):}{sub $1 \{ my (\$class) = shift; my \$self = \{\};}gmx;
            $self->{_class_block}->{($self->{_block_depth} + 1)} = 1;
        } else {
            s{def (.+):}{sub $1 \{}gmx;
        }
        _start_block($self);
    }

    s{__init__}{new}gmx;

    if(/elif (.+)/) {
        s{elif (.+)}{elsif $1}gmx;
    }
    elsif(/if (.*)/) {
        s{if (.*)}{if $1}gmx;
    }
    if(/\):$/) {
        s{:$}{ \{}gmx;
        _start_block($self);
    }
    if(/else:/) {
        s{:$}{\{}gmx;
        _start_block($self);
    }

    if($self->{_debug}) {
        print "$self->{line_no} $_";
    }
    return $status;
}

sub _handle_spacing {
    my $depth = shift;
    my $modifier = shift // 1;
    return (' ') x (4 * ($depth - $modifier));
}

sub _start_block {
    my ($self, $type) = @_;
    $self->{_in_block} = 1;
    ++ $self->{_block_depth};
    if(defined($type)) {
        $self->{$type}->{$self->{_block_depth}} = 1;
    }
}

sub _handle_block {
        my ($self) = @_;



( run in 0.264 second using v1.01-cache-2.11-cpan-0d8aa00de5b )