use of org.antlr.runtime.tree.TreeVisitor in project cuba by cuba-platform.
the class QueryParserAstBased method getAllEntityNames.
@Override
public Set<String> getAllEntityNames() {
TreeVisitor visitor = new TreeVisitor();
EntitiesFinder finder = new EntitiesFinder();
visitor.visit(getQueryAnalyzer().getTree(), finder);
return finder.resolveEntityNames(model, getQueryAnalyzer().getRootQueryVariableContext());
}
use of org.antlr.runtime.tree.TreeVisitor in project cuba by cuba-platform.
the class QueryParserAstBased method getParamNames.
@Override
public Set<String> getParamNames() {
TreeVisitor visitor = new TreeVisitor();
ParameterCounter parameterCounter = new ParameterCounter(true);
visitor.visit(getQueryAnalyzer().getTree(), parameterCounter);
return parameterCounter.getParameterNames();
}
use of org.antlr.runtime.tree.TreeVisitor in project cuba by cuba-platform.
the class QueryAnalyzerTest method mixinJoinOnIntoTree.
@Test
public void mixinJoinOnIntoTree() throws RecognitionException {
DomainModel model = prepareDomainModel();
QueryTreeTransformer qa = new QueryTreeTransformer();
qa.prepare(model, "select c from Car c");
CommonTree tree = qa.getTree();
CommonTree sources = (CommonTree) tree.getFirstChildWithType(JPA2Lexer.T_SOURCES);
assertEquals(1, sources.getChildCount());
assertTrue(sources.getChild(0) instanceof SelectionSourceNode);
CommonTree source = (CommonTree) sources.getFirstChildWithType(JPA2Lexer.T_SOURCE);
assertTrue(source.getChild(0) instanceof IdentificationVariableNode);
JoinVariableNode join = Parser.parseJoinClause("join Driver d on d.car.id = c.id").get(0);
qa.mixinJoinIntoTree(join, new VariableEntityReference("Car", "c"), true);
tree = qa.getTree();
sources = (CommonTree) tree.getFirstChildWithType(JPA2Lexer.T_SOURCES);
assertEquals(1, sources.getChildCount());
SelectionSourceNode sourceNode = (SelectionSourceNode) sources.getChild(0);
assertEquals(2, sourceNode.getChildCount());
assertTrue(sourceNode.getChild(0) instanceof IdentificationVariableNode);
assertTrue(sourceNode.getChild(1) instanceof JoinVariableNode);
JoinVariableNode joinNode = (JoinVariableNode) sourceNode.getChild(1);
TreeVisitor visitor = new TreeVisitor();
TreeToQuery treeToQuery = new TreeToQuery();
visitor.visit(joinNode, treeToQuery);
assertEquals("d", joinNode.getVariableName());
assertEquals("join Driver d on d.car.id = c.id", treeToQuery.getQueryString().trim());
}
use of org.antlr.runtime.tree.TreeVisitor in project antlr4 by tunnelvisionlabs.
the class GrammarTransformPipeline method setGrammarPtr.
/**
* Utility visitor that sets grammar ptr in each node
*/
public static void setGrammarPtr(final Grammar g, GrammarAST tree) {
if (tree == null)
return;
// ensure each node has pointer to surrounding grammar
TreeVisitor v = new TreeVisitor(new GrammarASTAdaptor());
v.visit(tree, new TreeVisitorAction() {
@Override
public Object pre(Object t) {
((GrammarAST) t).g = g;
return t;
}
@Override
public Object post(Object t) {
return t;
}
});
}
use of org.antlr.runtime.tree.TreeVisitor in project flink by apache.
the class HiveParserBaseSemanticAnalyzer method validateNoHavingReferenceToAlias.
// We support having referring alias just as in hive's semantic analyzer. This check only prints
// a warning now.
public static void validateNoHavingReferenceToAlias(HiveParserQB qb, HiveParserASTNode havingExpr, HiveParserRowResolver inputRR, HiveParserSemanticAnalyzer semanticAnalyzer) throws SemanticException {
HiveParserQBParseInfo qbPI = qb.getParseInfo();
Map<HiveParserASTNode, String> exprToAlias = qbPI.getAllExprToColumnAlias();
for (Map.Entry<HiveParserASTNode, String> exprAndAlias : exprToAlias.entrySet()) {
final HiveParserASTNode expr = exprAndAlias.getKey();
final String alias = exprAndAlias.getValue();
// put the alias in input RR so that we can generate ExprNodeDesc with it
if (inputRR.getExpression(expr) != null) {
inputRR.put("", alias, inputRR.getExpression(expr));
}
final Set<Object> aliasReferences = new HashSet<>();
TreeVisitorAction action = new TreeVisitorAction() {
@Override
public Object pre(Object t) {
if (HiveASTParseDriver.ADAPTOR.getType(t) == HiveASTParser.TOK_TABLE_OR_COL) {
Object c = HiveASTParseDriver.ADAPTOR.getChild(t, 0);
if (c != null && HiveASTParseDriver.ADAPTOR.getType(c) == HiveASTParser.Identifier && HiveASTParseDriver.ADAPTOR.getText(c).equals(alias)) {
aliasReferences.add(t);
}
}
return t;
}
@Override
public Object post(Object t) {
return t;
}
};
new TreeVisitor(HiveASTParseDriver.ADAPTOR).visit(havingExpr, action);
if (aliasReferences.size() > 0) {
String havingClause = semanticAnalyzer.ctx.getTokenRewriteStream().toString(havingExpr.getTokenStartIndex(), havingExpr.getTokenStopIndex());
String msg = String.format("Encountered Select alias '%s' in having clause '%s'" + " This is non standard behavior.", alias, havingClause);
LOG.warn(msg);
}
}
}
Aggregations