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());
}
}
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);
}
}
Aggregations