Debian Codegraph @

CodeLite/CxxScanner.l

raw
1 %{
2 //
3 //====================--------------------------------------------------------------
4 // Eran Ifrah 2014 (c)
5 //====================--------------------------------------------------------------
6 //
7 // To generate a source file from this .l file, you will need
8 // a flex version 2.5.34 and later
9 // Under Windows, you will need to run the following command
10 // from within *MSYS* terminal (or run codelite from an MSYS shell):
11 // /usr/bin/flex --noline --batch --outfile=CxxLexer.cpp CxxScanner.l
12 //
13 //====================--------------------------------------------------------------
14 //
15 extern "C" int yywrap(void*) { return 1; }
16
17 #include <wx/string.h>
18 #include <string>
19 #include <wx/filename.h>
20 #include "CxxLexerAPI.h"
21 #include "CxxScannerTokens.h"
22
23 #define YY_FATAL_ERROR(x)
24 #define YYSTYPE char*
25 #define ECHO
26 #define P(s)
27
28 #define YY_NO_UNISTD_H
29 #define YY_USER_ACTION yycolumn += yyleng;
30 #define RETURN_WHITESPACE() \
31 CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r; \
32 if(userData->IsCollectingWhitespace()) { \
33 return T_WHITESPACE; \
34 }
35 #define RETURN_NEWLINE() \
36 CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r; \
37 if(userData->IsCollectingWhitespace()) { \
38 return T_NEWLINE; \
39 }
40
41 %}
42
43 /* regex and modes */
44
45 /* options */
46 %option yylineno
47 %option default
48 %option reentrant
49
50 %x PREPR
51 %x WRAP_PREP
52 %x CPP_COMMENT
53 %x C_COMMENT
54 %x INCLUDE
55 %x RAW_STRING
56
57 identifier [a-zA-Z_][0-9a-zA-Z_]*
58 exponent_part [eE][-+]?[0-9]+
59 fractional_constant ([0-9]*"."[0-9]+)|([0-9]+".")
60 floating_constant (({fractional_constant}{exponent_part}?)|([0-9]+{exponent_part}))[FfLl]?
61 dchar_seq [^()\\[:space:]]{0,16}
62
63 integer_suffix_opt ([uU]?[lL]?)|([lL][uU])
64 decimal_constant [1-9][0-9]*{integer_suffix_opt}
65 octal_constant "0"[0-7]*{integer_suffix_opt}
66 hex_constant "0"[xX][0-9a-fA-F]+{integer_suffix_opt}
67
68 simple_escape [abfnrtv'"?\\]
69 octal_escape [0-7]{1,3}
70 hex_escape "x"[0-9a-fA-F]+
71
72 escape_sequence [\\]({simple_escape}|{octal_escape}|{hex_escape})
73 c_char [^'\\\n]|{escape_sequence}
74 s_char [^"\\\n]|{escape_sequence}
75
76 h_tab [\011]
77 form_feed [\014]
78 v_tab [\013]
79 c_return [\015]
80
81 horizontal_white [ ]|{h_tab}
82
83 %%
84 <<EOF>> {yyterminate();}
85 <INITIAL>alignas { return T_ALIGNAS; }
86 <INITIAL>alignof { return T_ALIGNOF; }
87 <INITIAL>and { return T_AND; }
88 <INITIAL>and_eq { return T_AND_EQ; }
89 <INITIAL>asm { return T_ASM; }
90 <INITIAL>auto { return T_AUTO; }
91 <INITIAL>bitand { return T_BITAND; }
92 <INITIAL>bitor { return T_BITOR; }
93 <INITIAL>bool { return T_BOOL; }
94 <INITIAL>break { return T_BREAK; }
95 <INITIAL>case { return T_CASE; }
96 <INITIAL>catch { return T_CATCH;}
97 <INITIAL>char { return T_CHAR;}
98 <INITIAL>char16_t { return T_CHAR16_T;}
99 <INITIAL>char32_t { return T_CHAR32_T;}
100 <INITIAL>class { return T_CLASS;}
101 <INITIAL>compl { return T_COMPL;}
102 <INITIAL>const { return T_CONST;}
103 <INITIAL>constexpr { return T_CONSTEXPR;}
104 <INITIAL>const_cast { return T_CONST_CAST;}
105 <INITIAL>continue { return T_CONTINUE;}
106 <INITIAL>decltype { return T_DECLTYPE;}
107 <INITIAL>default { return T_DEFAULT;}
108 <INITIAL>delete { return T_DELETE;}
109 <INITIAL>do { return T_DO;}
110 <INITIAL>double { return T_DOUBLE;}
111 <INITIAL>dynamic_cast { return T_DYNAMIC_CAST;}
112 <INITIAL>else { return T_ELSE;}
113 <INITIAL>enum { return T_ENUM;}
114 <INITIAL>explicit { return T_EXPLICIT;}
115 <INITIAL>export { return T_EXPORT;}
116 <INITIAL>extern { return T_EXTERN;}
117 <INITIAL>false { return T_FALSE;}
118 <INITIAL>final { return T_FINAL;}
119 <INITIAL>float { return T_FLOAT;}
120 <INITIAL>for { return T_FOR;}
121 <INITIAL>friend { return T_FRIEND;}
122 <INITIAL>goto { return T_GOTO;}
123 <INITIAL>if { return T_IF;}
124 <INITIAL>inline { return T_INLINE;}
125 <INITIAL>int { return T_INT;}
126 <INITIAL>long { return T_LONG;}
127 <INITIAL>mutable { return T_MUTABLE;}
128 <INITIAL>namespace { return T_NAMESPACE;}
129 <INITIAL>new { return T_NEW;}
130 <INITIAL>noexcept { return T_NOEXCEPT;}
131 <INITIAL>not { return T_NOT;}
132 <INITIAL>not_eq { return T_NOT_EQ;}
133 <INITIAL>nullptr { return T_NULLPTR;}
134 <INITIAL>operator { return T_OPERATOR;}
135 <INITIAL>or { return T_OR;}
136 <INITIAL>or_eq { return T_OR_EQ;}
137 <INITIAL>override { return T_OVERRIDE;}
138 <INITIAL>private { return T_PRIVATE;}
139 <INITIAL>protected { return T_PROTECTED;}
140 <INITIAL>public { return T_PUBLIC;}
141 <INITIAL>register { return T_REGISTER;}
142 <INITIAL>reinterpret_cast { return T_REINTERPRET_CAST;}
143 <INITIAL>return { return T_RETURN;}
144 <INITIAL>short { return T_SHORT;}
145 <INITIAL>signed { return T_SIGNED;}
146 <INITIAL>sizeof { return T_SIZEOF;}
147 <INITIAL>static { return T_STATIC;}
148 <INITIAL>static_assert { return T_STATIC_ASSERT;}
149 <INITIAL>static_cast { return T_STATIC_CAST;}
150 <INITIAL>struct { return T_STRUCT;}
151 <INITIAL>switch { return T_SWITCH;}
152 <INITIAL>template { return T_TEMPLATE;}
153 <INITIAL>this { return T_THIS;}
154 <INITIAL>thread_local { return T_THREAD_LOCAL;}
155 <INITIAL>throw { return T_THROW;}
156 <INITIAL>true { return T_TRUE;}
157 <INITIAL>try { return T_TRY;}
158 <INITIAL>typedef { return T_TYPEDEF;}
159 <INITIAL>typeid { return T_TYPEID;}
160 <INITIAL>typename { return T_TYPENAME;}
161 <INITIAL>union { return T_UNION;}
162 <INITIAL>unsigned { return T_UNSIGNED;}
163 <INITIAL>using { return T_USING;}
164 <INITIAL>virtual { return T_VIRTUAL;}
165 <INITIAL>void { return T_VOID;}
166 <INITIAL>volatile { return T_VOLATILE;}
167 <INITIAL>wchar_t { return T_WCHAR_T;}
168 <INITIAL>while { return T_WHILE;}
169 <INITIAL>xor { return T_XOR;}
170 <INITIAL>xor_eq { return T_XOR_EQ;}
171 <INITIAL>\n {}
172 <INITIAL>{horizontal_white}+ { RETURN_WHITESPACE(); }
173 <INITIAL>({v_tab}|{c_return}|{form_feed})+ {RETURN_WHITESPACE();}
174 <INITIAL>({horizontal_white}|{v_tab}|{c_return}|{form_feed})*"\n" { RETURN_NEWLINE(); }
175 <INITIAL>{decimal_constant} { return T_DEC_NUMBER;}
176 <INITIAL>{octal_constant} { return T_OCTAL_NUMBER;}
177 <INITIAL>{hex_constant} { return T_HEX_NUMBER; }
178 <INITIAL>{floating_constant} { return T_FLOAT_NUMBER;}
179 <INITIAL>".*" { return T_DOT_STAR; }
180 <INITIAL>"::" { return T_DOUBLE_COLONS; }
181 <INITIAL>"->" { return T_ARROW; }
182 <INITIAL>"->*" { return T_ARROW_STAR; }
183 <INITIAL>"++" { return T_PLUS_PLUS; }
184 <INITIAL>"--" { return T_MINUS_MINUS; }
185 <INITIAL>"<<" { return T_LS; }
186 <INITIAL>"<=" { return T_LE; }
187 <INITIAL>">=" { return T_GE; }
188 <INITIAL>"==" { return T_EQUAL; }
189 <INITIAL>"!=" { return T_NOT_EQUAL; }
190 <INITIAL>"&&" { return T_AND_AND;}
191 <INITIAL>"||" { return T_OR_OR;}
192 <INITIAL>"*=" { return T_STAR_EQUAL;}
193 <INITIAL>"/=" { return T_SLASH_EQUAL;}
194 <INITIAL>"%=" { return T_DIV_EQUAL;}
195 <INITIAL>"+=" { return T_PLUS_EQUAL;}
196 <INITIAL>"-=" { return T_MINUS_EQUAL;}
197 <INITIAL>"<<=" { return T_LS_ASSIGN;}
198 <INITIAL>">>=" { return T_RS_ASSIGN;}
199 <INITIAL>"&=" { return T_AND_EQUAL;}
200 <INITIAL>"^=" { return T_POW_EQUAL;}
201 <INITIAL>"|=" { return T_OR_EQUAL;}
202 <INITIAL>"..." { return T_3_DOTS;}
203 <INITIAL>"L"?[']{c_char}+['] {
204 return T_STRING;
205 }
206 <INITIAL>"L"?["]{s_char}*["] {
207 return T_STRING;
208 }
209 <INITIAL>R["]{dchar_seq}[(] {
210 BEGIN(RAW_STRING);
211 CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r;
212 // R"<d-char>(
213 userData->set_raw_string_label(yytext + 2 /* skip the `R"` */, yyleng - 3 /* we want just the d-char */);
214 userData->clear_raw_string();
215 }
216 <INITIAL>"/*" {
217 // Clear the comment collected
218 BEGIN C_COMMENT;
219
220 CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r;
221 if(userData->IsCollectingComments()) {
222 userData->ClearComment();
223 userData->AppendToComment("/*");
224 userData->SetCommentStartLine(yylineno);
225 }
226 }
227 <C_COMMENT>"*/" {
228 CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r;
229 if(userData->IsCollectingComments()) {
230 userData->AppendToComment("*/");
231 userData->SetCommentEndLine(yylineno);
232 }
233 BEGIN INITIAL;
234 if(userData->IsCollectingComments()) {
235 return T_C_COMMENT;
236 }
237 }
238 <C_COMMENT>"\n" {
239 CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r;
240 if(userData->IsCollectingComments()) {
241 userData->AppendToComment("\n");
242 }
243 }
244 <C_COMMENT>. {
245 CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r;
246 if(userData->IsCollectingComments()) {
247 userData->AppendToComment(yytext[0]);
248 }
249 }
250
251 <INITIAL,PREPR>"//" {
252 int oldState = yyg->yy_start;
253 BEGIN CPP_COMMENT;
254 CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r;
255 if(userData->IsCollectingComments()) {
256 userData->ClearComment();
257 userData->AppendToComment("//");
258 userData->SetCommentStartLine(yylineno);
259 }
260 if((1 + 2 * PREPR) == oldState) {
261 return T_PP_STATE_EXIT;
262 }
263 }
264
265 <CPP_COMMENT>"\\\n" {
266 CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r;
267 if(userData->IsCollectingComments()) {
268 userData->AppendToComment("\\\n");
269 }
270 }
271 <CPP_COMMENT>"\\\r\n" {
272 CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r;
273 if(userData->IsCollectingComments()) {
274 userData->AppendToComment("\\\r\n");
275 }
276 }
277 <CPP_COMMENT>"\n" {
278 BEGIN INITIAL;
279 CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r;
280 if(userData->IsCollectingComments()) {
281 userData->AppendToComment("\n");
282 return T_CXX_COMMENT;
283 }
284 }
285 <CPP_COMMENT>. {
286 CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r;
287 if(userData->IsCollectingComments()) {
288 userData->AppendToComment(yytext[0]);
289 }
290 }
291 <INITIAL>{identifier} {
292 return T_IDENTIFIER;
293 }
294 <INITIAL>"#" {
295 BEGIN(PREPR);
296 CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r;
297 userData->SetPreProcessorSection(true);
298 }
299 <INITIAL>. {
300 return yytext[0];
301 }
302 <PREPR>"\\\n" {
303 /* keep the current state */
304 }
305 <PREPR>"\\\r\n" {
306 /* keep the current state */
307 }
308 <PREPR>[ \t\r\v\b]+ {
309 /* do nothing */
310 RETURN_WHITESPACE();
311 }
312 <PREPR>"\n" {
313 BEGIN(INITIAL);
314 CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r;
315 userData->SetPreProcessorSection(false);
316 return T_PP_STATE_EXIT;
317 }
318 <PREPR>"define" {
319 return T_PP_DEFINE;
320 }
321 <PREPR>"defined" {
322 return T_PP_DEFINED;
323 }
324 <PREPR>"if" {
325 return T_PP_IF;
326 }
327 <PREPR>"ifdef" {
328 return T_PP_IFDEF;
329 }
330 <PREPR>"ifndef" {
331 return T_PP_IFNDEF;
332 }
333 <PREPR>"else" {
334 return T_PP_ELSE;
335 }
336 <PREPR>"elif" {
337 return T_PP_ELIF;
338 }
339 <PREPR>"endif" {
340 return T_PP_ENDIF;
341 }
342 <PREPR>"line" {
343 return T_PP_LINE;
344 }
345 <PREPR>"pragma" {
346 return T_PP_PRAGMA;
347 }
348 <PREPR>"undef" {
349 return T_PP_UNDEF;
350 }
351 <PREPR>"error" {
352 return T_PP_ERROR;
353 }
354 <PREPR>"&&" {
355 return T_PP_AND;
356 }
357 <PREPR>"||" {
358 return T_PP_OR;
359 }
360 <PREPR>">" {
361 return T_PP_GT;
362 }
363 <PREPR>">=" {
364 return T_PP_GTEQ;
365 }
366 <PREPR>"<" {
367 return T_PP_LT;
368 }
369 <PREPR>"<=" {
370 return T_PP_LTEQ;
371 }
372 <PREPR>"include" {
373 BEGIN(INCLUDE);
374 }
375 <PREPR>"L"?[']{c_char}+['] {
376 return T_PP_STRING;
377 }
378 <PREPR>"L"?["]{s_char}*["] {
379 return T_PP_STRING;
380 }
381 <PREPR>{decimal_constant} { return T_PP_DEC_NUMBER;}
382 <PREPR>{octal_constant} { return T_PP_OCTAL_NUMBER;}
383 <PREPR>{hex_constant} { return T_PP_HEX_NUMBER; }
384 <PREPR>{floating_constant} { return T_PP_FLOAT_NUMBER;}
385 <PREPR>{identifier} {
386 return T_PP_IDENTIFIER;
387 }
388 <PREPR>. {
389 return yytext[0];
390 }
391 <RAW_STRING>[)]{dchar_seq}["] {
392 CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r;
393 std::string delim_found(yytext + 1, yyleng - 2); // we just want the `dchar_seq` sequence
394 if(delim_found == userData->get_raw_string_label()) {
395 BEGIN(INITIAL);
396 return T_RAW_STRING;
397 } else {
398 userData->append_raw_string(yytext, yyleng);
399 }
400 }
401 <RAW_STRING>\n {
402 CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r;
403 userData->append_raw_string('\n');
404 }
405 <RAW_STRING>. {
406 CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r;
407 userData->append_raw_string(yytext[0]);
408 }
409 <INCLUDE>\n {
410 BEGIN(INITIAL);
411 CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r;
412 userData->SetPreProcessorSection(false);
413 return T_PP_STATE_EXIT;
414 }
415 <INCLUDE>[ \t]* { RETURN_WHITESPACE(); }
416 <INCLUDE>["<][^ \t\n]+[">] { /* got the include file name */
417 return T_PP_INCLUDE_FILENAME;
418 }
419
420 %%
421
422 //=============-------------------------------
423 // API methods implementation
424 //=============-------------------------------
425
426 void* LexerNew(const wxString& content, size_t options )
427 {
428 yyscan_t scanner;
429 yylex_init(&scanner);
430 struct yyguts_t * yyg = (struct yyguts_t*)scanner;
431 CppLexerUserData *userData = new CppLexerUserData(options);
432
433 // keep the file pointer (and make sure we close it at the end)
434 userData->SetCurrentPF(NULL);
435 yyg->yyextra_r = userData;
436
437 wxCharBuffer cb = content.mb_str(wxConvUTF8);
438 yy_switch_to_buffer(yy_scan_string(cb.data(), scanner), scanner);
439 yycolumn = 0;
440 yylineno = 0;
441 return scanner;
442 }
443
444 void* LexerNew(const wxFileName& filename, size_t options )
445 {
446 wxFileName fn = filename;
447 if(fn.IsRelative()) {
448 fn.MakeAbsolute();
449 }
450
451 FILE* fp = ::fopen(fn.GetFullPath().mb_str(wxConvUTF8).data(), "rb");
452 if(!fp) {
453 return NULL;
454 }
455 yyscan_t scanner;
456 yylex_init(&scanner);
457 struct yyguts_t * yyg = (struct yyguts_t*)scanner;
458 CppLexerUserData *userData = new CppLexerUserData(options);
459
460 // keep the file pointer (and make sure we close it at the end)
461 userData->SetCurrentPF(fp);
462 yyg->yyextra_r = userData;
463
464 yy_switch_to_buffer(yy_create_buffer(fp, YY_BUF_SIZE, scanner), scanner);
465 yycolumn = 1;
466 yylineno = 0;
467 return scanner;
468 }
469
470 void LexerDestroy(void** scanner)
471 {
472 struct yyguts_t * yyg = (struct yyguts_t*)(*scanner);
473 delete (CppLexerUserData*)yyg->yyextra_r;
474 yy_delete_buffer(YY_CURRENT_BUFFER, *scanner);
475
476 yylex_destroy(*scanner);
477 *scanner = NULL;
478 }
479
480 void LexerUnget(void* scanner)
481 {
482 // return the entire token back to the input stream
483 struct yyguts_t * yyg = (struct yyguts_t*)scanner;
484 yyless(0);
485 }
486
487 bool LexerNext(void* scanner, CxxLexerToken& token)
488 {
489 token.SetColumn(0);
490 token.SetType(yylex(scanner));
491 token.ClearComment();
492 token.ClearRawString();
493 if(!token.IsEOF()) {
494 struct yyguts_t * yyg = (struct yyguts_t*)scanner;
495 CppLexerUserData* userData = (CppLexerUserData*)yyg->yyextra_r;
496 switch(token.GetType()) {
497 case T_RAW_STRING:
498 token.SetLineNumber(yylineno);
499 token.SetRawString(userData->get_raw_string());
500 userData->clear_raw_string_label();
501 break;
502 case T_CXX_COMMENT:
503 // One line up for CXX comments
504 token.SetLineNumber(userData->GetCommentStartLine());
505 token.SetComment(userData->GetComment());
506 userData->ClearComment();
507 break;
508 case T_C_COMMENT:
509 token.SetLineNumber(userData->GetCommentStartLine());
510 token.SetComment(userData->GetComment());
511 userData->ClearComment();
512 break;
513 default:
514 token.SetLineNumber(yylineno);
515 token.SetText(yytext);
516 token.SetColumn(yycolumn);
517 break;
518 }
519
520 } else {
521 token.SetText(NULL);
522 token.SetLineNumber(0);
523 token.SetColumn(0);
524 }
525 return !token.IsEOF();
526 }
527
528 wxString LexerCurrentToken(void* scanner)
529 {
530 struct yyguts_t * yyg = (struct yyguts_t*)scanner;
531 return yytext;
532 }
533
534 CppLexerUserData* LexerGetUserData(void* scanner)
535 {
536 struct yyguts_t * yyg = (struct yyguts_t*)scanner;
537 return (CppLexerUserData*) yyg->yyextra_r;
538 }