blob: c6caa4547320c9532fce15849a03c9407b1d4b17 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===- Parser.cpp - Main dispatch module for the Parser library -------------===
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/assembly/parser.h
11//
12//===------------------------------------------------------------------------===
13
14#include "llvm/Analysis/Verifier.h"
15#include "llvm/Module.h"
16#include "ParserInternals.h"
Chris Lattner697954c2002-01-20 22:54:45 +000017using std::string;
Chris Lattner00950542001-06-06 20:29:01 +000018
Misha Brukman32299912003-09-22 23:50:25 +000019// The useful interface defined by this file... Parse an ASCII file, and return
Chris Lattner00950542001-06-06 20:29:01 +000020// the internal representation in a nice slice'n'dice'able representation.
21//
Chris Lattnerb4d22f92001-10-13 06:37:27 +000022Module *ParseAssemblyFile(const string &Filename) { // throw (ParseException)
Chris Lattner00950542001-06-06 20:29:01 +000023 FILE *F = stdin;
24
Chris Lattnerfbdb1da2002-02-20 18:06:43 +000025 if (Filename != "-") {
Chris Lattnera2850432001-07-22 18:36:00 +000026 F = fopen(Filename.c_str(), "r");
Chris Lattner00950542001-06-06 20:29:01 +000027
Chris Lattnerfbdb1da2002-02-20 18:06:43 +000028 if (F == 0)
29 throw ParseException(Filename, "Could not open file '" + Filename + "'");
Chris Lattner00950542001-06-06 20:29:01 +000030 }
31
Chris Lattnerfbdb1da2002-02-20 18:06:43 +000032 Module *Result;
33 try {
34 Result = RunVMAsmParser(Filename, F);
35 } catch (...) {
36 if (F != stdin) fclose(F); // Make sure to close file descriptor if an
37 throw; // exception is thrown
38 }
Chris Lattner00950542001-06-06 20:29:01 +000039
40 if (F != stdin)
41 fclose(F);
42
Chris Lattner00950542001-06-06 20:29:01 +000043 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}