anagram_1.0.0_49233f56/sources/anagram-grammars-rule_templates.adb

  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
--  Copyright (c) 2010-2017 Maxim Reznik <reznikmm@gmail.com>
--
--  SPDX-License-Identifier: MIT
--  License-Filename: LICENSE
-------------------------------------------------------------

with Ada.Containers.Ordered_Sets;
with Ada.Containers.Ordered_Maps;

package body Anagram.Grammars.Rule_Templates is

   --------------------
   -- Attribute_Name --
   --------------------

   function Attribute_Name
     (Self  : Rule_Template;
      Index : Positive)
      return S.Universal_String
   is
   begin
      return Self.Attrs.Element (Index);
   end Attribute_Name;

   -----------
   -- Count --
   -----------

   function Count (Self : Rule_Template) return Natural is
   begin
      return Self.Parts.Length;
   end Count;

   ------------
   -- Create --
   ------------

   function Create
     (Text : S.Universal_String)
      return Rule_Template
   is
      procedure Append (Part, Attr, Def : S.Universal_String);

      package String_Sets is new Ada.Containers.Ordered_Sets
        (S.Universal_String, S."<", S."=");

      Ready : String_Sets.Set;
      Parts : League.String_Vectors.Universal_String_Vector;
      Attrs : League.String_Vectors.Universal_String_Vector;
      Defs  : League.String_Vectors.Universal_String_Vector;

      procedure Append (Part, Attr, Def : S.Universal_String) is
      begin
         Parts.Append (Part);
         Attrs.Append (Attr);
         Defs.Append (Def);
      end Append;

      Items : constant League.String_Vectors.Universal_String_Vector :=
        Text.Split ('$');
   begin
      for J in 2 .. Items.Length loop
         declare
            To    : Natural;
            Point : Natural;
            Colon : Natural;
            Name  : S.Universal_String;
            Item  : constant S.Universal_String := Items.Element (J);
         begin
            if Item.Starts_With ("{") then
               To    := Item.Index ('}');
               Point := Item.Index ('.');
               Colon := Item.Index (':');

               if Colon = 0 or Colon > To then
                  Colon := To;
               end if;

               if To > 0 and Point < To then
                  Name := Item.Slice (1, To);

                  if not Ready.Contains (Name) then
                     Ready.Insert (Name);

                     if Point = 0 then
                        Append
                          (Part => League.Strings.Empty_Universal_String,
                           Attr => Item.Slice (2, Colon - 1),
                           Def  => Item.Slice (Colon + 1, To - 1));
                     else
                        Append
                          (Part => Item.Slice (2, Point - 1),
                           Attr => Item.Slice (Point + 1, Colon - 1),
                           Def  => Item.Slice (Colon + 1, To - 1));
                     end if;
                  end if;
               end if;
            end if;
         end;
      end loop;

      return (Text  => Text,
              Parts => Parts,
              Attrs => Attrs,
              Defs  => Defs);
   end Create;

   -------------
   -- Default --
   -------------

   function Default
     (Self  : Rule_Template;
      Index : Positive) return S.Universal_String is
   begin
      return Self.Defs.Element (Index);
   end Default;

   -----------------
   -- Has_Default --
   -----------------

   function Has_Default
     (Self  : Rule_Template;
      Index : Positive) return Boolean is
   begin
      return not Self.Default (Index).Is_Empty;
   end Has_Default;

   ---------------
   -- Part_Name --
   ---------------

   function Part_Name
     (Self  : Rule_Template;
      Index : Positive)
      return S.Universal_String
   is
   begin
      return Self.Parts.Element (Index);
   end Part_Name;

   ----------------
   -- Substitute --
   ----------------

   function Substitute
     (Self   : Rule_Template;
      Values : League.String_Vectors.Universal_String_Vector)
     return S.Universal_String
   is
      use type S.Universal_String;

      package Maps is new Ada.Containers.Ordered_Maps
        (S.Universal_String, S.Universal_String);

      Map   : Maps.Map;
      Items : League.String_Vectors.Universal_String_Vector :=
        Self.Text.Split ('$');
   begin
      for J in 1 .. Self.Count loop
         Map.Insert
           (Self.Part_Name (J) & "." & Self.Attribute_Name (J),
            Values.Element (J));
      end loop;

      for J in 2 .. Items.Length loop
         declare
            To    : Natural;
            Point : Natural;
            Colon : Natural;
            Name  : S.Universal_String;
            Item  : S.Universal_String := Items.Element (J);
         begin
            if Item.Starts_With ("{") then
               To    := Item.Index ('}');
               Point := Item.Index ('.');
               Colon := Item.Index (':');

               if Colon = 0 or Colon > To then
                  Colon := To;
               end if;

               if To > 0 and Point < To then
                  Name := Item.Slice (2, Colon - 1);

                  Item.Replace (1, To, Map.Element (Name));

                  Items.Replace (J, Item);
               end if;
            end if;
         end;
      end loop;

      return Items.Join ("");
   end Substitute;

   ----------
   -- Text --
   ----------

   function Text  (Self : Rule_Template) return S.Universal_String is
   begin
      return Self.Text;
   end Text;

end Anagram.Grammars.Rule_Templates;