Skip to content

Commit

Permalink
powershell: Parse enum labels
Browse files Browse the repository at this point in the history
  • Loading branch information
b4n committed May 13, 2024
1 parent 8da7ff0 commit bad24b7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Units/parser-powershell.r/enum-powershell.d/expected.tags
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
EnumName1 input.ps1 /^enum EnumName1 {$/;" g
Label11 input.ps1 /^ Label11$/;" e enum:EnumName1
Label12 input.ps1 /^ Label12 = 10$/;" e enum:EnumName1
EnumName2 input.ps1 /^enum EnumName2 {$/;" g
Label21 input.ps1 /^ Label21$/;" e enum:EnumName2
Label22 input.ps1 /^ Label22 = 20$/;" e enum:EnumName2
EnumName3 input.ps1 /^Enum EnumName3 {$/;" g
Label31 input.ps1 /^ Label31$/;" e enum:EnumName3
Label32 input.ps1 /^ Label32 = 30$/;" e enum:EnumName3
EnumName4 input.ps1 /^[Flags()] enum EnumName4 {$/;" g
Label41 input.ps1 /^ Label41$/;" e enum:EnumName4
Label42 input.ps1 /^ Label42 = 40$/;" e enum:EnumName4
31 changes: 31 additions & 0 deletions parsers/powershell.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ typedef enum {
K_CLASS,
K_FILTER,
K_ENUM,
K_ENUMLABEL,
COUNT_KIND
} powerShellKind;

Expand All @@ -51,6 +52,7 @@ static kindDefinition PowerShellKinds[COUNT_KIND] = {
{ true, 'c', "class", "classes" },
{ true, 'i', "filter", "filter" },
{ true, 'g', "enum", "enum names" },
{ true, 'e', "enumerator", "enum labels" },
};


Expand Down Expand Up @@ -648,6 +650,30 @@ static bool parseEnum (tokenInfo *const token)
return readNext;
}

/* parses declarations of the form
* <label> [= <int-value>]
* that is, contents of an enum
*/
static bool parseEnumLabel (tokenInfo *const token)
{
bool readNext = true;

if (token->parentKind != K_ENUM)
return false;

if (token->type != TOKEN_IDENTIFIER)
return false;

makeSimplePowerShellTag (token, K_ENUMLABEL, ACCESS_UNDEFINED);
readToken (token);
if (token->type != TOKEN_EQUAL_SIGN)
readNext = false;
else /* skip int-value */
readToken (token);

return readNext;
}

/* parses declarations of the form
* $var = VALUE
*/
Expand Down Expand Up @@ -735,6 +761,11 @@ static void enterScope (tokenInfo *const parentToken,
readNext = parseVariable (token);
break;

case TOKEN_IDENTIFIER:
if (parentKind == K_ENUM)
readNext = parseEnumLabel (token);
break;

default: break;
}

Expand Down

0 comments on commit bad24b7

Please sign in to comment.