public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [Ada] parser improvements
@ 2007-08-16 13:31 Arnaud Charlet
  0 siblings, 0 replies; only message in thread
From: Arnaud Charlet @ 2007-08-16 13:31 UTC (permalink / raw)
  To: gcc-patches; +Cc: Robert Dewar

[-- Attachment #1: Type: text/plain, Size: 2038 bytes --]

Tested on i686-linux, committed on trunk

This patch improves the handling of extra right parens in the
parser, so that they are properly ignored, rather than causing
error nodes to be generated. The change interacted with handling
parenthesized range expressions, so this has also been cleaned up.

The following test program:

     1. function f (x : integer) return integer is
     2. begin
     3.     pragma Assert (x > 0);
     4.     return 3);
                    |
        >>> unexpected right parenthesis

     5. end;

Now generates this -gnatG output:

function f (x : integer) return integer is
begin
   pragma assert (x > 0);
   return 3;
end f;

This patch also improves the error recovery if a bad delta
or digits constraint is given. The two examples below are
from ACATS tests, the compilation is shown with -gnatf
(which previously generated extra cascaded error messages)

    30. PROCEDURE B95061C IS
    31.
    32.      TASK T IS
    33.
    34.           ENTRY E1 ( X : FLOAT DIGITS 6 );     -- ERROR: DIGITS.
                                       |
        >>> constraint not allowed here

    35.
    36.      END T;
    37.
    38.      TASK BODY T IS
    39.      BEGIN
    40.           NULL;
    41.      END T;
    42.
    43. BEGIN
    44.      NULL;
    45. END B95061C;

    30. PROCEDURE B95061D IS
    31.
    32.      TASK T IS
    33.
    34.           ENTRY E1 ( X : DURATION DELTA 0.01 );     -- ERROR: DELTA.
                                          |
        >>> constraint not allowed here

    35.
    36.      END T;
    37.
    38.      TASK BODY T IS
    39.      BEGIN
    40.           NULL;
    41.      END T;
    42.
    43. BEGIN
    44.      NULL;
    45. END B95061D;

2007-08-14  Robert Dewar  <dewar@adacore.com>
	    Ed Schonberg  <schonberg@adacore.com>

	* par.ads, par.adb: Improve handling of extra right parens.
	(Par): Remove flag From_Limited_With_Clause.

	* par-util.adb, par-ch3.adb: Improve error recovery for bad constraint
	Improve handling of extra right parens.


