Search in sources :

Example 1 with RawAqlPreParser

use of annis.ql.RawAqlPreParser 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)

Aggregations

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