OSDN Git Service

New Language: Ada
[pf3gnuchains/gcc-fork.git] / gcc / ada / casing.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                               C A S I N G                                --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --                            $Revision: 1.23 $
10 --                                                                          --
11 --          Copyright (C) 1992-2001 Free Software Foundation, Inc.          --
12 --                                                                          --
13 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
14 -- terms of the  GNU General Public License as published  by the Free Soft- --
15 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
16 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
19 -- for  more details.  You should have  received  a copy of the GNU General --
20 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
21 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
22 -- MA 02111-1307, USA.                                                      --
23 --                                                                          --
24 -- As a special exception,  if other files  instantiate  generics from this --
25 -- unit, or you link  this unit with other files  to produce an executable, --
26 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
27 -- covered  by the  GNU  General  Public  License.  This exception does not --
28 -- however invalidate  any other reasons why  the executable file  might be --
29 -- covered by the  GNU Public License.                                      --
30 --                                                                          --
31 -- GNAT was originally developed  by the GNAT team at  New York University. --
32 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
33 --                                                                          --
34 ------------------------------------------------------------------------------
35
36 with Csets;    use Csets;
37 with Namet;    use Namet;
38 with Opt;      use Opt;
39 with Types;    use Types;
40 with Widechar; use Widechar;
41
42 package body Casing is
43
44    ----------------------
45    -- Determine_Casing --
46    ----------------------
47
48    function Determine_Casing (Ident : Text_Buffer) return Casing_Type is
49
50       All_Lower : Boolean := True;
51       --  Set False if upper case letter found
52
53       All_Upper : Boolean := True;
54       --  Set False if lower case letter found
55
56       Mixed : Boolean := True;
57       --  Set False if exception to mixed case rule found (lower case letter
58       --  at start or after underline, or upper case letter elsewhere).
59
60       Decisive : Boolean := False;
61       --  Set True if at least one instance of letter not after underline
62
63       After_Und : Boolean := True;
64       --  True at start of string, and after an underline character
65
66    begin
67       for S in Ident'Range loop
68          if Ident (S) = '_' or else Ident (S) = '.' then
69             After_Und := True;
70
71          elsif Is_Lower_Case_Letter (Ident (S)) then
72             All_Upper := False;
73
74             if not After_Und then
75                Decisive := True;
76             else
77                After_Und := False;
78                Mixed := False;
79             end if;
80
81          elsif Is_Upper_Case_Letter (Ident (S)) then
82             All_Lower := False;
83
84             if not After_Und then
85                Decisive := True;
86                Mixed := False;
87             else
88                After_Und := False;
89             end if;
90          end if;
91       end loop;
92
93       --  Now we can figure out the result from the flags we set in that loop
94
95       if All_Lower then
96          return All_Lower_Case;
97
98       elsif not Decisive then
99          return Unknown;
100
101       elsif All_Upper then
102          return All_Upper_Case;
103
104       elsif Mixed then
105          return Mixed_Case;
106
107       else
108          return Unknown;
109       end if;
110    end Determine_Casing;
111
112    ------------------------
113    -- Set_All_Upper_Case --
114    ------------------------
115
116    procedure Set_All_Upper_Case is
117    begin
118       Set_Casing (All_Upper_Case);
119    end Set_All_Upper_Case;
120
121    ----------------
122    -- Set_Casing --
123    ----------------
124
125    procedure Set_Casing (C : Casing_Type; D : Casing_Type := Mixed_Case) is
126       Ptr : Natural;
127
128       Actual_Casing : Casing_Type;
129       --  Set from C or D as appropriate
130
131       After_Und : Boolean := True;
132       --  True at start of string, and after an underline character or after
133       --  any other special character that is not a normal identifier char).
134
135    begin
136       if C /= Unknown then
137          Actual_Casing := C;
138       else
139          Actual_Casing := D;
140       end if;
141
142       Ptr := 1;
143
144       while Ptr <= Name_Len loop
145          if Name_Buffer (Ptr) = ASCII.ESC
146            or else Name_Buffer (Ptr) = '['
147            or else (Upper_Half_Encoding
148                      and then Name_Buffer (Ptr) in Upper_Half_Character)
149          then
150             Skip_Wide (Name_Buffer, Ptr);
151             After_Und := False;
152
153          elsif Name_Buffer (Ptr) = '_'
154             or else not Identifier_Char (Name_Buffer (Ptr))
155          then
156             After_Und := True;
157             Ptr := Ptr + 1;
158
159          elsif Is_Lower_Case_Letter (Name_Buffer (Ptr)) then
160             if Actual_Casing = All_Upper_Case
161               or else (After_Und and then Actual_Casing = Mixed_Case)
162             then
163                Name_Buffer (Ptr) := Fold_Upper (Name_Buffer (Ptr));
164             end if;
165
166             After_Und := False;
167             Ptr := Ptr + 1;
168
169          elsif Is_Upper_Case_Letter (Name_Buffer (Ptr)) then
170             if Actual_Casing = All_Lower_Case
171               or else (not After_Und and then Actual_Casing = Mixed_Case)
172             then
173                Name_Buffer (Ptr) := Fold_Lower (Name_Buffer (Ptr));
174             end if;
175
176             After_Und := False;
177             Ptr := Ptr + 1;
178
179          else  --  all other characters
180             After_Und := False;
181             Ptr := Ptr + 1;
182          end if;
183       end loop;
184    end Set_Casing;
185
186 end Casing;