/src/tools/moc/symbols.h

https://bitbucket.org/ultra_iter/qt-vtl · C Header · 147 lines · 87 code · 19 blank · 41 comment · 11 complexity · b251c97505c6ee6bb38d5f79302b3fba MD5 · raw file

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
  4. ** All rights reserved.
  5. ** Contact: Nokia Corporation (qt-info@nokia.com)
  6. **
  7. ** This file is part of the tools applications of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** GNU Lesser General Public License Usage
  11. ** This file may be used under the terms of the GNU Lesser General Public
  12. ** License version 2.1 as published by the Free Software Foundation and
  13. ** appearing in the file LICENSE.LGPL included in the packaging of this
  14. ** file. Please review the following information to ensure the GNU Lesser
  15. ** General Public License version 2.1 requirements will be met:
  16. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  17. **
  18. ** In addition, as a special exception, Nokia gives you certain additional
  19. ** rights. These rights are described in the Nokia Qt LGPL Exception
  20. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  21. **
  22. ** GNU General Public License Usage
  23. ** Alternatively, this file may be used under the terms of the GNU General
  24. ** Public License version 3.0 as published by the Free Software Foundation
  25. ** and appearing in the file LICENSE.GPL included in the packaging of this
  26. ** file. Please review the following information to ensure the GNU General
  27. ** Public License version 3.0 requirements will be met:
  28. ** http://www.gnu.org/copyleft/gpl.html.
  29. **
  30. ** Other Usage
  31. ** Alternatively, this file may be used in accordance with the terms and
  32. ** conditions contained in a signed written agreement between you and Nokia.
  33. **
  34. **
  35. **
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #ifndef SYMBOLS_H
  42. #define SYMBOLS_H
  43. #include "token.h"
  44. #include <QString>
  45. #include <QHash>
  46. #include <QVector>
  47. #include <QDebug>
  48. QT_BEGIN_NAMESPACE
  49. //#define USE_LEXEM_STORE
  50. struct SubArray
  51. {
  52. inline SubArray():from(0),len(-1){}
  53. inline SubArray(const QByteArray &a):array(a),from(0), len(a.size()){}
  54. inline SubArray(const char *s):array(s),from(0) { len = array.size(); }
  55. inline SubArray(const QByteArray &a, int from, int len):array(a), from(from), len(len){}
  56. QByteArray array;
  57. int from, len;
  58. inline bool operator==(const SubArray &other) const {
  59. if (len != other.len)
  60. return false;
  61. for (int i = 0; i < len; ++i)
  62. if (array.at(from + i) != other.array.at(other.from + i))
  63. return false;
  64. return true;
  65. }
  66. };
  67. inline uint qHash(const SubArray &key)
  68. {
  69. const uchar *p = reinterpret_cast<const uchar *>(key.array.data() + key.from);
  70. int n = key.len;
  71. uint h = 0;
  72. uint g;
  73. while (n--) {
  74. h = (h << 4) + *p++;
  75. if ((g = (h & 0xf0000000)) != 0)
  76. h ^= g >> 23;
  77. h &= ~g;
  78. }
  79. return h;
  80. }
  81. struct Symbol
  82. {
  83. #ifdef USE_LEXEM_STORE
  84. typedef QHash<SubArray, QHashDummyValue> LexemStore;
  85. static LexemStore lexemStore;
  86. inline Symbol() : lineNum(-1),token(NOTOKEN){}
  87. inline Symbol(int lineNum, Token token):
  88. lineNum(lineNum), token(token){}
  89. inline Symbol(int lineNum, Token token, const QByteArray &lexem):
  90. lineNum(lineNum), token(token),lex(lexem){}
  91. inline Symbol(int lineNum, Token token, const QByteArray &lexem, int from, int len):
  92. lineNum(lineNum), token(token){
  93. LexemStore::const_iterator it = lexemStore.constFind(SubArray(lexem, from, len));
  94. if (it != lexemStore.constEnd()) {
  95. lex = it.key().array;
  96. } else {
  97. lex = lexem.mid(from, len);
  98. lexemStore.insert(lex, QHashDummyValue());
  99. }
  100. }
  101. int lineNum;
  102. Token token;
  103. inline QByteArray unquotedLexem() const { return lex.mid(1, lex.length()-2); }
  104. inline QByteArray lexem() const { return lex; }
  105. inline operator QByteArray() const { return lex; }
  106. QByteArray lex;
  107. #else
  108. inline Symbol() : lineNum(-1),token(NOTOKEN), from(0),len(-1) {}
  109. inline Symbol(int lineNum, Token token):
  110. lineNum(lineNum), token(token), from(0), len(-1) {}
  111. inline Symbol(int lineNum, Token token, const QByteArray &lexem):
  112. lineNum(lineNum), token(token), lex(lexem), from(0) { len = lex.size(); }
  113. inline Symbol(int lineNum, Token token, const QByteArray &lexem, int from, int len):
  114. lineNum(lineNum), token(token),lex(lexem),from(from), len(len){}
  115. int lineNum;
  116. Token token;
  117. inline QByteArray lexem() const { return lex.mid(from, len); }
  118. inline QByteArray unquotedLexem() const { return lex.mid(from+1, len-2); }
  119. inline operator QByteArray() const { return lex.mid(from, len); }
  120. inline operator SubArray() const { return SubArray(lex, from, len); }
  121. QByteArray lex;
  122. int from, len;
  123. #endif
  124. };
  125. Q_DECLARE_TYPEINFO(Symbol, Q_MOVABLE_TYPE);
  126. typedef QVector<Symbol> Symbols;
  127. QT_END_NAMESPACE
  128. #endif // SYMBOLS_H