[Ada] Ada2020: matching parentheses and brackets
authorBob Duff <duff@adacore.com>
Thu, 2 Jul 2020 22:16:57 +0000 (18:16 -0400)
committerPierre-Marie de Rodat <derodat@adacore.com>
Mon, 19 Oct 2020 09:53:36 +0000 (05:53 -0400)
gcc/ada/

* par-ch4.adb (P_Aggregate_Or_Paren_Expr): Require matching
parens or brackets.
* par.adb, par-tchk.adb (T_Right_Bracket): New procedure to give
an error on missing ].

gcc/ada/par-ch4.adb
gcc/ada/par-tchk.adb
gcc/ada/par.adb

index cc5ed032bbbdc1b23a9a32aab3091c01c16d7b34..649c88ec2830f124a1067bb4719a2c13f44e993e 100644 (file)
@@ -1391,6 +1391,9 @@ package body Ch4 is
          return Maybe;
       end Is_Quantified_Expression;
 
+      Start_Token : constant Token_Type := Token;
+      --  Used to prevent mismatches (...] and [...)
+
    --  Start of processing for P_Aggregate_Or_Paren_Expr
 
    begin
@@ -1697,23 +1700,26 @@ package body Ch4 is
          end if;
       end loop;
 
-      --  All component associations (positional and named) have been scanned
+      --  All component associations (positional and named) have been scanned.
+      --  Scan ] or ) based on Start_Token.
 
-      if Token = Tok_Right_Bracket and then Ada_Version >= Ada_2020 then
-         Set_Component_Associations (Aggregate_Node, Assoc_List);
-         Set_Is_Homogeneous_Aggregate (Aggregate_Node);
-         Scan;  --  past right bracket
+      case Start_Token is
+         when Tok_Left_Bracket =>
+            Set_Component_Associations (Aggregate_Node, Assoc_List);
+            Set_Is_Homogeneous_Aggregate (Aggregate_Node);
+            T_Right_Bracket;
 
-         if Token = Tok_Apostrophe then
-            Scan;
+            if Token = Tok_Apostrophe then
+               Scan;
 
-            if Token = Tok_Identifier then
-               return P_Reduction_Attribute_Reference (Aggregate_Node);
+               if Token = Tok_Identifier then
+                  return P_Reduction_Attribute_Reference (Aggregate_Node);
+               end if;
             end if;
-         end if;
-      else
-         T_Right_Paren;
-      end if;
+         when Tok_Left_Paren =>
+            T_Right_Paren;
+         when others => raise Program_Error;
+      end case;
 
       if Nkind (Aggregate_Node) /= N_Delta_Aggregate then
          Set_Expressions (Aggregate_Node, Expr_List);
index 8eb705eeb45359592fa45fbe88f41981db8e8434..65ff45aa0e6fcf6262ca90ba46bd59ade45db8f4 100644 (file)
@@ -402,6 +402,20 @@ package body Tchk is
       Check_Token (Tok_Record, AP);
    end T_Record;
 
+   ---------------------
+   -- T_Right_Bracket --
+   ---------------------
+
+   procedure T_Right_Bracket is
+   begin
+      if Token = Tok_Right_Bracket then
+         Scan;
+      else
+         Error_Msg_AP -- CODEFIX
+           ("|missing ""']'""");
+      end if;
+   end T_Right_Bracket;
+
    -------------------
    -- T_Right_Paren --
    -------------------
index 1dee1e7dfaefbfbf8e63d28ecde8de187e321f8b..69da69d07be2803c25402cfcc957c64d57ba06a1 100644 (file)
@@ -1212,6 +1212,7 @@ function Par (Configuration_Pragmas : Boolean) return List_Id is
       procedure T_Private;
       procedure T_Range;
       procedure T_Record;
+      procedure T_Right_Bracket;
       procedure T_Right_Paren;
       procedure T_Semicolon;
       procedure T_Then;