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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
|
#ifndef TOKEN_H
#define TOKEN_H
#include "common/utils.h"
#include <QString>
#include <QList>
#include <QSharedPointer>
/** @file */
/**
* @def TOKEN_TYPE_MASK_DB_OBJECT
*
* Bit mask used to test Token::Type for representing any database object name, in any form.
* It's used in Token::isDbObjectType().
*/
#define TOKEN_TYPE_MASK_DB_OBJECT 0x1000
struct Token;
/**
* @brief Shared pointer to the Token.
*/
typedef QSharedPointer<Token> TokenPtr;
/**
* @brief SQL query entity representing isolated part of the query.
*
* Tokens are generated by Lexer. Each token represents isolated part of the query,
* like a name, a number, an operator, a string, a keyword, or a comment, etc.
*
* In other words, tokenizing SQL query is splitting it into logical parts.
*
* Each token has a type, a value and position indexes of where it starts and where it ends (in the query string).
*
* Tokens are used mainly by the Parser to perform syntax analysis, but they can be also very helpful
* in other areas. They provide easy way for safe manipulation on the query string, without worrying
* about counting open or close characters of the string, etc. If the string has a single-quote used twice inside,
* this is still a regular SQL string and in that case there will be only a single string token representing it.
*
* If you're constructing Token outside of the Lemon parser, you should be interested only in 4 constructors:
* Token(), Token(QString value), Token(Type type, QString value)
* and Token(Type type, QString value, qint64 start, qint64 end).
* Rest of the constructors are to be used from the Lemon parser, as they require Lemon token ID to be provided.
*
* You will usually have the most to do with tokens when dealing with SqliteStatement and its 2 members:
* SqliteStatement::tokens and SqliteStatement::tokenMap.
*/
struct API_EXPORT Token
{
/**
* @brief Token type.
*
* There are 2 kind of types - regular and context-oriented.
*
* Regular types are those defined by the SQL grammar and they represent real tokens. A special token type
* from group of regular types is the Type::INVALID, which means, that the character(s) encountered
* by the Lexer are invalid in SQL syntax understanding, or when there was no more query characters to read
* (which in this case means that tokenizing ended before this token).
*
* The context-oriented types are special meta types used by the Parser to probe potential candidates
* for next valid token when Parser::getNextTokenCandidates() is called. They are then processed by
* CompletionHelper. You won't deal with this kind of token types on regular basis. Context-oriented
* types are those starting with <tt>CTX_</tt>.
*/
enum Type
{
INVALID = 0x0001, /**< Invalid token, or no more tokens available from Lexer. */
OTHER = 0x1002, /**< A name, a word. */
STRING = 0x0003, /**< A string (value will be stripped of the surrounding quotes). */
COMMENT = 0x0004, /**< A comment, including starting/ending markers. */
FLOAT = 0x0005, /**< A decimal number. */
INTEGER = 0x0006, /**< An integer number. */
BIND_PARAM = 0x0007, /**< A bind parameter (<tt>:param</tt>, <tt>\@param</tt>, or <tt>?</tt>). */
OPERATOR = 0x0008, /**< An operator (like <tt>";"</tt>, <tt>"+"</tt>, <tt>","</tt>, etc). */
PAR_LEFT = 0x0009, /**< A left parenthesis (<tt>"("</tt>). */
PAR_RIGHT = 0x0010, /**< A right parenthesis (<tt>")"</tt>). */
SPACE = 0x0011, /**< White space(s), including new line characters and tabs. */
BLOB = 0x0012, /**< Literal BLOB value (<tt>X'...'</tt> or <tt>x'...'</tt>). */
KEYWORD = 0x0013, /**< A keyword (see getKeywords3() and getKeywords2()). */
CTX_COLUMN = 0x1014, /**< Existing column name is valid at this token position. */
CTX_TABLE = 0x1015, /**< Existing table name is valid at this token potision. */
CTX_DATABASE = 0x1016, /**< Database name is valid at this token position. */
CTX_FUNCTION = 0x0017, /**< Function name is valid at this token position. */
CTX_COLLATION = 0x0018, /**< Collation name is valid at this token position. */
CTX_INDEX = 0x1019, /**< Existing index name is valid at this token position. */
CTX_TRIGGER = 0x1020, /**< Existing trigger name is valid at this token position. */
CTX_VIEW = 0x1021, /**< View name is valid at this token position. */
CTX_JOIN_OPTS = 0x0022, /**< JOIN keywords are valid at this token position (see getJoinKeywords()). */
CTX_TABLE_NEW = 0x0023, /**< Name for new table is valid at this token position. */
CTX_INDEX_NEW = 0x0024, /**< Name for new index is valid at this token position. */
CTX_VIEW_NEW = 0x0025, /**< Name for new view is valid at this token position. */
CTX_TRIGGER_NEW = 0x0026, /**< Name for new trigger is valid at this token position. */
CTX_ALIAS = 0x0027, /**< Alias name is valid at this token position. */
CTX_TRANSACTION = 0x0028, /**< Transaction name is valid at this token position. */
CTX_COLUMN_NEW = 0x0029, /**< Name for the new column is valid at this token position. */
CTX_COLUMN_TYPE = 0x0030, /**< Data type for the new column is valid at this token position. */
CTX_CONSTRAINT = 0x0031, /**< Name for the new constraint is valid at this token position. */
CTX_FK_MATCH = 0x0032, /**< MATCH keywords are valid at this token position (see getFkMatchKeywords()). */
CTX_PRAGMA = 0x0033, /**< Pragma name is valid at this token position. */
CTX_ROWID_KW = 0x0034, /**< ROWID keywords is valid at this token position (see isRowIdKeyword()). */
CTX_NEW_KW = 0x0035, /**< The <tt>NEW</tt> keyword is valid at this token position. */
CTX_OLD_KW = 0x0036, /**< The <tt>OLD</tt> keyword is valid at this token position. */
CTX_ERROR_MESSAGE = 0x0037, /**< Error message string is valid at this token position. */
CTX_STRICT_KW = 0x0038, /**< STRICT keyword is valid at this token position. */
};
/**
* @brief Creates empty token with type Type::INVALID.
*
* Lemon token ID is set to 0 and start/end positions are set to -1.
*/
Token();
/**
* @brief Creates fully defined token.
* @param lemonType Lemon token ID to use (see sqlite3_parser.h).
* @param type Token type.
* @param value Value of the token.
* @param start Start position of the token (index of the first character in the query).
* @param end End position of the token (index of last character in the query).
*
* This constructor is intended to be used from Lemon parser only. For other use cases
* use constructors without the \p lemonType parameter, unless you need it and you know what you're doing.
*/
Token(int lemonType, Type type, QString value, qint64 start, qint64 end);
/**
* @overload
*/
Token(int lemonType, Type type, QChar value, qint64 start, qint64 end);
/**
* @overload
*/
Token(int lemonType, Type type, QString value);
/**
* @brief Creates token with type Type::INVALID and given value.
* @param value Value for the token.
*
* Start/end positions are set to -1.
*/
explicit Token(QString value);
/**
* @brief Creates token of given type and with given value.
* @param type Type for the token.
* @param value Value for the token.
*
* Start/end positions are set to -1.
*/
Token(Type type, QString value);
/**
* @brief Creates fully defined token.
* @param type Type of the token.
* @param value Value for the token.
* @param start Start position of the token (index of the first character in the query).
* @param end End position of the token (index of last character in the query).
*/
Token(Type type, QString value, qint64 start, qint64 end);
/**
* @brief Destructor declared as virtual. Does nothing in this implementation.
*/
virtual ~Token();
/**
* @brief Serializes token to human readable form.
* @return Token values in format: <tt>{type value start end}</tt>
*/
QString toString();
/**
* @brief Converts given token type into its string representation.
* @param type Type to convert.
* @return Type as a string (same as textual representation of the enum in the code).
*/
static const QString typeToString(Type type);
/**
* @brief Provides range of the token in the query.
* @return Start and end character index in relation to the query it comes from.
*/
Range getRange();
/**
* @brief Tests whether this token represents any kind of whitespace.
* @return true if it's a whitespace, or false otherwise.
*
* Note, that from SQL perspective also comments are whitespaces.
*/
bool isWhitespace(bool includeComments = true) const;
/**
* @brief Tests whether this token represents separating value (like an operator, or parenthesis) in SQL understanding.
* @return true if it's a separating token, or false otherwise.
*/
bool isSeparating() const;
/**
* @brief Tests whether the token represents meaningful type.
* @return true if token is meaningful, like a word, object, string, number, etc. Not comment, not whitespace, not context marker.
*/
bool isMeaningful() const;
/**
* @brief Tests whether this token is representing any kind of database object name.
* @return true if the token is the name an object, or false otherwise.
*
* From regular token types only the Type::OTHER represents.
* For context-oriented types there are several types representing object name.
* Use this method to find out which is and which is not.
*
* You won't need to use this method in most cases. It's useful only to CompletionHelper
* for now.
*/
bool isDbObjectType() const;
/**
* @brief Converts token's type into a string representation.
* @return Token type as a string.
*/
QString typeString() const;
/**
* @brief Compares other token to this token.
* @param other Other token to compare.
* @return 1 if tokens are equal, 0 if they're different.
*
* Tokens are equal then 4 members are equal: type, value, start and end.
* The lemonType member is ignored by this operator.
*/
int operator==(const Token& other);
/**
* @brief Compares other token to this token and tells which one is greater.
* @param other Other token to compare.
* @return true if the other token is greater than this one, or false if it's smaller or equal.
*
* This operator compares only 2 members: the start and the end indexes. This operator
* is used to sort tokens by the character position they occurred at.
*
* The start value has higher precedence than the end value, but if start values are equal,
* then the end value is conclusive.
*/
bool operator<(const Token& other) const;
/**
* @brief Lemon token ID. Used by the Parser class only.
*/
int lemonType;
/**
* @brief Token type, describing general class of the token.
*/
Type type;
/**
* @brief Literal value of the token, captured directly from the query.
*/
QString value;
/**
* @brief Start position (first character index) of the token in the query.
*/
qint64 start;
/**
* @brief End position (last character index) of the token in the query.
*/
qint64 end;
};
/**
* @brief qHash implementation for TokenPtr, so it can be used as a key in QHash and QSet.
* @param token Token that the hash code is calculated for.
* @return Unique integer value for the token.
*/
uint qHash(const TokenPtr& token);
struct TolerantToken;
/**
* @brief Shared pointer to TolerantToken.
*/
typedef QSharedPointer<TolerantToken> TolerantTokenPtr;
/**
* @brief Variation of token that has additional "invalid" flag.
*
* TolerantToken is used by Lexer to tolerate unfinished comments, like when you start the
* comment at the end of the query, but you never close the comment. This is tolerable case,
* while not entire correct by the syntax.
*
* In such cases the syntax highlighter must be aware of the token being invalid, so the proper
* state is marked for the paragraph.
*/
struct TolerantToken : public Token
{
/**
* @brief Invalid state flag for the token.
*/
bool invalid = false;
};
/**
* @brief Ordered list of tokens.
*
* This is pretty much a QList of Token pointers, but it also provides some
* utility methods regarding this collection, which is very common in SQLiteStudio.
*/
class API_EXPORT TokenList : public QList<TokenPtr>
{
public:
/**
* @brief Creates empty list.
*/
TokenList();
/**
* @brief Creates list filled with the same entries as the other list.
* @param other List to copy pointers from.
*/
TokenList(const QList<TokenPtr>& other);
/**
* @brief Serializes contents of the list into readable form.
* @return Contents in format: <tt>{type1 value1 start1 end1} {type2 value2 start2 end2} ...</tt>.
*
* Tokens are serialized with Token::toString(), then all serialized values are joined with single whitespace
* into the QString.
*/
QString toString() const;
/**
* @brief Serializes tokens from the list into strings.
* @return List of tokens serialized into strings.
*
* Tokens are serialized with Token::toString().
*/
QStringList toStringList() const;
/**
* @brief Converts list of tokens into list of their values.
* @return List of tokens values.
*/
QStringList toValueList() const;
/**
* @brief Provides index of first occurrence of the token in the list.
* @param token Token to look for.
* @return Index of the token, or -1 if token was not found.
*/
int indexOf(TokenPtr token) const;
/**
* @brief Provides index of first occurrence of the token with given type.
* @param type Toke type to look for.
* @return Index of the token, or -1 if token was not found.
*/
int indexOf(Token::Type type) const;
/**
* @brief Provides index of first occurrence of the token with given type and value.
* @param type Token type to look for.
* @param value Token value to look for.
* @param caseSensitivity Should value lookup be case sensitive?
* @return Index of the token, or -1 if token was not found.
*/
int indexOf(Token::Type type, const QString& value, Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive) const;
/**
* @brief Provides index of first occurrence of the token with given value.
* @param value Value to look for.
* @param caseSensitivity Should value lookup be case sensitive?
* @return Index of the token, or -1 if token was not found.
*/
int indexOf(const QString& value, Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive) const;
/**
* @brief Provides index of last occurrence of the token in the list.
* @param token Token to look for.
* @return Index of the token, or -1 if token was not found.
*/
int lastIndexOf(TokenPtr token) const;
/**
* @brief Provides index of last occurrence of the token with given type.
* @param type Token type to look for.
* @return Index of the token, or -1 if token was not found.
*/
int lastIndexOf(Token::Type type) const;
/**
* @brief Provides index of last occurrence of the token with given type and value.
* @param type Token type to look for.
* @param value Token value to look for.
* @param caseSensitivity Should value lookup be case sensitive?
* @return Index of the token, or -1 if token was not found.
*/
int lastIndexOf(Token::Type type, const QString& value, Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive) const;
/**
* @brief Provides index of last occurrence of the token with given value.
* @param value Value to look for.
* @param caseSensitivity Should value lookup be case sensitive?
* @return Index of the token, or -1 if token was not found.
*/
int lastIndexOf(const QString& value, Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive) const;
/**
* @brief Finds first token with given type in the list.
* @param type Type to look for.
* @return Token found, or null pointer if it was not found.
*/
TokenPtr find(Token::Type type) const;
/**
* @brief Finds first token with given type and value.
* @param type Type to look for.
* @param value Token value to look for.
* @param caseSensitivity Should value lookup be case sensitive?
* @return Token found, or null pointer if it was not found.
*/
TokenPtr find(Token::Type type, const QString& value, Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive) const;
/**
* @brief Finds first token with given type and value.
* @param value Token value to look for.
* @param caseSensitivity Should value lookup be case sensitive?
* @return Token found, or null pointer if it was not found.
*/
TokenPtr find(const QString& value, Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive) const;
/**
* @brief Finds last token with given type in the list.
* @param type Type to look for.
* @return Token found, or null pointer if it was not found.
*/
TokenPtr findLast(Token::Type type) const;
/**
* @brief Finds last token with given type and value.
* @param type Type to look for.
* @param value Token value to look for.
* @param caseSensitivity Should value lookup be case sensitive?
* @return Token found, or null pointer if it was not found.
*/
TokenPtr findLast(Token::Type type, const QString& value, Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive) const;
/**
* @brief Finds last token with given value.
* @param value Token value to look for.
* @param caseSensitivity Should value lookup be case sensitive?
* @return Token found, or null pointer if it was not found.
*/
TokenPtr findLast(const QString& value, Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive) const;
/**
* @brief Finds token that according to its start and end values covers given character position.
* @param cursorPosition Position to get token for.
* @return Token found, or null pointer if it was not found.
*/
TokenPtr atCursorPosition(quint64 cursorPosition) const;
/**
* @brief Inserts tokens at given position in this list.
* @param i Position to insert at.
* @param list List of tokens to insert.
*/
void insert(int i, const TokenList& list);
/**
* @brief Inserts a token at given position in this list.
* @param i Position to insert at.
* @param token Token to insert.
*/
void insert(int i, TokenPtr token);
/**
* @brief Puts all tokens from the other list to this list.
* @param other List to get tokens from.
* @return Reference to this list.
*
* It erases any previous entries in this list, just like you would normally expect for the <tt>=</tt> operator.
*/
TokenList& operator=(const QList<TokenPtr>& other);
/**
* @brief Detokenizes all tokens from this list.
* @return String from all tokens.
*
* This is just a convenient method to call Lexer::detokenize() on this list.
*/
QString detokenize() const;
/**
* @brief Replaces tokens on this list with other tokens.
* @param startIdx Position of the first token in this list to replace.
* @param length Number of tokens to replace.
* @param newTokens New tokens to put in place of removed ones.
*/
void replace(int startIdx, int length, const TokenList& newTokens);
/**
* @brief Replaces tokens on this list with another token.
* @param startIdx Position of the first token in this list to replace.
* @param length Number of tokens to replace.
* @param newToken New token to put in place of removed ones.
*/
void replace(int startIdx, int length, TokenPtr newToken);
/**
* @brief Replaces token with another token.
* @param startIdx Position of the token in this list to replace.
* @param newToken New token to put in place of removed one.
*/
void replace(int startIdx, TokenPtr newToken);
/**
* @brief Replaces token on this list with other tokens.
* @param startIdx Position of the token in this list to replace.
* @param newTokens New tokens to put in place of removed ones.
*/
void replace(int startIdx, const TokenList& newTokens);
/**
* @brief Replaces tokens on this list with other tokens.
* @param startToken First token to replace.
* @param endToken Last token to replace.
* @param newTokens New tokens to put in place of removed ones.
* @return Number of tokens replaced.
*
* If either \p startToken or \p endToken were not found on the list, this method does nothing
* and returns 0.
*/
int replace(TokenPtr startToken, TokenPtr endToken, const TokenList& newTokens);
/**
* @brief Replaces tokens on this list with other tokens.
* @param startToken First token to replace.
* @param endToken Last token to replace.
* @param newToken New token to put in place of removed ones.
* @return Number of tokens replaced.
*
* If either \p startToken or \p endToken were not found on the list, this method does nothing
* and returns 0.
*/
int replace(TokenPtr startToken, TokenPtr endToken, TokenPtr newToken);
/**
* @brief Replaces token on this list with another token.
* @param oldToken Token to replace.
* @param newToken Token to replace with.
* @return true if \p oldToken was found and replaced, or false otherwise.
*/
bool replace(TokenPtr oldToken, TokenPtr newToken);
/**
* @brief Replaces token on this list with other tokens.
* @param oldToken Token to replace.
* @param newTokens Tokens to replace with.
* @return true if \p oldToken was found and replaced, or false otherwise.
*/
bool replace(TokenPtr oldToken, const TokenList& newTokens);
/**
* @brief Removes tokens from the list.
* @param startToken First token to remove.
* @param endToken Last token to remove.
* @return true if \p startToken and \p endToken were found in the list and removed, or false otherwise.
*
* In case \p startToken is placed after \p endToken, this method does nothing and returns false.
*/
bool remove(TokenPtr startToken, TokenPtr endToken);
/**
* @brief Removes first token of given type from the list.
* @param type Token type to remove.
* @return true if token was located and removed, or false otherwise.
*/
bool remove(Token::Type type);
/**
* @brief Removes all white-space tokens from the beginning of the list.
*
* White-space tokens are tested with Token::isWhitespace().
*/
TokenList &trimLeft();
/**
* @brief Removes all white-space tokens from the end of the list.
*
* White-space tokens are tested with Token::isWhitespace().
*/
TokenList &trimRight();
/**
* @brief Removes all white-space tokens from both the beginning and the end of the list.
*
* White-space tokens are tested with Token::isWhitespace().
*/
TokenList &trim();
/**
* @brief Removes all tokens that match given criteria from the beginning of the list.
* @param type Token type to remove.
* @param alsoTrim Token value to remove.
*
* This method is an extension to the regular trimLeft(). It removes white-space tokens,
* as well as tokens that are of given \p type and have given \p value (both conditions must be met).
*/
TokenList &trimLeft(Token::Type type, const QString& alsoTrim);
/**
* @brief Removes all tokens that match given criteria from the end of the list.
* @param type Token type to remove.
* @param alsoTrim Token value to remove.
*
* This method is an extension to the regular trimRight(). It removes white-space tokens,
* as well as tokens that are of given \p type and have given \p value (both conditions must be met).
*/
TokenList &trimRight(Token::Type type, const QString& alsoTrim);
/**
* @brief Removes all tokens that match given criteria from the beginning and the end of the list.
* @param type Token type to remove.
* @param alsoTrim Token value to remove.
*
* This method is an extension to the regular trim(). It removes white-space tokens,
* as well as tokens that are of given \p type and have given \p value (both conditions must be met).
*/
TokenList &trim(Token::Type type, const QString& alsoTrim);
/**
* @brief Creates list of tokens from this list, letting through only tokens of given type.
* @param type Type of tokens to provide in the new list.
* @return List of tokens from this list matching given \p type.
*/
TokenList filter(Token::Type type) const;
TokenList filterOut(Token::Type type) const;
/**
* @brief Creates list of tokens from this list, letting through only tokens that are not a whitespace.
* @return List of tokens from this list that are not a whitespace.
*
* The condition to test if tokens is a whitespace is a call to Token::isWhitespace().
*/
TokenList filterWhiteSpaces(bool includeComments = true) const;
/**
* @brief Returns sub-list of tokens from this list.
* @param pos Position to start sublist from.
* @param length Number of tokens to get from this list. If -1 (default), then all from the \p pos to the end.
* @return Sub-list of tokens from this list.
*/
TokenList mid(int pos, int length = -1) const;
private:
/**
* @brief Finds first occurrence of token with given type.
* @param type Type of token to look for.
* @param idx Pointer to integer variable to store position in.
* @return Token found, or null pointer if token was not found.
*
* If \p idx is not null, then the position of token found is stored in it. If token was not found,
* then -1 is stored in the \p idx.
*
* This method is used by the public findFirst() and indexOf() methods, as they share common logic.
*/
TokenPtr findFirst(Token::Type type, int* idx) const;
/**
* @brief Finds first occurrence of token with given type and value.
* @param type Type of token to look for.
* @param value Value of the token to look for.
* @param caseSensitivity Should value lookup be case sensitive?
* @param idx Pointer to integer variable to store position in.
* @return Token found, or null pointer if token was not found.
*
* If \p idx is not null, then the position of token found is stored in it. If token was not found,
* then -1 is stored in the \p idx.
*
* This method is used by the public findFirst() and indexOf() methods, as they share common logic.
*/
TokenPtr findFirst(Token::Type type, const QString& value, Qt::CaseSensitivity caseSensitivity, int* idx) const;
/**
* @brief Finds first occurrence of token with given value.
* @param value Value of the token to look for.
* @param caseSensitivity Should value lookup be case sensitive?
* @param idx Pointer to integer variable to store position in.
* @return Token found, or null pointer if token was not found.
*
* If \p idx is not null, then the position of token found is stored in it. If token was not found,
* then -1 is stored in the \p idx.
*
* This method is used by the public findFirst() and indexOf() methods, as they share common logic.
*/
TokenPtr findFirst(const QString& value, Qt::CaseSensitivity caseSensitivity, int* idx) const;
/**
* @brief Finds last occurrence of token with given type.
* @param type Type of token to look for.
* @param idx Pointer to integer variable to store position in.
* @return Token found, or null pointer if token was not found.
*
* If \p idx is not null, then the position of token found is stored in it. If token was not found,
* then -1 is stored in the \p idx.
*
* This method is used by the public findLast() and lastIndexOf() methods, as they share common logic.
*/
TokenPtr findLast(Token::Type type, int* idx) const;
/**
* @brief Finds last occurrence of token with given type and value.
* @param type Type of token to look for.
* @param value Value of the token to look for.
* @param caseSensitivity Should value lookup be case sensitive?
* @param idx Pointer to integer variable to store position in.
* @return Token found, or null pointer if token was not found.
*
* If \p idx is not null, then the position of token found is stored in it. If token was not found,
* then -1 is stored in the \p idx.
*
* This method is used by the public findLast() and lastIndexOf() methods, as they share common logic.
*/
TokenPtr findLast(Token::Type type, const QString& value, Qt::CaseSensitivity caseSensitivity, int* idx) const;
/**
* @brief Finds last occurrence of token with given value.
* @param value Value of the token to look for.
* @param caseSensitivity Should value lookup be case sensitive?
* @param idx Pointer to integer variable to store position in.
* @return Token found, or null pointer if token was not found.
*
* If \p idx is not null, then the position of token found is stored in it. If token was not found,
* then -1 is stored in the \p idx.
*
* This method is used by the public findLast() and lastIndexOf() methods, as they share common logic.
*/
TokenPtr findLast(const QString& value, Qt::CaseSensitivity caseSensitivity, int* idx) const;
};
#endif // TOKEN_H
|