1 21 package oracle.toplink.essentials.internal.parsing.ejbql; 23 24 import java.util.Vector ; 25 26 import oracle.toplink.essentials.internal.parsing.*; 27 import oracle.toplink.essentials.exceptions.*; 28 29 import persistence.antlr.LLkParser; 31 import persistence.antlr.ANTLRException; 32 import persistence.antlr.MismatchedTokenException; 33 import persistence.antlr.MismatchedCharException; 34 import persistence.antlr.NoViableAltException; 35 import persistence.antlr.NoViableAltForCharException; 36 import persistence.antlr.TokenStreamRecognitionException; 37 import persistence.antlr.RecognitionException; 38 import persistence.antlr.ParserSharedInputState; 39 import persistence.antlr.TokenStream; 40 import persistence.antlr.Token; 41 42 55 public class EJBQLParserBase extends LLkParser { 56 private boolean verbose = false; 57 58 private Vector errors; 62 private String theEjbql = null; 63 64 65 protected static final int EOF_CHAR = 65535; 67 68 protected NodeFactory factory; 69 70 protected EJBQLParserBase(persistence.antlr.TokenBuffer tokenBuf, int k_) { 71 super(tokenBuf, k_); 72 initialize(); 73 } 74 75 public EJBQLParserBase(ParserSharedInputState state, int k_) { 76 super(state, k_); 77 } 78 79 protected EJBQLParserBase(TokenStream lexer, int k) { 80 super(lexer, k); 81 } 82 83 84 public void setNodeFactory(NodeFactory factory) { 85 this.factory = factory; 86 } 87 88 89 public NodeFactory getNodeFactory() { 90 return factory; 91 } 92 93 94 public ParseTree getParseTree() { 95 return (ParseTree)getRootNode(); 96 } 97 98 99 public Object getRootNode() { 100 return null; 101 } 102 103 107 public void addError(EJBQLException e) { 108 getErrors().add(e); 109 } 110 111 public void addError(Exception e) { 112 addError(EJBQLException.generalParsingException(getEjbqlString(), e)); 113 } 114 115 119 public static EJBQLParser buildParserFor(String ejbqlString) { 120 return EJBQLParser.buildParserFor(ejbqlString); 121 } 122 123 127 public static ParseTree buildParseTree(String ejbqlString) throws Exception { 128 EJBQLParser parser = buildParserFor(ejbqlString); 130 try { 131 parser.document(); 132 } catch (Exception e) { 133 parser.addError(e); 134 } 135 if (parser.hasErrors()) { 136 throw parser.generateException(); 137 } 138 139 return parser.getParseTree(); 141 } 142 143 148 public Exception generateException() { 149 Exception firstException = (Exception )getErrors().elementAt(0); 151 if (firstException instanceof EJBQLException) { 152 return firstException; 153 } 154 155 EJBQLException exception = EJBQLException.generalParsingException(getEjbqlString()); 157 exception.setInternalExceptions(getErrors()); 158 return exception; 159 } 160 161 165 private Vector getErrors() { 166 return errors; 167 } 168 169 173 public boolean hasErrors() { 174 return !getErrors().isEmpty(); 175 } 176 177 public void initialize() { 178 setErrors(new Vector ()); 179 setNodeFactory(new NodeFactoryImpl()); 180 } 181 182 187 public boolean isVerbose() { 188 return verbose; 189 } 190 191 194 public void output(String output) { 195 if (isVerbose()) { 196 System.out.println(output); 197 } 198 } 199 200 203 public void reportError(RecognitionException ex) { 204 EJBQLException error = handleANTLRException(ex); 205 addError(error); 206 } 207 208 209 protected EJBQLException handleANTLRException(ANTLRException ex) { 210 EJBQLException result = null; 211 if (ex instanceof MismatchedCharException) { 212 MismatchedCharException mismatched = (MismatchedCharException)ex; 213 if (mismatched.mismatchType == MismatchedCharException.CHAR) { 214 if (mismatched.foundChar == EOF_CHAR) { 215 result = EJBQLException.unexpectedEOF(getEjbqlString()); 216 } 217 else { 218 result = EJBQLException.expectedCharFound( 219 getEjbqlString(), 220 String.valueOf((char)mismatched.expecting), 221 String.valueOf((char)mismatched.foundChar)); 222 } 223 } 224 } 225 else if (ex instanceof MismatchedTokenException) { 226 MismatchedTokenException mismatched = (MismatchedTokenException)ex; 227 Token token = mismatched.token; 228 if ((mismatched.mismatchType == MismatchedTokenException.TOKEN) && 229 (token != null)) { 230 if (token.getType() == Token.EOF_TYPE) { 231 result = EJBQLException.unexpectedEOF(getEjbqlString()); 232 } 233 else { 234 result = EJBQLException.syntaxErrorAt( 235 getEjbqlString(), token.getText()); 236 } 237 } 238 } 239 else if (ex instanceof NoViableAltException) { 240 Token token = ((NoViableAltException)ex).token; 241 if (token != null) { 242 if (token.getType() == Token.EOF_TYPE) { 243 result = EJBQLException.unexpectedEOF(getEjbqlString()); 244 } 245 else { 246 result = EJBQLException.unexpectedToken( 247 getEjbqlString(), token.getText()); 248 } 249 } 250 } 251 else if (ex instanceof NoViableAltForCharException) { 252 NoViableAltForCharException noViableAlt = (NoViableAltForCharException)ex; 253 result = EJBQLException.unexpectedChar( 254 getEjbqlString(), String.valueOf((char)noViableAlt.foundChar)); 255 } 256 else if (ex instanceof TokenStreamRecognitionException) { 257 result = handleANTLRException(((TokenStreamRecognitionException)ex).recog); 258 } else { 259 result = EJBQLException.syntaxError(getEjbqlString()); 262 } 263 return result; 264 } 265 266 270 private void setErrors(Vector newErrors) { 271 errors = newErrors; 272 } 273 274 278 public void setVerbose(boolean newVerbose) { 279 verbose = newVerbose; 280 } 281 282 286 public short getDistinctState() { 287 return getParseTree().getDistinctState(); 288 } 289 290 294 public String getEjbqlString() { 295 return theEjbql; 296 } 297 298 302 public void setEjbqlString(String theEjbql) { 303 this.theEjbql = theEjbql; 304 } 305 306 311 public static EJBQLParser parseEJBQLString(String ejbqlString) throws QueryException { 312 EJBQLParser parser = buildParserFor(ejbqlString); 314 try { 315 parser.document(); 316 } catch (EJBQLException e) { 317 parser.addError(e); 318 } catch (ANTLRException e) { 319 parser.addError(parser.handleANTLRException(e)); 320 } catch (Exception e) { 321 parser.addError(e); 322 } 323 324 if (parser.hasErrors()) { 326 throw (EJBQLException)parser.generateException(); 327 } 328 return parser; 329 } 330 } 331 | Popular Tags |