ocarina-backends-xml_tree-nutils.adb 17.8 KB
Newer Older
1
2
3
4
5
6
7
8
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--     O C A R I N A . B A C K E N D S . X M L _ T R E E . N U T I L S      --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
--    Copyright (C) 2008-2009 Telecom ParisTech, 2010-2014 ESA & ISAE.      --
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
--                                                                          --
-- Ocarina  is free software;  you  can  redistribute  it and/or  modify    --
-- it under terms of the GNU General Public License as published by the     --
-- Free Software Foundation; either version 2, or (at your option) any      --
-- later version. Ocarina 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. You should have received  a copy of the --
-- GNU General Public License distributed with Ocarina; see file COPYING.   --
-- If not, write to the Free Software Foundation, 51 Franklin Street, Fifth --
-- Floor, Boston, MA 02111-1301, USA.                                       --
--                                                                          --
-- As a special exception,  if other files  instantiate  generics from this --
-- unit, or you link  this unit with other files  to produce an executable, --
-- this  unit  does not  by itself cause  the resulting  executable to be   --
-- covered  by the  GNU  General  Public  License. This exception does not  --
-- however invalidate  any other reasons why the executable file might be   --
-- covered by the GNU Public License.                                       --
--                                                                          --
jhugues's avatar
jhugues committed
29
30
--                 Ocarina is maintained by the TASTE project               --
--                      (taste-users@lists.tuxfamily.org)                   --
31
32
33
34
35
36
37
--                                                                          --
------------------------------------------------------------------------------

with GNAT.Table;

with Charset;   use Charset;
with Locations; use Locations;
38
with Ocarina.Namet;     use Ocarina.Namet;
39
40
41
with Utils;     use Utils;

with Ocarina.Backends.Utils;
42
with Ocarina.ME_AADL.AADL_Tree.Nodes; use Ocarina.ME_AADL.AADL_Tree.Nodes;
43
use Ocarina.Backends.Utils;
44
with Ocarina.Backends.XML_Values;     use Ocarina.Backends.XML_Values;
45
46
47
48
49
50
51
52
53
54

package body Ocarina.Backends.XML_Tree.Nutils is

   package ATN renames Ocarina.ME_AADL.AADL_Tree.Nodes;
   package XTN renames Ocarina.Backends.XML_Tree.Nodes;

   Keyword_Suffix : constant String := "%XML";
   --  Used to mark XML keywords and avoid collision with other languages

   type Entity_Stack_Entry is record
55
56
      Current_File   : Node_Id;
      Current_Entity : Node_Id;
57
58
59
   end record;

   No_Depth : constant Int := -1;
60
61
62
63
64
65
   package Entity_Stack is new GNAT.Table
     (Entity_Stack_Entry,
      Int,
      No_Depth + 1,
      10,
      10);
66
67
68
69
70
71
72
73
74

   use Entity_Stack;

   ------------------------
   -- Add_Prefix_To_Name --
   ------------------------

   function Add_Prefix_To_Name
     (Prefix : String;
75
      Name   : Name_Id) return Name_Id
76
77
78
79
80
81
82
83
84
85
86
87
88
   is
   begin
      Set_Str_To_Name_Buffer (Prefix);
      Get_Name_String_And_Append (Name);
      return Name_Find;
   end Add_Prefix_To_Name;

   ------------------------
   -- Add_Suffix_To_Name --
   ------------------------

   function Add_Suffix_To_Name
     (Suffix : String;
89
      Name   : Name_Id) return Name_Id
