Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame^] | 1 | //===- Parser.cpp - Main dispatch module for the Parser library -------------=== |
| 2 | // |
| 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 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This library implements the functionality defined in llvm/assembly/parser.h |
| 11 | // |
| 12 | //===------------------------------------------------------------------------=== |
| 13 | |
| 14 | #include "ParserInternals.h" |
| 15 | #include "llvm/Module.h" |
| 16 | using namespace llvm; |
| 17 | |
| 18 | |
| 19 | ParseError* TheParseError = 0; /// FIXME: Not threading friendly |
| 20 | |
| 21 | Module *llvm::ParseAssemblyFile(const std::string &Filename, ParseError* Err) { |
| 22 | FILE *F = stdin; |
| 23 | |
| 24 | if (Filename != "-") { |
| 25 | F = fopen(Filename.c_str(), "r"); |
| 26 | |
| 27 | if (F == 0) { |
| 28 | if (Err) |
| 29 | Err->setError(Filename,"Could not open file '" + Filename + "'"); |
| 30 | return 0; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | TheParseError = Err; |
| 35 | Module *Result = RunVMAsmParser(Filename, F); |
| 36 | |
| 37 | if (F != stdin) |
| 38 | fclose(F); |
| 39 | |
| 40 | return Result; |
| 41 | } |
| 42 | |
| 43 | Module *llvm::ParseAssemblyString( |
| 44 | const char * AsmString, Module * M, ParseError* Err) |
| 45 | { |
| 46 | TheParseError = Err; |
| 47 | return RunVMAsmParser(AsmString, M); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | //===------------------------------------------------------------------------=== |
| 52 | // ParseError Class |
| 53 | //===------------------------------------------------------------------------=== |
| 54 | |
| 55 | |
| 56 | void ParseError::setError(const std::string &filename, |
| 57 | const std::string &message, |
| 58 | int lineNo, int colNo) |
| 59 | { |
| 60 | Filename = filename; |
| 61 | Message = message; |
| 62 | LineNo = lineNo; |
| 63 | colNo = colNo; |
| 64 | } |
| 65 | |
| 66 | ParseError::ParseError(const ParseError &E) |
| 67 | : Filename(E.Filename), Message(E.Message) { |
| 68 | LineNo = E.LineNo; |
| 69 | ColumnNo = E.ColumnNo; |
| 70 | } |
| 71 | |
| 72 | // Includes info from options |
| 73 | const std::string ParseError::getMessage() const { |
| 74 | std::string Result; |
| 75 | char Buffer[10]; |
| 76 | |
| 77 | if (Filename == "-") |
| 78 | Result += "<stdin>"; |
| 79 | else |
| 80 | Result += Filename; |
| 81 | |
| 82 | if (LineNo != -1) { |
| 83 | sprintf(Buffer, "%d", LineNo); |
| 84 | Result += std::string(":") + Buffer; |
| 85 | if (ColumnNo != -1) { |
| 86 | sprintf(Buffer, "%d", ColumnNo); |
| 87 | Result += std::string(",") + Buffer; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return Result + ": " + Message; |
| 92 | } |