问题描述
这是我的子表达式:
"""$((($l -split " """)[1] -split """ ")[0])"""
我检查过,没有发现不成对的括号.然而,powershell 坚持说在表达中缺少结束')'."有趣的是,表达式
I have checked and found no unpaired parentheses. However powershell insists in saying "Missing closing ')' in expression." Interestingly, the expression
$((($l -split " """)[1] -split """ ")[0])
工作正常.
有没有人有过类似的经历?是Powershell的bug吗?
Has anyone had similiar experience before? Is it a bug of Powershell?
推荐答案
这.. 真的很有趣,是的,至少到目前为止我认为它是一个错误.
This.. is really interesting, and yes I would consider it a bug at least so far.
这是一个更简单的重现:
Here's a much simpler repro:
"$("`"")"
"$("""")"
这似乎是由于在子表达式内、可扩展字符串内使用任一形式的双引号转义(反引号或双双引号)引起的.
It seems to be caused by using either form of double quote escaping (backtick or double double quote), inside a sub-expression, inside an expandable string.
这似乎也归结为解析器本身的错误:
It also appears that this is error comes right down to the parser itself:
$sb = @'
"$("`"")"
'@
$tokens = $null
$er = $null
$ast = [System.Management.Automation.Language.Parser]::ParseInput($sb, [ref]$tokens, [ref]$er)
$ast.EndBlock.Statements[0].PipelineElements[0].Expression.NestedExpressions
$tokens[0].NestedTokens
它找到的嵌套标记/表达式不正确.
The nested tokens/expressions it finds just aren't correct.
我在 WLS 上使用 Windows PowerShell 5.1 和 PowerShell Core 6.0.0-rc.2 进行了测试.
I tested with Windows PowerShell 5.1 and PowerShell Core 6.0.0-rc.2 on WLS.
这篇关于即使子表达式是平衡的,Powershell 也会报告 MissingEndParenthesisInExpression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!