1 21 22 package net.percederberg.grammatica.output; 23 24 import java.io.IOException ; 25 import java.util.HashMap ; 26 27 import net.percederberg.grammatica.code.java.JavaComment; 28 import net.percederberg.grammatica.code.java.JavaFile; 29 import net.percederberg.grammatica.code.java.JavaInterface; 30 import net.percederberg.grammatica.code.java.JavaVariable; 31 import net.percederberg.grammatica.parser.ProductionPattern; 32 import net.percederberg.grammatica.parser.TokenPattern; 33 34 41 class JavaConstantsFile { 42 43 46 private static final String TYPE_COMMENT = 47 "An interface with constants for the parser and tokenizer."; 48 49 52 private static final String TOKEN_COMMENT = 53 "A token identity constant."; 54 55 58 private static final String PRODUCTION_COMMENT = 59 "A production node identity constant."; 60 61 64 private JavaParserGenerator gen; 65 66 69 private JavaFile file; 70 71 74 private JavaInterface ifc; 75 76 80 private HashMap constantNames = new HashMap (); 81 82 87 public JavaConstantsFile(JavaParserGenerator gen) { 88 int modifiers; 89 90 this.gen = gen; 91 this.file = gen.createJavaFile(); 92 if (gen.getPublicAccess()) { 93 modifiers = JavaInterface.PUBLIC; 94 } else { 95 modifiers = JavaInterface.PACKAGE_LOCAL; 96 } 97 this.ifc = new JavaInterface(modifiers, 98 gen.getBaseName() + "Constants"); 99 initializeCode(); 100 } 101 102 105 private void initializeCode() { 106 String str; 107 108 file.addInterface(ifc); 110 111 str = file.toString() + "\n\n" + gen.getFileComment(); 113 file.addComment(new JavaComment(JavaComment.BLOCK, str)); 114 115 str = TYPE_COMMENT; 117 if (gen.getClassComment() != null) { 118 str += "\n\n" + gen.getClassComment(); 119 } 120 ifc.addComment(new JavaComment(str)); 121 } 122 123 128 public void addToken(TokenPattern pattern) { 129 String constant; 130 JavaVariable var; 131 int modifiers; 132 133 constant = gen.getCodeStyle().getUpperCase(pattern.getName()); 134 modifiers = JavaVariable.PUBLIC + JavaVariable.STATIC + 135 JavaVariable.FINAL; 136 var = new JavaVariable(modifiers, 137 "int", 138 constant, 139 "" + pattern.getId()); 140 var.addComment(new JavaComment(TOKEN_COMMENT)); 141 ifc.addVariable(var); 142 constantNames.put(new Integer (pattern.getId()), constant); 143 } 144 145 151 public void addProduction(ProductionPattern pattern) { 152 String constant; 153 JavaVariable var; 154 int modifiers; 155 156 if (!pattern.isSynthetic()) { 157 constant = gen.getCodeStyle().getUpperCase(pattern.getName()); 158 modifiers = JavaVariable.PUBLIC + JavaVariable.STATIC + 159 JavaVariable.FINAL; 160 var = new JavaVariable(modifiers, 161 "int", 162 constant, 163 "" + pattern.getId()); 164 var.addComment(new JavaComment(PRODUCTION_COMMENT)); 165 ifc.addVariable(var); 166 constantNames.put(new Integer (pattern.getId()), constant); 167 } 168 } 169 170 179 public String getConstant(int id) { 180 String name = (String ) constantNames.get(new Integer (id)); 181 182 if (name == null) { 183 return null; 184 } else { 185 return ifc.toString() + "." + name; 186 } 187 } 188 189 195 public void writeCode() throws IOException { 196 file.writeCode(gen.getCodeStyle()); 197 } 198 } 199 | Popular Tags |