[-- Attachment #2: difs --]
[-- Type: text/plain, Size: 8450 bytes --]

Index: par.ads
===================================================================
--- par.ads	(revision 127358)
+++ par.ads	(working copy)
@@ -6,7 +6,7 @@
 --                                                                          --
 --                                 S p e c                                  --
 --                                                                          --
---          Copyright (C) 1992-2005, Free Software Foundation, Inc.         --
+--          Copyright (C) 1992-2007, Free Software Foundation, Inc.         --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -30,17 +30,14 @@
 with Types; use Types;
 
 function Par
-  (Configuration_Pragmas : Boolean;
-   From_Limited_With     : Boolean := False) return List_Id;
+  (Configuration_Pragmas : Boolean) return List_Id;
 --  Top level parsing routine. There are two cases:
 --
 --  If Configuration_Pragmas is False, Par parses a compilation unit in the
 --  current source file and sets the Cunit, Cunit_Entity and Unit_Name fields
 --  of the units table entry for Current_Source_Unit. On return the parse tree
 --  is complete, and decorated with any required implicit label declarations.
---  The value returned in this case is always No_List. If From_Limited_With is
---  True, we are parsing a compilation unit found in a limited-with clause (Ada
---  2005, AI-50217)
+--  The value returned in this case is always No_List.
 --
 --  If Configuration_Pragmas is True, Par parses a list of configuration
 --  pragmas from the current source file, and returns the list of pragmas.
Index: par.adb
===================================================================
--- par.adb	(revision 127358)
+++ par.adb	(working copy)
@@ -51,8 +51,7 @@ with Tbuild;   use Tbuild;
 ---------
 
 function Par
-  (Configuration_Pragmas : Boolean;
-   From_Limited_With     : Boolean := False) return List_Id
+  (Configuration_Pragmas : Boolean) return List_Id
 is
    Num_Library_Units : Natural := 0;
    --  Count number of units parsed (relevant only in syntax check only mode,
@@ -1042,6 +1041,10 @@ is
       --  it is returned unchanged. Otherwise an error message is issued
       --  and Error is returned.
 
+      procedure Check_No_Right_Paren;
+      --  Called to check that the current token is not a right paren. If it
+      --  is, then an error is given, and the right parenthesis is scanned out.
+
       function Comma_Present return Boolean;
       --  Used in comma delimited lists to determine if a comma is present, or
       --  can reasonably be assumed to have been present (an error message is
@@ -1089,10 +1092,6 @@ is
       --  parameter. If a constraint is present, an error message is posted,
       --  and the constraint is scanned and discarded.
 
-      function No_Right_Paren (Expr : Node_Id) return Node_Id;
-      --  Function to check for no right paren at end of expression, returns
-      --  its argument if no right paren, else flags paren and returns Error.
-
       procedure Push_Scope_Stack;
       pragma Inline (Push_Scope_Stack);
       --  Push a new entry onto the scope stack. Scope.Last (the stack pointer)
Index: par-util.adb
===================================================================
--- par-util.adb	(revision 127358)
+++ par-util.adb	(working copy)
@@ -6,7 +6,7 @@
 --                                                                          --
 --                                 B o d y                                  --
 --                                                                          --
---          Copyright (C) 1992-2004, Free Software Foundation, Inc.         --
+--          Copyright (C) 1992-2007, Free Software Foundation, Inc.         --
 --                                                                          --
 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
 -- terms of the  GNU General Public License as published  by the Free Soft- --
@@ -176,6 +176,18 @@ package body Util is
       end if;
    end Check_Misspelling_Of;
 
+   --------------------------
+   -- Check_No_Right_Paren --
+   --------------------------
+
+   procedure Check_No_Right_Paren is
+   begin
+      if Token = Tok_Right_Paren then
+         Error_Msg_SC ("unexpected right parenthesis");
+         Scan; -- past unexpected right paren
+      end if;
+   end Check_No_Right_Paren;
+
    -----------------------------
    -- Check_Simple_Expression --
    -----------------------------
@@ -587,21 +599,6 @@ package body Util is
       end if;
    end No_Constraint;
 
-   --------------------
-   -- No_Right_Paren --
-   --------------------
-
-   function No_Right_Paren (Expr : Node_Id) return Node_Id is
-   begin
-      if Token = Tok_Right_Paren then
-         Error_Msg_SC ("unexpected right parenthesis");
-         Resync_Expression;
-         return Error;
-      else
-         return Expr;
-      end if;
-   end No_Right_Paren;
-
    ---------------------
    -- Pop_Scope_Stack --
    ---------------------
Index: par-ch3.adb
===================================================================
--- par-ch3.adb	(revision 127358)
+++ par-ch3.adb	(working copy)
@@ -61,15 +61,14 @@ package body Ch3 is
       Done    : out Boolean;
       In_Spec : Boolean);
    --  Scans out a single declarative item, or, in the case of a declaration
-   --  with a list of identifiers, a list of declarations, one for each of
-   --  the identifiers in the list. The declaration or declarations scanned
-   --  are appended to the given list. Done indicates whether or not there
-   --  may be additional declarative items to scan. If Done is True, then
-   --  a decision has been made that there are no more items to scan. If
-   --  Done is False, then there may be additional declarations to scan.
-   --  In_Spec is true if we are scanning a package declaration, and is used
-   --  to generate an appropriate message if a statement is encountered in
-   --  such a context.
+   --  with a list of identifiers, a list of declarations, one for each of the
+   --  identifiers in the list. The declaration or declarations scanned are
+   --  appended to the given list. Done indicates whether or not there may be
+   --  additional declarative items to scan. If Done is True, then a decision
+   --  has been made that there are no more items to scan. If Done is False,
+   --  then there may be additional declarations to scan. In_Spec is true if
+   --  we are scanning a package declaration, and is used to generate an
+   --  appropriate message if a statement is encountered in such a context.
 
    procedure P_Identifier_Declarations
      (Decls   : List_Id;
@@ -2410,7 +2409,7 @@ package body Ch3 is
    begin
       Constraint_Node := New_Node (N_Digits_Constraint, Token_Ptr);
       Scan; -- past DIGITS
-      Expr_Node := P_Expression_No_Right_Paren;
+      Expr_Node := P_Expression;
       Check_Simple_Expression_In_Ada_83 (Expr_Node);
       Set_Digits_Expression (Constraint_Node, Expr_Node);
 
@@ -2442,7 +2441,7 @@ package body Ch3 is
    begin
       Constraint_Node := New_Node (N_Delta_Constraint, Token_Ptr);
       Scan; -- past DELTA
-      Expr_Node := P_Expression_No_Right_Paren;
+      Expr_Node := P_Expression;
       Check_Simple_Expression_In_Ada_83 (Expr_Node);
       Set_Delta_Expression (Constraint_Node, Expr_Node);
 
@@ -3505,7 +3504,8 @@ package body Ch3 is
 
          else
             begin
-               Expr_Node := No_Right_Paren (P_Expression_Or_Range_Attribute);
+               Expr_Node := P_Expression_Or_Range_Attribute;
+               Check_No_Right_Paren;
 
                if Token = Tok_Colon
                  and then Nkind (Expr_Node) = N_Identifier
@@ -3602,7 +3602,7 @@ package body Ch3 is
 
       if Abstract_Present then
          Error_Msg_SP ("ABSTRACT not allowed in interface type definition " &
-                       "('R'M' 3.9.4(2/2))");
+                       "(RM 3.9.4(2/2))");
       end if;
 
       Scan; -- past INTERFACE
@@ -3983,7 +3983,9 @@ package body Ch3 is
       Scan_State : Saved_Scan_State;
 
    begin
-      if Style_Check then Style.Check_Indentation; end if;
+      if Style_Check then
+         Style.Check_Indentation;
+      end if;
 
       case Token is
 

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2007-08-16 13:31 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-08-16 13:31 [Ada] parser improvements Arnaud Charlet

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).