
Shell::Parser - Simple shell script parser

Version 0.04

use Shell::Parser;
my $parser = new Shell::Parser syntax => 'bash', handlers => {
};
$parser->parse(...);
$parser->eof;

This 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.

Creates and returns a new Shell::Parser object. Options can be provided as key/value pairs.
Options
handlers - sets the parsing events handlers. See "handlers()" for more information.syntax - selects the shell syntax. See "syntax()" for more information.Examples
my $parser = new Shell::Parser syntax => 'bash',
handlers => { default => \&default_handler };
Parse the shell code given in argument.
Examples
$parser->parse(qq{echo "hello world"\n});
$parser->parse(<<'SHELL');
for pat; do
echo "greping for $pat"
ps aux | grep $pat
done
SHELL
Tells the parser that there is no more data.
Note that this method is a no-op for now, but this may change in the future.
Assign handlers to parsing events using a hash or a hashref. Available events:
assign - handler for assignments: VARIABLE=VALUEbuiltin - handler for shell builtin commands: alias, jobs, read...command - handler for external commands (not implemented)comment - handler for comments: # an impressive commentkeyword - handler for shell reserved words: for, if, case...metachar - handler for shell metacharacters: ;, &, |...variable - handler for variable expansion: $VARIABLEtext - handler for anything elseThere 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.
Selects the shell syntax. Use one of:
bourne - the standard Bourne shellcsh - the C shelltcsh - the TENEX C shellkorn88 - the Korn shell, 1988 versionkorn93 - the Korn shell 1993 versionbash - GNU Bourne Again SHellzsh - the Z shellReturns the current syntax when called with no argument, or the previous syntax when affecting a new one.

During parsing, the functions defined as handlers for the corresponding events will be called with the following arguments:
Shell::Parser objecttoken - the actual shell tokentype - the type of the tokenTherefore, a typical handler function will begin with something like this:
sub my_handler {
my $self = shift;
my %args = @_;
# do stuff
# ...
}

Here 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: <
>

(F) You gave a reference to parse(), which is not handled at this time.
(E) You gave an unknown handler name. Please check "handlers()" for the available handlers.
(E) You gave an unknown syntax. Please check "syntax()" for the available syntaxes.

Shell::Parser with the -W flag gives many warnings, but most come from Text::ParseWords.command event is currently unimplemented.
SEeacute>bastien Aperghis-Tramoni, <sebastien@aperghis.net>

Please 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 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.