RomanYankovsky / DelphiAST

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

Old skool Objects do not generate a node and fields of an object are incorrectly classified as ntIndentifier #229

Open JBontes opened 6 years ago

JBontes commented 6 years ago

The following code:

type
  O = object(ParentObject)
  end;

Does not generate a node for the object keyword.

Here's the fix for the first part:

procedure TPasSyntaxTreeBuilder.ObjectType; {override}
begin
  FStack.Push(ntType).SetAttribute(anType, AttributeValues[atObject]);
  try
    inherited;
  finally
    MoveMembersToVisibilityNodes(FStack.Pop);
  end;
end;

And the fix for the second part

procedure TmwSimplePasPar.ObjectField;
begin
  if TokenID = ptSquareOpen then
    CustomAttribute;
  FieldNameList;
  Expected(ptColon);
  TypeKind;
  TypeDirective;
end;

procedure TPasSyntaxTreeBuilder.ObjectField;  {override}
var
  Fields, Temp: TSyntaxNode;
  Field, TypeInfo, TypeArgs: TSyntaxNode;
begin
  Fields := TSyntaxNode.Create(ntFields);
  try
    FStack.Push(Fields);
    try
      inherited;
    finally
      FStack.Pop;
    end;

    TypeInfo := Fields.FindNode(ntType);
    TypeArgs := Fields.FindNode(ntTypeArgs);
    for Field in Fields.ChildNodes do
    begin
      if Field.Typ <> ntName then
        Continue;

      Temp := FStack.Push(ntField);
      try
        Temp.AssignPositionFrom(Field);

        FStack.AddChild(Field.Clone);
        TypeInfo := TypeInfo.Clone;
        if Assigned(TypeArgs) then
          TypeInfo.AddChild(TypeArgs.Clone);
        FStack.AddChild(TypeInfo);
      finally
        FStack.Pop;
      end;
    end;
  finally
    Fields.Free;
  end;
end;