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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
/*
SuperCollider Qt IDE
Copyright (c) 2012 Jakob Leben & Tim Blechmann
http://www.audiosynth.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#pragma once
#include <QTextBlock>
#include <vector>
namespace ScIDE {
struct Token {
enum Type {
Unknown = 0,
WhiteSpace,
Keyword,
Builtin,
Primitive,
Name,
Class,
Symbol,
SymbolMark,
StringMark,
Char,
RadixFloat,
ScaleDegreeFloat,
Float,
HexInt,
EnvVar,
SymbolArg,
SingleLineComment,
MultiLineCommentStart,
MultiLineCommentEnd,
OpeningBracket,
ClosingBracket,
Operator
};
Token(Type t, int pos, int len = 0, char c = 0): type(t), positionInBlock(pos), length(len), character(c) {}
Token(Token const& rhs):
type(rhs.type),
positionInBlock(rhs.positionInBlock),
length(rhs.length),
character(rhs.character) {}
Type type;
int positionInBlock;
int length;
char character;
};
struct TextBlockData : public QTextBlockUserData {
std::vector<Token> tokens;
};
class TokenIterator {
/*
TokenIterator allows easy iteration over tokens in a Document (produced by the syntax highlighter).
An iterator is valid as long as it refers to an existing token.
An invalid iterator must not be dereferenced, incremented or decremented.
Incrementing and decrementing an iterator makes it refer to the next and previous token
in the document, respectively, regardless of which block the tokens reside in.
If there is no next/previous token, then incrementing/decrementing an iterator will make it invalid.
*/
private:
QTextBlock blk;
int idx;
TextBlockData* data;
public:
TokenIterator(): idx(-1) {}
bool isValid() const { return idx >= 0; }
const QTextBlock& block() const { return blk; }
bool operator==(TokenIterator& other) const { return blk == other.blk && idx == other.idx; }
const Token& operator*() {
Q_ASSERT(blk.isValid());
Q_ASSERT(idx >= 0);
Q_ASSERT(data);
return data->tokens[idx];
}
const Token* operator->() const {
Q_ASSERT(blk.isValid());
Q_ASSERT(idx >= 0);
Q_ASSERT(data);
return &data->tokens[idx];
}
// Constructs an iterator for the first token at the given block.
// If there's no token in the block, the new iterator is invalid.
TokenIterator(const QTextBlock& block): blk(block), idx(-1) {
if (block.isValid()) {
data = static_cast<TextBlockData*>(block.userData());
if (data && !data->tokens.empty())
idx = 0;
}
}
// Constructs an iterator for the token that starts at, or contains the given position.
// If there is no such token, the new iterator is invalid.
TokenIterator(const QTextBlock& block, int positionInBlock): blk(block) {
if (block.isValid()) {
data = static_cast<TextBlockData*>(block.userData());
idx = data ? data->tokens.size() : 0;
while (idx--) {
const Token& token = data->tokens[idx];
if (token.positionInBlock > positionInBlock)
continue;
else if (token.positionInBlock == positionInBlock
|| token.positionInBlock + token.length > positionInBlock)
break;
}
} else
idx = -1;
}
// Return an iterator for the first token found left of given position, anywhere in the document.
// If there is no such token, the returned iterator is invalid.
static TokenIterator leftOf(const QTextBlock& block, int positionInBlock) {
TokenIterator it;
it.blk = block;
it.idx = -1;
while (it.blk.isValid()) {
it.data = static_cast<TextBlockData*>(it.blk.userData());
it.idx = it.data ? it.data->tokens.size() : 0;
while (it.idx--) {
Token const& token = it.data->tokens[it.idx];
if (positionInBlock < 0 || token.positionInBlock < positionInBlock)
return it;
}
positionInBlock = -1; // match on first token in next block;
it.blk = it.blk.previous();
}
return it;
}
// Return an iterator for the first token found at or right of given position, anywhere in the document.
// If there is no such token, the returned iterator is invalid.
static TokenIterator rightOf(const QTextBlock& block, int positionInBlock) {
TokenIterator it;
it.blk = block;
it.idx = -1;
while (it.blk.isValid()) {
it.data = static_cast<TextBlockData*>(it.blk.userData());
int n = it.data ? it.data->tokens.size() : 0;
while (++it.idx < n) {
Token const& token = it.data->tokens[it.idx];
if (token.positionInBlock >= positionInBlock)
return it;
}
it.idx = -1;
positionInBlock = -1; // match right on first token in next block;
it.blk = it.blk.next();
}
return it;
}
// Return an iterator for the token at 'pos' or 'pos-1'.
// If there is no such token, the returned iterator is invalid.
static TokenIterator around(const QTextBlock& block, int positionInBlock) {
TokenIterator it;
it.blk = block;
it.idx = -1;
if (!block.isValid())
return it;
it.data = static_cast<TextBlockData*>(block.userData());
if (!it.data)
return it;
int n = it.data->tokens.size();
for (int i = 0; i < n; ++i) {
Token const& token = it.data->tokens[i];
if (token.positionInBlock > positionInBlock) {
return it;
} else if (token.positionInBlock == positionInBlock - 1 || token.positionInBlock == positionInBlock) {
it.idx = i;
break;
}
}
return it;
}
TokenIterator& operator++() {
if (idx < 0)
return *this;
do {
if (idx < 0)
data = static_cast<TextBlockData*>(blk.userData());
if (data) {
int n = data->tokens.size();
if (++idx < n)
return *this;
}
idx = -1;
blk = blk.next();
} while (blk.isValid());
return *this;
}
TokenIterator& operator--() {
if (idx > 0) {
--idx;
return *this;
} else if (idx < 0) {
return *this;
}
idx = -1;
while ((blk = blk.previous()).isValid()) {
data = static_cast<TextBlockData*>(blk.userData());
if (data)
idx = data->tokens.size() - 1;
if (idx < 0)
continue;
// we have a valid idx
break;
}
return *this;
}
TokenIterator previous() const {
TokenIterator it(*this);
--it;
return it;
}
TokenIterator next() const {
TokenIterator it(*this);
++it;
return it;
}
char character() const { return (*this)->character; }
// A convenience function returning the global position in document of the token
// referred to by this iterator
int position() const { return (*this)->positionInBlock + blk.position(); }
// A convenience function returning the type of token referred to by this iterator,
// or Token::Unknown if the iterator is invalid
Token::Type type() const { return isValid() ? (*this)->type : Token::Unknown; }
};
} // namespace ScIDE
|