Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Updated PowerShell lexer and tests
  • Loading branch information
JohnRoos committed Nov 14, 2022
commit e01dff6c566f29a501c87550e84ed316f745b533
92 changes: 83 additions & 9 deletions lexers/embedded/powershell.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<alias>ps1</alias>
<alias>psm1</alias>
<alias>psd1</alias>
<alias>pwsh</alias>
<filename>*.ps1</filename>
<filename>*.psm1</filename>
<filename>*.psd1</filename>
Expand All @@ -22,6 +23,15 @@
<rule pattern="\s+">
<token type="Text"/>
</rule>
<rule pattern="(\s*)(#)(requires)(\s+)">
<bygroups>
<token type="TextWhitespace"/>
<token type="Comment"/>
<token type="Keyword"/>
<token type="TextWhitespace"/>
</bygroups>
<push state="requires"/>
</rule>
<rule pattern="^(\s*#[#\s]*)(\.(?:component|description|example|externalhelp|forwardhelpcategory|forwardhelptargetname|functionality|inputs|link|notes|outputs|parameter|remotehelprunspace|role|synopsis))([^\n]*$)">
<bygroups>
<token type="Comment"/>
Expand All @@ -46,6 +56,9 @@
<rule pattern="@&#39;\n.*?\n&#39;@">
<token type="LiteralStringHeredoc"/>
</rule>
<rule pattern="@(?=\(|{)|\$(?=\()">
<token type="NameVariableMagic"/>
</rule>
<rule pattern="`[\&#39;&#34;$@-]">
<token type="Punctuation"/>
</rule>
Expand All @@ -56,13 +69,42 @@
<rule pattern="&#39;([^&#39;]|&#39;&#39;)*&#39;">
<token type="LiteralStringSingle"/>
</rule>
<rule pattern="(?&lt;!\S)(function|filter|workflow)(\s*)(global:|script:|private:|env:)?(\w\S*\b)">
<bygroups>
<token type="KeywordDeclaration"/>
<token type="TextWhitespace"/>
<token type="NameVariableMagic"/>
<token type="NameBuiltin"/>
</bygroups>
</rule>
<rule pattern="(?&lt;!\S)(class|configuration)(\s+)(\w\S*)(\s*)(:*)">
<bygroups>
<token type="KeywordDeclaration"/>
<token type="TextWhitespace"/>
<token type="NameBuiltin"/>
<token type="NameBuiltin"/>
<token type="NameBuiltin"/>
</bygroups>
</rule>
<rule pattern="\$false|\$null|\$true(?=\b)">
<token type="NameVariableMagic"/>
</rule>
<rule pattern="(\$|@@|@)((global|script|private|env):)?\w+">
<token type="NameVariable"/>
</rule>
<rule pattern="(parameter|validatenotnullorempty|validatescript|validaterange|validateset|validaterange|validatepattern|validatelength|validatecount|validatenotnullorempty|validatescript|cmdletbinding|alias)\b">
<token type="NameBuiltin"/>
</rule>
<rule pattern="[a-z]\w*-[a-z]\w*\b">
<token type="NameBuiltin"/>
</rule>
<rule pattern="(while|validateset|validaterange|validatepattern|validatelength|validatecount|until|trap|switch|return|ref|process|param|parameter|in|if|global:|function|foreach|for|finally|filter|end|elseif|else|dynamicparam|do|default|continue|cmdletbinding|break|begin|alias|\?|%|#script|#private|#local|#global|mandatory|parametersetname|position|valuefrompipeline|valuefrompipelinebypropertyname|valuefromremainingarguments|helpmessage|try|catch|throw)\b">
<rule pattern="(mandatory|parametersetname|position|helpmessage|valuefrompipeline|valuefrompipelinebypropertyname|valuefromremainingarguments|dontshow)\b">
<token type="NameAttribute"/>
</rule>
<rule pattern="(confirmimpact|defaultparametersetname|helpuri|supportspaging|supportsshouldprocess|positionalbinding)\b">
<token type="NameAttribute"/>
</rule>
<rule pattern="(while|until|trap|switch|return|ref|process|param|parameter|in|if|global:|foreach|for|finally|filter|end|elseif|else|dynamicparam|do|default|continue|break|begin|\?|%|#script|#private|#local|#global|try|catch|throw)\b">
<token type="Keyword"/>
</rule>
<rule pattern="-(and|as|band|bnot|bor|bxor|casesensitive|ccontains|ceq|cge|cgt|cle|clike|clt|cmatch|cne|cnotcontains|cnotlike|cnotmatch|contains|creplace|eq|exact|f|file|ge|gt|icontains|ieq|ige|igt|ile|ilike|ilt|imatch|ine|inotcontains|inotlike|inotmatch|ireplace|is|isnot|le|like|lt|match|ne|not|notcontains|notlike|notmatch|or|regex|replace|wildcard)\b">
Expand All @@ -71,19 +113,47 @@
<rule pattern="(ac|asnp|cat|cd|cfs|chdir|clc|clear|clhy|cli|clp|cls|clv|cnsn|compare|copy|cp|cpi|cpp|curl|cvpa|dbp|del|diff|dir|dnsn|ebp|echo|epal|epcsv|epsn|erase|etsn|exsn|fc|fhx|fl|foreach|ft|fw|gal|gbp|gc|gci|gcm|gcs|gdr|ghy|gi|gjb|gl|gm|gmo|gp|gps|gpv|group|gsn|gsnp|gsv|gu|gv|gwmi|h|history|icm|iex|ihy|ii|ipal|ipcsv|ipmo|ipsn|irm|ise|iwmi|iwr|kill|lp|ls|man|md|measure|mi|mount|move|mp|mv|nal|ndr|ni|nmo|npssc|nsn|nv|ogv|oh|popd|ps|pushd|pwd|r|rbp|rcjb|rcsn|rd|rdr|ren|ri|rjb|rm|rmdir|rmo|rni|rnp|rp|rsn|rsnp|rujb|rv|rvpa|rwmi|sajb|sal|saps|sasv|sbp|sc|select|set|shcm|si|sl|sleep|sls|sort|sp|spjb|spps|spsv|start|sujb|sv|swmi|tee|trcm|type|wget|where|wjb|write)\s">
<token type="NameBuiltin"/>
</rule>
<rule pattern="\[[a-z_\[][\w. `,\[\]]*\]">
<token type="NameConstant"/>
<rule pattern="(\[)([a-z_\[][\w. `,\[\]]*)(\])">
<bygroups>
<token type="Punctuation"/>
<token type="NameConstant"/>
<token type="Punctuation"/>
</bygroups>
</rule>
<rule pattern="(?&lt;!\[)(?&lt;=\S[^\*|\n]\.)\w+(?=\s+|\(|\{|\.)">
<token type="NameProperty"/>
</rule>
<rule pattern="(?&lt;!\w)([-+]?(?:[0-9]+)?\.?[0-9]+(?:(?:e|E)[0-9]+)?(?:F|f|D|d|M|m)?)((?i:[kmgtp]b)?)\b">
<bygroups>
<token type="LiteralNumberFloat"/>
<token type="Punctuation"/>
</bygroups>
</rule>
<rule pattern="-[a-z_]\w*">
<rule pattern="-[a-z_]\w*:*">
<token type="Name"/>
</rule>
<rule pattern="\w+">
<token type="Name"/>
</rule>
<rule pattern="[.,;@{}\[\]$()=+*/\\&amp;%!~?^`|&lt;&gt;-]|::">
<rule pattern="[.,;@{}\[\]$()=+*/\\&amp;%!~?^\x60|&lt;&gt;-]|::">
<token type="Punctuation"/>
</rule>
</state>
<state name="requires">
<rule pattern="\s*\n|\s*$">
<token type="TextWhitespace"/>
<pop depth="1"/>
</rule>
<rule pattern="-(?i:modules|pssnapin|runasadministrator|ahellid|version|assembly|psedition)">
<token type="KeywordDeclaration"/>
</rule>
<rule pattern="-\S*\b">
<token type="Comment"/>
</rule>
<rule pattern="\s+(\S+)">
<token type="NameAttribute"/>
</rule>
</state>
<state name="child">
<rule pattern="\)">
<token type="Punctuation"/>
Expand All @@ -101,8 +171,12 @@
<token type="CommentMultiline"/>
<pop depth="1"/>
</rule>
<rule pattern="\.(component|description|example|externalhelp|forwardhelpcategory|forwardhelptargetname|functionality|inputs|link|notes|outputs|parameter|remotehelprunspace|role|synopsis)">
<token type="LiteralStringDoc"/>
<rule pattern="(\s*\.)(component|description|example|externalhelp|forwardhelpcategory|forwardhelptargetname|functionality|inputs|link|notes|outputs|parameter|remotehelprunspace|role|synopsis)(\s*$)">
<bygroups>
<token type="CommentMultiline"/>
<token type="LiteralStringDoc"/>
<token type="CommentMultiline"/>
</bygroups>
</rule>
<rule pattern="[#&amp;.]">
<token type="CommentMultiline"/>
Expand All @@ -119,7 +193,7 @@
<token type="Punctuation"/>
<push state="child"/>
</rule>
<rule pattern="(\$)((global|script|private|env):)?\w+">
<rule pattern="((\$)((global|script|private|env):)?\w+)|((\$){((global|script|private|env):)?\w+})">
<token type="NameVariable"/>
</rule>
<rule pattern="&#34;&#34;">
Expand All @@ -142,7 +216,7 @@
<token type="Punctuation"/>
<push state="child"/>
</rule>
<rule pattern="(\$)((global|script|private|env):)?\w+">
<rule pattern="((\$)((global|script|private|env):)?\w+)|((\$){((global|script|private|env):)?\w+})">
<token type="NameVariable"/>
</rule>
<rule pattern="[^@\n]+&#34;]">
Expand Down
87 changes: 82 additions & 5 deletions lexers/testdata/powershell.actual
Original file line number Diff line number Diff line change
@@ -1,5 +1,82 @@
Get-ChildItem -Recurse -Force -ErrorAction SilentlyContinue -Name -Path C:\ *.txt
$x = "Here is a $var inside a string"
$x = @"
Here is a $var inside a here-string
"@
Get-ChildItem -Recurse -Force -ErrorAction SilentlyContinue -Name -Path C:\ *.txt

