From f07788e465e4cd79954274b03f6ebeacf1542c4b Mon Sep 17 00:00:00 2001 From: John Roos Date: Mon, 14 Nov 2022 20:52:16 +0100 Subject: [PATCH] More improvements to PowerShell lexer (#708) --- lexers/embedded/powershell.xml | 92 ++++++++- lexers/testdata/powershell.actual | 87 +++++++- lexers/testdata/powershell.expected | 303 +++++++++++++++++++++++++++- 3 files changed, 459 insertions(+), 23 deletions(-) diff --git a/lexers/embedded/powershell.xml b/lexers/embedded/powershell.xml index 8eaa5807a..b63a15081 100644 --- a/lexers/embedded/powershell.xml +++ b/lexers/embedded/powershell.xml @@ -6,6 +6,7 @@ ps1 psm1 psd1 + pwsh *.ps1 *.psm1 *.psd1 @@ -22,6 +23,15 @@ + + + + + + + + + @@ -46,6 +56,9 @@ + + + @@ -56,13 +69,42 @@ + + + + + + + + + + + + + + + + + + + + + + + - + + + + + + + @@ -71,19 +113,47 @@ - - + + + + + + + + + + + + + + + - + - + + + + + + + + + + + + + + + + @@ -101,8 +171,12 @@ - - + + + + + + @@ -119,7 +193,7 @@ - + @@ -142,7 +216,7 @@ - + diff --git a/lexers/testdata/powershell.actual b/lexers/testdata/powershell.actual index f4b3dad92..16acc3571 100644 --- a/lexers/testdata/powershell.actual +++ b/lexers/testdata/powershell.actual @@ -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 -"@ \ No newline at end of file +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))) {} + } +} \ No newline at end of file diff --git a/lexers/testdata/powershell.expected b/lexers/testdata/powershell.expected index e513f1167..702f60c6b 100644 --- a/lexers/testdata/powershell.expected +++ b/lexers/testdata/powershell.expected @@ -12,26 +12,311 @@ {"type":"Name","value":"-Name"}, {"type":"Text","value":" "}, {"type":"Name","value":"-Path"}, - {"type":"Text","value":" "}, + {"type":"Text","value":" "}, {"type":"Name","value":"C:"}, {"type":"Punctuation","value":"\\"}, {"type":"Text","value":" "}, {"type":"Punctuation","value":"*."}, {"type":"Name","value":"txt"}, + {"type":"Text","value":"\n\n"}, + {"type":"Comment","value":"#"}, + {"type":"Keyword","value":"Requires"}, + {"type":"TextWhitespace","value":" "}, + {"type":"KeywordDeclaration","value":"-modules"}, + {"type":"NameAttribute","value":" ModuleName"}, + {"type":"TextWhitespace","value":"\n"}, + {"type":"Comment","value":"#"}, + {"type":"Keyword","value":"Requires"}, + {"type":"TextWhitespace","value":" "}, + {"type":"KeywordDeclaration","value":"-Assembly"}, + {"type":"NameAttribute","value":" \"System.Management.Automation, Version=1.0.0.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35\""}, + {"type":"TextWhitespace","value":"\n"}, + {"type":"Comment","value":"#"}, + {"type":"Keyword","value":"Requires"}, + {"type":"TextWhitespace","value":" "}, + {"type":"KeywordDeclaration","value":"-Modules"}, + {"type":"NameAttribute","value":" @{ ModuleName=\"ModuleName\"; ModuleVersion=\"1.2.3\" }"}, + {"type":"TextWhitespace","value":"\n"}, + {"type":"Comment","value":"#"}, + {"type":"Keyword","value":"Requires"}, + {"type":"TextWhitespace","value":" "}, + {"type":"KeywordDeclaration","value":"-PSSnapin"}, + {"type":"NameAttribute","value":" SnapinName -version 1.2"}, + {"type":"TextWhitespace","value":"\n"}, + {"type":"Comment","value":"#"}, + {"type":"Keyword","value":"Requires"}, + {"type":"TextWhitespace","value":" "}, + {"type":"KeywordDeclaration","value":"-RunAsAdministrator"}, + {"type":"TextWhitespace","value":"\n"}, + {"type":"Comment","value":"#"}, + {"type":"Keyword","value":"Requires"}, + {"type":"TextWhitespace","value":" "}, + {"type":"Comment","value":"-WrongParameter"}, + {"type":"NameAttribute","value":" ParamValue"}, + {"type":"TextWhitespace","value":"\n\n"}, + {"type":"CommentMultiline","value":"\u003c#\n Multiline comment\n ."}, + {"type":"LiteralStringDoc","value":"DESCRIPTION"}, + {"type":"CommentMultiline","value":"\n This is a description\n#\u003e"}, + {"type":"Text","value":"\n\n"}, + {"type":"Comment","value":"# this is a comment"}, + {"type":"Text","value":"\n\n"}, + {"type":"KeywordDeclaration","value":"function"}, + {"type":"TextWhitespace","value":" "}, + {"type":"NameBuiltin","value":"test"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"{"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"}"}, + {"type":"Text","value":"\n"}, + {"type":"KeywordDeclaration","value":"class"}, + {"type":"TextWhitespace","value":" "}, + {"type":"NameBuiltin","value":"test "}, + {"type":"Punctuation","value":"{}"}, + {"type":"Text","value":"\n"}, + {"type":"KeywordDeclaration","value":"class"}, + {"type":"TextWhitespace","value":" "}, + {"type":"NameBuiltin","value":"test2 :"}, + {"type":"Text","value":" "}, + {"type":"Name","value":"test"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"{"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"}"}, + {"type":"Text","value":"\n\n"}, + {"type":"LiteralStringHeredoc","value":"@\"\ndouble quoted "}, + {"type":"NameVariable","value":"$MyVar"}, + {"type":"LiteralStringHeredoc","value":" here string\n\"@"}, + {"type":"Text","value":"\n\n"}, + {"type":"LiteralStringHeredoc","value":"@\"\ndouble quoted "}, + {"type":"NameVariable","value":"${MyVar}"}, + {"type":"LiteralStringHeredoc","value":" here string\n\"@"}, + {"type":"Text","value":"\n\n"}, + {"type":"LiteralStringHeredoc","value":"@\"\ndouble quoted "}, + {"type":"Punctuation","value":"$("}, + {"type":"NameBuiltin","value":"Get-Stuff"}, + {"type":"Text","value":" "}, + {"type":"Name","value":"-Param1"}, + {"type":"Text","value":" "}, + {"type":"NameVariable","value":"$MyVar"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":")"}, + {"type":"LiteralStringHeredoc","value":" here string\n\"@"}, + {"type":"Text","value":"\n\n"}, + {"type":"LiteralStringHeredoc","value":"@'\nsingle quoted $(Get-Stuff -Param1 $MyVar ) here string\n'@"}, + {"type":"Text","value":"\n\n"}, + {"type":"LiteralStringSingle","value":"'Single quoted string $MyVar'"}, + {"type":"Text","value":"\n"}, + {"type":"LiteralStringDouble","value":"\"Double quoted string "}, + {"type":"NameVariable","value":"$MyVar"}, + {"type":"LiteralStringDouble","value":"\""}, + {"type":"Text","value":"\n"}, + {"type":"LiteralStringDouble","value":"\"Double quoted string "}, + {"type":"Punctuation","value":"$("}, + {"type":"NameBuiltin","value":"Get-Stuff"}, + {"type":"Text","value":" "}, + {"type":"Name","value":"-Param1"}, + {"type":"Text","value":" "}, + {"type":"NameVariable","value":"$MyVar"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":")"}, + {"type":"LiteralStringDouble","value":"\""}, + {"type":"Text","value":"\n"}, + {"type":"LiteralStringDouble","value":"\"Double quoted string "}, + {"type":"NameVariable","value":"${var}"}, + {"type":"LiteralStringDouble","value":"\""}, + {"type":"Text","value":"\n\n"}, + {"type":"NameVariable","value":"$HashTable"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"="}, + {"type":"Text","value":" "}, + {"type":"NameVariableMagic","value":"@"}, + {"type":"Punctuation","value":"{"}, + {"type":"Text","value":"\n "}, + {"type":"LiteralStringSingle","value":"'Something'"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"="}, + {"type":"Text","value":" "}, + {"type":"NameVariable","value":"$Var"}, + {"type":"Text","value":"\n "}, + {"type":"LiteralStringSingle","value":"'Else'"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"="}, + {"type":"Text","value":" "}, + {"type":"LiteralStringSingle","value":"'string'"}, + {"type":"Text","value":"\n"}, + {"type":"Punctuation","value":"}"}, + {"type":"Text","value":"\n\n"}, + {"type":"NameVariable","value":"$myvar"}, + {"type":"Punctuation","value":"."}, + {"type":"LiteralStringSingle","value":"'property name in string'"}, + {"type":"Text","value":"\n"}, + {"type":"Punctuation","value":"["}, + {"type":"NameConstant","value":"string"}, + {"type":"Punctuation","value":"]::"}, + {"type":"Name","value":"UseMethod"}, + {"type":"Punctuation","value":"("}, + {"type":"LiteralNumberFloat","value":"12.34"}, + {"type":"Punctuation","value":")"}, + {"type":"Text","value":"\n"}, + {"type":"LiteralNumberFloat","value":"15"}, + {"type":"Punctuation","value":"gb"}, + {"type":"Text","value":"\n"}, + {"type":"NameVariableMagic","value":"@"}, + {"type":"Punctuation","value":"{}"}, + {"type":"Text","value":"\n"}, + {"type":"NameVariableMagic","value":"@"}, + {"type":"Punctuation","value":"()"}, + {"type":"Text","value":"\n"}, + {"type":"NameVariableMagic","value":"$"}, + {"type":"Punctuation","value":"()"}, + {"type":"Text","value":"\n"}, + {"type":"NameVariable","value":"$test"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"="}, + {"type":"Text","value":" "}, + {"type":"Name","value":"C:"}, + {"type":"Punctuation","value":"\\"}, + {"type":"Name","value":"test"}, {"type":"Text","value":"\n"}, - {"type":"NameVariable","value":"$x"}, + {"type":"NameVariable","value":"$MyVar"}, {"type":"Text","value":" "}, {"type":"Punctuation","value":"="}, {"type":"Text","value":" "}, - {"type":"LiteralStringDouble","value":"\"Here is a "}, - {"type":"NameVariable","value":"$var"}, - {"type":"LiteralStringDouble","value":" inside a string\""}, + {"type":"NameBuiltin","value":"Get-Something"}, + {"type":"Text","value":" "}, + {"type":"Name","value":"-SwitchParam:"}, + {"type":"NameVariableMagic","value":"$false"}, + {"type":"Text","value":"\n"}, + {"type":"NameVariable","value":"$MyVar"}, + {"type":"Punctuation","value":"."}, + {"type":"NameProperty","value":"property"}, + {"type":"Text","value":"\n"}, + {"type":"NameVariable","value":"$MyVar"}, + {"type":"Punctuation","value":"."}, + {"type":"NameProperty","value":"property"}, + {"type":"Punctuation","value":"."}, + {"type":"NameProperty","value":"property"}, + {"type":"Punctuation","value":"."}, + {"type":"NameProperty","value":"somemethod"}, + {"type":"Punctuation","value":"()"}, + {"type":"Text","value":"\n"}, + {"type":"NameVariable","value":"$global:test"}, + {"type":"Text","value":"\n"}, + {"type":"NameVariableMagic","value":"$null"}, + {"type":"Text","value":"\n"}, + {"type":"NameVariableMagic","value":"$true"}, + {"type":"Text","value":"\n"}, + {"type":"NameVariableMagic","value":"$false"}, + {"type":"Text","value":"\n\n"}, + {"type":"KeywordDeclaration","value":"Configuration"}, + {"type":"TextWhitespace","value":" "}, + {"type":"NameBuiltin","value":"ConfigurationName\n"}, + {"type":"Punctuation","value":"{"}, + {"type":"Text","value":"\n "}, + {"type":"NameBuiltin","value":"Import-DSCResource"}, + {"type":"Text","value":" "}, + {"type":"Name","value":"-ModuleName"}, + {"type":"Text","value":" "}, + {"type":"Name","value":"MyModule"}, {"type":"Text","value":"\n"}, - {"type":"NameVariable","value":"$x"}, + {"type":"Punctuation","value":"}"}, + {"type":"Text","value":"\n\n"}, + {"type":"KeywordDeclaration","value":"function"}, + {"type":"TextWhitespace","value":" "}, + {"type":"NameBuiltin","value":"Get-Stuff"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"{"}, + {"type":"Text","value":"\n "}, + {"type":"Punctuation","value":"["}, + {"type":"NameBuiltin","value":"CmdletBinding"}, + {"type":"Punctuation","value":"("}, + {"type":"NameAttribute","value":"SupportsShouldProcess"}, + {"type":"Punctuation","value":"="}, + {"type":"NameVariableMagic","value":"$true"}, + {"type":"Punctuation","value":")]"}, + {"type":"Text","value":"\n "}, + {"type":"Keyword","value":"param"}, {"type":"Text","value":" "}, + {"type":"Punctuation","value":"("}, + {"type":"Text","value":"\n "}, + {"type":"Punctuation","value":"["}, + {"type":"NameBuiltin","value":"Parameter"}, + {"type":"Punctuation","value":"("}, + {"type":"NameAttribute","value":"Mandatory"}, {"type":"Punctuation","value":"="}, + {"type":"NameVariableMagic","value":"$true"}, + {"type":"Punctuation","value":","}, {"type":"Text","value":" "}, - {"type":"LiteralStringHeredoc","value":"@\"\nHere is a "}, - {"type":"NameVariable","value":"$var"}, - {"type":"LiteralStringHeredoc","value":" inside a here-string\n\"@"} + {"type":"NameAttribute","value":"DontShow"}, + {"type":"Punctuation","value":")]"}, + {"type":"Text","value":"\n "}, + {"type":"Punctuation","value":"["}, + {"type":"NameBuiltin","value":"ValidateNotNullOrEmpty"}, + {"type":"Punctuation","value":"()]"}, + {"type":"Text","value":"\n "}, + {"type":"Punctuation","value":"["}, + {"type":"NameBuiltin","value":"ValidateScript"}, + {"type":"Punctuation","value":"({"}, + {"type":"Text","value":" "}, + {"type":"NameVariable","value":"$_"}, + {"type":"Text","value":" "}, + {"type":"Operator","value":"-ne"}, + {"type":"Text","value":" "}, + {"type":"NameVariableMagic","value":"$null"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"})]"}, + {"type":"Text","value":"\n "}, + {"type":"Punctuation","value":"["}, + {"type":"NameBuiltin","value":"ValidateRange"}, + {"type":"Punctuation","value":"("}, + {"type":"LiteralNumberFloat","value":"0"}, + {"type":"Punctuation","value":","}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"["}, + {"type":"NameConstant","value":"int"}, + {"type":"Punctuation","value":"]::"}, + {"type":"Name","value":"MaxValue"}, + {"type":"Punctuation","value":")]"}, + {"type":"Text","value":"\n "}, + {"type":"Punctuation","value":"["}, + {"type":"NameConstant","value":"Microsoft.PowerShell.Commands.WebRequestMethod"}, + {"type":"Punctuation","value":"]"}, + {"type":"NameVariable","value":"$Param1"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"="}, + {"type":"Text","value":" "}, + {"type":"LiteralStringSingle","value":"'Get'"}, + {"type":"Text","value":"\n "}, + {"type":"Punctuation","value":")"}, + {"type":"Text","value":"\n\n "}, + {"type":"Keyword","value":"Process"}, + {"type":"Text","value":"\n "}, + {"type":"Punctuation","value":"{"}, + {"type":"Text","value":"\n "}, + {"type":"Keyword","value":"if"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"("}, + {"type":"NameVariable","value":"$MyVar"}, + {"type":"Text","value":" "}, + {"type":"Operator","value":"-lt"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"("}, + {"type":"NameBuiltin","value":"Get-Date"}, + {"type":"Punctuation","value":")."}, + {"type":"NameProperty","value":"AddSeconds"}, + {"type":"Punctuation","value":"(["}, + {"type":"NameConstant","value":"int"}, + {"type":"Punctuation","value":"]("}, + {"type":"LiteralNumberFloat","value":"12.4"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"/"}, + {"type":"Text","value":" "}, + {"type":"LiteralNumberFloat","value":"2"}, + {"type":"Punctuation","value":")))"}, + {"type":"Text","value":" "}, + {"type":"Punctuation","value":"{}"}, + {"type":"Text","value":"\n "}, + {"type":"Punctuation","value":"}"}, + {"type":"Text","value":"\n"}, + {"type":"Punctuation","value":"}"} ]