blob: a99849d31b45f8a582d51b1b53a23670f17b1c98 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===- Parser.cpp - Main dispatch module for the Parser library -------------===
2//
3// This library implements the functionality defined in llvm/assembly/parser.h
4//
5//===------------------------------------------------------------------------===
6
7#include "llvm/Analysis/Verifier.h"
8#include "llvm/Module.h"
9#include "ParserInternals.h"
10#include <stdio.h> // for sprintf
Chris Lattner697954c2002-01-20 22:54:45 +000011using std::string;
Chris Lattner00950542001-06-06 20:29:01 +000012
13// The useful interface defined by this file... Parse an ascii file, and return
14// the internal representation in a nice slice'n'dice'able representation.
15//
Chris Lattnerb4d22f92001-10-13 06:37:27 +000016Module *ParseAssemblyFile(const string &Filename) { // throw (ParseException)
Chris Lattner00950542001-06-06 20:29:01 +000017 FILE *F = stdin;
18
Chris Lattnerfbdb1da2002-02-20 18:06:43 +000019 if (Filename != "-") {
Chris Lattnera2850432001-07-22 18:36:00 +000020 F = fopen(Filename.c_str(), "r");
Chris Lattner00950542001-06-06 20:29:01 +000021
Chris Lattnerfbdb1da2002-02-20 18:06:43 +000022 if (F == 0)
23 throw ParseException(Filename, "Could not open file '" + Filename + "'");
Chris Lattner00950542001-06-06 20:29:01 +000024 }
25
Chris Lattnerfbdb1da2002-02-20 18:06:43 +000026 Module *Result;
27 try {
28 Result = RunVMAsmParser(Filename, F);
29 } catch (...) {
30 if (F != stdin) fclose(F); // Make sure to close file descriptor if an
31 throw; // exception is thrown
32 }
Chris Lattner00950542001-06-06 20:29:01 +000033
34 if (F != stdin)
35 fclose(F);
36
37 if (Result) { // Check to see that it is valid...
Chris Lattner0b91acf2002-02-20 17:56:02 +000038 if (verifyModule(Result)) {
39 delete Result;
40 throw ParseException(Filename, "Source file is not well formed LLVM!");
Chris Lattner00950542001-06-06 20:29:01 +000041 }
42 }
43 return Result;
44}
45
46
47//===------------------------------------------------------------------------===
48// ParseException Class
49//===------------------------------------------------------------------------===
50
51
Chris Lattnera2850432001-07-22 18:36:00 +000052ParseException::ParseException(const string &filename, const string &message,
53 int lineNo, int colNo)
54 : Filename(filename), Message(message) {
Chris Lattner00950542001-06-06 20:29:01 +000055 LineNo = lineNo; ColumnNo = colNo;
56}
57
58ParseException::ParseException(const ParseException &E)
Chris Lattnera2850432001-07-22 18:36:00 +000059 : Filename(E.Filename), Message(E.Message) {
Chris Lattner00950542001-06-06 20:29:01 +000060 LineNo = E.LineNo;
61 ColumnNo = E.ColumnNo;
62}
63
64const string ParseException::getMessage() const { // Includes info from options
65 string Result;
66 char Buffer[10];
67
Chris Lattnera2850432001-07-22 18:36:00 +000068 if (Filename == "-")
Chris Lattner00950542001-06-06 20:29:01 +000069 Result += "<stdin>";
70 else
Chris Lattnera2850432001-07-22 18:36:00 +000071 Result += Filename;
Chris Lattner00950542001-06-06 20:29:01 +000072
73 if (LineNo != -1) {
74 sprintf(Buffer, "%d", LineNo);
75 Result += string(":") + Buffer;
76 if (ColumnNo != -1) {
77 sprintf(Buffer, "%d", ColumnNo);
78 Result += string(",") + Buffer;
79 }
80 }
81
82 return Result + ": " + Message;
83}