use of org.antlr.runtime.tree.TreeVisitorAction in project antlr4 by antlr.
the class GrammarTransformPipeline method expandParameterizedLoops.
/**
* Find and replace
* ID*[','] with ID (',' ID)*
* ID+[','] with ID (',' ID)+
* (x {action} y)+[','] with x {action} y (',' x {action} y)+
*
* Parameter must be a token.
* todo: do we want?
*/
public void expandParameterizedLoops(GrammarAST root) {
TreeVisitor v = new TreeVisitor(new GrammarASTAdaptor());
v.visit(root, new TreeVisitorAction() {
@Override
public Object pre(Object t) {
if (((GrammarAST) t).getType() == 3) {
return expandParameterizedLoop((GrammarAST) t);
}
return t;
}
@Override
public Object post(Object t) {
return t;
}
});
}
use of org.antlr.runtime.tree.TreeVisitorAction 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.TreeVisitorAction 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