Pash-Project / Pash

An Open Source reimplementation of Windows PowerShell, for Mono.
https://groups.google.com/group/pash-project
BSD 3-Clause "New" or "Revised" License
514 stars 54 forks source link

finally clause quircks #411

Closed ForNeVeR closed 8 years ago

ForNeVeR commented 8 years ago

As reported at our maillist, we have some problems with finally clause.

  1. I got an exception with the following example:

    Pash> try { 2 + 2 } finally { 0 }
    System.InvalidOperationException: expected 'finally_clause' to be a 'catch_clauses'
    в Pash.ParserIntrinsics.AstBuilder.VerifyTerm(ParseTreeNode parseTreeNode, BnfTerm expectedTerm, BnfTerm[] moreExpectedTerms) в D:\X-Files\Projects\Pash\Source\System.Management\Pash\ParserIntrinsics\AstBuilder.cs:строка 40
    в Pash.ParserIntrinsics.AstBuilder.BuildCatchClausesAst(ParseTreeNode parseTreeNode) в D:\X-Files\Projects\Pash\Source\System.Management\Pash\ParserIntrinsics\AstBuilder.cs:строка 437
    в Pash.ParserIntrinsics.AstBuilder.BuildTryStatementAst(ParseTreeNode parseTreeNode) в D:\X-Files\Projects\Pash\Source\System.Management\Pash\ParserIntrinsics\AstBuilder.cs:строка 427
  2. That exception has killed the Pash process although generally parser exceptions shouldn't do that. (I am not sure how it does that; maybe that's a separate issue. Please provide more details while researching that.)
  3. We also have an inconsistency with PowerShell when returning the values from the finally clause. For example, the following will return a list with two values in PowerShell:

    PS> $x = try { 2 + 2 } catch { } finally { 0 }
    PS> $x
    4
    0

    But in Pash it returns only 4 and not 0. We should check the spec again and ultimately fix that inconsistency.

sburnicki commented 8 years ago

Finally terms aren't considered when building the parse tree in AstBuilder.cs:425. And this is also why Pash is crashing: Parsing technically works, but there is a real bug when building the parse tree as it assumes that all terms following the try are catch clauses which is obviously wrong.

Currently Pash can only recover from problems in parsing and execution. Building the parse tree is an intermediate step where Pash doesn't provide error recovery, yet.

I will resolve this bug and add support for finally clauses :)