Debian Codegraph @

CodeLite/CxxLexerAPI.h

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // Copyright : (C) 2015 Eran Ifrah
5 // File name : CxxLexerAPI.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
26 #ifndef CppLexerAPI_H__
27 #define CppLexerAPI_H__
28
29 #include "wxStringHash.h"
30
31 #include <codelite_exports.h>
32 #include <list>
33 #include <map>
34 #include <string.h>
35 #include <vector>
36 #include <wx/filename.h>
37 #include <wx/string.h>
38 #include <wx/variant.h>
39
40 enum eLexerOptions {
41 /// Options
42 kLexerOpt_None = 0,
43 kLexerOpt_ReturnComments = (1 << 0),
44 kLexerOpt_ReturnWhitespace = (1 << 1),
45 kLexerOpt_CollectMacroValueNumbers = (1 << 2),
46 kLexerOpt_DontCollectMacrosDefinedInThisFile = (1 << 3),
47 /// states
48 kLexerState_InPreProcessor = (1 << 10),
49 };
50
51 enum class eCxxStandard { kCxx03, kCxx11 };
52
53 struct WXDLLIMPEXP_CL CxxLexerException {
54 wxString message;
55 CxxLexerException(const wxString& msg)
56 : message(msg)
57 {
58 }
59 };
60
61 struct WXDLLIMPEXP_CL CxxLexerToken {
62 private:
63 int lineNumber = 0;
64 int column = 0;
65 char* text = nullptr;
66 int type = 0;
67 std::string comment;
68 std::string raw_string;
69
70 private:
71 bool m_owned;
72
73 private:
74 void deleteText()
75 {
76 if(m_owned && text) {
77 free(text);
78 }
79 m_owned = false;
80 text = nullptr;
81 }
82
83 public:
84 void SetColumn(int column) { this->column = column; }
85 void SetComment(const std::string& comment) { this->comment = comment; }
86 void SetRawString(const std::string& raw_string) { this->raw_string = raw_string; }
87 const std::string& GetRawString() const { return raw_string; }
88 void ClearRawString() { this->raw_string.clear(); }
89 void ClearComment() { this->comment.clear(); }
90 void SetLineNumber(int lineNumber) { this->lineNumber = lineNumber; }
91 void SetOwned(bool owned) { this->m_owned = owned; }
92 void SetType(int type) { this->type = type; }
93 int GetColumn() const { return column; }
94 const std::string& GetComment() const { return comment; }
95 int GetLineNumber() const { return lineNumber; }
96 bool IsOwned() const { return m_owned; }
97 const char* GetText() const { return text; }
98 void SetText(char* p) { text = p; }
99 wxString GetWXComment() const { return wxString(comment.c_str(), wxConvISO8859_1); }
100 wxString GetWXString() const { return wxString(text, wxConvISO8859_1); }
101 int GetType() const { return type; }
102
103 bool is_builtin_type() const;
104 bool is_keyword() const;
105 bool is_pp_keyword() const;
106 bool is_operator() const;
107 bool is_number() const;
108
109 CxxLexerToken()
110 : lineNumber(0)
111 , column(0)
112 , text(NULL)
113 , type(0)
114 , m_owned(false)
115 {
116 }
117
118 CxxLexerToken(int tokenType)
119 : lineNumber(0)
120 , column(0)
121 , text(NULL)
122 , type(tokenType)
123 , m_owned(false)
124 {
125 }
126
127 CxxLexerToken(const CxxLexerToken& other)
128 {
129 if(this == &other)
130 return;
131 *this = other;
132 }
133
134 CxxLexerToken& operator=(const CxxLexerToken& other)
135 {
136 deleteText();
137 lineNumber = other.lineNumber;
138 column = other.column;
139 type = other.type;
140 if(other.text) {
141 m_owned = true;
142 #ifdef __WXMSW__
143 text = _strdup(other.text);
144 #else
145 text = strdup(other.text);
146 #endif
147 }
148 return *this;
149 }
150
151 ~CxxLexerToken() { deleteText(); }
152 bool IsEOF() const { return type == 0; }
153
154 typedef std::vector<CxxLexerToken> Vect_t;
155 typedef std::list<CxxLexerToken> List_t;
156 };
157
158 struct WXDLLIMPEXP_CL CxxPreProcessorToken {
159 wxString name;
160 wxString value;
161 bool deleteOnExit;
162 CxxPreProcessorToken()
163 : deleteOnExit(false)
164 {
165 }
166 typedef std::unordered_map<wxString, CxxPreProcessorToken> Map_t;
167 };
168 /**
169 * @class CppLexerUserData
170 */
171 struct WXDLLIMPEXP_CL CppLexerUserData {
172 private:
173 size_t m_flags;
174 std::string m_comment;
175 std::string m_rawString;
176 std::string m_rawStringLabel;
177 int m_commentStartLine;
178 int m_commentEndLine;
179 FILE* m_currentPF;
180
181 public:
182 void Clear()
183 {
184 if(m_currentPF) {
185 ::fclose(m_currentPF);
186 m_currentPF = NULL;
187 }
188
189 ClearComment();
190 m_rawStringLabel.clear();
191 }
192
193 CppLexerUserData(size_t options)
194 : m_flags(options)
195 , m_commentStartLine(wxNOT_FOUND)
196 , m_commentEndLine(wxNOT_FOUND)
197 , m_currentPF(NULL)
198 {
199 }
200
201 ~CppLexerUserData() { Clear(); }
202
203 void SetCurrentPF(FILE* currentPF) { this->m_currentPF = currentPF; }
204 /**
205 * @brief do we collect comments?
206 */
207 bool IsCollectingComments() const { return m_flags & kLexerOpt_ReturnComments; }
208 bool IsCollectingWhitespace() const { return m_flags & kLexerOpt_ReturnWhitespace; }
209 bool IsInPreProcessorSection() const { return m_flags & kLexerState_InPreProcessor; }
210 void SetPreProcessorSection(bool b)
211 {
212 b ? m_flags |= kLexerState_InPreProcessor : m_flags &= ~kLexerState_InPreProcessor;
213 }
214
215 void set_raw_string_label(const char* label, size_t len) { m_rawStringLabel = std::string(label, len); }
216 void clear_raw_string_label() { m_rawStringLabel.clear(); }
217 const std::string& get_raw_string_label() const { return m_rawStringLabel; }
218 const std::string& get_raw_string() const { return m_rawString; }
219 void append_raw_string(const char* s, size_t len) { m_rawString.append(s, len); }
220 void append_raw_string(char c) { m_rawString.append(1, c); }
221 void clear_raw_string() { m_rawString.clear(); }
222
223 //==--------------------
224 // Comment management
225 //==--------------------
226 void AppendToComment(const std::string& str) { m_comment.append(str); }
227 void AppendToComment(char ch) { m_comment.append(1, ch); }
228 void SetCommentEndLine(int commentEndLine) { this->m_commentEndLine = commentEndLine; }
229 void SetCommentStartLine(int commentStartLine) { this->m_commentStartLine = commentStartLine; }
230 int GetCommentEndLine() const { return m_commentEndLine; }
231 int GetCommentStartLine() const { return m_commentStartLine; }
232 const std::string& GetComment() const { return m_comment; }
233 bool HasComment() const { return !m_comment.empty(); }
234 /**
235 * @brief clear all info collected for the last comment
236 */
237 void ClearComment()
238 {
239 m_comment.clear();
240 m_commentStartLine = wxNOT_FOUND;
241 m_commentEndLine = wxNOT_FOUND;
242 }
243 };
244
245 typedef void* Scanner_t;
246 /**
247 * @brief create a new Lexer for a buffer
248 */
249 WXDLLIMPEXP_CL Scanner_t LexerNew(const wxString& buffer, size_t options = kLexerOpt_None);
250
251 /**
252 * @brief create a scanner for a given file name
253 */
254 WXDLLIMPEXP_CL Scanner_t LexerNew(const wxFileName& filename, size_t options);
255 /**
256 * @brief destroy the current lexer and perform cleanup
257 */
258 WXDLLIMPEXP_CL void LexerDestroy(Scanner_t* scanner);
259
260 /**
261 * @brief return the next token, its type, line number and columns
262 */
263 WXDLLIMPEXP_CL bool LexerNext(Scanner_t scanner, CxxLexerToken& token);
264
265 /**
266 * @brief unget the last token
267 */
268 WXDLLIMPEXP_CL void LexerUnget(Scanner_t scanner);
269
270 /**
271 * @brief return the current lexer token
272 */
273 WXDLLIMPEXP_CL wxString LexerCurrentToken(Scanner_t scanner);
274
275 /**
276 * @brief return the associated data with this scanner
277 */
278 WXDLLIMPEXP_CL CppLexerUserData* LexerGetUserData(Scanner_t scanner);
279 #endif