#!/bin/perl my @Open_Key = qw{ covergroup case module program package interface class task function fork }; my @CloseKey = qw{ endgroup endcase endmodule endprogram endpackage endinterface endclass endtask endfunction join }; my $indent=0; while () { $line=$_; chomp($line); $line =~ s/^\s*//; &s_trim_comment($line, $line_1); foreach $Key ( @CloseKey ) { if($line_1 =~ /^[\s]*$Key[:\s]/) { if($indent >= 1) {$indent--;} else { die("negative indent $indent\n"); } } }; # if($line_1 =~ /[^a-z]*end[^a-zA-Z_0-9]/) &s_exact_match($line_1, "end", $res); if($res == 1) { $indent--; } else { &curly_braces($line_1, $curly_score); if($curly_score == -1) { $indent--; } } for($i=0; $i < $indent; $i++) {print(" ");} print("$line\n"); #indent open filter #interface AXI_vif #( #virtual interface AXI_vif m_vif; foreach $Key ( @Open_Key ) { #remove some key words, because I look only for function without the virtual ##or protoected etc ... $line_1 =~ s/protected //; $line_1 =~ s/virtual //; if($line_1 =~ /[\s]*^$Key[:\s]/) { if($line_1 !~ /interface [ \s]*[a-zA-Z0-9_][a-zA-Z0-9_]* [ \s]*[a-zA-Z0-9_].*;/) { $indent++; } } }; # if($line_1 =~ /[\s]*begin[^a-zA-Z_0-9]/) &s_exact_match($line_1, "begin", $res); if($res == 1) { $indent++; } else { &curly_braces($line_1, $curly_score); if($curly_score == +1) { $indent++; } } }# end while sub s_trim_comment { my $ix=index($_[0], "\/\/"); if($ix >= 0) { if($ix == 0) { $_[1] = ""; } else { $_[1] =substr($_[0], 0, $ix-1); } } else { $_[1] =$_[0]; } $_[1] = $_[1] . " "; } sub s_exact_match { my $ix = index($_[0], $_[1]); my $c=""; my $l=0; if( $ix >=0 ) { $l=length($_[1]) + $ix; $c=substr($_[0], $l, 1); if($c =~ /[a-zA-Z0-9_]/) { $_[2]=0; } else { if($ix == 0) {$_[2]=1;} else { $l=$ix - 1; $c=substr($_[0], $l, 1); if($c =~ /[a-zA-Z0-9_]/) { $_[2]=0; } else { $_[2]=1; } } } } else { $_[2]=0; } } #0 line from SV code, 1 is the indent value add (0, 1), 2 for {} sub curly_braces { my $ix=0; my @a=split(/\/\//, $_[0]); my $l=length($a[0]); $_[1]=0; my $c=""; for($ix=0; $ix < $l; $ix++) { $c=substr($a[0], $ix, 1); if($c eq '{' || $c eq '(') {$_[1]++;} if($c eq '}' || $c eq ')') {$_[1]--;} } #for if($_[1] != 0 && $_[1] != 1 && $_[1] != -1) { print("$_[1] "); die("can not handle curly braces\n"); } }