1 22 23 package net.percederberg.grammatica.output; 24 25 import java.io.IOException ; 26 27 import net.percederberg.grammatica.code.visualbasic.VisualBasicClass; 28 import net.percederberg.grammatica.code.visualbasic.VisualBasicComment; 29 import net.percederberg.grammatica.code.visualbasic.VisualBasicConstructor; 30 import net.percederberg.grammatica.code.visualbasic.VisualBasicFile; 31 import net.percederberg.grammatica.code.visualbasic.VisualBasicImports; 32 import net.percederberg.grammatica.code.visualbasic.VisualBasicNamespace; 33 import net.percederberg.grammatica.code.visualbasic.VisualBasicMethod; 34 import net.percederberg.grammatica.parser.TokenPattern; 35 36 45 class VisualBasicTokenizerFile { 46 47 50 private static final String TYPE_COMMENT = 51 "<remarks>A character stream tokenizer.</remarks>"; 52 53 56 private static final String CONSTRUCTOR_COMMENT = 57 "<summary>Creates a new tokenizer for the specified input\n" + 58 "stream.</summary>\n\n" + 59 "<param name='input'>the input stream to read</param>\n\n" + 60 "<exception cref='ParserCreationException'>if the tokenizer\n" + 61 "couldn't be initialized correctly</exception>"; 62 63 66 private static final String INIT_METHOD_COMMENT = 67 "<summary>Initializes the tokenizer by creating all the token\n" + 68 "patterns.</summary>\n\n" + 69 "<exception cref='ParserCreationException'>if the tokenizer\n" + 70 "couldn't be initialized correctly</exception>"; 71 72 75 private VisualBasicParserGenerator gen; 76 77 80 private VisualBasicFile file; 81 82 85 private VisualBasicClass cls; 86 87 90 private VisualBasicMethod initMethod; 91 92 97 public VisualBasicTokenizerFile(VisualBasicParserGenerator gen) { 98 String name = gen.getBaseName() + "Tokenizer"; 99 int modifiers; 100 101 this.gen = gen; 102 this.file = new VisualBasicFile(gen.getBaseDir(), name); 103 if (gen.getPublicAccess()) { 104 modifiers = VisualBasicClass.PUBLIC; 105 } else { 106 modifiers = VisualBasicClass.FRIEND; 107 } 108 this.cls = new VisualBasicClass(modifiers, name, "Tokenizer"); 109 this.initMethod = new VisualBasicMethod(VisualBasicMethod.PRIVATE, 110 "CreatePatterns", 111 "", 112 ""); 113 initializeCode(); 114 } 115 116 119 private void initializeCode() { 120 VisualBasicConstructor constr; 121 VisualBasicNamespace n; 122 String str; 123 124 file.addImports(new VisualBasicImports("System.IO")); 126 file.addImports(new VisualBasicImports("PerCederberg.Grammatica.Runtime")); 127 128 if (gen.getNamespace() == null) { 130 file.addClass(cls); 131 } else { 132 n = new VisualBasicNamespace(gen.getNamespace()); 133 n.addClass(cls); 134 file.addNamespace(n); 135 } 136 137 str = file.toString() + "\n\n" + gen.getFileComment(); 139 file.addComment(new VisualBasicComment(VisualBasicComment.SINGLELINE, 140 str)); 141 142 cls.addComment(new VisualBasicComment(TYPE_COMMENT)); 144 145 constr = new VisualBasicConstructor("ByVal input As TextReader"); 147 cls.addConstructor(constr); 148 constr.addComment(new VisualBasicComment(CONSTRUCTOR_COMMENT)); 149 constr.addCode("MyBase.New(input, " + 150 getBoolean(!gen.getGrammar().getCaseSensitive()) + 151 ")"); 152 constr.addCode("CreatePatterns()"); 153 154 cls.addMethod(initMethod); 156 initMethod.addComment(new VisualBasicComment(INIT_METHOD_COMMENT)); 157 initMethod.addCode("Dim pattern as TokenPattern"); 158 } 159 160 166 public void addToken(TokenPattern pattern, 167 VisualBasicConstantsFile constants) { 168 169 StringBuffer code = new StringBuffer (); 170 String str; 171 172 code.append("pattern = New TokenPattern(CInt("); 174 code.append(constants.getConstant(pattern.getId())); 175 code.append("), \""); 176 code.append(pattern.getName()); 177 code.append("\", TokenPattern.PatternType."); 178 switch (pattern.getType()) { 179 case TokenPattern.STRING_TYPE: 180 code.append("STRING"); 181 break; 182 case TokenPattern.REGEXP_TYPE: 183 code.append("REGEXP"); 184 break; 185 } 186 code.append(", "); 187 str = pattern.getPattern(); 188 code.append(gen.getCodeStyle().getStringConstant(str, '"')); 189 code.append(")\n"); 190 191 if (pattern.isError()) { 193 if (pattern.getErrorMessage() == null) { 194 code.append("pattern.Error = True"); 195 } else { 196 code.append("pattern.ErrorMessage = "); 197 str = pattern.getErrorMessage(); 198 code.append(gen.getCodeStyle().getStringConstant(str, '"')); 199 } 200 code.append("\n"); 201 } 202 if (pattern.isIgnore()) { 203 if (pattern.getIgnoreMessage() == null) { 204 code.append("pattern.Ignore = True"); 205 } else { 206 code.append("pattern.IgnoreMessage = "); 207 str = pattern.getIgnoreMessage(); 208 code.append(gen.getCodeStyle().getStringConstant(str, '"')); 209 } 210 code.append("\n"); 211 } 212 213 code.append("AddPattern(pattern)"); 215 initMethod.addCode(""); 216 initMethod.addCode(code.toString()); 217 } 218 219 227 protected String getConstructorCall(String param) { 228 return "New " + gen.getBaseName() + "Tokenizer(" + param + ")"; 229 } 230 231 238 private String getBoolean(boolean value) { 239 return value ? "True" : "False"; 240 } 241 242 248 public void writeCode() throws IOException { 249 file.writeCode(gen.getCodeStyle()); 250 } 251 } 252 | Popular Tags |