Search in sources :

Example 51 with ParseTree

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();
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) GraqlErrorListener(ai.grakn.graql.internal.parser.GraqlErrorListener) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 52 with ParseTree

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;
}
Also used : ASTBuilderVisitor(org.kie.dmn.feel.parser.feel11.ASTBuilderVisitor) BaseNode(org.kie.dmn.feel.lang.ast.BaseNode) FEEL_1_1Parser(org.kie.dmn.feel.parser.feel11.FEEL_1_1Parser) ParseTree(org.antlr.v4.runtime.tree.ParseTree) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression)

Example 53 with ParseTree

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);
    }
}
Also used : XpathParserException(cn.wanghaomiao.xpath.exception.XpathParserException) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 54 with ParseTree

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;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) XpathLexer(cn.wanghaomiao.xpath.antlr.XpathLexer) Element(org.jsoup.nodes.Element) XValue(cn.wanghaomiao.xpath.core.XValue) LinkedList(java.util.LinkedList) CharStream(org.antlr.v4.runtime.CharStream) DoFailOnErrorHandler(cn.wanghaomiao.xpath.exception.DoFailOnErrorHandler) XpathSyntaxErrorException(cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException) XpathParser(cn.wanghaomiao.xpath.antlr.XpathParser) XpathSyntaxErrorException(cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException) XpathProcessor(cn.wanghaomiao.xpath.core.XpathProcessor) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 55 with ParseTree

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;
}
Also used : BqsLexer(org.codice.alliance.nsili.common.grammar.BqsLexer) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) BqsParser(org.codice.alliance.nsili.common.grammar.BqsParser) Filter(org.opengis.filter.Filter) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Aggregations

ParseTree (org.antlr.v4.runtime.tree.ParseTree)311 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)104 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)60 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)49 AddedParseTree (org.eclipse.titan.common.parsers.AddedParseTree)46 CharStream (org.antlr.v4.runtime.CharStream)43 Test (org.junit.Test)43 CommonToken (org.antlr.v4.runtime.CommonToken)35 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)35 JavadocContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.JavadocContext)31 TextContext (com.puppycrawl.tools.checkstyle.grammars.javadoc.JavadocParser.TextContext)29 File (java.io.File)26 ArrayList (java.util.ArrayList)22 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)22 CancellationException (java.util.concurrent.CancellationException)20 ConsoleErrorListener (org.antlr.v4.runtime.ConsoleErrorListener)20 Grammar (org.antlr.v4.tool.Grammar)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 IOException (java.io.IOException)19 LexerGrammar (org.antlr.v4.tool.LexerGrammar)16