1 //===- Parser.cpp - Main dispatch module for the Parser library -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This library implements the functionality defined in llvm/Assembly/Parser.h
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Assembly/Parser.h"
16 #include "llvm/Module.h"
17 #include "llvm/ADT/OwningPtr.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/raw_ostream.h"
23 Module
*llvm::ParseAssemblyFile(const std::string
&Filename
, ParseError
&Err
) {
24 Err
.setFilename(Filename
);
27 OwningPtr
<MemoryBuffer
>
28 F(MemoryBuffer::getFileOrSTDIN(Filename
.c_str(), &ErrorStr
));
30 Err
.setError("Could not open input file '" + Filename
+ "'");
34 OwningPtr
<Module
> M(new Module(Filename
));
35 if (LLParser(F
.get(), Err
, M
.get()).Run())
40 Module
*llvm::ParseAssemblyString(const char *AsmString
, Module
*M
,
42 Err
.setFilename("<string>");
44 OwningPtr
<MemoryBuffer
>
45 F(MemoryBuffer::getMemBuffer(AsmString
, AsmString
+strlen(AsmString
),
48 // If we are parsing into an existing module, do it.
50 return LLParser(F
.get(), Err
, M
).Run() ? 0 : M
;
52 // Otherwise create a new module.
53 OwningPtr
<Module
> M2(new Module("<string>"));
54 if (LLParser(F
.get(), Err
, M2
.get()).Run())
60 //===------------------------------------------------------------------------===
62 //===------------------------------------------------------------------------===
64 void ParseError::PrintError(const char *ProgName
, raw_ostream
&S
) {
65 errs() << ProgName
<< ": ";
72 errs() << ':' << LineNo
;
74 errs() << ':' << (ColumnNo
+1);
77 errs() << ": " << Message
<< '\n';
79 if (LineNo
!= -1 && ColumnNo
!= -1) {
80 errs() << LineContents
<< '\n';
82 // Print out spaces/tabs before the caret.
83 for (unsigned i
= 0; i
!= unsigned(ColumnNo
); ++i
)
84 errs() << (LineContents
[i
] == '\t' ? '\t' : ' ');