use of org.antlr.v4.runtime.tree.ParseTree in project grakn by graknlabs.
the class TemplateParser method parseTemplate.
/**
* Parse and resolve a graql template.
* @param templateString a string representing a graql template
* @param data data to use in template
* @return resolved graql query string
*/
public String parseTemplate(String templateString, Map<String, Object> data) {
GraqlErrorListener errorListener = GraqlErrorListener.of(templateString);
CommonTokenStream tokens = lexGraqlTemplate(templateString, errorListener);
ParseTree tree = parseGraqlTemplate(tokens, errorListener);
TemplateVisitor visitor = new TemplateVisitor(tokens, data, macros);
return visitor.visit(tree).toString();
}
use of org.antlr.v4.runtime.tree.ParseTree in project drools by kiegroup.
the class FEELImpl method compileExpressionList.
public CompiledExpression compileExpressionList(String expression, CompilerContext ctx) {
FEEL_1_1Parser parser = FEELParser.parse(getEventsManager(ctx.getListeners()), expression, ctx.getInputVariableTypes(), ctx.getInputVariables(), ctx.getFEELFunctions(), profiles);
ParseTree tree = parser.expressionList();
ASTBuilderVisitor v = new ASTBuilderVisitor(ctx.getInputVariableTypes());
BaseNode expr = v.visit(tree);
CompiledExpression ce = new CompiledExpressionImpl(expr);
return ce;
}
use of org.antlr.v4.runtime.tree.ParseTree in project JsoupXpath by zhegexiaohuozi.
the class XpathProcessor method visitAdditiveExpr.
@Override
public XValue visitAdditiveExpr(XpathParser.AdditiveExprContext ctx) {
List<XpathParser.MultiplicativeExprContext> multiplicativeExprContexts = ctx.multiplicativeExpr();
if (multiplicativeExprContexts.size() == 1) {
return visit(multiplicativeExprContexts.get(0));
} else {
Double res = visit(multiplicativeExprContexts.get(0)).asDouble();
String op = null;
for (int i = 1; i < ctx.getChildCount(); i++) {
ParseTree chiCtx = ctx.getChild(i);
if (chiCtx instanceof XpathParser.MultiplicativeExprContext) {
XValue next = visit(chiCtx);
if ("+".equals(op)) {
res += next.asDouble();
} else if ("-".equals(op)) {
res -= next.asDouble();
} else {
throw new XpathParserException("syntax error, " + ctx.getText());
}
} else {
op = chiCtx.getText();
}
}
return XValue.create(res);
}
}
use of org.antlr.v4.runtime.tree.ParseTree in project JsoupXpath by zhegexiaohuozi.
the class JXDocument method selN.
public List<JXNode> selN(String xpath) throws XpathSyntaxErrorException {
List<JXNode> finalRes = new LinkedList<>();
try {
CharStream input = CharStreams.fromString(xpath);
XpathLexer lexer = new XpathLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
XpathParser parser = new XpathParser(tokens);
parser.setErrorHandler(new DoFailOnErrorHandler());
ParseTree tree = parser.main();
XpathProcessor processor = new XpathProcessor(elements);
XValue calRes = processor.visit(tree);
if (calRes.isElements()) {
for (Element el : calRes.asElements()) {
finalRes.add(JXNode.e(el));
}
} else if (calRes.isList()) {
for (String str : calRes.asList()) {
finalRes.add(JXNode.t(str));
}
}
} catch (Exception e) {
String msg = "Please check the syntax of your xpath expr, ";
throw new XpathSyntaxErrorException(msg + ExceptionUtils.getRootCauseMessage(e), e);
}
return finalRes;
}
use of org.antlr.v4.runtime.tree.ParseTree in project alliance by codice.
the class BqsConverter method convertBQSToDDF.
public Filter convertBQSToDDF(String query) {
query = query.trim();
LOGGER.debug("Original Query: {}", query);
ANTLRInputStream inputStream = new ANTLRInputStream(query);
// transforms characters into tokens
BqsLexer lex = new BqsLexer(inputStream);
// a token stream
CommonTokenStream tokens = new CommonTokenStream(lex);
// transforms tokens into parse trees
BqsParser parser = new BqsParser(tokens);
BqsTreeWalkerListener bqsListener = new BqsTreeWalkerListener(filterBuilder);
ParseTree tree = parser.query();
ParseTreeWalker.DEFAULT.walk(bqsListener, tree);
Filter filter = bqsListener.getFilter();
if (filter != null && StringUtils.isNotBlank(filter.toString())) {
LOGGER.debug("Parsed Query: {}", filter);
} else {
filter = filterBuilder.attribute(Metacard.ANY_TEXT).is().text("*");
LOGGER.debug("After parsing filter, didn't have any query parameters. Defaulting to everything search: {}", filter);
}
return filter;
}
Aggregations