| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// Copyright : (C) 2015 Eran Ifrah |
| 5 |
// File name : PHPSourceFile.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 PHPPARSER_H |
| 27 |
#define PHPPARSER_H |
| 28 |
|
| 29 |
#include "PHPEntityBase.h" |
| 30 |
#include "PhpLexerAPI.h" |
| 31 |
#include "codelite_exports.h" |
| 32 |
#include <vector> |
| 33 |
#include <wx/filename.h> |
| 34 |
|
| 35 |
class WXDLLIMPEXP_CL PHPLookupTable; |
| 36 |
|
| 37 |
class WXDLLIMPEXP_CL PHPSourceFile |
| 38 |
{ |
| 39 |
wxString m_text; |
| 40 |
PHPEntityBase::List_t m_scopes; |
| 41 |
PHPEntityBase::List_t m_defines; |
| 42 |
PHPScanner_t m_scanner; |
| 43 |
wxFileName m_filename; |
| 44 |
std::vector<phpLexerToken> m_lookBackTokens; |
| 45 |
std::vector<phpLexerToken> m_comments; |
| 46 |
bool m_parseFunctionBody; |
| 47 |
int m_depth; |
| 48 |
bool m_reachedEOF; |
| 49 |
// aliases defined by the 'use' operator |
| 50 |
std::map<wxString, wxString> m_aliases; |
| 51 |
PHPSourceFile* m_converter = nullptr; |
| 52 |
PHPLookupTable* m_lookup = nullptr; |
| 53 |
PHPEntityBase::List_t m_allMatchesInorder; |
| 54 |
|
| 55 |
public: |
| 56 |
typedef wxSharedPtr<PHPSourceFile> Ptr_t; |
| 57 |
|
| 58 |
protected: |
| 59 |
/** |
| 60 |
* @brief the lexer just read $something = ... |
| 61 |
* read the "..." and create a variable hint from it |
| 62 |
*/ |
| 63 |
bool ReadVariableInitialization(PHPEntityBase::Ptr_t var); |
| 64 |
|
| 65 |
/** |
| 66 |
* @brief calls phpLexerNextToken. Use this call instead of the global phpLexerNextToken |
| 67 |
* since this function will handle all PHP comments found |
| 68 |
*/ |
| 69 |
bool NextToken(phpLexerToken& token); |
| 70 |
|
| 71 |
/** |
| 72 |
* @brief return the previous token |
| 73 |
*/ |
| 74 |
phpLexerToken& GetPreviousToken(); |
| 75 |
|
| 76 |
/** |
| 77 |
* @brief read until we found 'delim' (consume it) |
| 78 |
*/ |
| 79 |
bool ReadUntilFound(int delim, phpLexerToken& token); |
| 80 |
|
| 81 |
/** |
| 82 |
* @brief read until we found 'delim1' or delim2 (return which one was found) |
| 83 |
* or wxNOT_FOUND if non found |
| 84 |
*/ |
| 85 |
int ReadUntilFoundOneOf(int delim1, int delim2, phpLexerToken& token); |
| 86 |
|
| 87 |
/** |
| 88 |
* @brief read expression from the current position until we hit |
| 89 |
* semi colon |
| 90 |
* @param tokens [output] |
| 91 |
*/ |
| 92 |
bool ReadExpression(wxString& expression); |
| 93 |
|
| 94 |
/** |
| 95 |
* @brief unget the token |
| 96 |
*/ |
| 97 |
void UngetToken(const phpLexerToken& token); |
| 98 |
|
| 99 |
/** |
| 100 |
* @brief run the lexer until we find 'delim' - consume delim as well |
| 101 |
*/ |
| 102 |
bool ConsumeUntil(int delim); |
| 103 |
|
| 104 |
/** |
| 105 |
* @brief read list of identifiers separated by comma until we find 'delim' |
| 106 |
* note that this function does not consume the 'delim' token |
| 107 |
*/ |
| 108 |
bool ReadCommaSeparatedIdentifiers(int delim, wxArrayString& list); |
| 109 |
|
| 110 |
/** |
| 111 |
* @brief read the type |
| 112 |
*/ |
| 113 |
wxString ReadType(); |
| 114 |
|
| 115 |
/** |
| 116 |
* @brief PHP7 style: read the return value |
| 117 |
*/ |
| 118 |
wxString ReadFunctionReturnValueFromSignature(); |
| 119 |
|
| 120 |
/** |
| 121 |
* @brief read the value that comes after the 'extends' keyword |
| 122 |
*/ |
| 123 |
wxString ReadExtends(); |
| 124 |
|
| 125 |
/** |
| 126 |
* @brief read the tokens after the implements keyword |
| 127 |
*/ |
| 128 |
void ReadImplements(wxArrayString& impls); |
| 129 |
|
| 130 |
/** |
| 131 |
* @brief parse 'use' statements (outer scope, for aliasing) |
| 132 |
*/ |
| 133 |
void OnUse(); |
| 134 |
|
| 135 |
/** |
| 136 |
* @brief parse 'use' statements inside class (usually this is done for 'traits') |
| 137 |
*/ |
| 138 |
void OnUseTrait(); |
| 139 |
|
| 140 |
/** |
| 141 |
* @brief we are looking at a case like: |
| 142 |
* use A, B { |
| 143 |
* B::bigTalk as talk; |
| 144 |
* } |
| 145 |
* parse the inner block and scan for aliases |
| 146 |
*/ |
| 147 |
void ParseUseTraitsBody(); |
| 148 |
|
| 149 |
/** |
| 150 |
* @brief found 'foreach' statement |
| 151 |
*/ |
| 152 |
void OnForEach(); |
| 153 |
|
| 154 |
/** |
| 155 |
* @brief 'namespace' keyword found |
| 156 |
*/ |
| 157 |
void OnNamespace(); |
| 158 |
|
| 159 |
/** |
| 160 |
* @brief 'function' keyword found |
| 161 |
*/ |
| 162 |
void OnFunction(); |
| 163 |
|
| 164 |
/** |
| 165 |
* @brief found catch keyword |
| 166 |
*/ |
| 167 |
void OnCatch(); |
| 168 |
|
| 169 |
/** |
| 170 |
* @brief found a global variable |
| 171 |
*/ |
| 172 |
void OnVariable(const phpLexerToken& token); |
| 173 |
|
| 174 |
/** |
| 175 |
* @brief 'class' keyword found |
| 176 |
*/ |
| 177 |
void OnClass(const phpLexerToken& tok); |
| 178 |
|
| 179 |
/** |
| 180 |
* @brief a define keyword was found |
| 181 |
*/ |
| 182 |
void OnDefine(const phpLexerToken& tok); |
| 183 |
|
| 184 |
/** |
| 185 |
* @brief found constant |
| 186 |
*/ |
| 187 |
void OnConstant(const phpLexerToken& tok); |
| 188 |
|
| 189 |
/** |
| 190 |
* @brief go over the look back tokens and extract all function flags |
| 191 |
*/ |
| 192 |
size_t LookBackForFunctionFlags(); |
| 193 |
|
| 194 |
/** |
| 195 |
* @brief go over the look back tokens and extract all variable flags |
| 196 |
*/ |
| 197 |
size_t LookBackForVariablesFlags(); |
| 198 |
|
| 199 |
/** |
| 200 |
* @brief return true of the look back tokens contains 'type' |
| 201 |
*/ |
| 202 |
bool LookBackTokensContains(int type) const; |
| 203 |
/** |
| 204 |
* @brief parse function signature |
| 205 |
*/ |
| 206 |
void ParseFunctionSignature(int startingDepth); |
| 207 |
|
| 208 |
/** |
| 209 |
* @brief consume the function body |
| 210 |
*/ |
| 211 |
void ConsumeFunctionBody(); |
| 212 |
|
| 213 |
/** |
| 214 |
* @brief parse function body and collect local variables + |
| 215 |
* possible return statements |
| 216 |
*/ |
| 217 |
void ParseFunctionBody(); |
| 218 |
|
| 219 |
/** |
| 220 |
* @brief go over the loop back tokens and construct the type hint |
| 221 |
*/ |
| 222 |
wxString LookBackForTypeHint(); |
| 223 |
|
| 224 |
/** |
| 225 |
* @brief parse phase 2. |
| 226 |
* On this stage, all phpdoc comments are assigned to the proper PHP entity |
| 227 |
*/ |
| 228 |
void PhaseTwo(); |
| 229 |
|
| 230 |
wxString DoMakeIdentifierAbsolute(const wxString& type, bool exactMatch); |
| 231 |
|
| 232 |
public: |
| 233 |
PHPSourceFile(const wxFileName& filename, PHPLookupTable* lookup); |
| 234 |
PHPSourceFile(const wxString& content, PHPLookupTable* lookup); |
| 235 |
|
| 236 |
virtual ~PHPSourceFile(); |
| 237 |
|
| 238 |
/** |
| 239 |
* @brief return a serialized list of all entries found in this source file |
| 240 |
* sorted by line number |
| 241 |
*/ |
| 242 |
const PHPEntityBase::List_t& GetAllMatchesInOrder(); |
| 243 |
|
| 244 |
/** |
| 245 |
* @brief use a different PHPSourceFile class for converting types to their |
| 246 |
* absolute path |
| 247 |
* @param converter |
| 248 |
*/ |
| 249 |
void SetTypeAbsoluteConverter(PHPSourceFile* converter) { m_converter = converter; } |
| 250 |
|
| 251 |
/** |
| 252 |
* @brief check if we are inside a PHP block at the end of the given buffer |
| 253 |
*/ |
| 254 |
static bool IsInPHPSection(const wxString& buffer); |
| 255 |
|
| 256 |
/** |
| 257 |
* @brief return list of aliases (their short name) that appears on this file |
| 258 |
*/ |
| 259 |
PHPEntityBase::List_t GetAliases() const; |
| 260 |
|
| 261 |
/** |
| 262 |
* @brief return list of defines defined in this source file |
| 263 |
*/ |
| 264 |
const PHPEntityBase::List_t& GetDefines() const { return m_defines; } |
| 265 |
|
| 266 |
/** |
| 267 |
* @brief attempt to resolve 'type' to its full path |
| 268 |
*/ |
| 269 |
wxString MakeIdentifierAbsolute(const wxString& type); |
| 270 |
|
| 271 |
/** |
| 272 |
* @brief attempt to resolve 'type' to its full path |
| 273 |
*/ |
| 274 |
wxString MakeTypehintAbsolute(const wxString& type); |
| 275 |
|
| 276 |
/** |
| 277 |
* @brief prepend the current scope to the class name (or trait, interface...) |
| 278 |
*/ |
| 279 |
wxString PrependCurrentScope(const wxString& className); |
| 280 |
|
| 281 |
/** |
| 282 |
* @brief return the source file text |
| 283 |
*/ |
| 284 |
const wxString& GetText() const { return m_text; } |
| 285 |
|
| 286 |
/** |
| 287 |
* @brief parse the source file |
| 288 |
*/ |
| 289 |
void Parse(int exitDepth = -1); |
| 290 |
|
| 291 |
/** |
| 292 |
* @brief return the current scope |
| 293 |
*/ |
| 294 |
PHPEntityBase::Ptr_t CurrentScope(); |
| 295 |
|
| 296 |
/** |
| 297 |
* @brief return the inner most class of the 'CurrentScope' |
| 298 |
* Note that this function return a const raw pointer to the |
| 299 |
* class |
| 300 |
*/ |
| 301 |
const PHPEntityBase* Class(); |
| 302 |
|
| 303 |
/** |
| 304 |
* @brief return the namespace scope of this file |
| 305 |
*/ |
| 306 |
PHPEntityBase::Ptr_t Namespace(); |
| 307 |
|
| 308 |
// Accessors |
| 309 |
void SetFilename(const wxFileName& filename) { this->m_filename = filename; } |
| 310 |
const wxFileName& GetFilename() const { return m_filename; } |
| 311 |
void SetParseFunctionBody(bool parseFunctionBody) { this->m_parseFunctionBody = parseFunctionBody; } |
| 312 |
bool IsParseFunctionBody() const { return m_parseFunctionBody; } |
| 313 |
void PrintStdout(); |
| 314 |
}; |
| 315 |
|
| 316 |
#endif // PHPPARSER_H |