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