From 06939662d8ba6448f17fc65eaf70c3e89bb17cb8 Mon Sep 17 00:00:00 2001 From: kumarstack55 Date: Sat, 3 Sep 2022 12:39:35 +0900 Subject: [PATCH 1/2] PowerShell: add tag generation from class --- .../class-powershell.d/args.ctags | 4 + .../class-powershell.d/expected.tags | 4 + .../class-powershell.d/input.ps1 | 23 ++++++ parsers/powershell.c | 77 +++++++++++++++++-- 4 files changed, 101 insertions(+), 7 deletions(-) create mode 100644 Units/parser-powershell.r/class-powershell.d/args.ctags create mode 100644 Units/parser-powershell.r/class-powershell.d/expected.tags create mode 100644 Units/parser-powershell.r/class-powershell.d/input.ps1 diff --git a/Units/parser-powershell.r/class-powershell.d/args.ctags b/Units/parser-powershell.r/class-powershell.d/args.ctags new file mode 100644 index 0000000000..6dd44f1133 --- /dev/null +++ b/Units/parser-powershell.r/class-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/class-powershell.d/expected.tags b/Units/parser-powershell.r/class-powershell.d/expected.tags new file mode 100644 index 0000000000..bd46eff81c --- /dev/null +++ b/Units/parser-powershell.r/class-powershell.d/expected.tags @@ -0,0 +1,4 @@ +MyException input.ps1 /^class MyException : Exception {$/;" c +Foo input.ps1 /^class Foo {$/;" c +GetBar input.ps1 /^function GetBar {$/;" f +GetBaz input.ps1 /^function GetBaz() {$/;" f signature:() diff --git a/Units/parser-powershell.r/class-powershell.d/input.ps1 b/Units/parser-powershell.r/class-powershell.d/input.ps1 new file mode 100644 index 0000000000..46336660ca --- /dev/null +++ b/Units/parser-powershell.r/class-powershell.d/input.ps1 @@ -0,0 +1,23 @@ +class MyException : Exception { + MyException([String]$Message) : base([String]$Message) { + Write-Host "dummy" + } +} + +class Foo { + $Property1 + $Property2 = 20 + Method($Arg1) { + $LocalVar1 = 100 + Write-Host "dummy" + } +} + +function GetBar { + $LocalVar2 = 200 + Write-Host "dummy" +} + +function GetBaz() { + Write-Host "dummy" +} diff --git a/parsers/powershell.c b/parsers/powershell.c index fad342b364..568be396f4 100644 --- a/parsers/powershell.c +++ b/parsers/powershell.c @@ -39,12 +39,14 @@ static const char *const accessTypes[] = { typedef enum { K_FUNCTION, K_VARIABLE, + K_CLASS, COUNT_KIND } powerShellKind; static kindDefinition PowerShellKinds[COUNT_KIND] = { { true, 'f', "function", "functions" }, - { true, 'v', "variable", "variables" } + { true, 'v', "variable", "variables" }, + { true, 'c', "class", "classes" }, }; @@ -55,7 +57,8 @@ typedef enum eTokenType { TOKEN_SEMICOLON, TOKEN_COLON, TOKEN_COMMA, - TOKEN_KEYWORD, + TOKEN_KEYWORD_FUNCTION, + TOKEN_KEYWORD_CLASS, TOKEN_OPEN_PAREN, TOKEN_OPERATOR, TOKEN_IDENTIFIER, @@ -143,6 +146,18 @@ static void makeFunctionTag (const tokenInfo *const token, const vString *const } } +static void makeClassTag (const tokenInfo *const token) +{ + if (PowerShellKinds[K_CLASS].enabled) + { + tagEntryInfo e; + + initPowerShellEntry (&e, token, K_CLASS, NULL); + + makeTagEntry (&e); + } +} + static tokenInfo *newToken (void) { tokenInfo *const token = xMalloc (1, tokenInfo); @@ -220,6 +235,11 @@ static bool isTokenFunction (vString *const name) strcasecmp (vStringValue (name), "filter") == 0); } +static bool isTokenClass (vString *const name) +{ + return strcasecmp (vStringValue (name), "class") == 0; +} + static bool isSpace (int c) { return (c == '\t' || c == ' ' || c == '\v' || @@ -358,7 +378,9 @@ static void readToken (tokenInfo *const token) { parseIdentifier (token->string, c); if (isTokenFunction (token->string)) - token->type = TOKEN_KEYWORD; + token->type = TOKEN_KEYWORD_FUNCTION; + else if (isTokenClass (token->string)) + token->type = TOKEN_KEYWORD_CLASS; else token->type = TOKEN_IDENTIFIER; } @@ -456,7 +478,6 @@ static bool parseFunction (tokenInfo *const token) case TOKEN_STRING: vStringCatS (arglist, "'...'"); break; case TOKEN_IDENTIFIER: - case TOKEN_KEYWORD: case TOKEN_VARIABLE: { switch (vStringLast (arglist)) @@ -506,6 +527,43 @@ static bool parseFunction (tokenInfo *const token) return readNext; } +/* parse a class + * + * class MyClass {} + */ +static bool parseClass (tokenInfo *const token) +{ + bool readNext = true; + tokenInfo *nameFree = NULL; + const char *access; + + readToken (token); + + if (token->type != TOKEN_IDENTIFIER) + return false; + + nameFree = newToken (); + copyToken (nameFree, token, true); + readToken (token); + + makeClassTag (nameFree); + + while (token->type != TOKEN_OPEN_CURLY && token->type != TOKEN_EOF) + { + readToken (token); + } + + if (token->type == TOKEN_OPEN_CURLY) + enterScope (token, nameFree->string, K_CLASS); + else + readNext = false; + + if (nameFree) + deleteToken (nameFree); + + return readNext; +} + /* parses declarations of the form * $var = VALUE */ @@ -521,8 +579,9 @@ static bool parseVariable (tokenInfo *const token) readToken (token); if (token->type == TOKEN_EQUAL_SIGN) { - if (token->parentKind != K_FUNCTION) - { /* ignore local variables (i.e. within a function) */ + if (token->parentKind != K_FUNCTION && token->parentKind != K_CLASS) + { /* ignore local variables (i.e. within a function) + * TODO: Parses class properties to make tags. */ access = parsePowerShellScope (name); makeSimplePowerShellTag (name, K_VARIABLE, access); readNext = true; @@ -563,10 +622,14 @@ static void enterScope (tokenInfo *const parentToken, enterScope (token, NULL, KIND_GHOST_INDEX); break; - case TOKEN_KEYWORD: + case TOKEN_KEYWORD_FUNCTION: readNext = parseFunction (token); break; + case TOKEN_KEYWORD_CLASS: + readNext = parseClass (token); + break; + case TOKEN_VARIABLE: readNext = parseVariable (token); break; From c59449679b8c545ddd6bf2ecf67dbea1b562d70c Mon Sep 17 00:00:00 2001 From: kumarstack55 Date: Sun, 4 Sep 2022 19:22:23 +0900 Subject: [PATCH 2/2] PowerShell: remove unnecessary if statements --- parsers/powershell.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/parsers/powershell.c b/parsers/powershell.c index 568be396f4..b9cc14b0f0 100644 --- a/parsers/powershell.c +++ b/parsers/powershell.c @@ -521,8 +521,7 @@ static bool parseFunction (tokenInfo *const token) else readNext = false; - if (nameFree) - deleteToken (nameFree); + deleteToken (nameFree); return readNext; } @@ -558,8 +557,7 @@ static bool parseClass (tokenInfo *const token) else readNext = false; - if (nameFree) - deleteToken (nameFree); + deleteToken (nameFree); return readNext; }