LLVM API Documentation
00001 //===- LineIterator.h - Iterator to read a text buffer's lines --*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #ifndef LLVM_SUPPORT_LINEITERATOR_H 00011 #define LLVM_SUPPORT_LINEITERATOR_H 00012 00013 #include "llvm/ADT/StringRef.h" 00014 #include "llvm/Support/DataTypes.h" 00015 #include <iterator> 00016 00017 namespace llvm { 00018 00019 class MemoryBuffer; 00020 00021 /// \brief A forward iterator which reads text lines from a buffer. 00022 /// 00023 /// This class provides a forward iterator interface for reading one line at 00024 /// a time from a buffer. When default constructed the iterator will be the 00025 /// "end" iterator. 00026 /// 00027 /// The iterator is aware of what line number it is currently processing. It 00028 /// strips blank lines by default, and comment lines given a comment-starting 00029 /// character. 00030 /// 00031 /// Note that this iterator requires the buffer to be nul terminated. 00032 class line_iterator 00033 : public std::iterator<std::forward_iterator_tag, StringRef> { 00034 const MemoryBuffer *Buffer; 00035 char CommentMarker; 00036 bool SkipBlanks; 00037 00038 unsigned LineNumber; 00039 StringRef CurrentLine; 00040 00041 public: 00042 /// \brief Default construct an "end" iterator. 00043 line_iterator() : Buffer(nullptr) {} 00044 00045 /// \brief Construct a new iterator around some memory buffer. 00046 explicit line_iterator(const MemoryBuffer &Buffer, bool SkipBlanks = true, 00047 char CommentMarker = '\0'); 00048 00049 /// \brief Return true if we've reached EOF or are an "end" iterator. 00050 bool is_at_eof() const { return !Buffer; } 00051 00052 /// \brief Return true if we're an "end" iterator or have reached EOF. 00053 bool is_at_end() const { return is_at_eof(); } 00054 00055 /// \brief Return the current line number. May return any number at EOF. 00056 int64_t line_number() const { return LineNumber; } 00057 00058 /// \brief Advance to the next (non-empty, non-comment) line. 00059 line_iterator &operator++() { 00060 advance(); 00061 return *this; 00062 } 00063 line_iterator operator++(int) { 00064 line_iterator tmp(*this); 00065 advance(); 00066 return tmp; 00067 } 00068 00069 /// \brief Get the current line as a \c StringRef. 00070 StringRef operator*() const { return CurrentLine; } 00071 const StringRef *operator->() const { return &CurrentLine; } 00072 00073 friend bool operator==(const line_iterator &LHS, const line_iterator &RHS) { 00074 return LHS.Buffer == RHS.Buffer && 00075 LHS.CurrentLine.begin() == RHS.CurrentLine.begin(); 00076 } 00077 00078 friend bool operator!=(const line_iterator &LHS, const line_iterator &RHS) { 00079 return !(LHS == RHS); 00080 } 00081 00082 private: 00083 /// \brief Advance the iterator to the next line. 00084 void advance(); 00085 }; 00086 } 00087 00088 #endif