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 #include <stdio.h> // for sprintf 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, string("Could not open file '") + 23 Filename + "'"); 24 } 25 26 // TODO: If this throws an exception, F is not closed. 27 Module *Result = RunVMAsmParser(Filename, F); 28 29 if (F != stdin) 30 fclose(F); 31 32 if (Result) { // Check to see that it is valid... 33 vector<string> Errors; 34 if (verify(Result, Errors)) { 35 delete Result; Result = 0; 36 string Message; 37 38 for (unsigned i = 0; i < Errors.size(); i++) 39 Message += Errors[i] + "\n"; 40 41 throw ParseException(Filename, Message); 42 } 43 } 44 return Result; 45 } 46 47 48 //===------------------------------------------------------------------------=== 49 // ParseException Class 50 //===------------------------------------------------------------------------=== 51 52 53 ParseException::ParseException(const string &filename, const string &message, 54 int lineNo, int colNo) 55 : Filename(filename), Message(message) { 56 LineNo = lineNo; ColumnNo = colNo; 57 } 58 59 ParseException::ParseException(const ParseException &E) 60 : Filename(E.Filename), Message(E.Message) { 61 LineNo = E.LineNo; 62 ColumnNo = E.ColumnNo; 63 } 64 65 const string ParseException::getMessage() const { // Includes info from options 66 string Result; 67 char Buffer[10]; 68 69 if (Filename == "-") 70 Result += "<stdin>"; 71 else 72 Result += Filename; 73 74 if (LineNo != -1) { 75 sprintf(Buffer, "%d", LineNo); 76 Result += string(":") + Buffer; 77 if (ColumnNo != -1) { 78 sprintf(Buffer, "%d", ColumnNo); 79 Result += string(",") + Buffer; 80 } 81 } 82 83 return Result + ": " + Message; 84 } 85