| 1 |
#! /usr/bin/env perl |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
use Term::ANSIColor ':constants'; |
| 5 |
$Term::ANSIColor::AUTORESET = 1; |
| 6 |
|
| 7 |
# ---------------------------------------------------------------------- |
| 8 |
# This first, to keep PadWalker away from lexical variables below. |
| 9 |
sub scoped_eval { |
| 10 |
print MAGENTA @_ if $App::REPL::DEBUG; |
| 11 |
eval shift; |
| 12 |
print BOLD YELLOW $@ if $@; |
| 13 |
} |
| 14 |
|
| 15 |
use PadWalker 'peek_my'; |
| 16 |
use PPI; |
| 17 |
use PPI::Find; |
| 18 |
use Data::Dumper; |
| 19 |
use Symbol; |
| 20 |
use Term::ReadLine; |
| 21 |
$App::REPL::DEBUG = 0; |
| 22 |
|
| 23 |
{ my $in_package = 'App::REPL'; |
| 24 |
sub in_package { @_ ? $in_package = shift : $in_package } |
| 25 |
} |
| 26 |
|
| 27 |
# ---------------------------------------------------------------------- |
| 28 |
# Added RESET as the color somehow bleeds into the prompt |
| 29 |
# -- when we use Term::ReadLine |
| 30 |
{ my $prompt; |
| 31 |
my $term = Term::ReadLine->new('iperl'); |
| 32 |
sub pnew { $prompt = RESET . in_package . ' _ ' } |
| 33 |
sub pcont { $prompt = RESET . in_package . '. ' } |
| 34 |
sub prompt { |
| 35 |
my $s = $term->readline($prompt); |
| 36 |
$term->addhistory($s) if defined($s) and $s =~ /\S/; |
| 37 |
$s |
| 38 |
} |
| 39 |
pnew; |
| 40 |
$term->ornaments(0) |
| 41 |
} |
| 42 |
|
| 43 |
sub eek { print STDERR BOLD RED @_, "\n"; goto REPL } |
| 44 |
|
| 45 |
|
| 46 |
# ---------------------------------------------------------------------- |
| 47 |
# Magic. This allows 'my' variables assigned within the eval to carry |
| 48 |
# through subsequent evals -- unless the eval'd returns from the eval, |
| 49 |
# in which case the next eval will get the same variables. |
| 50 |
#-- |
| 51 |
use constant PRO_IN => <<'EOP'; |
| 52 |
use App::REPL; |
| 53 |
use strict; |
| 54 |
no warnings 'void'; |
| 55 |
EOP |
| 56 |
sub PRO { |
| 57 |
my $r = "no strict 'refs';\n" |
| 58 |
. "package @{[in_package]};\n"; |
| 59 |
my $h = do { no strict 'refs'; ${in_package . '::REPL::env'} || {}}; |
| 60 |
for (keys %$h) { |
| 61 |
/^(.)/; |
| 62 |
$r .= "my $_ = $1" . q,{${", . in_package . q,::REPL::env"}->, . "{'$_'}};\n" |
| 63 |
} |
| 64 |
$r . PRO_IN |
| 65 |
} |
| 66 |
use constant EPI => <<'EOE'; |
| 67 |
; |
| 68 |
no strict 'refs'; |
| 69 |
for (Symbol::qualify('')) { s/::$//; main::in_package($_) } |
| 70 |
${main::in_package . '::REPL::env'} = PadWalker::peek_my(0) |
| 71 |
EOE |
| 72 |
|
| 73 |
# ---------------------------------------------------------------------- |
| 74 |
# More magic. This finds the final statement of some Perl, wherever |
| 75 |
# that statement may be (even if its result cannot escape the overall |
| 76 |
# evaluation), and saves its value in $App::REPL::ret |
| 77 |
#-- |
| 78 |
$App::REPL::ret = ''; |
| 79 |
{ my $f = PPI::Find->new(sub { shift->isa('PPI::Statement') }); |
| 80 |
sub save_ret { |
| 81 |
my $d = shift; |
| 82 |
|
| 83 |
# don't even try if it contains something troublesome. |
| 84 |
return $d->serialize if has_troublesome($d); |
| 85 |
|
| 86 |
my @s = $f->in($d); |
| 87 |
for (reverse @s) { |
| 88 |
next if within_constructor($_, $d); |
| 89 |
print Dumper $d if $App::REPL::DEBUG > 1; |
| 90 |
unshift @{$_->{children}}, |
| 91 |
bless({content => '$App::REPL::ret'}, 'PPI::Token::Symbol'), |
| 92 |
bless({content => '='}, 'PPI::Token::Operator'); |
| 93 |
return $d->serialize |
| 94 |
} |
| 95 |
|
| 96 |
# try and save the whole thing |
| 97 |
return '$App::REPL::ret = ' . $d->serialize if @s; |
| 98 |
|
| 99 |
# give up |
| 100 |
$d->serialize |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
{ my %troublesome = map { $_, 1 } qw(sub package use require my our local); |
| 106 |
my $f = PPI::Find->new(sub { |
| 107 |
return 0 unless (my $e = shift)->isa('PPI::Token::Word'); |
| 108 |
return 1 if exists $troublesome{$e->{content}}; |
| 109 |
0 |
| 110 |
}); |
| 111 |
sub has_troublesome { $f->in(shift) } |
| 112 |
} |
| 113 |
|
| 114 |
sub dump_ret { |
| 115 |
return if ref $_[0] eq 'CODE'; |
| 116 |
print BOLD CYAN Dumper $App::REPL::ret if $App::REPL::ret; |
| 117 |
} |
| 118 |
|
| 119 |
{ my $fc = PPI::Find->new(sub { $_[0]->isa('PPI::Structure::Constructor') |
| 120 |
or $_[0]->isa('PPI::Structure::Block') }); |
| 121 |
sub within_constructor { |
| 122 |
my ($s, $d) = @_; |
| 123 |
my $fs = PPI::Find->new(sub { shift eq $s }); |
| 124 |
for ($fc->in($d)) { |
| 125 |
return 1 for $fs->in($_); |
| 126 |
} |
| 127 |
0 |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
# ---------------------------------------------------------------------- |
| 132 |
# The PPI here handles the rest of the magic: it detects unfinished |
| 133 |
# blocks and such so that the repl can request more lines until they |
| 134 |
# complete. Note that this does -not- handle e.g. qw( |
| 135 |
#-- |
| 136 |
{ my $f = PPI::Find->new(sub { |
| 137 |
my %h = %{+shift}; |
| 138 |
(exists $h{start} and !exists $h{finish}) ? 1 : 0 |
| 139 |
}); |
| 140 |
sub repl { |
| 141 |
my $s = ''; |
| 142 |
REPL: while (defined($_ = prompt)) { |
| 143 |
$s .= "\n" . $_; |
| 144 |
my $d = PPI::Document->new(\$s); |
| 145 |
if ($f->in($d)) { |
| 146 |
pcont |
| 147 |
} |
| 148 |
else { |
| 149 |
scoped_eval PRO . save_ret($d) . EPI; |
| 150 |
dump_ret; |
| 151 |
$App::REPL::ret = ''; |
| 152 |
$s = ''; |
| 153 |
pnew |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
# ---------------------------------------------------------------------- |
| 161 |
package App::REPL; |
| 162 |
main::repl(); |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
# ---------------------------------------------------------------------- |
| 167 |
BEGIN { |
| 168 |
# Patch PPI 1.118 into suitability; subsequent versions should work fine. |
| 169 |
# Yes, this is somewhat wrong, and will go away as soon as PPI >1.118 |
| 170 |
# comes out -- but in these early versions of App::REPL , it should be |
| 171 |
# OK. |
| 172 |
return unless $PPI::VERSION eq 1.118; |
| 173 |
print "#-- Oh, you have PPI 1.118 -- we need to patch it up a bit.\n"; |
| 174 |
no warnings 'redefine'; |
| 175 |
package PPI::Find; |
| 176 |
sub _execute { |
| 177 |
my $self = shift; |
| 178 |
my $wanted = $self->{wanted}; |
| 179 |
my @queue = ( $self->{in} ); |
| 180 |
|
| 181 |
# Pull entries off the queue and hand them off to the wanted function |
| 182 |
while ( my $Element = shift @queue ) { |
| 183 |
my $rv = &$wanted( $Element, $self->{in} ); |
| 184 |
|
| 185 |
# Add to the matches if returns true |
| 186 |
push @{$self->{matches}}, $Element if $rv; |
| 187 |
|
| 188 |
# Continue and don't descend if it returned undef |
| 189 |
# or if it doesn't have children |
| 190 |
next unless defined $rv; |
| 191 |
next unless $Element->isa('PPI::Node'); |
| 192 |
|
| 193 |
# Add the children to the head of the queue |
| 194 |
if ( $Element->isa('PPI::Structure') ) { |
| 195 |
unshift @queue, $Element->finish if $Element->finish; |
| 196 |
unshift @queue, $Element->children; |
| 197 |
unshift @queue, $Element->start if $Element->start; |
| 198 |
} else { |
| 199 |
unshift @queue, $Element->children; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
1; |
| 204 |
} |
| 205 |
} |