1
+ with Ada.Containers.Ordered_Maps ;
1
2
with Ada.Strings.Unbounded ; use Ada.Strings.Unbounded;
2
3
with Ada.Text_IO ; use Ada.Text_IO;
3
4
with Ada.Text_IO.Unbounded_IO ; use Ada.Text_IO.Unbounded_IO;
@@ -19,7 +20,11 @@ procedure Commands is
19
20
Context : constant Lk_Context := Create_Context (Self_Id);
20
21
21
22
procedure Check (Filename : String; Buffer : String := " var i: Int = 0;" );
22
- procedure Remove_Ids (Value : JSON_Value);
23
+
24
+ procedure Reset_Ids (Value : JSON_Value);
25
+ -- Ids in prettier are generated with a process-wide counter. To avoid
26
+ -- interference between tests, we use this procedure to renumber Ids for a
27
+ -- given document.
23
28
24
29
-- ---------
25
30
-- Check --
@@ -58,19 +63,26 @@ procedure Commands is
58
63
-- Remove "id" fields from the JSON representation, for output stability
59
64
60
65
JSON := GNATCOLL.JSON.Read (JSON_Text);
61
- Remove_Ids (JSON);
66
+ Reset_Ids (JSON);
62
67
63
68
JSON_Text := JSON.Write (Compact => False);
64
69
Put_Line (JSON_Text);
65
70
New_Line;
66
71
end Check ;
67
72
68
- -- --------------
69
- -- Remove_Ids --
70
- -- --------------
73
+ -- -------------
74
+ -- Reset_Ids --
75
+ -- -------------
76
+
77
+ procedure Reset_Ids (Value : JSON_Value) is
78
+
79
+ package Id_Maps is new Ada.Containers.Ordered_Maps
80
+ (Key_Type => Integer, Element_Type => Integer);
81
+ Id_Map : Id_Maps.Map;
71
82
72
- procedure Remove_Ids (Value : JSON_Value) is
73
83
procedure Process (Name : String; Value : JSON_Value);
84
+ procedure Renumber (Object : JSON_Value; Name : String);
85
+ procedure Recurse (Value : JSON_Value);
74
86
75
87
-- -----------
76
88
-- Process --
@@ -79,25 +91,60 @@ procedure Commands is
79
91
procedure Process (Name : String; Value : JSON_Value) is
80
92
pragma Unreferenced (Name);
81
93
begin
82
- Remove_Ids (Value);
94
+ Recurse (Value);
83
95
end Process ;
96
+
97
+ -- ------------
98
+ -- Renumber --
99
+ -- ------------
100
+
101
+ procedure Renumber (Object : JSON_Value; Name : String) is
102
+ begin
103
+ if Object.Has_Field (Name) then
104
+ declare
105
+ Old_Id : constant Integer := Object.Get (Name);
106
+ New_Id : Integer;
107
+ begin
108
+ if Old_Id = 0 then
109
+ Object.Unset_Field (Name);
110
+ else
111
+ if Id_Map.Contains (Old_Id) then
112
+ New_Id := Id_Map.Element (Old_Id);
113
+ else
114
+ New_Id := Integer (Id_Map.Length) + 1 ;
115
+ Id_Map.Insert (Old_Id, New_Id);
116
+ end if ;
117
+ Object.Set_Field (Name, New_Id);
118
+ end if ;
119
+ end ;
120
+ end if ;
121
+ end Renumber ;
122
+
123
+ -- -----------
124
+ -- Recurse --
125
+ -- -----------
126
+
127
+ procedure Recurse (Value : JSON_Value) is
128
+ begin
129
+ case Value.Kind is
130
+ when JSON_Object_Type =>
131
+ Renumber (Value, " id" );
132
+ Renumber (Value, " ifBreakGroupId" );
133
+ Value.Map_JSON_Object (Process'Access );
134
+
135
+ when JSON_Array_Type =>
136
+ for V of JSON_Array'(Value.Get) loop
137
+ Recurse (V);
138
+ end loop ;
139
+
140
+ when others =>
141
+ return ;
142
+ end case ;
143
+ end Recurse ;
144
+
84
145
begin
85
- case Value.Kind is
86
- when JSON_Object_Type =>
87
- if Value.Has_Field (" id" ) then
88
- Value.Unset_Field (" id" );
89
- end if ;
90
- Value.Map_JSON_Object (Process'Access );
91
-
92
- when JSON_Array_Type =>
93
- for V of JSON_Array'(Value.Get) loop
94
- Remove_Ids (V);
95
- end loop ;
96
-
97
- when others =>
98
- return ;
99
- end case ;
100
- end Remove_Ids ;
146
+ Recurse (Value);
147
+ end Reset_Ids ;
101
148
102
149
begin
103
150
Check (" cmd_align.json" );
0 commit comments