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.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;
26 import java.util.List;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
31 * Abstract superclass for MySQL Lexers, for now containing some common code, so it's not in the grammar.
34 public abstract class MySQLLexer extends Lexer implements RecognizerErrorDelegate {
36 private static final Logger log = Logger.getLogger(MySQLLexer.class.getName());
38 boolean nextTokenIsID = false;
39 private ErrorListener errorListener;
43 * Check ahead in the input stream for a left paren to distinguish between built-in functions
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
49 public int checkFunctionAsID(int proposedType) {
50 return (input.LA(1) != '(') ? MySQL51Lexer.ID : proposedType;
53 public MySQLLexer() {}
55 public MySQLLexer(CharStream input) {
56 this(input, new RecognizerSharedState());
57 errorListener = new BaseErrorListener(this);
60 public MySQLLexer(CharStream input, RecognizerSharedState state) {
62 errorListener = new BaseErrorListener(this);
65 public void setErrorListener(ErrorListener listener) {
66 this.errorListener = listener;
69 public ErrorListener getErrorListener() {
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);
78 public String getErrorHeader(RecognitionException e) {
79 return errorListener.getErrorHeader(e);
82 public void emitErrorMessage(String msg) {
83 errorListener.emitErrorMessage(msg);
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());
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 + " >>";
99 msg = super.getErrorMessage(re, tokenNames);
101 return stack + "\n" + msg;
103 return errorListener.getErrorMessage(re, tokenNames);
107 public void reportError(RecognitionException e) {
108 errorListener.reportError(e);
111 /* trampoline methods to get to super implementation */
112 public void originalDisplayError(String[] tokenNames, RecognitionException e) {
113 super.displayRecognitionError(tokenNames, e);
116 public void originalReportError(RecognitionException e) {
117 super.reportError(e);
120 public String originalGetErrorHeader(RecognitionException e) {
121 return super.getErrorHeader(e);
124 public String originalGetErrorMessage(RecognitionException e, String[] tokenNames) {
125 return super.getErrorMessage(e, tokenNames);
128 public void originalEmitErrorMessage(String msg) {
129 //super.emitErrorMessage(msg);