Skip to content

Commit

Permalink
Fix test case after antlr upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
yozhao committed Mar 11, 2014
1 parent b9e2b84 commit aeeaf53
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 236 deletions.
50 changes: 0 additions & 50 deletions sensei-core/src/main/antlr4/com/senseidb/bql/parsers/BQL.g4
Expand Up @@ -438,12 +438,6 @@ tokens
}

@lexer::members {
// @Override
// public void reportError(RecognitionException e) {
// throw new IllegalArgumentException(e);
// }
}

// As the generated parser will reside in com.senseidb.bql.parsers
Expand Down Expand Up @@ -474,50 +468,6 @@ import com.senseidb.search.req.BQLParserUtils;
IDENT,
STRING_LITERAL_AND_IDENT
}

//@Override
//public String getErrorMessage(RecognitionException err, String[] tokenNames)
//{
// List stack = getRuleInvocationStack(err, this.getClass().getName());
// String msg = null;
// if (err instanceof NoViableAltException) {
// NoViableAltException nvae = (NoViableAltException) err;
// // msg = "No viable alt; token=" + err.token.getText() +
// // " (decision=" + nvae.decisionNumber +
// // " state "+nvae.stateNumber+")" +
// // " decision=<<" + nvae.grammarDecisionDescription + ">>";
// msg = "[line:" + err.line + ", col:" + err.charPositionInLine + "] " +
// "No viable alternative (token=" + err.token.getText() + ")" + " (stack=" + stack + ")";
// }
// else if (err instanceof MismatchedTokenException) {
// MismatchedTokenException mte = (MismatchedTokenException) err;
// String tokenName = (mte.expecting == Token.EOF) ? "EOF" : tokenNames[mte.expecting];
// msg = "[line:" + mte.line + ", col:" + mte.charPositionInLine + "] " +
// "Expecting " + tokenName +
// " (token=" + err.token.getText() + ")";
// }
// else if (err instanceof FailedPredicateException) {
// FailedPredicateException fpe = (FailedPredicateException) err;
// msg = "[line:" + fpe.line + ", col:" + fpe.charPositionInLine + "] " +
// fpe.predicateText +
// " (token=" + fpe.token.getText() + ")";
// }
// else if (err instanceof MismatchedSetException) {
// MismatchedSetException mse = (MismatchedSetException) err;
// msg = "[line:" + mse.line + ", col:" + mse.charPositionInLine + "] " +
// "Mismatched input (token=" + mse.token.getText() + ")";
// }
// else {
// msg = super.getErrorMessage(err, tokenNames);
// }
// return msg;
//}

//@Override
//public String getTokenErrorDisplay(Token t)
//{
// return t.toString();
//}
}

// ***************** parser rules:
Expand Down
@@ -1,6 +1,5 @@
package com.senseidb.bql.parsers;

import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.tree.ParseTree;
import org.json.JSONObject;

Expand All @@ -10,9 +9,9 @@ public AbstractCompiler() {
super();
}

public abstract JSONObject compile(String expression) throws RecognitionException;
public abstract JSONObject compile(String expression);

public abstract String getErrorMessage(RecognitionException error);
public abstract String getErrorMessage(IllegalStateException ex);

protected void printTree(ParseTree ast) {
print(ast, 0);
Expand Down
85 changes: 71 additions & 14 deletions sensei-core/src/main/java/com/senseidb/bql/parsers/BQLCompiler.java
Expand Up @@ -2,13 +2,17 @@

import java.util.HashMap;
import java.util.Map;

import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.BailErrorStrategy;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.FailedPredicateException;
import org.antlr.v4.runtime.InputMismatchException;
import org.antlr.v4.runtime.NoViableAltException;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.misc.ParseCancellationException;
import org.antlr.v4.runtime.tree.ParseTreeWalker;

import org.json.JSONObject;

public class BQLCompiler extends AbstractCompiler {
Expand All @@ -21,36 +25,89 @@ public BQLCompiler(Map<String, String[]> facetInfoMap) {
}

@Override
public JSONObject compile(String bqlStmt) throws RecognitionException {
public JSONObject compile(String bqlStmt) {
// Lexer splits input into tokens
ANTLRInputStream input = new ANTLRInputStream(bqlStmt);
TokenStream tokens = new CommonTokenStream(new BQLLexer(input));

// Parser generates abstract syntax tree
BQLParser parser = new BQLParser(tokens);
_parser.set(parser);

parser.removeErrorListeners();
parser.addErrorListener(BQLErrorListener.INSTANCE);

parser.setErrorHandler(new BailErrorStrategy());
BQLParser.StatementContext ret = parser.statement();

BQLCompilerAnalyzer analyzer = new BQLCompilerAnalyzer(parser, _facetInfoMap);
ParseTreeWalker.DEFAULT.walk(analyzer, ret);
JSONObject json = (JSONObject)analyzer.getJsonProperty(ret);
JSONObject json = (JSONObject) analyzer.getJsonProperty(ret);

// XXX To be removed
// printTree(ast);
// System.out.println(">>> json = " + json.toString());
return json;
}

@Override
public String getErrorMessage(RecognitionException error) {
BQLParser parser = _parser.get();
if (parser != null) {
// TODO: get v4 error message
return "TODO";
} else {
return null;
public String getErrorMessage(IllegalStateException ex) {
String errMsg = null;
if (ex instanceof ParseCancellationException) {
BQLParser parser = _parser.get();
Throwable err = ex.getCause();
if (err instanceof NoViableAltException) {
NoViableAltException nvae = (NoViableAltException) err;
int line = nvae.getOffendingToken().getLine();
TokenStream tokens = parser.getInputStream();
String input;
if (tokens != null) {
if (nvae.getStartToken().getType() == Token.EOF) {
input = "<EOF>";
} else {
input = tokens.getText(nvae.getStartToken(), nvae.getOffendingToken());
}
} else {
input = "<unknown input>";
}
errMsg = "[line " + line + "] no viable alternative at input " + escapeWSAndQuote(input);
} else if (err instanceof InputMismatchException) {
InputMismatchException ime = (InputMismatchException) err;
int line = ime.getOffendingToken().getLine();
errMsg = "[line " + line + "] mismatched input "
+ getTokenErrorDisplay(ime.getOffendingToken()) + " expecting "
+ ime.getExpectedTokens().toString(parser.getTokenNames());
} else if (err instanceof FailedPredicateException) {
FailedPredicateException fpe = (FailedPredicateException) err;
int line = fpe.getOffendingToken().getLine();
String ruleName = parser.getRuleNames()[parser.getContext().getRuleIndex()];
errMsg = "[line " + line + "] rule " + ruleName + " " + err.getMessage();
}
}
if (errMsg == null) {
errMsg = ex.getMessage();
}
if (errMsg == null) {
errMsg = "Unknown parsing error.";
}
return errMsg;
}

private String getTokenErrorDisplay(Token t) {
if (t == null) return "<no token>";
String s = t.getText();
if (s == null) {
if (t.getType() == Token.EOF) {
s = "<EOF>";
} else {
s = "<" + t.getType() + ">";
}
}
return escapeWSAndQuote(s);
}

private String escapeWSAndQuote(String s) {
s = s.replace("\n", "\\n");
s = s.replace("\r", "\\r");
s = s.replace("\t", "\\t");
return "'" + s + "'";
}

public void setFacetInfoMap(Map<String, String[]> facetInfoMap) {
Expand Down

0 comments on commit aeeaf53

Please sign in to comment.