05547ee099c85eb14c39e3bef84a3d636c1323ea
[oota-llvm.git] / lib / AsmParser / Parser.cpp
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 using std::string;
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, "Could not open file '" + Filename + "'");
23   }
24
25   Module *Result;
26   try {
27     Result = RunVMAsmParser(Filename, F);
28   } catch (...) {
29     if (F != stdin) fclose(F);      // Make sure to close file descriptor if an
30     throw;                          // exception is thrown
31   }
32
33   if (F != stdin)
34     fclose(F);
35
36   return Result;
37 }
38
39
40 //===------------------------------------------------------------------------===
41 //                              ParseException Class
42 //===------------------------------------------------------------------------===
43
44
45 ParseException::ParseException(const string &filename, const string &message, 
46                                int lineNo, int colNo) 
47   : Filename(filename), Message(message) {
48   LineNo = lineNo; ColumnNo = colNo;
49 }
50
51 ParseException::ParseException(const ParseException &E) 
52   : Filename(E.Filename), Message(E.Message) {
53   LineNo = E.LineNo;
54   ColumnNo = E.ColumnNo;
55 }
56
57 const string ParseException::getMessage() const { // Includes info from options
58   string Result;
59   char Buffer[10];
60
61   if (Filename == "-") 
62     Result += "<stdin>";
63   else
64     Result += Filename;
65
66   if (LineNo != -1) {
67     sprintf(Buffer, "%d", LineNo);
68     Result += string(":") + Buffer;
69     if (ColumnNo != -1) {
70       sprintf(Buffer, "%d", ColumnNo);
71       Result += string(",") + Buffer;
72     }
73   }
74   
75   return Result + ": " + Message;
76 }