Skip to content

Commit

Permalink
PowerShell: use keyword api
Browse files Browse the repository at this point in the history
This commit reverts the keyword type that was removed in a previous PR
and use keyword api.

Close universal-ctags#3479
  • Loading branch information
kumarstack55 committed Sep 13, 2022
1 parent fe5a84e commit 143746b
Showing 1 changed file with 38 additions and 23 deletions.
61 changes: 38 additions & 23 deletions parsers/powershell.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ typedef enum eTokenType {
TOKEN_SEMICOLON,
TOKEN_COLON,
TOKEN_COMMA,
TOKEN_KEYWORD_FUNCTION,
TOKEN_KEYWORD_CLASS,
TOKEN_KEYWORD,
TOKEN_OPEN_PAREN,
TOKEN_OPERATOR,
TOKEN_IDENTIFIER,
Expand All @@ -72,8 +71,24 @@ typedef enum eTokenType {
TOKEN_VARIABLE
} tokenType;

enum {
KEYWORD_function,
KEYWORD_filter,
KEYWORD_class,
};

/* We need an integer that is not an unsigned to allow KEYWORD_NONE. */
typedef int keywordId;

static const keywordTable PowerShellKeywordTable[] = {
{ "function", KEYWORD_function },
{ "filter", KEYWORD_filter },
{ "class", KEYWORD_class },
};

typedef struct {
tokenType type;
keywordId keyword;
vString * string;
vString * scope;
unsigned long lineNumber;
Expand Down Expand Up @@ -163,6 +178,7 @@ static tokenInfo *newToken (void)
tokenInfo *const token = xMalloc (1, tokenInfo);

token->type = TOKEN_UNDEFINED;
token->keyword = KEYWORD_NONE;
token->string = vStringNew ();
token->scope = vStringNew ();
token->lineNumber = getInputLineNumber ();
Expand Down Expand Up @@ -229,17 +245,6 @@ static void parseIdentifier (vString *const string, const int firstChar)
ungetcToInputFile (c);
}

static bool isTokenFunction (vString *const name)
{
return (strcasecmp (vStringValue (name), "function") == 0 ||
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 @@ -377,12 +382,12 @@ static void readToken (tokenInfo *const token)
else
{
parseIdentifier (token->string, c);
if (isTokenFunction (token->string))
token->type = TOKEN_KEYWORD_FUNCTION;
else if (isTokenClass (token->string))
token->type = TOKEN_KEYWORD_CLASS;
else
token->keyword = lookupCaseKeyword (
vStringValue (token->string), getInputLanguage ());
if (token->keyword == KEYWORD_NONE)
token->type = TOKEN_IDENTIFIER;
else
token->type = TOKEN_KEYWORD;
}
break;
}
Expand Down Expand Up @@ -619,12 +624,20 @@ static void enterScope (tokenInfo *const parentToken,
enterScope (token, NULL, KIND_GHOST_INDEX);
break;

case TOKEN_KEYWORD_FUNCTION:
readNext = parseFunction (token);
break;
case TOKEN_KEYWORD:
switch (token->keyword)
{
case KEYWORD_function:
case KEYWORD_filter:
readNext = parseFunction (token);
break;

case TOKEN_KEYWORD_CLASS:
readNext = parseClass (token);
case KEYWORD_class:
readNext = parseClass (token);
break;

default: break;
}
break;

case TOKEN_VARIABLE:
Expand Down Expand Up @@ -664,5 +677,7 @@ extern parserDefinition* PowerShellParser (void)
def->kindCount = ARRAY_SIZE (PowerShellKinds);
def->extensions = extensions;
def->parser = findPowerShellTags;
def->keywordTable = PowerShellKeywordTable;
def->keywordCount = ARRAY_SIZE (PowerShellKeywordTable);
return def;
}

0 comments on commit 143746b

Please sign in to comment.