Skip to content

Change p compiler to return a syntax error for lexical errors #846

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion Src/PCompiler/CompilerCore/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ private static PParser.ProgramContext Parse(ICompilerConfiguration job, FileInfo
var fileText = File.ReadAllText(inputFile.FullName);
var fileStream = new AntlrInputStream(fileText);
var lexer = new PLexer(fileStream);
lexer.AddErrorListener(new PLexerErrorListener(inputFile, job.Handler));
var tokens = new CommonTokenStream(lexer);
var parser = new PParser(tokens);
parser.RemoveErrorListeners();
Expand Down Expand Up @@ -133,6 +134,28 @@ private static PParser.ProgramContext Parse(ICompilerConfiguration job, FileInfo
}
}

/// <summary>
/// This error listener converts Antlr lexer errors into translation exceptions via the
/// active error handler
/// </summary>
private class PLexerErrorListener : IAntlrErrorListener<int>
{
private readonly ITranslationErrorHandler handler;
private readonly FileInfo inputFile;

public PLexerErrorListener(FileInfo inputFile, ITranslationErrorHandler handler)
{
this.inputFile = inputFile;
this.handler = handler;
}

public void SyntaxError(IRecognizer recognizer, int offendingSymbol, int line, int charPositionInLine,
string msg, RecognitionException e)
{
throw handler.ParseFailure(inputFile, $"line {line}:{charPositionInLine} {msg}");
}
}

/// <inheritdoc />
/// <summary>
/// This error listener converts Antlr parse errors into translation exceptions via the
Expand Down Expand Up @@ -193,4 +216,4 @@ public static int RunWithOutput(string activeDirectory,
return proc.ExitCode;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type ListUsersPayload = (_: any?);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
machine Name@@?@ {
}
Loading