Skip to content

Commit

Permalink
Improve includes (saving col/line and new "file" attribute added)
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanYankovsky committed Dec 11, 2015
1 parent fe57352 commit b15cd47
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 71 deletions.
2 changes: 2 additions & 0 deletions Demo/DelphiASTDemo.dpr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ uses
{$R *.res}

begin
System.ReportMemoryLeaksOnShutdown := True;

Application.Initialize;
Application.MainFormOnTaskbar := True;
Application.CreateForm(TMainForm, MainForm);
Expand Down
5 changes: 4 additions & 1 deletion Source/DelphiAST.Classes.pas
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ TSyntaxNode = class
private
FCol: Integer;
FLine: Integer;
FFileName: string;
function GetHasChildren: Boolean;
function GetHasAttributes: Boolean;
protected
Expand All @@ -48,6 +49,7 @@ TSyntaxNode = class

property Attributes: TDictionary<TAttributeName, string> read FAttributes;
property ChildNodes: TObjectList<TSyntaxNode> read FChildNodes;
property FileName: string read FFileName write FFileName;
property HasAttributes: Boolean read GetHasAttributes;
property HasChildren: Boolean read GetHasChildren;
property Typ: TSyntaxNodeType read FTyp;
Expand Down Expand Up @@ -320,7 +322,8 @@ class function TExpressionTools.CreateNodeWithParentsPosition(NodeType: TSyntaxN
begin
Result := TSyntaxNode.Create(NodeType);
Result.Line := ParentNode.Line;
Result.Col := ParentNode.Col;
Result.Col := ParentNode.Col;
Result.FileName := ParentNode.FileName;
end;

class procedure TExpressionTools.RawNodeListToTree(RawParentNode: TSyntaxNode; RawNodeList: TList<TSyntaxNode>;
Expand Down
3 changes: 3 additions & 0 deletions Source/DelphiAST.Writer.pas
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ class procedure TSyntaxTreeWriter.NodeToXML(const Builder: TStringBuilder;
Builder.Append(' col="' + IntToStr(Node.Col) + '"');
end;

if Node.FileName <> '' then
Builder.Append(' file="' + XMLEncode(Node.FileName) + '"');

if Node is TValuedSyntaxNode then
Builder.Append(' value="' + XMLEncode(TValuedSyntaxNode(Node).Value) + '"');

Expand Down
20 changes: 18 additions & 2 deletions Source/DelphiAST.pas
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,11 @@ procedure TPasSyntaxTreeBuilder.BuildExpressionTree(
NodeList: TList<TSyntaxNode>;
Node: TSyntaxNode;
Col, Line: Integer;
FileName: string;
begin
Line := Lexer.PosXY.Y;
Col := Lexer.PosXY.X;
FileName := Lexer.FileName;

RawExprNode := TSyntaxNode.Create(ntExpression);
try
Expand All @@ -472,7 +474,8 @@ procedure TPasSyntaxTreeBuilder.BuildExpressionTree(
ExprNode := FStack.Push(ntExpression, False);
try
ExprNode.Line := Line;
ExprNode.Col := Col;
ExprNode.Col := Col;
ExprNode.FileName := FileName;

NodeList := TList<TSyntaxNode>.Create;
try
Expand Down Expand Up @@ -1031,7 +1034,8 @@ procedure TPasSyntaxTreeBuilder.Expression;
procedure TPasSyntaxTreeBuilder.SetCurrentCompoundNodesEndPosition;
begin
TCompoundSyntaxNode(FStack.Peek).EndCol := Lexer.PosXY.X;
TCompoundSyntaxNode(FStack.Peek).EndLine := Lexer.PosXY.Y;
TCompoundSyntaxNode(FStack.Peek).EndLine := Lexer.PosXY.Y;
TCompoundSyntaxNode(FStack.Peek).FileName := Lexer.FileName;
end;

procedure TPasSyntaxTreeBuilder.CallInheritedExpression;
Expand Down Expand Up @@ -1501,6 +1505,7 @@ procedure TPasSyntaxTreeBuilder.DoOnComment(Sender: TObject; const Text: string)

Node.Col := Lexer.PosXY.X;
Node.Line := Lexer.PosXY.Y;
Node.FileName := Lexer.FileName;
Node.Text := Text;

FComments.Add(Node);
Expand Down Expand Up @@ -1838,8 +1843,10 @@ procedure TPasSyntaxTreeBuilder.SimpleStatement;
NodeList: TList<TSyntaxNode>;
I, AssignIdx: Integer;
Position: TTokenPoint;
FileName: string;
begin
Position := Lexer.PosXY;
FileName := Lexer.FileName;

RawStatement := TSyntaxNode.Create(ntStatement);
try
Expand All @@ -1859,6 +1866,7 @@ procedure TPasSyntaxTreeBuilder.SimpleStatement;
try
FStack.Peek.Col := Position.X;
FStack.Peek.Line := Position.Y;
FStack.Peek.FileName := FileName;

NodeList := TList<TSyntaxNode>.Create;
try
Expand All @@ -1879,6 +1887,7 @@ procedure TPasSyntaxTreeBuilder.SimpleStatement;
LHS := FStack.AddChild(ntLHS, False);
LHS.Col := NodeList[0].Col;
LHS.Line := NodeList[0].Line;
LHS.FileName := NodeList[0].FileName;
TExpressionTools.RawNodeListToTree(RawStatement, NodeList, LHS);

NodeList.Clear;
Expand All @@ -1892,6 +1901,7 @@ procedure TPasSyntaxTreeBuilder.SimpleStatement;
RHS := FStack.AddChild(ntRHS, False);
RHS.Col := NodeList[0].Col;
RHS.Line := NodeList[0].Line;
RHS.FileName := NodeList[0].FileName;
TExpressionTools.RawNodeListToTree(RawStatement, NodeList, RHS);
finally
NodeList.Free;
Expand Down Expand Up @@ -2216,8 +2226,10 @@ procedure TPasSyntaxTreeBuilder.UsedUnitName;
var
NamesNode, UnitNode: TSyntaxNode;
Position: TTokenPoint;
FileName: string;
begin
Position := Lexer.PosXY;
FileName := Lexer.FileName;

NamesNode := TSyntaxNode.Create(ntUnit);
try
Expand All @@ -2232,6 +2244,7 @@ procedure TPasSyntaxTreeBuilder.UsedUnitName;
UnitNode.SetAttribute(anName, NodeListToString(NamesNode));
UnitNode.Col := Position.X;
UnitNode.Line := Position.Y;
UnitNode.FileName := FileName;
finally
NamesNode.Free;
end;
Expand Down Expand Up @@ -2428,6 +2441,7 @@ function TNodeStack.AddChild(Typ: TSyntaxNodeType;
begin
Result.Col := FParser.Lexer.PosXY.X;
Result.Line := FParser.Lexer.PosXY.Y;
Result.FileName := FParser.Lexer.FileName;
end;
end;

Expand All @@ -2442,6 +2456,7 @@ function TNodeStack.AddValuedChild(Typ: TSyntaxNodeType;
Result := FStack.Peek.AddChild(TValuedSyntaxNode.Create(Typ));
Result.Col := FParser.Lexer.PosXY.X;
Result.Line := FParser.Lexer.PosXY.Y;
Result.FileName := FParser.Lexer.FileName;

TValuedSyntaxNode(Result).Value := Value;
end;
Expand Down Expand Up @@ -2487,6 +2502,7 @@ function TNodeStack.Push(Node: TSyntaxNode; SetPositionAttributes: Boolean): TSy
begin
Result.Col := FParser.Lexer.PosXY.X;
Result.Line := FParser.Lexer.PosXY.Y;
Result.FileName := FParser.Lexer.FileName;
end;
end;

Expand Down
Loading

0 comments on commit b15cd47

Please sign in to comment.