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
|
#include "token.h"
#include "lexer.h"
#include <stdio.h>
#include <QStringList>
Token::Token()
: lemonType(0), type(INVALID), value(QString()), start(-1), end(-1)
{
}
Token::Token(int lemonType, Type type, QString value, qint64 start, qint64 end)
: lemonType(lemonType), type(type), value(value), start(start), end(end)
{}
Token::Token(int lemonType, Type type, QChar value, qint64 start, qint64 end)
: lemonType(lemonType), type(type), value(value), start(start), end(end)
{}
Token::Token(int lemonType, Token::Type type, QString value)
: lemonType(lemonType), type(type), value(value), start(-1), end(-1)
{
}
Token::Token(QString value)
: lemonType(0), type(INVALID), value(value), start(0), end(0)
{}
Token::Token(Token::Type type, QString value)
: lemonType(0), type(type), value(value), start(0), end(0)
{
}
Token::Token(Token::Type type, QString value, qint64 start, qint64 end)
: lemonType(0), type(type), value(value), start(start), end(end)
{
}
Token::~Token()
{
}
QString Token::toString()
{
return "{" +
typeToString(type) +
" " +
value +
" " +
QString::number(start) +
" " +
QString::number(end) +
"}";
}
const QString Token::typeToString(Token::Type type)
{
switch (type)
{
case Token::CTX_ROWID_KW:
return "CTX_ROWID_KW";
case Token::CTX_STRICT_KW:
return "CTX_STRICT_KW";
case Token::CTX_NEW_KW:
return "CTX_NEW_KW";
case Token::CTX_OLD_KW:
return "CTX_OLD_KW";
case Token::CTX_TABLE_NEW:
return "CTX_TABLE_NEW";
case Token::CTX_INDEX_NEW:
return "CTX_INDEX_NEW";
case Token::CTX_VIEW_NEW:
return "CTX_VIEW_NEW";
case Token::CTX_TRIGGER_NEW:
return "CTX_TRIGGER_NEW";
case Token::CTX_ALIAS:
return "CTX_ALIAS";
case Token::CTX_TRANSACTION:
return "CTX_transaction";
case Token::CTX_COLUMN_NEW:
return "CTX_COLUMN_NEW";
case Token::CTX_COLUMN_TYPE:
return "CTX_COLUMN_TYPE";
case Token::CTX_CONSTRAINT:
return "CTX_CONSTRAINT";
case Token::CTX_FK_MATCH:
return "CTX_FK_MATCH";
case Token::CTX_PRAGMA:
return "CTX_PRAGMA";
case Token::CTX_ERROR_MESSAGE:
return "CTX_ERROR_MESSAGE";
case Token::CTX_COLUMN:
return "CTX_COLUMN";
case Token::CTX_TABLE:
return "CTX_TABLE";
case Token::CTX_DATABASE:
return "CTX_DATABASE";
case Token::CTX_FUNCTION:
return "CTX_FUNCTION";
case Token::CTX_COLLATION:
return "CTX_COLLATION";
case Token::CTX_INDEX:
return "CTX_INDEX";
case Token::CTX_TRIGGER:
return "CTX_TRIGGER";
case Token::CTX_VIEW:
return "CTX_VIEW";
case Token::CTX_JOIN_OPTS:
return "CTX_JOIN_OPTS";
case Token::INVALID:
return "INVALID";
case Token::OTHER:
return "OTHER";
case Token::STRING:
return "STRING";
case Token::COMMENT:
return "COMMENT";
case Token::FLOAT:
return "FLOAT";
case Token::INTEGER:
return "INTEGER";
case Token::BIND_PARAM:
return "BIND_PARAM";
case Token::OPERATOR:
return "OPERATOR";
case Token::PAR_LEFT:
return "PAR_LEFT";
case Token::PAR_RIGHT:
return "PAR_RIGHT";
case Token::SPACE:
return "SPACE";
case Token::BLOB:
return "BLOB";
case Token::KEYWORD:
return "KEYWORD";
}
return "";
}
Range Token::getRange()
{
return Range(start, end);
}
bool Token::isWhitespace(bool includeComments) const
{
return (type == SPACE || (includeComments && type == COMMENT));
}
bool Token::isSeparating() const
{
switch (type)
{
case Token::SPACE:
case Token::PAR_LEFT:
case Token::PAR_RIGHT:
case Token::OPERATOR:
return true;
default:
break;
}
return false;
}
bool Token::isMeaningful() const
{
switch (type)
{
case Token::BIND_PARAM:
case Token::PAR_LEFT:
case Token::PAR_RIGHT:
case Token::BLOB:
case Token::FLOAT:
case Token::INTEGER:
case Token::INVALID:
case Token::KEYWORD:
case Token::OTHER:
case Token::STRING:
case Token::OPERATOR:
return true;
default:
break;
}
return false;
}
bool Token::isDbObjectType() const
{
return ((type & TOKEN_TYPE_MASK_DB_OBJECT) == TOKEN_TYPE_MASK_DB_OBJECT);
}
QString Token::typeString() const
{
return typeToString(type);
}
int Token::operator ==(const Token &other)
{
return type == other.type && value == other.value && start == other.start && end == other.end;
}
bool Token::operator <(const Token &other) const
{
if (start == other.start)
return end < other.end;
else
return start < other.start;
}
uint qHash(const TokenPtr& token)
{
// This doesn't look nice, but it's good enough to satisfy a hash table.
// It's fast and quite distinguishable.
// It's rare to have two pointers with the same int type representation,
// and if that happens, there's always a comparision operator.
return (uint)reinterpret_cast<qint64>(token.data());
}
TokenList::TokenList()
: QList<TokenPtr>()
{
}
TokenList::TokenList(const QList<TokenPtr>& other)
: QList<TokenPtr>(other)
{
}
QString TokenList::toString() const
{
return toStringList().join(" ");
}
QStringList TokenList::toStringList() const
{
QStringList strList;
for (const TokenPtr& t : *this)
strList << t->toString();
return strList;
}
QStringList TokenList::toValueList() const
{
QStringList strList;
for (const TokenPtr& t : *this)
strList << t->value;
return strList;
}
int TokenList::indexOf(TokenPtr token) const
{
return QList<TokenPtr>::indexOf(token);
}
int TokenList::indexOf(Token::Type type) const
{
int i;
findFirst(type, &i);
return i;
}
int TokenList::indexOf(Token::Type type, const QString &value, Qt::CaseSensitivity caseSensitivity) const
{
int i;
findFirst(type, value, caseSensitivity, &i);
return i;
}
int TokenList::indexOf(const QString &value, Qt::CaseSensitivity caseSensitivity) const
{
int i;
findFirst(value, caseSensitivity, &i);
return i;
}
int TokenList::lastIndexOf(TokenPtr token) const
{
return QList<TokenPtr>::lastIndexOf(token);
}
int TokenList::lastIndexOf(Token::Type type) const
{
int i;
findLast(type, &i);
return i;
}
int TokenList::lastIndexOf(Token::Type type, const QString& value, Qt::CaseSensitivity caseSensitivity) const
{
int i;
findLast(type, value, caseSensitivity, &i);
return i;
}
int TokenList::lastIndexOf(const QString& value, Qt::CaseSensitivity caseSensitivity) const
{
int i;
findLast(value, caseSensitivity, &i);
return i;
}
TokenPtr TokenList::find(Token::Type type) const
{
return findFirst(type, nullptr);
}
TokenPtr TokenList::find(Token::Type type, const QString &value, Qt::CaseSensitivity caseSensitivity) const
{
return findFirst(type, value, caseSensitivity, nullptr);
}
TokenPtr TokenList::find(const QString &value, Qt::CaseSensitivity caseSensitivity) const
{
return findFirst(value, caseSensitivity, nullptr);
}
TokenPtr TokenList::findLast(Token::Type type) const
{
return findLast(type, nullptr);
}
TokenPtr TokenList::findLast(Token::Type type, const QString& value, Qt::CaseSensitivity caseSensitivity) const
{
return findLast(type, value, caseSensitivity, nullptr);
}
TokenPtr TokenList::findLast(const QString& value, Qt::CaseSensitivity caseSensitivity) const
{
return findLast(value, caseSensitivity, nullptr);
}
TokenPtr TokenList::atCursorPosition(quint64 cursorPosition) const
{
for (TokenPtr token : *this)
{
if (token->getRange().contains(cursorPosition))
return token;
}
return TokenPtr();
}
void TokenList::insert(int i, const TokenList &list)
{
for (TokenPtr token : list)
QList<TokenPtr>::insert(i++, token);
}
void TokenList::insert(int i, TokenPtr token)
{
QList<TokenPtr>::insert(i, token);
}
TokenList &TokenList::operator =(const QList<TokenPtr> &other)
{
QList<TokenPtr>::operator =(other);
return *this;
}
QString TokenList::detokenize() const
{
return Lexer::detokenize(*this);
}
void TokenList::replace(int startIdx, int length, const TokenList& newTokens)
{
for (int i = 0; i < length; i++)
removeAt(startIdx);
insert(startIdx, newTokens);
}
void TokenList::replace(int startIdx, int length, TokenPtr newToken)
{
for (int i = 0; i < length; i++)
removeAt(startIdx);
insert(startIdx, newToken);
}
void TokenList::replace(int startIdx, TokenPtr newToken)
{
QList<TokenPtr>::replace(startIdx, newToken);
}
void TokenList::replace(int startIdx, const TokenList& newTokens)
{
replace(startIdx, 1, newTokens);
}
int TokenList::replace(TokenPtr startToken, TokenPtr endToken, const TokenList& newTokens)
{
int startIdx = indexOf(startToken);
if (startIdx < 0)
return 0;
int endIdx = indexOf(endToken);
if (endIdx < 0)
return 0;
replace(startIdx, endIdx - startIdx, newTokens);
return endIdx - startIdx;
}
int TokenList::replace(TokenPtr startToken, TokenPtr endToken, TokenPtr newToken)
{
int startIdx = indexOf(startToken);
if (startIdx < 0)
return 0;
int endIdx = indexOf(endToken);
if (endIdx < 0)
return 0;
replace(startIdx, endIdx - startIdx, newToken);
return endIdx - startIdx;
}
bool TokenList::replace(TokenPtr oldToken, TokenPtr newToken)
{
int idx = indexOf(oldToken);
if (idx < 0)
return false;
replace(idx, newToken);
return true;
}
bool TokenList::replace(TokenPtr oldToken, const TokenList& newTokens)
{
int idx = indexOf(oldToken);
if (idx < 0)
return false;
replace(idx, newTokens);
return true;
}
bool TokenList::remove(TokenPtr startToken, TokenPtr endToken)
{
int startIdx = indexOf(startToken);
if (startIdx < 0)
return false;
int endIdx = indexOf(endToken);
if (endIdx < 0)
return false;
if (endIdx < startIdx)
return false;
for (int i = startIdx; i < endIdx; i++)
removeAt(startIdx);
return true;
}
bool TokenList::remove(Token::Type type)
{
int idx = indexOf(type);
if (idx == -1)
return false;
removeAt(idx);
return true;
}
TokenList& TokenList::trimLeft()
{
while (size() > 0 && first()->isWhitespace())
removeFirst();
return *this;
}
TokenList& TokenList::trimRight()
{
while (size() > 0 && last()->isWhitespace())
removeLast();
return *this;
}
TokenList& TokenList::trim()
{
trimLeft();
trimRight();
return *this;
}
TokenList& TokenList::trimLeft(Token::Type type, const QString& alsoTrim)
{
while (size() > 0 && (first()->isWhitespace() || (first()->type == type && first()->value == alsoTrim)))
removeFirst();
return *this;
}
TokenList& TokenList::trimRight(Token::Type type, const QString& alsoTrim)
{
while (size() > 0 && (last()->isWhitespace() || (last()->type == type && last()->value == alsoTrim)))
removeLast();
return *this;
}
TokenList& TokenList::trim(Token::Type type, const QString& alsoTrim)
{
trimLeft(type, alsoTrim);
trimRight(type, alsoTrim);
return *this;
}
TokenList TokenList::filter(Token::Type type) const
{
TokenList filtered;
for (TokenPtr token : *this)
if (token->type == type)
filtered << token;
return filtered;
}
TokenList TokenList::filterOut(Token::Type type) const
{
TokenList filtered;
for (TokenPtr token : *this)
if (token->type != type)
filtered << token;
return filtered;
}
TokenList TokenList::filterWhiteSpaces(bool includeComments) const
{
TokenList filtered;
for (TokenPtr token : *this)
if (!token->isWhitespace(includeComments))
filtered << token;
return filtered;
}
TokenList TokenList::mid(int pos, int length) const
{
TokenList newList = QList<TokenPtr>::mid(pos, length);
return newList;
}
TokenPtr TokenList::findFirst(Token::Type type, int *idx) const
{
int i = -1;
TokenPtr token;
QListIterator<TokenPtr> it(*this);
while (it.hasNext())
{
token = it.next();
i++;
if (token->type == type)
{
if (idx) (*idx) = i;
return token;
}
}
if (idx) (*idx) = -1;
return TokenPtr();
}
TokenPtr TokenList::findFirst(Token::Type type, const QString &value, Qt::CaseSensitivity caseSensitivity, int *idx) const
{
int i = -1;
TokenPtr token;
QListIterator<TokenPtr> it(*this);
while (it.hasNext())
{
token = it.next();
i++;
if (token->type != type)
continue;
if (token->value.compare(value, caseSensitivity) == 0)
{
if (idx) (*idx) = i;
return token;
}
}
if (idx) (*idx) = -1;
return TokenPtr();
}
TokenPtr TokenList::findFirst(const QString &value, Qt::CaseSensitivity caseSensitivity, int *idx) const
{
int i = -1;
TokenPtr token;
QListIterator<TokenPtr> it(*this);
while (it.hasNext())
{
token = it.next();
i++;
if (token->value.compare(value, caseSensitivity) == 0)
{
if (idx) (*idx) = i;
return token;
}
}
if (idx) (*idx) = -1;
return TokenPtr();
}
TokenPtr TokenList::findLast(Token::Type type, int* idx) const
{
int i = size();
TokenPtr token;
QListIterator<TokenPtr> it(*this);
it.toBack();
while (it.hasPrevious())
{
token = it.previous();
i--;
if (token->type == type)
{
if (idx) (*idx) = i;
return token;
}
}
if (idx) (*idx) = -1;
return TokenPtr();
}
TokenPtr TokenList::findLast(Token::Type type, const QString& value, Qt::CaseSensitivity caseSensitivity, int* idx) const
{
int i = size();
TokenPtr token;
QListIterator<TokenPtr> it(*this);
it.toBack();
while (it.hasPrevious())
{
token = it.previous();
i--;
if (token->type != type)
continue;
if (token->value.compare(value, caseSensitivity) == 0)
{
if (idx) (*idx) = i;
return token;
}
}
if (idx) (*idx) = -1;
return TokenPtr();
}
TokenPtr TokenList::findLast(const QString& value, Qt::CaseSensitivity caseSensitivity, int* idx) const
{
int i = size();
TokenPtr token;
QListIterator<TokenPtr> it(*this);
it.toBack();
while (it.hasPrevious())
{
token = it.previous();
i--;
if (token->value.compare(value, caseSensitivity) == 0)
{
if (idx) (*idx) = i;
return token;
}
}
if (idx) (*idx) = -1;
return TokenPtr();
}
|