Implement enough of a lexer and parser for MLIR to parse extfunc's without
arguments.

PiperOrigin-RevId: 201706570
diff --git a/tools/mlir-opt/mlir-opt.cpp b/tools/mlir-opt/mlir-opt.cpp
index dee86ed..b5a548d 100644
--- a/tools/mlir-opt/mlir-opt.cpp
+++ b/tools/mlir-opt/mlir-opt.cpp
@@ -22,7 +22,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "mlir/IR/Module.h"
+#include "mlir/Parser.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/FileUtilities.h"
 #include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/ToolOutputFile.h"
@@ -56,13 +58,27 @@
 
   cl::ParseCommandLineOptions(argc, argv, "MLIR modular optimizer driver\n");
 
-  // Instantiate an IR object.
-  Module m;
-  m.functionList.push_back(new Function("foo"));
-  m.functionList.push_back(new Function("bar"));
+  // Set up the input file.
+  auto fileOrErr = MemoryBuffer::getFileOrSTDIN(inputFilename);
+  if (std::error_code error = fileOrErr.getError()) {
+    llvm::errs() << argv[0] << ": could not open input file '" << inputFilename
+                 << "': " << error.message() << "\n";
+    return 1;
+  }
+
+  // Tell sourceMgr about this buffer, which is what the parser will pick up.
+  SourceMgr sourceMgr;
+  sourceMgr.AddNewSourceBuffer(std::move(*fileOrErr), SMLoc());
+
+  // Parse the input file and emit any errors.
+  std::unique_ptr<Module> module(parseSourceFile(sourceMgr));
+  if (!module) return 1;
 
   // Print the output.
   auto output = getOutputStream();
-  m.print(output->os());
+  module->print(output->os());
   output->keep();
+
+  // Success.
+  return 0;
 }