RomanYankovsky / DelphiAST

Abstract syntax tree builder for Delphi
Mozilla Public License 2.0
271 stars 116 forks source link

Synchronisation between `TOperators` and `TSyntaxNodeType` is not enforced. #258

Open JBontes opened 6 years ago

JBontes commented 6 years ago

If I add a new operator and mix up the definition for TOperators, DelphiAST will not complain.

A simple fix solves this issue, forcing the two structures to be in sync.

const
  OperatorsInfo: array [ntAddr..ntIs] of TOperatorInfo =  //1: use A TSyntaxNodeType as the index.
    ((Typ: ntAddr;         Priority: 1; Kind: okUnary;  AssocType: atRight),
     (Typ: ntDoubleAddr;   Priority: 1; Kind: okUnary;  AssocType: atRight),
     (Typ: ntDeref;        Priority: 1; Kind: okUnary;  AssocType: atLeft),
...

class function TOperators.GetItem(Typ: TSyntaxNodeType): TOperatorInfo;
begin
  Assert(Typ = OperatorsInfo[Typ].Typ);  //2: force the Typ's to match.
  if (Typ in [ntAddr..ntIs]) then Exit(OperatorsInfo[Typ])
  else Assert(false);  //do not allow buffer overruns.
end;