* Remove trailing whitespace
[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 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
46 //===------------------------------------------------------------------------===
47 //                              ParseException Class
48 //===------------------------------------------------------------------------===
49
50
51 ParseException::ParseException(const std::string &filename,
52                                const std::string &message,
53                                int lineNo, int colNo)
54   : Filename(filename), Message(message) {
55   LineNo = lineNo; ColumnNo = colNo;
56 }
57
58 ParseException::ParseException(const ParseException &E)
59   : Filename(E.Filename), Message(E.Message) {
60   LineNo = E.LineNo;
61   ColumnNo = E.ColumnNo;
62 }
63
64 // Includes info from options
65 const std::string ParseException::getMessage() const {
66   std::string Result;
67   char Buffer[10];
68
69   if (Filename == "-")
70     Result += "<stdin>";
71   else
72     Result += Filename;
73
74   if (LineNo != -1) {
75     sprintf(Buffer, "%d", LineNo);
76     Result += std::string(":") + Buffer;
77     if (ColumnNo != -1) {
78       sprintf(Buffer, "%d", ColumnNo);
79       Result += std::string(",") + Buffer;
80     }
81   }
82
83   return Result + ": " + Message;
84 }