Search in sources :

Example 1 with AqlLexer

use of annis.ql.AqlLexer in project ANNIS by korpling.

the class AnnisParserAntlr method parse.

public QueryData parse(String aql, List<Long> corpusList) {
    final List<AqlParseError> errors = new LinkedList<>();
    AqlLexer lexerNonDNF = new AqlLexer(new ANTLRInputStream(aql));
    lexerNonDNF.removeErrorListeners();
    lexerNonDNF.addErrorListener(new AqlLexerErrorListener(errors));
    // bring first into DNF
    RawAqlPreParser rawParser = new RawAqlPreParser(new CommonTokenStream(lexerNonDNF));
    rawParser.removeErrorListeners();
    rawParser.addErrorListener(new AqlParseErrorListener(errors));
    RawAqlPreParser.StartContext treeRaw = rawParser.start();
    if (!errors.isEmpty()) {
        throw new AnnisQLSyntaxException(Joiner.on("\n").join(errors), errors);
    }
    // treeRaw.inspect(rawParser);
    ParseTreeWalker walkerRaw = new ParseTreeWalker();
    RawAqlListener listenerRaw = new RawAqlListener();
    walkerRaw.walk(listenerRaw, treeRaw);
    LogicClause topNode = listenerRaw.getRoot();
    DNFTransformer.toDNF(topNode);
    // use the DNF form and parse it again
    TokenSource source = new ListTokenSource(topNode.getCoveredToken());
    AqlParser parserDNF = new AqlParser(new CommonTokenStream(source));
    parserDNF.removeErrorListeners();
    parserDNF.addErrorListener(new AqlParseErrorListener(errors));
    AqlParser.StartContext treeDNF = parserDNF.start();
    if (!errors.isEmpty()) {
        throw new AnnisQLSyntaxException(Joiner.on("\n").join(errors), errors);
    }
    ParseTreeWalker walker = new ParseTreeWalker();
    NodeIDListener idListener = new NodeIDListener();
    walker.walk(idListener, treeDNF);
    QueryNodeListener nodeListener = new QueryNodeListener(idListener.getNodeIntervalToID());
    try {
        walker.walk(nodeListener, treeDNF);
        QueryData data = nodeListener.getQueryData();
        data.setCorpusList(corpusList);
        data.addMetaAnnotations(nodeListener.getMetaData());
        JoinListener joinListener = new JoinListener(data, precedenceBound, nodeListener.getTokenPositions());
        walker.walk(joinListener, treeDNF);
        if (postProcessors != null) {
            for (QueryDataTransformer transformer : postProcessors) {
                data = transformer.transform(data);
            }
        }
        return data;
    } catch (NullPointerException ex) {
        log.warn("Null pointer exception occured during parsing", ex);
        throw new AnnisQLSemanticsException(ex.getMessage());
    } catch (IllegalArgumentException ex) {
        throw new AnnisQLSemanticsException(ex.getMessage());
    }
}
Also used : AqlParseError(annis.model.AqlParseError) AnnisQLSyntaxException(annis.exceptions.AnnisQLSyntaxException) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) TokenSource(org.antlr.v4.runtime.TokenSource) AqlLexer(annis.ql.AqlLexer) AnnisQLSemanticsException(annis.exceptions.AnnisQLSemanticsException) AqlParser(annis.ql.AqlParser) LinkedList(java.util.LinkedList) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) RawAqlPreParser(annis.ql.RawAqlPreParser)

Example 2 with AqlLexer

use of annis.ql.AqlLexer in project ANNIS by korpling.

the class AnnisParserAntlr method dumpTree.

public String dumpTree(String aql) {
    AqlLexer lexer = new AqlLexer(new ANTLRInputStream(aql));
    AqlParser parser = new AqlParser(new CommonTokenStream(lexer));
    final List<AqlParseError> errors = new LinkedList<>();
    parser.removeErrorListeners();
    parser.addErrorListener(new AqlParseErrorListener(errors));
    ParseTree tree = parser.start();
    if (errors.isEmpty()) {
        return tree.toStringTree();
    } else {
        throw new AnnisQLSyntaxException(Joiner.on("\n").join(errors), errors);
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) AqlLexer(annis.ql.AqlLexer) AqlParseError(annis.model.AqlParseError) AqlParser(annis.ql.AqlParser) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) LinkedList(java.util.LinkedList) ParseTree(org.antlr.v4.runtime.tree.ParseTree) AnnisQLSyntaxException(annis.exceptions.AnnisQLSyntaxException)

Aggregations

AnnisQLSyntaxException (annis.exceptions.AnnisQLSyntaxException)2 AqlParseError (annis.model.AqlParseError)2 AqlLexer (annis.ql.AqlLexer)2 AqlParser (annis.ql.AqlParser)2 LinkedList (java.util.LinkedList)2 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)2 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)2 AnnisQLSemanticsException (annis.exceptions.AnnisQLSemanticsException)1 RawAqlPreParser (annis.ql.RawAqlPreParser)1 TokenSource (org.antlr.v4.runtime.TokenSource)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)1