The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Verilog::Parser - Parse Verilog language files

SYNOPSIS

  use Verilog::Parser;

  my $parser = new Verilog::Parser;
  $string = $parser->unreadback ();
  $line   = $parser->line ();
  $parser->parse ($text)
  $parser->parse_file ($filename)

DESCRIPTION

The Verilog::Parser package will tokenize a Verilog file when the parse() method is called and invoke various callback methods.

The external interface to Verilog::Parser is:

$parser = Verilog::Parser->new

Create a new Parser.

$parser->parse ($string)

Parse the $string as a verilog file. Can be called multiple times. The return value is a reference to the parser object.

$parser->parse_file ($filename);

This method can be called to parse text from a file. The argument can be a filename or an already opened file handle. The return value from parse_file() is a reference to the parser object.

$parser->unreadback ()

Return any input string from the file that has not been sent to the callback. This will include whitespace and tokens which did not have a callback. (For example comments, if there is no comment callback.) This is useful for recording the entire contents of the input, for preprocessors, pretty-printers, and such.

$parser->line ($set)

Return (if $set is undefined) or set current line number.

In order to make the parser do anything interesting, you must make a subclass where you override one or more of the following methods as appropriate:

$self->comment ( $token )

This method is called when any text in // or /**/ comments are recognized. The first argument, $token, is the contents of the comment excluding the comment delimiters.

$self->string ( $token )

This method is called when any text in double quotes are recognized. The first argument, $token, is the contents of the string including the quotes.

$self->keyword ( $token )

This method is called when any Verilog keyword is recognized. The first argument, $token, is the keyword.

$self->symbol ( $token )

This method is called when any Verilog symbol is recognized. A symbol is considered a non-keyword bareword. The first argument, $token, is the symbol.

$self->operator ( $token )

This method is called when any symbolic operator (+, -, etc) is recognized. The first argument, $token, is the operator.

$self->number ( $token )

This method is called when any number is recognized. The first argument, $token, is the number. The Verilog::Language::number_value function may be useful for converting a Verilog value to a perl integer.

EXAMPLE

Here\'s a simple example which will print every symbol in a verilog file.

package MyParser; use Verilog::Parser; @ISA = qw(Verilog::Parser);

# parse, parse_file, etc are inherited from Verilog::Parser sub new { my $class = shift; #print "Class $class\n"; my $self = $class->SUPER::new(); # we could have inherited new, but we want to initialize symbols %{$self->{symbols}} = (); bless $self, $class; return $self; }

sub symbol { my $self = shift; my $token = shift;

    $self->{symbols}{$token}++;
}

sub report { my $self = shift;

    foreach my $sym (sort keys %{$self->{symbols}}) {
         printf "Symbol %-30s occurs %4d times\n",
         $sym, $self->{symbols}{$sym};
    }
}

package main;

my $parser = MyParser->new(); $parser->parse_file (shift); $parser->report();

SEE ALSO

Verilog::ParserSig, Verilog::Language, vrename

BUGS

This is being distributed as a baseline for future contributions. Don\'t expect a lot, the Parser is still nieve, and there are many awkward cases that aren\'t covered.

The parser currently assumes the string it is passed ends on a newline boundary. It should be changed to allow arbitrary chunks.

DISTRIBUTION

The latest version is available from http://www.ultranet.com/~wsnyder/verilog-perl.

AUTHORS

Wilson Snyder <wsnyder@world.std.com>