36c9712b9d52974376a222f16e77efdbcee0baaa
[oota-llvm.git] / lib / AsmParser / Parser.cpp
1 //===- Parser.cpp - Main dispatch module for the Parser library -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This library implements the functionality defined in llvm/assembly/parser.h
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Assembly/Parser.h"
15 #include "LLParser.h"
16 #include "llvm/Module.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include <cstring>
20 using namespace llvm;
21
22 Module *llvm::ParseAssemblyFile(const std::string &Filename, ParseError &Err) {
23   Err.setFilename(Filename);
24   
25   std::string ErrorStr;
26   MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr);
27   if (F == 0) {
28     Err.setError("Could not open input file '" + Filename + "'");
29     return 0;
30   }
31   
32   Module *Result = LLParser(F, Err).Run();
33   delete F;
34   return Result;
35 }
36
37 // FIXME: M is ignored??
38 Module *llvm::ParseAssemblyString(const char *AsmString, Module *M, 
39                                   ParseError &Err) {
40   Err.setFilename("<string>");
41
42   MemoryBuffer *F = MemoryBuffer::getMemBuffer(AsmString, 
43                                                AsmString+strlen(AsmString),
44                                                "<string>");
45   Module *Result = LLParser(F, Err).Run();
46   delete F;
47   return Result;
48 }
49
50
51 //===------------------------------------------------------------------------===
52 //                              ParseError Class
53 //===------------------------------------------------------------------------===
54
55 void ParseError::PrintError(const char *ProgName, raw_ostream &S) {
56   errs() << ProgName << ": ";
57   if (Filename == "-")
58     errs() << "<stdin>";
59   else
60     errs() << Filename;
61   
62   if (LineNo != -1) {
63     errs() << ':' << LineNo;
64     if (ColumnNo != -1)
65       errs() << ':' << (ColumnNo+1);
66   }
67   
68   errs() << ": " << Message << '\n';
69   
70   if (LineNo != -1 && ColumnNo != -1) {
71     errs() << LineContents << '\n';
72     
73     // Print out spaces/tabs before the caret.
74     for (unsigned i = 0; i != unsigned(ColumnNo); ++i)
75       errs() << (LineContents[i] == '\t' ? '\t' : ' ');
76     errs() << "^\n";
77   }
78 }
79