OSDN Git Service

2006-10-31 Ed Schonberg <schonberg@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-regpat.adb
index 20001bc..6bfc2d9 100644 (file)
@@ -7,7 +7,7 @@
 --                                 B o d y                                  --
 --                                                                          --
 --               Copyright (C) 1986 by University of Toronto.               --
---           Copyright (C) 1996-2003 Ada Core Technologies, Inc.            --
+--                      Copyright (C) 1999-2006, AdaCore                    --
 --                                                                          --
 -- 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- --
@@ -17,8 +17,8 @@
 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
 -- for  more details.  You should have  received  a copy of the GNU General --
 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
--- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
--- MA 02111-1307, USA.                                                      --
+-- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
+-- Boston, MA 02110-1301, USA.                                              --
 --                                                                          --
 -- As a special exception,  if other files  instantiate  generics from this --
 -- unit, or you link  this unit with other files  to produce an executable, --
@@ -129,7 +129,7 @@ package body GNAT.Regpat is
       --  Complex loops
 
       CURLYX,     -- 2num node Match this complex thing {n,m} times
-      --                       The nums are coded on two characters each.
+      --                       The nums are coded on two characters each
 
       WHILEM,     -- no        Do curly processing and see if rest matches
 
@@ -233,16 +233,15 @@ package body GNAT.Regpat is
    procedure Set_In_Class
      (Bitmap : in out Character_Class;
       C      : Character);
-   --  Set the entry to True for C in the class Bitmap.
+   --  Set the entry to True for C in the class Bitmap
 
    function Get_From_Class
      (Bitmap : Character_Class;
-      C      : Character)
-      return   Boolean;
-   --  Return True if the entry is set for C in the class Bitmap.
+      C      : Character) return Boolean;
+   --  Return True if the entry is set for C in the class Bitmap
 
    procedure Reset_Class (Bitmap : out Character_Class);
-   --  Clear all the entries in the class Bitmap.
+   --  Clear all the entries in the class Bitmap
 
    pragma Inline (Set_In_Class);
    pragma Inline (Get_From_Class);
@@ -268,8 +267,7 @@ package body GNAT.Regpat is
 
    function String_Length
      (Program : Program_Data;
-      P       : Pointer)
-      return    Program_Size;
+      P       : Pointer) return Program_Size;
    --  Return the length of the string argument of the node at P
 
    function String_Operand (P : Pointer) return Pointer;
@@ -283,14 +281,12 @@ package body GNAT.Regpat is
 
    function Get_Next_Offset
      (Program : Program_Data;
-      IP      : Pointer)
-      return    Pointer;
-   --  Get the offset field of a node. Used by Get_Next.
+      IP      : Pointer) return Pointer;
+   --  Get the offset field of a node. Used by Get_Next
 
    function Get_Next
      (Program : Program_Data;
-      IP      : Pointer)
-      return    Pointer;
+      IP      : Pointer) return Pointer;
    --  Dig the next instruction pointer out of a node
 
    procedure Optimize (Self : in out Pattern_Matcher);
@@ -298,9 +294,8 @@ package body GNAT.Regpat is
 
    function Read_Natural
      (Program : Program_Data;
-      IP      : Pointer)
-      return    Natural;
-   --  Return the 2-byte natural coded at position IP.
+      IP      : Pointer) return Natural;
+   --  Return the 2-byte natural coded at position IP
 
    --  All of the subprograms above are tiny and should be inlined
 
@@ -394,10 +389,10 @@ package body GNAT.Regpat is
       --  Return value is the location of new opcode, ie old Emit_Ptr.
 
       procedure Emit_Natural (IP : Pointer; N : Natural);
-      --  Split N on two characters at position IP.
+      --  Split N on two characters at position IP
 
       procedure Emit_Class (Bitmap : Character_Class);
-      --  Emits a character class.
+      --  Emits a character class
 
       procedure Case_Emit (C : Character);
       --  Emit C, after converting is to lower-case if the regular
