Mercurial > projects > qtd
annotate generator/parser/lexer.h @ 303:98b211f3ee34 signals
fix for #19
author | eldar_ins@eldar-laptop |
---|---|
date | Sat, 12 Dec 2009 16:01:55 +0500 |
parents | 09a0f1d048f2 |
children |
rev | line source |
---|---|
1 | 1 /**************************************************************************** |
2 ** | |
52
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
3 ** Copyright (C) 1992-2009 Nokia. All rights reserved. |
1 | 4 ** Copyright (C) 2002-2005 Roberto Raggi <roberto@kdevelop.org> |
5 ** | |
6 ** This file is part of Qt Jambi. | |
7 ** | |
52
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
8 ** Commercial Usage |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
9 Licensees holding valid Qt Commercial licenses may use this file in |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
10 accordance with the Qt Commercial License Agreement provided with the |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
11 Software or, alternatively, in accordance with the terms contained in |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
12 a written agreement between you and Nokia. |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
13 |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
14 GNU Lesser General Public License Usage |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
15 Alternatively, this file may be used under the terms of the GNU Lesser |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
16 General Public License version 2.1 as published by the Free Software |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
17 Foundation and appearing in the file LICENSE.LGPL included in the |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
18 packaging of this file. Please review the following information to |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
19 ensure the GNU Lesser General Public License version 2.1 requirements |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
20 will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
21 |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
22 In addition, as a special exception, Nokia gives you certain |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
23 additional rights. These rights are described in the Nokia Qt LGPL |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
24 Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
25 package. |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
26 |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
27 GNU General Public License Usage |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
28 Alternatively, this file may be used under the terms of the GNU |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
29 General Public License version 3.0 as published by the Free Software |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
30 Foundation and appearing in the file LICENSE.GPL included in the |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
31 packaging of this file. Please review the following information to |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
32 ensure the GNU General Public License version 3.0 requirements will be |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
33 met: http://www.gnu.org/copyleft/gpl.html. |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
34 |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
35 If you are unsure which license is appropriate for your use, please |
09a0f1d048f2
update parser to that from jambi 4.5, attemt to fix building with gcc 4.4
eldar
parents:
1
diff
changeset
|
36 contact the sales department at qt-sales@nokia.com. |
1 | 37 |
38 ** | |
39 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | |
40 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
41 ** | |
42 ****************************************************************************/ | |
43 | |
44 | |
45 #ifndef LEXER_H | |
46 #define LEXER_H | |
47 | |
48 #include "symbol.h" | |
49 | |
50 #include <QtCore/QString> | |
51 #include <cstdlib> | |
52 #include <cassert> | |
53 | |
54 struct NameSymbol; | |
55 class Lexer; | |
56 class Control; | |
57 | |
58 typedef void (Lexer::*scan_fun_ptr)(); | |
59 | |
60 class Token | |
61 { | |
62 public: | |
63 int kind; | |
64 std::size_t position; | |
65 std::size_t size; | |
66 char const *text; | |
67 | |
68 union | |
69 { | |
70 const NameSymbol *symbol; | |
71 std::size_t right_brace; | |
72 } extra; | |
73 }; | |
74 | |
75 class LocationTable | |
76 { | |
77 private: | |
78 LocationTable(const LocationTable &source); | |
79 void operator = (const LocationTable &source); | |
80 | |
81 public: | |
82 inline LocationTable(std::size_t size = 1024) | |
83 : lines(0), | |
84 line_count(0), | |
85 current_line(0) | |
86 { | |
87 resize(size); | |
88 } | |
89 | |
90 inline ~LocationTable() | |
91 { | |
92 free(lines); | |
93 } | |
94 | |
95 inline std::size_t size() const | |
96 { return line_count; } | |
97 | |
98 void resize(std::size_t size) | |
99 { | |
100 Q_ASSERT(size > 0); | |
101 lines = (std::size_t*) ::realloc(lines, sizeof(std::size_t) * size); | |
102 line_count = size; | |
103 } | |
104 | |
105 void positionAt(std::size_t offset, int *line, int *column) const | |
106 { positionAt(offset, (int) current_line, line, column); } | |
107 | |
108 void positionAt(std::size_t offset, int max_line, int *line, int *column) const; | |
109 | |
110 inline std::size_t &operator[](int index) | |
111 { return lines[index]; } | |
112 | |
113 private: | |
114 std::size_t *lines; | |
115 std::size_t line_count; | |
116 std::size_t current_line; | |
117 | |
118 friend class Lexer; | |
119 }; | |
120 | |
121 class TokenStream | |
122 { | |
123 private: | |
124 TokenStream(const TokenStream &); | |
125 void operator = (const TokenStream &); | |
126 | |
127 public: | |
128 inline TokenStream(std::size_t size = 1024) | |
129 : tokens(0), | |
130 index(0), | |
131 token_count(0) | |
132 { | |
133 resize(size); | |
134 } | |
135 | |
136 inline ~TokenStream() | |
137 { ::free(tokens); } | |
138 | |
139 inline std::size_t size() const | |
140 { return token_count; } | |
141 | |
142 inline std::size_t cursor() const | |
143 { return index; } | |
144 | |
145 inline void rewind(int i) | |
146 { index = i; } | |
147 | |
148 void resize(std::size_t size) | |
149 { | |
150 Q_ASSERT(size > 0); | |
151 tokens = (Token*) ::realloc(tokens, sizeof(Token) * size); | |
152 token_count = size; | |
153 } | |
154 | |
155 inline std::size_t nextToken() | |
156 { return index++; } | |
157 | |
158 inline int lookAhead(std::size_t i = 0) const | |
159 { return tokens[index + i].kind; } | |
160 | |
161 inline int kind(std::size_t i) const | |
162 { return tokens[i].kind; } | |
163 | |
164 inline std::size_t position(std::size_t i) const | |
165 { return tokens[i].position; } | |
166 | |
167 inline const NameSymbol *symbol(std::size_t i) const | |
168 { return tokens[i].extra.symbol; } | |
169 | |
170 inline std::size_t matchingBrace(std::size_t i) const | |
171 { return tokens[i].extra.right_brace; } | |
172 | |
173 inline Token &operator[](int index) | |
174 { return tokens[index]; } | |
175 | |
176 inline const Token &token(int index) const | |
177 { return tokens[index]; } | |
178 | |
179 private: | |
180 Token *tokens; | |
181 std::size_t index; | |
182 std::size_t token_count; | |
183 | |
184 private: | |
185 friend class Lexer; | |
186 }; | |
187 | |
188 class LocationManager | |
189 { | |
190 LocationManager(LocationManager const &__other); | |
191 void operator = (LocationManager const &__other); | |
192 | |
193 public: | |
194 LocationManager (TokenStream &__token_stream, | |
195 LocationTable &__location_table, | |
196 LocationTable &__line_table): | |
197 token_stream (__token_stream), | |
198 location_table (__location_table), | |
199 line_table (__line_table) {} | |
200 | |
201 void positionAt(std::size_t offset, int *line, int *column, | |
202 QString *filename) const; | |
203 | |
204 void extract_line(int offset, int *line, QString *filename) const; | |
205 | |
206 TokenStream &token_stream; | |
207 LocationTable &location_table; | |
208 LocationTable &line_table; | |
209 }; | |
210 | |
211 class Lexer | |
212 { | |
213 public: | |
214 Lexer(LocationManager &__location, Control *__control): | |
215 _M_location(__location), | |
216 token_stream(_M_location.token_stream), | |
217 location_table(_M_location.location_table), | |
218 line_table(_M_location.line_table), | |
219 control(__control) {} | |
220 | |
221 void tokenize(const char *contents, std::size_t size); | |
222 | |
223 LocationManager &_M_location; | |
224 TokenStream &token_stream; | |
225 LocationTable &location_table; | |
226 LocationTable &line_table; | |
227 | |
228 private: | |
229 void reportError(const QString& msg); | |
230 | |
231 void initialize_scan_table(); | |
232 void scan_newline(); | |
233 void scan_white_spaces(); | |
234 void scan_identifier_or_keyword(); | |
235 void scan_identifier_or_literal(); | |
236 void scan_int_constant(); | |
237 void scan_char_constant(); | |
238 void scan_string_constant(); | |
239 void scan_invalid_input(); | |
240 void scan_preprocessor(); | |
241 | |
242 // keywords | |
243 void scanKeyword0(); | |
244 void scanKeyword2(); | |
245 void scanKeyword3(); | |
246 void scanKeyword4(); | |
247 void scanKeyword5(); | |
248 void scanKeyword6(); | |
249 void scanKeyword7(); | |
250 void scanKeyword8(); | |
251 void scanKeyword9(); | |
252 void scanKeyword10(); | |
253 void scanKeyword11(); | |
254 void scanKeyword12(); | |
255 void scanKeyword13(); | |
256 void scanKeyword14(); | |
257 void scanKeyword16(); | |
258 | |
259 // operators | |
260 void scan_not(); | |
261 void scan_remainder(); | |
262 void scan_and(); | |
263 void scan_left_paren(); | |
264 void scan_right_paren(); | |
265 void scan_star(); | |
266 void scan_plus(); | |
267 void scan_comma(); | |
268 void scan_minus(); | |
269 void scan_dot(); | |
270 void scan_divide(); | |
271 void scan_colon(); | |
272 void scan_semicolon(); | |
273 void scan_less(); | |
274 void scan_equal(); | |
275 void scan_greater(); | |
276 void scan_question(); | |
277 void scan_left_bracket(); | |
278 void scan_right_bracket(); | |
279 void scan_xor(); | |
280 void scan_left_brace(); | |
281 void scan_or(); | |
282 void scan_right_brace(); | |
283 void scan_tilde(); | |
284 void scan_EOF(); | |
285 | |
286 private: | |
287 Control *control; | |
288 const unsigned char *cursor; | |
289 const unsigned char *begin_buffer; | |
290 const unsigned char *end_buffer; | |
291 std::size_t index; | |
292 | |
293 static scan_fun_ptr s_scan_table[]; | |
294 static scan_fun_ptr s_scan_keyword_table[]; | |
295 static bool s_initialized; | |
296 }; | |
297 | |
298 #endif // LEXER_H | |
299 | |
300 // kate: space-indent on; indent-width 2; replace-tabs on; |