-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathDelphiAST.Classes.pas
More file actions
618 lines (529 loc) · 17.6 KB
/
DelphiAST.Classes.pas
File metadata and controls
618 lines (529 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
unit DelphiAST.Classes;
{$IFDEF FPC}{$MODE DELPHI}{$ENDIF}
interface
uses
SysUtils, Generics.Collections, SimpleParser.Lexer.Types, DelphiAST.Consts;
type
EParserException = class(Exception)
strict private
FFileName: string;
FLine, FCol: Integer;
public
constructor Create(Line, Col: Integer; const FileName, Msg: string); reintroduce;
property FileName: string read FFileName;
property Line: Integer read FLine;
property Col: Integer read FCol;
end;
TAttributeEntry = TPair<TAttributeName, string>;
PAttributeEntry = ^TAttributeEntry;
TSyntaxNodeClass = class of TSyntaxNode;
TSyntaxNode = class
public
function HasAttribute(const Key: TAttributeName): Boolean; inline;
private
FCol: Integer;
FLine: Integer;
FFileName: string;
function GetHasChildren: Boolean; inline;
function TryGetAttributeEntry(const Key: TAttributeName; out AttributeEntry: PAttributeEntry): boolean;
function GetChildCount: cardinal; inline;
function GetChildNode(index: cardinal): TSyntaxNode; inline;
procedure RemoveAttribute(const Key: TAttributeName);
protected
FAttributes: TArray<TAttributeEntry>;
FChildNodes: TArray<TSyntaxNode>;
FTyp: TSyntaxNodeType;
FParentNode: TSyntaxNode;
FAttributesInUse: TAttributeNames;
public
constructor Create(Typ: TSyntaxNodeType);
destructor Destroy; override;
function Clone: TSyntaxNode; virtual;
procedure AssignPositionFrom(const Node: TSyntaxNode);
function GetAttribute(const Key: TAttributeName): string;
procedure SetAttribute(const Key: TAttributeName; const Value: string);
procedure ClearAttributes;
function AddChild(Typ: TSyntaxNodeType): TSyntaxNode; overload; inline;
procedure AddChildren(Nodes: TArray<TSyntaxNode>);
function AddChild(Node: TSyntaxNode): TSyntaxNode; overload;
procedure DeleteChild(Node: TSyntaxNode); inline;
function ExtractChild(Node: TSyntaxNode): TSyntaxNode; overload;
function ExtractChild(Typ: TSyntaxNodeType): TSyntaxNode; overload;
function FindNode(Typ: TSyntaxNodeType): TSyntaxNode; overload;
function FindNode(const Types: TSyntaxNodeTypes): TSyntaxNode; overload;
property Attributes: TArray<TAttributeEntry> read FAttributes;
property ChildNodes: TArray<TSyntaxNode> read FChildNodes;
property HasChildren: Boolean read GetHasChildren;
property Typ: TSyntaxNodeType read FTyp;
property ParentNode: TSyntaxNode read FParentNode;
property Col: Integer read FCol write FCol;
property Line: Integer read FLine write FLine;
property FileName: string read FFileName write FFileName;
property ChildNode[index: cardinal]:TSyntaxNode read GetChildNode;
property ChildCount: cardinal read GetChildCount;
property Attribute[const Key: TAttributeName]: string read GetAttribute write SetAttribute;
end;
TCompoundSyntaxNode = class(TSyntaxNode)
private
FEndCol: Integer;
FEndLine: Integer;
public
function Clone: TSyntaxNode; override;
property EndCol: Integer read FEndCol write FEndCol;
property EndLine: Integer read FEndLine write FEndLine;
end;
TValuedSyntaxNode = class(TSyntaxNode)
private
FValue: string;
public
function Clone: TSyntaxNode; override;
property Value: string read FValue write FValue;
end;
TCommentNode = class(TSyntaxNode)
private
FText: string;
public
function Clone: TSyntaxNode; override;
property Text: string read FText write FText;
end;
TExpressionTools = class
private
class function CreateNodeWithParentsPosition(NodeType: TSyntaxNodeType; ParentNode: TSyntaxNode): TSyntaxNode;
public
class function ExprToReverseNotation(Expr: TList<TSyntaxNode>): TList<TSyntaxNode>; static;
class procedure NodeListToTree(Expr: TList<TSyntaxNode>; Root: TSyntaxNode); static;
class function PrepareExpr(ExprNodes: TList<TSyntaxNode>): TList<TSyntaxNode>; static;
class procedure RawNodeListToTree(RawParentNode: TSyntaxNode; RawNodeList: TList<TSyntaxNode>; NewRoot: TSyntaxNode); static;
end;
implementation
type
TOperatorKind = (okUnary, okBinary);
TOperatorAssocType = (atLeft, atRight);
TOperatorInfo = record
Typ: TSyntaxNodeType;
Priority: Byte;
Kind: TOperatorKind;
AssocType: TOperatorAssocType;
end;
TOperators = class
strict private
class function GetItem(Typ: TSyntaxNodeType): TOperatorInfo; inline; static;
public
class function IsOpName(Typ: TSyntaxNodeType): Boolean;
class property Items[Typ: TSyntaxNodeType]: TOperatorInfo read GetItem; default;
end;
const
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),
(Typ: ntGeneric; Priority: 1; Kind: okBinary; AssocType: atRight),
(Typ: ntIndexed; Priority: 1; Kind: okUnary; AssocType: atLeft),
(Typ: ntDot; Priority: 2; Kind: okBinary; AssocType: atRight),
(Typ: ntCall; Priority: 3; Kind: okBinary; AssocType: atRight),
(Typ: ntUnaryMinus; Priority: 5; Kind: okUnary; AssocType: atRight),
(Typ: ntNot; Priority: 6; Kind: okUnary; AssocType: atRight),
(Typ: ntMul; Priority: 7; Kind: okBinary; AssocType: atRight),
(Typ: ntFDiv; Priority: 7; Kind: okBinary; AssocType: atRight),
(Typ: ntDiv; Priority: 7; Kind: okBinary; AssocType: atRight),
(Typ: ntMod; Priority: 7; Kind: okBinary; AssocType: atRight),
(Typ: ntAnd; Priority: 7; Kind: okBinary; AssocType: atRight),
(Typ: ntShl; Priority: 7; Kind: okBinary; AssocType: atRight),
(Typ: ntShr; Priority: 7; Kind: okBinary; AssocType: atRight),
(Typ: ntAs; Priority: 7; Kind: okBinary; AssocType: atRight),
(Typ: ntAdd; Priority: 8; Kind: okBinary; AssocType: atRight),
(Typ: ntSub; Priority: 8; Kind: okBinary; AssocType: atRight),
(Typ: ntOr; Priority: 8; Kind: okBinary; AssocType: atRight),
(Typ: ntXor; Priority: 8; Kind: okBinary; AssocType: atRight),
(Typ: ntEqual; Priority: 9; Kind: okBinary; AssocType: atRight),
(Typ: ntNotEqual; Priority: 9; Kind: okBinary; AssocType: atRight),
(Typ: ntLower; Priority: 9; Kind: okBinary; AssocType: atRight),
(Typ: ntGreater; Priority: 9; Kind: okBinary; AssocType: atRight),
(Typ: ntLowerEqual; Priority: 9; Kind: okBinary; AssocType: atRight),
(Typ: ntGreaterEqual; Priority: 9; Kind: okBinary; AssocType: atRight),
(Typ: ntIn; Priority: 9; Kind: okBinary; AssocType: atRight),
(Typ: ntIs; Priority: 9; Kind: okBinary; AssocType: atRight));
{ TOperators }
class function TOperators.GetItem(Typ: TSyntaxNodeType): TOperatorInfo;
begin
Assert(Typ = OperatorsInfo[Typ].Typ);
Assert(Typ in [ntAddr..ntIs]);
Result:= OperatorsInfo[Typ]; //don't use exit in inline routines.
end;
class function TOperators.IsOpName(Typ: TSyntaxNodeType): Boolean;
begin
Result:= (Typ in [ntAddr..ntIs]);
end;
function IsRoundClose(Typ: TSyntaxNodeType): Boolean; inline;
begin
Result := Typ = ntRoundClose;
end;
function IsRoundOpen(Typ: TSyntaxNodeType): Boolean; inline;
begin
Result := Typ = ntRoundOpen;
end;
class function TExpressionTools.ExprToReverseNotation(Expr: TList<TSyntaxNode>): TList<TSyntaxNode>;
var
Stack: TStack<TSyntaxNode>;
Node: TSyntaxNode;
begin
Result := TList<TSyntaxNode>.Create;
try
Stack := TStack<TSyntaxNode>.Create;
try
for Node in Expr do
if TOperators.IsOpName(Node.Typ) then
begin
while (Stack.Count > 0) and TOperators.IsOpName(Stack.Peek.Typ) and
(((TOperators.Items[Node.Typ].AssocType = atLeft) and
(TOperators.Items[Node.Typ].Priority >= TOperators.Items[Stack.Peek.Typ].Priority))
or
((TOperators.Items[Node.Typ].AssocType = atRight) and
(TOperators.Items[Node.Typ].Priority > TOperators.Items[Stack.Peek.Typ].Priority)))
do
Result.Add(Stack.Pop);
Stack.Push(Node);
end
else if IsRoundOpen(Node.Typ) then
Stack.Push(Node)
else if IsRoundClose(Node.Typ) then
begin
while not IsRoundOpen(Stack.Peek.Typ) do
Result.Add(Stack.Pop);
// RoundOpen and RoundClose nodes are not needed anymore
Stack.Pop.Free;
Node.Free;
if (Stack.Count > 0) and TOperators.IsOpName(Stack.Peek.Typ) then
Result.Add(Stack.Pop);
end else
Result.Add(Node);
while Stack.Count > 0 do
Result.Add(Stack.Pop);
finally
Stack.Free;
end;
except
FreeAndNil(Result);
raise;
end;
end;
class procedure TExpressionTools.NodeListToTree(Expr: TList<TSyntaxNode>; Root: TSyntaxNode);
var
Stack: TStack<TSyntaxNode>;
Node, SecondNode: TSyntaxNode;
begin
Stack := TStack<TSyntaxNode>.Create;
try
for Node in Expr do
begin
if TOperators.IsOpName(Node.Typ) then
case TOperators.Items[Node.Typ].Kind of
okUnary: Node.AddChild(Stack.Pop);
okBinary:
begin
SecondNode := Stack.Pop;
Node.AddChild(Stack.Pop);
Node.AddChild(SecondNode);
end;
end;
Stack.Push(Node);
end;
Root.AddChild(Stack.Pop);
Assert(Stack.Count = 0);
finally
Stack.Free;
end;
end;
class function TExpressionTools.PrepareExpr(ExprNodes: TList<TSyntaxNode>): TList<TSyntaxNode>;
var
Node, PrevNode: TSyntaxNode;
begin
Result := TList<TSyntaxNode>.Create;
try
Result.Capacity := ExprNodes.Count * 2;
PrevNode := nil;
for Node in ExprNodes do
begin
if Node.Typ = ntCall then
Continue;
if Assigned(PrevNode) and IsRoundOpen(Node.Typ) then
begin
if not TOperators.IsOpName(PrevNode.Typ) and not IsRoundOpen(PrevNode.Typ) then
Result.Add(CreateNodeWithParentsPosition(ntCall, Node.ParentNode));
if TOperators.IsOpName(PrevNode.Typ)
and (TOperators.Items[PrevNode.Typ].Kind = okUnary)
and (TOperators.Items[PrevNode.Typ].AssocType = atLeft)
then
Result.Add(CreateNodeWithParentsPosition(ntCall, Node.ParentNode));
end;
if Assigned(PrevNode) and (Node.Typ = ntTypeArgs) then
begin
if not TOperators.IsOpName(PrevNode.Typ) and (PrevNode.Typ <> ntTypeArgs) then
Result.Add(CreateNodeWithParentsPosition(ntGeneric, Node.ParentNode));
if TOperators.IsOpName(PrevNode.Typ)
and (TOperators.Items[PrevNode.Typ].Kind = okUnary)
and (TOperators.Items[PrevNode.Typ].AssocType = atLeft)
then
Result.Add(CreateNodeWithParentsPosition(ntGeneric, Node.ParentNode));
end;
if Node.Typ <> ntAlignmentParam then
Result.Add(Node.Clone);
PrevNode := Node;
end;
except
FreeAndNil(Result);
raise;
end;
end;
class function TExpressionTools.CreateNodeWithParentsPosition(NodeType: TSyntaxNodeType; ParentNode: TSyntaxNode): TSyntaxNode;
begin
Result := TSyntaxNode.Create(NodeType);
Result.AssignPositionFrom(ParentNode);
end;
class procedure TExpressionTools.RawNodeListToTree(RawParentNode: TSyntaxNode; RawNodeList: TList<TSyntaxNode>;
NewRoot: TSyntaxNode);
var
PreparedNodeList, ReverseNodeList: TList<TSyntaxNode>;
begin
try
PreparedNodeList := PrepareExpr(RawNodeList);
try
ReverseNodeList := ExprToReverseNotation(PreparedNodeList);
try
NodeListToTree(ReverseNodeList, NewRoot);
finally
ReverseNodeList.Free;
end;
finally
PreparedNodeList.Free;
end;
except
on E: Exception do
raise EParserException.Create(NewRoot.Line, NewRoot.Col, NewRoot.FileName, E.Message);
end;
end;
{ TSyntaxNode }
function TSyntaxNode.HasAttribute(const Key: TAttributeName): Boolean;
begin
Result := Key in FAttributesInUse;
end;
procedure TSyntaxNode.ClearAttributes;
begin
SetLength(FAttributes, 0);
FAttributesInUse:= [];
end;
function TSyntaxNode.GetHasChildren: Boolean;
begin
Result := Length(FChildNodes) > 0;
end;
function TSyntaxNode.GetChildCount: cardinal;
begin
Result:= Length(FChildNodes);
end;
function TSyntaxNode.AddChild(Typ: TSyntaxNodeType): TSyntaxNode;
begin
Result := AddChild(TSyntaxNode.Create(Typ));
end;
function TSyntaxNode.GetChildNode(index: cardinal): TSyntaxNode;
begin
Assert(index < ChildCount);
Result:= FChildNodes[index];
end;
procedure TSyntaxNode.DeleteChild(Node: TSyntaxNode);
begin
ExtractChild(Node);
Node.Free;
end;
procedure TSyntaxNode.SetAttribute(const Key: TAttributeName; const Value: string);
var
AttributeEntry: PAttributeEntry;
len: Integer;
begin
if not HasAttribute(Key) then
begin
if (Value = '') then Exit; //no action needed
len := Length(FAttributes);
SetLength(FAttributes, len + 1);
AttributeEntry := @FAttributes[len];
AttributeEntry^.Key := Key;
Include(FAttributesInUse, Key);
end;
if (Value = '') then RemoveAttribute(Key);
AttributeEntry^.Value := Value;
end;
procedure TSyntaxNode.RemoveAttribute(const Key: TAttributeName);
const
Size = SizeOf(TAttributeEntry);
var
Entry: PAttributeEntry;
Index: integer;
begin
if HasAttribute(Key) then begin
TryGetAttributeEntry(Key, Entry);
Index:= (NativeUInt(Entry) - NativeUInt(@FAttributes[0])) + Size;
Move(Entry^, Pointer(NativeUInt(Entry)+Size)^, (High(FAttributes) * Size) - Index);
Exclude(FAttributesInUse, Key);
end;
end;
function SameText(const Needle: string; const HayStack: array of string): boolean; overload;
var
S: string;
begin
for S in HayStack do begin
if (SameText(Needle, S)) then exit(true);
end;
Result:= false;
end;
function TSyntaxNode.TryGetAttributeEntry(const Key: TAttributeName; out AttributeEntry: PAttributeEntry): boolean;
var
i: integer;
begin
Result:= false;
if not(Key in FAttributesInUse) then begin
//Do not allow the AttributeEntry to be undefined.
AttributeEntry:= nil;
end else for i := 0 to High(FAttributes) do begin
if FAttributes[i].Key = Key then
begin
AttributeEntry := @FAttributes[i];
Exit(True);
end;
end;
end;
function TSyntaxNode.AddChild(Node: TSyntaxNode): TSyntaxNode;
begin
Assert(Assigned(Node));
SetLength(FChildNodes, Length(FChildNodes) + 1);
FChildNodes[Length(FChildNodes) - 1] := Node;
Node.FParentNode := Self;
Result := Node;
end;
procedure TSyntaxNode.AddChildren(Nodes: TArray<TSyntaxNode>);
var
Node: TSyntaxNode;
OldLength: integer;
begin
OldLength:= Length(FChildNodes);
SetLength(FChildNodes, OldLength + Length(Nodes));
for Node in Nodes do begin
FChildNodes[OldLength]:= Node;
Inc(OldLength);
Node.FParentNode:= Self;
end;
end;
function TSyntaxNode.Clone: TSyntaxNode;
var
i: Integer;
begin
Result := TSyntaxNodeClass(Self.ClassType).Create(FTyp);
SetLength(Result.FChildNodes, Length(FChildNodes));
for i := 0 to High(FChildNodes) do
begin
Result.FChildNodes[i] := FChildNodes[i].Clone;
Result.FChildNodes[i].FParentNode := Result;
end;
Result.FAttributes := Copy(FAttributes);
Result.FAttributesInUse:= FAttributesInUse;
Result.AssignPositionFrom(Self);
end;
constructor TSyntaxNode.Create(Typ: TSyntaxNodeType);
begin
inherited Create;
FTyp := Typ;
end;
function TSyntaxNode.ExtractChild(Node: TSyntaxNode): TSyntaxNode;
var
i: integer;
begin
Result:= nil; //do not allow undefined result
for i := 0 to High(FChildNodes) do
if FChildNodes[i] = Node then
begin
Result:= Node;
if i < High(FChildNodes) then
Move(FChildNodes[i + 1], FChildNodes[i], SizeOf(TSyntaxNode) * (Length(FChildNodes) - i - 1));
SetLength(FChildNodes, High(FChildNodes));
Break;
end;
end;
destructor TSyntaxNode.Destroy;
var
i: integer;
begin
for i := 0 to High(FChildNodes) do
FreeAndNil(FChildNodes[i]);
inherited;
end;
function TSyntaxNode.ExtractChild(Typ: TSyntaxNodeType): TSyntaxNode;
var
Child: TSyntaxNode;
begin
for Child in FChildNodes do begin
if (Child.Typ = Typ) then begin
ExtractChild(Child);
Exit(Child);
end;
end;
Result:= nil;
end;
function TSyntaxNode.FindNode(Typ: TSyntaxNodeType): TSyntaxNode;
var
i: Integer;
begin
for i := 0 to High(FChildNodes) do
if FChildNodes[i].Typ = Typ then
Exit(FChildNodes[i]);
Result := nil;
end;
function TSyntaxNode.FindNode(const Types: TSyntaxNodeTypes): TSyntaxNode;
var
i: integer;
begin
for i:= 0 to High(FChildNodes) do begin
if (FChildNodes[i].Typ in Types) then Exit(FChildNodes[i]);
end;
Result:= nil;
end;
function TSyntaxNode.GetAttribute(const Key: TAttributeName): string;
var
AttributeEntry: PAttributeEntry;
begin
if TryGetAttributeEntry(Key, AttributeEntry) then
Result := AttributeEntry^.Value
else
Result := '';
end;
procedure TSyntaxNode.AssignPositionFrom(const Node: TSyntaxNode);
begin
FCol := Node.Col;
FLine := Node.Line;
FFileName := Node.FileName;
end;
{ TCompoundSyntaxNode }
function TCompoundSyntaxNode.Clone: TSyntaxNode;
begin
Result := inherited;
TCompoundSyntaxNode(Result).EndLine := Self.EndLine;
TCompoundSyntaxNode(Result).EndCol := Self.EndCol;
end;
{ TValuedSyntaxNode }
function TValuedSyntaxNode.Clone: TSyntaxNode;
begin
Result := inherited;
TValuedSyntaxNode(Result).Value := Self.Value;
end;
{ TCommentNode }
function TCommentNode.Clone: TSyntaxNode;
begin
Result := inherited;
TCommentNode(Result).Text := Self.Text;
end;
{ EParserException }
constructor EParserException.Create(Line, Col: Integer; const FileName, Msg: string);
begin
inherited Create(Msg);
FFileName := FileName;
FLine := Line;
FCol := Col;
end;
end.