Here’s a perl script to indent verilog files.
Download here. vindent.pdf
Add your own list of keywords to indent any source code
For example, this script could be used to indent C/C++/Java source codes.
This script uses two spaces for each indent. This could be changed as required.
#!/usr/bin/perl
# Filename : vindent.pl
# Author : A.G.Raja
# Website : https://agraja.wordpress.com
#
# Usage : ./vindent.pl verilogfile.v
# : ./vindent.pl *.*v*
# Help : ./vindent.pl
# Verilog File indenter
# Space for each indent
my $Space = ” “;
# Block begin. For indenting C/C++/Java program use “{”
my $Open = “begin”;
# Block end. For indenting C/C++/Java program use “}”
my $Close = “end”;
# Block statements.
# Note: Each Open keyword must have corresponding Close keyword
my @Openkeywords = qw{ module program package interface class task function };
my @Closekeywords = qw{ endmodule endprogram endpackage endinterface endclass endtask endfunction };
sub indent {
my $indent = 0;
my($infile) = @_;
open(FILE, “$infile”) or die “Couldn’t open file \”$infile\”\n”;
open(TFILE, “>temp”) or die “Couldn’t create temp file\n”;
while($line = <FILE>) {
chomp $line;
$line =~ s/^\s*//;
print TFILE “$line \n”;
}
close TFILE;
close FILE;
open(TFILE, “<temp”);
open(FILE, “>$infile”);
while($line = <TFILE>) {
foreach $Closekey (@Closekeywords) {
if($line =~ /^$Closekey\s/) { $indent–; }
}
if (($line =~ /.*\s+$Close\s/ ) || ($line =~ /^$Close\s/))
{ $indent–; }
for($i=0;$i<$indent;$i++) { print FILE $Space; }
print FILE $line;
foreach $Openkey (@Openkeywords) {
if($line =~ /^$Openkey\s/) { $indent++; }
}
if (($line =~ /.*\s+$Open\s/ )|| ($line =~ /^$Open\s/))
{ $indent++; }
}
close FILE; close TFILE;
print “Indented File \”$infile\”\n”;
}
my $argnum;
if($ARGV[0] eq “”) {
print “Usage : vindent.pl verilogfile.v\n”;
print ” vindent.pl *.v\n”;
print ” vindent.pl *.*v*\n”;
print “Help :\n”;
print ” vindent.pl\n”;
}
foreach $argnum (0 .. $#ARGV) {
my $ifile = $ARGV[$argnum];
&indent($ifile);
}
unlink “temp”;
Download here. vindent.pdf