blob: 7bb4f0a362f8a91644223863983d681e554109cb [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===- Parser.cpp - Main dispatch module for the Parser library -------------===
Misha Brukman13f332c2005-04-21 21:10:11 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// 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.
Misha Brukman13f332c2005-04-21 21:10:11 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This library implements the functionality defined in llvm/assembly/parser.h
11//
12//===------------------------------------------------------------------------===
13
Chris Lattner2f7c9632001-06-06 20:29:01 +000014#include "ParserInternals.h"
Misha Brukmanad44de12003-10-23 18:00:34 +000015#include "llvm/Module.h"
Chris Lattnerd25cad92004-07-13 08:42:12 +000016using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000017
Misha Brukmand4d259c2003-09-22 23:50:25 +000018// The useful interface defined by this file... Parse an ASCII file, and return
Chris Lattner2f7c9632001-06-06 20:29:01 +000019// the internal representation in a nice slice'n'dice'able representation.
20//
Chris Lattnerd25cad92004-07-13 08:42:12 +000021Module *llvm::ParseAssemblyFile(const std::string &Filename) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000022 FILE *F = stdin;
23
Chris Lattneree6f2af2002-02-20 18:06:43 +000024 if (Filename != "-") {
Chris Lattnerf2d1e792001-07-22 18:36:00 +000025 F = fopen(Filename.c_str(), "r");
Chris Lattner2f7c9632001-06-06 20:29:01 +000026
Chris Lattneree6f2af2002-02-20 18:06:43 +000027 if (F == 0)
28 throw ParseException(Filename, "Could not open file '" + Filename + "'");
Chris Lattner2f7c9632001-06-06 20:29:01 +000029 }
30
Chris Lattneree6f2af2002-02-20 18:06:43 +000031 Module *Result;
32 try {
33 Result = RunVMAsmParser(Filename, F);
34 } catch (...) {
35 if (F != stdin) fclose(F); // Make sure to close file descriptor if an
36 throw; // exception is thrown
37 }
Chris Lattner2f7c9632001-06-06 20:29:01 +000038
39 if (F != stdin)
40 fclose(F);
41
Chris Lattner2f7c9632001-06-06 20:29:01 +000042 return Result;
43}
44
Chris Lattner416a0d42005-05-20 03:25:47 +000045Module *llvm::ParseAssemblyString(const char * AsmString, Module * M) {
46 return RunVMAsmParser(AsmString, M);
47}
48
Chris Lattner2f7c9632001-06-06 20:29:01 +000049
50//===------------------------------------------------------------------------===
51// ParseException Class
52//===------------------------------------------------------------------------===
53
54
Misha Brukmanad44de12003-10-23 18:00:34 +000055ParseException::ParseException(const std::string &filename,
Misha Brukman13f332c2005-04-21 21:10:11 +000056 const std::string &message,
57 int lineNo, int colNo)
Chris Lattnerf2d1e792001-07-22 18:36:00 +000058 : Filename(filename), Message(message) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000059 LineNo = lineNo; ColumnNo = colNo;
60}
61
Misha Brukman13f332c2005-04-21 21:10:11 +000062ParseException::ParseException(const ParseException &E)
Chris Lattnerf2d1e792001-07-22 18:36:00 +000063 : Filename(E.Filename), Message(E.Message) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000064 LineNo = E.LineNo;
65 ColumnNo = E.ColumnNo;
66}
67
Misha Brukmanad44de12003-10-23 18:00:34 +000068// Includes info from options
Misha Brukman13f332c2005-04-21 21:10:11 +000069const std::string ParseException::getMessage() const {
Misha Brukmanad44de12003-10-23 18:00:34 +000070 std::string Result;
Chris Lattner2f7c9632001-06-06 20:29:01 +000071 char Buffer[10];
72
Misha Brukman13f332c2005-04-21 21:10:11 +000073 if (Filename == "-")
Chris Lattner2f7c9632001-06-06 20:29:01 +000074 Result += "<stdin>";
75 else
Chris Lattnerf2d1e792001-07-22 18:36:00 +000076 Result += Filename;
Chris Lattner2f7c9632001-06-06 20:29:01 +000077
78 if (LineNo != -1) {
79 sprintf(Buffer, "%d", LineNo);
Misha Brukmanad44de12003-10-23 18:00:34 +000080 Result += std::string(":") + Buffer;
Chris Lattner2f7c9632001-06-06 20:29:01 +000081 if (ColumnNo != -1) {
82 sprintf(Buffer, "%d", ColumnNo);
Misha Brukmanad44de12003-10-23 18:00:34 +000083 Result += std::string(",") + Buffer;
Chris Lattner2f7c9632001-06-06 20:29:01 +000084 }
85 }
Misha Brukman13f332c2005-04-21 21:10:11 +000086
Chris Lattner2f7c9632001-06-06 20:29:01 +000087 return Result + ": " + Message;
88}