1 from .ClingoLexer
import ClingoLexer
2 from .ClingoParser
import ClingoParser
3 from .ClingoParserVisitor
import ClingoParserVisitor
4 from antlr4
import PredictionMode
5 from antlr4.CommonTokenStream
import CommonTokenStream
6 from antlr4.error.ErrorListener
import ConsoleErrorListener
7 from antlr4.error.Errors
import RecognitionException
8 from antlr4.error.ErrorStrategy
import BailErrorStrategy, DefaultErrorStrategy
9 from antlr4.InputStream
import InputStream
13 def __init__(self, answerSets):
16 def visitAnswer_set(self, ctx):
19 return self.visitChildren(ctx)
21 def visitModel(self, ctx):
22 cost = ctx.NEW_LINE().getText().strip()
25 tokens = cost.split(
' ')
26 levels = len(tokens) - 1
28 for index
in range(1, len(tokens)):
32 return self.visitChildren(ctx)
34 def visitPredicate_atom(self, ctx):
38 def parse(answerSets, clingoOutput, two_stageParsing):
39 tokens = CommonTokenStream(
ClingoLexer(InputStream(clingoOutput)))
43 if not two_stageParsing:
44 visitor.visit(parser.output())
48 parser._interp.predictionMode = PredictionMode.SLL
49 parser.removeErrorListeners()
50 parser._errHandler = BailErrorStrategy()
53 visitor.visit(parser.output())
54 except RuntimeError
as exception:
55 if isinstance(exception, RecognitionException):
57 parser.addErrorListener(ConsoleErrorListener.INSTANCE)
58 parser._errHandler = DefaultErrorStrategy()
59 parser._interp.predictionMode = PredictionMode.LL
60 visitor.visit(parser.output())