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.parser.ProductionPattern; 29 import net.percederberg.grammatica.parser.TokenPattern; 30 31 38 public class CSharpParserGenerator extends ParserGenerator { 39 40 43 private String baseName = null; 44 45 48 private String namespace = null; 49 50 53 private boolean publicAccess = false; 54 55 60 public CSharpParserGenerator(Grammar grammar) { 61 super(grammar); 62 initialize(); 63 } 64 65 68 private void initialize() { 69 String str; 70 71 str = getGrammar().getFileName(); 73 if (str.indexOf('/') >= 0) { 74 str = str.substring(str.lastIndexOf('/') + 1); 75 } 76 if (str.indexOf('\\') >= 0) { 77 str = str.substring(str.lastIndexOf('\\') + 1); 78 } 79 if (str.indexOf('.') > 0) { 80 str = str.substring(0, str.indexOf('.')); 81 } 82 if (Character.isLowerCase(str.charAt(0))) { 83 str = Character.toUpperCase(str.charAt(0)) + str.substring(1); 84 } 85 baseName = str; 86 } 87 88 94 public String getNamespace() { 95 return namespace; 96 } 97 98 103 public void setNamespace(String namespace) { 104 this.namespace = namespace; 105 } 106 107 112 public String getBaseName() { 113 return baseName; 114 } 115 116 121 public void setBaseName(String name) { 122 this.baseName = name; 123 } 124 125 131 public boolean getPublicAccess() { 132 return publicAccess; 133 } 134 135 140 public void setPublicAccess(boolean flag) { 141 publicAccess = flag; 142 } 143 144 149 public CodeStyle getCodeStyle() { 150 return CodeStyle.CSHARP; 151 } 152 153 158 public void write() throws IOException { 159 Grammar grammar = getGrammar(); 160 CSharpConstantsFile constants = new CSharpConstantsFile(this); 161 CSharpTokenizerFile tokenizer = new CSharpTokenizerFile(this); 162 CSharpParserFile parser = new CSharpParserFile(this, tokenizer); 163 CSharpAnalyzerFile analyzer = new CSharpAnalyzerFile(this); 164 TokenPattern token; 165 ProductionPattern production; 166 int i; 167 168 for (i = 0; i < grammar.getTokenPatternCount(); i++) { 170 token = grammar.getTokenPattern(i); 171 constants.addToken(token); 172 tokenizer.addToken(token, constants); 173 analyzer.addToken(token, constants); 174 } 175 176 for (i = 0; i < grammar.getProductionPatternCount(); i++) { 178 production = grammar.getProductionPattern(i); 179 constants.addProduction(production); 180 parser.addProductionConstant(production); 181 analyzer.addProduction(production, constants); 182 } 183 184 for (i = 0; i < grammar.getProductionPatternCount(); i++) { 186 production = grammar.getProductionPattern(i); 187 parser.addProduction(production, constants); 188 } 189 190 constants.writeCode(); 192 tokenizer.writeCode(); 193 parser.writeCode(); 194 analyzer.writeCode(); 195 } 196 } 197 | Popular Tags |