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/tools/mlir-opt/mlir-opt.cpp b/tools/mlir-opt/mlir-opt.cpp
index 75ed4f5..b90dcd2 100644
--- a/tools/mlir-opt/mlir-opt.cpp
+++ b/tools/mlir-opt/mlir-opt.cpp
@@ -64,9 +64,14 @@
   SourceMgr sourceMgr;
   sourceMgr.AddNewSourceBuffer(std::move(buffer), SMLoc());
 
-  // Parse the input file and emit any errors.
+  // Parse the input file.
   MLIRContext context;
-  std::unique_ptr<Module> module(parseSourceFile(sourceMgr, &context));
+  // Error reporter that simply prints the errors reported.
+  SMDiagnosticHandlerTy errorReporter = [&sourceMgr](llvm::SMDiagnostic err) {
+    sourceMgr.PrintMessage(err.getLoc(), err.getKind(), err.getMessage());
+  };
+  std::unique_ptr<Module> module(
+      parseSourceFile(sourceMgr, &context, errorReporter));
   if (!module) return false;
 
   // Print the output.