Make this const.
[oota-llvm.git] / lib / AsmParser / Parser.cpp
index c6caa4547320c9532fce15849a03c9407b1d4b17..759e00e3217a6989fe5a9e5195a8190e052cc5a3 100644 (file)
@@ -1,83 +1,87 @@
-//===- Parser.cpp - Main dispatch module for the Parser library -------------===
-// 
+//===- Parser.cpp - Main dispatch module for the Parser library -----------===//
+//
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by the LLVM research group and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
-// 
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
 //===----------------------------------------------------------------------===//
 //
-// This library implements the functionality defined in llvm/assembly/parser.h
+// This library implements the functionality defined in llvm/Assembly/Parser.h
 //
-//===------------------------------------------------------------------------===
+//===----------------------------------------------------------------------===//
 
-#include "llvm/Analysis/Verifier.h"
+#include "llvm/Assembly/Parser.h"
+#include "LLParser.h"
 #include "llvm/Module.h"
-#include "ParserInternals.h"
-using std::string;
-
-// The useful interface defined by this file... Parse an ASCII file, and return
-// the internal representation in a nice slice'n'dice'able representation.
-//
-Module *ParseAssemblyFile(const string &Filename) { // throw (ParseException)
-  FILE *F = stdin;
+#include "llvm/ADT/OwningPtr.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/raw_ostream.h"
+#include <cstring>
+using namespace llvm;
 
-  if (Filename != "-") {
-    F = fopen(Filename.c_str(), "r");
+Module *llvm::ParseAssemblyFile(const std::string &Filename, ParseError &Err) {
+  Err.setFilename(Filename);
 
-    if (F == 0)
-      throw ParseException(Filename, "Could not open file '" + Filename + "'");
+  std::string ErrorStr;
+  OwningPtr<MemoryBuffer>
+    F(MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr));
+  if (F == 0) {
+    Err.setError("Could not open input file '" + Filename + "'");
+    return 0;
   }
 
-  Module *Result;
-  try {
-    Result = RunVMAsmParser(Filename, F);
-  } catch (...) {
-    if (F != stdin) fclose(F);      // Make sure to close file descriptor if an
-    throw;                          // exception is thrown
-  }
+  OwningPtr<Module> M(new Module(Filename));
+  if (LLParser(F.get(), Err, M.get()).Run())
+    return 0;
+  return M.take();
+}
 
-  if (F != stdin)
-    fclose(F);
+Module *llvm::ParseAssemblyString(const char *AsmString, Module *M,
+                                  ParseError &Err) {
+  Err.setFilename("<string>");
+
+  OwningPtr<MemoryBuffer>
+    F(MemoryBuffer::getMemBuffer(AsmString, AsmString+strlen(AsmString),
+                                 "<string>"));
+  
+  // If we are parsing into an existing module, do it.
+  if (M)
+    return LLParser(F.get(), Err, M).Run() ? 0 : M;
 
-  return Result;
+  // Otherwise create a new module.
+  OwningPtr<Module> M2(new Module("<string>"));
+  if (LLParser(F.get(), Err, M2.get()).Run())
+    return 0;
+  return M2.take();
 }
 
 
 //===------------------------------------------------------------------------===
-//                              ParseException Class
+//                              ParseError Class
 //===------------------------------------------------------------------------===
 
+void ParseError::PrintError(const char *ProgName, raw_ostream &S) {
+  errs() << ProgName << ": ";
+  if (Filename == "-")
+    errs() << "<stdin>";
+  else
+    errs() << Filename;
 
-ParseException::ParseException(const string &filename, const string &message, 
-                              int lineNo, int colNo) 
-  : Filename(filename), Message(message) {
-  LineNo = lineNo; ColumnNo = colNo;
-}
-
-ParseException::ParseException(const ParseException &E) 
-  : Filename(E.Filename), Message(E.Message) {
-  LineNo = E.LineNo;
-  ColumnNo = E.ColumnNo;
-}
+  if (LineNo != -1) {
+    errs() << ':' << LineNo;
+    if (ColumnNo != -1)
+      errs() << ':' << (ColumnNo+1);
+  }
 
-const string ParseException::getMessage() const { // Includes info from options
-  string Result;
-  char Buffer[10];
+  errs() << ": " << Message << '\n';
 
-  if (Filename == "-") 
-    Result += "<stdin>";
-  else
-    Result += Filename;
+  if (LineNo != -1 && ColumnNo != -1) {
+    errs() << LineContents << '\n';
 
-  if (LineNo != -1) {
-    sprintf(Buffer, "%d", LineNo);
-    Result += string(":") + Buffer;
-    if (ColumnNo != -1) {
-      sprintf(Buffer, "%d", ColumnNo);
-      Result += string(",") + Buffer;
-    }
+    // Print out spaces/tabs before the caret.
+    for (unsigned i = 0; i != unsigned(ColumnNo); ++i)
+      errs() << (LineContents[i] == '\t' ? '\t' : ' ');
+    errs() << "^\n";
   }
-  
-  return Result + ": " + Message;
 }