RomanYankovsky / DelphiAST

Abstract syntax tree builder for Delphi
Mozilla Public License 2.0
273 stars 117 forks source link

Except else should have its own nodetype. #223

Open JBontes opened 7 years ago

JBontes commented 7 years ago

The except-else accepts a StatementList. The if-then-else does not. In order to tell the two apart the except-else should have a different nodetype, just like the case-else.

Here's the fix:

procedure TPasSyntaxTreeBuilder.ExceptionBlockElseBranch;
begin
  FStack.Push(ntExceptElse);
  try
    inherited;
  finally
    FStack.Pop;
  end;
end;

This allows a DelphiAST user to see the difference between a normal else statement and an except-else. The first needs a begin-end pair to accept more than one statement, the second does not.
This will also make the except-else consistent with the case-else.

We can now easily check the parent of a ntStatements node. If it is in the list [ntExcept, ntRepeat, ..., ntCaseElse, ntExceptElse] then we know there was no begin-end pair in the original source code.
Currently the except-else is the only case where we cannot tell the difference between a StatementList node and Statements node.

In addition consider the following code:

try
  Something;
except on E:EAccessViolation do
  if a>10 then Other else Same;
  MoreOfTheSame;
end;

vs

try
  Something
except on E:EAccessViolation do
  if a > 10 then Other;
else 
  Same;
  MoreOfTheSame;
end;

Clearly the two else statements perform very different actions and should not be confused.