Skip to content

Commit

Permalink
PHP: Add support for Enums
Browse files Browse the repository at this point in the history
  • Loading branch information
AJenbo committed Jun 6, 2024
1 parent 02fa54e commit 5907244
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CodeFormatter/PHPFormatterBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ PHPFormatterBuffer& PHPFormatterBuffer::ProcessToken(const phpLexerToken& token)
if(m_options.flags & kPFF_BreakBeforeWhile) {
InsertSeparatorLine();
}
} else if(token.type == kPHP_T_CLASS) {
} else if(token.type == kPHP_T_CLASS || token.type == kPHP_T_ENUM) {
if(m_options.flags & kPFF_BreakBeforeClass) {
InsertSeparatorLine();
}
Expand Down
1 change: 1 addition & 0 deletions CodeLite/PHPEntityBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ enum {
kClass_Interface = (1 << 1),
kClass_Trait = (1 << 2),
kClass_Abstract = (1 << 3),
kClass_Enum = (1 << 4),
};

class WXDLLIMPEXP_CL PHPEntityBase
Expand Down
2 changes: 2 additions & 0 deletions CodeLite/PHPEntityClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ class WXDLLIMPEXP_CL PHPEntityClass : public PHPEntityBase
void SetIsInterface(bool b) { SetFlag(kClass_Interface, b); }
bool IsInterface() const { return HasFlag(kClass_Interface); }
void SetIsTrait(bool b) { SetFlag(kClass_Trait, b); }
void SetIsEnum(bool b) { SetFlag(kClass_Enum, b); }
bool IsTrait() const { return HasFlag(kClass_Trait); }
bool IsEnum() const { return HasFlag(kClass_Enum); }
void SetIsAbstractClass(bool b) { SetFlag(kClass_Abstract, b); }
bool IsAbstractClass() const { return HasFlag(kClass_Abstract); }
};
Expand Down
1 change: 1 addition & 0 deletions CodeLite/PHPExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ phpLexerToken::Vet_t PHPExpression::CreateExpression(const wxString& text)
case kPHP_T_CASE:
case kPHP_T_RETURN:
case kPHP_T_THROW:
case kPHP_T_ENUM:
case kPHP_T_CLASS:
case kPHP_T_TRAIT:
case kPHP_T_INTERFACE:
Expand Down
5 changes: 3 additions & 2 deletions CodeLite/PHPScannerTokens.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ enum {
kPHP_T_ISSET,
kPHP_T_EMPTY,
kPHP_T_HALT_COMPILER,
kPHP_T_CLASS,
kPHP_T_TRAIT,
kPHP_T_CLASS,
kPHP_T_TRAIT,
kPHP_T_ENUM,
kPHP_T_INTERFACE,
kPHP_T_EXTENDS,
kPHP_T_IMPLEMENTS,
Expand Down
33 changes: 32 additions & 1 deletion CodeLite/PHPSourceFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ void PHPSourceFile::Parse(int exitDepth)
case kPHP_T_CONST:
OnConstant(token);
break;
case kPHP_T_CASE:
if(Class() == CurrentScope().get()) {
// in class cope, this means that this is an enum case
OnConstant(token);
}
break;
case kPHP_T_REQUIRE:
case kPHP_T_REQUIRE_ONCE:
case kPHP_T_INCLUDE:
Expand All @@ -212,6 +218,7 @@ void PHPSourceFile::Parse(int exitDepth)
case kPHP_T_CLASS:
case kPHP_T_INTERFACE:
case kPHP_T_TRAIT:
case kPHP_T_ENUM:
// Found class
OnClass(token);
m_lookBackTokens.clear();
Expand Down Expand Up @@ -364,7 +371,7 @@ void PHPSourceFile::OnFunction()
// update function attributes
ParseFunctionSignature(funcDepth);
func->SetFlags(LookBackForFunctionFlags());
if(LookBackTokensContains(kPHP_T_ABSTRACT) || // The 'abstract modifier was found for this this function
if(LookBackTokensContains(kPHP_T_ABSTRACT) || // The 'abstract modifier was found for this function
(funcPtr->Parent() && funcPtr->Parent()->Is(kEntityTypeClass) &&
funcPtr->Parent()->Cast<PHPEntityClass>()->IsInterface())) // We are inside an interface
{
Expand Down Expand Up @@ -792,6 +799,30 @@ void PHPSourceFile::OnClass(const phpLexerToken& tok)
pClass->SetFullName(PrependCurrentScope(token.Text()));
pClass->SetLine(token.lineNumber);

if (tok.type == kPHP_T_ENUM) {
pClass->SetIsEnum(true);
wxArrayString implements;
NextToken(token);
if (token.type == ':') {
NextToken(token);
if (token.type == kPHP_T_IDENTIFIER) {
if (token.Text() == "int") {
implements.Add("\\IntBackedEnum");
} else if (token.Text() == "string") {
implements.Add("\\StringBackedEnum");
} else {
implements.Add("\\BackedEnum");
}
} else {
implements.Add("\\BackedEnum");
}
} else {
UngetToken(token);
implements.Add("\\UnitEnum");
}
pClass->SetImplements(implements);
}

while(NextToken(token)) {
if(token.IsAnyComment())
continue;
Expand Down
1 change: 1 addition & 0 deletions CodeLite/PhpLexer.l
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ horizontal_white [ ]|{h_tab}
<PHP>"goto" {LEX_RETURN(kPHP_T_GOTO);}
<PHP>"echo" {LEX_RETURN(kPHP_T_ECHO);}
<PHP>"print" {LEX_RETURN(kPHP_T_PRINT);}
<PHP>"enum" {LEX_RETURN(kPHP_T_ENUM);}
<PHP>"class" {LEX_RETURN(kPHP_T_CLASS);}
<PHP>"interface" {LEX_RETURN(kPHP_T_INTERFACE);}
<PHP>"trait" {LEX_RETURN(kPHP_T_TRAIT);}
Expand Down
2 changes: 1 addition & 1 deletion codelitephp/PHPParser/php_code_completion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ int PHPCodeCompletion::GetLocationForSettersGetters(const wxString& filecontent,
phpLexerToken token;
bool isOK = false;
while(::phpLexerNext(scanner, token)) {
if(token.type != kPHP_T_CLASS) {
if(token.type != kPHP_T_CLASS && token.type != kPHP_T_ENUM) {
continue;
}

Expand Down

0 comments on commit 5907244

Please sign in to comment.