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 ToolCommandLine &Opts) throw (ParseException) {
18 if (Opts.getInputFilename() != "-")
19 F = fopen(Opts.getInputFilename().c_str(), "r");
22 throw ParseException(Opts, string("Could not open file '") +
23 Opts.getInputFilename() + "'");
26 // TODO: If this throws an exception, F is not closed.
27 Module *Result = RunVMAsmParser(Opts, 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(Opts, Message);
48 //===------------------------------------------------------------------------===
49 // ParseException Class
50 //===------------------------------------------------------------------------===
53 ParseException::ParseException(const ToolCommandLine &opts,
54 const string &message, int lineNo, int colNo)
55 : Opts(opts), Message(message) {
56 LineNo = lineNo; ColumnNo = colNo;
59 ParseException::ParseException(const ParseException &E)
60 : Opts(E.Opts), Message(E.Message) {
62 ColumnNo = E.ColumnNo;
65 const string ParseException::getMessage() const { // Includes info from options
69 if (Opts.getInputFilename() == "-")
72 Result += Opts.getInputFilename();
75 sprintf(Buffer, "%d", LineNo);
76 Result += string(":") + Buffer;
78 sprintf(Buffer, "%d", ColumnNo);
79 Result += string(",") + Buffer;
83 return Result + ": " + Message;