| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// Copyright : (C) 2015 Eran Ifrah |
| 5 |
// File name : PhpLexerAPI.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 PhpLexerAPI_H__ |
| 27 |
#define PhpLexerAPI_H__ |
| 28 |
|
| 29 |
#include <string> |
| 30 |
#include <vector> |
| 31 |
#include <wx/filename.h> |
| 32 |
|
| 33 |
#include "codelite_exports.h" |
| 34 |
#include "PHPScannerTokens.h" |
| 35 |
|
| 36 |
enum ePhpLexerOptions { |
| 37 |
kPhpLexerOpt_None = 0x00000000, |
| 38 |
kPhpLexerOpt_ReturnComments = 0x00000001, |
| 39 |
kPhpLexerOpt_ReturnWhitespace = 0x00000002, |
| 40 |
kPhpLexerOpt_ReturnAllNonPhp = 0x00000004, |
| 41 |
}; |
| 42 |
|
| 43 |
struct WXDLLIMPEXP_CL phpLexerToken { |
| 44 |
private: |
| 45 |
std::string text; |
| 46 |
wxString wtext; |
| 47 |
|
| 48 |
public: |
| 49 |
int type; |
| 50 |
int lineNumber; |
| 51 |
int endLineNumber; // Usually, this is the same as lineNumber. Unless a multiple line token is found (heredoc, |
| 52 |
// c-style comment etc) |
| 53 |
public: |
| 54 |
phpLexerToken() |
| 55 |
: type(-1) |
| 56 |
, lineNumber(-1) |
| 57 |
, endLineNumber(-1) |
| 58 |
{ |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* @brief clear the token, i.e. IsNull() return true |
| 63 |
*/ |
| 64 |
void Clear() |
| 65 |
{ |
| 66 |
type = -1; |
| 67 |
lineNumber = -1; |
| 68 |
endLineNumber = -1; |
| 69 |
text.clear(); |
| 70 |
wtext.clear(); |
| 71 |
} |
| 72 |
|
| 73 |
void ClearText() |
| 74 |
{ |
| 75 |
text.clear(); |
| 76 |
wtext.clear(); |
| 77 |
} |
| 78 |
|
| 79 |
void SetText(const std::string& t) |
| 80 |
{ |
| 81 |
text = t; |
| 82 |
wtext = wxString(text.c_str(), wxConvUTF8); |
| 83 |
} |
| 84 |
|
| 85 |
const wxString& Text() const { return wtext; } |
| 86 |
bool IsNull() const { return type == -1; } |
| 87 |
/** |
| 88 |
* @brief is the current token a comment? (c++ or c comment) |
| 89 |
*/ |
| 90 |
bool IsAnyComment() const { return type == kPHP_T_C_COMMENT || type == kPHP_T_CXX_COMMENT; } |
| 91 |
/** |
| 92 |
* @brief is the current token a PHP Doc comment? |
| 93 |
*/ |
| 94 |
bool IsDocComment() const { return type == kPHP_T_C_COMMENT; } |
| 95 |
typedef std::vector<phpLexerToken> Vet_t; |
| 96 |
}; |
| 97 |
|
| 98 |
/** |
| 99 |
* @class phpLexerUserData |
| 100 |
*/ |
| 101 |
struct WXDLLIMPEXP_CL phpLexerUserData { |
| 102 |
private: |
| 103 |
size_t m_flags; |
| 104 |
std::string m_comment; |
| 105 |
std::string m_rawStringLabel; |
| 106 |
std::string m_string; |
| 107 |
int m_commentStartLine; |
| 108 |
int m_commentEndLine; |
| 109 |
bool m_insidePhp; |
| 110 |
FILE* m_fp; |
| 111 |
|
| 112 |
public: |
| 113 |
void Clear() |
| 114 |
{ |
| 115 |
if(m_fp) { |
| 116 |
::fclose(m_fp); |
| 117 |
} |
| 118 |
m_fp = NULL; |
| 119 |
m_insidePhp = false; |
| 120 |
ClearComment(); |
| 121 |
m_rawStringLabel.clear(); |
| 122 |
m_string.clear(); |
| 123 |
} |
| 124 |
|
| 125 |
phpLexerUserData(size_t options) |
| 126 |
: m_flags(options) |
| 127 |
, m_commentStartLine(wxNOT_FOUND) |
| 128 |
, m_commentEndLine(wxNOT_FOUND) |
| 129 |
, m_insidePhp(false) |
| 130 |
, m_fp(NULL) |
| 131 |
{ |
| 132 |
} |
| 133 |
|
| 134 |
~phpLexerUserData() { Clear(); } |
| 135 |
void SetFp(FILE* fp) { this->m_fp = fp; } |
| 136 |
/** |
| 137 |
* @brief do we collect comments? |
| 138 |
*/ |
| 139 |
bool IsCollectingComments() const { return m_flags & kPhpLexerOpt_ReturnComments; } |
| 140 |
bool IsCollectingWhitespace() const { return m_flags & kPhpLexerOpt_ReturnWhitespace; } |
| 141 |
bool IsCollectingAllNonPhp() const { return m_flags & kPhpLexerOpt_ReturnAllNonPhp; } |
| 142 |
void SetCollectingWhitespace(bool b) |
| 143 |
{ |
| 144 |
if(b) { |
| 145 |
m_flags |= kPhpLexerOpt_ReturnWhitespace; |
| 146 |
} else { |
| 147 |
m_flags &= ~kPhpLexerOpt_ReturnWhitespace; |
| 148 |
} |
| 149 |
} |
| 150 |
void SetInsidePhp(bool insidePhp) { this->m_insidePhp = insidePhp; } |
| 151 |
bool IsInsidePhp() const { return m_insidePhp; } |
| 152 |
|
| 153 |
void SetString(const std::string& string) { this->m_string = string; } |
| 154 |
std::string& GetString() { return m_string; } |
| 155 |
|
| 156 |
void SetRawStringLabel(const std::string& rawStringLabel) { this->m_rawStringLabel = rawStringLabel; } |
| 157 |
const std::string& GetRawStringLabel() const { return m_rawStringLabel; } |
| 158 |
//==-------------------- |
| 159 |
// Comment management |
| 160 |
//==-------------------- |
| 161 |
void AppendToComment(const std::string& str) { m_comment.append(str); } |
| 162 |
void AppendToComment(char c) { m_comment.append(std::string(1, c)); } |
| 163 |
void SetCommentEndLine(int commentEndLine) { this->m_commentEndLine = commentEndLine; } |
| 164 |
void SetCommentStartLine(int commentStartLine) { this->m_commentStartLine = commentStartLine; } |
| 165 |
int GetCommentEndLine() const { return m_commentEndLine; } |
| 166 |
int GetCommentStartLine() const { return m_commentStartLine; } |
| 167 |
const std::string& GetComment() const { return m_comment; } |
| 168 |
bool HasComment() const { return !m_comment.empty(); } |
| 169 |
/** |
| 170 |
* @brief clear all info collected for the last comment |
| 171 |
*/ |
| 172 |
void ClearComment() |
| 173 |
{ |
| 174 |
m_comment.clear(); |
| 175 |
m_commentStartLine = wxNOT_FOUND; |
| 176 |
m_commentEndLine = wxNOT_FOUND; |
| 177 |
} |
| 178 |
}; |
| 179 |
|
| 180 |
typedef void* PHPScanner_t; |
| 181 |
|
| 182 |
/** |
| 183 |
* @brief create a new Lexer for a given file name |
| 184 |
*/ |
| 185 |
WXDLLIMPEXP_CL PHPScanner_t phpLexerNew(const wxFileName& filename, size_t options = kPhpLexerOpt_None); |
| 186 |
|
| 187 |
/** |
| 188 |
* @brief return the user data associated with this scanner |
| 189 |
*/ |
| 190 |
WXDLLIMPEXP_CL phpLexerUserData* phpLexerGetUserData(PHPScanner_t scanner); |
| 191 |
|
| 192 |
/** |
| 193 |
* @brief create a new Lexer for a given file content |
| 194 |
*/ |
| 195 |
WXDLLIMPEXP_CL PHPScanner_t phpLexerNew(const wxString& content, size_t options = kPhpLexerOpt_None); |
| 196 |
|
| 197 |
/** |
| 198 |
* @brief destroy the current lexer and perform cleanup |
| 199 |
*/ |
| 200 |
WXDLLIMPEXP_CL void phpLexerDestroy(PHPScanner_t* scanner); |
| 201 |
|
| 202 |
/** |
| 203 |
* @brief return the next token, its type, line number and columns |
| 204 |
*/ |
| 205 |
WXDLLIMPEXP_CL bool phpLexerNext(PHPScanner_t scanner, phpLexerToken& token); |
| 206 |
|
| 207 |
/** |
| 208 |
* @brief return true if we are currently inside a PHP block |
| 209 |
*/ |
| 210 |
WXDLLIMPEXP_CL bool phpLexerIsPHPCode(PHPScanner_t scanner); |
| 211 |
|
| 212 |
/** |
| 213 |
* @brief peek at the next token |
| 214 |
*/ |
| 215 |
WXDLLIMPEXP_CL void phpLexerUnget(PHPScanner_t scanner); |
| 216 |
|
| 217 |
/** |
| 218 |
* @class PHPScannerLocker |
| 219 |
* @brief a wrapper around the C API for PHPScanner |
| 220 |
*/ |
| 221 |
struct WXDLLIMPEXP_CL PHPScannerLocker { |
| 222 |
PHPScanner_t scanner; |
| 223 |
PHPScannerLocker(const wxString& content, size_t options = kPhpLexerOpt_None) |
| 224 |
{ |
| 225 |
scanner = ::phpLexerNew(content, options); |
| 226 |
} |
| 227 |
|
| 228 |
PHPScannerLocker(const wxFileName& filename, size_t options = kPhpLexerOpt_None) |
| 229 |
{ |
| 230 |
scanner = ::phpLexerNew(filename, options); |
| 231 |
} |
| 232 |
|
| 233 |
PHPScannerLocker(PHPScanner_t phpScanner) |
| 234 |
: scanner(phpScanner) |
| 235 |
{ |
| 236 |
} |
| 237 |
|
| 238 |
~PHPScannerLocker() |
| 239 |
{ |
| 240 |
if(scanner) { |
| 241 |
::phpLexerDestroy(&scanner); |
| 242 |
} |
| 243 |
} |
| 244 |
}; |
| 245 |
|
| 246 |
#endif |