From c60f2e028f8f9b29dee98a48a63240b2b3ff81d2 Mon Sep 17 00:00:00 2001 From: kumarstack55 Date: Sat, 3 Dec 2022 23:19:04 +0900 Subject: [PATCH] PowerShell: introduce enum kind --- .../enum-powershell.d/args.ctags | 4 ++ .../enum-powershell.d/expected.tags | 4 ++ .../enum-powershell.d/input.ps1 | 23 ++++++++ parsers/powershell.c | 53 +++++++++++++++++++ 4 files changed, 84 insertions(+) create mode 100644 Units/parser-powershell.r/enum-powershell.d/args.ctags create mode 100644 Units/parser-powershell.r/enum-powershell.d/expected.tags create mode 100644 Units/parser-powershell.r/enum-powershell.d/input.ps1 diff --git a/Units/parser-powershell.r/enum-powershell.d/args.ctags b/Units/parser-powershell.r/enum-powershell.d/args.ctags new file mode 100644 index 0000000000..6dd44f1133 --- /dev/null +++ b/Units/parser-powershell.r/enum-powershell.d/args.ctags @@ -0,0 +1,4 @@ +--sort=no +# "qualified" extra doesn't work. +# --extras=+q +--fields=+S diff --git a/Units/parser-powershell.r/enum-powershell.d/expected.tags b/Units/parser-powershell.r/enum-powershell.d/expected.tags new file mode 100644 index 0000000000..78a0a7b429 --- /dev/null +++ b/Units/parser-powershell.r/enum-powershell.d/expected.tags @@ -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 diff --git a/Units/parser-powershell.r/enum-powershell.d/input.ps1 b/Units/parser-powershell.r/enum-powershell.d/input.ps1 new file mode 100644 index 0000000000..f7e9cf2f01 --- /dev/null +++ b/Units/parser-powershell.r/enum-powershell.d/input.ps1 @@ -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 +} diff --git a/parsers/powershell.c b/parsers/powershell.c index 293f98d36a..fd37fe824d 100644 --- a/parsers/powershell.c +++ b/parsers/powershell.c @@ -41,6 +41,7 @@ typedef enum { K_VARIABLE, K_CLASS, K_FILTER, + K_ENUM, COUNT_KIND } powerShellKind; @@ -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" }, }; @@ -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. */ @@ -86,6 +89,7 @@ static const keywordTable PowerShellKeywordTable[] = { { "function", KEYWORD_function }, { "filter", KEYWORD_filter }, { "class", KEYWORD_class }, + { "enum", KEYWORD_enum }, }; typedef struct { @@ -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); @@ -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 */ @@ -675,6 +724,10 @@ static void enterScope (tokenInfo *const parentToken, readNext = parseClass (token); break; + case KEYWORD_enum: + readNext = parseEnum (token); + break; + default: break; } break;