Skip to content

Commit 8a9d71c

Browse files
committed
Change p compiler to return a syntax error for lexical errors
Previously, the p compiler was silently skipping over unexpected characters encountered during lexical analysis. Added a listener on the lexer to raise an error when lexical analysis encounters an error. Added unit tests to verify this behavior.
1 parent 7653b2a commit 8a9d71c

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

Src/PCompiler/CompilerCore/Compiler.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ private static PParser.ProgramContext Parse(ICompilerConfiguration job, FileInfo
106106
var fileText = File.ReadAllText(inputFile.FullName);
107107
var fileStream = new AntlrInputStream(fileText);
108108
var lexer = new PLexer(fileStream);
109+
lexer.AddErrorListener(new PLexerErrorListener(inputFile, job.Handler));
109110
var tokens = new CommonTokenStream(lexer);
110111
var parser = new PParser(tokens);
111112
parser.RemoveErrorListeners();
@@ -133,6 +134,28 @@ private static PParser.ProgramContext Parse(ICompilerConfiguration job, FileInfo
133134
}
134135
}
135136

137+
/// <summary>
138+
/// This error listener converts Antlr lexer errors into translation exceptions via the
139+
/// active error handler
140+
/// </summary>
141+
private class PLexerErrorListener : IAntlrErrorListener<int>
142+
{
143+
private readonly ITranslationErrorHandler handler;
144+
private readonly FileInfo inputFile;
145+
146+
public PLexerErrorListener(FileInfo inputFile, ITranslationErrorHandler handler)
147+
{
148+
this.inputFile = inputFile;
149+
this.handler = handler;
150+
}
151+
152+
public void SyntaxError(IRecognizer recognizer, int offendingSymbol, int line, int charPositionInLine,
153+
string msg, RecognitionException e)
154+
{
155+
throw handler.ParseFailure(inputFile, $"line {line}:{charPositionInLine} {msg}");
156+
}
157+
}
158+
136159
/// <inheritdoc />
137160
/// <summary>
138161
/// This error listener converts Antlr parse errors into translation exceptions via the
@@ -193,4 +216,4 @@ public static int RunWithOutput(string activeDirectory,
193216
return proc.ExitCode;
194217
}
195218
}
196-
}
219+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type ListUsersPayload = (_: any?);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
machine Name@@?@ {
2+
}

0 commit comments

Comments
 (0)