1 21 22 package net.percederberg.grammatica.output; 23 24 import java.io.IOException ; 25 26 import net.percederberg.grammatica.code.csharp.CSharpClass; 27 import net.percederberg.grammatica.code.csharp.CSharpComment; 28 import net.percederberg.grammatica.code.csharp.CSharpConstructor; 29 import net.percederberg.grammatica.code.csharp.CSharpFile; 30 import net.percederberg.grammatica.code.csharp.CSharpNamespace; 31 import net.percederberg.grammatica.code.csharp.CSharpMethod; 32 import net.percederberg.grammatica.code.csharp.CSharpUsing; 33 import net.percederberg.grammatica.parser.TokenPattern; 34 35 42 class CSharpTokenizerFile { 43 44 47 private static final String TYPE_COMMENT = 48 "<remarks>A character stream tokenizer.</remarks>"; 49 50 53 private static final String CONSTRUCTOR_COMMENT = 54 "<summary>Creates a new tokenizer for the specified input\n" + 55 "stream.</summary>\n\n" + 56 "<param name='input'>the input stream to read</param>\n\n" + 57 "<exception cref='ParserCreationException'>if the tokenizer\n" + 58 "couldn't be initialized correctly</exception>"; 59 60 63 private static final String INIT_METHOD_COMMENT = 64 "<summary>Initializes the tokenizer by creating all the token\n" + 65 "patterns.</summary>\n\n" + 66 "<exception cref='ParserCreationException'>if the tokenizer\n" + 67 "couldn't be initialized correctly</exception>"; 68 69 72 private CSharpParserGenerator gen; 73 74 77 private CSharpFile file; 78 79 82 private CSharpClass cls; 83 84 87 private CSharpMethod initMethod; 88 89 94 public CSharpTokenizerFile(CSharpParserGenerator gen) { 95 String name = gen.getBaseName() + "Tokenizer"; 96 int modifiers; 97 98 this.gen = gen; 99 this.file = new CSharpFile(gen.getBaseDir(), name); 100 if (gen.getPublicAccess()) { 101 modifiers = CSharpClass.PUBLIC; 102 } else { 103 modifiers = CSharpClass.INTERNAL; 104 } 105 this.cls = new CSharpClass(modifiers, name, "Tokenizer"); 106 this.initMethod = new CSharpMethod(CSharpMethod.PRIVATE, 107 "CreatePatterns", 108 "", 109 "void"); 110 initializeCode(); 111 } 112 113 116 private void initializeCode() { 117 CSharpConstructor constr; 118 String str; 119 120 file.addUsing(new CSharpUsing("System.IO")); 122 file.addUsing(new CSharpUsing("PerCederberg.Grammatica.Runtime")); 123 124 if (gen.getNamespace() == null) { 126 file.addClass(cls); 127 } else { 128 CSharpNamespace n = new CSharpNamespace(gen.getNamespace()); 129 n.addClass(cls); 130 file.addNamespace(n); 131 } 132 133 str = file.toString() + "\n\n" + gen.getFileComment(); 135 file.addComment(new CSharpComment(CSharpComment.BLOCK, str)); 136 137 cls.addComment(new CSharpComment(TYPE_COMMENT)); 139 140 constr = new CSharpConstructor("TextReader input"); 142 cls.addConstructor(constr); 143 constr.addComment(new CSharpComment(CONSTRUCTOR_COMMENT)); 144 constr.addInitializer("base(input, " + 145 !gen.getGrammar().getCaseSensitive() + 146 ")"); 147 constr.addCode("CreatePatterns();"); 148 149 cls.addMethod(initMethod); 151 initMethod.addComment(new CSharpComment(INIT_METHOD_COMMENT)); 152 initMethod.addCode("TokenPattern pattern;"); 153 } 154 155 161 public void addToken(TokenPattern pattern, 162 CSharpConstantsFile constants) { 163 164 StringBuffer code = new StringBuffer (); 165 String str; 166 167 code.append("pattern = new TokenPattern((int) "); 169 code.append(constants.getConstant(pattern.getId())); 170 code.append(",\n"); 171 code.append(" \""); 172 code.append(pattern.getName()); 173 code.append("\",\n"); 174 code.append(" TokenPattern.PatternType."); 175 switch (pattern.getType()) { 176 case TokenPattern.STRING_TYPE: 177 code.append("STRING"); 178 break; 179 case TokenPattern.REGEXP_TYPE: 180 code.append("REGEXP"); 181 break; 182 } 183 code.append(",\n"); 184 code.append(" "); 185 str = pattern.getPattern(); 186 code.append(gen.getCodeStyle().getStringConstant(str, '\\')); 187 code.append(");\n"); 188 189 if (pattern.isError()) { 191 if (pattern.getErrorMessage() == null) { 192 code.append("pattern.Error = true"); 193 } else { 194 code.append("pattern.ErrorMessage = "); 195 str = pattern.getErrorMessage(); 196 code.append(gen.getCodeStyle().getStringConstant(str, '\\')); 197 } 198 code.append(";\n"); 199 } 200 if (pattern.isIgnore()) { 201 if (pattern.getIgnoreMessage() == null) { 202 code.append("pattern.Ignore = true"); 203 } else { 204 code.append("pattern.IgnoreMessage = "); 205 str = pattern.getIgnoreMessage(); 206 code.append(gen.getCodeStyle().getStringConstant(str, '\\')); 207 } 208 code.append(";\n"); 209 } 210 211 code.append("AddPattern(pattern);"); 213 initMethod.addCode(""); 214 initMethod.addCode(code.toString()); 215 } 216 217 225 protected String getConstructorCall(String param) { 226 return "new " + gen.getBaseName() + "Tokenizer(" + param + ")"; 227 } 228 229 235 public void writeCode() throws IOException { 236 file.writeCode(gen.getCodeStyle()); 237 } 238 } 239 | Popular Tags |