Change Lexer and Parser to take diagnostic reporter function.

Add diagnostic reporter function to lexer/parser and use that from mlir-opt to report errors instead of having the lexer/parser print the errors.

PiperOrigin-RevId: 201892004
diff --git a/lib/Parser/Lexer.cpp b/lib/Parser/Lexer.cpp
index b192f71..7f53886 100644
--- a/lib/Parser/Lexer.cpp
+++ b/lib/Parser/Lexer.cpp
@@ -25,7 +25,9 @@
 using llvm::SMLoc;
 using llvm::SourceMgr;
 
-Lexer::Lexer(llvm::SourceMgr &sourceMgr) : sourceMgr(sourceMgr) {
+Lexer::Lexer(llvm::SourceMgr &sourceMgr,
+             const SMDiagnosticHandlerTy &errorReporter)
+    : sourceMgr(sourceMgr), errorReporter(errorReporter) {
   auto bufferID = sourceMgr.getMainFileID();
   curBuffer = sourceMgr.getMemoryBuffer(bufferID)->getBuffer();
   curPtr = curBuffer.begin();
@@ -33,10 +35,8 @@
 
 /// emitError - Emit an error message and return an Token::error token.
 Token Lexer::emitError(const char *loc, const Twine &message) {
-  // TODO(clattner): If/when we want to implement a -verify mode, this will need
-  // to package up errors into SMDiagnostic and report them.
-  sourceMgr.PrintMessage(SMLoc::getFromPointer(loc), SourceMgr::DK_Error,
-                         message);
+  errorReporter(sourceMgr.GetMessage(SMLoc::getFromPointer(loc),
+                                     SourceMgr::DK_Error, message));
   return formToken(Token::error, loc);
 }