llvm-project/llvm/lib/AsmParser/Parser.cpp

89 lines
2.4 KiB
C++
Raw Normal View History

2001-06-07 04:29:01 +08:00
//===- Parser.cpp - Main dispatch module for the Parser library -------------===
//
// 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"
#include "llvm/Module.h"
#include "llvm/Analysis/Verifier.h"
2001-06-07 04:29:01 +08:00
namespace llvm {
// 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.
//
Module *ParseAssemblyFile(const std::string &Filename) {
2001-06-07 04:29:01 +08:00
FILE *F = stdin;
if (Filename != "-") {
F = fopen(Filename.c_str(), "r");
2001-06-07 04:29:01 +08:00
if (F == 0)
throw ParseException(Filename, "Could not open file '" + Filename + "'");
2001-06-07 04:29:01 +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
//===------------------------------------------------------------------------===
ParseException::ParseException(const std::string &filename,
const std::string &message,
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)
: Filename(E.Filename), Message(E.Message) {
2001-06-07 04:29:01 +08:00
LineNo = E.LineNo;
ColumnNo = E.ColumnNo;
}
// Includes info from options
const std::string ParseException::getMessage() const {
std::string Result;
2001-06-07 04:29:01 +08:00
char Buffer[10];
if (Filename == "-")
2001-06-07 04:29:01 +08:00
Result += "<stdin>";
else
Result += Filename;
2001-06-07 04:29:01 +08:00
if (LineNo != -1) {
sprintf(Buffer, "%d", LineNo);
Result += std::string(":") + Buffer;
2001-06-07 04:29:01 +08:00
if (ColumnNo != -1) {
sprintf(Buffer, "%d", ColumnNo);
Result += std::string(",") + Buffer;
2001-06-07 04:29:01 +08:00
}
}
return Result + ": " + Message;
}
} // End llvm namespace