DTW-DanWard / PowerShell-Beautifier

A whitespace reformatter and code cleaner for Windows PowerShell and PowerShell Core
MIT License
334 stars 38 forks source link

Can't parse valid RegEx PoSh Function #63

Open falloutphil opened 3 years ago

falloutphil commented 3 years ago

Hi,

The following function returns the error below. Any ideas?

The function works perfectly well in PoSh 5.1 - I got it from an MS blog so it should be "good" PoSh: https://devblogs.microsoft.com/scripting/use-powershell-to-work-with-any-ini-file/

Thanks, Phil.

function Get-IniContent ($filePath)
{
    $ini = @{}
    switch -regex -file $FilePath
    {
        “^\[(.+)\]” # Section
        {
            $section = $matches[1]
            $ini[$section] = @{}
            $CommentCount = 0
        }
        “^(;.*)$” # Comment
        {
            $value = $matches[1]
            $CommentCount = $CommentCount + 1
            $name = “Comment” + $CommentCount
            $ini[$section][$name] = $value
        }
        “(.+?)\s*=(.*)” # Key
        {
            $name,$value = $matches[1..2]
            $ini[$section][$name] = $value
        }
    }
    return $ini
}
Invoke-TokenizeSourceScriptContent : An error occurred; is there invalid PowerShell or some formatting / syntax issue in the script? See error record below.
At \\NAS01\Users$\foo.bar\Documents\WindowsPowerShell\Modules\PowerShell-Beautifier\1.2.5\src\DTW.PS.Beautifier.Main.psm1:1329 char:5
+     Invoke-TokenizeSourceScriptContent -EV Err
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-TokenizeSourceScriptContent

Invoke-TokenizeSourceScriptContent : Missing statement block in switch statement clause. Content: , line: 57, column: 13
At \\NAS01\Users$\foo.bar\Documents\WindowsPowerShell\Modules\PowerShell-Beautifier\1.2.5\src\DTW.PS.Beautifier.Main.psm1:1329 char:5
+     Invoke-TokenizeSourceScriptContent -EV Err
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-TokenizeSourceScriptContent

Invoke-TokenizeSourceScriptContent : Missing statement block in switch statement clause. Content: , line: 57, column: 17
At \\NAS01\Users$\foo.bar\Documents\WindowsPowerShell\Modules\PowerShell-Beautifier\1.2.5\src\DTW.PS.Beautifier.Main.psm1:1329 char:5
+     Invoke-TokenizeSourceScriptContent -EV Err
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-TokenizeSourceScriptContent

Invoke-TokenizeSourceScriptContent : Missing statement block in switch statement clause. Content: , line: 63, column: 11
At \\NAS01\Users$\foo.bar\Documents\WindowsPowerShell\Modules\PowerShell-Beautifier\1.2.5\src\DTW.PS.Beautifier.Main.psm1:1329 char:5
+     Invoke-TokenizeSourceScriptContent -EV Err
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-TokenizeSourceScriptContent

Invoke-TokenizeSourceScriptContent : An expression was expected after '('. Content: , line: 63, column: 12
At \\NAS01\Users$\foo.bar\Documents\WindowsPowerShell\Modules\PowerShell-Beautifier\1.2.5\src\DTW.PS.Beautifier.Main.psm1:1329 char:5
+     Invoke-TokenizeSourceScriptContent -EV Err
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-TokenizeSourceScriptContent

Invoke-TokenizeSourceScriptContent : Missing closing ')' in expression. Content: , line: 63, column: 12
At \\NAS01\Users$\foo.bar\Documents\WindowsPowerShell\Modules\PowerShell-Beautifier\1.2.5\src\DTW.PS.Beautifier.Main.psm1:1329 char:5
+     Invoke-TokenizeSourceScriptContent -EV Err
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-TokenizeSourceScriptContent

Invoke-TokenizeSourceScriptContent : Missing statement block in switch statement clause. Content: , line: 63, column: 12
At \\NAS01\Users$\foo.bar\Documents\WindowsPowerShell\Modules\PowerShell-Beautifier\1.2.5\src\DTW.PS.Beautifier.Main.psm1:1329 char:5
+     Invoke-TokenizeSourceScriptContent -EV Err
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-TokenizeSourceScriptContent

Invoke-TokenizeSourceScriptContent : Missing statement block in switch statement clause. Content: , line: 63, column: 15
At \\NAS01\Users$\foo.bar\Documents\WindowsPowerShell\Modules\PowerShell-Beautifier\1.2.5\src\DTW.PS.Beautifier.Main.psm1:1329 char:5
+     Invoke-TokenizeSourceScriptContent -EV Err
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-TokenizeSourceScriptContent

Invoke-TokenizeSourceScriptContent : Missing condition in switch statement clause. Content: , line: 63, column: 15
At \\NAS01\Users$\foo.bar\Documents\WindowsPowerShell\Modules\PowerShell-Beautifier\1.2.5\src\DTW.PS.Beautifier.Main.psm1:1329 char:5
+     Invoke-TokenizeSourceScriptContent -EV Err
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-TokenizeSourceScriptContent

Invoke-TokenizeSourceScriptContent : Missing closing '}' in statement block or type definition. Content: {, line: 53, column: 1
At \\NAS01\Users$\foo.bar\Documents\WindowsPowerShell\Modules\PowerShell-Beautifier\1.2.5\src\DTW.PS.Beautifier.Main.psm1:1329 char:5
+     Invoke-TokenizeSourceScriptContent -EV Err
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-TokenizeSourceScriptContent

Invoke-TokenizeSourceScriptContent : Unexpected token ')' in expression or statement. Content: ), line: 63, column: 15
At \\NAS01\Users$\foo.bar\Documents\WindowsPowerShell\Modules\PowerShell-Beautifier\1.2.5\src\DTW.PS.Beautifier.Main.psm1:1329 char:5
+     Invoke-TokenizeSourceScriptContent -EV Err
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-TokenizeSourceScriptContent

Invoke-TokenizeSourceScriptContent : Unexpected token '}' in expression or statement. Content: }, line: 75, column: 5
At \\NAS01\Users$\foo.bar\Documents\WindowsPowerShell\Modules\PowerShell-Beautifier\1.2.5\src\DTW.PS.Beautifier.Main.psm1:1329 char:5
+     Invoke-TokenizeSourceScriptContent -EV Err
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-TokenizeSourceScriptContent

Invoke-TokenizeSourceScriptContent : Unexpected token '}' in expression or statement. Content: }, line: 77, column: 1
At \\NAS01\Users$\foo.bar\Documents\WindowsPowerShell\Modules\PowerShell-Beautifier\1.2.5\src\DTW.PS.Beautifier.Main.psm1:1329 char:5
+     Invoke-TokenizeSourceScriptContent -EV Err
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-TokenizeSourceScriptContent
DTW-DanWard commented 3 years ago

When I cut and paste the code directly from that web page (saved with VS Code) it works on my machine (tested in Win PowerShell 5.1). If you want to try the tokenize API all by itself (to confirm the error is/isn't happening), there's an example buried in the docs: https://github.com/DTW-DanWard/PowerShell-Beautifier/blob/master/docs/TokenizeExample.md

I'm guessing you have some type funky hidden character in that page/your copy of the source that it's choking on. If you use VS Code, there's a great extension you should install: Highlight Bad Chars. It does exactly what it sounds like - highlights in red any funky characters, usually Unicode characters like non-breaking white space, etc. https://marketplace.visualstudio.com/items?itemName=wengerk.highlight-bad-chars