anagram_1.0.0_49233f56/sources/anagram-grammars_checks.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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
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
--  Copyright (c) 2010-2017 Maxim Reznik <reznikmm@gmail.com>
--
--  SPDX-License-Identifier: MIT
--  License-Filename: LICENSE
-------------------------------------------------------------

with Ada.Wide_Wide_Text_IO;

package body Anagram.Grammars_Checks is

   use Anagram.Grammars;

   ---------------------
   -- Is_L_Attributed --
   ---------------------

   function Is_L_Attributed
     (Self : Anagram.Grammars.Grammar)
     return Boolean
   is

      function Check
        (Part : Part_Index;
         Attr : Attribute_Index) return Boolean;

      -----------
      -- Check --
      -----------

      function Check
        (Part : Part_Index;
         Attr : Attribute_Index) return Boolean is
      begin
         if Self.Attribute (Attr).Is_Left_Hand_Side then
            return Self.Declaration
              (Self.Attribute (Attr).Declaration).Is_Inherited;
         else
            return Self.Attribute (Attr).Origin < Part;
         end if;
      end Check;

   begin
      for J in 1 .. Self.Last_Rule loop
         declare
            Result : constant Attribute_Index := Self.Rule (J).Result;
            Attr   : Attribute renames Self.Attribute (Result);
            Decl   : Attribute_Declaration renames
              Self.Declaration (Attr.Declaration);
         begin
            if Decl.Is_Inherited and not Attr.Is_Left_Hand_Side then
               for A in
                 Self.Rule (J).First_Argument .. Self.Rule (J).Last_Argument
               loop
                  if not Check (Attr.Origin, A) then
                     return False;
                  end if;
               end loop;
            end if;
         end;
      end loop;

      return True;
   end Is_L_Attributed;

   --------------------
   -- Is_Well_Formed --
   --------------------

   function Is_Well_Formed
     (Self    : Anagram.Grammars.Grammar;
      Verbose : Boolean)
     return Boolean
   is
      function Check
        (NT : Non_Terminal_Index;
         P  : Production_Index)
        return Boolean;

      function Check_NT
        (NT   : Non_Terminal_Index;
         From : Production_Index;
         To   : Production_Count)
        return Boolean;

      type Count_Array is array (Attribute_Declaration_Index range <>)
        of Natural;

      function Check_Count
        (NT    : Non_Terminal_Index;
         Item  : Non_Terminal_Index;
         Count : Count_Array;
         Prod  : Production_Index;
         Part  : Part_Count := 0;
         RHS   : Boolean := True)
        return Boolean;

      function Check_Local
        (NT    : Non_Terminal_Index;
         Prod  : Production_Index;
         Count : Count_Array)
        return Boolean;

      -----------------
      -- Check_Count --
      -----------------

      function Check_Count
        (NT    : Non_Terminal_Index;
         Item  : Non_Terminal_Index;
         Count : Count_Array;
         Prod  : Production_Index;
         Part  : Part_Count := 0;
         RHS   : Boolean := True)
        return Boolean
      is
         function Part_Name return Wide_Wide_String;

         function Part_Name return Wide_Wide_String is
         begin
            if RHS then
               return Self.Non_Terminal (NT).Name.To_Wide_Wide_String & "." &
                 Self.Production (Prod).Name.To_Wide_Wide_String &
                 " " &
                 Self.Part (Part).Name.To_Wide_Wide_String &
                 ".";
            else
               return Self.Non_Terminal (NT).Name.To_Wide_Wide_String & "." &
                 Self.Production (Prod).Name.To_Wide_Wide_String &
                 " LHS.";
            end if;
         end Part_Name;

         Result : Boolean := True;
         From   : constant Attribute_Declaration_Index :=
           Self.Non_Terminal (Item).First_Attribute;
         To     : constant Attribute_Declaration_Count :=
           Self.Non_Terminal (Item).Last_Attribute;
      begin
         for Decl in From .. To loop
            if Self.Declaration (Decl).Is_Inherited xor RHS then
               if Count (Count'First + Decl - From) /= 0 then
                  if Verbose then
                     Ada.Wide_Wide_Text_IO.Put_Line
                       ("Not Well Formed: unexpected rule for " &
                          Part_Name &
                          Self.Declaration (Decl).Name.To_Wide_Wide_String);
                  end if;

                  Result := False;
               end if;
            else
               if Count (Count'First + Decl - From) = 0 then
                  if Verbose then
                     Ada.Wide_Wide_Text_IO.Put_Line
                       ("Not Well Formed: no rule for " &
                          Part_Name &
                          Self.Declaration (Decl).Name.To_Wide_Wide_String);
                  end if;

                  Result := False;
               elsif Count (Count'First + Decl - From) > 1 then
                  if Verbose then
                     Ada.Wide_Wide_Text_IO.Put_Line
                       ("Not Well Formed: not unique rule for " &
                          Part_Name &
                          Self.Declaration (Decl).Name.To_Wide_Wide_String);
                  end if;

                  Result := False;
               end if;
            end if;
         end loop;

         return Result;
      end Check_Count;

      -----------------
      -- Check_Local --
      -----------------

      function Check_Local
        (NT    : Non_Terminal_Index;
         Prod  : Production_Index;
         Count : Count_Array) return Boolean
      is
         Result : Boolean := True;
         From   : constant Attribute_Declaration_Index :=
           Self.Production (Prod).First_Attribute;
         To     : constant Attribute_Declaration_Count :=
           Self.Production (Prod).Last_Attribute;
      begin
         for Decl in From .. To loop
            if Count (Count'First + Decl - From) = 0 then
               if Verbose then
                  Ada.Wide_Wide_Text_IO.Put_Line
                    ("Not Well Formed: no rule for local attribute " &
                       Self.Non_Terminal (NT).Name.To_Wide_Wide_String & "." &
                       Self.Production (Prod).Name.To_Wide_Wide_String & " " &
                       Self.Declaration (Decl).Name.To_Wide_Wide_String);
               end if;

               Result := False;
            elsif Count (Count'First + Decl - From) > 1 then
               if Verbose then
                  Ada.Wide_Wide_Text_IO.Put_Line
                    ("Not Well Formed: not unique rule for local attribute " &
                       Self.Non_Terminal (NT).Name.To_Wide_Wide_String & "." &
                       Self.Production (Prod).Name.To_Wide_Wide_String & " " &
                       Self.Declaration (Decl).Name.To_Wide_Wide_String);
               end if;

               Result := False;
            end if;
         end loop;

         return Result;
      end Check_Local;

      -----------
      -- Check --
      -----------

      function Check
        (NT : Non_Terminal_Index;
         P  : Production_Index)
        return Boolean
      is
         Result : Boolean := True;
         From   : constant Part_Index := Self.Production (P).First;
         To     : constant Part_Count := Self.Production (P).Last;
         First  : array (From .. To) of Attribute_Declaration_Index;
         Offset : array (From .. To) of Attribute_Declaration_Count;

         LHS    : constant Attribute_Declaration_Count :=
           Self.Non_Terminal (NT).Last_Attribute -
           Self.Non_Terminal (NT).First_Attribute + 1;

         Local  : constant Attribute_Declaration_Count :=
           Self.Production (P).Last_Attribute -
           Self.Production (P).First_Attribute + 1;

         Total  : Attribute_Declaration_Count := LHS + Local;
      begin
         --  Fill Offset and First arrays
         for J in Offset'Range loop
            Offset (J) := Total + 1;

            if Self.Part (J).Is_Non_Terminal_Reference then
               declare
                  T : constant Non_Terminal_Index := Self.Part (J).Denote;
               begin
                  First (J) := Self.Non_Terminal (T).First_Attribute;
                  Total := Total + Self.Non_Terminal (T).Last_Attribute -
                    First (J) + 1;
               end;
            else
               First (J) := 1;
            end if;
         end loop;

         declare
            Count  : Count_Array (1 .. Total) := (others => 0);
            From   : constant Rule_Index := Self.Production (P).First_Rule;
            To     : constant Rule_Count := Self.Production (P).Last_Rule;
            Index  : Attribute_Declaration_Index;
         begin
            --  Fill Count array
            for R in From .. To loop
               declare
                  Result : constant Attribute_Index := Self.Rule (R).Result;
                  Decl   : constant Attribute_Declaration_Index :=
                    Self.Attribute (Result).Declaration;
               begin
                  if Self.Declaration (Decl).Is_Local then
                     Index := LHS +
                       Decl -
                       Self.Production (P).First_Attribute + 1;
                  elsif Self.Attribute (Result).Is_Left_Hand_Side then
                     Index := Decl -
                       Self.Non_Terminal (NT).First_Attribute + 1;
                  else
                     Index := Offset (Self.Attribute (Result).Origin) +
                       Decl -
                       First (Self.Attribute (Result).Origin);
                  end if;

                  Count (Index) := Count (Index) + 1;
               end;
            end loop;

            for J in Offset'Range loop
               if Self.Part (J).Is_Non_Terminal_Reference and then
                 not Check_Count (NT,
                                  Self.Part (J).Denote,
                                  Count (Offset (J) .. Count'Last),
                                  P,
                                  J)
               then
                  Result := False;
               end if;
            end loop;

            if not Check_Count (NT, NT, Count, P, RHS => False) then
               Result := False;
            end if;

            if not Check_Local (NT, P, Count (LHS + 1 .. LHS + Local)) then
               Result := False;
            end if;
         end;

         return Result;
      end Check;

      --------------
      -- Check_NT --
      --------------

      function Check_NT
        (NT   : Non_Terminal_Index;
         From : Production_Index;
         To   : Production_Count)
        return Boolean
      is
         Result : Boolean := True;
      begin
         for P in From .. To loop
            if not Check (NT, P) then
               Result := False;
            end if;
         end loop;

         return Result;
      end Check_NT;

      Result : Boolean := True;
   begin
      for NT in 1 .. Self.Last_Non_Terminal loop
         if not Check_NT
           (NT, Self.Non_Terminal (NT).First, Self.Non_Terminal (NT).Last)
         then
            Result := False;
         end if;
      end loop;

      return Result;
   end Is_Well_Formed;

end Anagram.Grammars_Checks;