/*
* Licensed to CRATE Technology GmbH ("Crate") under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership. Crate licenses
* this file to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* However, if you have executed another commercial license agreement
* with Crate these terms will supersede the license and you may use the
* software solely pursuant to the terms of the relevant commercial agreement.
*/
package io.crate.sql.parser;
import io.crate.sql.tree.Expression;
import io.crate.sql.tree.Statement;
import com.google.common.annotations.VisibleForTesting;
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.TokenStream;
import org.antlr.runtime.tree.BufferedTreeNodeStream;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.TreeNodeStream;
public final class SqlParser
{
private SqlParser() {}
public static Statement createStatement(String sql)
{
try {
return createStatement(parseStatement(sql));
}
catch (StackOverflowError e) {
throw new ParsingException("statement is too large (stack overflow while parsing)");
}
}
public static Expression createExpression(String expression)
{
try {
return createExpression(parseExpression(expression));
}
catch (StackOverflowError e) {
throw new ParsingException("expression is too large (stack overflow while parsing)");
}
}
@VisibleForTesting
static Statement createStatement(CommonTree tree)
{
TreeNodeStream stream = new BufferedTreeNodeStream(tree);
StatementBuilder builder = new StatementBuilder(stream);
try {
return builder.statement().value;
}
catch (RecognitionException e) {
throw new AssertionError(e); // RecognitionException is not thrown
}
}
private static Expression createExpression(CommonTree tree)
{
TreeNodeStream stream = new BufferedTreeNodeStream(tree);
StatementBuilder builder = new StatementBuilder(stream);
try {
return builder.singleExpression().value;
}
catch (RecognitionException e) {
throw new AssertionError(e); // RecognitionException is not thrown
}
}
@VisibleForTesting
static CommonTree parseStatement(String sql)
{
try {
return (CommonTree) getParser(sql).singleStatement().getTree();
}
catch (RecognitionException e) {
throw new AssertionError(e); // RecognitionException is not thrown
}
}
private static CommonTree parseExpression(String expression)
{
try {
return (CommonTree) getParser(expression).singleExpression().getTree();
}
catch (RecognitionException e) {
throw new AssertionError(e); // RecognitionException is not thrown
}
}
private static StatementParser getParser(String sql)
{
CharStream stream = new CaseInsensitiveStream(new ANTLRStringStream(sql));
StatementLexer lexer = new StatementLexer(stream);
TokenStream tokenStream = new CommonTokenStream(lexer);
return new StatementParser(tokenStream);
}
}