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 | -- part of AdaYaml, (c) 2017 Felix Krause
-- released under the terms of the MIT license, see the file "copying.txt"
with Ada.Unchecked_Deallocation;
with Yaml.Dom.Node;
with Yaml.Dom.Sequence_Data;
with Yaml.Dom.Mapping_Data;
with Yaml.Dom.Node_Memory;
package body Yaml.Dom is
use type Text.Reference;
use type Count_Type;
use type Node.Instance;
function For_Document (Document : not null access Document_Instance)
return Sequence_Data.Instance with Import,
Convention => Ada, Link_Name => "AdaYaml__Sequence_Data__For_Document";
function For_Document (Document : not null access Document_Instance)
return Mapping_Data.Instance with Import,
Convention => Ada, Link_Name => "AdaYaml__Mapping_Data__For_Document";
type Nullable_Node_Pointer is access all Node.Instance;
procedure Free is new Ada.Unchecked_Deallocation
(Node.Instance, Nullable_Node_Pointer);
procedure Decrease_Refcount (Object : not null access Document_Instance) is
begin
if Object.Refcount = 1 and then Object.Root_Node /= null then
declare
Memory : Node_Memory.Instance;
procedure Visit_Pair (Key, Value : not null access Node.Instance);
procedure Visit (Cur : not null access Node.Instance) is
Visited : Boolean;
begin
Memory.Visit (Cur, Visited);
if Visited then
return;
end if;
case Cur.Kind is
when Scalar => null;
when Sequence => Cur.Items.Iterate (Visit'Access);
when Mapping => Cur.Pairs.Iterate (Visit_Pair'Access);
end case;
end Visit;
procedure Visit_Pair (Key, Value : not null access Node.Instance) is
begin
Visit (Key);
Visit (Value);
end Visit_Pair;
begin
Visit (Object.Root_Node);
while not Memory.Is_Empty loop
declare
Ptr : Nullable_Node_Pointer :=
Nullable_Node_Pointer (Memory.Pop_First);
begin
Free (Ptr);
end;
end loop;
end;
end if;
Yaml.Decrease_Refcount (Object);
end Decrease_Refcount;
procedure Adjust (Object : in out Document_Reference) is
begin
Increase_Refcount (Object.Data);
end Adjust;
procedure Finalize (Object : in out Document_Reference) is
begin
Dom.Decrease_Refcount (Object.Data);
end Finalize;
procedure Adjust (Object : in out Node_Reference) is
begin
Increase_Refcount (Object.Document);
end Adjust;
procedure Finalize (Object : in out Node_Reference) is
begin
Dom.Decrease_Refcount (Object.Document);
end Finalize;
procedure Adjust (Object : in out Optional_Node_Reference) is
begin
if Object.Document /= null then
Increase_Refcount (Object.Document);
end if;
end Adjust;
procedure Finalize (Object : in out Optional_Node_Reference) is
begin
if Object.Document /= null then
Dom.Decrease_Refcount (Object.Document);
end if;
end Finalize;
function New_Sequence (Document : not null access Document_Instance;
Tag : Text.Reference;
Style : Collection_Style_Type)
return Node_Pointer is
(new Node.Instance'(Kind => Sequence, Tag => Tag, Sequence_Style => Style,
Items => For_Document (Document)));
function New_Mapping (Document : not null access Document_Instance;
Tag : Text.Reference;
Style : Collection_Style_Type)
return Node_Pointer is
(new Node.Instance'(Kind => Mapping, Tag => Tag, Mapping_Style => Style,
Pairs => For_Document (Document)));
function New_Document (Pool : Text.Pool.Reference :=
Text.Pool.With_Capacity (Text.Pool.Default_Size);
Implicit_Start, Implicit_End : Boolean := True)
return Document_Reference
is
begin
return (Ada.Finalization.Controlled with Data =>
new Document_Instance'(Refcount_Base with
Pool => Pool, Root_Node => null,
Implicit_Start => Implicit_Start,
Implicit_End => Implicit_End));
end New_Document;
function New_Scalar (Parent : Document_Reference'Class;
Content : String := "";
Tag : Text.Reference := Yaml.Tags.Question_Mark;
Style : Scalar_Style_Type := Any)
return Node_Reference is
begin
Increase_Refcount (Parent.Data);
return ((Ada.Finalization.Controlled with Document => Parent.Data, Data =>
new Node.Instance'(Tag => Tag, Kind => Scalar,
Scalar_Style => Style,
Content => Parent.Data.Pool.From_String (Content))));
end New_Scalar;
function New_Scalar (Parent : Document_Reference'Class;
Content : Text.Reference;
Tag : Text.Reference := Yaml.Tags.Question_Mark;
Style : Scalar_Style_Type := Any)
return Node_Reference is
begin
Increase_Refcount (Parent.Data);
return ((Ada.Finalization.Controlled with Document => Parent.Data, Data =>
new Node.Instance'(Tag => Tag, Kind => Scalar,
Scalar_Style => Style, Content => Content)));
end New_Scalar;
function New_Sequence (Parent : Document_Reference'Class;
Tag : Text.Reference := Yaml.Tags.Question_Mark;
Style : Collection_Style_Type := Any)
return Node_Reference is
begin
Increase_Refcount (Parent.Data);
return ((Ada.Finalization.Controlled with Document => Parent.Data, Data =>
New_Sequence (Parent.Data, Tag, Style)));
end New_Sequence;
function New_Mapping (Parent : Document_Reference'Class;
Tag : Text.Reference := Yaml.Tags.Question_Mark;
Style : Collection_Style_Type := Any)
return Node_Reference is
begin
Increase_Refcount (Parent.Data);
return ((Ada.Finalization.Controlled with Document => Parent.Data, Data =>
New_Mapping (Parent.Data, Tag, Style)));
end New_Mapping;
function Nodes_Equal (Left, Right : access Node.Instance) return Boolean is
(Left = Right or else (Left /= null and then Right /= null and then
Left.all = Right.all));
function "=" (Left, Right : Document_Reference) return Boolean is
(Nodes_Equal (Left.Data.Root_Node, Right.Data.Root_Node));
function "=" (Left, Right : Node_Reference) return Boolean is
(Same_Node (Left, Right) or else Left.Data.all = Right.Data.all);
-- checks whether the two references reference the same node
function Same_Node (Left, Right : Node_Reference) return Boolean is
(Left.Data = Right.Data);
function Is_Empty (Object : Document_Reference) return Boolean is
(Object.Data.Root_Node = null);
function Root (Object : Document_Reference'Class) return Node_Reference is
begin
Increase_Refcount (Object.Data);
return (Ada.Finalization.Controlled with Document => Object.Data,
Data => Node_Pointer (Object.Data.Root_Node));
end Root;
procedure Set_Root (Object : Document_Reference;
Value : Node_Reference'Class) is
begin
Object.Data.Root_Node := Value.Data;
end Set_Root;
procedure Set_Root (Object : Document_Reference;
Value : Optional_Node_Reference'Class) is
begin
Object.Data.Root_Node := Value.Data;
end Set_Root;
function Starts_Implicitly (Object : Document_Reference) return Boolean is
(Object.Data.Implicit_Start);
function Ends_Implicitly (Object : Document_Reference) return Boolean is
(Object.Data.Implicit_End);
procedure Set_Representation_Hints (Object : Document_Reference;
Implicit_Start, Implicit_End : Boolean)
is begin
Object.Data.Implicit_Start := Implicit_Start;
Object.Data.Implicit_End := Implicit_End;
end Set_Representation_Hints;
function Value (Object : Node_Reference) return Accessor is
((Data => Object.Data));
function Value (Object : Optional_Node_Reference) return Accessor is
((Data => Object.Data));
function Required (Object : Optional_Node_Reference'Class)
return Node_Reference is
begin
Increase_Refcount (Object.Document);
return (Ada.Finalization.Controlled with Document => Object.Document,
Data => Node_Pointer (Object.Data));
end Required;
function Optional (Object : Node_Reference'Class)
return Optional_Node_Reference is
begin
Increase_Refcount (Object.Document);
return (Ada.Finalization.Controlled with Document => Object.Document,
Data => Object.Data);
end Optional;
end Yaml.Dom;
|