RomanYankovsky / DelphiAST

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

Record constants are not recorded correctly + fields in record constants are recorded differently from other ntField nodes. #250

Open JBontes opened 6 years ago

JBontes commented 6 years ago

Consider the following example:

unit Test;
interface
const
  OperatorsInfo: array [0..2] of TOperatorInfo =
    ((Typ: ntAddr;         AssocType: atRight),
     (Typ: ntDeref;        AssocType: atLeft),
     (Typ: ntGeneric;      AssocType: atRight));

implementation
end.

The record constants are not recorded. In addition the ntField nodes are constructed different from ntField nodes elsewhere. All ntField should be constructed the same, so that we don't need different code to decode them depending on the context.

The fix:

procedure TPasSyntaxTreeBuilder.RecordConstant;
begin
  FStack.Push(ntRecordConstant);
  try
    inherited;
  finally
    FStack.Pop;
  end;
end;

procedure TPasSyntaxTreeBuilder.RecordFieldConstant;
var
  Node: TSyntaxNode;
begin
  //A field in a record constant should have exactly the same layout
  //as a field in a class.
  //ntField (class)
  //+-- ntName (anName = name)
  //+-- ntType
  //Recordconstant
  //ntField (recordconstant)
  //+-- ntName
  //+-- ntExpression.
  FStack.Push(ntField).AddChild(ntName).Attribute[anName]:= Lexer.Token;
  try
    inherited;
  finally
    FStack.Pop;
  end;
end;