Skip to content

Commit

Permalink
PowerShell: introduce enum kind
Browse files Browse the repository at this point in the history
  • Loading branch information
kumarstack55 committed Dec 9, 2022
1 parent b307057 commit c60f2e0
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Units/parser-powershell.r/enum-powershell.d/args.ctags
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
--sort=no
# "qualified" extra doesn't work.
# --extras=+q
--fields=+S
4 changes: 4 additions & 0 deletions Units/parser-powershell.r/enum-powershell.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
EnumName1 input.ps1 /^enum EnumName1 {$/;" g
EnumName2 input.ps1 /^enum EnumName2 {$/;" g
EnumName3 input.ps1 /^Enum EnumName3 {$/;" g
EnumName4 input.ps1 /^[Flags()] enum EnumName4 {$/;" g
23 changes: 23 additions & 0 deletions Units/parser-powershell.r/enum-powershell.d/input.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# EnumName1
enum EnumName1 {
Label11
Label12 = 10
}

# EnumName2
enum EnumName2 {
Label21
Label22 = 20
}

# EnumName3
Enum EnumName3 {
Label31
Label32 = 30
}

# EnumName4
[Flags()] enum EnumName4 {
Label41
Label42 = 40
}
53 changes: 53 additions & 0 deletions parsers/powershell.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ typedef enum {
K_VARIABLE,
K_CLASS,
K_FILTER,
K_ENUM,
COUNT_KIND
} powerShellKind;

Expand All @@ -49,6 +50,7 @@ static kindDefinition PowerShellKinds[COUNT_KIND] = {
{ true, 'v', "variable", "variables" },
{ true, 'c', "class", "classes" },
{ true, 'i', "filter", "filter" },
{ true, 'g', "enum", "enum names" },
};


Expand Down Expand Up @@ -77,6 +79,7 @@ enum {
KEYWORD_function,
KEYWORD_filter,
KEYWORD_class,
KEYWORD_enum,
};

/* We need an integer that is not an unsigned to allow KEYWORD_NONE. */
Expand All @@ -86,6 +89,7 @@ static const keywordTable PowerShellKeywordTable[] = {
{ "function", KEYWORD_function },
{ "filter", KEYWORD_filter },
{ "class", KEYWORD_class },
{ "enum", KEYWORD_enum },
};

typedef struct {
Expand Down Expand Up @@ -175,6 +179,18 @@ static void makeClassTag (const tokenInfo *const token)
}
}

static void makeEnumTag (const tokenInfo *const token)
{
if (PowerShellKinds[K_ENUM].enabled)
{
tagEntryInfo e;

initPowerShellEntry (&e, token, K_ENUM, NULL);

makeTagEntry (&e);
}
}

static tokenInfo *newToken (void)
{
tokenInfo *const token = xMalloc (1, tokenInfo);
Expand Down Expand Up @@ -600,6 +616,39 @@ static bool parseClass (tokenInfo *const token)
return readNext;
}

/* parse a enum
*
* enum EnumName {}
*/
static bool parseEnum (tokenInfo *const token)
{
bool readNext = true;
vString *nameFree = NULL;

readToken (token);

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

makeEnumTag (token);
nameFree = vStringNewCopy (token->string);
readToken (token);

while (token->type != TOKEN_OPEN_CURLY && token->type != TOKEN_EOF)
{
readToken (token);
}

if (token->type == TOKEN_OPEN_CURLY)
enterScope (token, nameFree, K_ENUM);
else
readNext = false;

vStringDelete (nameFree);

return readNext;
}

/* parses declarations of the form
* $var = VALUE
*/
Expand Down Expand Up @@ -675,6 +724,10 @@ static void enterScope (tokenInfo *const parentToken,
readNext = parseClass (token);
break;

case KEYWORD_enum:
readNext = parseEnum (token);
break;

default: break;
}
break;
Expand Down

0 comments on commit c60f2e0

Please sign in to comment.