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, 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: 00112 TextProvider(const llvm::sys::Path& path); 00113 00123 TextProvider(const char *buffer, size_t size); 00124 00129 TextProvider(const std::string &string); 00130 00131 ~TextProvider(); 00132 00141 Location getLocation(const TextIterator &ti) const; 00142 00145 SourceLocation getSourceLocation(const TextIterator &ti) const; 00146 00149 SourceLocation getSourceLocation(const Location loc) const; 00150 00153 unsigned getLine(Location) const; 00154 00156 unsigned getColumn(Location) const; 00157 00159 unsigned getLine(const TextIterator &ti) const { 00160 return getLine(getLocation(ti)); 00161 } 00162 00164 unsigned getColumn(const TextIterator &ti) const { 00165 return getColumn(getLocation(ti)); 00166 } 00167 00170 TextIterator begin() const; 00171 00173 TextIterator end() const; 00174 00184 std::string extract(Location start, Location end) const; 00185 00195 std::string extract(const TextIterator &iter, 00196 const TextIterator &endIter) const; 00197 00220 unsigned extract(const TextIterator &iter, 00221 const TextIterator &endIter, 00222 char *buffer, size_t size) const; 00223 00224 00225 private: 00226 friend class TextIterator; 00227 00228 // Disallow copy and assignment. 00229 TextProvider(const TextProvider&); 00230 TextProvider &operator=(const TextProvider&); 00231 00234 unsigned indexOf(const char *ptr) const { 00235 return ptr - buffer; 00236 } 00237 00240 std::pair<unsigned, unsigned> getLineOf(Location loc) const; 00241 00244 std::string identity; 00245 00247 llvm::MemoryBuffer *memBuffer; 00248 00250 const char *buffer; 00251 00258 mutable std::vector<unsigned> lines; 00259 00261 void initializeLinevec(); 00262 00267 mutable unsigned maxLineIndex; 00268 }; 00269 00270 } // End comma namespace. 00271 00272 #endif 00273 00274 00275