Automatic indentation and alignment of the Perl script using the OpenGL code

Source: Internet
Author: User
Tags case statement perl script

In the lab for FPGA development, we often use OpenGL. The code specification becomes a problem. As a result, we have written a Perl script to normalize the code, mainly for automatic indentation and alignment.

The Code principle is very simple. It mainly uses regular expressions for matching and replacement.

The Code is as follows. For more information, see Perl:

######################################## ############# Code indent alignment script # functions: auto indent and alignment of the OpenGL Code # This version has not yet processed the case statement # Change: added indentation of assign # running environment: Perl # Running Mode: perl code_indent.pl # Duan Cong # Date: ###################################### ############## my $ filename = 'C: \ Users \ cvcv \ Desktop \ perl_tmp \ code. V '; # input file name my $ outfilename = 'C: \ Users \ cvcv \ Desktop \ perl_tmp \ out. V '; # output file name #---------------------------------- ------------------------ # The test file contains the die "File $ filename does not exist! \ N "unless-e $ filename; # open the file open (vfile," <", $ filename) | die" An error occurred while opening the file! \ N "; open (ofile,"> ", $ outfilename) | die" An error occurred while opening the file! \ N "; # variable definition $ begin_cnt = 0; $ if_cnt = 0; $ autoindent_space =" "X4; # Number of automatically indented spaces (Tab width) $ last_line = ""; $ assign_start = 0; # -------------------------------------------------------- # Read and process a file while (<vfile>) {$ line = $ _; if (/\ s * \ B (input | output | Reg | wire) \ B/I) {$ line = ~ S/^ \ s + //; my @ result = ($ line = ~ /\ S * (input | output | Reg | wire | (?: Output \ s + REG) \ s * (\ [\ D + \: \ D + \])? \ S *((?! Reg | wire) \ W +) ([,;]?) /Ig); # print length ($ result [0]), "[email protected] \ n"; if ($ # result> 0) {$ line = $ result [0]. "" X (20-length ($ result [0]); $ line. = $ result [1]. "" X (20-length ($ result [1]); $ line. = $ result [2]. "" X (20-length ($ result [2]); $ line. = $ result [3]. "\ n" ;}$ last_line =$ _;} elsif (/(parameter )? \ S * (\ W +) \ s * \ = \ s * (\ D + \ '[hbhb] [0-9a-fa-f _] +) \ s * ([,;]?) /I) {$ line = ~ S/^ \ s + //; my @ result = ($ line = ~ /(Parameter )? \ S * (\ W +) \ s * \ = \ s * (\ D + \ '[hbhb] [0-9a-fa-f _] +) \ s * ([,;]?) /Ig); # Print $ # result. "[email protected] \ n"; if ($ # result> 0) {$ line = $ result [0]. "" X (20-length ($ result [0]); $ line. = $ result [1]. "" X (35-length ($ result [1]); $ line. = "= ". $ result [2]. "" X (18-length ($ result [2]); $ line. = $ result [3]. "\ n" ;}$ last_line =$ _;} elsif (/\ s * \ B (module | endmodule | always | assign) \ B/I) {# matching module | always | assign $ line = ~ S/^ \ s + //; If (/\ s * \ bassign \ B \ s + \ W/I) {# match to assign $ line = ~ S/\ s * \ bassign \ B \ s + (\ W)/assign $1/I; $ assign_start = 1; # print ;}$ last_line =$ _;} elsif (/\ bbegin \ B/I) {# match to begin $ begin_cnt ++; my $ tmp_space = $ autoindent_space x $ begin_cnt; $ line = ~ S/^ \ s */$ tmp_space/; $ last_line = $ _;} elsif (/\ bend \ B/I) {# match to endif ($ if_cnt> 0) {$ if_cnt --;} My $ tmp_space = $ autoindent_space x $ begin_cnt; $ begin_cnt --; $ line = ~ S/^ \ s */$ tmp_space/; $ last_line =$ _;} elsif (/\ BIF \ s * \ (/I) {# match to if $ if_cnt ++; my $ tmp_space = $ autoindent_space X ($ begin_cnt> 0? ($ Begin_cnt + 1): $ begin_cnt); $ line = ~ S/^ \ s */$ tmp_space/; $ last_line =$ _;} elsif (/\ belse \ s */I) {# match elsemy $ tmp_space = $ autoindent_space X ($ begin_cnt> 0? ($ Begin_cnt + 1): $ begin_cnt); $ line = ~ S/^ \ s */$ tmp_space/; $ last_line =$ _;} elsif (/^ \ s * $ /) {# match to blank line $ line = "\ n";} else {# Print $ if_cnt, "--", $ _; if ($ assign_start) {my $ tmp_space = $ autoindent_space X2; $ line = ~ S/^ \ s */$ tmp_space/; If (/". *; \ s * $"/) {$ assign_start = 0 ;}} elsif ($ last_line = ~ /(\ BIF \ s * \ () | (\ belse \ s *)/) {$ if_cnt --; my $ tmp_space = $ begin_cnt> 0? ($ Begin_cnt + 2): $ autoindent_space; $ line = ~ S/^ \ s */$ tmp_space/;} else {my $ tmp_space = $ begin_cnt> 0? $ Autoindent_space X ($ begin_cnt + 1): ""; $ line = ~ S/^ \ s */$ tmp_space/;} $ last_line =$ _;} $ text. = $ line ;}# close the file close vfile; select ofile; print $ text; close ofile; select stdout; print "after processing, the file is output to $ outfilename \ n ";


Automatic indentation and alignment of the Perl script using the OpenGL code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.