blob: 3e55e90461f96be071ebaff61b6ccc08325121ef [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 Lattnera2850432001-07-22 18:36:00 +000019 if (Filename != "-")
20 F = fopen(Filename.c_str(), "r");
Chris Lattner00950542001-06-06 20:29:01 +000021
22 if (F == 0) {
Chris Lattnera2850432001-07-22 18:36:00 +000023 throw ParseException(Filename, string("Could not open file '") +
24 Filename + "'");
Chris Lattner00950542001-06-06 20:29:01 +000025 }
26
27 // TODO: If this throws an exception, F is not closed.
Chris Lattnera2850432001-07-22 18:36:00 +000028 Module *Result = RunVMAsmParser(Filename, F);
Chris Lattner00950542001-06-06 20:29:01 +000029
30 if (F != stdin)
31 fclose(F);
32
33 if (Result) { // Check to see that it is valid...
Chris Lattner0b91acf2002-02-20 17:56:02 +000034 if (verifyModule(Result)) {
35 delete Result;
36 throw ParseException(Filename, "Source file is not well formed LLVM!");
Chris Lattner00950542001-06-06 20:29:01 +000037 }
38 }
39 return Result;
40}
41
42
43//===------------------------------------------------------------------------===
44// ParseException Class
45//===------------------------------------------------------------------------===
46
47
Chris Lattnera2850432001-07-22 18:36:00 +000048ParseException::ParseException(const string &filename, const string &message,
49 int lineNo, int colNo)
50 : Filename(filename), Message(message) {
Chris Lattner00950542001-06-06 20:29:01 +000051 LineNo = lineNo; ColumnNo = colNo;
52}
53
54ParseException::ParseException(const ParseException &E)
Chris Lattnera2850432001-07-22 18:36:00 +000055 : Filename(E.Filename), Message(E.Message) {
Chris Lattner00950542001-06-06 20:29:01 +000056 LineNo = E.LineNo;
57 ColumnNo = E.ColumnNo;
58}
59
60const string ParseException::getMessage() const { // Includes info from options
61 string Result;
62 char Buffer[10];
63
Chris Lattnera2850432001-07-22 18:36:00 +000064 if (Filename == "-")
Chris Lattner00950542001-06-06 20:29:01 +000065 Result += "<stdin>";
66 else
Chris Lattnera2850432001-07-22 18:36:00 +000067 Result += Filename;
Chris Lattner00950542001-06-06 20:29:01 +000068
69 if (LineNo != -1) {
70 sprintf(Buffer, "%d", LineNo);
71 Result += string(":") + Buffer;
72 if (ColumnNo != -1) {
73 sprintf(Buffer, "%d", ColumnNo);
74 Result += string(",") + Buffer;
75 }
76 }
77
78 return Result + ": " + Message;
79}