blob: 57c831e9e17e581e22746ee6a3d61dc488fd2a46 [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//
15Module *ParseAssemblyFile(const ToolCommandLine &Opts) throw (ParseException) {
16 FILE *F = stdin;
17
18 if (Opts.getInputFilename() != "-")
19 F = fopen(Opts.getInputFilename().c_str(), "r");
20
21 if (F == 0) {
22 throw ParseException(Opts, string("Could not open file '") +
23 Opts.getInputFilename() + "'");
24 }
25
26 // TODO: If this throws an exception, F is not closed.
27 Module *Result = RunVMAsmParser(Opts, F);
28
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
41 throw ParseException(Opts, Message);
42 }
43 }
44 return Result;
45}
46
47
48//===------------------------------------------------------------------------===
49// ParseException Class
50//===------------------------------------------------------------------------===
51
52
53ParseException::ParseException(const ToolCommandLine &opts,
54 const string &message, int lineNo, int colNo)
55 : Opts(opts), Message(message) {
56 LineNo = lineNo; ColumnNo = colNo;
57}
58
59ParseException::ParseException(const ParseException &E)
60 : Opts(E.Opts), Message(E.Message) {
61 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
69 if (Opts.getInputFilename() == "-")
70 Result += "<stdin>";
71 else
72 Result += Opts.getInputFilename();
73
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}