1 package it.unical.mat.parsers.asp.clingo;
3 import it.unical.mat.parsers.asp.ASPDataCollection;
4 import org.antlr.v4.runtime.BailErrorStrategy;
5 import org.antlr.v4.runtime.CharStreams;
6 import org.antlr.v4.runtime.CommonTokenStream;
7 import org.antlr.v4.runtime.ConsoleErrorListener;
8 import org.antlr.v4.runtime.DefaultErrorStrategy;
9 import org.antlr.v4.runtime.RecognitionException;
10 import org.antlr.v4.runtime.atn.PredictionMode;
16 this.answerSets = answerSets;
21 answerSets.addAnswerSet();
23 return visitChildren(ctx);
28 final String cost = ctx.NEW_LINE().getText().trim();
30 if(cost.length() > 1) {
31 final String[] tokens = cost.split(
" ");
32 int levels = tokens.length - 1;
34 for(
int i = 1; i < tokens.length; i++)
35 answerSets.storeCost(levels--, Integer.parseInt(tokens[i]));
38 return visitChildren(ctx);
43 answerSets.storeAtom(ctx.getText());
48 public static void parse(
final ASPDataCollection answerSets,
final String atomsList,
final boolean two_stageParsing) {
49 final CommonTokenStream tokens =
new CommonTokenStream(
new ClingoLexer(CharStreams.fromString(atomsList)));
53 if(!two_stageParsing) {
54 visitor.visit(parser.output());
59 parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
60 parser.removeErrorListeners();
61 parser.setErrorHandler(
new BailErrorStrategy());
64 visitor.visit(parser.output());
65 }
catch (
final RuntimeException exception) {
66 if(exception.getClass() == RuntimeException.class && exception.getCause() instanceof RecognitionException) {
68 parser.addErrorListener(ConsoleErrorListener.INSTANCE);
69 parser.setErrorHandler(
new DefaultErrorStrategy());
70 parser.getInterpreter().setPredictionMode(PredictionMode.LL);
71 visitor.visit(parser.output());