1 19 20 package org.netbeans.modules.lexer.gen; 21 22 import java.lang.reflect.Field ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.Map ; 27 import java.util.HashMap ; 28 import java.util.Set ; 29 import java.util.List ; 30 31 import org.netbeans.api.lexer.TokenId; 32 import org.netbeans.spi.lexer.util.LexerUtilities; 33 import java.util.Iterator ; 34 import org.netbeans.modules.lexer.gen.util.LexerGenUtilities; 35 36 46 47 public class LanguageGenerator { 48 49 50 private Map imports; 51 52 public LanguageGenerator() { 53 this.imports = new HashMap (); 54 } 55 56 public String createSource(LanguageData data) { 57 data.check(); 59 60 StringBuffer sb = new StringBuffer (); 61 62 appendClassDeclaration(sb, data); 64 65 appendClassStart(sb, data); 67 68 addToImports("org.netbeans.api.lexer.TokenId"); 69 70 for (Iterator idsIterator = data.getSortedIds( 72 LanguageData.IDS_INT_ID_COMPARATOR).iterator(); 73 idsIterator.hasNext(); 74 ) { 75 MutableTokenId id = (MutableTokenId)idsIterator.next(); 76 LexerGenUtilities.appendSpaces(sb, 4); 77 sb.append("public static final int "); 78 sb.append(id.getIntIdFieldName()); 79 sb.append(" = "); 80 sb.append(id.getIntId()); 81 sb.append(";\n"); 82 } 83 84 sb.append("\n\n"); 85 86 87 for (Iterator idsIterator = data.getSortedIds( 88 LanguageData.IDS_FIELD_NAME_COMPARATOR).iterator(); 89 idsIterator.hasNext(); 90 ) { 91 MutableTokenId id = (MutableTokenId)idsIterator.next(); 92 LexerGenUtilities.appendSpaces(sb, 4); 93 sb.append("public static final TokenId "); 94 sb.append(id.getFieldName()); 95 sb.append(" = new TokenId(\""); 96 LexerUtilities.appendToSource(sb, id.getName()); 97 sb.append("\", "); 98 sb.append(id.getIntIdFieldName()); 99 100 List catList = id.getCategoryNames(); 102 int catCnt = catList.size(); 103 if (catCnt > 0) { 104 sb.append(", new String[]{\""); 105 for (int j = 0; j < catCnt; j++) { 106 if (j > 0) { 107 sb.append(", \""); 108 } 109 LexerUtilities.appendToSource(sb, (String )catList.get(j)); 110 sb.append('"'); 111 } 112 sb.append('}'); 113 } 114 115 List sampleTexts = id.getSampleTexts(); 117 int sampleTextsLength = sampleTexts.size(); 118 if (sampleTextsLength > 0) { 119 if (catCnt == 0) { sb.append(", null"); 121 } 122 123 addToImports("org.netbeans.spi.lexer.MatcherFactory"); 124 sb.append(", MatcherFactory.create"); 125 String sampleTextCheck = id.getSampleTextCheck(); 126 if (sampleTextsLength == 1) { if ("none".equals(sampleTextCheck)) { 128 sb.append("NoCheckMatcher"); 129 } else if ("length".equals(sampleTextCheck)) { 130 sb.append("LengthCheckMatcher"); 131 } else { 132 sb.append("TextCheckMatcher"); 133 } 134 135 sb.append("(\""); 136 LexerUtilities.appendToSource(sb, (String )sampleTexts.get(0)); 137 sb.append('"'); 138 139 } else { sb.append("MultiMatcher(new String[] {"); 141 142 for (Iterator it = sampleTexts.iterator(); it.hasNext();) { 143 sb.append('"'); 144 LexerUtilities.appendToSource(sb, (String )it.next()); 145 sb.append('"'); 146 if (it.hasNext()) { 147 sb.append(", "); 148 } 149 } 150 sb.append("}"); 151 } 152 sb.append(")"); 153 154 } 155 156 sb.append(");"); 157 158 String comment = id.getComment(); 159 if (comment != null) { 160 sb.append(" // "); 161 sb.append(comment); 162 } 163 164 sb.append('\n'); 165 } 166 167 sb.append('\n'); 169 appendClassConstructor(sb, data); 170 171 LexerGenUtilities.appendSpaces(sb, 4); 173 sb.append("public "); 174 sb.append(addToImports("org.netbeans.api.lexer.Lexer")); 175 sb.append(" createLexer() {\n"); 176 177 appendLexerMethodBody(sb, data); 178 LexerGenUtilities.appendSpaces(sb, 4); 179 sb.append("}\n"); 180 181 appendClassEnd(sb, data); 183 184 sb.append("\n}\n"); 185 186 187 StringBuffer hsb = new StringBuffer (); 189 String langClassPkgName = getLanguageClassPkgName(data); 192 if (!"".equals(langClassPkgName)) { 193 hsb.append("\npackage ").append(langClassPkgName).append(";\n"); 194 } 195 196 hsb.append('\n'); 198 appendImports(hsb, data); 199 hsb.append('\n'); 200 201 sb.insert(0, hsb.toString()); 203 204 return sb.toString(); 205 } 206 207 private String getLanguageClassNameWithoutPkg(LanguageData data) { 208 return addToImports(data.getLanguageClassName()); 209 } 210 211 private String getLanguageClassPkgName(LanguageData data) { 212 return LexerGenUtilities.splitClassName(data.getLanguageClassName())[0]; 213 } 214 215 protected void appendClassDeclaration(StringBuffer sb, LanguageData data) { 216 sb.append("public class "); 217 sb.append(getLanguageClassNameWithoutPkg(data)); 218 sb.append(" extends "); 219 sb.append(addToImports("org.netbeans.spi.lexer.AbstractLanguage")); 220 sb.append(" {\n\n"); 221 } 222 223 protected void appendClassStart(StringBuffer sb, LanguageData data) { 224 225 String langClassNameWithoutPkg = getLanguageClassNameWithoutPkg(data); 227 LexerGenUtilities.appendSpaces(sb, 4); 228 sb.append("/** Lazily initialized singleton instance of this language. */\n"); 229 LexerGenUtilities.appendSpaces(sb, 4); 230 sb.append("private static ").append(langClassNameWithoutPkg); 231 sb.append(" INSTANCE;\n\n"); 232 LexerGenUtilities.appendSpaces(sb, 4); 233 sb.append("/** @return singleton instance of this language. */\n"); 234 LexerGenUtilities.appendSpaces(sb, 4); 235 sb.append("public static synchronized ").append(langClassNameWithoutPkg); 236 sb.append(" get() {\n"); 237 LexerGenUtilities.appendSpaces(sb, 8); 238 sb.append("if (INSTANCE == null)\n"); 239 LexerGenUtilities.appendSpaces(sb, 12); 240 sb.append("INSTANCE = new ").append(langClassNameWithoutPkg); 241 sb.append("();\n\n"); 242 LexerGenUtilities.appendSpaces(sb, 8); 243 sb.append("return INSTANCE;\n"); 244 LexerGenUtilities.appendSpaces(sb, 4); 245 sb.append("}\n\n"); 246 } 247 248 protected void appendClassConstructor(StringBuffer sb, LanguageData data) { 249 LexerGenUtilities.appendSpaces(sb, 4); 250 sb.append(getLanguageClassNameWithoutPkg(data)); 251 sb.append("() {\n"); 252 LexerGenUtilities.appendSpaces(sb, 4); 253 sb.append("}\n\n"); 254 } 255 256 protected void appendClassEnd(StringBuffer sb, LanguageData data) { 257 } 258 259 protected void appendLexerMethodBody(StringBuffer sb, LanguageData data) { 260 String lexerClassName = data.getLexerClassName(); 261 addToImports(lexerClassName); 262 LexerGenUtilities.appendSpaces(sb, 8); 263 sb.append("return new "); 264 sb.append(addToImports(lexerClassName)); 265 sb.append("();\n"); 266 } 267 268 276 protected final String addToImports(String className) { 277 String [] pkgCls = (String [])imports.get(className); 278 if (pkgCls == null) { 279 pkgCls = LexerGenUtilities.splitClassName(className); 280 imports.put(className, pkgCls); 281 } 282 return pkgCls[1]; 283 } 284 285 private void appendImports(StringBuffer sb, LanguageData data) { 286 String langClassPkgName = getLanguageClassPkgName(data); 287 Set iSet = imports.keySet(); 288 String [] importClasses = new String [iSet.size()]; 289 iSet.toArray(importClasses); 290 Arrays.sort(importClasses); 291 for (int i = 0; i < importClasses.length; i++) { 292 String className = importClasses[i]; 293 String [] pkgCls = (String [])imports.get(className); 294 if (!"".equals(pkgCls[0]) && !"java.lang".equals(pkgCls[0]) 295 && !pkgCls[0].equals(langClassPkgName) ) { 297 sb.append("import ").append(className).append(";\n"); 298 } 299 } 300 } 301 302 } 303 304 | Popular Tags |