]> review.fuel-infra Code Review - packages/trusty/mysql-wsrep-5.6.git/blob
87a955e8588ec071cfb1c70e39693d7572c57b19
[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.CharStream;
21 import org.antlr.runtime.Lexer;
22 import org.antlr.runtime.NoViableAltException;
23 import org.antlr.runtime.RecognitionException;
24 import org.antlr.runtime.RecognizerSharedState;
25
26 import java.util.List;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29
30 /**
31  * Abstract superclass for MySQL Lexers, for now containing some common code, so it's not in the grammar.
32  * Author: kroepke
33  */
34 public abstract class MySQLLexer extends Lexer implements RecognizerErrorDelegate {
35
36     private static final Logger log = Logger.getLogger(MySQLLexer.class.getName());
37     
38     boolean nextTokenIsID = false;
39     private ErrorListener errorListener;
40
41
42     /**
43      * Check ahead in the input stream for a left paren to distinguish between built-in functions
44      * and identifiers.
45      * TODO: This is the place to support certain SQL modes.
46      * @param proposedType the original token type for this input.
47      * @return the new token type to emit a token with
48      */
49     public int checkFunctionAsID(int proposedType) {
50         return (input.LA(1) != '(') ? MySQL51Lexer.ID : proposedType;
51     }
52
53     public MySQLLexer() {} 
54
55     public MySQLLexer(CharStream input) {
56         this(input, new RecognizerSharedState());
57         errorListener = new BaseErrorListener(this);
58     }
59
60     public MySQLLexer(CharStream input, RecognizerSharedState state) {
61         super(input, state);
62         errorListener = new BaseErrorListener(this);
63     }
64
65     public void setErrorListener(ErrorListener listener) {
66         this.errorListener = listener;
67     }
68
69     public ErrorListener getErrorListener() {
70         return errorListener;
71     }
72
73     /** Delegate error presentation to our errorListener if that's set, otherwise pass up the chain. */
74      public void displayRecognitionError(String[] tokenNames, RecognitionException e) {
75          errorListener.displayRecognitionError(tokenNames, e);
76      }
77
78      public String getErrorHeader(RecognitionException e) {
79          return errorListener.getErrorHeader(e);
80      }
81
82      public void emitErrorMessage(String msg) {
83          errorListener.emitErrorMessage(msg);
84      }
85
86      /* generate more useful error messages for debugging */
87      @SuppressWarnings("unchecked")
88     public String getErrorMessage(RecognitionException re, String[] tokenNames) {
89         if (log.isLoggable(Level.FINEST)) {
90             List stack = getRuleInvocationStack(re, this.getClass().getName());
91             String msg;
92             if (re instanceof NoViableAltException) {
93                 NoViableAltException nvae = (NoViableAltException)re;
94                 msg = "  no viable alternative for token="+ re.token + "\n" +
95                         "  at decision="+nvae.decisionNumber + " state=" + nvae.stateNumber;
96                 if (nvae.grammarDecisionDescription != null && nvae.grammarDecisionDescription.length() > 0)
97                     msg = msg + "\n  decision grammar=<< "+ nvae.grammarDecisionDescription + " >>";
98             } else {
99                 msg = super.getErrorMessage(re, tokenNames);
100             }
101             return stack + "\n" + msg;
102         } else {
103             return errorListener.getErrorMessage(re, tokenNames);
104         }
105     }
106
107      public void reportError(RecognitionException e) {
108          errorListener.reportError(e);
109      }
110
111     /* trampoline methods to get to super implementation */
112     public void originalDisplayError(String[] tokenNames, RecognitionException e) {
113         super.displayRecognitionError(tokenNames, e);
114     }
115
116     public void originalReportError(RecognitionException e) {
117         super.reportError(e);
118     }
119
120     public String originalGetErrorHeader(RecognitionException e) {
121         return super.getErrorHeader(e);
122     }
123
124     public String originalGetErrorMessage(RecognitionException e, String[] tokenNames) {
125         return super.getErrorMessage(e, tokenNames);
126     }
127
128     public void originalEmitErrorMessage(String msg) {
129         //super.emitErrorMessage(msg);
130         log.warning(msg);
131     }
132     
133 }