1 21 22 package net.percederberg.grammatica.output; 23 24 import java.io.IOException ; 25 26 import net.percederberg.grammatica.code.java.JavaClass; 27 import net.percederberg.grammatica.code.java.JavaComment; 28 import net.percederberg.grammatica.code.java.JavaConstructor; 29 import net.percederberg.grammatica.code.java.JavaFile; 30 import net.percederberg.grammatica.code.java.JavaImport; 31 import net.percederberg.grammatica.code.java.JavaMethod; 32 import net.percederberg.grammatica.parser.TokenPattern; 33 34 41 class JavaTokenizerFile { 42 43 46 private static final String CLASS_COMMENT = 47 "A character stream tokenizer."; 48 49 52 private static final String CONSTRUCTOR_COMMENT = 53 "Creates a new tokenizer for the specified input stream.\n\n" + 54 "@param input the input stream to read\n\n" + 55 "@throws ParserCreationException if the tokenizer couldn't be\n" + 56 " initialized correctly"; 57 58 61 private static final String INIT_METHOD_COMMENT = 62 "Initializes the tokenizer by creating all the token patterns.\n\n" + 63 "@throws ParserCreationException if the tokenizer couldn't be\n" + 64 " initialized correctly"; 65 66 69 private JavaParserGenerator gen; 70 71 74 private JavaFile file; 75 76 79 private JavaClass cls; 80 81 84 private JavaMethod initMethod; 85 86 91 public JavaTokenizerFile(JavaParserGenerator gen) { 92 int modifiers; 93 94 this.gen = gen; 95 this.file = gen.createJavaFile(); 96 if (gen.getPublicAccess()) { 97 modifiers = JavaClass.PUBLIC; 98 } else { 99 modifiers = JavaClass.PACKAGE_LOCAL; 100 } 101 this.cls = new JavaClass(modifiers, 102 gen.getBaseName() + "Tokenizer", 103 "Tokenizer"); 104 this.initMethod = new JavaMethod(JavaMethod.PRIVATE, 105 "createPatterns", 106 "", 107 "void"); 108 initializeCode(); 109 } 110 111 114 private void initializeCode() { 115 JavaConstructor constr; 116 String str; 117 118 file.addImport(new JavaImport("java.io", "Reader")); 120 file.addImport(new JavaImport("net.percederberg.grammatica.parser", 121 "ParserCreationException")); 122 file.addImport(new JavaImport("net.percederberg.grammatica.parser", 123 "TokenPattern")); 124 file.addImport(new JavaImport("net.percederberg.grammatica.parser", 125 "Tokenizer")); 126 127 file.addClass(cls); 129 str = CLASS_COMMENT; 130 if (gen.getClassComment() != null) { 131 str += "\n\n" + gen.getClassComment(); 132 } 133 cls.addComment(new JavaComment(str)); 134 135 str = file.toString() + "\n\n" + gen.getFileComment(); 137 file.addComment(new JavaComment(JavaComment.BLOCK, str)); 138 139 constr = new JavaConstructor("Reader input"); 141 cls.addConstructor(constr); 142 constr.addComment(new JavaComment(CONSTRUCTOR_COMMENT)); 143 constr.addThrows("ParserCreationException"); 144 constr.addCode("super(input, " + 145 !gen.getGrammar().getCaseSensitive() + 146 ");"); 147 constr.addCode("createPatterns();"); 148 149 cls.addMethod(initMethod); 151 initMethod.addComment(new JavaComment(INIT_METHOD_COMMENT)); 152 initMethod.addThrows("ParserCreationException"); 153 initMethod.addCode("TokenPattern pattern;"); 154 } 155 156 162 public void addToken(TokenPattern pattern, JavaConstantsFile constants) { 163 StringBuffer code = new StringBuffer (); 164 String str; 165 166 code.append("pattern = new TokenPattern("); 168 code.append(constants.getConstant(pattern.getId())); 169 code.append(",\n"); 170 code.append(" \""); 171 code.append(pattern.getName()); 172 code.append("\",\n"); 173 code.append(" TokenPattern."); 174 switch (pattern.getType()) { 175 case TokenPattern.STRING_TYPE: 176 code.append("STRING_TYPE"); 177 break; 178 case TokenPattern.REGEXP_TYPE: 179 code.append("REGEXP_TYPE"); 180 break; 181 } 182 code.append(",\n"); 183 code.append(" "); 184 str = pattern.getPattern(); 185 code.append(gen.getCodeStyle().getStringConstant(str, '\\')); 186 code.append(");\n"); 187 188 if (pattern.isError()) { 190 code.append("pattern.setError("); 191 if (pattern.getErrorMessage() != null) { 192 str = pattern.getErrorMessage(); 193 code.append(gen.getCodeStyle().getStringConstant(str, '\\')); 194 } 195 code.append(");\n"); 196 } 197 if (pattern.isIgnore()) { 198 code.append("pattern.setIgnore("); 199 if (pattern.getIgnoreMessage() != null) { 200 str = pattern.getIgnoreMessage(); 201 code.append(gen.getCodeStyle().getStringConstant(str, '\\')); 202 } 203 code.append(");\n"); 204 } 205 206 code.append("addPattern(pattern);"); 208 initMethod.addCode(""); 209 initMethod.addCode(code.toString()); 210 } 211 212 220 protected String getConstructorCall(String param) { 221 return "new " + gen.getBaseName() + "Tokenizer(" + param + ")"; 222 } 223 224 230 public void writeCode() throws IOException { 231 file.writeCode(gen.getCodeStyle()); 232 } 233 } 234 | Popular Tags |