Changeset 361


Ignore:
Timestamp:
Dec 26, 2014, 7:20:54 AM (6 years ago)
Author:
Maxim Reznik
Message:

Use recursion instead of loop to iterate sequences

Rewrite AG generator to use recursion. This allows to provide explicit rules
for attributes of sequence heads.
Provide explicit copy rules (auto generated earlier).

Location:
trunk/ada-2012
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/ada-2012/src/ag/env_in.ag

    r349 r361  
    308308    : Gela.Semantic_Types.Env_Index : env_in;
    309309
     310Rules for Aborted_Tasks.name :
     311(.
     312      ${head.env_in} := ${Aborted_Tasks.env_in};
     313.)
     314Rules for Accept_Body_Exception_Handlers.exception_handler :
     315(.
     316      ${head.env_in} := ${Accept_Body_Exception_Handlers.env_in};
     317.)
     318Rules for Accept_Body_Statements.statement :
     319(.
     320      ${head.env_in} := ${Accept_Body_Statements.env_in};
     321.)
     322Rules for Accept_Parameters.parameter_specification :
     323(.
     324      ${head.env_in} := ${Accept_Parameters.env_in};
     325.)
     326Rules for Access_To_Subprogram_Parameter_Profile.parameter_specification :
     327(.
     328      ${head.env_in} := ${Access_To_Subprogram_Parameter_Profile.env_in};
     329.)
     330Rules for Array_Component_Choices.discrete_choice :
     331(.
     332      ${head.env_in} := ${Array_Component_Choices.env_in};
     333.)
     334Rules for Aspect_Specifications.aspect_specification :
     335(.
     336      ${head.env_in} := ${Aspect_Specifications.env_in};
     337.)
     338Rules for Associations.association :
     339(.
     340      ${head.env_in} := ${Associations.env_in};
     341.)
     342Rules for Asynchronous_Statement_Paths.select_then_abort_path :
     343(.
     344      ${head.env_in} := ${Asynchronous_Statement_Paths.env_in};
     345.)
     346Rules for Block_Declarative_Items.declarative_item :
     347(.
     348      ${head.env_in} := ${Block_Declarative_Items.env_in};
     349.)
     350Rules for Block_Exception_Handlers.exception_handler :
     351(.
     352      ${head.env_in} := ${Block_Exception_Handlers.env_in};
     353.)
     354Rules for Block_Statements.statement :
     355(.
     356      ${head.env_in} := ${Block_Statements.env_in};
     357.)
     358Rules for Body_Declarative_Items.declarative_item :
     359(.
     360      ${head.env_in} := ${Body_Declarative_Items.env_in};
     361.)
     362Rules for Body_Exception_Handlers.exception_handler :
     363(.
     364      ${head.env_in} := ${Body_Exception_Handlers.env_in};
     365.)
     366Rules for Body_Statements.statement :
     367(.
     368      ${head.env_in} := ${Body_Statements.env_in};
     369.)
     370Rules for Case_Expression_Paths.case_expression_path :
     371(.
     372      ${head.env_in} := ${Case_Expression_Paths.env_in};
     373.)
     374Rules for Case_Path_Alternative_Choices.discrete_choice :
     375(.
     376      ${head.env_in} := ${Case_Path_Alternative_Choices.env_in};
     377.)
     378Rules for Case_Statement_Alternative_Choices.discrete_choice :
     379(.
     380      ${head.env_in} := ${Case_Statement_Alternative_Choices.env_in};
     381.)
     382Rules for Case_Statement_Paths.case_path :
     383(.
     384      ${head.env_in} := ${Case_Statement_Paths.env_in};
     385.)
     386Rules for Clause_Names.program_unit_name :
     387(.
     388      ${head.env_in} := ${Clause_Names.env_in};
     389.)
     390Rules for Compilation_Pragmas.context_item :
     391(.
     392      ${head.env_in} := ${Compilation_Pragmas.env_in};
     393.)
     394Rules for Component_Clauses.clause_or_pragma :
     395(.
     396      ${head.env_in} := ${Component_Clauses.env_in};
     397.)
     398Rules for Context_Clause_Elements.context_item :
     399(.
     400      ${head.env_in} := ${Context_Clause_Elements.env_in};
     401.)
     402Rules for Discrete_Subtype_Definitions.discrete_subtype_definition :
     403(.
     404      ${head.env_in} := ${Discrete_Subtype_Definitions.env_in};
     405.)
     406Rules for Discriminants.discriminant_specification :
     407(.
     408      ${head.env_in} := ${Discriminants.env_in};
     409.)
     410Rules for Enumeration_Literal_Declarations.enumeration_literal_specification :
     411(.
     412      ${head.env_in} := ${Enumeration_Literal_Declarations.env_in};
     413.)
     414Rules for Exception_Choices.exception_choice :
     415(.
     416      ${head.env_in} := ${Exception_Choices.env_in};
     417.)
     418Rules for Expression_Paths.if_else_expression_path :
     419(.
     420      ${head.env_in} := ${Expression_Paths.env_in};
     421.)
     422Rules for Extended_Return_Exception_Handlers.exception_handler :
     423(.
     424      ${head.env_in} := ${Extended_Return_Exception_Handlers.env_in};
     425.)
     426Rules for Extended_Return_Statements.statement :
     427(.
     428      ${head.env_in} := ${Extended_Return_Statements.env_in};
     429.)
     430Rules for Generic_Actual_Part.generic_association :
     431(.
     432      ${head.env_in} := ${Generic_Actual_Part.env_in};
     433.)
     434Rules for Generic_Formal_Part.generic_formal :
     435(.
     436      ${head.env_in} := ${Generic_Formal_Part.env_in};
     437.)
     438Rules for Handler_Statements.statement :
     439(.
     440      ${head.env_in} := ${Handler_Statements.env_in};
     441.)
     442Rules for Index_Subtype_Definitions.subtype_mark :
     443(.
     444      ${head.env_in} := ${Index_Subtype_Definitions.env_in};
     445.)
     446Rules for Label_Names.defining_identifier :
     447(.
     448      ${head.env_in} := ${Label_Names.env_in};
     449.)
     450Rules for Loop_Statements.statement :
     451(.
     452      ${head.env_in} := ${Loop_Statements.env_in};
     453.)
     454Rules for Membership_Test_Choices.membership_choice :
     455(.
     456      ${head.env_in} := ${Membership_Test_Choices.env_in};
     457.)
     458Rules for Names.defining_identifier :
     459(.
     460      ${head.env_in} := ${Names.env_in};
     461.)
     462Rules for Parameter_Profile.parameter_specification :
     463(.
     464      ${head.env_in} := ${Parameter_Profile.env_in};
     465.)
     466Rules for Pragma_Argument_Associations.pragma_argument_association :
     467(.
     468      ${head.env_in} := ${Pragma_Argument_Associations.env_in};
     469.)
     470Rules for Private_Part_Declarative_Items.basic_declarative_item :
     471(.
     472      ${head.env_in} := ${Private_Part_Declarative_Items.env_in};
     473.)
     474Rules for Private_Protected_Items.protected_element_declaration :
     475(.
     476      ${head.env_in} := ${Private_Protected_Items.env_in};
     477.)
     478Rules for Private_Task_Items.task_item :
     479(.
     480      ${head.env_in} := ${Private_Task_Items.env_in};
     481.)
     482Rules for Progenitor_List.subtype_mark :
     483(.
     484      ${head.env_in} := ${Progenitor_List.env_in};
     485.)
     486Rules for Protected_Operation_Items.protected_operation_item :
     487(.
     488      ${head.env_in} := ${Protected_Operation_Items.env_in};
     489.)
     490Rules for Record_Component_Associations.association :
     491(.
     492      ${head.env_in} := ${Record_Component_Associations.env_in};
     493.)
     494Rules for Record_Components.component_item :
     495(.
     496      ${head.env_in} := ${Record_Components.env_in};
     497.)
     498Rules for Selective_Statement_Paths.select_or_else_path :
     499(.
     500      ${head.env_in} := ${Selective_Statement_Paths.env_in};
     501.)
     502Rules for Sequence_Of_Statements.statement :
     503(.
     504      ${head.env_in} := ${Sequence_Of_Statements.env_in};
     505.)
     506Rules for Statement_Paths.if_elsif_else_path :
     507(.
     508      ${head.env_in} := ${Statement_Paths.env_in};
     509.)
     510Rules for Type_Clause_Names.subtype_mark :
     511(.
     512      ${head.env_in} := ${Type_Clause_Names.env_in};
     513.)
     514Rules for Variant_Choices.discrete_choice :
     515(.
     516      ${head.env_in} := ${Variant_Choices.env_in};
     517.)
     518Rules for Variants.variant :
     519(.
     520      ${head.env_in} := ${Variants.env_in};
     521.)
     522Rules for Visible_Part_Declarative_Items.basic_declarative_item :
     523(.
     524      ${head.env_in} := ${Visible_Part_Declarative_Items.env_in};
     525.)
     526Rules for Visible_Protected_Items.protected_operation_declaration :
     527(.
     528      ${head.env_in} := ${Visible_Protected_Items.env_in};
     529.)
     530Rules for Visible_Task_Items.task_item :
     531(.
     532      ${head.env_in} := ${Visible_Task_Items.env_in};
     533.)
     534Rules for With_Clause_Names.program_unit_name :
     535(.
     536      ${head.env_in} := ${With_Clause_Names.env_in};
     537.)
    310538
    311539Rules for Aborted_Tasks.name :
  • trunk/ada-2012/tools/ag/ag_tools-check_ordered.adb

    r260 r361  
    8787               when Gela.Grammars.Ordered.Evaluate_Rule =>
    8888                  Ada.Text_IO.Put_Line (Item.Rule'Img);
     89                  Ada.Text_IO.Put_Line
     90                    (Result.Rule (Item.Rule).Text.To_UTF_8_String);
    8991               when Gela.Grammars.Ordered.Descent =>
    9092                  Ada.Text_IO.Put
  • trunk/ada-2012/tools/ag/ag_tools-input.adb

    r202 r361  
    165165                        use type Gela.Grammars.Part_Count;
    166166                        R : Gela.Grammars.Rule renames G.Rule (Y);
    167                         A : Gela.Grammars.Attribute renames
    168                           G.Attribute (R.Result);
    169167                     begin
    170168                        V.Create_Rule (N.Name, S.Name, R.Text);
    171169
    172170                        if not Is_Concrete (N.Index) then
    173                            raise Constraint_Error;
    174                         elsif N.Is_List and A.Origin = S.First then
    175                            --  Rule for eval head.inherited is not supported
    176171                           raise Constraint_Error;
    177172                        end if;
     
    246241      Parent : Gela.Grammars.Non_Terminal_Index)
    247242   is
    248       NT : Gela.Grammars.Non_Terminal renames G.Non_Terminal (Parent);
    249    begin
    250       for J in NT.First_Attribute .. NT.Last_Attribute loop
    251          declare
    252             A : Gela.Grammars.Attribute_Declaration renames G.Declaration (J);
    253             Text : League.Strings.Universal_String;
    254          begin
    255             if A.Is_Inherited then
    256                Text.Append ("         ${head.");
    257                Text.Append (A.Name);
    258                Text.Append ("} := ${");
    259                Text.Append (NT.Name);
    260                Text.Append (".");
    261                Text.Append (A.Name);
    262                Text.Append ("};");
    263                Text.Append (Wide_Wide_Character'Val (10));
    264 
    265                V.Create_Rule
    266                  (NT.Name,
    267                   G.Production (NT.First).Name,
    268                   Text);
    269             end if;
    270          end;
    271       end loop;
     243   begin
     244      null;
    272245   end Copy_Head_Attr;
    273246
  • trunk/ada-2012/tools/ag/ag_tools-nt_generators.adb

    r291 r361  
    223223
    224224      Spec.P ("", Impl);
    225       Impl.N ("   --  ");
    226       Impl.N (Pass);
    227       Impl.P;
    228225      Spec.N ("   ", Impl);
    229226      Write_Declaration (Self.Context, NT, Pass);
     
    238235            Spec.N (" : ", Impl);
    239236
     237            Code.P (";");
     238            Code.N ("      ");
     239            Code.N (This);
     240            Code.N ("_");
     241            Code.N (To_Ada (G.Declaration (J).Name));
     242            Code.N (" : ");
     243
    240244            if not G.Declaration (J).Is_Inherited then
    241245               Spec.N ("out ", Impl);
     246               Code.N ("out ");
    242247            end if;
    243248
    244249            Spec.N (To_Ada (G.Declaration (J).Type_Name), Impl);
     250            Code.N (To_Ada (G.Declaration (J).Type_Name));
     251
    245252            Self.Context.Add_With
    246253              (Package_Name (To_Ada (G.Declaration (J).Type_Name)),
     
    254261      Impl.P ("   is");
    255262
     263      Code.P (")");
     264
     265      Impl.P ("      procedure Descent");
     266      Impl.N ("      (This : in out ");
     267      Impl.N (RT);
     268      Impl.N ("_Cursor");
     269      Impl.N (Code);
     270      Code.Clear;
     271      Impl.P ("      is");
     272
     273      Self.Context.Factory.Get (G.Part (Prod.Last)).
     274         Make_Local_Variable (Prod.Last);
     275
     276      Write_Rules (Self.Context, NT, NT.First, Pass, Pos);
     277      Impl.P ("      begin");
     278      Impl.P ("         if not This.Has_Element then");
     279      Piece := Code;
     280      Code.Clear;
     281
     282      Pos := Order.Ceiling ((NT.Last, Pass, Step => 1));
     283      Write_Rules (Self.Context, NT, NT.Last, Pass, Pos);
     284      Impl.N (Code);
     285      Code.Clear;
     286      Impl.P ("            return;");
     287      Impl.P ("         end if;");
     288
     289      Impl.N ("         ");
     290      Impl.N (Item);
     291      Impl.P (" := This.Element;");
     292      Impl.P ("         This.Next;");
     293
     294      Impl.N (Piece);
     295
     296      Impl.P ("      end Descent;");
     297      Impl.P;
    256298      Impl.N ("      This : ");
    257299      Impl.N (RT);
    258300      Impl.P ("_Cursor :=");
    259301      Impl.P ("        Node.First;");
    260 
    261       Self.Context.Factory.Get (G.Part (Prod.Last)).
    262          Make_Local_Variable (Prod.Last);
    263 
    264       Code.P ("      while This.Has_Element loop");
    265       Code.N ("      ");
    266       Code.N (Item);
    267       Code.P (" := This.Element;");
    268 
    269       Write_Rules (Self.Context, NT, NT.First, Pass, Pos);
    270 
    271       Code.P ("         This.Next;");
    272       Code.P ("      end loop;");
    273       Piece := Code;
    274       Code.Clear;
    275       Pos := Order.Ceiling ((NT.Last, Pass, Step => 1));
    276       Write_Rules (Self.Context, NT, NT.Last, Pass, Pos);
    277302      Impl.P ("   begin");
    278       Impl.N (Code);
    279 
    280       Impl.P ("      if not This.Has_Element then");
    281       Impl.P ("         return;");
    282       Impl.P ("      end if;");
    283 
    284       Impl.N (Piece);
     303      Impl.N ("      Descent (This");
     304
     305      for J in NT.First_Attribute .. NT.Last_Attribute loop
     306         if Gela.Grammars.Ordered.To_Pass (Parts, J) = Pass then
     307            Impl.N (", ");
     308            Impl.N (This);
     309            Impl.N ("_");
     310            Impl.N (To_Ada (G.Declaration (J).Name));
     311         end if;
     312      end loop;
     313
     314      Impl.P (");");
     315
    285316      Impl.N ("   end ");
    286317      Impl.N (To_Ada (NT.Name));
  • trunk/ada-2012/tools/ag/ag_tools-part_generators.adb

    r264 r361  
    122122      Pass : Positive)
    123123   is
    124       pragma Unreferenced (Part);
     124      use type League.Strings.Universal_String;
     125      G    : Gela.Grammars.Grammar renames Self.Context.Grammar.all;
     126      P    : Gela.Grammars.Part renames Self.Context.Grammar.Part (Part);
    125127      Code : AG_Tools.Writers.Writer renames Self.Context.Code;
    126    begin
     128      NT   : Gela.Grammars.Non_Terminal renames G.Non_Terminal (P.Denote);
     129      Parts : Gela.Grammars.Ordered.Partition_Array renames
     130        Self.Context.Partition.all (NT.First_Attribute .. NT.Last_Attribute);
     131   begin
     132--      Self.Make_Local_Variable (Part);
    127133      Code.N ("   --  Make_Descent HEAD ");
    128134      Code.N (Pass);
    129135      Code.P;
     136      Code.N ("      Descent");
     137      Code.N (" (This");
     138
     139      for J in NT.First_Attribute .. NT.Last_Attribute loop
     140         if Gela.Grammars.Ordered.To_Pass (Parts, J) = Pass then
     141            Self.Make_Local_Variable
     142              (P.Name, G.Declaration (J));
     143
     144            Code.N (", ");
     145            Code.N (To_Ada (P.Name));
     146            Code.N ("_");
     147            Code.N (To_Ada (G.Declaration (J).Name));
     148         end if;
     149      end loop;
     150
     151      Code.P (");");
    130152   end Make_Descent;
    131153
     
    182204        G.Declaration (Attribute.Declaration);
    183205   begin
    184       Code.N ("      ");
    185       Code.N ("Head_");
    186       Code.N (To_Ada (D.Name));
    187       Code.N (" := This_");
    188       Code.N (To_Ada (D.Name));
    189       Code.P (";");
    190 
    191206      Self.Make_Local_Variable (Head, D);
     207      Code.P ("         --  Make_Get HEAD");
    192208   end Make_Get;
    193209
  • trunk/ada-2012/tools/ag/ag_tools-writers.adb

    r225 r361  
    7070   procedure N
    7171     (Self  : in out Writer;
    72       Value : Writer'Class) is
     72      Value : Writer'Class)
     73   is
     74      Text : constant League.Strings.Universal_String := Value.Text;
     75      List : constant League.String_Vectors.Universal_String_Vector :=
     76        Text.Split (New_Line);
    7377   begin
    74       Self.Text.Append (Value.Text);
     78      for J in 1 .. List.Length loop
     79         Self.P (List.Element (J));
     80      end loop;
    7581   end N;
    7682
     
    95101   procedure P
    96102     (Self   : in out Writer;
    97       Text   : League.Strings.Universal_String)
    98    is
    99       LF : constant Wide_Wide_Character := Wide_Wide_Character'Val (10);
     103      Text   : League.Strings.Universal_String) is
    100104   begin
    101       if Text.Index (LF) > 0 then
     105      if Text.Index (New_Line) > 0 then
    102106         declare
    103107            List : League.String_Vectors.Universal_String_Vector;
    104108         begin
    105             List := Text.Split (LF);
     109            List := Text.Split (New_Line);
    106110            for J in 1 .. List.Length loop
    107111               Self.P (List.Element (J));
Note: See TracChangeset for help on using the changeset viewer.