DelphiAST icon indicating copy to clipboard operation
DelphiAST copied to clipboard

[Position information] Literal

Open uschuster opened this issue 10 years ago • 1 comments

I do suppose the position information of nodes (Col and Line) do specify the begin. The col for nodes of type ntLiteral specify the end.

Sample

unit TestUnit;

interface

implementation

procedure Test;
var
  S: string;
begin
  S := 'FooBar';
end;

end.

Expected: node of type ntLiteral with value "FooBar" does have col = 8 Actual: node of type ntLiteral with value "FooBar" does have col = 16

uschuster avatar Jan 13 '16 20:01 uschuster

The following code will fix this, but pretty it isn't. The problem is that inherited moves advances the column. The solution is to create the node prior to calling inherited.

procedure TPasSyntaxTreeBuilder.StringConst;
var
  StrConst: TSyntaxNode;
  Literal, Node: TSyntaxNode;
  Str: string;
begin
  Node := FStack.AddValuedChild(ntLiteral, Str);  //Create the node first. Col = correct.
  StrConst := TSyntaxNode.Create(ntUnknown);
  try
    FStack.Push(StrConst);
    try
      inherited;
    finally
      FStack.Pop;
    end;

    Str := '';
    for Literal in StrConst.ChildNodes do
      Str := Str + TValuedSyntaxNode(Literal).Value;
  finally
    StrConst.Free;
  end;
  TValuedSyntaxNode(Node).Value:= Str;  //Fixup the string value inside.

  Node.SetAttribute(anType, 'string');
end;

JBontes avatar May 13 '16 11:05 JBontes