blob: 626fe263ad49c7936321193f4b2cbd651351fdc8 [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
11
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 Lattnera2850432001-07-22 18:36:00 +000015Module *ParseAssemblyFile(const string &Filename) throw (ParseException) {
Chris Lattner00950542001-06-06 20:29:01 +000016 FILE *F = stdin;
17
Chris Lattnera2850432001-07-22 18:36:00 +000018 if (Filename != "-")
19 F = fopen(Filename.c_str(), "r");
Chris Lattner00950542001-06-06 20:29:01 +000020
21 if (F == 0) {
Chris Lattnera2850432001-07-22 18:36:00 +000022 throw ParseException(Filename, string("Could not open file '") +
23 Filename + "'");
Chris Lattner00950542001-06-06 20:29:01 +000024 }
25
26 // TODO: If this throws an exception, F is not closed.
Chris Lattnera2850432001-07-22 18:36:00 +000027 Module *Result = RunVMAsmParser(Filename, F);
Chris Lattner00950542001-06-06 20:29:01 +000028
29 if (F != stdin)
30 fclose(F);
31
32 if (Result) { // Check to see that it is valid...
33 vector<string> Errors;
34 if (verify(Result, Errors)) {
35 delete Result; Result = 0;
36 string Message;
37
38 for (unsigned i = 0; i < Errors.size(); i++)
39 Message += Errors[i] + "\n";
40
Chris Lattnera2850432001-07-22 18:36:00 +000041 throw ParseException(Filename, Message);
Chris Lattner00950542001-06-06 20:29:01 +000042 }
43 }
44 return Result;
45}
46
47
48//===------------------------------------------------------------------------===
49// ParseException Class
50//===------------------------------------------------------------------------===
51
52
Chris Lattnera2850432001-07-22 18:36:00 +000053ParseException::ParseException(const string &filename, const string &message,
54 int lineNo, int colNo)
55 : Filename(filename), Message(message) {
Chris Lattner00950542001-06-06 20:29:01 +000056 LineNo = lineNo; ColumnNo = colNo;
57}
58
59ParseException::ParseException(const ParseException &E)
Chris Lattnera2850432001-07-22 18:36:00 +000060 : Filename(E.Filename), Message(E.Message) {
Chris Lattner00950542001-06-06 20:29:01 +000061 LineNo = E.LineNo;
62 ColumnNo = E.ColumnNo;
63}
64
65const string ParseException::getMessage() const { // Includes info from options
66 string Result;
67 char Buffer[10];
68
Chris Lattnera2850432001-07-22 18:36:00 +000069 if (Filename == "-")
Chris Lattner00950542001-06-06 20:29:01 +000070 Result += "<stdin>";
71 else
Chris Lattnera2850432001-07-22 18:36:00 +000072 Result += Filename;
Chris Lattner00950542001-06-06 20:29:01 +000073
74 if (LineNo != -1) {
75 sprintf(Buffer, "%d", LineNo);
76 Result += string(":") + Buffer;
77 if (ColumnNo != -1) {
78 sprintf(Buffer, "%d", ColumnNo);
79 Result += string(",") + Buffer;
80 }
81 }
82
83 return Result + ": " + Message;
84}