├── .github └── workflows │ └── main-push.yml ├── .gitignore ├── README.md ├── bin ├── verilog-format-LINUX.zip └── verilog-format-WIN.zip ├── build-dist ├── images └── verilog-format.gif ├── launch-linux ├── build-dist └── verilog-format ├── pom.xml ├── src └── main │ └── java │ └── net │ └── ericsonj │ ├── commonscli │ ├── CommonsCLI.java │ └── ConsoleApplication.java │ ├── util │ └── StringHelper.java │ └── verilog │ ├── FileFormat.java │ ├── FormatSetting.java │ ├── Global.java │ ├── IndentationStyle.java │ ├── LineIndentable.java │ ├── StatementState.java │ ├── StyleImp.java │ ├── VerilogBlock.java │ ├── VerilogFile.java │ ├── VerilogFormat.java │ ├── VerilogHelper.java │ ├── decorations │ ├── AbstractLineDecoration.java │ ├── AbstractModuleAlign.java │ ├── AlignBlockingAssignments.java │ ├── AlignConsecutive.java │ ├── AlignLineComment.java │ ├── AlignNoBlockingAssignments.java │ ├── ModuleAlign.java │ ├── ModuleInstantiation.java │ ├── SpacesBeforeIfStatement.java │ ├── SpacesBlockingAssignment.java │ ├── SpacesInParentheses.java │ ├── SpacesInSquareBrackets.java │ ├── SpacesNoBlockingAssignment.java │ └── SpacesTrailingComment.java │ └── statements │ ├── AbstractStatement.java │ ├── Always.java │ ├── AlwaysState.java │ ├── BlockComment.java │ ├── BlockCommentState.java │ ├── Case.java │ ├── CaseState.java │ ├── For.java │ ├── ForState.java │ ├── Forever.java │ ├── ForeverState.java │ ├── Function.java │ ├── FunctionState.java │ ├── If.java │ ├── IfState.java │ ├── Initial.java │ ├── InitialState.java │ ├── Module.java │ ├── ModuleState.java │ ├── Repeat.java │ ├── RepeatState.java │ ├── Task.java │ ├── TaskState.java │ ├── While.java │ └── WhileState.java └── verilog ├── .verilog-format.properties ├── fsmtx.v └── genrom.v /.github/workflows/main-push.yml: -------------------------------------------------------------------------------- 1 | on: 2 | push: 3 | branches: [ master ] 4 | 5 | jobs: 6 | run: 7 | name: Build project 8 | strategy: 9 | matrix: 10 | os: [ubuntu-latest] 11 | runs-on: ${{ matrix.os }} 12 | steps: 13 | - name: Checkout 14 | uses: actions/checkout@v3 15 | - name: Install toolchain 16 | run: sudo apt update && sudo apt install maven 17 | - name: Build project 18 | run: | 19 | perl -0777 -pe 's{\s*.*?}{ ($a = $&) !~ /launch4j-maven-plugin/ && $a || "" }gse' pom.xml > pom_new.xml && mv pom_new.xml pom.xml 20 | mvn clean package 21 | - name: publish 22 | env: 23 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 24 | run: | 25 | cd target 26 | RELEASE_FILE="verilog-format-1.0.1-full.jar" 27 | RELEASE_NAME="verilog-format.jar" 28 | RELEASE_TAG="$(date +%y%m%d)" 29 | curl -sL -XPOST -d '{"tag_name": "'$RELEASE_TAG'"}' \ 30 | -H "Authorization: Bearer $GITHUB_TOKEN" \ 31 | -H 'Content-Type: application/json' \ 32 | "https://api.github.com/repos/${GITHUB_REPOSITORY}/releases" 33 | RELEASE_ID=$(curl -svL https://api.github.com/repos/${GITHUB_REPOSITORY}/releases/tags/$RELEASE_TAG | jq .id) 34 | curl -sL -XPOST -T ${RELEASE_FILE}* \ 35 | -H "Authorization: token $GITHUB_TOKEN" \ 36 | -H "Content-Type:application/octet-stream" \ 37 | "https://uploads.github.com/repos/${GITHUB_REPOSITORY}/releases/$RELEASE_ID/assets?&name=$RELEASE_NAME" 38 | shell: bash 39 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | nbactions.xml 3 | .idea/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Verilog Format 2 | 3 | Console application for apply format to verilog file. 4 | 5 | ![sample](images/verilog-format.gif) 6 | 7 | ## How to use 8 | 9 | Application options: 10 | 11 | ``` 12 | usage: [java -jar verilog-format.jar|./verilog-format|verilog-format.exe] 13 | [-f ] [-h] [-p] [-s ] [-v] 14 | -f,--format verilog file 15 | -h,--help print this message 16 | -p,--print print file formated 17 | -s,--settings settings config 18 | -v,--version verilog-format version 19 | ``` 20 | 21 | ## Examples 22 | 23 | ```sh 24 | ## Print input_file.v formatted 25 | $ ./verilog-format -p -f input_file.v -s verilog-format.properties 26 | 27 | ## Format input_file.v 28 | $ ./verilog-format -f input_file.v -s verilog-format.properties 29 | 30 | ## Format input_file.v 31 | ## If .verilog-format.properties exist in project folder, this is used, 32 | ## otherwise default setting is used.. 33 | $ ./verilog-format -f input_file.v 34 | 35 | ``` 36 | 37 | ## Install in Linux 38 | 39 | 1. Clone repository. 40 | 41 | `$ git clone https://github.com/ericsonj/verilog-format.git` 42 | 43 | 2. Install verilog-format 44 | 45 | `$ cd verilog-format/bin/` 46 | `$ sudo mkdir /opt/verilog-format` 47 | `$ sudo unzip verilog-format-LINUX.zip -d /opt/verilog-format/` 48 | 49 | 3. Execute like java 50 | 51 | `$ java -jar /opt/verilog-format/verilog-format.jar` 52 | 53 | 4. Execute like linux script 54 | 55 | `$ /opt/verilog-format/verilog-format` 56 | 57 | 5. Install in system 58 | 59 | `$ sudo cp /opt/verilog-format/verilog-format /usr/bin/` 60 | 61 | ## Install in Windows 62 | 63 | 1. Clone repository or download [verilog-format-WIN.zip](bin/verilog-format-WIN.zip) 64 | 65 | 2. Unzip and copy in your preferer folder. 66 | 67 | ## Build project 68 | 69 | For build de project, Maven is needed. 70 | 71 | `$ cd verilog-format` 72 | `$ mvn clean package` 73 | `$ ls target/` 74 | 75 | ## Verilog-Format Style Options 76 | 77 | This options are setting in `.verilog-format.properties` file. 78 | 79 | ### Example 80 | 81 | ```properties 82 | ## File .verilog-format.properties 83 | IndentWidth=4 84 | IndentType=space 85 | SpacesBeforeTrailingComments=0 86 | SpacesAfterTrailingComments=0 87 | AlignLineComments=true 88 | AlignNoBlockingAssignments=true 89 | AlignBlockingAssignments=true 90 | SpacesInParentheses=false 91 | SpacesInSquareBrackets=false 92 | ``` 93 | 94 | --- 95 | ### IndentWidth=[number] 96 | 97 | ```verilog 98 | // IndentWidth=4 #(default) 99 | always @(posedge clk) 100 | if (load == 1) 101 | bitc <= 0; 102 | else if (load == 0 && clk_baud == 1) 103 | bitc <= bitc + 1; 104 | 105 | // IndentWidth=1 106 | always @(posedge clk) 107 | if (load == 1) 108 | bitc <= 0; 109 | else if (load == 0 && clk_baud == 1) 110 | bitc <= bitc + 1; 111 | ``` 112 | --- 113 | ### IndentType=[space|tab] 114 | ```verilog 115 | // IndentType=space #(default) 116 | always @(posedge clk) 117 | if (load == 1) 118 | bitc <= 0; 119 | else if (load == 0 && clk_baud == 1) 120 | bitc <= bitc + 1; 121 | 122 | // IndentType=tab # not recommended yet 123 | always @(posedge clk) 124 | if (load == 1) 125 | bitc <= 0; 126 | else if (load == 0 && clk_baud == 1) 127 | bitc <= bitc + 1; 128 | ``` 129 | --- 130 | ### SpacesInParentheses=[true|false] 131 | ```verilog 132 | // SpacesInParentheses=false #(default) 133 | always @(posedge clk) 134 | if (load == 1) 135 | 136 | // SpacesInParentheses=true 137 | always @( posedge clk ) 138 | if ( load == 1 ) 139 | ``` 140 | --- 141 | 142 | ### SpacesInSquareBrackets=[true|false] 143 | ```verilog 144 | // SpacesInSquareBrackets=false #(default) 145 | reg [DW-1:0] rom [0:NPOS-1]; 146 | 147 | always @(posedge clk) begin 148 | data <= rom[addr]; 149 | end 150 | 151 | // SpacesInSquareBrackets=true 152 | reg [ DW-1:0 ] rom [ 0:NPOS-1 ]; 153 | 154 | always @(posedge clk) begin 155 | data <= rom[ addr ]; 156 | ``` 157 | --- 158 | ### AlignBlockingAssignments=[true|false] 159 | ```verilog 160 | // AlignBlockingAssignments=true #(default) 161 | assign load = (state == START) ? 1 : 0; 162 | assign baud_en = (state == IDLE) ? 0 : 1; 163 | 164 | // AlignBlockingAssignments=false 165 | assign load = (state == START) ? 1 : 0; 166 | assign baud_en = (state == IDLE) ? 0 : 1; 167 | 168 | ``` 169 | --- 170 | ### AlignNoBlockingAssignments=[true|false] 171 | ```verilog 172 | // AlignNoBlockingAssignments=true #(default) 173 | state_ts <= IDLE; 174 | state_pad <= IDLE; 175 | state_wait <= IDLE; 176 | 177 | // AlignNoBlockingAssignments=false 178 | state_ts <= IDLE; 179 | state_pad <= IDLE; 180 | state_wait <= IDLE; 181 | ``` 182 | --- 183 | ### AlignLineComments=[true|false] 184 | ```verilog 185 | // AlignLineComments=false #(default) 186 | always @(posedge clk) // always 187 | if (load == 1) // if 188 | bitc <= 0; // 189 | else if (load == 0 && clk_baud == 1) // else if 190 | bitc <= bitc + 1; // 191 | 192 | // AlignLineComments=true 193 | always @(posedge clk) // always 194 | if (load == 1) // if 195 | bitc <= 0; // 196 | else if (load == 0 && clk_baud == 1) // else if 197 | bitc <= bitc + 1; // 198 | ``` 199 | --- 200 | ### SpacesBeforeTrailingComments=[number] 201 | ```verilog 202 | // SpacesBeforeTrailingComments=1 #(default) 203 | localparam IDLE = 0; //IDLE 204 | 205 | // SpacesBeforeTrailingComments=0 206 | localparam IDLE = 0;//IDLE 207 | ``` 208 | --- 209 | ### SpacesAfterTrailingComments=[number] 210 | ```verilog 211 | // SpacesAfterTrailingComments=0 #(default) 212 | localparam IDLE = 0; //IDLE 213 | 214 | // SpacesAfterTrailingComments=3 215 | localparam IDLE = 0; // IDLE 216 | ``` 217 | --- 218 | -------------------------------------------------------------------------------- /bin/verilog-format-LINUX.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ericsonj/verilog-format/c1608af5ab57bac113a977d5242012f07a48aa69/bin/verilog-format-LINUX.zip -------------------------------------------------------------------------------- /bin/verilog-format-WIN.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ericsonj/verilog-format/c1608af5ab57bac113a977d5242012f07a48aa69/bin/verilog-format-WIN.zip -------------------------------------------------------------------------------- /build-dist: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | #MOV TO BIN 4 | cd bin/ 5 | 6 | # BUILD FOR LINUX 7 | cp ../target/*-full.jar verilog-format.jar 8 | cp ../launch-linux/verilog-format . 9 | zip verilog-format-LINUX.zip verilog-format.jar verilog-format 10 | rm verilog-format.jar verilog-format 11 | 12 | # BUILD FOR WIN 13 | cp ../target/verilog-format.exe . 14 | zip verilog-format-WIN.zip verilog-format.exe 15 | rm verilog-format.exe -------------------------------------------------------------------------------- /images/verilog-format.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ericsonj/verilog-format/c1608af5ab57bac113a977d5242012f07a48aa69/images/verilog-format.gif -------------------------------------------------------------------------------- /launch-linux/build-dist: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | cp ../target/*-full.jar verilog-format.jar 4 | -------------------------------------------------------------------------------- /launch-linux/verilog-format: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | java=java 4 | if test -n "$JAVA_HOME"; then 5 | java="$JAVA_HOME/bin/java" 6 | fi 7 | exec "$java" -jar /opt/verilog-format/verilog-format.jar "$@" 8 | exit 1 9 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | net.ericsonj 5 | verilog-format 6 | 1.0.1 7 | jar 8 | 9 | UTF-8 10 | 1.8 11 | 1.8 12 | 13 | 14 | 15 | 16 | 17 | commons-cli 18 | commons-cli 19 | 1.4 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | maven-assembly-plugin 28 | 29 | 30 | 31 | net.ericsonj.verilog.VerilogFormat 32 | 33 | 34 | 35 | jar-with-dependencies 36 | 37 | ${project.artifactId}-${project.version}-full 38 | false 39 | 40 | 41 | 42 | make-assembly 43 | package 44 | 45 | single 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | com.akathist.maven.plugins.launch4j 54 | launch4j-maven-plugin 55 | 1.5.2 56 | 57 | 58 | l4j-cli 59 | package 60 | 61 | launch4j 62 | 63 | 64 | console 65 | target/verilog-format.exe 66 | target/${project.artifactId}-${project.version}-full.jar 67 | false 68 | Error in launch4j plugin 69 | 70 | net.ericsonj.verilog.VerilogFormat 71 | 72 | 73 | 1.7.0 74 | 1.8.0 75 | 512 76 | 1024 77 | 78 | 79 | 1.0.0.0 80 | 1.0.0.0 81 | des 82 | Copyright (c) 2019 83 | ericsonj 84 | 1.0.0.0 85 | ${project.version} 86 | verilog-format 87 | verilog-format 88 | verilog-format.exe 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/commonscli/CommonsCLI.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.commonscli; 2 | 3 | import java.util.Properties; 4 | import org.apache.commons.cli.CommandLine; 5 | import org.apache.commons.cli.Option; 6 | import org.apache.commons.cli.Options; 7 | 8 | /** 9 | * 10 | * @author ejoseph 11 | */ 12 | public class CommonsCLI extends ConsoleApplication { 13 | 14 | /** 15 | * @param args the command line arguments 16 | */ 17 | // public static void main(String[] args) { 18 | // CommonsCLI cli = new CommonsCLI(); 19 | // cli.process(args); 20 | // } 21 | 22 | @Override 23 | public Options getOptions() { 24 | Options options = new Options(); 25 | options.addOption("h", "help", false, "print this message"); 26 | options.addOption("a", "all", false, "do not hide entries starting with ."); 27 | options.addOption("A", "almost-all", false, "do not list implied . and .."); 28 | options.addOption("b", "escape", false, "print octal escapes for nongraphic " 29 | + "characters"); 30 | 31 | options.addOption(Option.builder() 32 | .longOpt("block-size") 33 | .desc("use SIZE-byte blocks") 34 | .hasArg() 35 | .build()); 36 | 37 | options.addOption(Option.builder("D").argName("property=value") 38 | .hasArgs() 39 | .valueSeparator() 40 | .desc("use value for given property") 41 | .build()); 42 | 43 | options.addOption(Option.builder("logfile") 44 | .argName("file") 45 | .hasArg() 46 | .desc("use given file for log").build()); 47 | 48 | options.addOption("B", "ignore-backups", false, "do not list implied entried " 49 | + "ending with ~"); 50 | 51 | options.addOption("c", false, "with -lt: sort by, and show, ctime (time of last " 52 | + "modification of file status information) with " 53 | + "-l:show ctime and sort by name otherwise: sort " 54 | + "by ctime"); 55 | 56 | options.addOption("C", false, "list entries by columns"); 57 | 58 | return options; 59 | } 60 | 61 | @Override 62 | public void processOptions(CommandLine line) { 63 | 64 | if (isArgsEmpty()) { 65 | printHelp(); 66 | return; 67 | } 68 | 69 | if (line.hasOption("block-size")) { 70 | System.out.println(line.getOptionValue("block-size")); 71 | } 72 | 73 | if (line.hasOption("a")) { 74 | System.out.println("all: " + line.getOptionValue("a")); 75 | } 76 | 77 | if (line.hasOption("D")) { 78 | Properties p = line.getOptionProperties("D"); 79 | System.out.println("D: property2 = |" + p.getProperty("property2") + "|"); 80 | System.out.println("D: property1 = |" + p.getProperty("property1") + "|"); 81 | } 82 | 83 | if (line.hasOption("logfile")) { 84 | System.out.println("logfile: |" + line.getOptionValue("logfile") + "|"); 85 | } 86 | 87 | if (line.hasOption("help")) { 88 | printHelp(); 89 | } 90 | 91 | } 92 | 93 | } 94 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/commonscli/ConsoleApplication.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.commonscli; 2 | 3 | import org.apache.commons.cli.CommandLine; 4 | import org.apache.commons.cli.CommandLineParser; 5 | import org.apache.commons.cli.DefaultParser; 6 | import org.apache.commons.cli.HelpFormatter; 7 | import org.apache.commons.cli.Options; 8 | import org.apache.commons.cli.ParseException; 9 | 10 | /** 11 | * 12 | * @author ejoseph 13 | */ 14 | public abstract class ConsoleApplication { 15 | 16 | private final CommandLineParser parser; 17 | private Options options; 18 | private String[] args; 19 | 20 | public ConsoleApplication() { 21 | this.parser = new DefaultParser(); 22 | this.options = new Options(); 23 | } 24 | 25 | protected abstract Options getOptions(); 26 | 27 | protected abstract void processOptions(CommandLine line); 28 | 29 | protected String getApplicationName(){ 30 | return getClass().getSimpleName(); 31 | } 32 | 33 | public void process(String[] args) { 34 | this.args = args; 35 | this.options = getOptions(); 36 | try { 37 | CommandLine line = parser.parse(options, args); 38 | processOptions(line); 39 | } catch (ParseException ex) { 40 | // Logger.getLogger(ConsoleApplication.class.getName()).log(Level.SEVERE, null, ex); 41 | System.out.println(ex.getMessage()); 42 | printHelp(); 43 | } 44 | } 45 | 46 | /** 47 | * Print in system out Help. 48 | */ 49 | protected void printHelp() { 50 | HelpFormatter formatter = new HelpFormatter(); 51 | formatter.printHelp(getCmdLineSyntax(),getHeaderHelp(),options,getFooterHelp(),true); 52 | } 53 | 54 | public String[] getArgs() { 55 | return args; 56 | } 57 | 58 | public void setArgs(String[] args) { 59 | this.args = args; 60 | } 61 | 62 | public boolean isArgsEmpty() { 63 | return (this.args.length == 0); 64 | } 65 | 66 | public String getCmdLineSyntax(){ 67 | return "java -jar "+ getApplicationName()+".jar"; 68 | } 69 | 70 | public String getHeaderHelp(){ 71 | return ""; 72 | } 73 | 74 | public String getFooterHelp(){ 75 | return ""; 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/util/StringHelper.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.util; 2 | 3 | import java.util.LinkedList; 4 | 5 | /** 6 | * 7 | * @author Ericson Joseph 8 | * 9 | * Create on Feb 16, 2019 9:16:55 AM 10 | */ 11 | public class StringHelper { 12 | 13 | public static boolean stringMatches(String str, LinkedList matchesOptions) { 14 | return stringMatches(str, matchesOptions.toArray(new String[matchesOptions.size()])); 15 | } 16 | 17 | public static boolean stringMatches(String str, String... matchesOptions) { 18 | for (String matchesOption : matchesOptions) { 19 | if (str.matches(matchesOption)) { 20 | return true; 21 | } 22 | } 23 | return false; 24 | } 25 | 26 | public static String getSpaces(int count) { 27 | String spaces = ""; 28 | for (int i = 0; i < count; i++) { 29 | spaces += " "; 30 | } 31 | return spaces; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/FileFormat.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog; 2 | 3 | import java.util.LinkedList; 4 | 5 | /** 6 | * 7 | * @author Ericson Joseph 8 | */ 9 | public class FileFormat { 10 | 11 | public static final String SPACES_AFTER_TRAILING_COMMENTS = "SpacesAfterTrailingComments"; 12 | public static final String SPACES_BEFORE_TRAILING_COMMENTS = "SpacesBeforeTrailingComments"; 13 | public static final String SPACES_BEFORE_IF_STATEMENT = "SpacesBeforeIfStatement"; 14 | public static final String SPACES_BLOKING_ASSIGNMENTS = "SpacesBlockingAssignment"; 15 | public static final String SPACES_NOBLOKING_ASSIGNMENTS = "SpacesNoBlockingAssignment"; 16 | public static final String SPACES_IN_PARENTHESES = "SpacesInParentheses"; 17 | public static final String SPACES_IN_SQUARE_BRACKETS = "SpacesInSquareBrackets"; 18 | public static final String ALIGN_BLOKING_ASSIGNMENTS = "AlignBlockingAssignments"; 19 | public static final String ALIGN_NOBLOKING_ASSIGNMENTS = "AlignNoBlockingAssignments"; 20 | public static final String ALIGN_LINE_COMMENTS = "AlignLineComments"; 21 | 22 | public static final char INDENT_TAB = '\t'; 23 | public static final char INDENT_SPACE = ' '; 24 | public static final char LF = '\n'; 25 | public static final String CRLF = "\r\n"; 26 | 27 | private FormatSetting setting; 28 | 29 | public LinkedList states; 30 | 31 | private int countIndent; 32 | 33 | public FileFormat(FormatSetting setting) { 34 | this.setting = setting; 35 | this.countIndent = 0; 36 | this.states = new LinkedList<>(); 37 | } 38 | 39 | public char getIndentType() { 40 | String value = this.setting.getStringValue("IndentType", "space"); 41 | if (value.equals("tab")) { 42 | return INDENT_TAB; 43 | } 44 | return INDENT_SPACE; 45 | } 46 | 47 | public int getIndentSize() { 48 | return this.setting.getIntValue("IndentWidth", 4); 49 | } 50 | 51 | public void addCountIndent() { 52 | this.countIndent++; 53 | } 54 | 55 | public void resCountIndent() { 56 | this.countIndent--; 57 | } 58 | 59 | public int getCountIndent() { 60 | return countIndent; 61 | } 62 | 63 | public LinkedList getStates() { 64 | return states; 65 | } 66 | 67 | public void setCountIndent(int countIndent) { 68 | this.countIndent = countIndent; 69 | } 70 | 71 | public FormatSetting getSetting() { 72 | return setting; 73 | } 74 | 75 | public int getSpacesAfterTrailingComments() { 76 | return this.setting.getIntValue(SPACES_AFTER_TRAILING_COMMENTS, 0); 77 | } 78 | 79 | public int getSpacesBeforeTrailingComments() { 80 | return this.setting.getIntValue(SPACES_BEFORE_TRAILING_COMMENTS, 1); 81 | } 82 | 83 | public int getSpacesBeforeIfStatement() { 84 | return this.setting.getIntValue(SPACES_BEFORE_IF_STATEMENT, 1); 85 | } 86 | 87 | public int getSpacesBlockingAssignment() { 88 | return this.setting.getIntValue(SPACES_BLOKING_ASSIGNMENTS, 1); 89 | } 90 | 91 | public int getSpacesNoBlockingAssignment() { 92 | return this.setting.getIntValue(SPACES_NOBLOKING_ASSIGNMENTS, 1); 93 | } 94 | 95 | public boolean getAlignBlockingAssignments() { 96 | return this.setting.getBooleanValue(ALIGN_BLOKING_ASSIGNMENTS, true); 97 | } 98 | 99 | public boolean getAlignNoBlockingAssignments() { 100 | return this.setting.getBooleanValue(ALIGN_NOBLOKING_ASSIGNMENTS, true); 101 | } 102 | 103 | public boolean getAlignLineComments() { 104 | return this.setting.getBooleanValue(ALIGN_LINE_COMMENTS, false); 105 | } 106 | 107 | public boolean getSpacesInParentheses() { 108 | return this.setting.getBooleanValue(SPACES_IN_PARENTHESES, false); 109 | } 110 | 111 | public boolean getSpacesInSquareBrackets() { 112 | return this.setting.getBooleanValue(SPACES_IN_SQUARE_BRACKETS, false); 113 | } 114 | 115 | } -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/FormatSetting.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog; 2 | 3 | import java.io.File; 4 | import java.io.FileNotFoundException; 5 | import java.io.FileReader; 6 | import java.io.IOException; 7 | import java.util.Properties; 8 | 9 | /** 10 | * 11 | * @author Ericson Joseph 12 | * 13 | * Create on Feb 17, 2019 11:12:50 PM 14 | */ 15 | public class FormatSetting { 16 | 17 | public static final String FILE_PROP = "verilog-format.properties"; 18 | 19 | private File file; 20 | private Properties prop; 21 | private boolean filePresent; 22 | 23 | public FormatSetting() { 24 | this(new File(FILE_PROP)); 25 | } 26 | 27 | public FormatSetting(File file) { 28 | this.file = file; 29 | this.prop = new Properties(); 30 | if (this.file == null) { 31 | this.filePresent = false; 32 | return; 33 | } 34 | if (!this.file.exists()) { 35 | this.filePresent = false; 36 | return; 37 | } 38 | try { 39 | this.prop.load(new FileReader(file)); 40 | this.filePresent = true; 41 | return; 42 | } catch (FileNotFoundException ex) { 43 | } catch (IOException ex) { 44 | } 45 | this.filePresent = false; 46 | } 47 | 48 | public String getStringValue(String key, String defaultValue) { 49 | return prop.getProperty(key, defaultValue); 50 | } 51 | 52 | public int getIntValue(String key, int defaultValue) { 53 | int value = Integer.parseInt(prop.getProperty(key, String.valueOf(defaultValue))); 54 | return value; 55 | } 56 | 57 | public boolean getBooleanValue(String key, boolean defaultValue) { 58 | String value = getStringValue(key, String.valueOf(defaultValue)); 59 | return Boolean.valueOf(value); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/Global.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog; 2 | 3 | /** 4 | * 5 | * @author Ericson Joseph 6 | * 7 | * Create on Feb 16, 2019 8:41:34 PM 8 | */ 9 | public class Global { 10 | 11 | public static final String VERSION = "1.0.1"; 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/IndentationStyle.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog; 2 | 3 | import java.util.LinkedList; 4 | import net.ericsonj.verilog.statements.Always; 5 | import net.ericsonj.verilog.statements.BlockComment; 6 | import net.ericsonj.verilog.statements.Case; 7 | import net.ericsonj.verilog.statements.For; 8 | import net.ericsonj.verilog.statements.Forever; 9 | import net.ericsonj.verilog.statements.Function; 10 | import net.ericsonj.verilog.statements.If; 11 | import net.ericsonj.verilog.statements.Initial; 12 | import net.ericsonj.verilog.statements.Module; 13 | import net.ericsonj.verilog.statements.Repeat; 14 | import net.ericsonj.verilog.statements.Task; 15 | import net.ericsonj.verilog.statements.While; 16 | 17 | /** 18 | * 19 | * @author Ericson Joseph 20 | */ 21 | public class IndentationStyle implements StyleImp { 22 | 23 | private LinkedList indents; 24 | 25 | public IndentationStyle() { 26 | this.indents = new LinkedList<>(); 27 | 28 | this.indents.add(new BlockComment()); 29 | this.indents.add(new For()); 30 | this.indents.add(new While()); 31 | this.indents.add(new Repeat()); 32 | this.indents.add(new Forever()); 33 | this.indents.add(new Case()); 34 | this.indents.add(new If()); 35 | this.indents.add(new Task()); 36 | this.indents.add(new Function()); 37 | this.indents.add(new Initial()); 38 | this.indents.add(new Always()); 39 | this.indents.add(new Module()); 40 | } 41 | 42 | @Override 43 | public void applyStyle(FileFormat format, LinkedList buffer) { 44 | for (int index = 0; index < buffer.size(); index++) { 45 | String line = buffer.get(index); 46 | String newLine = processLine(format, line); 47 | buffer.remove(index); 48 | buffer.add(index, newLine); 49 | } 50 | } 51 | 52 | private String processLine(FileFormat format, String line) { 53 | 54 | int recursive = 0; 55 | if (format.states.isEmpty() || format.states.size() == 1) { 56 | recursive = 1; 57 | } else { 58 | recursive = format.states.size(); 59 | } 60 | 61 | for (int i = 0; i < recursive; i++) { 62 | for (LineIndentable indent : indents) { 63 | String indentLine = indent.indentLine(format, line.trim()); 64 | if (indentLine != null) { 65 | return indentLine; 66 | } 67 | } 68 | } 69 | 70 | return indent(format, line); 71 | } 72 | 73 | private String indent(FileFormat format, String line) { 74 | StringBuilder sb = new StringBuilder(line); 75 | for (int i = 0; i < (format.getIndentSize() * format.getCountIndent()); i++) { 76 | sb.insert(0, format.getIndentType()); 77 | } 78 | return sb.toString(); 79 | } 80 | 81 | } 82 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/LineIndentable.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog; 2 | 3 | /** 4 | * 5 | * @author Ericson Joseph 6 | */ 7 | public abstract class LineIndentable { 8 | 9 | public abstract String indentLine(FileFormat format, String line); 10 | 11 | public String indent(FileFormat format, String line) { 12 | StringBuilder sb = new StringBuilder(line); 13 | for (int i = 0; i < (format.getIndentSize() * format.getCountIndent()); i++) { 14 | sb.insert(0, format.getIndentType()); 15 | } 16 | return sb.toString(); 17 | } 18 | 19 | public String indent(FileFormat format, int baseIndent, String line) { 20 | StringBuilder sb = new StringBuilder(line); 21 | for (int i = 0; i < (format.getIndentSize() * baseIndent); i++) { 22 | sb.insert(0, format.getIndentType()); 23 | } 24 | return sb.toString(); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/StatementState.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog; 2 | 3 | /** 4 | * 5 | * @author Ericson Joseph 6 | */ 7 | public class StatementState { 8 | 9 | private final String stateName; 10 | private int baseIndent; 11 | 12 | public StatementState(String stateName, int baseIndent) { 13 | this.stateName = stateName; 14 | this.baseIndent = baseIndent; 15 | } 16 | 17 | public String getStateName() { 18 | return stateName; 19 | } 20 | 21 | public int getBaseIndent() { 22 | return baseIndent; 23 | } 24 | 25 | public void setBaseIndent(int baseIndent) { 26 | this.baseIndent = baseIndent; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/StyleImp.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog; 2 | 3 | import java.util.LinkedList; 4 | 5 | /** 6 | * 7 | * @author Ericson Joseph 8 | */ 9 | public interface StyleImp { 10 | 11 | public void applyStyle(FileFormat format, LinkedList buffer); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/VerilogBlock.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog; 2 | 3 | import java.util.LinkedList; 4 | 5 | /** 6 | * 7 | * @author ericson 8 | */ 9 | public class VerilogBlock { 10 | 11 | private String header; 12 | private boolean headerIndentBefore; 13 | private String end; 14 | private boolean endIndentBefore; 15 | private boolean withoutEnd = false; 16 | // private boolean headerDetect = false; 17 | 18 | public VerilogBlock(String header, boolean headerIndentBefore, String end, boolean endIndentBefore) { 19 | this.header = header; 20 | this.headerIndentBefore = headerIndentBefore; 21 | this.end = end; 22 | this.endIndentBefore = endIndentBefore; 23 | if (this.end == null) { 24 | withoutEnd = true; 25 | } 26 | } 27 | 28 | public VerilogBlock(String header, String end) { 29 | this(header, true, end, false); 30 | } 31 | 32 | // public void process(FileFormat format, LinkedList buffer) { 33 | // for (int index = 0; index < buffer.size(); index++) { 34 | // String newLine = getIndentLine(format, buffer, index); 35 | // buffer.remove(index); 36 | // buffer.add(index, newLine); 37 | // } 38 | // } 39 | 40 | public String process(FileFormat format, String line) { 41 | 42 | String tmp = line; 43 | if (line.matches(header)) { 44 | // this.headerDetect = true; 45 | 46 | if(headerIndentBefore) tmp = indentLine(format, line); 47 | format.addCountIndent(); 48 | if(!headerIndentBefore) tmp = indentLine(format, line); 49 | 50 | } else if (end != null && line.matches(end) && format.getCountIndent() > 0) { 51 | 52 | if(endIndentBefore) tmp = indentLine(format, line); 53 | format.resCountIndent(); 54 | if(!endIndentBefore) tmp = indentLine(format, line); 55 | 56 | } else if (line.isEmpty()) { 57 | tmp = line; 58 | } else { 59 | tmp = indentLine(format, line); 60 | if (withoutEnd && format.getCountIndent() > 0) { 61 | format.resCountIndent(); 62 | } 63 | } 64 | return tmp; 65 | } 66 | 67 | private String indentLine(FileFormat format, String line) { 68 | StringBuilder sb = new StringBuilder(line); 69 | for (int i = 0; i < (format.getIndentSize() * format.getCountIndent()); i++) { 70 | sb.insert(0, format.getIndentType()); 71 | } 72 | return sb.toString(); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/VerilogFile.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.File; 5 | import java.io.FileNotFoundException; 6 | import java.io.FileReader; 7 | import java.io.FileWriter; 8 | import java.io.IOException; 9 | import java.util.LinkedList; 10 | import java.util.logging.Level; 11 | import java.util.logging.Logger; 12 | 13 | /** 14 | * 15 | * @author Ericson Joseph 16 | */ 17 | public class VerilogFile { 18 | 19 | public LinkedList memFile; 20 | public LinkedList styles; 21 | public final FileFormat format; 22 | public String pathname; 23 | 24 | public VerilogFile(String pathname, FileFormat format) { 25 | this.memFile = new LinkedList<>(); 26 | this.styles = new LinkedList<>(); 27 | this.format = format; 28 | this.pathname = pathname; 29 | try (FileReader fileReader = new FileReader(new File(pathname)); 30 | BufferedReader bufferReader = new BufferedReader(fileReader)) { 31 | String line; 32 | while ((line = bufferReader.readLine()) != null) { 33 | memFile.add(line.trim()); 34 | } 35 | } catch (FileNotFoundException ex) { 36 | Logger.getLogger(VerilogFormat.class.getName()).log(Level.SEVERE, null, ex); 37 | } catch (IOException ex) { 38 | Logger.getLogger(VerilogFormat.class.getName()).log(Level.SEVERE, null, ex); 39 | } 40 | } 41 | 42 | public void format() { 43 | for (StyleImp style : styles) { 44 | style.applyStyle(format, memFile); 45 | } 46 | } 47 | 48 | public void addStyle(StyleImp style) { 49 | this.styles.add(style); 50 | } 51 | 52 | public void print() { 53 | for (String line : memFile) { 54 | System.out.println(line); 55 | } 56 | } 57 | 58 | public void overWrite() { 59 | 60 | try { 61 | FileWriter formatFile = new FileWriter(new File(pathname), false); 62 | for (String string : memFile) { 63 | formatFile.write(string + FileFormat.LF); 64 | } 65 | formatFile.close(); 66 | } catch (IOException ex) { 67 | Logger.getLogger(VerilogFile.class.getName()).log(Level.SEVERE, null, ex); 68 | } 69 | 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/VerilogFormat.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog; 2 | 3 | import java.io.File; 4 | import net.ericsonj.commonscli.ConsoleApplication; 5 | import net.ericsonj.verilog.decorations.AbstractModuleAlign; 6 | import net.ericsonj.verilog.decorations.AlignBlockingAssignments; 7 | import net.ericsonj.verilog.decorations.AlignLineComment; 8 | import net.ericsonj.verilog.decorations.AlignNoBlockingAssignments; 9 | import net.ericsonj.verilog.decorations.ModuleAlign; 10 | import net.ericsonj.verilog.decorations.ModuleInstantiation; 11 | import net.ericsonj.verilog.decorations.SpacesBeforeIfStatement; 12 | import net.ericsonj.verilog.decorations.SpacesTrailingComment; 13 | import net.ericsonj.verilog.decorations.SpacesBlockingAssignment; 14 | import net.ericsonj.verilog.decorations.SpacesInParentheses; 15 | import net.ericsonj.verilog.decorations.SpacesInSquareBrackets; 16 | import net.ericsonj.verilog.decorations.SpacesNoBlockingAssignment; 17 | import org.apache.commons.cli.CommandLine; 18 | import org.apache.commons.cli.Option; 19 | import org.apache.commons.cli.Options; 20 | 21 | /** 22 | * 23 | * @author Ericson Joseph 24 | */ 25 | public class VerilogFormat extends ConsoleApplication { 26 | 27 | private boolean printFileFormated = false; 28 | private FormatSetting settings; 29 | 30 | /** 31 | * @param args the command line arguments 32 | */ 33 | public static void main(String[] args) { 34 | VerilogFormat vf = new VerilogFormat(); 35 | vf.process(args); 36 | } 37 | 38 | @Override 39 | protected Options getOptions() { 40 | Options options = new Options(); 41 | options.addOption("h", "help", false, "print this message"); 42 | options.addOption("v", "version", false, "verilog-format version"); 43 | options.addOption(Option.builder("f") 44 | .longOpt("format") 45 | .argName("pathname") 46 | .hasArg() 47 | .desc("Verilog file") 48 | .build()); 49 | options.addOption("p", "print", false, "print file formated"); 50 | options.addOption(Option.builder("s") 51 | .longOpt("settings") 52 | .argName(FormatSetting.FILE_PROP) 53 | .hasArg() 54 | .desc("Settings config") 55 | .build()); 56 | return options; 57 | } 58 | 59 | @Override 60 | protected void processOptions(CommandLine line) { 61 | if (isArgsEmpty()) { 62 | printHelp(); 63 | return; 64 | } 65 | 66 | if (line.hasOption("h")) { 67 | printHelp(); 68 | return; 69 | } 70 | 71 | if (line.hasOption("v")) { 72 | System.out.println(Global.VERSION); 73 | return; 74 | } 75 | 76 | if (line.hasOption("p")) { 77 | printFileFormated = true; 78 | } 79 | 80 | if (line.hasOption("s")) { 81 | String pathname = line.getOptionValue("s"); 82 | settings = new FormatSetting(new File(pathname)); 83 | } else { 84 | 85 | File localSetting = new File("." + FormatSetting.FILE_PROP); 86 | if (localSetting.exists()) { 87 | settings = new FormatSetting(localSetting); 88 | } else { 89 | settings = new FormatSetting(null); 90 | } 91 | 92 | } 93 | 94 | if (line.hasOption("f")) { 95 | String pathname = line.getOptionValue("f"); 96 | File file = new File(pathname); 97 | if (!file.exists() || file.isDirectory()) { 98 | printError("File not found"); 99 | System.exit(0); 100 | return; 101 | } 102 | formatFile(file); 103 | } 104 | } 105 | 106 | private void formatFile(File file) { 107 | FileFormat format = new FileFormat(this.settings); 108 | VerilogFile vFile = new VerilogFile(file.getAbsolutePath(), format); 109 | vFile.addStyle(new IndentationStyle()); 110 | vFile.addStyle(new ModuleAlign()); 111 | vFile.addStyle(new SpacesTrailingComment()); 112 | vFile.addStyle(new SpacesBeforeIfStatement()); 113 | vFile.addStyle(new SpacesBlockingAssignment()); 114 | vFile.addStyle(new SpacesNoBlockingAssignment()); 115 | vFile.addStyle(new SpacesInParentheses()); 116 | vFile.addStyle(new SpacesInSquareBrackets()); 117 | vFile.addStyle(new AlignBlockingAssignments()); 118 | vFile.addStyle(new AlignNoBlockingAssignments()); 119 | vFile.addStyle(new AlignLineComment()); 120 | vFile.format(); 121 | if (printFileFormated) { 122 | vFile.print(); 123 | } else { 124 | vFile.overWrite(); 125 | } 126 | } 127 | 128 | private void printError(String message) { 129 | System.err.println(message); 130 | } 131 | 132 | @Override 133 | protected String getApplicationName() { 134 | return "verilog-format"; //To change body of generated methods, choose Tools | Templates. 135 | } 136 | 137 | @Override 138 | public String getCmdLineSyntax() { 139 | return "[java -jar " + getApplicationName() + ".jar|./verilog-format|verilog-format.exe]"; 140 | } 141 | 142 | } 143 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/VerilogHelper.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | 6 | /** 7 | * 8 | * @author Ericson Joseph 9 | */ 10 | public class VerilogHelper { 11 | 12 | public Set keyWords = new HashSet<>(); 13 | private static final VerilogHelper instance; 14 | 15 | static { 16 | instance = new VerilogHelper(); 17 | } 18 | 19 | private VerilogHelper() { 20 | keyWords.add("always"); 21 | keyWords.add("and"); 22 | keyWords.add("assign"); 23 | keyWords.add("automatic"); 24 | keyWords.add("begin"); 25 | keyWords.add("buf"); 26 | keyWords.add("bufif0"); 27 | keyWords.add("bufif1"); 28 | keyWords.add("case"); 29 | keyWords.add("casex"); 30 | keyWords.add("casez"); 31 | keyWords.add("cell"); 32 | keyWords.add("cmos"); 33 | keyWords.add("config"); 34 | keyWords.add("deassign"); 35 | keyWords.add("default"); 36 | keyWords.add("defparam"); 37 | keyWords.add("design"); 38 | keyWords.add("disable"); 39 | keyWords.add("edge"); 40 | keyWords.add("else"); 41 | keyWords.add("end"); 42 | keyWords.add("endcase"); 43 | keyWords.add("endconfig"); 44 | keyWords.add("endfunction"); 45 | keyWords.add("endgenerate"); 46 | keyWords.add("endmodule"); 47 | keyWords.add("endprimitive"); 48 | keyWords.add("endspecify"); 49 | keyWords.add("endtable"); 50 | keyWords.add("endtask"); 51 | keyWords.add("event"); 52 | keyWords.add("for"); 53 | keyWords.add("force"); 54 | keyWords.add("forever"); 55 | keyWords.add("fork"); 56 | keyWords.add("function"); 57 | keyWords.add("generate"); 58 | keyWords.add("genvar"); 59 | keyWords.add("highz0"); 60 | keyWords.add("highz1"); 61 | keyWords.add("if"); 62 | keyWords.add("ifnone"); 63 | keyWords.add("incdir"); 64 | keyWords.add("include"); 65 | keyWords.add("initial"); 66 | keyWords.add("inout"); 67 | keyWords.add("input"); 68 | keyWords.add("instance"); 69 | keyWords.add("integer"); 70 | keyWords.add("join"); 71 | keyWords.add("large"); 72 | keyWords.add("liblist"); 73 | keyWords.add("library"); 74 | keyWords.add("localparam"); 75 | keyWords.add("macromodule"); 76 | keyWords.add("medium"); 77 | keyWords.add("module"); 78 | keyWords.add("nand"); 79 | keyWords.add("negedge"); 80 | keyWords.add("nmos"); 81 | keyWords.add("nor"); 82 | keyWords.add("noshowcancelledno"); 83 | keyWords.add("not"); 84 | keyWords.add("notif0"); 85 | keyWords.add("notif1"); 86 | keyWords.add("or"); 87 | keyWords.add("output"); 88 | keyWords.add("parameter"); 89 | keyWords.add("pmos"); 90 | keyWords.add("posedge"); 91 | keyWords.add("primitive"); 92 | keyWords.add("pull0"); 93 | keyWords.add("pull1"); 94 | keyWords.add("pulldown"); 95 | keyWords.add("pullup"); 96 | keyWords.add("pulsestyle_oneventglitch"); 97 | keyWords.add("pulsestyle_ondetectglitch"); 98 | keyWords.add("remos"); 99 | keyWords.add("real"); 100 | keyWords.add("realtime"); 101 | keyWords.add("reg"); 102 | keyWords.add("release"); 103 | keyWords.add("repeat"); 104 | keyWords.add("rnmos"); 105 | keyWords.add("rpmos"); 106 | keyWords.add("rtran"); 107 | keyWords.add("rtranif0"); 108 | keyWords.add("rtranif1"); 109 | keyWords.add("scalared"); 110 | keyWords.add("showcancelled"); 111 | keyWords.add("signed"); 112 | keyWords.add("small"); 113 | keyWords.add("specify"); 114 | keyWords.add("specparam"); 115 | keyWords.add("strong0"); 116 | keyWords.add("strong1"); 117 | keyWords.add("supply0"); 118 | keyWords.add("supply1"); 119 | keyWords.add("table"); 120 | keyWords.add("task"); 121 | keyWords.add("time"); 122 | keyWords.add("tran"); 123 | keyWords.add("tranif0"); 124 | keyWords.add("tranif1"); 125 | keyWords.add("tri"); 126 | keyWords.add("tri0"); 127 | keyWords.add("tri1"); 128 | keyWords.add("triand"); 129 | keyWords.add("trior"); 130 | keyWords.add("trireg"); 131 | keyWords.add("unsigned"); 132 | keyWords.add("use"); 133 | keyWords.add("vectored"); 134 | keyWords.add("wait"); 135 | keyWords.add("wand"); 136 | keyWords.add("weak0"); 137 | keyWords.add("weak1"); 138 | keyWords.add("while"); 139 | keyWords.add("wire"); 140 | keyWords.add("wor"); 141 | keyWords.add("xnor"); 142 | keyWords.add("xor"); 143 | } 144 | 145 | public static VerilogHelper getInstance() { 146 | return instance; 147 | } 148 | 149 | public static boolean isKeyWord(String word) { 150 | return getInstance().keyWords.contains(word); 151 | } 152 | 153 | } 154 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/decorations/AbstractLineDecoration.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.decorations; 2 | 3 | import java.util.LinkedList; 4 | import net.ericsonj.verilog.FileFormat; 5 | import net.ericsonj.verilog.StyleImp; 6 | 7 | /** 8 | * 9 | * @author ejoseph 10 | */ 11 | public abstract class AbstractLineDecoration implements StyleImp { 12 | 13 | public enum BLOCK_COMMNET_STATE { 14 | INIT, 15 | IN_BLOCK_COMMNET 16 | } 17 | 18 | private BLOCK_COMMNET_STATE state = BLOCK_COMMNET_STATE.INIT; 19 | 20 | @Override 21 | public void applyStyle(FileFormat format, LinkedList buffer) { 22 | for (int i = 0; i < buffer.size(); i++) { 23 | String line = buffer.get(i); 24 | if (startBlockComment(line)) { 25 | state = BLOCK_COMMNET_STATE.IN_BLOCK_COMMNET; 26 | } 27 | if (endBlockComment(line)) { 28 | state = BLOCK_COMMNET_STATE.INIT; 29 | } 30 | if (state == BLOCK_COMMNET_STATE.INIT 31 | || (state == BLOCK_COMMNET_STATE.IN_BLOCK_COMMNET && inBlockComment())) { 32 | line = decorateLine(format, line, i); 33 | buffer.remove(i); 34 | buffer.add(i, line); 35 | } 36 | } 37 | } 38 | 39 | public abstract String decorateLine(FileFormat format, String line, int lineIndex); 40 | 41 | public abstract boolean inBlockComment(); 42 | 43 | public boolean startBlockComment(String line) { 44 | return line.matches("/\\*.*"); 45 | } 46 | 47 | public boolean endBlockComment(String line) { 48 | return line.matches(".*\\*/"); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/decorations/AbstractModuleAlign.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.decorations; 2 | 3 | import java.util.LinkedList; 4 | import java.util.regex.Matcher; 5 | import java.util.regex.Pattern; 6 | import net.ericsonj.verilog.FileFormat; 7 | import net.ericsonj.verilog.StyleImp; 8 | import net.ericsonj.verilog.VerilogFile; 9 | import net.ericsonj.verilog.VerilogHelper; 10 | 11 | /** 12 | * 13 | * @author ejoseph 14 | */ 15 | public class AbstractModuleAlign implements StyleImp { 16 | 17 | private enum STATE { 18 | INIT, 19 | IN_COMMENT, 20 | IN_MODULE, 21 | IN_GEN_MODULE 22 | } 23 | 24 | private STATE state = STATE.INIT; 25 | 26 | @Override 27 | public void applyStyle(FileFormat format, LinkedList buffer) { 28 | for (int i = 0; i < buffer.size(); i++) { 29 | String line = buffer.get(i); 30 | switch (state) { 31 | case INIT: 32 | if (isLineComment(line)) { 33 | } else if (isStartBlockComment(line)) { 34 | state = STATE.IN_COMMENT; 35 | } else if (isModule(line)) { 36 | state = STATE.IN_MODULE; 37 | System.out.println(line); 38 | } else if (isGenModule(line)) { 39 | state = STATE.IN_GEN_MODULE; 40 | } else { 41 | 42 | } 43 | break; 44 | case IN_COMMENT: 45 | if (isEndBlockComment(line)) { 46 | state = STATE.INIT; 47 | } 48 | break; 49 | case IN_MODULE: 50 | System.out.println(line); 51 | if (isEndModule(line)) { 52 | state = STATE.INIT; 53 | System.out.println("-------"); 54 | } 55 | break; 56 | case IN_GEN_MODULE: 57 | 58 | break; 59 | default: 60 | throw new AssertionError(state.name()); 61 | 62 | } 63 | } 64 | } 65 | 66 | private boolean isLineComment(String line) { 67 | return line.matches("^[ ]*//.*"); 68 | } 69 | 70 | private boolean isStartBlockComment(String line) { 71 | return line.matches("^[ ]*/\\*.*"); 72 | } 73 | 74 | private boolean isEndBlockComment(String line) { 75 | return line.matches(".*\\*/.*"); 76 | } 77 | 78 | private boolean isModule(String line) { 79 | return line.matches("^[ ]*module[ ]+.*"); 80 | } 81 | 82 | private boolean isEndModule(String line) { 83 | return line.matches(".*[;].*"); 84 | } 85 | 86 | private boolean isGenModule(String line) { 87 | Pattern p = Pattern.compile("^[ ]*([A-Za-z0-9_-])[ ]+.*"); 88 | Matcher m = p.matcher(line); 89 | if (m.find()) { 90 | String name = m.group(1); 91 | if (!VerilogHelper.isKeyWord(name)) { 92 | return true; 93 | } 94 | } 95 | return false; 96 | } 97 | 98 | } 99 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/decorations/AlignBlockingAssignments.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.decorations; 2 | 3 | import java.util.LinkedList; 4 | import net.ericsonj.verilog.FileFormat; 5 | 6 | /** 7 | * 8 | * @author ericson 9 | */ 10 | public class AlignBlockingAssignments extends AlignConsecutive { 11 | 12 | public AlignBlockingAssignments() { 13 | super(".*[^<]", "=", ".*[;]"); 14 | } 15 | 16 | @Override 17 | public void applyStyle(FileFormat format, LinkedList buffer) { 18 | if (format.getAlignBlockingAssignments()) { 19 | super.applyStyle(format, buffer); //To change body of generated methods, choose Tools | Templates. 20 | } 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/decorations/AlignConsecutive.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.decorations; 2 | 3 | import java.util.LinkedList; 4 | import net.ericsonj.util.StringHelper; 5 | import net.ericsonj.verilog.FileFormat; 6 | 7 | /** 8 | * 9 | * @author ericson 10 | */ 11 | public class AlignConsecutive extends AbstractLineDecoration { 12 | 13 | private static final String COMMNET = "[ ]*[//|/*].*"; 14 | 15 | public enum STATE { 16 | INIT, 17 | IS_CONSECUTIVE, 18 | CONSECUTIVE 19 | } 20 | 21 | private STATE state = STATE.INIT; 22 | private int alignIndex; 23 | private int startLineIndex; 24 | private int endLineIndex; 25 | private LinkedList assignmentsBlocks = new LinkedList<>(); 26 | 27 | private String beforeKeyRegex; 28 | private String key; 29 | private String afterKeyRegex; 30 | 31 | public AlignConsecutive(String beforeKeyRegex, String KeyRegex, String afterKeyRegex) { 32 | this.beforeKeyRegex = beforeKeyRegex; 33 | this.key = KeyRegex; 34 | this.afterKeyRegex = afterKeyRegex; 35 | } 36 | 37 | private String getLinePattern() { 38 | return beforeKeyRegex + key + afterKeyRegex; 39 | } 40 | 41 | @Override 42 | public void applyStyle(FileFormat format, LinkedList buffer) { 43 | super.applyStyle(format, buffer); //To change body of generated methods, choose Tools | Templates. 44 | 45 | assignmentsBlocks.forEach((assignmentsBlock) -> { 46 | 47 | for (int i = assignmentsBlock.getStartLineIndex(); i <= assignmentsBlock.getEndLineIndex(); i++) { 48 | String line = buffer.get(i); 49 | int assignmentIdx = line.indexOf(getKey()); 50 | if (assignmentIdx < assignmentsBlock.getAlignValue()) { 51 | line = line.replaceAll(getKey(), StringHelper.getSpaces(assignmentsBlock.getAlignValue() - assignmentIdx) + getKey()); 52 | if (getKey().equals("=")) { 53 | line = line.replaceAll("[ ]*([=!<>&|~^+\\-*/]+)[ ]*=[ ]*", " $1= "); 54 | } 55 | } 56 | buffer.remove(i); 57 | buffer.add(i, line); 58 | } 59 | 60 | }); 61 | } 62 | 63 | @Override 64 | public String decorateLine(FileFormat format, String line, int lineIndex) { 65 | switch (state) { 66 | case INIT: 67 | if (line.matches(getLinePattern()) || line.matches(getLinePattern() + COMMNET)) { 68 | alignIndex = line.indexOf(getKey()); 69 | startLineIndex = lineIndex; 70 | state = STATE.IS_CONSECUTIVE; 71 | } 72 | break; 73 | case IS_CONSECUTIVE: 74 | if (line.matches(getLinePattern()) || line.matches(getLinePattern() + COMMNET)) { 75 | int aux = line.indexOf(getKey()); 76 | if (aux > alignIndex) { 77 | alignIndex = aux; 78 | } 79 | endLineIndex = lineIndex; 80 | state = STATE.CONSECUTIVE; 81 | } else { 82 | state = STATE.INIT; 83 | } 84 | break; 85 | case CONSECUTIVE: 86 | if (line.matches(getLinePattern()) || line.matches(getLinePattern() + COMMNET)) { 87 | int aux = line.indexOf(getKey()); 88 | if (aux > alignIndex) { 89 | alignIndex = aux; 90 | } 91 | endLineIndex = lineIndex; 92 | state = STATE.CONSECUTIVE; 93 | } else { 94 | assignmentsBlocks.add(new AssignmentLine(startLineIndex, endLineIndex, alignIndex)); 95 | alignIndex = 0; 96 | state = STATE.INIT; 97 | } 98 | break; 99 | default: 100 | throw new AssertionError(state.name()); 101 | } 102 | return line; 103 | } 104 | 105 | @Override 106 | public boolean inBlockComment() { 107 | return false; 108 | } 109 | 110 | public String getBeforeKeyRegex() { 111 | return beforeKeyRegex; 112 | } 113 | 114 | public void setBeforeKeyRegex(String beforeKeyRegex) { 115 | this.beforeKeyRegex = beforeKeyRegex; 116 | } 117 | 118 | public String getKey() { 119 | return key; 120 | } 121 | 122 | public void setKey(String key) { 123 | this.key = key; 124 | } 125 | 126 | public String getAfterKeyRegex() { 127 | return afterKeyRegex; 128 | } 129 | 130 | public void setAfterKeyRegex(String afterKeyRegex) { 131 | this.afterKeyRegex = afterKeyRegex; 132 | } 133 | 134 | public class AssignmentLine { 135 | 136 | private int startLineIndex; 137 | private int endLineIndex; 138 | private int alignValue; 139 | 140 | public AssignmentLine(int startLineIndex, int endLineIndex, int alignValue) { 141 | this.startLineIndex = startLineIndex; 142 | this.endLineIndex = endLineIndex; 143 | this.alignValue = alignValue; 144 | } 145 | 146 | public int getStartLineIndex() { 147 | return startLineIndex; 148 | } 149 | 150 | public void setStartLineIndex(int startLineIndex) { 151 | this.startLineIndex = startLineIndex; 152 | } 153 | 154 | public int getEndLineIndex() { 155 | return endLineIndex; 156 | } 157 | 158 | public void setEndLineIndex(int endLineIndex) { 159 | this.endLineIndex = endLineIndex; 160 | } 161 | 162 | public int getAlignValue() { 163 | return alignValue; 164 | } 165 | 166 | public void setAlignValue(int alignValue) { 167 | this.alignValue = alignValue; 168 | } 169 | 170 | @Override 171 | public String toString() { 172 | return "AssignmentLine{" + "startLineIndex=" + startLineIndex + ", endLineIndex=" + endLineIndex + ", alignValue=" + alignValue + '}'; 173 | } 174 | 175 | } 176 | 177 | } 178 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/decorations/AlignLineComment.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.decorations; 2 | 3 | import java.util.LinkedList; 4 | import net.ericsonj.verilog.FileFormat; 5 | 6 | /** 7 | * 8 | * @author ericson 9 | */ 10 | public class AlignLineComment extends AlignConsecutive { 11 | 12 | public AlignLineComment() { 13 | super(".*", "//", ".*"); 14 | } 15 | 16 | @Override 17 | public void applyStyle(FileFormat format, LinkedList buffer) { 18 | if (format.getAlignLineComments()) { 19 | super.applyStyle(format, buffer); //To change body of generated methods, choose Tools | Templates. 20 | } 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/decorations/AlignNoBlockingAssignments.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.decorations; 2 | 3 | import java.util.LinkedList; 4 | import net.ericsonj.verilog.FileFormat; 5 | 6 | /** 7 | * 8 | * @author ericson 9 | */ 10 | public class AlignNoBlockingAssignments extends AlignConsecutive { 11 | 12 | public AlignNoBlockingAssignments() { 13 | super(".*", "<=", ".*[;]"); 14 | } 15 | 16 | @Override 17 | public void applyStyle(FileFormat format, LinkedList buffer) { 18 | if (format.getAlignNoBlockingAssignments()) { 19 | super.applyStyle(format, buffer); //To change body of generated methods, choose Tools | Templates. 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/decorations/ModuleAlign.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.decorations; 2 | 3 | import java.util.LinkedHashMap; 4 | import java.util.LinkedList; 5 | import java.util.StringTokenizer; 6 | import java.util.regex.Matcher; 7 | import java.util.regex.Pattern; 8 | import net.ericsonj.verilog.FileFormat; 9 | import net.ericsonj.verilog.StyleImp; 10 | 11 | /** 12 | * 13 | * @author Ericson Joseph 14 | * 15 | * Create on Feb 18, 2019 10:37:47 PM 16 | */ 17 | public class ModuleAlign implements StyleImp { 18 | 19 | public enum COMMENT_STATE { 20 | BLOCK_COMMENT, 21 | LINE_COMMNET 22 | } 23 | 24 | private String keyWord; 25 | 26 | public ModuleAlign() { 27 | this("module"); 28 | } 29 | 30 | public ModuleAlign(String keyWord) { 31 | this.keyWord = keyWord; 32 | } 33 | 34 | private LinkedHashMap commnets = new LinkedHashMap<>(); 35 | 36 | @Override 37 | public void applyStyle(FileFormat format, LinkedList buffer) { 38 | 39 | String align = format.getSetting().getStringValue("ModuleAlign", "BAS_Align"); 40 | 41 | // if (align.equals("BAS_Align")) { 42 | // process(buffer); 43 | // } 44 | int startModuleLine = getIdxLineMatches(buffer, "[ ]*" + keyWord + "[ ]+[a-zA-Z0-9-_,;&$# ]*.*", 0); 45 | if (startModuleLine == -1) { 46 | return; 47 | } 48 | 49 | int endModuleLine = getIdxLineMatches(buffer, ".*[)][ ]*[;][ ]*[//|/*]*.*", startModuleLine); 50 | if (endModuleLine == -1) { 51 | return; 52 | } 53 | 54 | removeComments(buffer, startModuleLine, endModuleLine); 55 | 56 | String moduleDef = getModuleInLine(buffer, startModuleLine, endModuleLine); 57 | 58 | LinkedList resp = BASAlign(moduleDef); 59 | 60 | int commentAlign = getMostLargeLineSize(resp); 61 | 62 | for (int i = 0; i < resp.size(); i++) { 63 | String line = resp.get(i); 64 | String[] words = line.split(" "); 65 | String lastWord = words[words.length - 1]; 66 | if (commnets.containsKey(lastWord)) { 67 | LinkedList lines = getCommentAlign(line, commentAlign, commnets.get(lastWord)); 68 | resp.remove(i); 69 | resp.addAll(i, lines); 70 | } else { 71 | if (lastWord.matches("[^ ]+[)];")) { 72 | String newWordKey = lastWord.replace(");", ""); 73 | if (commnets.containsKey(newWordKey)) { 74 | LinkedList lines = getCommentAlign(line, commentAlign, commnets.get(newWordKey)); 75 | resp.remove(i); 76 | resp.addAll(i, lines); 77 | } 78 | } 79 | } 80 | } 81 | 82 | replaceInBuffer(buffer, startModuleLine, endModuleLine, resp); 83 | 84 | } 85 | 86 | private int getIdxLineMatches(LinkedList buffer, String regex, int offset) { 87 | for (int i = offset; i < buffer.size(); i++) { 88 | String line = buffer.get(i); 89 | if (line.matches(regex)) { 90 | return i; 91 | } 92 | } 93 | return -1; 94 | } 95 | 96 | private String getModuleInLine(LinkedList buffer, int startModuleLine, int endModuleLine) { 97 | StringBuilder sb = new StringBuilder(); 98 | 99 | int startIndet = getIndent(buffer.get(startModuleLine)); 100 | 101 | for (int i = startModuleLine; i < endModuleLine + 1; i++) { 102 | sb.append(buffer.get(i).trim()); 103 | sb.append(' '); 104 | } 105 | 106 | String moduleDef = sb.toString().trim(); 107 | 108 | moduleDef = orderLine(moduleDef); 109 | 110 | moduleDef = indent(startIndet, moduleDef); 111 | 112 | return moduleDef; 113 | 114 | } 115 | 116 | private void replaceInBuffer(LinkedList buffer, int startModuleLine, int endModuleLine, LinkedList bufferSrc) { 117 | int linesRemove = endModuleLine - startModuleLine + 1; 118 | for (int i = 0; i < linesRemove; i++) { 119 | buffer.remove(startModuleLine); 120 | } 121 | buffer.addAll(startModuleLine, bufferSrc); 122 | } 123 | 124 | private String indent(int indent, String line) { 125 | StringBuilder sb = new StringBuilder(line); 126 | for (int i = 0; i < indent; i++) { 127 | sb.insert(0, ' '); 128 | } 129 | return sb.toString(); 130 | } 131 | 132 | private int getIndent(String line) { 133 | int indent = 0; 134 | for (int i = 0; i < line.length(); i++) { 135 | if (line.charAt(i) == ' ') { 136 | indent++; 137 | } else { 138 | break; 139 | } 140 | } 141 | return indent; 142 | } 143 | 144 | private LinkedList BASAlign(String moduleInLine) { 145 | 146 | LinkedList resp = new LinkedList<>(); 147 | 148 | boolean moduleWithParam = moduleInLine.contains("#("); 149 | 150 | int indentSize = 0; 151 | int initParamBrackt = 0; 152 | int endParamBrackt = 0; 153 | 154 | if (moduleWithParam) { 155 | 156 | initParamBrackt = moduleInLine.indexOf("#("); 157 | endParamBrackt = moduleInLine.indexOf(")"); 158 | 159 | resp.add(moduleInLine.substring(0, initParamBrackt + 2)); 160 | 161 | String paramArgs = moduleInLine.substring(initParamBrackt + 2, endParamBrackt); 162 | 163 | StringTokenizer st = new StringTokenizer(paramArgs, ","); 164 | int count = st.countTokens(); 165 | if (count == 1) { 166 | resp.set(0, resp.getFirst() + paramArgs + moduleInLine.charAt(endParamBrackt)); 167 | } else { 168 | for (int i = 0; i < count - 1; i++) { 169 | String arg = st.nextToken(); 170 | if (i == 0) { 171 | resp.set(0, resp.getFirst() + arg + ","); 172 | } else { 173 | resp.add(indent(initParamBrackt + 1, arg + ",")); 174 | } 175 | } 176 | 177 | String arg = st.nextToken(); 178 | resp.addLast(indent(initParamBrackt + 1, arg) + moduleInLine.charAt(endParamBrackt)); 179 | } 180 | indentSize = initParamBrackt; 181 | 182 | } 183 | 184 | int initBracket = moduleInLine.indexOf("(", moduleWithParam ? endParamBrackt : 0); 185 | int endBracket = moduleInLine.lastIndexOf(")"); 186 | 187 | int endParamLine = 0; 188 | if (moduleWithParam) { 189 | resp.add(indent(indentSize, moduleInLine.substring(moduleWithParam ? endParamBrackt + 1 : 0, initBracket + 1))); 190 | endParamLine = resp.size() - 1; 191 | indentSize++; 192 | } else { 193 | resp.add(moduleInLine.substring(0, initBracket + 1)); 194 | indentSize = initBracket; 195 | } 196 | 197 | String moduleArgs = moduleInLine.substring(initBracket + 1, endBracket); 198 | if (moduleArgs.isEmpty()) { 199 | resp.set(endParamLine, resp.get(endParamLine) + moduleArgs + moduleInLine.substring(endBracket)); 200 | return resp; 201 | } 202 | 203 | StringTokenizer st = new StringTokenizer(moduleArgs, ","); 204 | int count = st.countTokens(); 205 | if (count == 1) { 206 | resp.set(endParamLine, resp.get(endParamLine) + moduleArgs + moduleInLine.substring(endBracket)); 207 | return resp; 208 | } 209 | for (int i = 0; i < count - 1; i++) { 210 | String arg = st.nextToken(); 211 | if (i == 0) { 212 | resp.set(endParamLine, resp.get(endParamLine) + arg + ","); 213 | } else { 214 | resp.add(indent(indentSize, arg + ",")); 215 | } 216 | } 217 | 218 | String arg = st.nextToken(); 219 | resp.addLast(indent(indentSize, arg) + moduleInLine.substring(endBracket)); 220 | 221 | return resp; 222 | 223 | } 224 | 225 | private void removeComments(LinkedList buffer, int startModuleLine, int endModuleLine) { 226 | 227 | COMMENT_STATE commentState = COMMENT_STATE.LINE_COMMNET; 228 | String blockComment = ""; 229 | String blockKey = ""; 230 | 231 | for (int i = startModuleLine; i < endModuleLine + 1; i++) { 232 | String line = buffer.get(i); 233 | line = orderLine(line); 234 | 235 | switch (commentState) { 236 | case LINE_COMMNET: 237 | if (line.matches(".*/\\*.*\\*/")) { 238 | Pattern p = Pattern.compile(".*[ ](.*)[ ](/\\*.*\\*/)"); 239 | Matcher m = p.matcher(line); 240 | if (m.find()) { 241 | String key = m.group(1); 242 | String comment = m.group(2); 243 | commnets.put(key, comment); 244 | } 245 | } else if (line.matches(".*//.*")) { 246 | Pattern p = Pattern.compile(".*[ ](.*)[ ](//.*)"); 247 | Matcher m = p.matcher(line); 248 | if (m.find()) { 249 | String key = m.group(1); 250 | String comment = m.group(2); 251 | commnets.put(key, comment); 252 | } 253 | } else if (line.matches(".*/\\*.*")) { 254 | Pattern p = Pattern.compile(".*[ ](.*)[ ](/\\*.*)"); 255 | Matcher m = p.matcher(line); 256 | if (m.find()) { 257 | String key = m.group(1); 258 | String comment = m.group(2); 259 | blockComment += comment; 260 | blockKey = key; 261 | commnets.put(key, comment); 262 | commentState = COMMENT_STATE.BLOCK_COMMENT; 263 | } 264 | } 265 | break; 266 | case BLOCK_COMMENT: 267 | if (line.matches(".*\\*/")) { 268 | blockComment += "\\" + line; 269 | commnets.put(blockKey, blockComment); 270 | commentState = COMMENT_STATE.LINE_COMMNET; 271 | } else { 272 | blockComment += "\\" + line; 273 | line = ""; 274 | } 275 | break; 276 | default: 277 | throw new AssertionError(commentState.name()); 278 | } 279 | 280 | line = line.replaceAll("/\\*.*", ""); 281 | line = line.replaceAll("//.*", ""); 282 | line = line.replaceAll(".*\\*/", ""); 283 | buffer.remove(i); 284 | buffer.add(i, line); 285 | } 286 | } 287 | 288 | private String orderLine(String line) { 289 | int startIndet = getIndent(line); 290 | String orderLine = line.replaceAll("[#][ ]*[(]", "#("); 291 | orderLine = orderLine.replaceAll("[ ]+", " "); 292 | orderLine = orderLine.replaceAll("[(][ ]*", "("); 293 | orderLine = orderLine.replaceAll("[ ]*[)]", ")"); 294 | orderLine = orderLine.replaceAll("[)][ ]*[;]", ");"); 295 | orderLine = orderLine.replaceAll("[ ]*[,][ ]*", ", "); 296 | orderLine = indent(startIndet, orderLine.trim()); 297 | return orderLine; 298 | } 299 | 300 | private int getMostLargeLineSize(LinkedList buffer) { 301 | int size = 0; 302 | for (String line : buffer) { 303 | if (line.length() > size) { 304 | size = line.length(); 305 | } 306 | } 307 | return size; 308 | } 309 | 310 | private LinkedList getCommentAlign(String line, int lineAlign, String comment) { 311 | LinkedList lines = new LinkedList<>(); 312 | int spaces = lineAlign - line.length() + 1; 313 | if (spaces == -1) { 314 | lines.add(line); 315 | return lines; 316 | } 317 | 318 | StringTokenizer st = new StringTokenizer(comment, "\\"); 319 | int count = st.countTokens(); 320 | for (int j = 0; j < count; j++) { 321 | StringBuilder sb = new StringBuilder(); 322 | if (j == 0) { 323 | sb.append(line); 324 | } else { 325 | for (int i = 0; i < line.length(); i++) { 326 | sb.append(" "); 327 | } 328 | } 329 | for (int i = 0; i < spaces; i++) { 330 | sb.append(" "); 331 | } 332 | sb.append(st.nextToken()); 333 | lines.add(sb.toString()); 334 | } 335 | 336 | return lines; 337 | 338 | } 339 | 340 | } 341 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/decorations/ModuleInstantiation.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.decorations; 2 | 3 | import java.util.LinkedList; 4 | import java.util.regex.Matcher; 5 | import java.util.regex.Pattern; 6 | import net.ericsonj.verilog.FileFormat; 7 | import net.ericsonj.verilog.StyleImp; 8 | import net.ericsonj.verilog.VerilogHelper; 9 | 10 | /** 11 | * 12 | * @author Ericson Joseph 13 | */ 14 | public class ModuleInstantiation implements StyleImp { 15 | 16 | @Override 17 | public void applyStyle(FileFormat format, LinkedList buffer) { 18 | for (String line : buffer) { 19 | 20 | if (line.matches("^[ ]*\\.*") || line.matches("^[ ]*/\\*.*")) { 21 | continue; 22 | } 23 | 24 | Pattern p = Pattern.compile("^[ ]*([0-9A-Za-z-_]+)[ ]+.*"); 25 | Matcher m = p.matcher(line); 26 | if (m.find()) { 27 | String word = m.group(1); 28 | boolean isModuelInst = (line.matches(".*[ ]+[(].*") || line.matches(".*" + word + "[ ]+[#][(].*")); 29 | if (!VerilogHelper.isKeyWord(word) && isModuelInst) { 30 | // System.out.println(line); 31 | ModuleAlign align = new ModuleAlign(word); 32 | align.applyStyle(format, buffer); 33 | break; 34 | } 35 | } 36 | 37 | } 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/decorations/SpacesBeforeIfStatement.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.decorations; 2 | 3 | import net.ericsonj.util.StringHelper; 4 | import net.ericsonj.verilog.FileFormat; 5 | 6 | /** 7 | * 8 | * @author Ericson Joseph 9 | * 10 | * Create on Feb 28, 2019 11:46:28 PM 11 | */ 12 | public class SpacesBeforeIfStatement extends AbstractLineDecoration { 13 | 14 | @Override 15 | public String decorateLine(FileFormat format, String line, int lineIndex) { 16 | int spaces = format.getSpacesBeforeIfStatement(); 17 | String aux = line; 18 | if (line.matches(".*[ |\t]+if\\b[ ]*[(].*") 19 | || line.matches(".*[ |\t]+else\\b[ ]+?") 20 | || line.matches(".*else[ ]+if\\b[ ]*[(].*")) { 21 | aux = line.replaceAll("\\bif\\b[ ]*", "if" + StringHelper.getSpaces(spaces)); 22 | aux = aux.replaceAll("\\belse\\b[ ]*", "else" + StringHelper.getSpaces(spaces)); 23 | } 24 | return aux; 25 | } 26 | 27 | @Override 28 | public boolean inBlockComment() { 29 | return false; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/decorations/SpacesBlockingAssignment.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.decorations; 2 | 3 | import net.ericsonj.util.StringHelper; 4 | import net.ericsonj.verilog.FileFormat; 5 | 6 | /** 7 | * 8 | * @author ericson 9 | */ 10 | public class SpacesBlockingAssignment extends AbstractLineDecoration { 11 | 12 | @Override 13 | public String decorateLine(FileFormat format, String line, int lineIndex) { 14 | if (format.getSpacesBlockingAssignment() == 0) { 15 | return line; 16 | } 17 | if (line.matches(".*[^<]=.*")) { 18 | String aux = line.replaceAll("[ ]*(?&|~^+\\-*/])=", StringHelper.getSpaces(format.getSpacesBlockingAssignment()) + "=").replaceAll("=(?![=])[ ]*", "=" + StringHelper.getSpaces(format.getSpacesBlockingAssignment())); 19 | return aux; 20 | } 21 | return line; 22 | } 23 | 24 | @Override 25 | public boolean inBlockComment() { 26 | return false; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/decorations/SpacesInParentheses.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.decorations; 2 | 3 | import net.ericsonj.verilog.FileFormat; 4 | 5 | /** 6 | * 7 | * @author ericson 8 | */ 9 | public class SpacesInParentheses extends AbstractLineDecoration { 10 | 11 | @Override 12 | public String decorateLine(FileFormat format, String line, int lineIndex) { 13 | if (!line.matches(".*[(].*[)].*")) { 14 | return line; 15 | } 16 | if (format.getSpacesInParentheses()) { 17 | String aux = line.replaceAll("[(][ ]*", "( "); 18 | aux = aux.replaceAll("[ ]*[)]", " )"); 19 | return aux; 20 | } else { 21 | String aux = line.replaceAll("[(][ ]*", "("); 22 | aux = aux.replaceAll("[ ]*[)]", ")"); 23 | return aux; 24 | } 25 | } 26 | 27 | @Override 28 | public boolean inBlockComment() { 29 | return false; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/decorations/SpacesInSquareBrackets.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.decorations; 2 | 3 | import net.ericsonj.verilog.FileFormat; 4 | 5 | /** 6 | * 7 | * @author ericson 8 | */ 9 | public class SpacesInSquareBrackets extends AbstractLineDecoration { 10 | 11 | @Override 12 | public String decorateLine(FileFormat format, String line, int lineIndex) { 13 | 14 | if (!line.matches(".*[\\[].*[\\]].*")) { 15 | return line; 16 | } 17 | 18 | if (format.getSpacesInSquareBrackets()) { 19 | String aux = line.replaceAll("[\\[][ ]*", "[ "); 20 | aux = aux.replaceAll("[ ]*[\\]]", " ]"); 21 | return aux; 22 | } else { 23 | String aux = line.replaceAll("[\\[][ ]*", "["); 24 | aux = aux.replaceAll("[ ]*[\\]]", "]"); 25 | return aux; 26 | } 27 | } 28 | 29 | @Override 30 | public boolean inBlockComment() { 31 | return false; 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/decorations/SpacesNoBlockingAssignment.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.decorations; 2 | 3 | import net.ericsonj.util.StringHelper; 4 | import net.ericsonj.verilog.FileFormat; 5 | 6 | /** 7 | * 8 | * @author ericson 9 | */ 10 | public class SpacesNoBlockingAssignment extends AbstractLineDecoration { 11 | 12 | @Override 13 | public String decorateLine(FileFormat format, String line, int lineIndex) { 14 | if (format.getSpacesNoBlockingAssignment() == 0) { 15 | return line; 16 | } 17 | String aux = line.replaceAll("[ ]*<=[ ]*", StringHelper.getSpaces(format.getSpacesNoBlockingAssignment()) + "<=" + StringHelper.getSpaces(format.getSpacesNoBlockingAssignment())); 18 | return aux; 19 | } 20 | 21 | @Override 22 | public boolean inBlockComment() { 23 | return false; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/decorations/SpacesTrailingComment.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.decorations; 2 | 3 | import net.ericsonj.util.StringHelper; 4 | import net.ericsonj.verilog.FileFormat; 5 | 6 | /** 7 | * 8 | * @author ejoseph 9 | */ 10 | public class SpacesTrailingComment extends AbstractLineDecoration { 11 | 12 | @Override 13 | public String decorateLine(FileFormat format, String line, int LineIndex) { 14 | if (format.getSpacesAfterTrailingComments() == 0) { 15 | return line; 16 | } 17 | 18 | String aux = line; 19 | if (line.matches("[ ]*//.*")) { 20 | if (format.getSpacesAfterTrailingComments() > 0) { 21 | aux = aux.replaceAll("//[ ]*", "//" + StringHelper.getSpaces(format.getSpacesAfterTrailingComments())); 22 | } 23 | return aux; 24 | } 25 | if (line.matches(".*[ ]+//.*")) { 26 | if (format.getSpacesAfterTrailingComments() > 0) { 27 | aux = aux.replaceAll("//[ ]*", "//" + StringHelper.getSpaces(format.getSpacesAfterTrailingComments())); 28 | } 29 | if (format.getSpacesBeforeTrailingComments() > 0) { 30 | aux = aux.replaceAll("[ ]*//", StringHelper.getSpaces(format.getSpacesBeforeTrailingComments()) + "//"); 31 | } 32 | } else if (line.matches(".*[^ ]//.*")) { 33 | if (format.getSpacesAfterTrailingComments() > 0) { 34 | aux = aux.replaceAll("//[ ]*", " //" + StringHelper.getSpaces(format.getSpacesAfterTrailingComments())); 35 | } 36 | if (format.getSpacesBeforeTrailingComments() > 0) { 37 | aux = aux.replaceAll("//", StringHelper.getSpaces(format.getSpacesBeforeTrailingComments()) + "//"); 38 | } 39 | } 40 | 41 | return aux; 42 | } 43 | 44 | @Override 45 | public boolean inBlockComment() { 46 | return false; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/statements/AbstractStatement.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.statements; 2 | 3 | import net.ericsonj.verilog.FileFormat; 4 | import net.ericsonj.verilog.LineIndentable; 5 | import net.ericsonj.verilog.StatementState; 6 | 7 | /** 8 | * 9 | * @author ericson 10 | * @param 11 | */ 12 | public abstract class AbstractStatement extends LineIndentable { 13 | 14 | @Override 15 | public String indentLine(FileFormat format, String line) { 16 | 17 | if (isInitStatement(format, line)) { 18 | E state = getInitStateElement(format, line); 19 | format.states.push(state); 20 | } 21 | 22 | if (format.states.isEmpty()) { 23 | return null; 24 | } 25 | 26 | StatementState state = format.states.peek(); 27 | boolean inState = getStateType().isAssignableFrom(state.getClass()); 28 | 29 | if (!inState) { 30 | return null; 31 | } 32 | 33 | return stateMachine(format, (E) state, line); 34 | 35 | } 36 | 37 | public abstract boolean isInitStatement(FileFormat format, String line); 38 | 39 | public abstract E getInitStateElement(FileFormat format, String liine); 40 | 41 | public abstract Class getStateType(); 42 | 43 | public abstract String stateMachine(FileFormat format, E state, String line); 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/statements/Always.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.statements; 2 | 3 | import net.ericsonj.util.StringHelper; 4 | import net.ericsonj.verilog.FileFormat; 5 | 6 | /** 7 | * 8 | * @author Ericson Joseph 9 | */ 10 | public class Always extends AbstractStatement { 11 | 12 | @Override 13 | public boolean isInitStatement(FileFormat format, String line) { 14 | return matchesAlways(line) || matchesAlwaysBegin(line); 15 | } 16 | 17 | @Override 18 | public AlwaysState getInitStateElement(FileFormat format, String liine) { 19 | AlwaysState state = new AlwaysState(); 20 | state.setBaseIndent(format.getCountIndent()); 21 | state.setState(AlwaysState.STATE.INIT); 22 | return state; 23 | } 24 | 25 | @Override 26 | public Class getStateType() { 27 | return AlwaysState.class; 28 | } 29 | 30 | @Override 31 | public String stateMachine(FileFormat format, AlwaysState state, String line) { 32 | switch (state.getState()) { 33 | case INIT: 34 | if (matchesAlways(line)) { 35 | format.addCountIndent(); 36 | state.setState(AlwaysState.STATE.MAYBE_BLOCK); 37 | return indent(format, state.getBaseIndent(), line); 38 | } else if (matchesAlwaysBegin(line)) { 39 | format.addCountIndent(); 40 | state.setState(AlwaysState.STATE.WAIT_ENDBLOCK); 41 | return indent(format, state.getBaseIndent(), line); 42 | } 43 | break; 44 | case MAYBE_BLOCK: 45 | if (matchesBegin(line)) { 46 | state.setState(AlwaysState.STATE.WAIT_ENDBLOCK); 47 | return indent(format, state.getBaseIndent(), line); 48 | } else if (matchesEmpty(line)) { 49 | format.resCountIndent(); 50 | format.states.poll(); 51 | return indent(format, state.getBaseIndent(), line); 52 | } else { 53 | state.setState(AlwaysState.STATE.WAIT_END); 54 | } 55 | break; 56 | case WAIT_ENDBLOCK: 57 | if (matchesEnd(line)) { 58 | format.resCountIndent(); 59 | format.states.poll(); 60 | return indent(format, state.getBaseIndent(), line); 61 | } 62 | break; 63 | case WAIT_END: 64 | if (matchesEmpty(line)) { 65 | format.resCountIndent(); 66 | format.states.poll(); 67 | } else if (line.matches("[ ]*endmodule") || line.matches("[ ]*end")) { 68 | format.resCountIndent(); 69 | format.states.poll(); 70 | } 71 | break; 72 | default: 73 | break; 74 | } 75 | 76 | return null; 77 | } 78 | 79 | private boolean matchesAlways(String line) { 80 | String basic = "[ ]*always[ ]*.*[)]"; 81 | String comment = "[ ]*[//|/*].*"; 82 | return StringHelper.stringMatches(line, basic, basic + comment); 83 | } 84 | 85 | private boolean matchesAlwaysBegin(String line) { 86 | String basic = "[ ]*always[ ]*.*[ ]*begin"; 87 | String comment = "[ ]*[//|/*].*"; 88 | return StringHelper.stringMatches(line, basic, basic + comment); 89 | } 90 | 91 | private boolean matchesBegin(String line) { 92 | String basic = "[ ]*begin"; 93 | String comment = "[ ]*[//|/*].*"; 94 | return StringHelper.stringMatches(line, basic, basic + comment); 95 | } 96 | 97 | private boolean matchesEnd(String line) { 98 | String basic = "[ ]*end"; 99 | String comment = "[ ]*[//|/*].*"; 100 | return StringHelper.stringMatches(line, basic, basic + comment); 101 | } 102 | 103 | private boolean matchesEmpty(String line) { 104 | String basic = "[ ]*"; 105 | return StringHelper.stringMatches(line, basic); 106 | } 107 | 108 | } 109 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/statements/AlwaysState.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.statements; 2 | 3 | import net.ericsonj.verilog.StatementState; 4 | 5 | /** 6 | * 7 | * @author Ericson Joseph 8 | */ 9 | public class AlwaysState extends StatementState { 10 | 11 | public enum STATE { 12 | INIT, 13 | MAYBE_BLOCK, 14 | WAIT_ENDBLOCK, 15 | WAIT_END, 16 | } 17 | 18 | private AlwaysState.STATE state; 19 | 20 | public AlwaysState() { 21 | super("always", 0); 22 | } 23 | 24 | public AlwaysState.STATE getState() { 25 | return state; 26 | } 27 | 28 | public void setState(AlwaysState.STATE state) { 29 | this.state = state; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/statements/BlockComment.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.statements; 2 | 3 | import net.ericsonj.verilog.FileFormat; 4 | 5 | /** 6 | * 7 | * @author Ericson Joseph 8 | * 9 | * Create on Feb 28, 2019 12:42:31 AM 10 | */ 11 | public class BlockComment extends AbstractStatement { 12 | 13 | @Override 14 | public boolean isInitStatement(FileFormat format, String line) { 15 | return line.matches("/\\*.*"); 16 | } 17 | 18 | @Override 19 | public BlockCommentState getInitStateElement(FileFormat format, String liine) { 20 | BlockCommentState state = new BlockCommentState(); 21 | state.setBaseIndent(format.getCountIndent()); 22 | state.setState(BlockCommentState.STATE.INIT); 23 | return state; 24 | } 25 | 26 | @Override 27 | public Class getStateType() { 28 | return BlockCommentState.class; 29 | } 30 | 31 | @Override 32 | public String stateMachine(FileFormat format, BlockCommentState state, String line) { 33 | switch (state.getState()) { 34 | case INIT: 35 | if (line.matches("/\\*.*") && line.matches(".*\\*/")) { 36 | format.states.poll(); 37 | } else if (line.matches("/\\*.*")) { 38 | state.setState(BlockCommentState.STATE.COMMENT); 39 | return indent(format, line); 40 | } 41 | break; 42 | case COMMENT: 43 | if (line.matches(".*\\*/")) { 44 | format.states.poll(); 45 | } 46 | return indent(format, " " + line); 47 | default: 48 | throw new AssertionError(state.getState().name()); 49 | } 50 | return null; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/statements/BlockCommentState.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.statements; 2 | 3 | import net.ericsonj.verilog.StatementState; 4 | 5 | /** 6 | * 7 | * @author Ericson Joseph 8 | * 9 | * Create on Feb 28, 2019 12:43:30 AM 10 | */ 11 | public class BlockCommentState extends StatementState { 12 | 13 | public enum STATE { 14 | INIT, 15 | COMMENT 16 | } 17 | 18 | private BlockCommentState.STATE state; 19 | 20 | public BlockCommentState() { 21 | super("blockcomment", 0); 22 | this.state = STATE.INIT; 23 | } 24 | 25 | public STATE getState() { 26 | return state; 27 | } 28 | 29 | public void setState(STATE state) { 30 | this.state = state; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/statements/Case.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.statements; 2 | 3 | import net.ericsonj.util.StringHelper; 4 | import net.ericsonj.verilog.FileFormat; 5 | 6 | /** 7 | * 8 | * @author Ericson Joseph 9 | */ 10 | public class Case extends AbstractStatement { 11 | 12 | public static final String KEYWORD_CASE = "case"; 13 | public static final String KEYWORD_CASEZ = "casez"; 14 | public static final String KEYWORD_CASEX = "casex"; 15 | public static final String KEYWORD_ENDCASE = "endcase"; 16 | public static final String KEYWORD_BEGIN = "begin"; 17 | public static final String KEYWORD_END = "end"; 18 | public static final String LINE_COMMENT = "[ ]*[//|/*].*"; 19 | 20 | @Override 21 | public boolean isInitStatement(FileFormat format, String line) { 22 | return matchesCase(line); 23 | } 24 | 25 | @Override 26 | public CaseState getInitStateElement(FileFormat format, String liine) { 27 | CaseState state = new CaseState(); 28 | state.setBaseIndent(format.getCountIndent()); 29 | state.setState(CaseState.STATE.INIT); 30 | return state; 31 | } 32 | 33 | @Override 34 | public Class getStateType() { 35 | return CaseState.class; 36 | } 37 | 38 | @Override 39 | public String stateMachine(FileFormat format, CaseState state, String line) { 40 | 41 | switch (state.getState()) { 42 | case INIT: 43 | if (matchesCase(line)) { 44 | format.addCountIndent(); 45 | state.setState(CaseState.STATE.CASE); 46 | return indent(format, state.getBaseIndent(), line); 47 | } 48 | break; 49 | case CASE: 50 | if (matchesBegin(line)) { 51 | int cIndent = format.getCountIndent(); 52 | format.addCountIndent(); 53 | return indent(format, cIndent, line); 54 | } else if (matchesEnd(line)) { 55 | format.resCountIndent(); 56 | return indent(format, line); 57 | } else if (matchesEndcase(line)) { 58 | format.states.poll(); 59 | format.setCountIndent(state.getBaseIndent()); 60 | return indent(format, state.getBaseIndent(), line); 61 | } 62 | break; 63 | default: 64 | throw new AssertionError(state.getState().name()); 65 | } 66 | 67 | return null; 68 | 69 | } 70 | 71 | private boolean matchesCase(String line) { 72 | 73 | String baseCase = "[ ]*" + KEYWORD_CASE + "[ ]*.*[)]"; 74 | String baseCasex = "[ ]*" + KEYWORD_CASEX + "[ ]*.*[)]"; 75 | String baseCasez = "[ ]*" + KEYWORD_CASEZ + "[ ]*.*[)]"; 76 | 77 | return StringHelper.stringMatches( 78 | line, 79 | baseCase, 80 | baseCasex, 81 | baseCasez, 82 | baseCase + LINE_COMMENT, 83 | baseCasex + LINE_COMMENT, 84 | baseCasez + LINE_COMMENT); 85 | } 86 | 87 | private boolean matchesBegin(String line) { 88 | String basic = ".*[ ]*" + KEYWORD_BEGIN; 89 | String comment = LINE_COMMENT; 90 | return StringHelper.stringMatches(line, basic, basic + comment); 91 | } 92 | 93 | private boolean matchesEndcase(String line) { 94 | String basic = "[ ]*" + KEYWORD_ENDCASE; 95 | String comment = LINE_COMMENT; 96 | return StringHelper.stringMatches(line, basic, basic + comment); 97 | } 98 | 99 | private boolean matchesEnd(String line) { 100 | String basic = ".*[ ]*" + KEYWORD_END; 101 | String comment = LINE_COMMENT; 102 | return StringHelper.stringMatches(line, basic, basic + comment); 103 | } 104 | 105 | } 106 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/statements/CaseState.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.statements; 2 | 3 | import net.ericsonj.verilog.StatementState; 4 | 5 | /** 6 | * 7 | * @author Ericson Joseph 8 | */ 9 | public class CaseState extends StatementState { 10 | 11 | public enum STATE { 12 | INIT, 13 | CASE 14 | } 15 | 16 | private STATE state; 17 | 18 | public CaseState() { 19 | super("case", 0); 20 | this.state = STATE.INIT; 21 | } 22 | 23 | public STATE getState() { 24 | return state; 25 | } 26 | 27 | public void setState(STATE state) { 28 | this.state = state; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/statements/For.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.statements; 2 | 3 | import net.ericsonj.util.StringHelper; 4 | import net.ericsonj.verilog.FileFormat; 5 | 6 | /** 7 | * 8 | * @author Ericson Joseph 9 | * 10 | * Create on Feb 16, 2019 12:05:55 PM 11 | */ 12 | public class For extends AbstractStatement { 13 | 14 | private static final String KEYWORD_FOR = "for"; 15 | private static final String KEYWORD_BEGIN = "begin"; 16 | private static final String KEYWORD_END = "end"; 17 | public static final String LINE_COMMENT = "[ ]*[//|/*].*"; 18 | 19 | @Override 20 | public boolean isInitStatement(FileFormat format, String line) { 21 | return matchesFor(line) || matchesForBegin(line); 22 | } 23 | 24 | @Override 25 | public ForState getInitStateElement(FileFormat format, String liine) { 26 | ForState state = new ForState(); 27 | state.setBaseIndent(format.getCountIndent()); 28 | state.setState(ForState.STATE.INIT); 29 | return state; 30 | } 31 | 32 | @Override 33 | public Class getStateType() { 34 | return ForState.class; 35 | } 36 | 37 | @Override 38 | public String stateMachine(FileFormat format, ForState state, String line) { 39 | switch (state.getState()) { 40 | case INIT: 41 | if (matchesFor(line)) { 42 | format.addCountIndent(); 43 | state.setState(ForState.STATE.FOR); 44 | state.setStateBlock(ForState.STATE_BLOCK.MAYBE_BLOCK); 45 | return indent(format, state.getBaseIndent(), line); 46 | } 47 | if (matchesForBegin(line)) { 48 | format.addCountIndent(); 49 | state.setState(ForState.STATE.FOR); 50 | state.setStateBlock(ForState.STATE_BLOCK.IN_BLOCK); 51 | return indent(format, state.getBaseIndent(), line); 52 | } 53 | break; 54 | case FOR: 55 | switch (state.getStateBlock()) { 56 | case INIT: 57 | break; 58 | case MAYBE_BLOCK: 59 | if (matchesBegin(line)) { 60 | state.setStateBlock(ForState.STATE_BLOCK.IN_BLOCK); 61 | return indent(format, state.getBaseIndent(), line); 62 | } else if (matchesEmpty(line)) { 63 | format.resCountIndent(); 64 | format.states.poll(); 65 | return indent(format, state.getBaseIndent(), line); 66 | } else { 67 | state.setState(ForState.STATE.FOR); 68 | state.setStateBlock(ForState.STATE_BLOCK.NO_BLOCK); 69 | } 70 | break; 71 | case IN_BLOCK: 72 | if (matchesEnd(line)) { 73 | format.resCountIndent(); 74 | format.states.poll(); 75 | return indent(format, state.getBaseIndent(), line); 76 | } 77 | break; 78 | case NO_BLOCK: 79 | if (matchesEmpty(line) || line.matches("[ ]*endmodule") || line.matches("[ ]*end")) { 80 | format.resCountIndent(); 81 | format.states.poll(); 82 | } 83 | break; 84 | default: 85 | throw new AssertionError(state.getStateBlock().name()); 86 | } 87 | break; 88 | 89 | default: 90 | throw new AssertionError(state.getState().name()); 91 | } 92 | 93 | return null; 94 | } 95 | 96 | private boolean matchesFor(String line) { 97 | String base = "[ ]*" + KEYWORD_FOR + ".*[)]"; 98 | return StringHelper.stringMatches(line, base, base + LINE_COMMENT); 99 | } 100 | 101 | private boolean matchesForBegin(String line) { 102 | String base = "[ ]*" + KEYWORD_FOR + "[ ]*[(].*[)][ ]*" + KEYWORD_BEGIN; 103 | return StringHelper.stringMatches(line, base, base + LINE_COMMENT); 104 | } 105 | 106 | private boolean matchesBegin(String line) { 107 | String base = "[ ]*" + KEYWORD_BEGIN; 108 | return StringHelper.stringMatches(line, base, base + LINE_COMMENT); 109 | } 110 | 111 | private boolean matchesEmpty(String line) { 112 | String base = "[ ]*"; 113 | return StringHelper.stringMatches(line, base); 114 | } 115 | 116 | private boolean matchesEnd(String line) { 117 | String base = "[ ]*" + KEYWORD_END; 118 | return StringHelper.stringMatches(line, base, base + LINE_COMMENT); 119 | } 120 | 121 | } 122 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/statements/ForState.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.statements; 2 | 3 | import net.ericsonj.verilog.StatementState; 4 | 5 | /** 6 | * 7 | * @author Ericson Joseph 8 | * 9 | * Create on Feb 16, 2019 12:06:32 PM 10 | */ 11 | public class ForState extends StatementState { 12 | 13 | public enum STATE { 14 | INIT, 15 | FOR 16 | } 17 | 18 | public enum STATE_BLOCK { 19 | INIT, 20 | MAYBE_BLOCK, 21 | IN_BLOCK, 22 | NO_BLOCK 23 | } 24 | 25 | private STATE_BLOCK stateBlock; 26 | private STATE state; 27 | 28 | public ForState() { 29 | super("for", 0); 30 | this.stateBlock = STATE_BLOCK.INIT; 31 | } 32 | 33 | public STATE_BLOCK getStateBlock() { 34 | return stateBlock; 35 | } 36 | 37 | public void setStateBlock(STATE_BLOCK stateBlock) { 38 | this.stateBlock = stateBlock; 39 | } 40 | 41 | public STATE getState() { 42 | return state; 43 | } 44 | 45 | public void setState(STATE state) { 46 | this.state = state; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/statements/Forever.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.statements; 2 | 3 | import net.ericsonj.util.StringHelper; 4 | import net.ericsonj.verilog.FileFormat; 5 | 6 | /** 7 | * 8 | * @author Ericson Joseph 9 | * 10 | * Create on Feb 16, 2019 12:05:55 PM 11 | */ 12 | public class Forever extends AbstractStatement { 13 | 14 | private static final String KEYWORD_FOREVER = "forever"; 15 | private static final String KEYWORD_BEGIN = "begin"; 16 | private static final String KEYWORD_END = "end"; 17 | public static final String LINE_COMMENT = "[ ]*[//|/*].*"; 18 | 19 | @Override 20 | public boolean isInitStatement(FileFormat format, String line) { 21 | return matchesForever(line) || matchesForeverBegin(line); 22 | } 23 | 24 | @Override 25 | public ForeverState getInitStateElement(FileFormat format, String liine) { 26 | ForeverState state = new ForeverState(); 27 | state.setBaseIndent(format.getCountIndent()); 28 | state.setState(ForeverState.STATE.INIT); 29 | return state; 30 | } 31 | 32 | @Override 33 | public Class getStateType() { 34 | return ForeverState.class; 35 | } 36 | 37 | @Override 38 | public String stateMachine(FileFormat format, ForeverState state, String line) { 39 | switch (state.getState()) { 40 | case INIT: 41 | if (matchesForever(line)) { 42 | format.addCountIndent(); 43 | state.setState(ForeverState.STATE.FOREVER); 44 | state.setStateBlock(ForeverState.STATE_BLOCK.MAYBE_BLOCK); 45 | return indent(format, state.getBaseIndent(), line); 46 | } 47 | if (matchesForeverBegin(line)) { 48 | format.addCountIndent(); 49 | state.setState(ForeverState.STATE.FOREVER); 50 | state.setStateBlock(ForeverState.STATE_BLOCK.IN_BLOCK); 51 | return indent(format, state.getBaseIndent(), line); 52 | } 53 | break; 54 | case FOREVER: 55 | switch (state.getStateBlock()) { 56 | case INIT: 57 | break; 58 | case MAYBE_BLOCK: 59 | if (matchesBegin(line)) { 60 | state.setStateBlock(ForeverState.STATE_BLOCK.IN_BLOCK); 61 | return indent(format, state.getBaseIndent(), line); 62 | } else if (matchesEmpty(line)) { 63 | format.resCountIndent(); 64 | format.states.poll(); 65 | return indent(format, state.getBaseIndent(), line); 66 | } else { 67 | state.setState(ForeverState.STATE.FOREVER); 68 | state.setStateBlock(ForeverState.STATE_BLOCK.NO_BLOCK); 69 | } 70 | break; 71 | case IN_BLOCK: 72 | if (matchesEnd(line)) { 73 | format.resCountIndent(); 74 | format.states.poll(); 75 | return indent(format, state.getBaseIndent(), line); 76 | } 77 | break; 78 | case NO_BLOCK: 79 | if (matchesEmpty(line) || line.matches("[ ]*endmodule") || line.matches("[ ]*end")) { 80 | format.resCountIndent(); 81 | format.states.poll(); 82 | } 83 | break; 84 | default: 85 | throw new AssertionError(state.getStateBlock().name()); 86 | } 87 | break; 88 | 89 | 90 | 91 | default: 92 | throw new AssertionError(state.getState().name()); 93 | } 94 | 95 | return null; 96 | } 97 | 98 | private boolean matchesForever(String line) { 99 | String base = "[ ]*" + KEYWORD_FOREVER; 100 | return StringHelper.stringMatches(line, base, base + LINE_COMMENT); 101 | } 102 | 103 | private boolean matchesForeverBegin(String line) { 104 | String base = "[ ]*" + KEYWORD_FOREVER + "[ ]*" + KEYWORD_BEGIN; 105 | return StringHelper.stringMatches(line, base, base + LINE_COMMENT); 106 | } 107 | 108 | private boolean matchesBegin(String line) { 109 | String base = "[ ]*" + KEYWORD_BEGIN; 110 | return StringHelper.stringMatches(line, base, base + LINE_COMMENT); 111 | } 112 | 113 | private boolean matchesEmpty(String line) { 114 | String base = "[ ]*"; 115 | return StringHelper.stringMatches(line, base); 116 | } 117 | 118 | private boolean matchesEnd(String line) { 119 | String base = "[ ]*" + KEYWORD_END; 120 | return StringHelper.stringMatches(line, base, base + LINE_COMMENT); 121 | } 122 | 123 | } 124 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/statements/ForeverState.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.statements; 2 | 3 | import net.ericsonj.verilog.StatementState; 4 | 5 | /** 6 | * 7 | * @author Ericson Joseph 8 | * 9 | * Create on Feb 16, 2019 12:06:32 PM 10 | */ 11 | public class ForeverState extends StatementState { 12 | 13 | public enum STATE { 14 | INIT, 15 | FOREVER 16 | } 17 | 18 | public enum STATE_BLOCK { 19 | INIT, 20 | MAYBE_BLOCK, 21 | IN_BLOCK, 22 | NO_BLOCK 23 | } 24 | 25 | private STATE_BLOCK stateBlock; 26 | private STATE state; 27 | 28 | public ForeverState() { 29 | super("forever", 0); 30 | this.stateBlock = STATE_BLOCK.INIT; 31 | } 32 | 33 | public STATE_BLOCK getStateBlock() { 34 | return stateBlock; 35 | } 36 | 37 | public void setStateBlock(STATE_BLOCK stateBlock) { 38 | this.stateBlock = stateBlock; 39 | } 40 | 41 | public STATE getState() { 42 | return state; 43 | } 44 | 45 | public void setState(STATE state) { 46 | this.state = state; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/statements/Function.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.statements; 2 | 3 | import net.ericsonj.util.StringHelper; 4 | import net.ericsonj.verilog.FileFormat; 5 | 6 | /** 7 | * 8 | * @author Ericson Joseph 9 | */ 10 | public class Function extends AbstractStatement { 11 | 12 | public static final String KEYWORD_FUNCTION = "function"; 13 | public static final String KEYWORD_ENDFUNCTION = "endfunction"; 14 | public static final String KEYWORD_BEGIN = "begin"; 15 | public static final String KEYWORD_END = "end"; 16 | public static final String LINE_COMMENT = "[ ]*[//|/*].*"; 17 | 18 | @Override 19 | public boolean isInitStatement(FileFormat format, String line) { 20 | return matchesFunction(line); 21 | } 22 | 23 | @Override 24 | public FunctionState getInitStateElement(FileFormat format, String liine) { 25 | FunctionState state = new FunctionState(); 26 | state.setBaseIndent(format.getCountIndent()); 27 | state.setState(FunctionState.STATE.INIT); 28 | return state; 29 | } 30 | 31 | @Override 32 | public Class getStateType() { 33 | return FunctionState.class; 34 | } 35 | 36 | @Override 37 | public String stateMachine(FileFormat format, FunctionState state, String line) { 38 | 39 | switch (state.getState()) { 40 | case INIT: 41 | if (matchesFunction(line)) { 42 | format.addCountIndent(); 43 | state.setState(FunctionState.STATE.FUNCTION); 44 | return indent(format, state.getBaseIndent(), line); 45 | } 46 | break; 47 | case FUNCTION: 48 | if (matchesBegin(line)) { 49 | int cIndent = format.getCountIndent(); 50 | format.addCountIndent(); 51 | return indent(format, cIndent, line); 52 | } else if (matchesEnd(line)) { 53 | format.resCountIndent(); 54 | return indent(format, line); 55 | } else if (matchesEndfunction(line)) { 56 | format.states.poll(); 57 | format.setCountIndent(state.getBaseIndent()); 58 | return indent(format, state.getBaseIndent(), line); 59 | } 60 | break; 61 | default: 62 | throw new AssertionError(state.getState().name()); 63 | } 64 | 65 | return null; 66 | 67 | } 68 | 69 | private boolean matchesFunction(String line) { 70 | 71 | String baseFunction = "[ ]*" + KEYWORD_FUNCTION + "[\\[( ]?[ ]+?.*"; 72 | 73 | return StringHelper.stringMatches( 74 | line, 75 | baseFunction, 76 | baseFunction + LINE_COMMENT); 77 | } 78 | 79 | private boolean matchesBegin(String line) { 80 | String basic = ".*[ ]*" + KEYWORD_BEGIN; 81 | String comment = LINE_COMMENT; 82 | return StringHelper.stringMatches(line, basic, basic + comment); 83 | } 84 | 85 | private boolean matchesEndfunction(String line) { 86 | String basic = "[ ]*" + KEYWORD_ENDFUNCTION; 87 | String comment = LINE_COMMENT; 88 | return StringHelper.stringMatches(line, basic, basic + comment); 89 | } 90 | 91 | private boolean matchesEnd(String line) { 92 | String basic = ".*[ ]*" + KEYWORD_END; 93 | String comment = LINE_COMMENT; 94 | return StringHelper.stringMatches(line, basic, basic + comment); 95 | } 96 | 97 | } 98 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/statements/FunctionState.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.statements; 2 | 3 | import net.ericsonj.verilog.StatementState; 4 | 5 | /** 6 | * 7 | * @author Ericson Joseph 8 | */ 9 | public class FunctionState extends StatementState { 10 | 11 | public enum STATE { 12 | INIT, 13 | FUNCTION 14 | } 15 | 16 | private STATE state; 17 | 18 | public FunctionState() { 19 | super("case", 0); 20 | this.state = STATE.INIT; 21 | } 22 | 23 | public STATE getState() { 24 | return state; 25 | } 26 | 27 | public void setState(STATE state) { 28 | this.state = state; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/statements/If.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.statements; 2 | 3 | import net.ericsonj.util.StringHelper; 4 | import net.ericsonj.verilog.FileFormat; 5 | import net.ericsonj.verilog.LineIndentable; 6 | import net.ericsonj.verilog.StatementState; 7 | 8 | /** 9 | * 10 | * @author Ericson Joseph 11 | */ 12 | public class If extends LineIndentable { 13 | 14 | private static final String COMMNET = "[ ]*[//|/*].*"; 15 | 16 | @Override 17 | public String indentLine(FileFormat format, String line) { 18 | 19 | if (matchesIf(line) || matchesIfBegin(line)) { 20 | IfState state = new IfState(); 21 | state.setBaseIndent(format.getCountIndent()); 22 | state.setState(IfState.IF_STATE.INIT); 23 | state.setStateBlock(IfState.BLOCK_STATE.INIT); 24 | format.states.push(state); 25 | } 26 | 27 | if (format.states.isEmpty()) { 28 | return null; 29 | } 30 | 31 | StatementState state = format.states.peek(); 32 | boolean inState = state instanceof IfState; 33 | 34 | if (!inState) { 35 | return null; 36 | } 37 | 38 | IfState ifState = (IfState) state; 39 | 40 | switch (ifState.getState()) { 41 | case INIT: 42 | if (matchesIf(line)) { 43 | format.addCountIndent(); 44 | ifState.setState(IfState.IF_STATE.IF); 45 | ifState.setStateBlock(IfState.BLOCK_STATE.MAYBE_BLOCK); 46 | return indent(format, ifState.getBaseIndent(), line); 47 | } 48 | if (matchesIfBegin(line)) { 49 | format.addCountIndent(); 50 | ifState.setState(IfState.IF_STATE.IF); 51 | ifState.setStateBlock(IfState.BLOCK_STATE.IN_BLOCK); 52 | return indent(format, ifState.getBaseIndent(), line); 53 | } 54 | break; 55 | case IF: 56 | switch (ifState.getStateBlock()) { 57 | case INIT: 58 | break; 59 | case MAYBE_BLOCK: 60 | if (line.matches(COMMNET)) { 61 | ifState.setState(IfState.IF_STATE.IF); 62 | ifState.setStateBlock(IfState.BLOCK_STATE.MAYBE_BLOCK); 63 | return indent(format, ifState.getBaseIndent(), line); 64 | } 65 | if (matchesBegin(line)) { 66 | ifState.setState(IfState.IF_STATE.IF); 67 | ifState.setStateBlock(IfState.BLOCK_STATE.IN_BLOCK); 68 | return indent(format, ifState.getBaseIndent(), line); 69 | } else { 70 | ifState.setState(IfState.IF_STATE.IF); 71 | ifState.setStateBlock(IfState.BLOCK_STATE.NO_BLOCK); 72 | } 73 | break; 74 | case IN_BLOCK: 75 | if (matchesEnd(line)) { 76 | ifState.setState(IfState.IF_STATE.ELSE_IF); 77 | ifState.setStateBlock(IfState.BLOCK_STATE.INIT); 78 | return indent(format, ifState.getBaseIndent(), line); 79 | } 80 | break; 81 | case NO_BLOCK: 82 | if (matchesElseIf(line)) { 83 | ifState.setState(IfState.IF_STATE.ELSE_IF); 84 | ifState.setStateBlock(IfState.BLOCK_STATE.MAYBE_BLOCK); 85 | return indent(format, ifState.getBaseIndent(), line); 86 | } else if (matchesElseIfBegin(line)) { 87 | ifState.setState(IfState.IF_STATE.ELSE_IF); 88 | ifState.setStateBlock(IfState.BLOCK_STATE.IN_BLOCK); 89 | return indent(format, ifState.getBaseIndent(), line); 90 | } else if (matchesElse(line)) { 91 | ifState.setState(IfState.IF_STATE.ELSE); 92 | ifState.setStateBlock(IfState.BLOCK_STATE.MAYBE_BLOCK); 93 | return indent(format, ifState.getBaseIndent(), line); 94 | } else if (matchesElseBegin(line)) { 95 | ifState.setState(IfState.IF_STATE.ELSE); 96 | ifState.setStateBlock(IfState.BLOCK_STATE.IN_BLOCK); 97 | return indent(format, ifState.getBaseIndent(), line); 98 | } else if (line.matches("[ ]*")) { 99 | format.resCountIndent(); 100 | format.states.poll(); 101 | } 102 | break; 103 | default: 104 | throw new AssertionError(ifState.getStateBlock().name()); 105 | } 106 | break; 107 | case ELSE_IF: 108 | switch (ifState.getStateBlock()) { 109 | case INIT: 110 | if (matchesElseIf(line)) { 111 | ifState.setState(IfState.IF_STATE.ELSE_IF); 112 | ifState.setStateBlock(IfState.BLOCK_STATE.MAYBE_BLOCK); 113 | return indent(format, ifState.getBaseIndent(), line); 114 | } else if (matchesElseIfBegin(line)) { 115 | ifState.setState(IfState.IF_STATE.ELSE_IF); 116 | ifState.setStateBlock(IfState.BLOCK_STATE.IN_BLOCK); 117 | return indent(format, ifState.getBaseIndent(), line); 118 | } else if (matchesElse(line)) { 119 | ifState.setState(IfState.IF_STATE.ELSE); 120 | ifState.setStateBlock(IfState.BLOCK_STATE.MAYBE_BLOCK); 121 | return indent(format, ifState.getBaseIndent(), line); 122 | } else if (matchesElseBegin(line)) { 123 | ifState.setState(IfState.IF_STATE.ELSE); 124 | ifState.setStateBlock(IfState.BLOCK_STATE.IN_BLOCK); 125 | return indent(format, ifState.getBaseIndent(), line); 126 | } else { 127 | format.resCountIndent(); 128 | format.states.poll(); 129 | } 130 | break; 131 | case MAYBE_BLOCK: 132 | if (line.matches(COMMNET)) { 133 | ifState.setState(IfState.IF_STATE.ELSE_IF); 134 | ifState.setStateBlock(IfState.BLOCK_STATE.MAYBE_BLOCK); 135 | return indent(format, ifState.getBaseIndent(), line); 136 | } else if (matchesBegin(line)) { 137 | ifState.setState(IfState.IF_STATE.ELSE_IF); 138 | ifState.setStateBlock(IfState.BLOCK_STATE.IN_BLOCK); 139 | return indent(format, ifState.getBaseIndent(), line); 140 | } else { 141 | ifState.setState(IfState.IF_STATE.ELSE_IF); 142 | ifState.setStateBlock(IfState.BLOCK_STATE.NO_BLOCK); 143 | } 144 | break; 145 | case IN_BLOCK: 146 | if (matchesEnd(line)) { 147 | ifState.setState(IfState.IF_STATE.ELSE); 148 | ifState.setStateBlock(IfState.BLOCK_STATE.INIT); 149 | return indent(format, ifState.getBaseIndent(), line); 150 | } 151 | break; 152 | case NO_BLOCK: 153 | if (matchesElse(line)) { 154 | ifState.setState(IfState.IF_STATE.ELSE); 155 | ifState.setStateBlock(IfState.BLOCK_STATE.MAYBE_BLOCK); 156 | return indent(format, ifState.getBaseIndent(), line); 157 | } else if (matchesElseBegin(line)) { 158 | ifState.setState(IfState.IF_STATE.ELSE); 159 | ifState.setStateBlock(IfState.BLOCK_STATE.IN_BLOCK); 160 | return indent(format, ifState.getBaseIndent(), line); 161 | } else if (matchesElseIf(line)) { 162 | ifState.setState(IfState.IF_STATE.ELSE_IF); 163 | ifState.setStateBlock(IfState.BLOCK_STATE.MAYBE_BLOCK); 164 | return indent(format, ifState.getBaseIndent(), line); 165 | } else if (matchesElseIfBegin(line)) { 166 | ifState.setState(IfState.IF_STATE.ELSE_IF); 167 | ifState.setStateBlock(IfState.BLOCK_STATE.IN_BLOCK); 168 | return indent(format, ifState.getBaseIndent(), line); 169 | } else if (line.matches("[ ]*")) { 170 | format.resCountIndent(); 171 | format.states.poll(); 172 | } 173 | break; 174 | default: 175 | throw new AssertionError(ifState.getStateBlock().name()); 176 | } 177 | break; 178 | case ELSE: 179 | switch (ifState.getStateBlock()) { 180 | case INIT: 181 | if (matchesElse(line)) { 182 | ifState.setState(IfState.IF_STATE.ELSE); 183 | ifState.setStateBlock(IfState.BLOCK_STATE.MAYBE_BLOCK); 184 | return indent(format, ifState.getBaseIndent(), line); 185 | } else if (matchesElseBegin(line)) { 186 | ifState.setState(IfState.IF_STATE.ELSE); 187 | ifState.setStateBlock(IfState.BLOCK_STATE.IN_BLOCK); 188 | return indent(format, ifState.getBaseIndent(), line); 189 | } 190 | break; 191 | case MAYBE_BLOCK: 192 | if (line.matches(COMMNET)) { 193 | ifState.setState(IfState.IF_STATE.ELSE); 194 | ifState.setStateBlock(IfState.BLOCK_STATE.MAYBE_BLOCK); 195 | return indent(format, ifState.getBaseIndent(), line); 196 | } else if (matchesBegin(line)) { 197 | ifState.setState(IfState.IF_STATE.ELSE); 198 | ifState.setStateBlock(IfState.BLOCK_STATE.IN_BLOCK); 199 | return indent(format, ifState.getBaseIndent(), line); 200 | } else if (line.matches("[ ]*")) { 201 | format.resCountIndent(); 202 | format.states.poll(); 203 | } else { 204 | ifState.setState(IfState.IF_STATE.ELSE); 205 | ifState.setStateBlock(IfState.BLOCK_STATE.NO_BLOCK); 206 | } 207 | break; 208 | case IN_BLOCK: 209 | if (matchesEnd(line)) { 210 | format.resCountIndent(); 211 | format.states.poll(); 212 | return indent(format, ifState.getBaseIndent(), line); 213 | } 214 | break; 215 | case NO_BLOCK: 216 | if (line.matches("[ ]*")) { 217 | format.resCountIndent(); 218 | format.states.poll(); 219 | } else if (matchesEnd(line) || line.matches("[ ]*\\bendmodule\\b")) { 220 | format.resCountIndent(); 221 | format.states.poll(); 222 | } 223 | break; 224 | default: 225 | throw new AssertionError(ifState.getStateBlock().name()); 226 | } 227 | break; 228 | default: 229 | throw new AssertionError(ifState.getState().name()); 230 | } 231 | 232 | return null; 233 | 234 | } 235 | 236 | private boolean matchesIf(String line) { 237 | String ifBase = "[ ]*\\bif\\b[ ]*.*[)]"; 238 | return StringHelper.stringMatches(line, ifBase, ifBase + COMMNET); 239 | } 240 | 241 | private boolean matchesIfBegin(String line) { 242 | String ifBase = "[ ]*\\bif\\b[ ]*.*[ ]*\\bbegin\\b"; 243 | return StringHelper.stringMatches(line, ifBase, ifBase + COMMNET); 244 | } 245 | 246 | private boolean matchesBegin(String line) { 247 | String ifBase = "[ ]*\\bbegin\\b"; 248 | return StringHelper.stringMatches(line, ifBase, ifBase + COMMNET); 249 | } 250 | 251 | private boolean matchesEnd(String line) { 252 | String ifBase = "[ ]*\\bend\\b"; 253 | return StringHelper.stringMatches(line, ifBase, ifBase + COMMNET); 254 | } 255 | 256 | private boolean matchesElse(String line) { 257 | String ifBase = "[ ]*\\belse\\b"; 258 | return StringHelper.stringMatches(line, ifBase, ifBase + COMMNET); 259 | } 260 | 261 | private boolean matchesElseBegin(String line) { 262 | String ifBase = "[ ]*\\belse\\b[ ]*\\bbegin\\b"; 263 | return StringHelper.stringMatches(line, ifBase, ifBase + COMMNET); 264 | } 265 | 266 | private boolean matchesElseIf(String line) { 267 | String ifBase = "[ ]*\\belse\\b[ ]*\\bif\\b[ ]*.*[)]"; 268 | return StringHelper.stringMatches(line, ifBase, ifBase + COMMNET); 269 | } 270 | 271 | private boolean matchesElseIfBegin(String line) { 272 | String ifBase = "[ ]*\\belse\\b[ ]*\\bif\\b[ ]*.*[ ]*\\bbegin\\b"; 273 | return StringHelper.stringMatches(line, ifBase, ifBase + COMMNET); 274 | } 275 | 276 | } 277 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/statements/IfState.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.statements; 2 | 3 | import net.ericsonj.verilog.StatementState; 4 | 5 | /** 6 | * 7 | * @author Ericson Joseph 8 | */ 9 | public class IfState extends StatementState { 10 | 11 | public enum IF_STATE { 12 | INIT, 13 | IF, 14 | ELSE_IF, 15 | ELSE 16 | } 17 | 18 | public enum BLOCK_STATE { 19 | INIT, 20 | MAYBE_BLOCK, 21 | IN_BLOCK, 22 | NO_BLOCK 23 | } 24 | 25 | private IfState.IF_STATE state; 26 | private IfState.BLOCK_STATE stateBlock; 27 | 28 | public IfState() { 29 | super("if", 0); 30 | } 31 | 32 | public IfState.IF_STATE getState() { 33 | return state; 34 | } 35 | 36 | public void setState(IfState.IF_STATE state) { 37 | this.state = state; 38 | } 39 | 40 | public BLOCK_STATE getStateBlock() { 41 | return stateBlock; 42 | } 43 | 44 | public void setStateBlock(BLOCK_STATE stateBlock) { 45 | this.stateBlock = stateBlock; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/statements/Initial.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.statements; 2 | 3 | import net.ericsonj.util.StringHelper; 4 | import net.ericsonj.verilog.FileFormat; 5 | 6 | /** 7 | * 8 | * @author Ericson Joseph 9 | * 10 | * Create on Feb 16, 2019 12:05:55 PM 11 | */ 12 | public class Initial extends AbstractStatement { 13 | 14 | private static final String KEYWORD_INITIAL = "initial"; 15 | private static final String KEYWORD_BEGIN = "begin"; 16 | private static final String KEYWORD_END = "end"; 17 | public static final String LINE_COMMENT = "[ ]*[//|/*].*"; 18 | 19 | @Override 20 | public boolean isInitStatement(FileFormat format, String line) { 21 | return matchesInitial(line) || matchesInitialBegin(line); 22 | } 23 | 24 | @Override 25 | public InitialState getInitStateElement(FileFormat format, String liine) { 26 | InitialState state = new InitialState(); 27 | state.setBaseIndent(format.getCountIndent()); 28 | state.setState(InitialState.STATE.INIT); 29 | return state; 30 | } 31 | 32 | @Override 33 | public Class getStateType() { 34 | return InitialState.class; 35 | } 36 | 37 | @Override 38 | public String stateMachine(FileFormat format, InitialState state, String line) { 39 | switch (state.getState()) { 40 | case INIT: 41 | if (matchesInitial(line)) { 42 | format.addCountIndent(); 43 | state.setState(InitialState.STATE.IN_INITIAL); 44 | state.setStateBlock(InitialState.STATE_BLOCK.MAYBE_BLOCK); 45 | return indent(format, state.getBaseIndent(), line); 46 | } 47 | if (matchesInitialBegin(line)) { 48 | format.addCountIndent(); 49 | state.setState(InitialState.STATE.IN_INITIAL); 50 | state.setStateBlock(InitialState.STATE_BLOCK.IN_BLOCK); 51 | return indent(format, state.getBaseIndent(), line); 52 | } 53 | break; 54 | case IN_INITIAL: 55 | switch (state.getStateBlock()) { 56 | case INIT: 57 | break; 58 | case MAYBE_BLOCK: 59 | if (matchesBegin(line)) { 60 | state.setStateBlock(InitialState.STATE_BLOCK.IN_BLOCK); 61 | return indent(format, state.getBaseIndent(), line); 62 | } else if (matchesEmpty(line)) { 63 | format.resCountIndent(); 64 | format.states.poll(); 65 | return indent(format, state.getBaseIndent(), line); 66 | } else { 67 | state.setState(InitialState.STATE.IN_INITIAL); 68 | state.setStateBlock(InitialState.STATE_BLOCK.NO_BLOCK); 69 | } 70 | break; 71 | case IN_BLOCK: 72 | if (matchesEnd(line)) { 73 | format.resCountIndent(); 74 | format.states.poll(); 75 | return indent(format, state.getBaseIndent(), line); 76 | } 77 | break; 78 | case NO_BLOCK: 79 | if (matchesEmpty(line) || line.matches("[ ]*endmodule") || line.matches("[ ]*end")) { 80 | format.resCountIndent(); 81 | format.states.poll(); 82 | } 83 | break; 84 | default: 85 | throw new AssertionError(state.getStateBlock().name()); 86 | } 87 | break; 88 | 89 | default: 90 | throw new AssertionError(state.getState().name()); 91 | } 92 | 93 | return null; 94 | } 95 | 96 | private boolean matchesInitial(String line) { 97 | String base = "[ ]*" + KEYWORD_INITIAL; 98 | return StringHelper.stringMatches(line, base, base + LINE_COMMENT); 99 | } 100 | 101 | private boolean matchesInitialBegin(String line) { 102 | String base = "[ ]*" + KEYWORD_INITIAL + "[ ]*" + KEYWORD_BEGIN; 103 | return StringHelper.stringMatches(line, base, base + LINE_COMMENT); 104 | } 105 | 106 | private boolean matchesBegin(String line) { 107 | String base = "[ ]*" + KEYWORD_BEGIN; 108 | return StringHelper.stringMatches(line, base, base + LINE_COMMENT); 109 | } 110 | 111 | private boolean matchesEmpty(String line) { 112 | String base = "[ ]*"; 113 | return StringHelper.stringMatches(line, base); 114 | } 115 | 116 | private boolean matchesEnd(String line) { 117 | String base = "[ ]*" + KEYWORD_END; 118 | return StringHelper.stringMatches(line, base, base + LINE_COMMENT); 119 | } 120 | 121 | } 122 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/statements/InitialState.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.statements; 2 | 3 | import net.ericsonj.verilog.StatementState; 4 | 5 | /** 6 | * 7 | * @author Ericson Joseph 8 | * 9 | * Create on Feb 16, 2019 12:06:32 PM 10 | */ 11 | public class InitialState extends StatementState { 12 | 13 | public enum STATE { 14 | INIT, 15 | IN_INITIAL 16 | } 17 | 18 | public enum STATE_BLOCK { 19 | INIT, 20 | MAYBE_BLOCK, 21 | IN_BLOCK, 22 | NO_BLOCK 23 | } 24 | 25 | private STATE_BLOCK stateBlock; 26 | private STATE state; 27 | 28 | public InitialState() { 29 | super("initial", 0); 30 | this.stateBlock = STATE_BLOCK.INIT; 31 | } 32 | 33 | public STATE_BLOCK getStateBlock() { 34 | return stateBlock; 35 | } 36 | 37 | public void setStateBlock(STATE_BLOCK stateBlock) { 38 | this.stateBlock = stateBlock; 39 | } 40 | 41 | public STATE getState() { 42 | return state; 43 | } 44 | 45 | public void setState(STATE state) { 46 | this.state = state; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/statements/Module.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.statements; 2 | 3 | import java.util.LinkedList; 4 | import net.ericsonj.util.StringHelper; 5 | import net.ericsonj.verilog.FileFormat; 6 | import net.ericsonj.verilog.LineIndentable; 7 | import net.ericsonj.verilog.StatementState; 8 | 9 | /** 10 | * 11 | * @author Ericson Joseph 12 | */ 13 | public class Module extends LineIndentable { 14 | 15 | @Override 16 | public String indentLine(FileFormat format, String line) { 17 | 18 | if (matchesModule(line)) { 19 | ModuleState state = new ModuleState(); 20 | state.setBaseIndent(format.getCountIndent()); 21 | state.setState(ModuleState.MODULE_STATE.INIT); 22 | format.states.push(state); 23 | } 24 | 25 | if (format.states.isEmpty()) { 26 | return null; 27 | } 28 | 29 | StatementState state = format.states.peek(); 30 | boolean inState = state instanceof ModuleState; 31 | 32 | if (!inState) { 33 | return null; 34 | } 35 | 36 | ModuleState moduleState = (ModuleState) state; 37 | 38 | switch (moduleState.getState()) { 39 | case INIT: 40 | format.addCountIndent(); 41 | moduleState.setState(ModuleState.MODULE_STATE.WAIT_ENDMODULE); 42 | return indent(format, moduleState.getBaseIndent(), line); 43 | case WAIT_ENDMODULE: 44 | if (matchesEndmodule(line)) { 45 | format.states.poll(); 46 | format.resCountIndent(); 47 | return indent(format, moduleState.getBaseIndent(), line); 48 | } 49 | break; 50 | default: 51 | return null; 52 | } 53 | 54 | return null; 55 | } 56 | 57 | private boolean matchesModule(String line) { 58 | 59 | LinkedList opts = new LinkedList<>(); 60 | 61 | String comment = "[ ]*[//|/*].*"; 62 | String basicModule = "[ ]*module[ ]*[a-zA-Z0-9-_,;&$#()= ]*"; 63 | 64 | opts.add(basicModule); 65 | opts.add(basicModule + "[(][a-zA-Z0-9-_,&$# ]*"); 66 | opts.add(basicModule + "[(].*[)][;]?"); 67 | opts.add(basicModule + comment); 68 | opts.add(basicModule + "[(][a-zA-Z0-9-_,&$# ]*" + comment); 69 | opts.add(basicModule + "[(].*[)]" + comment); 70 | 71 | return StringHelper.stringMatches(line, opts); 72 | 73 | } 74 | 75 | private boolean matchesEndmodule(String line) { 76 | LinkedList opts = new LinkedList<>(); 77 | 78 | String comment = "[ ]*[//|/*].*"; 79 | opts.add("[ ]*endmodule"); 80 | opts.add("[ ]*endmodule" + comment); 81 | 82 | return StringHelper.stringMatches(line, opts); 83 | 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/statements/ModuleState.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.statements; 2 | 3 | import net.ericsonj.verilog.StatementState; 4 | 5 | /** 6 | * 7 | * @author Ericson Joseph 8 | */ 9 | public class ModuleState extends StatementState { 10 | 11 | public enum MODULE_STATE { 12 | INIT, 13 | WAIT_ENDMODULE 14 | } 15 | 16 | private MODULE_STATE state; 17 | 18 | public ModuleState() { 19 | super("module", 0); 20 | this.state = MODULE_STATE.INIT; 21 | } 22 | 23 | public MODULE_STATE getState() { 24 | return state; 25 | } 26 | 27 | public void setState(MODULE_STATE state) { 28 | this.state = state; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/statements/Repeat.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.statements; 2 | 3 | import net.ericsonj.util.StringHelper; 4 | import net.ericsonj.verilog.FileFormat; 5 | 6 | /** 7 | * 8 | * @author Ericson Joseph 9 | * 10 | * Create on Feb 16, 2019 12:05:55 PM 11 | */ 12 | public class Repeat extends AbstractStatement { 13 | 14 | private static final String KEYWORD_REPEAT = "repeat"; 15 | private static final String KEYWORD_BEGIN = "begin"; 16 | private static final String KEYWORD_END = "end"; 17 | public static final String LINE_COMMENT = "[ ]*[//|/*].*"; 18 | 19 | @Override 20 | public boolean isInitStatement(FileFormat format, String line) { 21 | return matchesRepeat(line) || matchesRepeatBegin(line); 22 | } 23 | 24 | @Override 25 | public RepeatState getInitStateElement(FileFormat format, String liine) { 26 | RepeatState state = new RepeatState(); 27 | state.setBaseIndent(format.getCountIndent()); 28 | state.setState(RepeatState.STATE.INIT); 29 | return state; 30 | } 31 | 32 | @Override 33 | public Class getStateType() { 34 | return RepeatState.class; 35 | } 36 | 37 | @Override 38 | public String stateMachine(FileFormat format, RepeatState state, String line) { 39 | switch (state.getState()) { 40 | case INIT: 41 | if (matchesRepeat(line)) { 42 | format.addCountIndent(); 43 | state.setState(RepeatState.STATE.REPEAT); 44 | state.setStateBlock(RepeatState.STATE_BLOCK.MAYBE_BLOCK); 45 | return indent(format, state.getBaseIndent(), line); 46 | } 47 | if (matchesRepeatBegin(line)) { 48 | format.addCountIndent(); 49 | state.setState(RepeatState.STATE.REPEAT); 50 | state.setStateBlock(RepeatState.STATE_BLOCK.IN_BLOCK); 51 | return indent(format, state.getBaseIndent(), line); 52 | } 53 | break; 54 | case REPEAT: 55 | switch (state.getStateBlock()) { 56 | case INIT: 57 | break; 58 | case MAYBE_BLOCK: 59 | if (matchesBegin(line)) { 60 | state.setStateBlock(RepeatState.STATE_BLOCK.IN_BLOCK); 61 | return indent(format, state.getBaseIndent(), line); 62 | } else if (matchesEmpty(line)) { 63 | format.resCountIndent(); 64 | format.states.poll(); 65 | return indent(format, state.getBaseIndent(), line); 66 | } else { 67 | state.setState(RepeatState.STATE.REPEAT); 68 | state.setStateBlock(RepeatState.STATE_BLOCK.NO_BLOCK); 69 | } 70 | break; 71 | case IN_BLOCK: 72 | if (matchesEnd(line)) { 73 | format.resCountIndent(); 74 | format.states.poll(); 75 | return indent(format, state.getBaseIndent(), line); 76 | } 77 | break; 78 | case NO_BLOCK: 79 | if (matchesEmpty(line) || line.matches("[ ]*endmodule") || line.matches("[ ]*end")) { 80 | format.resCountIndent(); 81 | format.states.poll(); 82 | } 83 | break; 84 | default: 85 | throw new AssertionError(state.getStateBlock().name()); 86 | } 87 | break; 88 | 89 | 90 | 91 | default: 92 | throw new AssertionError(state.getState().name()); 93 | } 94 | 95 | return null; 96 | } 97 | 98 | private boolean matchesRepeat(String line) { 99 | String base = "[ ]*" + KEYWORD_REPEAT+".*[)]"; 100 | return StringHelper.stringMatches(line, base, base + LINE_COMMENT); 101 | } 102 | 103 | private boolean matchesRepeatBegin(String line) { 104 | String base = "[ ]*" + KEYWORD_REPEAT + "[ ]*[(].*[)][ ]*" + KEYWORD_BEGIN; 105 | return StringHelper.stringMatches(line, base, base + LINE_COMMENT); 106 | } 107 | 108 | private boolean matchesBegin(String line) { 109 | String base = "[ ]*" + KEYWORD_BEGIN; 110 | return StringHelper.stringMatches(line, base, base + LINE_COMMENT); 111 | } 112 | 113 | private boolean matchesEmpty(String line) { 114 | String base = "[ ]*"; 115 | return StringHelper.stringMatches(line, base); 116 | } 117 | 118 | private boolean matchesEnd(String line) { 119 | String base = "[ ]*" + KEYWORD_END; 120 | return StringHelper.stringMatches(line, base, base + LINE_COMMENT); 121 | } 122 | 123 | } 124 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/statements/RepeatState.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.statements; 2 | 3 | import net.ericsonj.verilog.StatementState; 4 | 5 | /** 6 | * 7 | * @author Ericson Joseph 8 | * 9 | * Create on Feb 16, 2019 12:06:32 PM 10 | */ 11 | public class RepeatState extends StatementState { 12 | 13 | public enum STATE { 14 | INIT, 15 | REPEAT 16 | } 17 | 18 | public enum STATE_BLOCK { 19 | INIT, 20 | MAYBE_BLOCK, 21 | IN_BLOCK, 22 | NO_BLOCK 23 | } 24 | 25 | private STATE_BLOCK stateBlock; 26 | private STATE state; 27 | 28 | public RepeatState() { 29 | super("repeat", 0); 30 | this.stateBlock = STATE_BLOCK.INIT; 31 | } 32 | 33 | public STATE_BLOCK getStateBlock() { 34 | return stateBlock; 35 | } 36 | 37 | public void setStateBlock(STATE_BLOCK stateBlock) { 38 | this.stateBlock = stateBlock; 39 | } 40 | 41 | public STATE getState() { 42 | return state; 43 | } 44 | 45 | public void setState(STATE state) { 46 | this.state = state; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/statements/Task.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.statements; 2 | 3 | import net.ericsonj.util.StringHelper; 4 | import net.ericsonj.verilog.FileFormat; 5 | 6 | /** 7 | * 8 | * @author Ericson Joseph 9 | */ 10 | public class Task extends AbstractStatement { 11 | 12 | public static final String KEYWORD_TASK = "task"; 13 | public static final String KEYWORD_ENDTASK = "endtask"; 14 | public static final String KEYWORD_BEGIN = "begin"; 15 | public static final String KEYWORD_END = "end"; 16 | public static final String LINE_COMMENT = "[ ]*[//|/*].*"; 17 | 18 | @Override 19 | public boolean isInitStatement(FileFormat format, String line) { 20 | return matchesTask(line); 21 | } 22 | 23 | @Override 24 | public TaskState getInitStateElement(FileFormat format, String liine) { 25 | TaskState state = new TaskState(); 26 | state.setBaseIndent(format.getCountIndent()); 27 | state.setState(TaskState.STATE.INIT); 28 | return state; 29 | } 30 | 31 | @Override 32 | public Class getStateType() { 33 | return TaskState.class; 34 | } 35 | 36 | @Override 37 | public String stateMachine(FileFormat format, TaskState state, String line) { 38 | 39 | switch (state.getState()) { 40 | case INIT: 41 | if (matchesTask(line)) { 42 | format.addCountIndent(); 43 | state.setState(TaskState.STATE.TASK); 44 | return indent(format, state.getBaseIndent(), line); 45 | } 46 | break; 47 | case TASK: 48 | if (matchesBegin(line)) { 49 | int cIndent = format.getCountIndent(); 50 | format.addCountIndent(); 51 | return indent(format, cIndent, line); 52 | } else if (matchesEnd(line)) { 53 | format.resCountIndent(); 54 | return indent(format, line); 55 | } else if (matchesEndfunction(line)) { 56 | format.states.poll(); 57 | format.setCountIndent(state.getBaseIndent()); 58 | return indent(format, state.getBaseIndent(), line); 59 | } 60 | break; 61 | default: 62 | throw new AssertionError(state.getState().name()); 63 | } 64 | 65 | return null; 66 | 67 | } 68 | 69 | private boolean matchesTask(String line) { 70 | 71 | String baseTask = "[ ]*" + KEYWORD_TASK + "[ ]+?.*"; 72 | 73 | return StringHelper.stringMatches( 74 | line, 75 | baseTask, 76 | baseTask + LINE_COMMENT); 77 | } 78 | 79 | private boolean matchesBegin(String line) { 80 | String basic = ".*[ ]*" + KEYWORD_BEGIN; 81 | String comment = LINE_COMMENT; 82 | return StringHelper.stringMatches(line, basic, basic + comment); 83 | } 84 | 85 | private boolean matchesEndfunction(String line) { 86 | String basic = "[ ]*" + KEYWORD_ENDTASK; 87 | String comment = LINE_COMMENT; 88 | return StringHelper.stringMatches(line, basic, basic + comment); 89 | } 90 | 91 | private boolean matchesEnd(String line) { 92 | String basic = ".*[ ]*" + KEYWORD_END; 93 | String comment = LINE_COMMENT; 94 | return StringHelper.stringMatches(line, basic, basic + comment); 95 | } 96 | 97 | } 98 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/statements/TaskState.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.statements; 2 | 3 | import net.ericsonj.verilog.StatementState; 4 | 5 | /** 6 | * 7 | * @author Ericson Joseph 8 | */ 9 | public class TaskState extends StatementState { 10 | 11 | public enum STATE { 12 | INIT, 13 | TASK 14 | } 15 | 16 | private STATE state; 17 | 18 | public TaskState() { 19 | super("case", 0); 20 | this.state = STATE.INIT; 21 | } 22 | 23 | public STATE getState() { 24 | return state; 25 | } 26 | 27 | public void setState(STATE state) { 28 | this.state = state; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/statements/While.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.statements; 2 | 3 | import net.ericsonj.util.StringHelper; 4 | import net.ericsonj.verilog.FileFormat; 5 | 6 | /** 7 | * 8 | * @author Ericson Joseph 9 | * 10 | * Create on Feb 16, 2019 12:05:55 PM 11 | */ 12 | public class While extends AbstractStatement { 13 | 14 | private static final String KEYWORD_WHILE = "while"; 15 | private static final String KEYWORD_BEGIN = "begin"; 16 | private static final String KEYWORD_END = "end"; 17 | public static final String LINE_COMMENT = "[ ]*[//|/*].*"; 18 | 19 | @Override 20 | public boolean isInitStatement(FileFormat format, String line) { 21 | return matchesWhile(line) || matchesWhileBegin(line); 22 | } 23 | 24 | @Override 25 | public WhileState getInitStateElement(FileFormat format, String liine) { 26 | WhileState state = new WhileState(); 27 | state.setBaseIndent(format.getCountIndent()); 28 | state.setState(WhileState.STATE.INIT); 29 | return state; 30 | } 31 | 32 | @Override 33 | public Class getStateType() { 34 | return WhileState.class; 35 | } 36 | 37 | @Override 38 | public String stateMachine(FileFormat format, WhileState state, String line) { 39 | switch (state.getState()) { 40 | case INIT: 41 | if (matchesWhile(line)) { 42 | format.addCountIndent(); 43 | state.setState(WhileState.STATE.WHILE); 44 | state.setStateBlock(WhileState.STATE_BLOCK.MAYBE_BLOCK); 45 | return indent(format, state.getBaseIndent(), line); 46 | } 47 | if (matchesWhileBegin(line)) { 48 | format.addCountIndent(); 49 | state.setState(WhileState.STATE.WHILE); 50 | state.setStateBlock(WhileState.STATE_BLOCK.IN_BLOCK); 51 | return indent(format, state.getBaseIndent(), line); 52 | } 53 | break; 54 | case WHILE: 55 | switch (state.getStateBlock()) { 56 | case INIT: 57 | break; 58 | case MAYBE_BLOCK: 59 | if (matchesBegin(line)) { 60 | state.setStateBlock(WhileState.STATE_BLOCK.IN_BLOCK); 61 | return indent(format, state.getBaseIndent(), line); 62 | } else if (matchesEmpty(line)) { 63 | format.resCountIndent(); 64 | format.states.poll(); 65 | return indent(format, state.getBaseIndent(), line); 66 | } else { 67 | state.setState(WhileState.STATE.WHILE); 68 | state.setStateBlock(WhileState.STATE_BLOCK.NO_BLOCK); 69 | } 70 | break; 71 | case IN_BLOCK: 72 | if (matchesEnd(line)) { 73 | format.resCountIndent(); 74 | format.states.poll(); 75 | return indent(format, state.getBaseIndent(), line); 76 | } 77 | break; 78 | case NO_BLOCK: 79 | if (matchesEmpty(line) || line.matches("[ ]*endmodule") || line.matches("[ ]*end")) { 80 | format.resCountIndent(); 81 | format.states.poll(); 82 | } 83 | break; 84 | default: 85 | throw new AssertionError(state.getStateBlock().name()); 86 | } 87 | break; 88 | 89 | default: 90 | throw new AssertionError(state.getState().name()); 91 | } 92 | 93 | return null; 94 | } 95 | 96 | private boolean matchesWhile(String line) { 97 | String base = "[ ]*" + KEYWORD_WHILE + ".*[)]"; 98 | return StringHelper.stringMatches(line, base, base + LINE_COMMENT); 99 | } 100 | 101 | private boolean matchesWhileBegin(String line) { 102 | String base = "[ ]*" + KEYWORD_WHILE + "[ ]*[(].*[)][ ]*" + KEYWORD_BEGIN; 103 | return StringHelper.stringMatches(line, base, base + LINE_COMMENT); 104 | } 105 | 106 | private boolean matchesBegin(String line) { 107 | String base = "[ ]*" + KEYWORD_BEGIN; 108 | return StringHelper.stringMatches(line, base, base + LINE_COMMENT); 109 | } 110 | 111 | private boolean matchesEmpty(String line) { 112 | String base = "[ ]*"; 113 | return StringHelper.stringMatches(line, base); 114 | } 115 | 116 | private boolean matchesEnd(String line) { 117 | String base = "[ ]*" + KEYWORD_END; 118 | return StringHelper.stringMatches(line, base, base + LINE_COMMENT); 119 | } 120 | 121 | } 122 | -------------------------------------------------------------------------------- /src/main/java/net/ericsonj/verilog/statements/WhileState.java: -------------------------------------------------------------------------------- 1 | package net.ericsonj.verilog.statements; 2 | 3 | import net.ericsonj.verilog.StatementState; 4 | 5 | /** 6 | * 7 | * @author Ericson Joseph 8 | * 9 | * Create on Feb 16, 2019 12:06:32 PM 10 | */ 11 | public class WhileState extends StatementState { 12 | 13 | public enum STATE { 14 | INIT, 15 | WHILE 16 | } 17 | 18 | public enum STATE_BLOCK { 19 | INIT, 20 | MAYBE_BLOCK, 21 | IN_BLOCK, 22 | NO_BLOCK 23 | } 24 | 25 | private STATE_BLOCK stateBlock; 26 | private STATE state; 27 | 28 | public WhileState() { 29 | super("while", 0); 30 | this.stateBlock = STATE_BLOCK.INIT; 31 | } 32 | 33 | public STATE_BLOCK getStateBlock() { 34 | return stateBlock; 35 | } 36 | 37 | public void setStateBlock(STATE_BLOCK stateBlock) { 38 | this.stateBlock = stateBlock; 39 | } 40 | 41 | public STATE getState() { 42 | return state; 43 | } 44 | 45 | public void setState(STATE state) { 46 | this.state = state; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /verilog/.verilog-format.properties: -------------------------------------------------------------------------------- 1 | IndentWidth=4 2 | IndentType=space 3 | SpacesBeforeTrailingComments=0 4 | SpacesAfterTrailingComments=0 5 | AlignLineComments=true 6 | AlignNoBlockingAssignments=true 7 | AlignBlockingAssignments=true 8 | SpacesInParentheses=false 9 | SpacesInSquareBrackets=false -------------------------------------------------------------------------------- /verilog/fsmtx.v: -------------------------------------------------------------------------------- 1 | // ---------------------------------------------------------------------------- 2 | // -- Prueba de tranmision 1. Se transmiten ráfagas del caracter "A" cuando 3 | // -- la señal de dtr se activa 4 | // ------------------------------------------ 5 | // -- (C) BQ. September 2015. Written by Juan Gonzalez (Obijuan) 6 | // -- GPL license 7 | // -- 8 | // ---------------------------------------------------------------------------- 9 | // -- Comprobado su funcionamiento a todas las velocidades estandares: 10 | // -- 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200 11 | // ---------------------------------------------------------------------------- 12 | // -- Although this transmitter has been written from the scratch, it has been 13 | // -- inspired by the one developed in the swapforth proyect by James Bowman 14 | // -- 15 | // -- https:// github.com/jamesbowman/swapforth 16 | // -- 17 | // ---------------------------------------------------------------------------- 18 | `default_nettype none 19 | 20 | `include "baudgen.vh" 21 | 22 | // --- Modulo que envia un caracter cuando load esta a 1 23 | // --- La salida tx ESTA REGISTRADA 24 | module fsmtx (input wire clk, // -- Reloj del sistema (12MHz en ICEstick) 25 | input wire start, // -- Activar a 1 para transmitir 26 | output reg tx); 27 | 28 | // -- Parametro: velocidad de transmision 29 | // -- Pruebas del caso peor: a 300 baudios 30 | parameter BAUD = `B300; 31 | 32 | // -- Caracter a enviar 33 | parameter CAR = "A"; 34 | 35 | // -- Registro de 10 bits para almacenar la trama a enviar: 36 | // -- 1 bit start + 8 bits datos + 1 bit stop 37 | reg [9:0] shiftiring; 38 | 39 | // -- Señal de start registrada 40 | reg start_r; 41 | 42 | // -- Reloj para la transmision 43 | wire clk_baud; 44 | 45 | // -- Reset 46 | reg rstn = 0; 47 | 48 | // -- Bitcounter 49 | reg [3:0] bitc; 50 | 51 | // --------- Microordenes 52 | wire load; // -- Carga del registro de desplazamiento. Puesta a 0 del 53 | // -- contador de bits 54 | wire baud_en; // -- Habilitar el generador de baudios para la transmision 55 | 56 | // ------------------------------------- 57 | // -- RUTA DE DATOS 58 | // ------------------------------------- 59 | 60 | // -- Registrar la entrada start 61 | // -- (para cumplir con las reglas de diseño sincrono) 62 | always @(posedge clk) 63 | start_r <= start; 64 | 65 | // -- Registro de desplazamiento, con carga paralela 66 | // -- Cuando load_r es 0, se carga la trama 67 | // -- Cuando load_r es 1 y el reloj de baudios esta a 1 se desplaza hacia 68 | // -- la derecha, enviando el siguiente bit 69 | // -- Se introducen '1's por la izquierda 70 | always @(posedge clk) 71 | // -- Reset 72 | if (rstn == 0) 73 | shiftiring <= 10'b11_1111_1111; 74 | else if (load == 1) 75 | shiftiring <= {CAR,2'b01}; 76 | else if (load == 0 && clk_baud == 1) // -- Modo desplazamiento 77 | shiftiring <= {1'b1, shiftiringf ter[9:1]}; 78 | 79 | always @(posedge clk) 80 | if (load == 1) 81 | bitc <= 0; 82 | else if (load == 0 && clk_baud == 1) 83 | bitc <= bitc + 1; 84 | 85 | // -- Sacar por tx el bit menos signif icativo del registros de desplazamiento 86 | // -- Cuando estamos en modo carga (load_r == 0), se saca siempre un 1 para 87 | // -- que la linea este siempre a un estado de reposo. De esta forma en el 88 | // -- inicio tx esta en reposo, aunque el valor del registro de desplazamiento 89 | // -- sea desconocido 90 | // -- ES UNA SALIDA REGISTRADA, puesto que tx se conecta a un bus sincrono 91 | // -- y hay que evitar que salgan pulsos espureos (glitches) 92 | always @(posedge clk) 93 | tx <= shiftiring[0]; 94 | 95 | // -- Divisor para obtener el reloj de transmision 96 | 97 | baudgen #(BAUD) BAUD0 (.clk(clk), 98 | .clk_ena(baud_en), 99 | .clk_out(clk_baud)); 100 | 101 | // ------------------------------ 102 | // -- CONTROLADOR 103 | // ------------------------------ 104 | 105 | // -- Estados del automata finito del controlador 106 | localparam IDLE = 0; 107 | localparam START = 1; 108 | localparam TRANS = 2; 109 | 110 | // -- Estados del autómata del controlador 111 | reg [1:0] state; 112 | 113 | // -- Transiciones entre los estados 114 | always @(posedge clk) 115 | begin 116 | // -- Reset del automata. Al estado inicial 117 | if (rstn == 0) 118 | state <= IDLE; 119 | else 120 | // -- Transiciones a los siguientes estados 121 | case (state) 122 | 123 | // -- Estado de reposo. Se sale cuando la señal 124 | // -- de start se pone a 1 125 | IDLE: begin 126 | if (start_r == 1) 127 | state <= START; 128 | else 129 | state <= IDLE; 130 | end 131 | // -- Estado de comienzo. Prepararse para empezar 132 | // -- a transmitir. Duracion: 1 ciclo de reloj 133 | START: begin 134 | state <= TRANS; 135 | end 136 | // -- Transmitiendo. Se esta en este estado hasta 137 | // -- que se hayan transmitido todos los bits pendientes 138 | TRANS: begin 139 | if (bitc == 11) 140 | state <= IDLE; 141 | else 142 | state <= TRANS; 143 | end 144 | // -- Por defecto. NO USADO. Puesto para 145 | // -- cubrir todos los casos y que no se generen latches 146 | default: begin 147 | state <= IDLE; 148 | end 149 | endcase 150 | end 151 | // -- Generacion de las microordenes 152 | assign load = (state == START) ? 1 : 0; 153 | assign baud_en = (state == IDLE) ? 0 : 1; 154 | 155 | 156 | // -- Inicializador 157 | always @(posedge clk) 158 | rstn <= 1; 159 | 160 | 161 | endmodule 162 | -------------------------------------------------------------------------------- /verilog/genrom.v: -------------------------------------------------------------------------------- 1 | /** 2 | * File: genrom.v 3 | * Author: Ericson Joseph 4 | * 5 | * Created on January 28, 2019, 8:15 PM 6 | */ 7 | 8 | module genrom #(parameter AW = 5, // Address Width 9 | parameter DW = 4) 10 | (input clk, 11 | input wire [ AW-1:0 ] addr, 12 | output reg [ DW-1:0 ] data); 13 | 14 | // holaa comentario 15 | 16 | /** 17 | *block comment 18 | */ 19 | 20 | parameter ROMFILE = "rom1.list"; // COMENTARIO 1 21 | localparam NPOS = 2 ** AW; 22 | 23 | reg [ DW-1:0 ] rom [ 0:NPOS-1 ]; 24 | 25 | always @( posedge clk ) begin 26 | data <= rom[ addr ]; 27 | if ( a == 0 ) // IF COMMENT 28 | begin // BEGIN COMMENT 29 | a = 2; // 30 | end // END commnet 31 | else if ( b == 4 ) // COMMNET ELSE IF 32 | begin // 33 | a = 55; // 34 | end // 35 | else // ELSE COMMNET 36 | begin // 37 | a = 3; // 38 | end // 39 | 40 | end 41 | 42 | initial begin 43 | $readmemh( ROMFILE, rom ); 44 | end 45 | 46 | 47 | endmodule 48 | --------------------------------------------------------------------------------