| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2008 by Eran Ifrah |
| 5 |
// file name : cppwordscanner.h |
| 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 |
#ifndef __cppwordscanner__ |
| 26 |
#define __cppwordscanner__ |
| 27 |
|
| 28 |
#include <wx/arrstr.h> |
| 29 |
#include <vector> |
| 30 |
#include "cpptoken.h" |
| 31 |
#include "smart_ptr.h" |
| 32 |
#include "codelite_exports.h" |
| 33 |
#include <set> |
| 34 |
#include "macros.h" |
| 35 |
#include "wxStringHash.h" |
| 36 |
|
| 37 |
struct ByteState { |
| 38 |
short state; // Holds the current byte state (one of CppWordScanner::STATE_*) |
| 39 |
short depth; // The current char depth |
| 40 |
short depthId; // The depth ID (there can be multiple blocks of the same depth in a given scope) |
| 41 |
int lineNo; // the line number which holds this byte |
| 42 |
}; |
| 43 |
|
| 44 |
class WXDLLIMPEXP_CL TextStates |
| 45 |
{ |
| 46 |
public: |
| 47 |
wxString text; |
| 48 |
std::vector<ByteState> states; |
| 49 |
std::vector<int> lineToPos; |
| 50 |
int pos; |
| 51 |
|
| 52 |
public: |
| 53 |
TextStates() |
| 54 |
: pos(wxNOT_FOUND) |
| 55 |
{ |
| 56 |
} |
| 57 |
|
| 58 |
virtual ~TextStates() {} |
| 59 |
|
| 60 |
void SetPosition(int pos); |
| 61 |
wxChar Previous(); |
| 62 |
wxChar Next(); |
| 63 |
|
| 64 |
/** |
| 65 |
* @brief return true if the current TextState is valid. The test is simple: |
| 66 |
* if the vector size and the text size are equal |
| 67 |
*/ |
| 68 |
bool IsOk() const { return states.size() == text.length(); } |
| 69 |
|
| 70 |
void SetState(size_t where, int state, int depth, int lineNo); |
| 71 |
|
| 72 |
/** |
| 73 |
* @brief return the end of a given function |
| 74 |
* @param position function start position. This function searches for the first opening brace from position '{' and |
| 75 |
* returns the position of the matching |
| 76 |
* closing brace '}' |
| 77 |
*/ |
| 78 |
int FunctionEndPos(int position); |
| 79 |
|
| 80 |
/** |
| 81 |
* @brief convert line number to position |
| 82 |
*/ |
| 83 |
int LineToPos(int lineNo); |
| 84 |
}; |
| 85 |
|
| 86 |
typedef SmartPtr<TextStates> TextStatesPtr; |
| 87 |
|
| 88 |
class WXDLLIMPEXP_CL CppWordScanner |
| 89 |
{ |
| 90 |
wxStringSet_t m_keywords; |
| 91 |
wxString m_filename; |
| 92 |
wxString m_text; |
| 93 |
int m_offset; |
| 94 |
|
| 95 |
public: |
| 96 |
enum { |
| 97 |
STATE_NORMAL = 0, |
| 98 |
STATE_C_COMMENT, |
| 99 |
STATE_CPP_COMMENT, |
| 100 |
STATE_DQ_STRING, |
| 101 |
STATE_SINGLE_STRING, |
| 102 |
STATE_PRE_PROCESSING |
| 103 |
}; |
| 104 |
|
| 105 |
protected: |
| 106 |
void doFind(const wxString& filter, CppTokensMap& l, int from, int to); |
| 107 |
void doInit(); |
| 108 |
|
| 109 |
public: |
| 110 |
CppWordScanner() {} |
| 111 |
CppWordScanner(const wxString& file_name); |
| 112 |
CppWordScanner(const wxString& file_name, const wxString& text, int offset); |
| 113 |
|
| 114 |
/** |
| 115 |
* @brief tokenize the file and return list of tokens |
| 116 |
* @return |
| 117 |
*/ |
| 118 |
CppToken::Vec_t tokenize(); |
| 119 |
|
| 120 |
~CppWordScanner(); |
| 121 |
|
| 122 |
void FindAll(CppTokensMap& l); |
| 123 |
void Match(const wxString& word, CppTokensMap& l); |
| 124 |
/** |
| 125 |
* @brief same as Match(const wxString &word, CppTokensMap &l) however, search for matches only in a given range |
| 126 |
*/ |
| 127 |
void Match(const wxString& word, CppTokensMap& l, int from, int to); |
| 128 |
// we use std::vector<char> and NOT std::vector<char> since the specialization of vector<bool> |
| 129 |
// is broken |
| 130 |
TextStatesPtr states(); |
| 131 |
}; |
| 132 |
|
| 133 |
#endif // __cppwordscanner__ |