use of org.antlr.v4.runtime.tree.Tree in project aic-expresso by aic-sri-international.
the class AntlrGrinderParserWrapper method parse.
@Override
public Expression parse(String string, Parser.ErrorListener parserEerrorListener) {
Expression result = null;
try {
AntlrErrorListener antlrErrorListener = new AntlrErrorListener(parserEerrorListener);
ANTLRInputStream input = new ANTLRInputStream(string);
AntlrGrinderLexer lexer = new AntlrGrinderLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
AntlrGrinderParser parser = new AntlrGrinderParser(tokens);
lexer.removeErrorListeners();
parser.removeErrorListeners();
lexer.addErrorListener(antlrErrorListener);
parser.addErrorListener(antlrErrorListener);
ParseTree tree = parser.expression();
boolean eof = parser.getInputStream().LA(1) == Recognizer.EOF;
if (!antlrErrorListener.errorsDetected) {
if (!eof) {
System.err.println("Unable to parse the complete input expression: " + input);
} else {
lexer.removeErrorListeners();
parser.removeErrorListeners();
ExpressionVisitor expressionVisitor = new ExpressionVisitor(getRandomPredicatesSignatures());
result = expressionVisitor.visit(tree);
}
}
} catch (RecognitionException re) {
re.printStackTrace();
} catch (RuntimeException re) {
re.printStackTrace();
}
return result;
}
use of org.antlr.v4.runtime.tree.Tree in project presto by prestodb.
the class SqlParser method invokeParser.
private Node invokeParser(String name, String sql, Function<SqlBaseParser, ParserRuleContext> parseFunction) {
try {
SqlBaseLexer lexer = new SqlBaseLexer(new CaseInsensitiveStream(new ANTLRInputStream(sql)));
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
SqlBaseParser parser = new SqlBaseParser(tokenStream);
parser.addParseListener(new PostProcessor());
lexer.removeErrorListeners();
lexer.addErrorListener(ERROR_LISTENER);
parser.removeErrorListeners();
parser.addErrorListener(ERROR_LISTENER);
ParserRuleContext tree;
try {
// first, try parsing with potentially faster SLL mode
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
tree = parseFunction.apply(parser);
} catch (ParseCancellationException ex) {
// if we fail, parse with LL mode
// rewind input stream
tokenStream.reset();
parser.reset();
parser.getInterpreter().setPredictionMode(PredictionMode.LL);
tree = parseFunction.apply(parser);
}
return new AstBuilder().visit(tree);
} catch (StackOverflowError e) {
throw new ParsingException(name + " is too large (stack overflow while parsing)");
}
}
use of org.antlr.v4.runtime.tree.Tree in project presto by prestodb.
the class TypeCalculation method calculateLiteralValue.
public static Long calculateLiteralValue(String calculation, Map<String, Long> inputs) {
try {
ParserRuleContext tree = parseTypeCalculation(calculation);
CalculateTypeVisitor visitor = new CalculateTypeVisitor(inputs);
BigInteger result = visitor.visit(tree);
return result.longValueExact();
} catch (StackOverflowError e) {
throw new ParsingException("Type calculation is too large (stack overflow while parsing)");
}
}
use of org.antlr.v4.runtime.tree.Tree in project SSM by Intel-bigdata.
the class TestSmartRuleParser method parseAndExecuteRule.
private void parseAndExecuteRule(String rule) throws Exception {
System.out.println("--> " + rule);
InputStream input = new ByteArrayInputStream(rule.getBytes());
ANTLRInputStream antlrInput = new ANTLRInputStream(input);
SmartRuleLexer lexer = new SmartRuleLexer(antlrInput);
CommonTokenStream tokens = new CommonTokenStream(lexer);
SmartRuleParser parser = new SmartRuleParser(tokens);
parser.removeErrorListeners();
parser.addErrorListener(new SSMRuleErrorListener());
ParseTree tree = parser.ssmrule();
System.out.println("Parser tree: " + tree.toStringTree(parser));
System.out.println("Total number of errors: " + parseErrors.size());
SmartRuleVisitTranslator visitor = new SmartRuleVisitTranslator();
visitor.visit(tree);
System.out.println("\nQuery:");
TranslateResult result = visitor.generateSql();
int index = 1;
for (String sql : result.getSqlStatements()) {
System.out.println("" + index + ". " + sql);
index++;
}
if (parseErrors.size() > 0) {
throw new IOException("Error while parse rule");
}
ExecutionContext ctx = new ExecutionContext();
ctx.setProperty(ExecutionContext.RULE_ID, 2016);
DBAdapter dbAdapter = new DBAdapter(TestDBUtil.getTestDBInstance());
RuleQueryExecutor qe = new RuleQueryExecutor(null, ctx, result, dbAdapter);
List<String> paths = qe.executeFileRuleQuery();
index = 1;
System.out.println("\nFiles:");
for (String path : paths) {
System.out.println("" + index + ". " + path);
index++;
}
}
use of org.antlr.v4.runtime.tree.Tree in project oxTrust by GluuFederation.
the class ScimFilterParserService method visitTree.
private String visitTree(String filter, Class clazz) throws Exception {
log.info(" visitTree() ");
ParseTree parseTree = getParser(filter).scimFilter();
// Visit tree
MainScimFilterVisitor visitor = VisitorFactory.getVisitorInstance(clazz);
String result = visitor.visit(parseTree);
return result;
}
Aggregations