90
91
92
93
94
95
96
97
98
99
100
101
102
   is
   begin
      Get_Name_String (Name);
      Add_Str_To_Name_Buffer (Suffix);
      return Name_Find;
   end Add_Suffix_To_Name;

   -----------------------------
   -- Remove_Suffix_From_Name --
   -----------------------------

   function Remove_Suffix_From_Name
     (Suffix : String;
103
      Name   : Name_Id) return Name_Id
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
   is
      Length   : Natural;
      Temp_Str : String (1 .. Suffix'Length);
   begin
      Set_Str_To_Name_Buffer (Suffix);
      Length := Name_Len;
      Get_Name_String (Name);
      if Name_Len > Length then
         Temp_Str := Name_Buffer (Name_Len - Length + 1 .. Name_Len);
         if Suffix = Temp_Str then
            Set_Str_To_Name_Buffer (Name_Buffer (1 .. Name_Len - Length));
            return Name_Find;
         end if;
      end if;
      return Name;
   end Remove_Suffix_From_Name;

   -------------------------
   -- Append_Node_To_List --
   -------------------------

   procedure Append_Node_To_List (E : Node_Id; L : List_Id) is
      Last : Node_Id;

   begin
      Last := XTN.Last_Node (L);
      if No (Last) then
         XTN.Set_First_Node (L, E);
      else
         XTN.Set_Next_Node (Last, E);
      end if;
      Last := E;
      while Present (Last) loop
         XTN.Set_Last_Node (L, Last);
         Last := XTN.Next_Node (Last);
      end loop;
   end Append_Node_To_List;

   -----------------------
   -- Insert_After_Node --
   -----------------------

   procedure Insert_After_Node (E : Node_Id; N : Node_Id) is
      Next : constant Node_Id := XTN.Next_Node (N);
   begin
      XTN.Set_Next_Node (N, E);
      XTN.Set_Next_Node (E, Next);
   end Insert_After_Node;

   ------------------------
   -- Insert_Before_Node --
   ------------------------

   procedure Insert_Before_Node (E : Node_Id; N : Node_Id; L : List_Id) is
      Entity : Node_Id;
   begin
      Entity := XTN.First_Node (L);
      if Entity = N then
         XTN.Set_Next_Node (E, Entity);
         XTN.Set_First_Node (L, E);
      else
         while Present (Entity) loop
            exit when XTN.Next_Node (Entity) = N;
            Entity := XTN.Next_Node (Entity);
         end loop;

         Insert_After_Node (E, Entity);
      end if;
   end Insert_Before_Node;

   ---------------
   -- Copy_Node --
   ---------------

   function Copy_Node (N : Node_Id) return Node_Id is
      C : Node_Id;
   begin
      case XTN.Kind (N) is
         when K_Defining_Identifier =>
            C := New_Node (K_Defining_Identifier);
            XTN.Set_Name (C, XTN.Name (N));
            XTN.Set_Corresponding_Node (C, XTN.Corresponding_Node (N));

         when K_Literal =>
            C := New_Node (K_Literal);
            XTN.Set_Value (C, XTN.Value (N));

         when others =>
            raise Program_Error;
      end case;
      return C;
   end Copy_Node;

   ---------------------
   -- Message_Comment --
   ---------------------

   function Message_Comment (M : Name_Id) return Node_Id is
      C : Node_Id;
   begin
      C := Make_XML_Comment (M);
      return C;
   end Message_Comment;

   ---------------------
   -- Message_Comment --
   ---------------------

   function Message_Comment (M : String) return Node_Id is
      C : Node_Id;
   begin
      Set_Str_To_Name_Buffer (M);
      C := Make_XML_Comment (Name_Find);
      return C;
   end Message_Comment;

   -----------
   -- Image --
   -----------

   function Image (T : Token_Type) return String is
      S : String := Token_Type'Image (T);
   begin
      To_Lower (S);
      return S (5 .. S'Last);
   end Image;

   ----------------
   -- Initialize --
   ----------------

   procedure Initialize is
   begin
      --  Initialize Nutils only once

      if Initialized then
         return;
      end if;

      Initialized := True;

      --  Keywords.
      for I in Keyword_Type loop
         New_Token (I);
      end loop;

      --  Graphic Characters
      New_Token (Tok_Slash, "/");
      New_Token (Tok_Less, "<");
      New_Token (Tok_Equal, "=");
      New_Token (Tok_Greater, ">");

   end Initialize;

   --------------
   -- Is_Empty --
   --------------

   function Is_Empty (L : List_Id) return Boolean is
   begin
      return L = No_List or else No (XTN.First_Node (L));
   end Is_Empty;

   ------------
   -- Length --
   ------------

   function Length (L : List_Id) return Natural is
      N : Node_Id;
      C : Natural := 0;
   begin
      if not Is_Empty (L) then
         N := XTN.First_Node (L);

         while Present (N) loop
            C := C + 1;
            N := XTN.Next_Node (N);
         end loop;
      end if;

      return C;
   end Length;

   --------------------
   -- Make_C_Comment --
   --------------------

291
   function Make_XML_Comment (N : Name_Id) return Node_Id is
292
293
294
295
296
297
298
299
300
301
302
303
      C : Node_Id;
   begin
      C := New_Node (K_XML_Comment);
      Set_Defining_Identifier (C, New_Node (K_Defining_Identifier));
      XTN.Set_Name (Defining_Identifier (C), N);
      return C;
   end Make_XML_Comment;

   ------------------------------
   -- Make_Defining_Identifier --
   ------------------------------

304
   function Make_Defining_Identifier (Name : Name_Id) return Node_Id is
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
      N : Node_Id;

   begin
      N := New_Node (K_Defining_Identifier);
      XTN.Set_Name (N, Name);
      return N;
   end Make_Defining_Identifier;

   ------------------
   -- Make_List_Id --
   ------------------

   function Make_List_Id
     (N1 : Node_Id;
      N2 : Node_Id := No_Node;
320
      N3 : Node_Id := No_Node) return List_Id
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
   is
      L : List_Id;
   begin
      L := New_List (K_List_Id);
      Append_Node_To_List (N1, L);
      if Present (N2) then
         Append_Node_To_List (N2, L);

         if Present (N3) then
            Append_Node_To_List (N3, L);
         end if;
      end if;
      return L;
   end Make_List_Id;

   -----------------
   -- Next_N_Node --
   -----------------

   function Next_N_Node (N : Node_Id; Num : Natural) return Node_Id is
      Result : Node_Id := N;
   begin
      for I in 1 .. Num loop
         Result := XTN.Next_Node (Result);
      end loop;

      return Result;
   end Next_N_Node;

   --------------
   -- New_List --
   --------------

   function New_List
     (Kind : XTN.Node_Kind;
356
357
      From : Node_Id := No_Node) return List_Id
   is
358
359
360
361
      N : Node_Id;

   begin
      XTN.Entries.Increment_Last;
362
      N                     := XTN.Entries.Last;
363
364
365
      XTN.Entries.Table (N) := XTN.Default_Node;
      Set_Kind (N, Kind);
      if Present (From) then
366
         XTN.Set_Loc (N, XTN.Loc (From));
367
      else
368
         XTN.Set_Loc (N, No_Location);
369
370
371
372
373
374
375
376
377
378
      end if;
      return List_Id (N);
   end New_List;

   --------------
   -- New_Node --
   --------------

   function New_Node
     (Kind : XTN.Node_Kind;
379
      From : Node_Id := No_Node) return Node_Id
380
381
382
383
   is
      N : Node_Id;
   begin
      XTN.Entries.Increment_Last;
384
      N                     := XTN.Entries.Last;
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
      XTN.Entries.Table (N) := XTN.Default_Node;
      XTN.Set_Kind (N, Kind);

      if Present (From) then
         XTN.Set_Loc (N, ATN.Loc (From));
      else
         XTN.Set_Loc (N, No_Location);
      end if;

      return N;
   end New_Node;

   ---------------
   -- New_Token --
   ---------------

401
   procedure New_Token (T : Token_Type; I : String := "") is
402
403
404
405
406
407
408
409
410
411
      Name : Name_Id;
   begin
      if T in Keyword_Type then
         --  Marking the token image as a keyword for fas searching
         --  purpose, we add the prefix to avoir collision with other
         --  languages keywords

         Set_Str_To_Name_Buffer (Image (T));
         Name := Name_Find;
         Name := Add_Suffix_To_Name (Keyword_Suffix, Name);
yoogx's avatar
yoogx committed
412
413
         Set_Name_Table_Byte
           (Name, Ocarina.Types.Byte (Token_Type'Pos (T) + 1));
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

         Set_Str_To_Name_Buffer (Image (T));
      else
         Set_Str_To_Name_Buffer (I);
      end if;
      Token_Image (T) := Name_Find;
   end New_Token;

   ----------------
   -- Pop_Entity --
   ----------------

   procedure Pop_Entity is
   begin
      if Last > No_Depth then
         Decrement_Last;
      end if;
   end Pop_Entity;

   -----------------
   -- Push_Entity --
   -----------------

   procedure Push_Entity (E : Node_Id) is
   begin
      Increment_Last;
      Table (Last).Current_Entity := E;
   end Push_Entity;

   ---------------------------
   -- Remove_Node_From_List --
   ---------------------------

   procedure Remove_Node_From_List (E : Node_Id; L : List_Id) is
      C : Node_Id;

   begin
      C := XTN.First_Node (L);
      if C = E then
         XTN.Set_First_Node (L, XTN.Next_Node (E));
         if XTN.Last_Node (L) = E then
            XTN.Set_Last_Node (L, No_Node);
         end if;
      else
         while Present (C) loop
            if XTN.Next_Node (C) = E then
               XTN.Set_Next_Node (C, XTN.Next_Node (E));
               if XTN.Last_Node (L) = E then
                  XTN.Set_Last_Node (L, C);
               end if;
               exit;
            end if;
            C := XTN.Next_Node (C);
         end loop;
      end if;
   end Remove_Node_From_List;

   ---------------
   -- To_C_Name --
   ---------------

   function To_XML_Name (N : Name_Id) return Name_Id is
      First     : Natural := 1;
      Name      : Name_Id;
      Test_Name : Name_Id;
479
      V         : Ocarina.Types.Byte;
480
481
   begin
      Get_Name_String (Normalize_Name (N));
482
      while First <= Name_Len and then Name_Buffer (First) = '_' loop
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
         First := First + 1;
      end loop;

      for I in First .. Name_Len loop
         if Name_Buffer (I) = '_'
           and then I < Name_Len
           and then Name_Buffer (I + 1) = '_'
         then
            Name_Buffer (I + 1) := 'U';
         end if;
      end loop;

      if Name_Buffer (Name_Len) = '_' then
         Add_Char_To_Name_Buffer ('U');
      end if;
      Name := Name_Find;

      --  If the identifier collides with an Ada reserved word insert
      --  "AADL_" string before the identifier.

      Test_Name := Add_Suffix_To_Name (Keyword_Suffix, Name);
504
      V         := Get_Name_Table_Byte (Test_Name);
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
      if V > 0 then
         Set_Str_To_Name_Buffer ("AADL_");
         Get_Name_String_And_Append (Name);
         Name := Name_Find;
      end if;

      return To_Lower (Name);
   end To_XML_Name;

   ----------------------------
   -- Conventional_Base_Name --
   ----------------------------

   function Conventional_Base_Name (N : Name_Id) return Name_Id is
   begin
      Get_Name_String (N);

      for Index in 1 .. Name_Len loop
         Name_Buffer (Index) := To_Lower (Name_Buffer (Index));
      end loop;

      return Name_Find;
   end Conventional_Base_Name;

   --------------------
   -- Current_Entity --
   --------------------

   function Current_Entity return Node_Id is
   begin
      if Last = No_Depth then
         return No_Node;
      else
         return Table (Last).Current_Entity;
      end if;
   end Current_Entity;

   ------------------
   -- Current_File --
   ------------------

   function Current_File return Node_Id is
   begin
      if Last = No_Depth then
         return No_Node;
      else
         return Table (Last).Current_File;
      end if;
   end Current_File;

   -------------------
   -- Make_XML_File --
   -------------------

559
   function Make_XML_File
560
561
     (Identifier : Node_Id;
      DTD        : Node_Id := No_Node) return Node_Id
562
   is
563
      File         : Node_Id;
564
565
      The_XML_Node : Node_Id;

566
   begin
567
      File         := New_Node (K_XML_File);
568
569
      The_XML_Node := New_Node (XTN.K_XML_Node);

570
571
572
      Set_Defining_Identifier (File, Identifier);
      Set_Corresponding_Node (Identifier, File);

573
      XTN.Set_Is_HTML (File, False);
574
575
576
577
      XTN.Set_Root_Node (File, The_XML_Node);
      if Present (DTD) then
         XTN.Set_XML_DTD (File, DTD);
      end if;
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597

      return File;
   end Make_XML_File;

   ------------------
   -- Make_Literal --
   ------------------

   function Make_Literal (Value : Value_Id) return Node_Id is
      N : Node_Id;
   begin
      N := New_Node (K_Literal);
      XTN.Set_Value (N, Value);
      return N;
   end Make_Literal;

   -------------------
   -- Make_XML_Node --
   -------------------

598
599
600
601
602
   function Make_XML_Node
     (Name_String : String            := "";
      Name_Nameid : Name_Id           := No_Name;
      Kind        : XML_New_Node_Kind := K_String) return Node_Id
   is
603
604
605
606
607
608
609
610
611
612
      N : Node_Id;
      L : List_Id;
      K : List_Id;
   begin
      N := New_Node (K_XML_Node);

      if Kind = K_String then
         Set_Str_To_Name_Buffer (Name_String);
         XTN.Set_Defining_Identifier (N, Make_Defining_Identifier (Name_Find));
      else
613
614
615
         XTN.Set_Defining_Identifier
           (N,
            Make_Defining_Identifier (Name_Nameid));
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
      end if;

      L := New_List (XTN.K_List_Id);
      XTN.Set_Items (N, L);

      K := New_List (XTN.K_List_Id);
      XTN.Set_Subitems (N, K);

      return N;
   end Make_XML_Node;

   ----------------------
   -- Make_Assignement --
   ----------------------

631
632
633
634
   function Make_Assignement
     (Left  : Node_Id;
      Right : Node_Id) return Node_Id
   is
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
      N : Node_Id;
   begin
      N := New_Node (XTN.K_Assignement);
      XTN.Set_Left_Expression (N, Left);
      XTN.Set_Right_Expression (N, Right);
      return N;
   end Make_Assignement;

   --------------------
   -- Make_Container --
   --------------------

   function Make_Container (Content : Node_Id) return Node_Id is
      N : Node_Id;
   begin
      N := New_Node (XTN.K_Container);
      XTN.Set_Content (N, Content);
      return N;
   end Make_Container;

655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
   -------------------
   -- Add_Attribute --
   -------------------

   procedure Add_Attribute (Key : String; Value : String; N : Node_Id) is
      R : Node_Id;
      Q : Node_Id;
   begin
      Set_Str_To_Name_Buffer (Key);
      R := Make_Defining_Identifier (Name_Find);
      Set_Str_To_Name_Buffer (Value);
      Q := Make_Defining_Identifier (Name_Find);
      Append_Node_To_List (Make_Assignement (R, Q), XTN.Items (N));
   end Add_Attribute;

670
671
672
673
674
   procedure Add_Attribute (Key : String; Value : Value_Id; N : Node_Id) is
   begin
      Add_Attribute (Key, Image (Value), N);
   end Add_Attribute;

675
end Ocarina.Backends.XML_Tree.Nutils;