1 /* 2 * Copyright (c) 2018 Helmut Neemann. 3 * Use of this source code is governed by the GPL v3 license 4 * that can be found in the LICENSE file. 5 */ 6 package de.neemann.digital.hdl.vhdl2; 7 8 import de.neemann.digital.hdl.model2.HDLModel; 9 10 import java.util.Collections; 11 import java.util.HashSet; 12 13 /** 14 * Renames the labels to valid VHDL names. 15 */ 16 public class VHDLRenaming implements HDLModel.Renaming { 17 18 private static final HashSet<String> KEYWORDS = new HashSet<>(); 19 20 static { Collections.addAll(KEYWORDS, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R, R)21 Collections.addAll(KEYWORDS, 22 "abs", "access", "after", "alias", "all", "and", "architecture", "array", "assert", 23 "attribute", "begin", "block", "body", "buffer", "bus", "case", "component", "configuration", 24 "constant", "disconnect", "downto", "else", "elsif", "end", "entity", "exit", "file", 25 "for", "function", "generate", "generic", "group", "guarded", "if", "impure", "in", 26 "inertial", "inout", "is", "label", "library", "linkage", "literal", "loop", 27 "map", "mod", "nand", "new", "next", "nor", "not", "null", "of", 28 "on", "open", "or", "others", "out", "package", "port", "postponed", "procedure", 29 "process", "pure", "range", "record", "register", "reject", "rem", "report", "return", 30 "rol", "ror", "select", "severity", "signal", "shared", "sla", "sll", "sra", 31 "srl", "subtype", "then", "to", "transport", "type", "unaffected", "units", "until", 32 "use", "variable", "wait", "when", "while", "with", "xnor", "xor"); 33 } 34 35 36 @Override checkName(String name)37 public String checkName(String name) { 38 if (isKeyword(name)) 39 return "p_" + name; 40 else { 41 if (Character.isDigit(name.charAt(0))) 42 name = "n" + name; 43 return cleanName(name); 44 } 45 } 46 isKeyword(String str)47 private boolean isKeyword(String str) { 48 return KEYWORDS.contains(str.toLowerCase()); 49 } 50 cleanName(String name)51 private String cleanName(String name) { 52 StringBuilder sb = new StringBuilder(); 53 for (int i = 0; i < name.length(); i++) { 54 char c = name.charAt(i); 55 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) 56 sb.append(c); 57 else { 58 switch (c) { 59 case '/': 60 case '!': 61 case '~': 62 case '\u00AC': 63 sb.append("not"); 64 break; 65 case '=': 66 sb.append("eq"); 67 break; 68 case '<': 69 sb.append("le"); 70 break; 71 case '>': 72 sb.append("gr"); 73 break; 74 default: 75 if (sb.length() > 0 && sb.charAt(sb.length() - 1) != '_') 76 sb.append("_"); 77 } 78 } 79 } 80 81 while (sb.length() > 0 && sb.charAt(sb.length() - 1) == '_') 82 sb.setLength(sb.length() - 1); 83 84 return sb.toString(); 85 } 86 87 88 } 89