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.
Syntax::Highlight::Shell
Creates and returns a new Shell::Parser object. Options can be provided as key/value pairs.
Shell::Parser
Options
handlers - sets the parsing events handlers. See "handlers()" for more information.
handlers
syntax - selects the shell syntax. See "syntax()" for more information.
syntax
Examples
my $parser = new Shell::Parser syntax => 'bash', handlers => { default => \&default_handler };
Parse the shell code given in argument.
$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=VALUE
assign
VARIABLE=VALUE
builtin - handler for shell builtin commands: alias, jobs, read...
builtin
alias
jobs
read
command - handler for external commands (not implemented)
command
comment - handler for comments: # an impressive comment
comment
# an impressive comment
keyword - handler for shell reserved words: for, if, case...
keyword
for
if
case
metachar - handler for shell metacharacters: ;, &, |...
metachar
;
&
|
variable - handler for variable expansion: $VARIABLE
variable
$VARIABLE
text - handler for anything else
text
There is also a default handler, which will be used for any handler which has not been explicitely defined.
default
# 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 shell
bourne
csh - the C shell
csh
tcsh - the TENEX C shell
tcsh
korn88 - the Korn shell, 1988 version
korn88
korn93 - the Korn shell 1993 version
korn93
bash - GNU Bourne Again SHell
bash
zsh - the Z shell
zsh
Returns 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:
a reference to the current Shell::Parser object
a hash with the following keys:
token - the actual shell token
token
type - the type of the token
type
Therefore, 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.
parse()
(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.
Running Shell::Parser with the -W flag gives many warnings, but most come from Text::ParseWords.
-W
Text::ParseWords
Comments curently contains the newline character that terminate them. This is not very intuituive and will be corrected in later versions.
The command event is currently unimplemented.
Here-documents are currently not parsed.
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.
bug-shell-parser@rt.cpan.org
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.
To install Shell::Parser, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Shell::Parser
CPAN shell
perl -MCPAN -e shell install Shell::Parser
For more information on module installation, please visit the detailed CPAN module installation guide.