]> review.fuel-infra Code Review - packages/trusty/mysql-wsrep-5.6.git/blob
17a47d1d6910e9b64de8a2ea1adb74800dc6d9fa
[packages/trusty/mysql-wsrep-5.6.git] /
1 /*
2  *  Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
3  *
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.
7  *
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.
12  *
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
16  */
17
18 package com.mysql.clusterj.jdbc.antlr;
19
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;
27
28 import java.util.List;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31
32 /**
33  * Author: kroepke
34  */
35 public abstract class MySQLParser extends Parser implements RecognizerErrorDelegate {
36
37     private static final Logger log = Logger.getLogger(MySQLParser.class.getName());
38     
39     private ErrorListener errorListener;
40
41     public String getTokenErrorDisplay(Token t) {
42         return t.toString();
43     }
44
45     public MySQLParser(TokenStream input) {
46         super(input);
47         errorListener = new BaseErrorListener(this);
48     }
49
50     public MySQLParser(TokenStream input, RecognizerSharedState state) {
51         super(input, state);
52         errorListener = new BaseErrorListener(this);
53     }
54
55     public void setErrorListener(ErrorListener listener) {
56         this.errorListener = listener;
57     }
58
59     public ErrorListener getErrorListener() {
60         return errorListener;
61     }
62
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);
66     }
67
68     public String getErrorHeader(RecognitionException e) {
69         return errorListener.getErrorHeader(e);
70     }
71
72     public void emitErrorMessage(String msg) {
73         errorListener.emitErrorMessage(msg);
74     }
75
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());
81             String msg;
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 + " >>";
88             } else {
89                 msg = super.getErrorMessage(re, tokenNames);
90             }
91             return stack + "\n" + msg;
92         } else {
93             return errorListener.getErrorMessage(re, tokenNames);
94
95         }
96    }
97
98     public void reportError(RecognitionException e) {
99         errorListener.reportError(e);
100     }
101
102     protected Object getCurrentInputSymbol(IntStream input) {
103         return super.getCurrentInputSymbol(input);    //To change body of overridden methods use File | Settings | File Templates.
104     }
105
106     /* trampoline methods to get to super implementation */
107     public void originalDisplayError(String[] tokenNames, RecognitionException e) {
108         super.displayRecognitionError(tokenNames, e);
109     }
110
111     public void originalReportError(RecognitionException e) {
112         super.reportError(e);
113     }
114
115     public String originalGetErrorHeader(RecognitionException e) {
116         return super.getErrorHeader(e);
117     }
118
119     public String originalGetErrorMessage(RecognitionException e, String[] tokenNames) {
120         return super.getErrorMessage(e, tokenNames);
121     }
122
123     public void originalEmitErrorMessage(String msg) {
124         // super.emitErrorMessage(msg);
125         log.warning(msg);
126     }
127 }