Skip to content

Commit 299fe52

Browse files
authored
Merge pull request #3480 from kumarstack55/develop
PowerShell: introduce filter kind
2 parents 943aa3b + 6a52c7e commit 299fe52

File tree

5 files changed

+76
-34
lines changed

5 files changed

+76
-34
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--sort=no
2+
# "qualified" extra doesn't work.
3+
# --extras=+q
4+
--fields=+S
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
f1 input.ps1 /^filter f1 {$/;" i
2+
f2 input.ps1 /^filter f2() {$/;" i signature:()
3+
f3 input.ps1 /^filter f3([switch]$Message) {$/;" i signature:([switch] $Message)
4+
f4 input.ps1 /^filter f4 {$/;" i
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
filter f1 {
2+
}
3+
4+
filter f2() {
5+
}
6+
7+
filter f3([switch]$Message) {
8+
}
9+
10+
filter f4 {
11+
$LocalVariable = 1
12+
}

Units/parser-powershell.r/simple-powershell.d/expected.tags

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ Read-Configuration-File input.ps1 /^FUNCTION Read-Configuration-File() {$/;" f s
66
LogMessageOK input.ps1 /^Function LogMessageOK()$/;" f signature:()
77
LogMessage input.ps1 /^function LogMessage() {$/;" f signature:()
88
A-Global-Scope-Function input.ps1 /^function global:A-Global-Scope-Function() {$/;" f signature:()
9-
MyFilter input.ps1 /^filter Script:MyFilter {$/;" f
10-
MyPrivateFilter input.ps1 /^Filter Private:MyPrivateFilter {$/;" f
9+
MyFilter input.ps1 /^filter Script:MyFilter {$/;" i
10+
MyPrivateFilter input.ps1 /^Filter Private:MyPrivateFilter {$/;" i
1111
LoadTemplate input.ps1 /^function LoadTemplate($template) {$/;" f signature:($template)
1212
TopLevelFunction input.ps1 /^function TopLevelFunction() {$/;" f signature:()
1313
SecondLevelNestedFunction input.ps1 /^ function SecondLevelNestedFunction() {$/;" f function:TopLevelFunction signature:()

parsers/powershell.c

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@ typedef enum {
4040
K_FUNCTION,
4141
K_VARIABLE,
4242
K_CLASS,
43+
K_FILTER,
4344
COUNT_KIND
4445
} powerShellKind;
4546

4647
static kindDefinition PowerShellKinds[COUNT_KIND] = {
4748
{ true, 'f', "function", "functions" },
4849
{ true, 'v', "variable", "variables" },
4950
{ true, 'c', "class", "classes" },
51+
{ true, 'i', "filter", "filter" },
5052
};
5153

5254

@@ -57,8 +59,7 @@ typedef enum eTokenType {
5759
TOKEN_SEMICOLON,
5860
TOKEN_COLON,
5961
TOKEN_COMMA,
60-
TOKEN_KEYWORD_FUNCTION,
61-
TOKEN_KEYWORD_CLASS,
62+
TOKEN_KEYWORD,
6263
TOKEN_OPEN_PAREN,
6364
TOKEN_OPERATOR,
6465
TOKEN_IDENTIFIER,
@@ -72,13 +73,29 @@ typedef enum eTokenType {
7273
TOKEN_VARIABLE
7374
} tokenType;
7475

76+
enum {
77+
KEYWORD_function,
78+
KEYWORD_filter,
79+
KEYWORD_class,
80+
};
81+
82+
/* We need an integer that is not an unsigned to allow KEYWORD_NONE. */
83+
typedef int keywordId;
84+
85+
static const keywordTable PowerShellKeywordTable[] = {
86+
{ "function", KEYWORD_function },
87+
{ "filter", KEYWORD_filter },
88+
{ "class", KEYWORD_class },
89+
};
90+
7591
typedef struct {
7692
tokenType type;
93+
keywordId keyword;
7794
vString * string;
7895
vString * scope;
7996
unsigned long lineNumber;
8097
MIOPos filePosition;
81-
int parentKind; /* KIND_GHOST_INDEX if none */
98+
int parentKind; /* KIND_GHOST_INDEX if none */
8299
} tokenInfo;
83100

84101

@@ -131,13 +148,13 @@ static void makeSimplePowerShellTag (const tokenInfo *const token, const powerSh
131148
}
132149

133150
static void makeFunctionTag (const tokenInfo *const token, const vString *const arglist,
134-
const char *const access)
151+
const char *const access, int kind)
135152
{
136-
if (PowerShellKinds[K_FUNCTION].enabled)
153+
if (PowerShellKinds[kind].enabled)
137154
{
138155
tagEntryInfo e;
139156

140-
initPowerShellEntry (&e, token, K_FUNCTION, access);
157+
initPowerShellEntry (&e, token, kind, access);
141158

142159
if (arglist)
143160
e.extensionFields.signature = vStringValue (arglist);
@@ -163,6 +180,7 @@ static tokenInfo *newToken (void)
163180
tokenInfo *const token = xMalloc (1, tokenInfo);
164181

165182
token->type = TOKEN_UNDEFINED;
183+
token->keyword = KEYWORD_NONE;
166184
token->string = vStringNew ();
167185
token->scope = vStringNew ();
168186
token->lineNumber = getInputLineNumber ();
@@ -229,17 +247,6 @@ static void parseIdentifier (vString *const string, const int firstChar)
229247
ungetcToInputFile (c);
230248
}
231249

232-
static bool isTokenFunction (vString *const name)
233-
{
234-
return (strcasecmp (vStringValue (name), "function") == 0 ||
235-
strcasecmp (vStringValue (name), "filter") == 0);
236-
}
237-
238-
static bool isTokenClass (vString *const name)
239-
{
240-
return strcasecmp (vStringValue (name), "class") == 0;
241-
}
242-
243250
static bool isSpace (int c)
244251
{
245252
return (c == '\t' || c == ' ' || c == '\v' ||
@@ -377,12 +384,12 @@ static void readToken (tokenInfo *const token)
377384
else
378385
{
379386
parseIdentifier (token->string, c);
380-
if (isTokenFunction (token->string))
381-
token->type = TOKEN_KEYWORD_FUNCTION;
382-
else if (isTokenClass (token->string))
383-
token->type = TOKEN_KEYWORD_CLASS;
384-
else
387+
token->keyword = lookupCaseKeyword (
388+
vStringValue (token->string), getInputLanguage ());
389+
if (token->keyword == KEYWORD_NONE)
385390
token->type = TOKEN_IDENTIFIER;
391+
else
392+
token->type = TOKEN_KEYWORD;
386393
}
387394
break;
388395
}
@@ -428,7 +435,7 @@ static const char *parsePowerShellScope (tokenInfo *const token)
428435
*
429436
* function myfunc($foo, $bar) {}
430437
*/
431-
static bool parseFunction (tokenInfo *const token)
438+
static bool parseFunction (tokenInfo *const token, int kind)
432439
{
433440
bool readNext = true;
434441
tokenInfo *nameFree = NULL;
@@ -506,18 +513,18 @@ static bool parseFunction (tokenInfo *const token)
506513
}
507514
while (token->type != TOKEN_EOF && depth > 0);
508515

509-
makeFunctionTag (nameFree, arglist, access);
516+
makeFunctionTag (nameFree, arglist, access, kind);
510517
vStringDelete (arglist);
511518

512519
readToken (token);
513520
}
514521
else if (token->type == TOKEN_OPEN_CURLY)
515522
{ /* filters doesn't need to have an arglist */
516-
makeFunctionTag (nameFree, NULL, access);
523+
makeFunctionTag (nameFree, NULL, access, kind);
517524
}
518525

519526
if (token->type == TOKEN_OPEN_CURLY)
520-
enterScope (token, nameFree->string, K_FUNCTION);
527+
enterScope (token, nameFree->string, kind);
521528
else
522529
readNext = false;
523530

@@ -576,7 +583,9 @@ static bool parseVariable (tokenInfo *const token)
576583
readToken (token);
577584
if (token->type == TOKEN_EQUAL_SIGN)
578585
{
579-
if (token->parentKind != K_FUNCTION && token->parentKind != K_CLASS)
586+
if (token->parentKind != K_FUNCTION &&
587+
token->parentKind != K_FILTER &&
588+
token->parentKind != K_CLASS)
580589
{ /* ignore local variables (i.e. within a function)
581590
* TODO: Parses class properties to make tags. */
582591
access = parsePowerShellScope (name);
@@ -619,12 +628,23 @@ static void enterScope (tokenInfo *const parentToken,
619628
enterScope (token, NULL, KIND_GHOST_INDEX);
620629
break;
621630

622-
case TOKEN_KEYWORD_FUNCTION:
623-
readNext = parseFunction (token);
624-
break;
631+
case TOKEN_KEYWORD:
632+
switch (token->keyword)
633+
{
634+
case KEYWORD_function:
635+
readNext = parseFunction (token, K_FUNCTION);
636+
break;
637+
638+
case KEYWORD_filter:
639+
readNext = parseFunction (token, K_FILTER);
640+
break;
625641

626-
case TOKEN_KEYWORD_CLASS:
627-
readNext = parseClass (token);
642+
case KEYWORD_class:
643+
readNext = parseClass (token);
644+
break;
645+
646+
default: break;
647+
}
628648
break;
629649

630650
case TOKEN_VARIABLE:
@@ -664,5 +684,7 @@ extern parserDefinition* PowerShellParser (void)
664684
def->kindCount = ARRAY_SIZE (PowerShellKinds);
665685
def->extensions = extensions;
666686
def->parser = findPowerShellTags;
687+
def->keywordTable = PowerShellKeywordTable;
688+
def->keywordCount = ARRAY_SIZE (PowerShellKeywordTable);
667689
return def;
668690
}

0 commit comments

Comments
 (0)