EmbASP-CSharp
DLV2ParserBaseVisitorImplementation.cs
1 using System;
2 using System.Collections.Generic;
3 using Antlr4.Runtime;
4 using Antlr4.Runtime.Atn;
5 
7 {
9  {
10  private readonly IASPDataCollection answerSets;
11  private Dictionary<int, int> costs;
12 
14  {
15  this.answerSets = answerSets;
16  }
17 
18  public override object VisitAnswer_set(DLV2Parser.Answer_setContext context)
19  {
20  answerSets.AddAnswerSet();
21 
22  if (context.cost() != null && !context.cost().IsEmpty)
23  {
24  costs = new Dictionary<int, int>();
25  string[] FirstCost = context.cost().COST_LABEL().GetText().Split(' ')[1].Split('@');
26 
27  costs[Int32.Parse(FirstCost[1])] = Int32.Parse(FirstCost[0]);
28  }
29 
30  if (costs != null)
31  foreach (KeyValuePair<int, int> entry in costs)
32  answerSets.StoreCost(entry.Key, entry.Value);
33 
34  return VisitChildren(context);
35  }
36 
37  public override object VisitLevel(DLV2Parser.LevelContext context)
38  {
39  int level = Int32.Parse(context.INTEGER()[1].GetText()), cost = Int32.Parse(context.INTEGER()[0].GetText());
40 
41  costs[level] = cost;
42  answerSets.StoreCost(level, cost);
43 
44  return null;
45  }
46 
47  public override object VisitPredicate_atom(DLV2Parser.Predicate_atomContext context)
48  {
49  answerSets.StoreAtom(context.GetText());
50 
51  return null;
52  }
53 
54  public static void Parse(IASPDataCollection answerSets, string atomsList, bool two_stageParsing)
55  {
56  CommonTokenStream tokens = new CommonTokenStream(new DLV2Lexer(CharStreams.fromstring(atomsList)));
57  DLV2Parser parser = new DLV2Parser(tokens);
59 
60  if (!two_stageParsing)
61  {
62  visitor.Visit(parser.output());
63 
64  return;
65  }
66 
67  parser.Interpreter.PredictionMode = PredictionMode.SLL;
68 
69  parser.RemoveErrorListeners();
70 
71  parser.ErrorHandler = new BailErrorStrategy();
72 
73  try
74  {
75  visitor.Visit(parser.output());
76  }
77  catch (SystemException exception)
78  {
79  if (exception.GetBaseException() is RecognitionException)
80  {
81  tokens.Seek(0);
82  parser.AddErrorListener(ConsoleErrorListener<object>.Instance);
83 
84  parser.ErrorHandler = new DefaultErrorStrategy();
85  parser.Interpreter.PredictionMode = PredictionMode.LL;
86 
87  visitor.Visit(parser.output());
88  }
89  }
90  }
91  }
92 }
it.unical.mat.parsers.asp.dlv2.DLV2ParserBaseVisitorImplementation.VisitLevel
override object VisitLevel(DLV2Parser.LevelContext context)
Visit a parse tree produced by DLV2Parser.level.
Definition: DLV2ParserBaseVisitorImplementation.cs:37
DLV2ParserBaseVisitor
This class provides an empty implementation of IDLV2ParserVisitor<Result>, which can be extended to c...
Definition: DLV2ParserBaseVisitor.cs:35
it.unical.mat.parsers.asp.dlv2.DLV2ParserBaseVisitorImplementation.VisitPredicate_atom
override object VisitPredicate_atom(DLV2Parser.Predicate_atomContext context)
Visit a parse tree produced by DLV2Parser.predicate_atom.
Definition: DLV2ParserBaseVisitorImplementation.cs:47
it.unical.mat.parsers.asp.dlv2
Definition: DLV2ParserBaseVisitorImplementation.cs:6
it.unical.mat.parsers.asp.dlv2.DLV2ParserBaseVisitorImplementation.VisitAnswer_set
override object VisitAnswer_set(DLV2Parser.Answer_setContext context)
Visit a parse tree produced by DLV2Parser.answer_set.
Definition: DLV2ParserBaseVisitorImplementation.cs:18
it.unical.mat.parsers.asp.IASPDataCollection
Definition: IASPDataCollection.cs:3
it.unical.mat.parsers.asp.dlv2.DLV2ParserBaseVisitorImplementation
Definition: DLV2ParserBaseVisitorImplementation.cs:8