EmbASP-CSharp
ASPParser.cs
1 using System;
2 using System.Collections.Generic;
3 using Antlr4.Runtime;
4 using Antlr4.Runtime.Atn;
5 
7 {
8  public class ASPParser : ASPGrammarBaseVisitor<object>
9  {
10  private readonly List<string> parameters = new List<string>();
11 
12  private ASPParser()
13  {
14 
15  }
16 
17  public static ASPParser Parse(string atom)
18  {
19  CommonTokenStream tokens = new CommonTokenStream(new ASPGrammarLexer(CharStreams.fromstring(atom)));
20  ASPGrammarParser parser = new ASPGrammarParser(tokens);
21  ASPParser visitor = new ASPParser();
22  parser.Interpreter.PredictionMode = PredictionMode.SLL;
23 
24  parser.RemoveErrorListeners();
25 
26  parser.ErrorHandler = new BailErrorStrategy();
27 
28  try
29  {
30  visitor.Visit(parser.output());
31  }
32  catch (SystemException exception)
33  {
34  if (exception.GetBaseException() is RecognitionException)
35  {
36  tokens.Seek(0);
37  parser.AddErrorListener(ConsoleErrorListener<object>.Instance);
38 
39  parser.ErrorHandler = new DefaultErrorStrategy();
40  parser.Interpreter.PredictionMode = PredictionMode.LL;
41 
42  visitor.Visit(parser.output());
43  }
44  }
45 
46  return visitor;
47  }
48 
49  public string[] GetParameters()
50  {
51  return parameters.ToArray();
52  }
53 
54  public override object VisitTerm(ASPGrammarParser.TermContext context)
55  {
56  parameters.Add(context.GetText());
57 
58  return null;
59  }
60  }
61 }
it.unical.mat.parsers.asp.ASPParser.VisitTerm
override object VisitTerm(ASPGrammarParser.TermContext context)
Visit a parse tree produced by ASPGrammarParser.term.
Definition: ASPParser.cs:54
it.unical.mat.parsers.asp
Definition: ASPParser.cs:6
ASPGrammarBaseVisitor
This class provides an empty implementation of IASPGrammarVisitor<Result>, which can be extended to c...
Definition: ASPGrammarBaseVisitor.cs:35
it.unical.mat.parsers.asp.ASPParser
Definition: ASPParser.cs:8