blob: 7af313348d29b1bb9412ef06980636f791772aa1 [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"
Chris Lattner697954c2002-01-20 22:54:45 +000010using std::string;
Chris Lattner00950542001-06-06 20:29:01 +000011
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.
14//
Chris Lattnerb4d22f92001-10-13 06:37:27 +000015Module *ParseAssemblyFile(const string &Filename) { // throw (ParseException)
Chris Lattner00950542001-06-06 20:29:01 +000016 FILE *F = stdin;
17
Chris Lattnerfbdb1da2002-02-20 18:06:43 +000018 if (Filename != "-") {
Chris Lattnera2850432001-07-22 18:36:00 +000019 F = fopen(Filename.c_str(), "r");
Chris Lattner00950542001-06-06 20:29:01 +000020
Chris Lattnerfbdb1da2002-02-20 18:06:43 +000021 if (F == 0)
22 throw ParseException(Filename, "Could not open file '" + Filename + "'");
Chris Lattner00950542001-06-06 20:29:01 +000023 }
24
Chris Lattnerfbdb1da2002-02-20 18:06:43 +000025 Module *Result;
26 try {
27 Result = RunVMAsmParser(Filename, F);
28 } catch (...) {
29 if (F != stdin) fclose(F); // Make sure to close file descriptor if an
30 throw; // exception is thrown
31 }
Chris Lattner00950542001-06-06 20:29:01 +000032
33 if (F != stdin)
34 fclose(F);
35
36 if (Result) { // Check to see that it is valid...
Chris Lattner7e708292002-06-25 16:13:24 +000037 if (verifyModule(*Result)) {
Chris Lattner0b91acf2002-02-20 17:56:02 +000038 delete Result;
39 throw ParseException(Filename, "Source file is not well formed LLVM!");
Chris Lattner00950542001-06-06 20:29:01 +000040 }
41 }
42 return Result;
43}
44
45
46//===------------------------------------------------------------------------===
47// ParseException Class
48//===------------------------------------------------------------------------===
49
50
Chris Lattnera2850432001-07-22 18:36:00 +000051ParseException::ParseException(const string &filename, const string &message,
52 int lineNo, int colNo)
53 : Filename(filename), Message(message) {
Chris Lattner00950542001-06-06 20:29:01 +000054 LineNo = lineNo; ColumnNo = colNo;
55}
56
57ParseException::ParseException(const ParseException &E)
Chris Lattnera2850432001-07-22 18:36:00 +000058 : Filename(E.Filename), Message(E.Message) {
Chris Lattner00950542001-06-06 20:29:01 +000059 LineNo = E.LineNo;
60 ColumnNo = E.ColumnNo;
61}
62
63const string ParseException::getMessage() const { // Includes info from options
64 string Result;
65 char Buffer[10];
66
Chris Lattnera2850432001-07-22 18:36:00 +000067 if (Filename == "-")
Chris Lattner00950542001-06-06 20:29:01 +000068 Result += "<stdin>";
69 else
Chris Lattnera2850432001-07-22 18:36:00 +000070 Result += Filename;
Chris Lattner00950542001-06-06 20:29:01 +000071
72 if (LineNo != -1) {
73 sprintf(Buffer, "%d", LineNo);
74 Result += string(":") + Buffer;
75 if (ColumnNo != -1) {
76 sprintf(Buffer, "%d", ColumnNo);
77 Result += string(",") + Buffer;
78 }
79 }
80
81 return Result + ": " + Message;
82}