#Requires -modules ModuleName
#Requires -Assembly "System.Management.Automation, Version=1.0.0.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35"
#Requires -Modules @{ ModuleName="ModuleName"; ModuleVersion="1.2.3" }
#Requires -PSSnapin SnapinName -version 1.2
#Requires -RunAsAdministrator
#Requires -WrongParameter ParamValue

<#
Multiline comment
.DESCRIPTION
This is a description
#>

# this is a comment

function test { }
class test {}
class test2 : test { }

@"
double quoted $MyVar here string
"@

@"
double quoted ${MyVar} here string
"@

@"
double quoted $(Get-Stuff -Param1 $MyVar ) here string
"@

@'
single quoted $(Get-Stuff -Param1 $MyVar ) here string
'@

'Single quoted string $MyVar'
"Double quoted string $MyVar"
"Double quoted string $(Get-Stuff -Param1 $MyVar )"
"Double quoted string ${var}"

$HashTable = @{
'Something' = $Var
'Else' = 'string'
}

$myvar.'property name in string'
[string]::UseMethod(12.34)
15gb
@{}
@()
$()
$test = C:\test
$MyVar = Get-Something -SwitchParam:$false
$MyVar.property
$MyVar.property.property.somemethod()
$global:test
$null
$true
$false

Configuration ConfigurationName
{
Import-DSCResource -ModuleName MyModule
}

function Get-Stuff {
[CmdletBinding(SupportsShouldProcess=$true)]
param (
[Parameter(Mandatory=$true, DontShow)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ $_ -ne $null })]
[ValidateRange(0, [int]::MaxValue)]
[Microsoft.PowerShell.Commands.WebRequestMethod]$Param1 = 'Get'
)

Process
{
if ($MyVar -lt (Get-Date).AddSeconds([int](12.4 / 2))) {}
}
}
Loading