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