1 /****************************************************************************
3 ** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved.
4 ** Copyright (C) 2002-2005 Roberto Raggi <roberto@kdevelop.org>
6 ** This file is part of the Qt Script Generator project on Trolltech Labs.
8 ** This file may be used under the terms of the GNU General Public
9 ** License version 2.0 as published by the Free Software Foundation
10 ** and appearing in the file LICENSE.GPL included in the packaging of
11 ** this file. Please review the following information to ensure GNU
12 ** General Public Licensing requirements will be met:
13 ** http://www.trolltech.com/products/qt/opensource.html
15 ** If you are unsure which license is appropriate for your use, please
16 ** review the following information:
17 ** http://www.trolltech.com/products/qt/licensing.html or contact the
18 ** sales department at sales@trolltech.com.
20 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
21 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 ****************************************************************************/
31 #include <QtCore/QString>
39 typedef void (Lexer::*scan_fun_ptr
)();
51 const NameSymbol
*symbol
;
52 std::size_t right_brace
;
59 LocationTable(const LocationTable
&source
);
60 void operator = (const LocationTable
&source
);
63 inline LocationTable(std::size_t size
= 1024)
71 inline ~LocationTable()
76 inline std::size_t size() const
77 { return line_count
; }
79 void resize(std::size_t size
)
82 lines
= (std::size_t*) std::realloc(lines
, sizeof(std::size_t) * size
);
86 void positionAt(std::size_t offset
, int *line
, int *column
) const
87 { positionAt(offset
, (int) current_line
, line
, column
); }
89 void positionAt(std::size_t offset
, int max_line
, int *line
, int *column
) const;
91 inline std::size_t &operator[](int index
)
92 { return lines
[index
]; }
96 std::size_t line_count
;
97 std::size_t current_line
;
105 TokenStream(const TokenStream
&);
106 void operator = (const TokenStream
&);
109 inline TokenStream(std::size_t size
= 1024)
117 inline ~TokenStream()
118 { std::free(tokens
); }
120 inline std::size_t size() const
121 { return token_count
; }
123 inline std::size_t cursor() const
126 inline void rewind(int i
)
129 void resize(std::size_t size
)
132 tokens
= (Token
*) std::realloc(tokens
, sizeof(Token
) * size
);
136 inline std::size_t nextToken()
139 inline int lookAhead(std::size_t i
= 0) const
140 { return tokens
[index
+ i
].kind
; }
142 inline int kind(std::size_t i
) const
143 { return tokens
[i
].kind
; }
145 inline std::size_t position(std::size_t i
) const
146 { return tokens
[i
].position
; }
148 inline const NameSymbol
*symbol(std::size_t i
) const
149 { return tokens
[i
].extra
.symbol
; }
151 inline std::size_t matchingBrace(std::size_t i
) const
152 { return tokens
[i
].extra
.right_brace
; }
154 inline Token
&operator[](int index
)
155 { return tokens
[index
]; }
157 inline const Token
&token(int index
) const
158 { return tokens
[index
]; }
163 std::size_t token_count
;
169 class LocationManager
171 LocationManager(LocationManager
const &__other
);
172 void operator = (LocationManager
const &__other
);
175 LocationManager (TokenStream
&__token_stream
,
176 LocationTable
&__location_table
,
177 LocationTable
&__line_table
):
178 token_stream (__token_stream
),
179 location_table (__location_table
),
180 line_table (__line_table
) {}
182 void positionAt(std::size_t offset
, int *line
, int *column
,
183 QString
*filename
) const;
185 void extract_line(int offset
, int *line
, QString
*filename
) const;
187 TokenStream
&token_stream
;
188 LocationTable
&location_table
;
189 LocationTable
&line_table
;
195 Lexer(LocationManager
&__location
, Control
*__control
):
196 _M_location(__location
),
197 token_stream(_M_location
.token_stream
),
198 location_table(_M_location
.location_table
),
199 line_table(_M_location
.line_table
),
200 control(__control
) {}
202 void tokenize(const char *contents
, std::size_t size
);
204 LocationManager
&_M_location
;
205 TokenStream
&token_stream
;
206 LocationTable
&location_table
;
207 LocationTable
&line_table
;
210 void reportError(const QString
& msg
);
212 void initialize_scan_table();
214 void scan_white_spaces();
215 void scan_identifier_or_keyword();
216 void scan_identifier_or_literal();
217 void scan_int_constant();
218 void scan_char_constant();
219 void scan_string_constant();
220 void scan_invalid_input();
221 void scan_preprocessor();
233 void scanKeyword10();
234 void scanKeyword11();
235 void scanKeyword12();
236 void scanKeyword13();
237 void scanKeyword14();
238 void scanKeyword16();
242 void scan_remainder();
244 void scan_left_paren();
245 void scan_right_paren();
253 void scan_semicolon();
257 void scan_question();
258 void scan_left_bracket();
259 void scan_right_bracket();
261 void scan_left_brace();
263 void scan_right_brace();
269 const unsigned char *cursor
;
270 const unsigned char *begin_buffer
;
271 const unsigned char *end_buffer
;
274 static scan_fun_ptr s_scan_table
[];
275 static scan_fun_ptr s_scan_keyword_table
[];
276 static bool s_initialized
;
281 // kate: space-indent on; indent-width 2; replace-tabs on;