Debian Codegraph @

CodeLite/cppwordscanner.cpp

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2008 by Eran Ifrah
5 // file name : cppwordscanner.cpp
6 //
7 // -------------------------------------------------------------------------
8 // A
9 // _____ _ _ _ _
10 // / __ \ | | | | (_) |
11 // | / \/ ___ __| | ___| | _| |_ ___
12 // | | / _ \ / _ |/ _ \ | | | __/ _ )
13 // | \__/\ (_) | (_| | __/ |___| | || __/
14 // \____/\___/ \__,_|\___\_____/_|\__\___|
15 //
16 // F i l e
17 //
18 // This program is free software; you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation; either version 2 of the License, or
21 // (at your option) any later version.
22 //
23 //////////////////////////////////////////////////////////////////////////////
24 //////////////////////////////////////////////////////////////////////////////
25 #include <wx/ffile.h>
26 #include <wx/strconv.h>
27 #include <wx/tokenzr.h>
28 #include <wx/log.h>
29 #include "cppwordscanner.h"
30 #include "stringaccessor.h"
31 #include "dirsaver.h"
32 #include "ctags_manager.h"
33
34 CppWordScanner::CppWordScanner(const wxString& fileName)
35 : m_filename(fileName)
36 , m_offset(0)
37 {
38 // disable log
39 wxLogNull nolog;
40 wxCSConv fontEncConv(wxFONTENCODING_ISO8859_1);
41
42 wxFFile thefile(m_filename, wxT("rb"));
43 if(thefile.IsOpened()) {
44 m_text.Clear();
45 thefile.ReadAll(&m_text, fontEncConv);
46 if(m_text.IsEmpty()) {
47 // Try another converter
48 fontEncConv = wxFONTENCODING_UTF8;
49 thefile.ReadAll(&m_text, fontEncConv);
50 }
51 }
52 doInit();
53 }
54
55 CppWordScanner::CppWordScanner(const wxString& fileName, const wxString& text, int offset)
56 : m_filename(fileName)
57 , m_text(text.c_str())
58 , m_offset(offset)
59 {
60 doInit();
61 }
62
63 CppWordScanner::~CppWordScanner() {}
64
65 void CppWordScanner::FindAll(CppTokensMap& tokensMap) { doFind("", tokensMap, wxNOT_FOUND, wxNOT_FOUND); }
66
67 void CppWordScanner::Match(const wxString& word, CppTokensMap& l)
68 {
69 // scan the entire text
70 doFind(word, l, wxNOT_FOUND, wxNOT_FOUND);
71 }
72
73 void CppWordScanner::Match(const wxString& word, CppTokensMap& l, int from, int to)
74 {
75 // scan the entire text
76 doFind(word, l, from, to);
77 }
78
79 void CppWordScanner::doFind(const wxString& filter, CppTokensMap& l, int from, int to)
80 {
81 int state(STATE_NORMAL);
82 StringAccessor accessor(m_text);
83 CppToken token;
84 int lineNo(0);
85
86 // set the scan range
87 size_t f = (from == wxNOT_FOUND) ? 0 : from;
88 size_t t = (to == wxNOT_FOUND) ? m_text.size() : to;
89
90 // sanity
91 if(f > m_text.size() || t > m_text.size()) return;
92
93 for(size_t i = f; i < t; i++) {
94 char ch = accessor.safeAt(i);
95
96 // Keep track of line numbers
97 if(accessor.match("\n", i) && (state == STATE_NORMAL || state == STATE_PRE_PROCESSING ||
98 state == STATE_CPP_COMMENT || state == STATE_C_COMMENT)) {
99 lineNo++;
100 }
101
102 switch(state) {
103
104 case STATE_NORMAL:
105 if(accessor.match("#", i)) {
106
107 if(i == 0 || // satrt of document
108 accessor.match("\n", i - 1)) { // we are at start of line
109 state = STATE_PRE_PROCESSING;
110 }
111 } else if(accessor.match("//", i)) {
112
113 // C++ comment, advance i
114 state = STATE_CPP_COMMENT;
115 i++;
116
117 } else if(accessor.match("/*", i)) {
118
119 // C comment
120 state = STATE_C_COMMENT;
121 i++;
122
123 } else if(accessor.match("'", i)) {
124
125 // single quoted string
126 state = STATE_SINGLE_STRING;
127
128 } else if(accessor.match("\"", i)) {
129
130 // dbouble quoted string
131 state = STATE_DQ_STRING;
132
133 } else if(accessor.isWordChar(ch)) {
134
135 // is valid C++ word?
136 token.append(ch);
137 if(token.getOffset() == wxString::npos) {
138 token.setOffset(i + m_offset);
139 }
140 } else {
141
142 if(token.getName().empty() == false) {
143
144 if((int)token.getName().at(0) >= 48 && (int)token.getName().at(0) <= 57) {
145 token.reset();
146 } else {
147 // dont add C++ key words
148 wxString tmpName(token.getName());
149
150 if(m_keywords.count(tmpName) == 0) {
151
152 token.setFilename(m_filename);
153 token.setLineNumber(lineNo);
154
155 // Ok, we are not a number!
156 // filter out non matching words
157 if(filter.empty() || filter == token.getName()) {
158 l.addToken(token);
159 }
160 }
161 token.reset();
162 }
163 }
164 }
165
166 break;
167 case STATE_PRE_PROCESSING:
168 // if the char before the \n is \ (backslash) or \r\ (CR followed by backslash) remain in pre-processing
169 // state
170 if(accessor.match("\n", i) && (!accessor.match("\\", i - 1) && !accessor.match("\\\r", i - 2))) {
171 // no wrap
172 state = STATE_NORMAL;
173
174 } else if(accessor.match("//", i)) {
175 // C++ comment, advance i
176 state = STATE_CPP_COMMENT;
177 i++;
178 }
179 break;
180 case STATE_C_COMMENT:
181 if(accessor.match("*/", i)) {
182 state = STATE_NORMAL;
183 i++;
184 }
185 break;
186 case STATE_CPP_COMMENT:
187 if(accessor.match("\n", i)) {
188 state = STATE_NORMAL;
189 }
190 break;
191 case STATE_DQ_STRING:
192 if(accessor.match("\\\"", i)) {
193 // escaped string
194 i++;
195 } else if(accessor.match("\\", i)) {
196 i++;
197 } else if(accessor.match("\"", i)) {
198 state = STATE_NORMAL;
199 }
200 break;
201 case STATE_SINGLE_STRING:
202 if(accessor.match("\\'", i)) {
203 // escaped single string
204 i++;
205
206 } else if(accessor.match("\\", i)) {
207 i++;
208
209 } else if(accessor.match("'", i)) {
210 state = STATE_NORMAL;
211 }
212 break;
213 }
214 }
215 }
216
217 CppToken::Vec_t CppWordScanner::tokenize()
218 {
219 int state(STATE_NORMAL);
220 StringAccessor accessor(m_text);
221 CppToken token;
222 int lineNo(0);
223 CppToken::Vec_t allTokens;
224
225 // set the scan range
226 size_t f = 0;
227 size_t t = m_text.size();
228
229 // sanity
230 if(f > m_text.size() || t > m_text.size()) return CppToken::Vec_t();
231
232 for(size_t i = f; i < t; i++) {
233 char ch = accessor.safeAt(i);
234
235 // Keep track of line numbers
236 if(accessor.match("\n", i) && (state == STATE_NORMAL || state == STATE_PRE_PROCESSING ||
237 state == STATE_CPP_COMMENT || state == STATE_C_COMMENT)) {
238 lineNo++;
239 }
240
241 switch(state) {
242
243 case STATE_NORMAL:
244 if(accessor.match("#", i)) {
245
246 if(i == 0 || // satrt of document
247 accessor.match("\n", i - 1)) { // we are at start of line
248 state = STATE_PRE_PROCESSING;
249 }
250 } else if(accessor.match("//", i)) {
251
252 // C++ comment, advance i
253 state = STATE_CPP_COMMENT;
254 i++;
255
256 } else if(accessor.match("/*", i)) {
257
258 // C comment
259 state = STATE_C_COMMENT;
260 i++;
261
262 } else if(accessor.match("'", i)) {
263
264 // single quoted string
265 state = STATE_SINGLE_STRING;
266
267 } else if(accessor.match("\"", i)) {
268
269 // dbouble quoted string
270 state = STATE_DQ_STRING;
271
272 } else if(accessor.isWordChar(ch)) {
273
274 // is valid C++ word?
275 token.append(ch);
276 if(token.getOffset() == wxString::npos) {
277 token.setOffset(i + m_offset);
278 }
279 } else {
280
281 if(token.getName().empty() == false) {
282
283 if((int)token.getName().at(0) >= 48 && (int)token.getName().at(0) <= 57) {
284 token.reset();
285 } else {
286 // dont add C++ key words
287 wxString tmpName(token.getName());
288
289 if(m_keywords.count(tmpName) == 0) {
290
291 token.setFilename(m_filename);
292 token.setLineNumber(lineNo);
293 allTokens.push_back(token);
294 }
295 token.reset();
296 }
297 }
298 }
299
300 break;
301 case STATE_PRE_PROCESSING:
302 // if the char before the \n is \ (backslash) or \r\ (CR followed by backslash) remain in pre-processing
303 // state
304 if(accessor.match("\n", i) && (!accessor.match("\\", i - 1) && !accessor.match("\\\r", i - 2))) {
305 // no wrap
306 state = STATE_NORMAL;
307
308 } else if(accessor.match("//", i)) {
309 // C++ comment, advance i
310 state = STATE_CPP_COMMENT;
311 i++;
312 }
313 break;
314 case STATE_C_COMMENT:
315 if(accessor.match("*/", i)) {
316 state = STATE_NORMAL;
317 i++;
318 }
319 break;
320 case STATE_CPP_COMMENT:
321 if(accessor.match("\n", i)) {
322 state = STATE_NORMAL;
323 }
324 break;
325 case STATE_DQ_STRING:
326 if(accessor.match("\\\"", i)) {
327 // escaped string
328 i++;
329 } else if(accessor.match("\\", i)) {
330 i++;
331 } else if(accessor.match("\"", i)) {
332 state = STATE_NORMAL;
333 }
334 break;
335 case STATE_SINGLE_STRING:
336 if(accessor.match("\\'", i)) {
337 // escaped single string
338 i++;
339
340 } else if(accessor.match("\\", i)) {
341 i++;
342
343 } else if(accessor.match("'", i)) {
344 state = STATE_NORMAL;
345 }
346 break;
347 }
348 }
349 return allTokens;
350 }
351
352 void CppWordScanner::doInit() { TagsManager::GetCXXKeywords(m_keywords); }
353
354 TextStatesPtr CppWordScanner::states()
355 {
356 TextStatesPtr bitmap(new TextStates());
357 bitmap->states.resize(m_text.size());
358
359 if(bitmap->states.size() == 0) {
360 return NULL;
361 }
362
363 bitmap->text = m_text;
364
365 int state(STATE_NORMAL);
366 int depth(0);
367 int lineNo(0);
368
369 StringAccessor accessor(m_text);
370 for(size_t i = 0; i < m_text.size(); i++) {
371
372 // Keep track of line numbers
373 if(accessor.match("\n", i) && (state == STATE_NORMAL || state == STATE_PRE_PROCESSING ||
374 state == STATE_CPP_COMMENT || state == STATE_C_COMMENT)) {
375 lineNo++;
376 }
377
378 switch(state) {
379
380 case STATE_NORMAL:
381 if(accessor.match("#", i)) {
382
383 if(i == 0 || // satrt of document
384 accessor.match("\n", i - 1)) { // we are at start of line
385 state = STATE_PRE_PROCESSING;
386 }
387
388 } else if(accessor.match("//", i)) {
389
390 // C++ comment, advance i
391 state = STATE_CPP_COMMENT;
392 bitmap->SetState(i, STATE_CPP_COMMENT, depth, lineNo);
393 i++;
394
395 } else if(accessor.match("/*", i)) {
396
397 // C comment
398 state = STATE_C_COMMENT;
399 bitmap->SetState(i, STATE_C_COMMENT, depth, lineNo);
400 i++;
401
402 } else if(accessor.match("'", i)) {
403
404 // single quoted string
405 state = STATE_SINGLE_STRING;
406
407 } else if(accessor.match("\"", i)) {
408
409 // dbouble quoted string
410 state = STATE_DQ_STRING;
411
412 } else if(accessor.match("{", i)) {
413 depth++;
414
415 } else if(accessor.match("}", i)) {
416 depth--;
417 }
418
419 break;
420 case STATE_PRE_PROCESSING:
421 // if the char before the \n is \ (backslash) or \r\ (CR followed by backslash) remain in pre-processing
422 // state
423 if(accessor.match("\n", i) && (!accessor.match("\\", i - 1) && !accessor.match("\\\r", i - 2))) {
424 // no wrap
425 state = STATE_NORMAL;
426
427 } else if(accessor.match("//", i)) {
428 // C++ comment, advance i
429 state = STATE_CPP_COMMENT;
430 bitmap->SetState(i, STATE_CPP_COMMENT, depth, lineNo);
431 i++;
432 }
433 break;
434 case STATE_C_COMMENT:
435 if(accessor.match("*/", i)) {
436 bitmap->SetState(i, state, depth, lineNo);
437 state = STATE_NORMAL;
438 i++;
439 }
440 break;
441 case STATE_CPP_COMMENT:
442 if(accessor.match("\n", i)) {
443 state = STATE_NORMAL;
444 }
445 break;
446 case STATE_DQ_STRING:
447 if(accessor.match("\\\"", i)) {
448 // escaped string
449 bitmap->SetState(i, STATE_DQ_STRING, depth, lineNo);
450 i++;
451
452 } else if(accessor.match("\\", i)) {
453 bitmap->SetState(i, STATE_DQ_STRING, depth, lineNo);
454 i++;
455
456 } else if(accessor.match("\"", i)) {
457 state = STATE_NORMAL;
458 }
459 break;
460 case STATE_SINGLE_STRING:
461 if(accessor.match("\\'", i)) {
462 // escaped single string
463 bitmap->SetState(i, STATE_SINGLE_STRING, depth, lineNo);
464 i++;
465 } else if(accessor.match("\\", i)) {
466 bitmap->SetState(i, STATE_SINGLE_STRING, depth, lineNo);
467 i++;
468
469 } else if(accessor.match("'", i)) {
470 state = STATE_NORMAL;
471 }
472 break;
473 }
474 bitmap->SetState(i, state, depth, lineNo);
475 }
476 return bitmap;
477 }
478
479 int TextStates::FunctionEndPos(int position)
480 {
481 // Sanity
482 if(text.length() != states.size()) return wxNOT_FOUND;
483
484 if(position < 0) return wxNOT_FOUND;
485
486 if(position >= (int)text.length()) return wxNOT_FOUND;
487
488 if(states[position].depth < 0) {
489 // we are already at depth 0 (which means global scope)
490 return wxNOT_FOUND;
491 }
492
493 // Note that each call to 'Next' / 'Prev' updates the 'pos' member
494 int curdepth = states[position].depth;
495
496 // Step 1: search from this point downward until we find an opening brace (i.e. depth is equal to curdepth+1)
497 SetPosition(position);
498
499 wxChar ch = Next();
500 while(ch) {
501 if(states[pos].depth == curdepth + 1) break;
502 ch = Next();
503 }
504
505 // Step 2: Continue from the current position,
506 // but this time break when depth is curdepth
507
508 ch = Next();
509 while(ch) {
510 if(states[pos].depth == curdepth) break;
511 ch = Next();
512 }
513
514 if(pos > position) {
515 return pos;
516 }
517
518 return wxNOT_FOUND;
519 }
520
521 wxChar TextStates::Next()
522 {
523 if(text.length() != states.size()) return 0;
524
525 if(pos == wxNOT_FOUND) return 0;
526
527 // reached end of text
528 pos++;
529 while(pos < (int)text.length()) {
530 int st = states[pos].state;
531 if(st == CppWordScanner::STATE_NORMAL) {
532 if(text.length() > (size_t)pos) return text.at(pos);
533 ;
534 return 0;
535 }
536 pos++;
537 }
538 return 0;
539 }
540
541 wxChar TextStates::Previous()
542 {
543 if(text.length() != states.size()) return 0;
544
545 if(pos == wxNOT_FOUND) return 0;
546
547 // reached start of text
548 if(pos == 0) return 0;
549
550 pos--;
551 while(pos) {
552 int st = states[pos].state;
553 if(st == CppWordScanner::STATE_NORMAL) {
554 if(text.length() > (size_t)pos) return text.at(pos);
555 return 0;
556 }
557 pos--;
558 }
559 return 0;
560 }
561
562 void TextStates::SetPosition(int pos) { this->pos = pos; }
563
564 void TextStates::SetState(size_t where, int state, int depth, int lineNo)
565 {
566 if(where < states.size()) {
567 states[where].depth = depth;
568 states[where].state = state;
569 states[where].lineNo = lineNo;
570 }
571
572 if(lineToPos.empty() || (int)lineToPos.size() - 1 < lineNo) {
573 lineToPos.push_back(where);
574 }
575 }
576
577 int TextStates::LineToPos(int lineNo)
578 {
579 if(IsOk() == false) return wxNOT_FOUND;
580
581 if(lineToPos.empty() || (int)lineToPos.size() < lineNo || lineNo < 0) return wxNOT_FOUND;
582
583 return lineToPos.at(lineNo);
584 }