EmbASP-Python
dlvhex_parser_visitor_implementation.py
1 from .DLVHEXLexer import DLVHEXLexer
2 from .DLVHEXParser import DLVHEXParser
3 from .DLVHEXParserVisitor import DLVHEXParserVisitor
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
10 
11 
13  def __init__(self, answerSets):
14  self._answerSets = answerSets
15 
16  def visitModel(self, ctx):
17  self._answerSets.add_answer_set()
18 
19  return self.visitChildren(ctx)
20 
21  def visitLevel(self, ctx):
22  self._answerSets.store_cost(
23  ctx.INTEGER(1).getText(),
24  ctx.INTEGER(0).getText())
25 
26  def visitPredicate_atom(self, ctx):
27  self._answerSets.store_atom(ctx.getText())
28 
29  def visitWitness(self, ctx):
30  self._answerSets.add_answer_set()
31 
32  return self.visitChildren(ctx)
33 
34  @staticmethod
35  def parse(answerSets, dlvhexOutput, two_stageParsing):
36  tokens = CommonTokenStream(DLVHEXLexer(InputStream(dlvhexOutput)))
37  parser = DLVHEXParser(tokens)
38  visitor = DLVHEXParserVisitorImplementation(answerSets)
39 
40  if not two_stageParsing:
41  visitor.visit(parser.output())
42 
43  return
44 
45  parser._interp.predictionMode = PredictionMode.SLL
46  parser.removeErrorListeners()
47  parser._errHandler = BailErrorStrategy()
48 
49  try:
50  visitor.visit(parser.output())
51  except RuntimeError as exception:
52  if isinstance(exception, RecognitionException):
53  tokens.seek(0)
54  parser.addErrorListener(ConsoleErrorListener.INSTANCE)
55  parser._errHandler = DefaultErrorStrategy()
56  parser._interp.predictionMode = PredictionMode.LL
57  visitor.visit(parser.output())
parsers.asp.dlvhex.DLVHEXParser.DLVHEXParser
Definition: DLVHEXParser.py:45
parsers.asp.dlvhex.DLVHEXParserVisitor.DLVHEXParserVisitor
Definition: DLVHEXParserVisitor.py:6
parsers.asp.dlvhex.DLVHEXLexer.DLVHEXLexer
Definition: DLVHEXLexer.py:86
parsers.asp.dlvhex.dlvhex_parser_visitor_implementation.DLVHEXParserVisitorImplementation
Definition: dlvhex_parser_visitor_implementation.py:12
parsers.asp.dlvhex.dlvhex_parser_visitor_implementation.DLVHEXParserVisitorImplementation._answerSets
_answerSets
Definition: dlvhex_parser_visitor_implementation.py:14