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