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.csharp.CSharpComment; 28 import net.percederberg.grammatica.code.csharp.CSharpEnumeration; 29 import net.percederberg.grammatica.code.csharp.CSharpFile; 30 import net.percederberg.grammatica.code.csharp.CSharpNamespace; 31 import net.percederberg.grammatica.parser.ProductionPattern; 32 import net.percederberg.grammatica.parser.TokenPattern; 33 34 41 class CSharpConstantsFile { 42 43 46 private static final String TYPE_COMMENT = 47 "<remarks>An enumeration with token and production node\n" + 48 "constants.</remarks>"; 49 50 53 private CSharpParserGenerator gen; 54 55 58 private CSharpFile file; 59 60 63 private CSharpEnumeration enm; 64 65 69 private HashMap constantNames = new HashMap (); 70 71 76 public CSharpConstantsFile(CSharpParserGenerator gen) { 77 String name = gen.getBaseName() + "Constants"; 78 int modifiers; 79 80 this.gen = gen; 81 this.file = new CSharpFile(gen.getBaseDir(), name); 82 if (gen.getPublicAccess()) { 83 modifiers = CSharpEnumeration.PUBLIC; 84 } else { 85 modifiers = CSharpEnumeration.INTERNAL; 86 } 87 this.enm = new CSharpEnumeration(modifiers, name); 88 initializeCode(); 89 } 90 91 94 private void initializeCode() { 95 String str; 96 97 if (gen.getNamespace() == null) { 99 file.addEnumeration(enm); 100 } else { 101 CSharpNamespace n = new CSharpNamespace(gen.getNamespace()); 102 n.addEnumeration(enm); 103 file.addNamespace(n); 104 } 105 106 str = file.toString() + "\n\n" + gen.getFileComment(); 108 file.addComment(new CSharpComment(CSharpComment.BLOCK, str)); 109 110 enm.addComment(new CSharpComment(TYPE_COMMENT)); 112 } 113 114 119 public void addToken(TokenPattern pattern) { 120 String constant; 121 122 constant = gen.getCodeStyle().getUpperCase(pattern.getName()); 123 enm.addConstant(constant, String.valueOf(pattern.getId())); 124 constantNames.put(new Integer (pattern.getId()), constant); 125 } 126 127 133 public void addProduction(ProductionPattern pattern) { 134 String constant; 135 136 if (!pattern.isSynthetic()) { 137 constant = gen.getCodeStyle().getUpperCase(pattern.getName()); 138 enm.addConstant(constant, String.valueOf(pattern.getId())); 139 constantNames.put(new Integer (pattern.getId()), constant); 140 } 141 } 142 143 152 public String getConstant(int id) { 153 String name = (String ) constantNames.get(new Integer (id)); 154 155 if (name == null) { 156 return null; 157 } else { 158 return enm.toString() + "." + name; 159 } 160 } 161 162 168 public void writeCode() throws IOException { 169 file.writeCode(gen.getCodeStyle()); 170 } 171 } 172 | Popular Tags |