Skip to content

Commit

Permalink
Merge pull request #3476 from kumarstack55/develop
Browse files Browse the repository at this point in the history
PowerShell: add tag generation from class
  • Loading branch information
masatake committed Sep 5, 2022
2 parents 09c8b03 + c594496 commit a5721f6
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 9 deletions.
4 changes: 4 additions & 0 deletions Units/parser-powershell.r/class-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/class-powershell.d/expected.tags
Original file line number Diff line number Diff line change
@@ -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:()
23 changes: 23 additions & 0 deletions Units/parser-powershell.r/class-powershell.d/input.ps1
Original file line number Diff line number Diff line change
@@ -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"
}
79 changes: 70 additions & 9 deletions parsers/powershell.c
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
};


Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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' ||
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -500,8 +521,43 @@ static bool parseFunction (tokenInfo *const token)
else
readNext = false;

if (nameFree)
deleteToken (nameFree);
deleteToken (nameFree);

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;

deleteToken (nameFree);

return readNext;
}
Expand All @@ -521,8 +577,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;
Expand Down Expand Up @@ -563,10 +620,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;
Expand Down

0 comments on commit a5721f6

Please sign in to comment.