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