@@ -459,7 +454,7 @@ package body GNAT.Regpat is
       function  Next_Instruction (P : Pointer) return Pointer;
       --  Dig the "next" pointer out of a node
 
-      procedure Fail (M : in String);
+      procedure Fail (M : String);
       pragma No_Return (Fail);
       --  Fail with a diagnostic message, if possible
 
@@ -577,9 +572,9 @@ package body GNAT.Regpat is
       -- Fail --
       ----------
 
-      procedure Fail (M : in String) is
+      procedure Fail (M : String) is
       begin
-         raise Expression_Error;
+         raise Expression_Error with M;
       end Fail;
 
       -------------------------
@@ -850,7 +845,7 @@ package body GNAT.Regpat is
       --  makes it hard to avoid.
 
       procedure Parse
-        (Parenthesized  : in Boolean;
+        (Parenthesized  : Boolean;
          Flags          : out Expression_Flags;
          IP             : out Pointer)
       is
@@ -1211,7 +1206,7 @@ package body GNAT.Regpat is
             Parse_Pos := Parse_Pos + 1;
          end if;
 
-         --  First character can be ] or -, without closing the class.
+         --  First character can be ] or - without closing the class
 
          if Parse_Pos <= Parse_End
            and then (Expression (Parse_Pos) = ']'
@@ -2052,8 +2047,7 @@ package body GNAT.Regpat is
 
    function Compile
      (Expression : String;
-      Flags      : Regexp_Flags := No_Flags)
-      return       Pattern_Matcher
+      Flags      : Regexp_Flags := No_Flags) return Pattern_Matcher
    is
       Size  : Program_Size;
       Dummy : Pattern_Matcher (0);
@@ -2296,8 +2290,7 @@ package body GNAT.Regpat is
 
    function Get_From_Class
      (Bitmap : Character_Class;
-      C      : Character)
-      return   Boolean
+      C      : Character) return Boolean
    is
       Value : constant Class_Byte := Character'Pos (C);
 
@@ -2327,8 +2320,7 @@ package body GNAT.Regpat is
 
    function Get_Next_Offset
      (Program : Program_Data;
-      IP      : Pointer)
-      return    Pointer
+      IP      : Pointer) return Pointer
    is
    begin
       return Pointer (Read_Natural (Program, IP + 1));
