1 22 23 package net.percederberg.grammatica.output; 24 25 import java.io.IOException ; 26 import java.util.HashMap ; 27 28 import net.percederberg.grammatica.code.visualbasic.VisualBasicComment; 29 import net.percederberg.grammatica.code.visualbasic.VisualBasicEnumeration; 30 import net.percederberg.grammatica.code.visualbasic.VisualBasicFile; 31 import net.percederberg.grammatica.code.visualbasic.VisualBasicNamespace; 32 import net.percederberg.grammatica.parser.ProductionPattern; 33 import net.percederberg.grammatica.parser.TokenPattern; 34 35 45 class VisualBasicConstantsFile { 46 47 50 private static final String TYPE_COMMENT = 51 "<remarks>An enumeration with token and production node\n" + 52 "constants.</remarks>"; 53 54 57 private VisualBasicParserGenerator gen; 58 59 62 private VisualBasicFile file; 63 64 67 private VisualBasicEnumeration enm; 68 69 73 private HashMap constantNames = new HashMap (); 74 75 80 public VisualBasicConstantsFile(VisualBasicParserGenerator gen) { 81 String name = gen.getBaseName() + "Constants"; 82 int modifiers; 83 84 this.gen = gen; 85 this.file = new VisualBasicFile(gen.getBaseDir(), name); 86 if (gen.getPublicAccess()) { 87 modifiers = VisualBasicEnumeration.PUBLIC; 88 } else { 89 modifiers = VisualBasicEnumeration.FRIEND; 90 } 91 this.enm = new VisualBasicEnumeration(modifiers, name); 92 initializeCode(); 93 } 94 95 98 private void initializeCode() { 99 String str; 100 VisualBasicNamespace n; 101 102 if (gen.getNamespace() == null) { 104 file.addEnumeration(enm); 105 } else { 106 n = new VisualBasicNamespace(gen.getNamespace()); 107 n.addEnumeration(enm); 108 file.addNamespace(n); 109 } 110 111 str = file.toString() + "\n\n" + gen.getFileComment(); 113 file.addComment(new VisualBasicComment(VisualBasicComment.SINGLELINE, 114 str)); 115 116 enm.addComment(new VisualBasicComment(TYPE_COMMENT)); 118 } 119 120 125 public void addToken(TokenPattern pattern) { 126 String constant; 127 128 constant = gen.getCodeStyle().getUpperCase(pattern.getName()); 129 enm.addConstant(constant, String.valueOf(pattern.getId())); 130 constantNames.put(new Integer (pattern.getId()), constant); 131 } 132 133 139 public void addProduction(ProductionPattern pattern) { 140 String constant; 141 142 if (!pattern.isSynthetic()) { 143 constant = gen.getCodeStyle().getUpperCase(pattern.getName()); 144 enm.addConstant(constant, String.valueOf(pattern.getId())); 145 constantNames.put(new Integer (pattern.getId()), constant); 146 } 147 } 148 149 158 public String getConstant(int id) { 159 String name = (String ) constantNames.get(new Integer (id)); 160 161 if (name == null) { 162 return null; 163 } else { 164 return enm.toString() + "." + name; 165 } 166 } 167 168 174 public void writeCode() throws IOException { 175 file.writeCode(gen.getCodeStyle()); 176 } 177 } 178 | Popular Tags |