Skip to content

Commit 6ac66f5

Browse files
committed
long overdue fix - report completely unrecognised tokens when parsing as failures, instead of just ignoring them.
1 parent bef9939 commit 6ac66f5

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/SCFirstOrderLogic/SentenceCreation/(ParserInternals)/AntlrFacade.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,25 +149,25 @@ public bool TryParseDeclarationList(
149149

150150
private static FirstOrderLogicParser MakeParser(
151151
AntlrInputStream inputStream,
152-
IAntlrErrorListener<IToken> errorListener)
152+
SyntaxErrorListener errorListener)
153153
{
154154
// NB: ANTLR apparently adds a listener by default that writes to the console.
155155
// Which is crazy default behaviour if you ask me, but never mind.
156156
// We remove it so that consumers of this lib don't get random messages turning up on their console.
157+
// We add our own listener instead, that keeps track of errors so that we can check at the end.
157158
FirstOrderLogicLexer lexer = new(inputStream);
158159
lexer.RemoveErrorListeners();
160+
lexer.AddErrorListener(errorListener);
159161
CommonTokenStream tokens = new(lexer);
160162

161-
// NB: In the parser, we add our own error listener that throws an exception.
162-
// Otherwise errors would just be ignored and the method would just return null, which is obviously bad behaviour.
163+
// ..and same for the parser..
163164
FirstOrderLogicParser parser = new(tokens);
164165
parser.RemoveErrorListeners();
165166
parser.AddErrorListener(errorListener);
166-
167167
return parser;
168168
}
169169

170-
private bool HasNoErrors(
170+
private static bool HasNoErrors(
171171
SyntaxErrorListener syntaxErrorListener,
172172
[MaybeNullWhen(true)] out SyntaxError[] errors)
173173
{

src/SCFirstOrderLogic/SentenceCreation/(ParserInternals)/SyntaxErrorListener.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace SCFirstOrderLogic.SentenceCreation;
77

8-
internal class SyntaxErrorListener : BaseErrorListener
8+
internal class SyntaxErrorListener : IAntlrErrorListener<IToken>, IAntlrErrorListener<int>
99
{
1010
private readonly List<SyntaxError> errors = new();
1111

@@ -16,8 +16,13 @@ public SyntaxErrorListener()
1616

1717
public IReadOnlyCollection<SyntaxError> Errors { get; }
1818

19-
public override void SyntaxError(TextWriter output, IRecognizer recognizer, IToken offendingSymbol, int line, int charPositionInLine, string msg, RecognitionException e)
19+
public void SyntaxError(TextWriter output, IRecognizer recognizer, IToken offendingSymbol, int line, int charPositionInLine, string msg, RecognitionException e)
2020
{
2121
errors.Add(new SyntaxError(line, charPositionInLine, offendingSymbol.Text, msg, e));
2222
}
23+
24+
public void SyntaxError(TextWriter output, IRecognizer recognizer, int offendingSymbol, int line, int charPositionInLine, string msg, RecognitionException e)
25+
{
26+
errors.Add(new SyntaxError(line, charPositionInLine, offendingSymbol.ToString(), msg, e));
27+
}
2328
}

0 commit comments

Comments
 (0)