00001 //===-- basic/TextProvider.h ---------------------------------- -*- C++ -*-===// 00002 // 00003 // This file is distributed under the MIT license. See LICENSE.txt for details. 00004 // 00005 // Copyright (C) 2008-2009, Stephen Wilson 00006 // 00007 //===----------------------------------------------------------------------===// 00008 00009 #ifndef COMMA_BASIC_TEXTPROVIDER_HDR_GUARD 00010 #define COMMA_BASIC_TEXTPROVIDER_HDR_GUARD 00011 00012 #include "comma/basic/Location.h" 00013 #include "llvm/System/Path.h" 00014 #include "llvm/Support/MemoryBuffer.h" 00015 #include <string> 00016 #include <vector> 00017 00018 namespace comma { 00019 00029 class TextIterator { 00030 public: 00031 00032 TextIterator(const TextIterator &ti) 00033 : cursor(ti.cursor) 00034 { } 00035 00036 TextIterator &operator=(const TextIterator &ti) { 00037 cursor = ti.cursor; 00038 return *this; 00039 } 00040 00041 bool operator==(const TextIterator &ti) const { 00042 return cursor == ti.cursor; 00043 } 00044 00045 bool operator!=(const TextIterator &ti) const { 00046 return cursor != ti.cursor; 00047 } 00048 00049 TextIterator &operator++() { 00050 ++cursor; 00051 return *this; 00052 } 00053 00054 TextIterator operator++(int) { 00055 TextIterator prev(*this); 00056 ++cursor; 00057 return prev; 00058 } 00059 00060 TextIterator &operator--() { 00061 --cursor; 00062 return *this; 00063 } 00064 00065 TextIterator operator--(int) { 00066 TextIterator copy(*this); 00067 --cursor; 00068 return copy; 00069 } 00070 00071 unsigned operator*() const { 00072 return *cursor; 00073 } 00074 00075 const char *operator&() const { 00076 return cursor; 00077 } 00078 00079 private: 00080 friend class TextProvider; 00081 00083 TextIterator(const char *c) : cursor(c) { } 00084 00085 const char *cursor; 00086 }; 00087 00102 class TextProvider { 00103 00104 public: 00113 TextProvider(const llvm::sys::Path& path); 00114 00124 TextProvider(const char *buffer, size_t size); 00125 00130 TextProvider(const std::string &string); 00131 00132 ~TextProvider(); 00133 00140 std::string getIdentity() const { return identity; } 00141 00150 Location getLocation(const TextIterator &ti) const; 00151 00154 SourceLocation getSourceLocation(const TextIterator &ti) const; 00155 00158 SourceLocation getSourceLocation(const Location loc) const; 00159 00162 unsigned getLine(Location) const; 00163 00165 unsigned getColumn(Location) const; 00166 00168 unsigned getLine(const TextIterator &ti) const { 00169 return getLine(getLocation(ti)); 00170 } 00171 00173 unsigned getColumn(const TextIterator &ti) const { 00174 return getColumn(getLocation(ti)); 00175 } 00176 00179 TextIterator begin() const; 00180 00182 TextIterator end() const; 00183 00193 std::string extract(Location start, Location end) const; 00194 00204 std::string extract(const TextIterator &iter, 00205 const TextIterator &endIter) const; 00206 00211 std::string extract(const SourceLocation &sloc) const; 00212 00235 unsigned extract(const TextIterator &iter, 00236 const TextIterator &endIter, 00237 char *buffer, size_t size) const; 00238 00239 private: 00240 friend class TextIterator; 00241 00242 // Disallow copy and assignment. 00243 TextProvider(const TextProvider&); 00244 TextProvider &operator=(const TextProvider&); 00245 00248 unsigned indexOf(const char *ptr) const { 00249 return ptr - buffer; 00250 } 00251 00254 std::pair<unsigned, unsigned> getLineOf(Location loc) const; 00255 00258 std::string identity; 00259 00261 llvm::MemoryBuffer *memBuffer; 00262 00264 const char *buffer; 00265 00272 mutable std::vector<unsigned> lines; 00273 00275 void initializeLinevec(); 00276 00281 mutable unsigned maxLineIndex; 00282 }; 00283 00284 } // End comma namespace. 00285 00286 #endif 00287 00288 00289