|
NAMEShell::Parser - Simple shell script parser VERSIONVersion 0.04 SYNOPSIS use Shell::Parser;
my $parser = new Shell::Parser syntax => 'bash', handlers => {
};
$parser->parse(...);
$parser->eof;
DESCRIPTIONThis module implements a rudimentary shell script parser in Perl. It was primarily written as a backend for "Syntax::Highlight::Shell", in order to simplify the creation of the later. METHODS
Examples my $parser = new Shell::Parser syntax => 'bash',
handlers => { default => \&default_handler };
There is also a "default" handler, which will be used for any handler which has not been explicitely defined. Examples # set the default event handler
$parser->handlers(default => \&default_handler);
# set the 'builtin' and 'keywords' events handlers
$parser->handlers({ builtin => \&handle_internals, keywords => \&handle_internals });
See also "Handlers" for more information on how event handlers receive their data in argument.
Returns the current syntax when called with no argument, or the previous syntax when affecting a new one. HANDLERSDuring parsing, the functions defined as handlers for the corresponding events will be called with the following arguments:
Therefore, a typical handler function will begin with something like this: sub my_handler {
my $self = shift;
my %args = @_;
# do stuff
# ...
}
EXAMPLEHere is an example that shows how the tokens are given to the events handlers. It uses the script eg/parsedump.pl: #!/usr/bin/perl
use strict;
use Shell::Parser;
my $parser = new Shell::Parser handlers => { default => \&dumpnode };
$parser->parse(join '', <>);
sub dumpnode {
my $self = shift;
my %args = @_;
print "$args{type}: <$args{token}>\n"
}
Running this Perl script with the following shell script in argument: #!/bin/sh
if [ "$text" != "" ]; then grep "$text" file.txt; fi
will produce the following trace: comment: <#!/bin/sh>
text: <
>
keyword: <if>
text: < >
text: <[>
text: < >
text: <"$text">
text: < >
assign: <!=>
text: < >
text: <"">
text: < >
text: <]>
metachar: <;>
text: < >
keyword: <then>
text: < >
text: <grep>
text: < >
text: <"$text">
text: < >
text: <file.txt>
metachar: <;>
text: < >
keyword: <fi>
text: <
>
DIAGNOSTICS
CAVEATS
AUTHORSEeacute>bastien Aperghis-Tramoni, <sebastien@aperghis.net> BUGSPlease report any bugs or feature requests to "bug-shell-parser@rt.cpan.org", or through the web interface at <https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Shell-Parser>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. COPYRIGHT & LICENSECopyright 2004 Sébastien Aperghis-Tramoni, All Rights Reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
|