LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Parser.cpp

Go to the documentation of this file.
00001 //===- Parser.cpp - Main dispatch module for the Parser library -------------===
00002 // 
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 // 
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This library implements the functionality defined in llvm/assembly/parser.h
00011 //
00012 //===------------------------------------------------------------------------===
00013 
00014 #include "ParserInternals.h"
00015 #include "llvm/Module.h"
00016 using namespace llvm;
00017 
00018 // The useful interface defined by this file... Parse an ASCII file, and return
00019 // the internal representation in a nice slice'n'dice'able representation.
00020 //
00021 Module *llvm::ParseAssemblyFile(const std::string &Filename) {
00022   FILE *F = stdin;
00023 
00024   if (Filename != "-") {
00025     F = fopen(Filename.c_str(), "r");
00026 
00027     if (F == 0)
00028       throw ParseException(Filename, "Could not open file '" + Filename + "'");
00029   }
00030 
00031   Module *Result;
00032   try {
00033     Result = RunVMAsmParser(Filename, F);
00034   } catch (...) {
00035     if (F != stdin) fclose(F);      // Make sure to close file descriptor if an
00036     throw;                          // exception is thrown
00037   }
00038 
00039   if (F != stdin)
00040     fclose(F);
00041 
00042   return Result;
00043 }
00044 
00045 
00046 //===------------------------------------------------------------------------===
00047 //                              ParseException Class
00048 //===------------------------------------------------------------------------===
00049 
00050 
00051 ParseException::ParseException(const std::string &filename,
00052                                const std::string &message, 
00053              int lineNo, int colNo) 
00054   : Filename(filename), Message(message) {
00055   LineNo = lineNo; ColumnNo = colNo;
00056 }
00057 
00058 ParseException::ParseException(const ParseException &E) 
00059   : Filename(E.Filename), Message(E.Message) {
00060   LineNo = E.LineNo;
00061   ColumnNo = E.ColumnNo;
00062 }
00063 
00064 // Includes info from options
00065 const std::string ParseException::getMessage() const { 
00066   std::string Result;
00067   char Buffer[10];
00068 
00069   if (Filename == "-") 
00070     Result += "<stdin>";
00071   else
00072     Result += Filename;
00073 
00074   if (LineNo != -1) {
00075     sprintf(Buffer, "%d", LineNo);
00076     Result += std::string(":") + Buffer;
00077     if (ColumnNo != -1) {
00078       sprintf(Buffer, "%d", ColumnNo);
00079       Result += std::string(",") + Buffer;
00080     }
00081   }
00082   
00083   return Result + ": " + Message;
00084 }