Skip to content

Commit 7c21251

Browse files
authored
Update the regex for the PowerShell parser (#33)
* Update the regex for powershell * Fix typo * Make attribute and keyword matching case insensitive * Fix a typo
1 parent f5be673 commit 7c21251

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

ColorCode.Core/Common/ScopeName.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ public class ScopeName
2222
public const string Keyword = "Keyword";
2323
public const string LanguagePrefix = "&";
2424
public const string PlainText = "Plain Text";
25-
public const string PowerShellAttribute = "PowerShell PowerShellAttribute";
25+
public const string PowerShellAttribute = "PowerShell Attribute";
2626
public const string PowerShellOperator = "PowerShell Operator";
2727
public const string PowerShellType = "PowerShell Type";
2828
public const string PowerShellVariable = "PowerShell Variable";
29+
public const string PowerShellCommand = "PowerShell Command";
30+
public const string PowerShellParameter = "PowerShell Parameter";
2931
public const string PreprocessorKeyword = "Preprocessor Keyword";
3032
public const string SqlSystemFunction = "SQL System Function";
3133
public const string String = "String";

ColorCode.Core/Compilation/Languages/PowerShell.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,17 @@ public IList<LanguageRule> Rules
7878
{0, ScopeName.PowerShellVariable}
7979
}),
8080
new LanguageRule(
81-
@"\b(begin|break|catch|continue|data|do|dynamicparam|elseif|else|end|exit|filter|finally|foreach|for|from|function|if|in|param|process|return|switch|throw|trap|try|until|while)\b",
81+
@"(?i)\b(begin|break|catch|continue|data|do|dynamicparam|elseif|else|end|exit|filter|finally|foreach|for|from|function|if|in|param|process|return|switch|throw|trap|try|until|while)\b",
8282
new Dictionary<int, string>
8383
{
8484
{1, ScopeName.Keyword}
8585
}),
86+
new LanguageRule(
87+
@"\b(\w+\-\w+)\b",
88+
new Dictionary<int, string>
89+
{
90+
{1, ScopeName.PowerShellCommand}
91+
}),
8692
new LanguageRule(
8793
@"-(?:c|i)?(?:eq|ne|gt|ge|lt|le|notlike|like|notmatch|match|notcontains|contains|replace)",
8894
new Dictionary<int, string>
@@ -98,7 +104,14 @@ public IList<LanguageRule> Rules
98104
}
99105
),
100106
new LanguageRule(
101-
@"(?:\+=|-=|\*=|/=|%=|=|\+\+|--|\+|-|\*|/|%)",
107+
@"-\w+\d*\w*",
108+
new Dictionary<int, string>
109+
{
110+
{0, ScopeName.PowerShellParameter}
111+
}
112+
),
113+
new LanguageRule(
114+
@"(?:\+=|-=|\*=|/=|%=|=|\+\+|--|\+|-|\*|/|%|\||,)",
102115
new Dictionary<int, string>
103116
{
104117
{0, ScopeName.PowerShellOperator}
@@ -112,7 +125,7 @@ public IList<LanguageRule> Rules
112125
}
113126
),
114127
new LanguageRule(
115-
@"(?s)\[(CmdletBinding)[^\]]+\]",
128+
@"(?is)\[(cmdletbinding|alias|outputtype|parameter|validatenotnull|validatenotnullorempty|validatecount|validateset|allownull|allowemptycollection|allowemptystring|validatescript|validaterange|validatepattern|validatelength|supportswildcards)[^\]]+\]",
116129
new Dictionary<int, string>
117130
{
118131
{1, ScopeName.PowerShellAttribute}
@@ -136,6 +149,7 @@ public bool HasAlias(string lang)
136149
{
137150
case "posh":
138151
case "ps1":
152+
case "pwsh":
139153
return true;
140154

141155
default:

ColorCode.Core/Styling/StyleDictionary.DefaultDark.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,16 @@ public static StyleDictionary DefaultDark
197197
Foreground = OrangeRed,
198198
ReferenceName = "powershellVariable"
199199
},
200+
new Style(ScopeName.PowerShellCommand)
201+
{
202+
Foreground = Yellow,
203+
ReferenceName = "powershellCommand"
204+
},
205+
new Style(ScopeName.PowerShellParameter)
206+
{
207+
Foreground = VSDarkGray,
208+
ReferenceName = "powershellParameter"
209+
},
200210

201211
new Style(ScopeName.Type)
202212
{

ColorCode.Core/Styling/StyleDictionary.DefaultLight.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,16 @@ public static StyleDictionary DefaultLight
181181
Foreground = OrangeRed,
182182
ReferenceName = "powershellVariable"
183183
},
184+
new Style(ScopeName.PowerShellCommand)
185+
{
186+
Foreground = Navy,
187+
ReferenceName = "powershellCommand"
188+
},
189+
new Style(ScopeName.PowerShellParameter)
190+
{
191+
Foreground = Gray,
192+
ReferenceName = "powershellParameter"
193+
},
184194

185195
new Style(ScopeName.Type)
186196
{

0 commit comments

Comments
 (0)