1 //===- Parser.cpp - Main dispatch module for the Parser library -------------===
3 // This library implements the functionality defined in llvm/assembly/parser.h
5 //===------------------------------------------------------------------------===
7 #include "llvm/Analysis/Verifier.h"
8 #include "llvm/Module.h"
9 #include "ParserInternals.h"
10 #include <stdio.h> // for sprintf
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.
15 Module *ParseAssemblyFile(const string &Filename) throw (ParseException) {
19 F = fopen(Filename.c_str(), "r");
22 throw ParseException(Filename, string("Could not open file '") +
26 // TODO: If this throws an exception, F is not closed.
27 Module *Result = RunVMAsmParser(Filename, F);
32 if (Result) { // Check to see that it is valid...
33 vector<string> Errors;
34 if (verify(Result, Errors)) {
35 delete Result; Result = 0;
38 for (unsigned i = 0; i < Errors.size(); i++)
39 Message += Errors[i] + "\n";
41 throw ParseException(Filename, Message);
48 //===------------------------------------------------------------------------===
49 // ParseException Class
50 //===------------------------------------------------------------------------===
53 ParseException::ParseException(const string &filename, const string &message,
54 int lineNo, int colNo)
55 : Filename(filename), Message(message) {
56 LineNo = lineNo; ColumnNo = colNo;
59 ParseException::ParseException(const ParseException &E)
60 : Filename(E.Filename), Message(E.Message) {
62 ColumnNo = E.ColumnNo;
65 const string ParseException::getMessage() const { // Includes info from options
75 sprintf(Buffer, "%d", LineNo);
76 Result += string(":") + Buffer;
78 sprintf(Buffer, "%d", ColumnNo);
79 Result += string(",") + Buffer;
83 return Result + ": " + Message;