Skip to content

Commit

Permalink
RomanYankovsky#258 type safe TOperators. RomanYankovsky#217 fix cor…
Browse files Browse the repository at this point in the history
…ruption of childnode list.
  • Loading branch information
jbontes committed Oct 19, 2017
1 parent 644161a commit 1e3101d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 23 deletions.
11 changes: 6 additions & 5 deletions Source/DelphiAST.Classes.pas
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ EParserException = class(Exception)
property Line: Integer read FLine;
property Col: Integer read FCol;
end;

TAttributeEntry = TPair<TAttributeName, string>;
PAttributeEntry = ^TAttributeEntry;

Expand Down Expand Up @@ -136,7 +136,7 @@ TOperators = class
end;

const
OperatorsInfo: array [0..28] of TOperatorInfo =
OperatorsInfo: array [ntAddr..ntIs] of TOperatorInfo =
((Typ: ntAddr; Priority: 1; Kind: okUnary; AssocType: atRight),
(Typ: ntDoubleAddr; Priority: 1; Kind: okUnary; AssocType: atRight),
(Typ: ntDeref; Priority: 1; Kind: okUnary; AssocType: atLeft),
Expand Down Expand Up @@ -171,7 +171,9 @@ TOperators = class

class function TOperators.GetItem(Typ: TSyntaxNodeType): TOperatorInfo;
begin
if (Typ in [ntAddr..ntIs]) then Exit(OperatorsInfo[Ord(Typ) - Ord(ntAddr)]); //#224
Assert(Typ = OperatorsInfo[Typ].Typ);
if (Typ in [ntAddr..ntIs]) then Exit(OperatorsInfo[Typ])
else Assert(false);
end;

class function TOperators.IsOpName(Typ: TSyntaxNodeType): Boolean;
Expand Down Expand Up @@ -397,11 +399,9 @@ function TSyntaxNode.TryGetAttributeEntry(const Key: TAttributeName; out Attribu
end;
end;


function TSyntaxNode.AddChild(Node: TSyntaxNode): TSyntaxNode;
begin
Assert(Assigned(Node));

SetLength(FChildNodes, Length(FChildNodes) + 1);
FChildNodes[Length(FChildNodes) - 1] := Node;

Expand Down Expand Up @@ -593,4 +593,5 @@ constructor EParserException.Create(Line, Col: Integer; const FileName, Msg: str
FCol := Col;
end;


end.
16 changes: 7 additions & 9 deletions Source/DelphiAST.Consts.pas
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ interface
ntShl,
ntShr,
ntAs,
ntAsmFragment,
ntAsmStatement,
ntAdd,
ntSub,
ntOr,
Expand Down Expand Up @@ -50,6 +48,8 @@ interface
ntAnonymousMethod,
ntAnonymousMethodType,
ntArguments,
ntAsmFragment,
ntAsmStatement,
ntAssign,
ntAt,
ntAttribute,
Expand Down Expand Up @@ -183,17 +183,15 @@ interface
TAttributeNames = set of TAttributeName;

type
TSyntaxNodeNames = record
SyntaxNodeNames = class
strict private
class var FData: array[TSyntaxNodeType] of string;
function GetItem(const index: TSyntaxNodeType): string; inline;
class function GetItem(const index: TSyntaxNodeType): string; static; inline;
class constructor Init;
public
property Items[const index: TSyntaxNodeType]: string read GetItem; default;
class property Items[const index: TSyntaxNodeType]: string read GetItem; default;
end;

var
SyntaxNodeNames: TSyntaxNodeNames; //for some reason default does not work on class properties.


//const
Expand Down Expand Up @@ -361,12 +359,12 @@ implementation

{ TSyntaxNodeNames }

function TSyntaxNodeNames.GetItem(const index: TSyntaxNodeType): string;
class function SyntaxNodeNames.GetItem(const index: TSyntaxNodeType): string;
begin
Result:= FData[index];
end;

class constructor TSyntaxNodeNames.Init;
class constructor SyntaxNodeNames.Init;
var
value: TSyntaxNodeType;
begin
Expand Down
75 changes: 66 additions & 9 deletions Source/DelphiAST.pas
Original file line number Diff line number Diff line change
Expand Up @@ -773,11 +773,68 @@ procedure TPasSyntaxTreeBuilder.BuildExpressionTree(
end;
end;

//ExtractChild causes corruption of the childlist. need to investigate why.
//procedure TPasSyntaxTreeBuilder.BuildParametersList(
// ParametersListMethod: TTreeBuilderMethod);
//var
// Params, Temp: TSyntaxNode;
// Attributes: TSyntaxNode;
// ParamList, Param, TypeInfo, ParamExpr: TSyntaxNode;
// ParamKind: string;
//begin
// Params := TSyntaxNode.Create(ntUnknown);
// try
// FStack.Push(ntParameters);
//
// FStack.Push(Params);
// try
// ParametersListMethod;
// finally
// FStack.Pop;
// end;
//
// for ParamList in Params.ChildNodes do
// begin
// TypeInfo := ParamList.ExtractChild(ntType);
// ParamKind := ParamList.Attribute[anKind];
// ParamExpr := ParamList.ExtractChild(ntExpression);
// Attributes:= ParamList.ExtractChild(ntAttributes);
//
// for Param in ParamList.ChildNodes do
// begin
// if Param.Typ <> ntName then
// Continue;
//
// Temp := FStack.Push(ntParameter);
// if ParamKind <> '' then
// Temp.Attribute[anKind] := ParamKind;
//
// Temp.Col := Param.Col;
// Temp.Line := Param.Line;
//
// if Assigned(Attributes) then
// FStack.AddChild(Attributes);
//
// FStack.AddChild(Param.Clone);
// if Assigned(TypeInfo) then
// FStack.AddChild(TypeInfo);
//
// if Assigned(ParamExpr) then
// FStack.AddChild(ParamExpr);
//
// FStack.Pop;
// end;
// end;
// FStack.Pop;
// finally
// Params.Free;
// end;
//end;

procedure TPasSyntaxTreeBuilder.BuildParametersList(
ParametersListMethod: TTreeBuilderMethod);
var
Params, Temp: TSyntaxNode;
Attributes: TSyntaxNode;
Params, Temp, Attributes: TSyntaxNode;
ParamList, Param, TypeInfo, ParamExpr: TSyntaxNode;
ParamKind: string;
begin
Expand All @@ -794,10 +851,10 @@ procedure TPasSyntaxTreeBuilder.BuildParametersList(

for ParamList in Params.ChildNodes do
begin
TypeInfo := ParamList.ExtractChild(ntType);
TypeInfo := ParamList.FindNode(ntType);
ParamKind := ParamList.Attribute[anKind];
ParamExpr := ParamList.ExtractChild(ntExpression);
Attributes:= ParamList.ExtractChild(ntAttributes);
ParamExpr := ParamList.FindNode(ntExpression);
Attributes:= ParamList.FindNode(ntAttributes);

for Param in ParamList.ChildNodes do
begin
Expand All @@ -806,20 +863,20 @@ procedure TPasSyntaxTreeBuilder.BuildParametersList(

Temp := FStack.Push(ntParameter);
if ParamKind <> '' then
Temp.Attribute[anKind] := ParamKind;
Temp.Attribute[anKind]:= ParamKind;

Temp.Col := Param.Col;
Temp.Line := Param.Line;

if Assigned(Attributes) then
FStack.AddChild(Attributes);
FStack.AddChild(Attributes.Clone);

FStack.AddChild(Param.Clone);
if Assigned(TypeInfo) then
FStack.AddChild(TypeInfo);
FStack.AddChild(TypeInfo.Clone);

if Assigned(ParamExpr) then
FStack.AddChild(ParamExpr);
FStack.AddChild(ParamExpr.Clone);

FStack.Pop;
end;
Expand Down

0 comments on commit 1e3101d

Please sign in to comment.