2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 package com.mysql.clusterj.jdbc.antlr;
20 import org.antlr.runtime.IntStream;
21 import org.antlr.runtime.NoViableAltException;
22 import org.antlr.runtime.Parser;
23 import org.antlr.runtime.RecognizerSharedState;
24 import org.antlr.runtime.RecognitionException;
25 import org.antlr.runtime.Token;
26 import org.antlr.runtime.TokenStream;
28 import java.util.List;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
35 public abstract class MySQLParser extends Parser implements RecognizerErrorDelegate {
37 private static final Logger log = Logger.getLogger(MySQLParser.class.getName());
39 private ErrorListener errorListener;
41 public String getTokenErrorDisplay(Token t) {
45 public MySQLParser(TokenStream input) {
47 errorListener = new BaseErrorListener(this);
50 public MySQLParser(TokenStream input, RecognizerSharedState state) {
52 errorListener = new BaseErrorListener(this);
55 public void setErrorListener(ErrorListener listener) {
56 this.errorListener = listener;
59 public ErrorListener getErrorListener() {
63 /** Delegate error presentation to our errorListener if that's set, otherwise pass up the chain. */
64 public void displayRecognitionError(String[] tokenNames, RecognitionException e) {
65 errorListener.displayRecognitionError(tokenNames, e);
68 public String getErrorHeader(RecognitionException e) {
69 return errorListener.getErrorHeader(e);
72 public void emitErrorMessage(String msg) {
73 errorListener.emitErrorMessage(msg);
76 /* generate more useful error messages for debugging */
77 @SuppressWarnings("unchecked")
78 public String getErrorMessage(RecognitionException re, String[] tokenNames) {
79 if (log.isLoggable(Level.FINEST)) {
80 List stack = getRuleInvocationStack(re, this.getClass().getName());
82 if (re instanceof NoViableAltException) {
83 NoViableAltException nvae = (NoViableAltException)re;
84 msg = " no viable alternative for token="+ re.token + "\n" +
85 " at decision="+nvae.decisionNumber + " state=" + nvae.stateNumber;
86 if (nvae.grammarDecisionDescription != null && nvae.grammarDecisionDescription.length() > 0)
87 msg = msg + "\n decision grammar=<< "+ nvae.grammarDecisionDescription + " >>";
89 msg = super.getErrorMessage(re, tokenNames);
91 return stack + "\n" + msg;
93 return errorListener.getErrorMessage(re, tokenNames);
98 public void reportError(RecognitionException e) {
99 errorListener.reportError(e);
102 protected Object getCurrentInputSymbol(IntStream input) {
103 return super.getCurrentInputSymbol(input); //To change body of overridden methods use File | Settings | File Templates.
106 /* trampoline methods to get to super implementation */
107 public void originalDisplayError(String[] tokenNames, RecognitionException e) {
108 super.displayRecognitionError(tokenNames, e);
111 public void originalReportError(RecognitionException e) {
112 super.reportError(e);
115 public String originalGetErrorHeader(RecognitionException e) {
116 return super.getErrorHeader(e);
119 public String originalGetErrorMessage(RecognitionException e, String[] tokenNames) {
120 return super.getErrorMessage(e, tokenNames);
123 public void originalEmitErrorMessage(String msg) {
124 // super.emitErrorMessage(msg);