File: otheridentifiertree.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (288 lines) | stat: -rw-r--r-- 8,178 bytes parent folder | download | duplicates (6)
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
{
 ***************************************************************************
 *                                                                         *
 *   This source is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This code is distributed in the hope that it will be useful, but      *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   General Public License for more details.                              *
 *                                                                         *
 *   A copy of the GNU General Public License is available on the World    *
 *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
 *   obtain it by writing to the Free Software Foundation,                 *
 *   Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.   *
 *                                                                         *
 ***************************************************************************

  Author: Mattias Gaertner

  Abstract:
    - tree storing identifiers of other languages (e.g. javascript)
    - identifier completion for other languages
}
unit OtherIdentifierTree;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileProcs, CodeTree;

type

  { TOtherIdentifierTreeNode }

  TOtherIdentifierTreeNode = class
  public
    Desc: TCodeTreeNodeDesc;
    SubDesc: TCodeTreeNodeSubDesc;
    Parent: TOtherIdentifierTreeNode;
    NextSibling, PrevSibling: TOtherIdentifierTreeNode;
    FirstChild, LastChild: TOtherIdentifierTreeNode;
    constructor Create; virtual;
    destructor Destroy; override;
    procedure ClearChilds;
    procedure AddLastChild(Child: TOtherIdentifierTreeNode); virtual;
    procedure RemoveChild(Child: TOtherIdentifierTreeNode); virtual;
    function Next: TOtherIdentifierTreeNode;
    function NextSkipChilds: TOtherIdentifierTreeNode;
    function Prev: TOtherIdentifierTreeNode;
    function PrevSkipChilds: TOtherIdentifierTreeNode;
    function HasAsParent(Node: TOtherIdentifierTreeNode): boolean;
    function HasAsChild(Node: TOtherIdentifierTreeNode): boolean;
    function HasParentOfType(ParentDesc: TCodeTreeNodeDesc): boolean;
    function HasAsRoot(RootNode: TOtherIdentifierTreeNode): boolean;
    function GetRootNode: TOtherIdentifierTreeNode;
    function GetParentOfType(ADesc: TCodeTreeNodeDesc): TOtherIdentifierTreeNode;
    function GetParentOfTypes(Descriptors: array of TCodeTreeNodeDesc): TOtherIdentifierTreeNode;
    function ChildCount: integer; virtual;
    function AsString: string; virtual;
    procedure ConsistencyCheck; virtual;
    procedure WriteDebugReport(const Prefix: string; WithChilds: boolean); virtual;
  end;

  TOtherIdentifierTree = class
  public
    Root: TOtherIdentifierTreeNode;
    constructor Create;
    destructor Destroy; override;
    procedure ClearNodes;
  end;

implementation

{ TOtherIdentifierTreeNode }

procedure TOtherIdentifierTreeNode.AddLastChild(Child: TOtherIdentifierTreeNode);
begin
  Child.Parent:=Self;
  if LastChild<>nil then
    LastChild.NextSibling:=Child;
  Child.PrevSibling:=LastChild;
  LastChild:=Child;
  if FirstChild=nil then FirstChild:=Child;
end;

procedure TOtherIdentifierTreeNode.RemoveChild(Child: TOtherIdentifierTreeNode);
begin
  if FirstChild=Child then FirstChild:=Child.NextSibling;
  if LastChild=Child then LastChild:=Child.PrevSibling;
  Child.Parent:=nil;
end;

constructor TOtherIdentifierTreeNode.Create;
begin

end;

destructor TOtherIdentifierTreeNode.Destroy;
begin
  ClearChilds;
  if Parent<>nil then Parent.RemoveChild(Self);
  if NextSibling<>nil then NextSibling.PrevSibling:=PrevSibling;
  if PrevSibling<>nil then PrevSibling.NextSibling:=NextSibling;
  PrevSibling:=nil;
  NextSibling:=nil;
  inherited Destroy;
end;

procedure TOtherIdentifierTreeNode.ClearChilds;
begin
  while LastChild<>nil do LastChild.Free;
end;

function TOtherIdentifierTreeNode.Next: TOtherIdentifierTreeNode;
begin
  Result:=FirstChild;
  if Result<>nil then exit;
  Result:=NextSkipChilds;
end;

function TOtherIdentifierTreeNode.NextSkipChilds: TOtherIdentifierTreeNode;
begin
  Result:=Self;
  while (Result<>nil) do begin
    if Result.NextSibling<>nil then
      exit(Result.NextSibling);
    Result:=Result.Parent;
  end;
