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