1 //===- Parser.cpp - Main dispatch module for the Parser library -------------=== 2 // 3 // This library implements the functionality defined in llvm/assembly/parser.h 4 // 5 //===------------------------------------------------------------------------=== 6 7 #include "llvm/Analysis/Verifier.h" 8 #include "llvm/Module.h" 9 #include "ParserInternals.h" 10 using std::string; 11 12 // The useful interface defined by this file... Parse an ascii file, and return 13 // the internal representation in a nice slice'n'dice'able representation. 14 // 15 Module *ParseAssemblyFile(const string &Filename) { // throw (ParseException) 16 FILE *F = stdin; 17 18 if (Filename != "-") { 19 F = fopen(Filename.c_str(), "r"); 20 21 if (F == 0) 22 throw ParseException(Filename, "Could not open file '" + Filename + "'"); 23 } 24 25 Module *Result; 26 try { 27 Result = RunVMAsmParser(Filename, F); 28 } catch (...) { 29 if (F != stdin) fclose(F); // Make sure to close file descriptor if an 30 throw; // exception is thrown 31 } 32 33 if (F != stdin) 34 fclose(F); 35 36 if (Result) { // Check to see that it is valid... 37 if (verifyModule(Result)) { 38 delete Result; 39 throw ParseException(Filename, "Source file is not well formed LLVM!"); 40 } 41 } 42 return Result; 43 } 44 45 46 //===------------------------------------------------------------------------=== 47 // ParseException Class 48 //===------------------------------------------------------------------------=== 49 50 51 ParseException::ParseException(const string &filename, const string &message, 52 int lineNo, int colNo) 53 : Filename(filename), Message(message) { 54 LineNo = lineNo; ColumnNo = colNo; 55 } 56 57 ParseException::ParseException(const ParseException &E) 58 : Filename(E.Filename), Message(E.Message) { 59 LineNo = E.LineNo; 60 ColumnNo = E.ColumnNo; 61 } 62 63 const string ParseException::getMessage() const { // Includes info from options 64 string Result; 65 char Buffer[10]; 66 67 if (Filename == "-") 68 Result += "<stdin>"; 69 else 70 Result += Filename; 71 72 if (LineNo != -1) { 73 sprintf(Buffer, "%d", LineNo); 74 Result += string(":") + Buffer; 75 if (ColumnNo != -1) { 76 sprintf(Buffer, "%d", ColumnNo); 77 Result += string(",") + Buffer; 78 } 79 } 80 81 return Result + ": " + Message; 82 } 83