1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
/*
Copyright 2006 Hamish Rodda <rodda@kde.org>
Copyright 2008-2009 David Nolden <david.nolden.kdevelop@art-master.de>
Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
KDEVELOP TEAM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef STREAM_H
#define STREAM_H
#include <cppparserexport.h>
#include <QtCore/QIODevice>
#include <language/editor/cursorinrevision.h>
#include <language/duchain/indexedstring.h>
#include "anchor.h"
#include "chartools.h"
typedef QVector<unsigned int> PreprocessedContents;
namespace KDevelop {
class IndexedString;
}
namespace rpp {
class LocationTable;
/**
* Stream designed for character-at-a-time processing
*
* @author Hamish Rodda <rodda@kde.org>
*/
class KDEVCPPRPP_EXPORT Stream
{
static const uint newline;
public:
Stream();
//If the given offset anchor has the member "collapsed" set to true, the position will be locked.
explicit Stream( const uint * string, uint stringSize, const Anchor& offset = Anchor(0,0), LocationTable* table = 0 );
explicit Stream( PreprocessedContents * string, const Anchor& offset = Anchor(0,0), LocationTable* table = 0 );
explicit Stream( PreprocessedContents * string, LocationTable* table );
virtual ~Stream();
bool isNull() const;
inline bool atEnd() const
{
return c == end;
}
void toEnd();
/// Returns true if toEnd was called on this stream.
bool skippedToEnd() const;
int offset() const;
const uint& peek(uint offset = 1) const;
char peekNextCharacter() const {
const unsigned int* next = c+1;
if (next >= end || !isCharacter(*next))
return (char)0;
return characterFromIndex(*next);
}
//Slow, just for debugging
QByteArray stringFrom(int offset) const;
/// \warning the input and output lines are not updated when calling this function.
/// if you're seek()ing over a line boundary, you'll need to fix the line and column
/// numbers.
void seek(int offset);
/// Start from the beginning again
void reset();
/// Lock/unlock the input position. If the input position is locked, it will not be moved forwards.
void lockInputPosition(bool lock);
/// If a macro-expansion is set, all anchors given to mark() will get that macro-expansion set.
/// It marks the position from where the macro-expansion was started that leads to the current output @see rpp::Anchor
void setMacroExpansion(const KDevelop::CursorInRevision&);
KDevelop::CursorInRevision macroExpansion() const;
//Checks whether the current index represents a character, and eventually compares it
bool operator==(const char otherc) const {
return !atEnd() && isCharacter(*c) && *c == indexFromCharacter(otherc);
}
//Checks whether the current index represents a character, and eventually compares it
bool operator!=(const char otherc) const {
return atEnd() || !isCharacter(*c) || *c != indexFromCharacter(otherc);
}
inline const uint& current() const { return *c; } //krazy:exclude=inline
inline operator const uint&() const { return *c; } //krazy:exclude=inline
inline Stream& operator++() //krazy:exclude=inline
{
if (c == end)
return *this;
if(m_inputPositionLocked)
++m_inputLineStartedAt;
else if (*c == newline) {
++m_inputLine;
m_inputLineStartedAt = m_pos + 1;
}else if(!isCharacter(*c)) {
//We have to do some special hacking here to keep the column-number correct.
m_inputLineStartedAt += 1-KDevelop::IndexedString::lengthFromIndex(*c);
}
++c;
++m_pos;
return *this;
}
///@warning Doesn't handle newlines correctly!
Stream& operator--();
///Removes the last added item from the output, and returns it
uint popLastOutput();
///Allows peeking at the items that were added to the output recently
uint peekLastOutput(uint backOffset = 0) const;
///Returns the cursor that points to the current input position.
Anchor inputPosition() const;
///If the input position is collapsed, the input position will be locked from now on. It will stay the same until a new one is set.
void setInputPosition(const Anchor& position);
///Input-position that marks the start of the topmost currently expanding macro in the original document
KDevelop::CursorInRevision originalInputPosition() const;
void setOriginalInputPosition(const KDevelop::CursorInRevision& position);
///Used for output streams to mark stream positions with input position
///The macroExpansion member may have no effect if macroExpansion is set for this stream.
void mark(const Anchor& position);
///Returns the anchor for the current output position.
Anchor currentOutputAnchor() const;
Stream & operator<< ( const char& c ) {
return operator<<(indexFromCharacter(c));
}
Stream & operator<< ( const unsigned int& c );
Stream & operator<< ( const Stream& input );
Stream& appendString( const Anchor& inputPosition, const PreprocessedContents & string );
Stream& appendString( const Anchor& inputPosition, const KDevelop::IndexedString& string );
const PreprocessedContents* source() const {
return m_string;
}
private:
Q_DISABLE_COPY(Stream)
PreprocessedContents* m_string;
const unsigned int* c;
const unsigned int* end;
bool m_isNull, m_skippedToEnd, m_inputPositionLocked, m_onwsString;
KDevelop::CursorInRevision m_macroExpansion;
int m_pos;
int m_inputLine;
int m_inputLineStartedAt;
LocationTable* m_locationTable;
KDevelop::CursorInRevision m_originalInputPosition;
};
}
#endif
|