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