1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
|
#!/usr/local/bin/perl
use strict;
use warnings;
use Pod::Usage;
use PPIx::Regexp;
use PPIx::Regexp::Constant qw{
ARRAY_REF
SCALAR_REF
};
use PPIx::Regexp::Dumper;
use PPIx::Regexp::Util qw{ __instance };
use Term::ReadLine;
@ARGV or die "Need an argument.\n";
my $re = PPIx::Regexp->new( $ARGV[0] )
or die PPIx::Regexp->errmsg();
my $obj = $re;
my $tr = Term::ReadLine->new( 'Navigate a regular expression' );
my %internal = (
capture_names => sub {
defined $obj or return;
return join( ', ', map { "'$_'" } $obj->capture_names() );
},
delimiters => sub {
defined $obj or return;
return join( ', ', map { "'$_'" } $obj->delimiters() );
},
dump => sub {
my @args;
defined $_[0] and @args = split qr{ \s+ }smx, $_[0];
return PPIx::Regexp::Dumper->new(
$obj, @args )->string();
},
help => sub {
my ( $arg ) = @_;
my @extra;
if ( defined $arg ) {
$arg =~ m/ \A [[:alpha:]_] \w* (?: :: [[:alpha:]_] \w* )* \z /smx
or die "Invalid module name $arg\n";
( my $fn = "$arg.pm" ) =~ s< :: ></>smxg;
push @extra, '-input', exists $INC{$fn} ? $INC{$fn} :
_find_file_in_inc( $fn, "Can not find module $arg" );
}
pod2usage(
-exitval => 'NOEXIT',
-verbose => 2,
-output => \*STDOUT,
@extra,
);
return \'';
},
nav => sub {
return _safe( $obj->nav() );
},
parse => sub {
my $temp = PPIx::Regexp->new( $_[0] )
or return PPIx::Regexp->errstr();
return ( $obj = $re = $temp );
},
reset => sub {
return ( $obj = $re );
},
);
sub _safe {
my ( @args ) = @_;
my $rslt = join ', ', map {
ARRAY_REF eq ref $_ ? '[ ' . _safe( @{ $_ } ) . ' ]' : "'$_'"
} @args;
$rslt =~ s/ \[ \s+ \] /[]/smxg;
$rslt =~ s/ ' ( \d+ ) ' /$1/smxg;
$rslt =~ s/ \[ \s* ( \d+ ) \s* \] /$1/smxg;
$rslt =~ s/ ' ( \w+ ) ', /$1 =>/smxg;
return $rslt;
}
while ( defined ( my $buffer = $tr->readline( 'prenav> ' ) ) ) {
$buffer =~ s/ \s+ \z //smx;
$buffer or next;
$buffer =~ s/ \A \s+ //smx;
'#' eq substr $buffer, 0, 1 and next;
my ( $method, $arg ) = split qr{\s+}smx, $buffer, 2;
'exit' eq $method and last;
my $temp;
eval {
$temp = $internal{$method} ?
$internal{$method}->( $arg ) :
$obj->$method( $arg );
1;
} or do {
warn $@;
next;
};
print _format( $temp );
__instance( $temp, 'PPIx::Regexp::Element' )
and $obj = $temp;
}
sub _find_file_in_inc {
my ( $fn, $msg ) = @_;
foreach my $dir ( @INC ) {
ref $dir # Guard against code and objects in @INC
and next;
my $path = "$dir/$fn";
-f $path
and return $path;
}
$msg ||= "Can not find file $fn";
$msg =~ s/ (<! \n ) \z /\n/smx;
die $msg;
}
sub _format {
my ( @args ) = @_;
my $rslt;
foreach my $thing ( @args ) {
if ( __instance( $thing, 'PPIx::Regexp::Element' ) ) {
$rslt .= $thing->class() . "\t" . $thing->content() . "\n";
} elsif ( ARRAY_REF eq ref $thing ) {
$rslt .= _format( @{ $thing } );
} elsif ( SCALAR_REF eq ref $thing ) {
$rslt .= ${ $thing };
} elsif ( defined $thing ) {
$rslt .= $thing =~ m/ \n /smx ? $thing :
$thing =~ m/ \A ' /smx ? "$thing\n" :
$thing =~ m/ \D /smx ? "'$thing'\n" :
"$thing\n";
} else {
$rslt .= "undef\n";
}
}
return $rslt;
}
__END__
=head1 NAME
prenav - Navigate a PPIx::Regexp parse tree
=head1 SYNOPSIS
prenav 's/(\w+)/\u$1/g'
prenav> find_first Token::CharClass::Simple
PPIx::Regexp::Token::CharClass::Simple \w
prenav> dump verbose 1
PPIx::Regexp::Token::CharClass::Simple '\\w' significant
can_be_quantified
prenav> parent
PPIx::Regexp::Structure::Capture (\w+)
prenav> exit
=head1 DESCRIPTION
This script takes as its argument a string to be parsed as a regular
expression, and prompts the user for navigation commands. A navigation
command is any method that returns another element in the parse tree.
Unless documented otherwise, all commands apply to the current
object. Initially the current object is the L<PPIx::Regexp|PPIx::Regexp>
object generated by the parse. Once a navigation command is issued, the
object navigated to becomes the current object. If the navigation
command does not specify an object (e.g. C<child 5> when the current
object has fewer than 5 children) the current object remains unchanged.
In addition to the navigation methods, any method that returns a scalar
value can be used as a command. The value returned will be displayed.
In addition to all the above, the following commands are recognized:
=over
=item capture_names
This command wraps the
L<< PPIx::Regexp->capture_names()|PPIx::Regexp/capture_names >> method,
joining the results into a comma-delimited string.
=item dump
This command dumps the current object. Options to
L<< PPIx::Regexp::Dumper->new()|PPIx::Regexp::Dumper/new >> may be
specified as arguments to the command. See the L<SYNOPSIS|/SYNOPSIS> for
an example.
=item exit
This comamnd terminates the script.
=item help
This command displays this documentation.
If you supply a module name as an argument, the documentation for that
module will be displayed.
=item nav
nav
This command displays the method calls and arguments needed to navigate
from the root of the parse tree to the current object. Yes, this is a
perfectly good method, but we wrap the results of that method in some
semi-nice formatting.
Any arguments are ignored
=item parse
parse s/ ( \w+ ) foo \1 /bar/smx
This command provides another regular expression to parse. If the parse
succeeds, the previous regular expression is abandoned, and the new
L<PPIx::Regexp|PPIx::Regexp> object becomes the current object.
The new regular expression is taken to be everything on the line after
the whit espace after the word C<parse>. It should B<not> be quoted.
=item reset
This command selects the top-level object as the current object. The
C<top> command does the same thing, but C<top> does it by running
through the parent chain, where C<reset> simply slam-dunks the retained
L<PPIx::Regexp|PPIx::Regexp> object.
=back
=head1 SUPPORT
Support is by the author. Please file bug reports at
L<https://rt.cpan.org/Public/Dist/Display.html?Name=PPIx-Regexp>,
L<https://github.com/trwyant/perl-PPIx-Regexp/issues>, or in
electronic mail to the author.
=head1 AUTHOR
Thomas R. Wyant, III F<wyant at cpan dot org>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2009-2023, 2025 by Thomas R. Wyant, III
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl 5.10.0. For more details, see the full text
of the licenses in the directory LICENSES.
This program is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of
merchantability or fitness for a particular purpose.
=cut
# ex: set textwidth=72 :
|