1 21 22 package net.percederberg.grammatica.output; 23 24 import java.io.IOException ; 25 26 import net.percederberg.grammatica.Grammar; 27 import net.percederberg.grammatica.code.CodeStyle; 28 import net.percederberg.grammatica.code.java.JavaFile; 29 import net.percederberg.grammatica.code.java.JavaPackage; 30 import net.percederberg.grammatica.parser.ProductionPattern; 31 import net.percederberg.grammatica.parser.TokenPattern; 32 33 40 public class JavaParserGenerator extends ParserGenerator { 41 42 45 private String basePackage = null; 46 47 50 private String baseName = null; 51 52 55 private boolean publicAccess = false; 56 57 60 private String classComment = null; 61 62 67 public JavaParserGenerator(Grammar grammar) { 68 super(grammar); 69 initialize(); 70 } 71 72 75 private void initialize() { 76 StringBuffer buffer; 77 String str; 78 79 str = getGrammar().getFileName(); 81 if (str.indexOf('/') >= 0) { 82 str = str.substring(str.lastIndexOf('/') + 1); 83 } 84 if (str.indexOf('\\') >= 0) { 85 str = str.substring(str.lastIndexOf('\\') + 1); 86 } 87 if (str.indexOf('.') > 0) { 88 str = str.substring(0, str.indexOf('.')); 89 } 90 if (Character.isLowerCase(str.charAt(0))) { 91 str = Character.toUpperCase(str.charAt(0)) + str.substring(1); 92 } 93 baseName = str; 94 95 buffer = new StringBuffer (); 97 str = getGrammar().getDeclaration(Grammar.AUTHOR_DECLARATION); 98 if (str != null) { 99 buffer.append("@author "); 100 buffer.append(str); 101 } 102 str = getGrammar().getDeclaration(Grammar.VERSION_DECLARATION); 103 if (str != null) { 104 if (buffer.length() > 0) { 105 buffer.append("\n"); 106 } 107 buffer.append("@version "); 108 buffer.append(str); 109 } 110 classComment = buffer.toString(); 111 } 112 113 118 public String getBasePackage() { 119 return basePackage; 120 } 121 122 127 public void setBasePackage(String pkg) { 128 this.basePackage = pkg; 129 } 130 131 136 public String getBaseName() { 137 return baseName; 138 } 139 140 145 public void setBaseName(String name) { 146 this.baseName = name; 147 } 148 149 155 public boolean getPublicAccess() { 156 return publicAccess; 157 } 158 159 164 public void setPublicAccess(boolean flag) { 165 publicAccess = flag; 166 } 167 168 173 public CodeStyle getCodeStyle() { 174 return CodeStyle.JAVA; 175 } 176 177 182 public String getClassComment() { 183 return classComment; 184 } 185 186 191 public void write() throws IOException { 192 Grammar grammar = getGrammar(); 193 JavaConstantsFile constants = new JavaConstantsFile(this); 194 JavaTokenizerFile tokenizer = new JavaTokenizerFile(this); 195 JavaParserFile parser = new JavaParserFile(this, tokenizer); 196 JavaAnalyzerFile analyzer = new JavaAnalyzerFile(this); 197 TokenPattern token; 198 ProductionPattern production; 199 int i; 200 201 for (i = 0; i < grammar.getTokenPatternCount(); i++) { 203 token = grammar.getTokenPattern(i); 204 constants.addToken(token); 205 tokenizer.addToken(token, constants); 206 analyzer.addToken(token, constants); 207 } 208 209 for (i = 0; i < grammar.getProductionPatternCount(); i++) { 211 production = grammar.getProductionPattern(i); 212 constants.addProduction(production); 213 parser.addProductionConstant(production); 214 analyzer.addProduction(production, constants); 215 } 216 217 for (i = 0; i < grammar.getProductionPatternCount(); i++) { 219 production = grammar.getProductionPattern(i); 220 parser.addProduction(production, constants); 221 } 222 223 constants.writeCode(); 225 tokenizer.writeCode(); 226 parser.writeCode(); 227 analyzer.writeCode(); 228 } 229 230 236 public JavaFile createJavaFile() { 237 if (basePackage == null) { 238 return new JavaFile(getBaseDir()); 239 } else { 240 return new JavaFile(getBaseDir(), 241 new JavaPackage(getBasePackage())); 242 } 243 } 244 } 245 | Popular Tags |