end;

function TOtherIdentifierTreeNode.Prev: TOtherIdentifierTreeNode;
begin
  if PrevSibling=nil then
    Result:=Parent
  else begin
    Result:=PrevSibling;
    while Result.LastChild<>nil do Result:=Result.LastChild;
  end;
end;

function TOtherIdentifierTreeNode.PrevSkipChilds: TOtherIdentifierTreeNode;
begin
  if PrevSibling<>nil then
    Result:=PrevSibling
  else
    Result:=Parent;
end;

function TOtherIdentifierTreeNode.HasAsParent(Node: TOtherIdentifierTreeNode
  ): boolean;
var
  CurNode: TOtherIdentifierTreeNode;
begin
  CurNode:=Parent;
  while CurNode<>nil do begin
    if CurNode=Node then exit(true);
    CurNode:=CurNode.Parent;
  end;
  Result:=false;
end;

function TOtherIdentifierTreeNode.HasAsChild(Node: TOtherIdentifierTreeNode
  ): boolean;
begin
  Result:=(Node<>nil) and Node.HasAsParent(Self);
end;

function TOtherIdentifierTreeNode.HasParentOfType(ParentDesc: TCodeTreeNodeDesc
  ): boolean;
begin
  Result:=GetParentOfType(ParentDesc)<>nil;
end;

function TOtherIdentifierTreeNode.HasAsRoot(RootNode: TOtherIdentifierTreeNode
  ): boolean;
begin
  Result:=GetRootNode=RootNode;
end;

function TOtherIdentifierTreeNode.GetRootNode: TOtherIdentifierTreeNode;
begin
  Result:=Self;
  while Result.Parent<>nil do Result:=Result.Parent;
end;

function TOtherIdentifierTreeNode.GetParentOfType(ADesc: TCodeTreeNodeDesc
  ): TOtherIdentifierTreeNode;
begin
  Result:=Parent;
  while (Result<>nil) do begin
    if Result.Desc=ADesc then exit;
    Result:=Result.Parent;
  end;
end;

function TOtherIdentifierTreeNode.GetParentOfTypes(
  Descriptors: array of TCodeTreeNodeDesc): TOtherIdentifierTreeNode;
var
  i: Integer;
begin
  Result:=Parent;
  while (Result<>nil) do begin
    for i:=low(Descriptors) to high(Descriptors) do
      if Result.Desc=Descriptors[i] then exit;
    Result:=Result.Parent;
  end;
end;

function TOtherIdentifierTreeNode.ChildCount: integer;
var
  Node: TOtherIdentifierTreeNode;
begin
  Result:=0;
  Node:=FirstChild;
  while Node<>nil do begin
    inc(Result);
    Node:=Node.NextSibling;
  end;
end;

function TOtherIdentifierTreeNode.AsString: string;
begin
  Result:=IntToStr(Desc);
end;

procedure TOtherIdentifierTreeNode.ConsistencyCheck;
var
  Node: TOtherIdentifierTreeNode;
begin
  if (NextSibling=Self) then
    raise Exception.Create('');
  if (PrevSibling=Self) then
    raise Exception.Create('');
  if (NextSibling<>nil) and (NextSibling.PrevSibling<>Self) then
    raise Exception.Create('');
  if (PrevSibling<>nil) and (PrevSibling.NextSibling<>Self) then
    raise Exception.Create('');
  if Parent=Self then
    raise Exception.Create('');

  if (FirstChild=nil)<>(LastChild=nil) then
    raise Exception.Create('');
  Node:=FirstChild;
  while Node<>nil do begin
    if Node.Parent<>Self then
      raise Exception.Create('');
    if (Node=FirstChild) and (Node.PrevSibling<>nil) then
      raise Exception.Create('');
    if (Node=LastChild) and (Node.NextSibling<>nil) then
      raise Exception.Create('');
    Node.ConsistencyCheck;
    Node:=Node.NextSibling;
  end;
end;

procedure TOtherIdentifierTreeNode.WriteDebugReport(const Prefix: string;
  WithChilds: boolean);
begin
  writeln(Prefix,AsString);
  if WithChilds then
    WriteDebugReport(Prefix+'  ',true);
end;

{ TOtherIdentifierTree }

constructor TOtherIdentifierTree.Create;
begin

end;

destructor TOtherIdentifierTree.Destroy;
begin
  ClearNodes;
  inherited Destroy;
end;

procedure TOtherIdentifierTree.ClearNodes;
begin
  FreeAndNil(Root);
end;

end.