Reverting back to original 1.8 version so I can manually merge in patch.
[llvm-complete.git] / lib / AsmParser / Parser.cpp
blob7bb4f0a362f8a91644223863983d681e554109cb
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
12 //===------------------------------------------------------------------------===
14 #include "ParserInternals.h"
15 #include "llvm/Module.h"
16 using namespace llvm;
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.
21 Module *llvm::ParseAssemblyFile(const std::string &Filename) {
22 FILE *F = stdin;
24 if (Filename != "-") {
25 F = fopen(Filename.c_str(), "r");
27 if (F == 0)
28 throw ParseException(Filename, "Could not open file '" + Filename + "'");
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
39 if (F != stdin)
40 fclose(F);
42 return Result;
45 Module *llvm::ParseAssemblyString(const char * AsmString, Module * M) {
46 return RunVMAsmParser(AsmString, M);
50 //===------------------------------------------------------------------------===
51 // ParseException Class
52 //===------------------------------------------------------------------------===
55 ParseException::ParseException(const std::string &filename,
56 const std::string &message,
57 int lineNo, int colNo)
58 : Filename(filename), Message(message) {
59 LineNo = lineNo; ColumnNo = colNo;
62 ParseException::ParseException(const ParseException &E)
63 : Filename(E.Filename), Message(E.Message) {
64 LineNo = E.LineNo;
65 ColumnNo = E.ColumnNo;
68 // Includes info from options
69 const std::string ParseException::getMessage() const {
70 std::string Result;
71 char Buffer[10];
73 if (Filename == "-")
74 Result += "<stdin>";
75 else
76 Result += Filename;
78 if (LineNo != -1) {
79 sprintf(Buffer, "%d", LineNo);
80 Result += std::string(":") + Buffer;
81 if (ColumnNo != -1) {
82 sprintf(Buffer, "%d", ColumnNo);
83 Result += std::string(",") + Buffer;
87 return Result + ": " + Message;