2001-06-07 04:29:01 +08:00
|
|
|
//===- Parser.cpp - Main dispatch module for the Parser library -------------===
|
2003-10-21 03:43:21 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-07 04:29:01 +08:00
|
|
|
//
|
|
|
|
// This library implements the functionality defined in llvm/assembly/parser.h
|
|
|
|
//
|
|
|
|
//===------------------------------------------------------------------------===
|
|
|
|
|
|
|
|
#include "ParserInternals.h"
|
2003-10-24 02:00:34 +08:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Analysis/Verifier.h"
|
2001-06-07 04:29:01 +08:00
|
|
|
|
2003-11-12 06:41:34 +08:00
|
|
|
namespace llvm {
|
|
|
|
|
2003-09-23 07:50:25 +08:00
|
|
|
// The useful interface defined by this file... Parse an ASCII file, and return
|
2001-06-07 04:29:01 +08:00
|
|
|
// the internal representation in a nice slice'n'dice'able representation.
|
|
|
|
//
|
2003-10-24 02:00:34 +08:00
|
|
|
Module *ParseAssemblyFile(const std::string &Filename) {
|
2001-06-07 04:29:01 +08:00
|
|
|
FILE *F = stdin;
|
|
|
|
|
2002-02-21 02:06:43 +08:00
|
|
|
if (Filename != "-") {
|
2001-07-23 02:36:00 +08:00
|
|
|
F = fopen(Filename.c_str(), "r");
|
2001-06-07 04:29:01 +08:00
|
|
|
|
2002-02-21 02:06:43 +08:00
|
|
|
if (F == 0)
|
|
|
|
throw ParseException(Filename, "Could not open file '" + Filename + "'");
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
|
2002-02-21 02:06:43 +08:00
|
|
|
Module *Result;
|
|
|
|
try {
|
|
|
|
Result = RunVMAsmParser(Filename, F);
|
|
|
|
} catch (...) {
|
|
|
|
if (F != stdin) fclose(F); // Make sure to close file descriptor if an
|
|
|
|
throw; // exception is thrown
|
|
|
|
}
|
2001-06-07 04:29:01 +08:00
|
|
|
|
|
|
|
if (F != stdin)
|
|
|
|
fclose(F);
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//===------------------------------------------------------------------------===
|
|
|
|
// ParseException Class
|
|
|
|
//===------------------------------------------------------------------------===
|
|
|
|
|
|
|
|
|
2003-10-24 02:00:34 +08:00
|
|
|
ParseException::ParseException(const std::string &filename,
|
|
|
|
const std::string &message,
|
2001-07-23 02:36:00 +08:00
|
|
|
int lineNo, int colNo)
|
|
|
|
: Filename(filename), Message(message) {
|
2001-06-07 04:29:01 +08:00
|
|
|
LineNo = lineNo; ColumnNo = colNo;
|
|
|
|
}
|
|
|
|
|
|
|
|
ParseException::ParseException(const ParseException &E)
|
2001-07-23 02:36:00 +08:00
|
|
|
: Filename(E.Filename), Message(E.Message) {
|
2001-06-07 04:29:01 +08:00
|
|
|
LineNo = E.LineNo;
|
|
|
|
ColumnNo = E.ColumnNo;
|
|
|
|
}
|
|
|
|
|
2003-10-24 02:00:34 +08:00
|
|
|
// Includes info from options
|
|
|
|
const std::string ParseException::getMessage() const {
|
|
|
|
std::string Result;
|
2001-06-07 04:29:01 +08:00
|
|
|
char Buffer[10];
|
|
|
|
|
2001-07-23 02:36:00 +08:00
|
|
|
if (Filename == "-")
|
2001-06-07 04:29:01 +08:00
|
|
|
Result += "<stdin>";
|
|
|
|
else
|
2001-07-23 02:36:00 +08:00
|
|
|
Result += Filename;
|
2001-06-07 04:29:01 +08:00
|
|
|
|
|
|
|
if (LineNo != -1) {
|
|
|
|
sprintf(Buffer, "%d", LineNo);
|
2003-10-24 02:00:34 +08:00
|
|
|
Result += std::string(":") + Buffer;
|
2001-06-07 04:29:01 +08:00
|
|
|
if (ColumnNo != -1) {
|
|
|
|
sprintf(Buffer, "%d", ColumnNo);
|
2003-10-24 02:00:34 +08:00
|
|
|
Result += std::string(",") + Buffer;
|
2001-06-07 04:29:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result + ": " + Message;
|
|
|
|
}
|
2003-11-12 06:41:34 +08:00
|
|
|
|
|
|
|
} // End llvm namespace
|