1 package persistence.antlr; 2 3 8 9 import java.util.Hashtable ; 10 import java.util.Enumeration ; 11 import java.io.IOException ; 12 13 import persistence.antlr.collections.impl.BitSet; 14 import persistence.antlr.collections.impl.Vector; 15 16 20 public abstract class Grammar { 21 protected Tool antlrTool; 22 protected CodeGenerator generator; 23 protected LLkGrammarAnalyzer theLLkAnalyzer; 24 protected Hashtable symbols; 25 protected boolean buildAST = false; 26 protected boolean analyzerDebug = false; 27 protected boolean interactive = false; 28 protected String superClass = null; 29 30 35 protected TokenManager tokenManager; 36 37 40 protected String exportVocab = null; 41 42 44 protected String importVocab = null; 45 46 protected Hashtable options; 48 protected Vector rules; 50 51 protected Token preambleAction = new CommonToken(Token.INVALID_TYPE, ""); 52 protected String className = null; 53 protected String fileName = null; 54 protected Token classMemberAction = new CommonToken(Token.INVALID_TYPE, ""); 55 protected boolean hasSyntacticPredicate = false; 56 protected boolean hasUserErrorHandling = false; 57 58 protected int maxk = 1; 60 61 protected boolean traceRules = false; 63 protected boolean debuggingOutput = false; 64 protected boolean defaultErrorHandler = true; 65 66 protected String comment = null; 68 public Grammar(String className_, Tool tool_, String superClass) { 69 className = className_; 70 antlrTool = tool_; 71 symbols = new Hashtable (); 72 options = new Hashtable (); 73 rules = new Vector(100); 74 this.superClass = superClass; 75 } 76 77 78 public void define(RuleSymbol rs) { 79 rules.appendElement(rs); 80 symbols.put(rs.getId(), rs); 82 } 83 84 85 public abstract void generate() throws IOException ; 86 87 protected String getClassName() { 88 return className; 89 } 90 91 92 public boolean getDefaultErrorHandler() { 93 return defaultErrorHandler; 94 } 95 96 public String getFilename() { 97 return fileName; 98 } 99 100 106 public int getIntegerOption(String key) throws NumberFormatException { 107 Token t = (Token)options.get(key); 108 if (t == null || t.getType() != ANTLRTokenTypes.INT) { 109 throw new NumberFormatException (); 110 } 111 else { 112 return Integer.parseInt(t.getText()); 113 } 114 } 115 116 120 public Token getOption(String key) { 121 return (Token)options.get(key); 122 } 123 124 protected abstract String getSuperClass(); 126 127 public GrammarSymbol getSymbol(String s) { 128 return (GrammarSymbol)symbols.get(s); 129 } 130 131 public Enumeration getSymbols() { 132 return symbols.elements(); 133 } 134 135 139 public boolean hasOption(String key) { 140 return options.containsKey(key); 141 } 142 143 144 public boolean isDefined(String s) { 145 return symbols.containsKey(s); 146 } 147 148 149 public abstract void processArguments(String [] args); 150 151 public void setCodeGenerator(CodeGenerator gen) { 152 generator = gen; 153 } 154 155 public void setFilename(String s) { 156 fileName = s; 157 } 158 159 public void setGrammarAnalyzer(LLkGrammarAnalyzer a) { 160 theLLkAnalyzer = a; 161 } 162 163 173 public boolean setOption(String key, Token value) { 174 options.put(key, value); 175 String s = value.getText(); 176 int i; 177 if (key.equals("k")) { 178 try { 179 maxk = getIntegerOption("k"); 180 if ( maxk<=0 ) { 181 antlrTool.error("option 'k' must be greater than 0 (was " + 182 value.getText() + ")", 183 getFilename(), 184 value.getLine(), 185 value.getColumn()); 186 maxk = 1; 187 } 188 } 189 catch (NumberFormatException e) { 190 antlrTool.error("option 'k' must be an integer (was " + value.getText() + ")", getFilename(), value.getLine(), value.getColumn()); 191 } 192 return true; 193 } 194 if (key.equals("codeGenMakeSwitchThreshold")) { 195 try { 196 i = getIntegerOption("codeGenMakeSwitchThreshold"); 197 } 198 catch (NumberFormatException e) { 199 antlrTool.error("option 'codeGenMakeSwitchThreshold' must be an integer", getFilename(), value.getLine(), value.getColumn()); 200 } 201 return true; 202 } 203 if (key.equals("codeGenBitsetTestThreshold")) { 204 try { 205 i = getIntegerOption("codeGenBitsetTestThreshold"); 206 } 207 catch (NumberFormatException e) { 208 antlrTool.error("option 'codeGenBitsetTestThreshold' must be an integer", getFilename(), value.getLine(), value.getColumn()); 209 } 210 return true; 211 } 212 if (key.equals("defaultErrorHandler")) { 213 if (s.equals("true")) { 214 defaultErrorHandler = true; 215 } 216 else if (s.equals("false")) { 217 defaultErrorHandler = false; 218 } 219 else { 220 antlrTool.error("Value for defaultErrorHandler must be true or false", getFilename(), value.getLine(), value.getColumn()); 221 } 222 return true; 223 } 224 if (key.equals("analyzerDebug")) { 225 if (s.equals("true")) { 226 analyzerDebug = true; 227 } 228 else if (s.equals("false")) { 229 analyzerDebug = false; 230 } 231 else { 232 antlrTool.error("option 'analyzerDebug' must be true or false", getFilename(), value.getLine(), value.getColumn()); 233 } 234 return true; 235 } 236 if (key.equals("codeGenDebug")) { 237 if (s.equals("true")) { 238 analyzerDebug = true; 239 } 240 else if (s.equals("false")) { 241 analyzerDebug = false; 242 } 243 else { 244 antlrTool.error("option 'codeGenDebug' must be true or false", getFilename(), value.getLine(), value.getColumn()); 245 } 246 return true; 247 } 248 if (key.equals("classHeaderSuffix")) { 249 return true; 250 } 251 if (key.equals("classHeaderPrefix")) { 252 return true; 253 } 254 if (key.equals("namespaceAntlr")) { 255 return true; 256 } 257 if (key.equals("namespaceStd")) { 258 return true; 259 } 260 if (key.equals("genHashLines")) { 261 return true; 262 } 263 if (key.equals("noConstructors")) { 264 return true; 265 } 266 return false; 267 } 268 269 public void setTokenManager(TokenManager tokenManager_) { 270 tokenManager = tokenManager_; 271 } 272 273 274 public String toString() { 275 StringBuffer buf = new StringBuffer (20000); 276 Enumeration ids = rules.elements(); 277 while (ids.hasMoreElements()) { 278 RuleSymbol rs = (RuleSymbol)ids.nextElement(); 279 if (!rs.id.equals("mnextToken")) { 280 buf.append(rs.getBlock().toString()); 281 buf.append("\n\n"); 282 } 283 } 284 return buf.toString(); 285 } 286 287 } 288 | Popular Tags |