forked from tunnelvisionlabs/antlr4cs
-
Notifications
You must be signed in to change notification settings - Fork 10
/
DiagnosticErrorListener.cs
156 lines (147 loc) · 6.35 KB
/
DiagnosticErrorListener.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (c) Terence Parr, Sam Harwell. All Rights Reserved.
// Licensed under the BSD License. See LICENSE.txt in the project root for license information.
using Antlr4.Runtime.Atn;
using Antlr4.Runtime.Dfa;
using Antlr4.Runtime.Misc;
using Antlr4.Runtime.Sharpen;
namespace Antlr4.Runtime
{
/// <summary>
/// This implementation of
/// <see cref="IAntlrErrorListener{Symbol}"/>
/// can be used to identify
/// certain potential correctness and performance problems in grammars. "Reports"
/// are made by calling
/// <see cref="Parser.NotifyErrorListeners(string)"/>
/// with the appropriate
/// message.
/// <ul>
/// <li><b>Ambiguities</b>: These are cases where more than one path through the
/// grammar can match the input.</li>
/// <li><b>Weak context sensitivity</b>: These are cases where full-context
/// prediction resolved an SLL conflict to a unique alternative which equaled the
/// minimum alternative of the SLL conflict.</li>
/// <li><b>Strong (forced) context sensitivity</b>: These are cases where the
/// full-context prediction resolved an SLL conflict to a unique alternative,
/// <em>and</em> the minimum alternative of the SLL conflict was found to not be
/// a truly viable alternative. Two-stage parsing cannot be used for inputs where
/// this situation occurs.</li>
/// </ul>
/// </summary>
/// <author>Sam Harwell</author>
public class DiagnosticErrorListener : BaseErrorListener
{
/// <summary>
/// When
/// <see langword="true"/>
/// , only exactly known ambiguities are reported.
/// </summary>
protected internal readonly bool exactOnly;
/// <summary>
/// Initializes a new instance of
/// <see cref="DiagnosticErrorListener"/>
/// which only
/// reports exact ambiguities.
/// </summary>
public DiagnosticErrorListener()
: this(true)
{
}
/// <summary>
/// Initializes a new instance of
/// <see cref="DiagnosticErrorListener"/>
/// , specifying
/// whether all ambiguities or only exact ambiguities are reported.
/// </summary>
/// <param name="exactOnly">
///
/// <see langword="true"/>
/// to report only exact ambiguities, otherwise
/// <see langword="false"/>
/// to report all ambiguities.
/// </param>
public DiagnosticErrorListener(bool exactOnly)
{
this.exactOnly = exactOnly;
}
public override void ReportAmbiguity([NotNull] Parser recognizer, [NotNull] DFA dfa, int startIndex, int stopIndex, bool exact, [Nullable] BitSet ambigAlts, [NotNull] ATNConfigSet configs)
{
if (exactOnly && !exact)
{
return;
}
string format = "reportAmbiguity d={0}: ambigAlts={1}, input='{2}'";
string decision = GetDecisionDescription(recognizer, dfa);
BitSet conflictingAlts = GetConflictingAlts(ambigAlts, configs);
string text = ((ITokenStream)recognizer.InputStream).GetText(Interval.Of(startIndex, stopIndex));
string message = string.Format(format, decision, conflictingAlts, text);
recognizer.NotifyErrorListeners(message);
}
public override void ReportAttemptingFullContext([NotNull] Parser recognizer, [NotNull] DFA dfa, int startIndex, int stopIndex, [Nullable] BitSet conflictingAlts, [NotNull] SimulatorState conflictState)
{
string format = "reportAttemptingFullContext d={0}, input='{1}'";
string decision = GetDecisionDescription(recognizer, dfa);
string text = ((ITokenStream)recognizer.InputStream).GetText(Interval.Of(startIndex, stopIndex));
string message = string.Format(format, decision, text);
recognizer.NotifyErrorListeners(message);
}
public override void ReportContextSensitivity([NotNull] Parser recognizer, [NotNull] DFA dfa, int startIndex, int stopIndex, int prediction, [NotNull] SimulatorState acceptState)
{
string format = "reportContextSensitivity d={0}, input='{1}'";
string decision = GetDecisionDescription(recognizer, dfa);
string text = ((ITokenStream)recognizer.InputStream).GetText(Interval.Of(startIndex, stopIndex));
string message = string.Format(format, decision, text);
recognizer.NotifyErrorListeners(message);
}
protected internal virtual string GetDecisionDescription([NotNull] Parser recognizer, [NotNull] DFA dfa)
{
int decision = dfa.decision;
int ruleIndex = dfa.atnStartState.ruleIndex;
string[] ruleNames = recognizer.RuleNames;
if (ruleIndex < 0 || ruleIndex >= ruleNames.Length)
{
return decision.ToString();
}
string ruleName = ruleNames[ruleIndex];
if (string.IsNullOrEmpty(ruleName))
{
return decision.ToString();
}
return string.Format("{0} ({1})", decision, ruleName);
}
/// <summary>
/// Computes the set of conflicting or ambiguous alternatives from a
/// configuration set, if that information was not already provided by the
/// parser.
/// </summary>
/// <param name="reportedAlts">
/// The set of conflicting or ambiguous alternatives, as
/// reported by the parser.
/// </param>
/// <param name="configs">The conflicting or ambiguous configuration set.</param>
/// <returns>
/// Returns
/// <paramref name="reportedAlts"/>
/// if it is not
/// <see langword="null"/>
/// , otherwise
/// returns the set of alternatives represented in
/// <paramref name="configs"/>
/// .
/// </returns>
[return: NotNull]
protected internal virtual BitSet GetConflictingAlts([Nullable] BitSet reportedAlts, [NotNull] ATNConfigSet configs)
{
if (reportedAlts != null)
{
return reportedAlts;
}
BitSet result = new BitSet();
foreach (ATNConfig config in configs)
{
result.Set(config.Alt);
}
return result;
}
}
}