@@ -2397,7 +2389,7 @@ package body GNAT.Regpat is
 
       type Natural_Array is array (Match_Count range <>) of Natural;
       Matches_Tmp : Natural_Array (Matches_Full'Range);
-      --  Save the opening position of parenthesis.
+      --  Save the opening position of parenthesis
 
       Last_Paren  : Natural := 0;
       --  Last parenthesis seen
@@ -2422,7 +2414,7 @@ package body GNAT.Regpat is
       --  operators for complex expressions.
 
       Current_Curly : Current_Curly_Access := null;
-      --  The curly currently being processed.
+      --  The curly currently being processed
 
       -----------------------
       -- Local Subprograms --
@@ -2432,14 +2424,13 @@ package body GNAT.Regpat is
       --  Find character C in Data starting at Start and return position
 
       function Repeat
-        (IP   : Pointer;
-         Max  : Natural := Natural'Last)
-         return Natural;
+        (IP  : Pointer;
+         Max : Natural := Natural'Last) return Natural;
       --  Repeatedly match something simple, report how many
       --  It only matches on things of length 1.
       --  Starting from Input_Pos, it matches at most Max CURLY.
 
-      function Try (Pos : in Positive) return Boolean;
+      function Try (Pos : Positive) return Boolean;
       --  Try to match at specific point
 
       function Match (IP : Pointer) return Boolean;
@@ -2468,14 +2459,13 @@ package body GNAT.Regpat is
         (Op     : Opcode;
          Scan   : Pointer;
          Next   : Pointer;
-         Greedy : Boolean)
-         return   Boolean;
+         Greedy : Boolean) return Boolean;
       --  Return True it the simple operator (possibly non-greedy) matches
 
       pragma Inline (Index);
       pragma Inline (Repeat);
 
-      --  These are two complex functions, but used only once.
+      --  These are two complex functions, but used only once
 
       pragma Inline (Match_Whilem);
       pragma Inline (Match_Simple_Operator);
@@ -2484,11 +2474,7 @@ package body GNAT.Regpat is
       -- Index --
       -----------
 
-      function Index
-        (Start : Positive;
-         C     : Character)
-         return  Natural
-      is
+      function Index (Start : Positive; C : Character) return Natural is
       begin
          for J in Start .. Last_In_Data loop
             if Data (J) = C then
@@ -2529,7 +2515,7 @@ package body GNAT.Regpat is
       -- Match --
       -----------
 
-      function Match (IP   : Pointer) return Boolean is
+      function Match (IP : Pointer) return Boolean is
          Scan   : Pointer := IP;
          Next   : Pointer;
          Op     : Opcode;
@@ -2563,7 +2549,7 @@ package body GNAT.Regpat is
                         end if;
 
                         Scan := Get_Next (Program, Scan);
-                        exit when Scan = 0 or Program (Scan) /= BRANCH;
+                        exit when Scan = 0 or else Program (Scan) /= BRANCH;
                      end loop;
 
                      exit State_Machine;
@@ -2835,8 +2821,7 @@ package body GNAT.Regpat is
         (Op     : Opcode;
          Scan   : Pointer;
          Next   : Pointer;
-         Greedy : Boolean)
-         return   Boolean
+         Greedy : Boolean) return Boolean
       is
          Next_Char       : Character := ASCII.Nul;
          Next_Char_Known : Boolean := False;
@@ -2893,12 +2878,14 @@ package body GNAT.Regpat is
             if Next_Char_Known then
                --  Last position to check
 
-               Last_Pos := Input_Pos + Max;
-
-               if Last_Pos > Last_In_Data
-                 or else Max = Natural'Last
-               then
+               if Max = Natural'Last then
                   Last_Pos := Last_In_Data;
+               else
+                  Last_Pos := Input_Pos + Max;
+
+                  if Last_Pos > Last_In_Data then
+                     Last_Pos := Last_In_Data;
+                  end if;
                end if;
 
                --  Look for the first possible opportunity
@@ -3012,15 +2999,15 @@ package body GNAT.Regpat is
       function Match_Whilem (IP : Pointer) return Boolean is
          pragma Unreferenced (IP);
 
-         Cc : Current_Curly_Access := Current_Curly;
-         N  : constant Natural     := Cc.Cur + 1;
-         Ln : Natural              := 0;
+         Cc : constant Current_Curly_Access := Current_Curly;
+         N  : constant Natural              := Cc.Cur + 1;
+         Ln : Natural                       := 0;
 
          Lastloc : constant Natural := Cc.Lastloc;
-         --  Detection of 0-len.
+         --  Detection of 0-len
 
       begin
-         --  If degenerate scan matches "", assume scan done.
+         --  If degenerate scan matches "", assume scan done
 
          if Input_Pos = Cc.Lastloc
            and then N >= Cc.Min
@@ -3046,7 +3033,7 @@ package body GNAT.Regpat is
             return False;
          end if;
 
-         --  First, just match a string of min scans.
+         --  First, just match a string of min scans
 
          if N < Cc.Min then
             Cc.Cur := N;
@@ -3061,7 +3048,7 @@ package body GNAT.Regpat is
             return False;
          end if;
 
-         --  Prefer next over scan for minimal matching.
+         --  Prefer next over scan for minimal matching
 
          if not Cc.Greedy then
             Current_Curly := Cc.Old_Cc;
@@ -3137,9 +3124,8 @@ package body GNAT.Regpat is
       ------------
 
       function Repeat
-        (IP   : Pointer;
-         Max  : Natural := Natural'Last)
-         return Natural
+        (IP  : Pointer;
+         Max : Natural := Natural'Last) return Natural
       is
          Scan  : Natural := Input_Pos;
          Last  : Natural;
@@ -3256,7 +3242,7 @@ package body GNAT.Regpat is
       -- Try --
       ---------
 
-      function Try (Pos : in Positive) return Boolean is
+      function Try (Pos : Positive) return Boolean is
       begin
          Input_Pos  := Pos;
          Last_Paren := 0;
@@ -3384,12 +3370,15 @@ package body GNAT.Regpat is
       return;
    end Match;
 
-   function  Match
-     (Self : Pattern_Matcher;
-      Data : String;
+   -----------
+   -- Match --
+   -----------
+
+   function Match
+     (Self       : Pattern_Matcher;
+      Data       : String;
       Data_First : Integer := -1;
-      Data_Last  : Positive := Positive'Last)
-      return Natural
+      Data_Last  : Positive := Positive'Last) return Natural
    is
       Matches : Match_Array (0 .. 0);
 
@@ -3402,12 +3391,11 @@ package body GNAT.Regpat is
       end if;
    end Match;
 
-   function  Match
+   function Match
      (Self       : Pattern_Matcher;
       Data       : String;
       Data_First : Integer  := -1;
-      Data_Last  : Positive := Positive'Last)
-     return Boolean
+      Data_Last  : Positive := Positive'Last) return Boolean
    is
       Matches : Match_Array (0 .. 0);
 
@@ -3420,9 +3408,9 @@ package body GNAT.Regpat is
      (Expression : String;
       Data       : String;
       Matches    : out Match_Array;
-      Size       : Program_Size := 0;
-      Data_First : Integer := -1;
-      Data_Last  : Positive := Positive'Last)
+      Size       : Program_Size := Auto_Size;
+      Data_First : Integer      := -1;
+      Data_Last  : Positive     := Positive'Last)
    is
       PM            : Pattern_Matcher (Size);
       Finalize_Size : Program_Size;
@@ -3436,13 +3424,16 @@ package body GNAT.Regpat is
       end if;
    end Match;
 
-   function  Match
+   -----------
+   -- Match --
+   -----------
+
+   function Match
      (Expression : String;
       Data       : String;
-      Size       : Program_Size := 0;
-      Data_First : Integer := -1;
-      Data_Last  : Positive := Positive'Last)
-      return       Natural
+      Size       : Program_Size := Auto_Size;
+      Data_First : Integer      := -1;
+      Data_Last  : Positive     := Positive'Last) return Natural
    is
       PM         : Pattern_Matcher (Size);
       Final_Size : Program_Size; -- unused
@@ -3456,13 +3447,16 @@ package body GNAT.Regpat is
       end if;
    end Match;
 
+   -----------
+   -- Match --
+   -----------
+
    function  Match
      (Expression : String;
       Data       : String;
-      Size       : Program_Size := 0;
-      Data_First : Integer := -1;
-      Data_Last  : Positive := Positive'Last)
-      return       Boolean
+      Size       : Program_Size := Auto_Size;
+      Data_First : Integer      := -1;
+      Data_Last  : Positive     := Positive'Last) return Boolean
    is
       Matches    : Match_Array (0 .. 0);
       PM         : Pattern_Matcher (Size);
@@ -3592,8 +3586,7 @@ package body GNAT.Regpat is
 
    function Read_Natural
      (Program : Program_Data;
-      IP      : Pointer)
-      return    Natural
+      IP      : Pointer) return Natural
    is
    begin
       return Character'Pos (Program (IP)) +
@@ -3618,7 +3611,6 @@ package body GNAT.Regpat is
       C      : Character)
    is
       Value : constant Class_Byte := Character'Pos (C);
-
    begin
       Bitmap (Value / 8) := Bitmap (Value / 8)
         or Bit_Conversion (Value mod 8);
@@ -3630,8 +3622,7 @@ package body GNAT.Regpat is
 
    function String_Length
      (Program : Program_Data;
-      P       : Pointer)
-      return    Program_Size
+      P       : Pointer) return Program_Size
    is
    begin
       pragma Assert (Program (P) = EXACT or else Program (P) = EXACTF);