anagram_1.0.0_49233f56/sources/anagram-grammars-constructors.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
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 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
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 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
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
--  Copyright (c) 2010-2017 Maxim Reznik <reznikmm@gmail.com>
--
--  SPDX-License-Identifier: MIT
--  License-Filename: LICENSE
-------------------------------------------------------------

with Anagram.Grammars.Rule_Templates;
with Ada.Wide_Wide_Text_IO;
with Ada.Unchecked_Deallocation;


package body Anagram.Grammars.Constructors is

   procedure Fill_Attr_Declarations
     (Self   : in out Constructor'Class;
      Result : in out Grammar;
      Parent : S.Universal_String;
      Attr   : in out Attribute_Declaration_Maps.Map);
   --  Fill Result with attribute declarations from Attr
   --  Collect attribute declaration indexes in Denotes using Parent name.

   procedure Fill_Terminals
     (Self    : in out Constructor'Class;
      Result  : in out Grammar);
   --  Fill Result with terminals from Self. Add their attribute declarations.
   --  Collect terminal indexes in Denotes

   procedure Fill_Non_Terminals
     (Self    : in out Constructor'Class;
      Result  : in out Grammar);
   --     Fill Result with non-terminals from Self.
   --     Add their attribute declarations.
   --     Collect terminal indexes in Denotes

   procedure Fill_Productions
     (Self    : in out Constructor'Class;
      Result  : in out Grammar);
   --  Fill Result with non-terminals from Self. Add their parts and rules.
   --  Collect terminal indexes in Denotes

   procedure Fill_Rule
     (Self   : in out Constructor'Class;
      Result : in out Grammar;
      Item   : Rule_Data);

   procedure Fill_Attr
     (Result  : in out Grammar;
      Index   : Attribute_Index;
      Attr    : Attribute_Declaration;
      LHS     : Boolean;
      Default : Boolean;
      Part    : Part_Access;
      Rule    : Rule_Index);

   procedure Join
     (Left  : in out Reference_Maps.Map;
      Right : Reference_Maps.Map);

   procedure Fill_Part
     (Self   : in out Constructor'Class;
      Result : in out Grammar;
      Item   : Part_Access;
      Parent : Production_Index);

   procedure Fill_Production_List
     (Self   : in out Constructor'Class;
      Result : in out Grammar;
      List   : Production_List_Access;
      Parent : Non_Terminal_Count);

   function Get_Production
     (Self         : in out Constructor'Class;
      Non_Terminal : S.Universal_String;
      Production   : S.Universal_String) return Production_Access;

   procedure Free is new Ada.Unchecked_Deallocation
     (Production_List_Node, Production_List_Access);

   procedure Free is new Ada.Unchecked_Deallocation
     (Production_Node, Production_Access);

   procedure Free is new Ada.Unchecked_Deallocation (Part_Node, Part_Access);

   ---------
   -- Add --
   ---------

   procedure Add
     (Self : in out Production;
      Item : Part'Class) is
   begin
      Self.Data.Parts.Append (Item.Data);

      case Item.Data.Kind is
         when Terminal_Reference =>
            if not Item.Data.Name.Is_Empty then
               if Self.Data.References.Contains (Item.Data.Name) then
                  Ada.Wide_Wide_Text_IO.Put_Line
                    ("Duplicated token name: " &
                       Item.Data.Name.To_Wide_Wide_String);
                  Ada.Wide_Wide_Text_IO.Put_Line ("in production name: " &
                       Self.Data.Name.To_Wide_Wide_String);
               end if;

               Self.Data.References.Insert (Item.Data.Name, Item.Data);
            end if;

            Self.Data.Parts_Count := Self.Data.Parts_Count + 1;
         when Non_Terminal_Reference | List =>
            if not Item.Data.Name.Is_Empty then
               if Self.Data.References.Contains (Item.Data.Name) then
                  Ada.Wide_Wide_Text_IO.Put_Line
                    ("Duplicated part name: " &
                       Item.Data.Name.To_Wide_Wide_String);
               end if;

               Self.Data.References.Insert (Item.Data.Name, Item.Data);
            end if;

            Self.Data.Parts_Count := Self.Data.Parts_Count + 1;
         when Option =>
            Self.Data.Prods_Count := Self.Data.Prods_Count
              + Item.Data.List.Prods_Count;
            Self.Data.Parts_Count := Self.Data.Parts_Count
              + Item.Data.List.Parts_Count + 1;

            Join (Self.Data.References, Item.Data.Refs);
      end case;
   end Add;

   ---------
   -- Add --
   ---------

   procedure Add
     (List : in out Production_List;
      Item : Production'Class) is
   begin
      if not Item.Data.Name.Is_Empty and then
        List.Data.Productions.Find_Index (Item.Data) /= 0
      then
         raise Constraint_Error;
      end if;

      List.Data.Productions.Append (Item.Data);
      List.Data.Attr_Count  := List.Data.Attr_Count  + Item.Data.Attr_Count;
      List.Data.Prods_Count := List.Data.Prods_Count + Item.Data.Prods_Count;
      List.Data.Parts_Count := List.Data.Parts_Count + Item.Data.Parts_Count;
   end Add;

   --------------
   -- Complete --
   --------------

   function Complete (Self : in out Constructor) return Grammar is
      procedure Destroy (List : in out Production_List_Access);

      -------------
      -- Destroy --
      -------------

      procedure Destroy (List : in out Production_List_Access) is
      begin
         for X of List.Productions loop
            for Y of X.Parts loop
               if Y.Kind = Option then
                  Destroy (Y.List);
               end if;
               Free (Y);
            end loop;

            Free (X);
         end loop;

         Free (List);
      end Destroy;

      Last_Terminal     : constant Terminal_Count :=
        Terminal_Count (Self.Terminals.Length);
      Last_Non_Terminal : constant Non_Terminal_Count :=
        Non_Terminal_Count (Self.Non_Terminals.Length);
   begin
      return Result : Grammar (Last_Terminal,
                               Last_Non_Terminal,
                               Self.Last_Production,
                               Self.Last_Part,
                               Self.Last_Declaration,
                               Self.Last_Attribute,
                               Self.Last_Rule)
      do
         declare
         begin
            Self.Last_Production := 0;
            Self.Last_Part := 0;
            Self.Last_Declaration := 0;
            Self.Last_Attribute := 0;
            Self.Last_Rule := 0;

            Fill_Terminals (Self, Result);
            Fill_Non_Terminals (Self, Result);
            Fill_Productions (Self, Result);
            Result.Root := 1;

            for Item of Self.Non_Terminals loop
               Destroy (Item.List);
            end loop;
         end;
      end return;
   end Complete;

   ----------------------------------
   -- Create_Attribute_Declaration --
   ----------------------------------

   procedure Create_Attribute_Declaration
     (Self         : in out Constructor;
      Terminal     : S.Universal_String;
      Name         : S.Universal_String;
      Type_Name    : S.Universal_String)
   is
      Next : constant Attribute_Declaration :=
        (999, Name, Synthesized, Type_Name);
   begin
      if Self.Terminals (Terminal).Attr.Contains (Name) then
         raise Constraint_Error;
      end if;

      Self.Terminals (Terminal).Attr.Insert (Name, Next);
      Self.Last_Declaration := Self.Last_Declaration + 1;
   end Create_Attribute_Declaration;

   ----------------------------------
   -- Create_Attribute_Declaration --
   ----------------------------------

   procedure Create_Attribute_Declaration
     (Self         : in out Constructor;
      Non_Terminal : S.Universal_String;
      Name         : S.Universal_String;
      Is_Inherited : Boolean;
      Type_Name    : S.Universal_String)
   is
      Map  : constant array (Boolean) of Attribute_Kind :=
        (True => Inherited, False => Synthesized);
      Next : constant Attribute_Declaration :=
        (999, Name, Map (Is_Inherited), Type_Name);
   begin
      if Self.Non_Terminals (Non_Terminal).Attr.Contains (Name) then
         raise Constraint_Error;
      end if;

      Self.Non_Terminals (Non_Terminal).Attr.Insert (Name, Next);
      Self.Last_Declaration := Self.Last_Declaration + 1;
   end Create_Attribute_Declaration;

   -----------------
   -- Create_List --
   -----------------

   procedure Create_List
     (Self : in out Constructor;
      Name : S.Universal_String;
      List : in out Production_List'Class)
   is
      Next : constant Non_Terminal :=
        (Index       => 999,
         Is_List     => True,
         Name        => Name,
         Attr        => Attribute_Declaration_Maps.Empty_Map,
         List        => List.Data);
   begin
      Self.Non_Terminals.Insert (Name, Next);
      Self.Last_Production := Self.Last_Production + List.Data.Prods_Count;
      Self.Last_Part := Self.Last_Part + List.Data.Parts_Count;
      List.Data := null;
   end Create_List;

   ---------------------------
   -- Create_List_Reference --
   ---------------------------

   function Create_List_Reference
     (Self   : Constructor'Class;
      Name   : S.Universal_String;
      Denote : S.Universal_String)
      return Part
   is
      pragma Unreferenced (Self);

      Next : constant Part_Node :=
        (Index  => 999,
         Kind   => List,
         Name   => Name,
         Denote => Denote);
   begin
      return (Data => new Part_Node'(Next));
   end Create_List_Reference;

   ----------------------------
   -- Create_Local_Attribute --
   ----------------------------

   procedure Create_Local_Attribute
     (Self         : in out Constructor;
      Non_Terminal : S.Universal_String;
      Production   : S.Universal_String;
      Name         : S.Universal_String;
      Type_Name    : S.Universal_String)
   is
      Next : constant Attribute_Declaration := (999, Name, Local, Type_Name);
      Prod : constant Production_Access :=
        Get_Production (Self, Non_Terminal, Production);
   begin
      if Prod.Attr.Contains (Name) then
         raise Constraint_Error;
      end if;

      Prod.Attr.Insert (Name, Next);
      Self.Last_Declaration := Self.Last_Declaration + 1;
   end Create_Local_Attribute;

   -------------------------
   -- Create_Non_Terminal --
   -------------------------

   procedure Create_Non_Terminal
     (Self : in out Constructor;
      Name : S.Universal_String;
      List : in out Production_List'Class)
   is
      Next : constant Non_Terminal :=
        (Index       => 999,
         Is_List     => False,
         Name        => Name,
         Attr        => Attribute_Declaration_Maps.Empty_Map,
         List        => List.Data);
   begin
      Self.Non_Terminals.Insert (Name, Next);
      Self.Last_Attribute := Self.Last_Attribute + List.Data.Attr_Count;
      Self.Last_Production := Self.Last_Production + List.Data.Prods_Count;
      Self.Last_Part := Self.Last_Part + List.Data.Parts_Count;
      List.Data := null;
   end Create_Non_Terminal;

   -----------------------------------
   -- Create_Non_Terminal_Reference --
   -----------------------------------

   function Create_Non_Terminal_Reference
     (Self   : Constructor'Class;
      Name   : S.Universal_String;
      Denote : S.Universal_String)
      return Part
   is
      pragma Unreferenced (Self);

      Next : constant Part_Node :=
        (Index  => 999,
         Kind   => Non_Terminal_Reference,
         Name   => Name,
         Denote => Denote);
   begin
      return (Data => new Part_Node'(Next));
   end Create_Non_Terminal_Reference;

   -------------------
   -- Create_Option --
   -------------------

   function Create_Option
     (Self  : Constructor'Class;
      Name  : S.Universal_String;
      List  : in out Production_List'Class)
      return Part
   is
      pragma Unreferenced (Self);

      Refs : Reference_Maps.Map;
      X    : Production_Vectors.Vector renames List.Data.Productions;
   begin
      for J in X.First_Index .. X.Last_Index loop
         Join (Refs, X.Element (J).References);
      end loop;

      return Result : constant Part :=
        (Data => new Part_Node'
                (Index => 999,
                 Kind  => Option,
                 Name  => Name,
                 List  => List.Data,
                 Refs  => Refs))
      do
         List.Data := null;
      end return;
   end Create_Option;

   -----------------------
   -- Create_Production --
   -----------------------

   function Create_Production
     (Self   : Constructor'Class;
      Name   : S.Universal_String;
      Prec  : Precedence_Value := Undefined_Precedence)
      return Production
   is
      pragma Unreferenced (Self);
   begin
      return (Data => new Production_Node'
                (Index       => 999,
                 Name        => Name,
                 Parts       => Part_Vectors.Empty_Vector,
                 Rules       => Rule_Vectors.Empty_Vector,
                 References  => Reference_Maps.Empty_Map,
                 Attr_Count  => 0,
                 Prods_Count => 1,
                 Parts_Count => 0,
                 Precedence  => Prec,
                 Attr        => Attribute_Declaration_Maps.Empty_Map));
   end Create_Production;

   ----------------------------
   -- Create_Production_List --
   ----------------------------

   function Create_Production_List
     (Self : Constructor'Class)
      return Production_List
   is
      pragma Unreferenced (Self);
   begin
      return (Data => new Production_List_Node'
                (Productions => Production_Vectors.Empty_Vector,
                 Attr_Count  => 0,
                 Prods_Count => 0,
                 Parts_Count => 0));
   end Create_Production_List;

   -----------------
   -- Create_Rule --
   -----------------

   procedure Create_Rule
     (Self         : in out Constructor;
      Non_Terminal : S.Universal_String;
      Production   : S.Universal_String;
      Text         : S.Universal_String)
   is
      use type S.Universal_String;

      List     : Production_List_Access;
      Template : constant Rule_Templates.Rule_Template :=
        Rule_Templates.Create (Text);
   begin
      List := Self.Non_Terminals.Element (Non_Terminal).List;

      for Prod of List.Productions loop
         if Prod.Name = Production then
            Prod.Rules.Append
              ((Text         => Text,
                Non_Terminal => Non_Terminal,
                Production   => Prod));
            Self.Last_Rule := Self.Last_Rule + 1;
            Self.Last_Attribute := Self.Last_Attribute +
              Attribute_Count (Template.Count);

            return;
         end if;
      end loop;
   end Create_Rule;

   ---------------------
   -- Create_Terminal --
   ---------------------

   procedure Create_Terminal
     (Self  : in out Constructor;
      Image : S.Universal_String;
      Prec  : Precedence_Value := Undefined_Precedence)
   is
      Next : constant Terminal :=
        (Index       => 999,
         Name        => Image,
         Attr        => Attribute_Declaration_Maps.Empty_Map,
         Prec        => Prec);
   begin
      Self.Terminals.Insert (Image, Next);
   end Create_Terminal;

   -------------------------------
   -- Create_Terminal_Reference --
   -------------------------------

   function Create_Terminal_Reference
     (Self  : Constructor'Class;
      Name  : S.Universal_String;
      Image : S.Universal_String)
      return Part
   is
      pragma Unreferenced (Self);

      Next : constant Part_Node :=
        (Index => 999,
         Kind  => Terminal_Reference,
         Name  => Name,
         Image => Image);
   begin
      return (Data => new Part_Node'(Next));
   end Create_Terminal_Reference;

   ----------------
   -- Equal_Name --
   ----------------

   function Equal_Name (Left, Right : Production_Access) return Boolean is
      use type S.Universal_String;
   begin
      return Left.Name = Right.Name;
   end Equal_Name;

   ----------------------------
   -- Fill_Attr_Declarations --
   ----------------------------

   procedure Fill_Attr_Declarations
     (Self   : in out Constructor'Class;
      Result : in out Grammar;
      Parent : S.Universal_String;
      Attr   : in out Attribute_Declaration_Maps.Map)
   is
      pragma Unreferenced (Parent);
   begin
      for Item of Attr loop
         Self.Last_Declaration := Self.Last_Declaration + 1;
         Item.Index := Self.Last_Declaration;
         Result.Declaration (Self.Last_Declaration).Name := Item.Name;
         Result.Declaration (Self.Last_Declaration).Type_Name :=
           Item.Type_Name;
         Result.Declaration (Self.Last_Declaration).Index :=
           Self.Last_Declaration;
         Result.Declaration (Self.Last_Declaration).Kind := Item.Kind;
      end loop;
   end Fill_Attr_Declarations;

   ------------------------
   -- Fill_Non_Terminals --
   ------------------------

   procedure Fill_Non_Terminals
     (Self    : in out Constructor'Class;
      Result  : in out Grammar)
   is
      Last_Non_Terminal : Non_Terminal_Count := 1;
   begin
      for Item of Self.Non_Terminals loop
         Item.Index := Last_Non_Terminal;

         Result.Non_Terminal (Last_Non_Terminal).Name := Item.Name;
         Result.Non_Terminal (Last_Non_Terminal).Index :=
           Last_Non_Terminal;
         Result.Non_Terminal (Last_Non_Terminal).Is_List := Item.Is_List;
         Result.Non_Terminal (Last_Non_Terminal).First_Attribute :=
           Self.Last_Declaration + 1;
         Result.Non_Terminal (Last_Non_Terminal).Last_Attribute :=
           Self.Last_Declaration +
             Attribute_Declaration_Count (Item.Attr.Length);

         Fill_Attr_Declarations (Self, Result, Item.Name, Item.Attr);

         Last_Non_Terminal := Last_Non_Terminal + 1;
      end loop;
   end Fill_Non_Terminals;

   ---------------
   -- Fill_Part --
   ---------------

   procedure Fill_Part
     (Self   : in out Constructor'Class;
      Result : in out Grammar;
      Item   : Part_Access;
      Parent : Production_Index) is
   begin
      Self.Last_Part := Self.Last_Part + 1;

      declare
         P : Grammars.Part renames Result.Part (Self.Last_Part);
      begin
         Item.Index := Self.Last_Part;
         P.Name := Item.Name;
         P.Index := Self.Last_Part;
         P.Parent := Parent;
         P.Is_Terminal_Reference     := Item.Kind = Terminal_Reference;
         P.Is_Non_Terminal_Reference := Item.Kind = Non_Terminal_Reference;
         P.Is_List                   := Item.Kind = List;
         P.Is_Option                 := Item.Kind = Option;

         case Item.Kind is
            when Terminal_Reference =>
               if Self.Terminals.Contains (Item.Image) then
                  P.Denoted_Terminal :=
                    Self.Terminals.Element (Item.Image).Index;
                  P.Denoted_Non_Terminal := 0;
               else
                  raise Constraint_Error;
               end if;

            when Non_Terminal_Reference | List =>
               if Self.Non_Terminals.Contains (Item.Denote) then
                  P.Denoted_Non_Terminal :=
                    Self.Non_Terminals.Element (Item.Denote).Index;
                  P.Denoted_Terminal := 0;
                  P.Is_List :=
                    Self.Non_Terminals.Element (Item.Denote).Is_List;
               else
                  Ada.Wide_Wide_Text_IO.Put_Line
                    ("Not defined: " & Item.Denote.To_Wide_Wide_String);
                  raise Constraint_Error;
               end if;

            when Option =>
               null;
         end case;
      end;
   end Fill_Part;

   --------------------------
   -- Fill_Production_List --
   --------------------------

   procedure Fill_Production_List
     (Self   : in out Constructor'Class;
      Result : in out Grammar;
      List   : Production_List_Access;
      Parent : Non_Terminal_Count) is
   begin
      for P of List.Productions loop
         Self.Last_Production := Self.Last_Production + 1;
         P.Index := Self.Last_Production;

         Result.Production (Self.Last_Production).Name := P.Name;
         Result.Production (Self.Last_Production).Index :=
           Self.Last_Production;
         Result.Production (Self.Last_Production).First := Self.Last_Part + 1;
         Result.Production (Self.Last_Production).Last := Self.Last_Part +
           P.Parts.Last_Index;
         Result.Production (Self.Last_Production).Parent := Parent;
         Result.Production (Self.Last_Production).Precedence := P.Precedence;
         Result.Production (Self.Last_Production).First_Attribute :=
           Self.Last_Declaration + 1;
         Result.Production (Self.Last_Production).Last_Attribute :=
           Self.Last_Declaration +
             Attribute_Declaration_Count (P.Attr.Length);

         for Part of P.Parts loop
            Fill_Part (Self, Result, Part, Self.Last_Production);
         end loop;

         if Parent /= 0 then
            Fill_Attr_Declarations
              (Self, Result, Result.Non_Terminal (Parent).Name, P.Attr);
         end if;
      end loop;

      for P of List.Productions loop
         for Part of P.Parts loop
            if Part.Kind = Option then
               Result.Part (Part.Index).First := Self.Last_Production + 1;
               Result.Part (Part.Index).Last := Self.Last_Production
                 + Part.List.Productions.Last_Index;
               Fill_Production_List (Self, Result, Part.List, Parent => 0);
            end if;
         end loop;

         Result.Production (P.Index).First_Rule := Self.Last_Rule + 1;
         Result.Production (P.Index).Last_Rule  := Self.Last_Rule
           + P.Rules.Last_Index;

         for R of P.Rules loop
            Fill_Rule (Self, Result, R);
         end loop;
      end loop;
   end Fill_Production_List;

   ----------------------
   -- Fill_Productions --
   ----------------------

   procedure Fill_Productions
     (Self    : in out Constructor'Class;
      Result  : in out Grammar) is
   begin
      for Item of Self.Non_Terminals loop
         Result.Non_Terminal (Item.Index).First := Self.Last_Production + 1;
         Result.Non_Terminal (Item.Index).Last := Self.Last_Production
           + Item.List.Productions.Last_Index;
         Fill_Production_List (Self, Result, Item.List, Parent => Item.Index);
      end loop;
   end Fill_Productions;

   ---------------
   -- Fill_Rule --
   ---------------

   procedure Fill_Rule
     (Self   : in out Constructor'Class;
      Result : in out Grammar;
      Item   : Rule_Data)
   is
      use type S.Universal_String;
      LHS       : Boolean;
      No_Origin : Boolean;  --  Attr in form ${Attr} which means local attr
      Part      : Part_Access;
      Attr      : Attribute_Declaration;
      Name      : S.Universal_String;
      Origin    : S.Universal_String;
      Denote    : S.Universal_String;
      LHS_Attr  : Natural := 0;
      Template  : constant Rule_Templates.Rule_Template :=
        Rule_Templates.Create (Item.Text);
   begin
      Self.Last_Rule := Self.Last_Rule + 1;
      --  Reserve space for result attr
      Self.Last_Attribute := Self.Last_Attribute + 1;
      Result.Rule (Self.Last_Rule).Index := Self.Last_Rule;
      Result.Rule (Self.Last_Rule).Text := Item.Text;
      Result.Rule (Self.Last_Rule).Result := Self.Last_Attribute;
      Result.Rule (Self.Last_Rule).First_Argument := Self.Last_Attribute + 1;
      Result.Rule (Self.Last_Rule).Last_Argument := Self.Last_Attribute
        + Attribute_Count (Template.Count - 1);
      Result.Rule (Self.Last_Rule).Parent := Item.Production.Index;

      for J in 1 .. Template.Count loop
         Name   := Template.Attribute_Name (J);
         Origin := Template.Part_Name (J);

         LHS := Item.Non_Terminal = Origin;

         No_Origin := Origin.Is_Empty;

         if Origin.Is_Empty then
            Origin := Item.Production.Name;
         end if;

         if LHS then
            Denote := Item.Non_Terminal;
            Attr := Self.Non_Terminals.Element (Denote).Attr.Element (Name);
         elsif Item.Production.References.Contains (Origin) then
            Part := Item.Production.References.Element (Origin);

            case Part.Kind is
               when Terminal_Reference =>
                  Denote := Part.Image;
                  Attr :=
                    Self.Terminals.Element (Denote).Attr.Element (Name);
               when Non_Terminal_Reference | List =>
                  Denote := Part.Denote;

                  if Self.Non_Terminals.Element (Denote)
                    .Attr.Contains (Name)
                  then
                     Attr :=
                       Self.Non_Terminals.Element (Denote).Attr.Element (Name);
                  elsif Origin = Item.Production.Name then
                     Attr := Get_Production (Self, Item.Non_Terminal, Origin)
                       .Attr.Element (Name);
                  else
                     Ada.Wide_Wide_Text_IO.Put_Line
                       ("No attribute '" & Name.To_Wide_Wide_String &
                          "' in " & Denote.To_Wide_Wide_String);
                     raise Constraint_Error;
                  end if;

               when Option =>
                  raise Constraint_Error;
            end case;
         elsif Origin = Item.Production.Name then
            Attr := Get_Production (Self, Item.Non_Terminal, Origin)
              .Attr.Element (Name);
         else
            Ada.Wide_Wide_Text_IO.Put_Line
              ("Unknown origin:" & Origin.To_Wide_Wide_String);
            raise Constraint_Error;
         end if;

         if (LHS and Attr.Kind = Synthesized) or
           (No_Origin and Attr.Kind = Local) or
           (not LHS and not No_Origin and Attr.Kind = Inherited)
         then
            if LHS_Attr = 0 then
               LHS_Attr := J;
               Fill_Attr
                 (Result,
                  Result.Rule (Self.Last_Rule).Result,
                  Attr,
                  LHS,
                  Template.Has_Default (J),
                  Part,
                  Self.Last_Rule);
            else
               raise Constraint_Error;
            end if;
         else
            Self.Last_Attribute := Self.Last_Attribute + 1;
            Fill_Attr
              (Result,
               Self.Last_Attribute,
               Attr,
               LHS,
               Template.Has_Default (J),
               Part,
               Self.Last_Rule);
         end if;

      end loop;

      if LHS_Attr = 0 then
         raise Constraint_Error;
      end if;
   end Fill_Rule;

   ---------------
   -- Fill_Attr --
   ---------------

   procedure Fill_Attr
     (Result  : in out Grammar;
      Index   : Attribute_Index;
      Attr    : Attribute_Declaration;
      LHS     : Boolean;
      Default : Boolean;
      Part    : Part_Access;
      Rule    : Rule_Index) is
   begin
      Result.Attribute (Index).Index := Index;
      Result.Attribute (Index).Declaration := Attr.Index;
      Result.Attribute (Index).Has_Default := Default;
      Result.Attribute (Index).Parent := Rule;

      if LHS or Attr.Kind = Local then
         Result.Attribute (Index).Origin := 0;
      else
         Result.Attribute (Index).Origin := Part.Index;
      end if;
   end Fill_Attr;

   --------------------
   -- Fill_Terminals --
   --------------------

   procedure Fill_Terminals
     (Self   : in out Constructor'Class;
      Result : in out Grammar)
   is
      Last_Terminal : Terminal_Index := 1;
   begin
      for Item of Self.Terminals loop
         Item.Index := Last_Terminal;
         Result.Terminal (Last_Terminal).Image := Item.Name;
         Result.Terminal (Last_Terminal).Index := Last_Terminal;
         Result.Terminal (Last_Terminal).First_Attribute :=
           Self.Last_Declaration + 1;
         Result.Terminal (Last_Terminal).Last_Attribute :=
           Self.Last_Declaration +
             Attribute_Declaration_Count (Item.Attr.Length);
         Result.Terminal (Last_Terminal).Precedence := Item.Prec;

         Fill_Attr_Declarations (Self, Result, Item.Name, Item.Attr);

         Last_Terminal := Last_Terminal + 1;
      end loop;
   end Fill_Terminals;

   --------------------
   -- Get_Production --
   --------------------

   function Get_Production
     (Self         : in out Constructor'Class;
      Non_Terminal : S.Universal_String;
      Production   : S.Universal_String) return Production_Access
   is
      use type S.Universal_String;

      List : constant Production_List_Access :=
        Self.Non_Terminals.Element (Non_Terminal).List;
   begin
      for Prod of List.Productions loop
         if Prod.Name = Production then
            return Prod;
         end if;
      end loop;

      return null;
   end Get_Production;

   ----------
   -- Join --
   ----------

   procedure Join
     (Left  : in out Reference_Maps.Map;
      Right : Reference_Maps.Map)
   is
      use League.Strings;
      Pos : Reference_Maps.Cursor := Right.First;
   begin
      while Reference_Maps.Has_Element (Pos) loop
         declare
            X : constant S.Universal_String := Reference_Maps.Key (Pos);
            Y : constant Part_Access := Reference_Maps.Element (Pos);
            Z : Part_Access;
         begin
            if Left.Contains (X) then
               Z := Left.Element (X);

               if Z.Kind = Terminal_Reference and Z.Kind = Y.Kind then
                  null;
               elsif Z.Denote /= Y.Denote then
                  Ada.Wide_Wide_Text_IO.Put_Line (X.To_Wide_Wide_String);
                  raise Constraint_Error;
               end if;
            else
               Left.Insert (X, Y);
            end if;

            Reference_Maps.Next (Pos);
         end;
      end loop;
   end Join;

   --------------------
   -- Set_Precedence --
   --------------------

   procedure Set_Precedence
     (Self         : in out Constructor;
      Non_Terminal : S.Universal_String;
      Production   : S.Universal_String;
      Precedence   : Precedence_Value) is
   begin
      Get_Production (Self, Non_Terminal, Production).all.Precedence :=
        Precedence;
   end Set_Precedence;

   ------------------
   -- To_Augmented --
   ------------------

   function To_Augmented (Input : Grammar) return Grammar is
   begin
      return Result : Grammar
        (Input.Last_Terminal,
         Input.Last_Non_Terminal + 1,
         Input.Last_Production + 1,
         Input.Last_Part + 1,
         Input.Last_Declaration,
         Input.Last_Attribute,
         Input.Last_Rule)
      do
         Result.Root := Result.Last_Non_Terminal;

         Result.Terminal := Input.Terminal;

         Result.Non_Terminal (Input.Non_Terminal'Range) := Input.Non_Terminal;

         declare
            use type S.Universal_String;
            T : Grammars.Non_Terminal renames Result.Non_Terminal
              (Result.Last_Non_Terminal);
         begin
            T := (Name => Input.Non_Terminal (Input.Root).Name & "'",
                  Index => Result.Last_Non_Terminal,
                  First => Result.Last_Production,
                  Last => Result.Last_Production,
                  Is_List => False,
                  First_Attribute => 1,
                  Last_Attribute => 0);
         end;

         Result.Production (Input.Production'Range) := Input.Production;

         declare
            T : Grammars.Production renames
              Result.Production (Result.Last_Production);
         begin
            T := (Name => League.Strings.Empty_Universal_String,
                  Index => Result.Last_Production,
                  Parent => Result.Root,
                  First => Result.Last_Part,
                  Last => Result.Last_Part,
                  First_Rule => 1,
                  Last_Rule => 0,
                  Precedence => Undefined_Precedence,
                  First_Attribute => 1,
                  Last_Attribute  => 0);
         end;

         Result.Part (Input.Part'Range) := Input.Part;

         declare
            NT : Grammars.Non_Terminal renames Input.Non_Terminal (Input.Root);
            T : Grammars.Part renames Result.Part (Result.Last_Part);
         begin
            T := (Name => NT.Name,
                  Index => Result.Last_Part,
                  Parent => Result.Last_Production,
                  Is_Terminal_Reference => False,
                  Is_Non_Terminal_Reference => True,
                  Is_List => False,
                  Is_Option => False,
                  Denoted_Terminal => 0,
                  Denoted_Non_Terminal => NT.Index,
                  First => 1,
                  Last => 0);
         end;

         Result.Declaration (Input.Declaration'Range) := Input.Declaration;
         Result.Attribute (Input.Attribute'Range) := Input.Attribute;
         Result.Rule (Input.Rule'Range) := Input.Rule;
         Result.With_List := Input.With_List;
      end return;
   end To_Augmented;

end Anagram.Grammars.Constructors;