OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / gcc / ada / xtreeprs.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                          GNAT SYSTEM UTILITIES                           --
4 --                                                                          --
5 --                             X T R E E P R S                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --                                                                          --
10 --          Copyright (C) 1992-2001 Free Software Foundation, Inc.
11 --                                                                          --
12 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
13 -- terms of the  GNU General Public License as published  by the Free Soft- --
14 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
15 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
18 -- for  more details.  You should have  received  a copy of the GNU General --
19 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
20 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
21 -- MA 02111-1307, USA.                                                      --
22 --                                                                          --
23 -- GNAT was originally developed  by the GNAT team at  New York University. --
24 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
25 --                                                                          --
26 ------------------------------------------------------------------------------
27
28 --  Program to construct the spec of the Treeprs package
29
30 --    Input files:
31
32 --       sinfo.ads     Spec of Sinfo package
33 --       treeprs.adt   Template for Treeprs package
34
35 --    Output files:
36
37 --       treeprs.ads   Spec of Treeprs package
38
39 --  Note: this program assumes that sinfo.ads has passed the error checks which
40 --  are carried out by the CSinfo utility so it does not duplicate these checks
41
42 --  An optional argument allows the specification of an output file name to
43 --  override the default treeprs.ads file name for the generated output file.
44
45 with Ada.Command_Line;              use Ada.Command_Line;
46 with Ada.Strings.Unbounded;         use Ada.Strings.Unbounded;
47 with Ada.Strings.Unbounded.Text_IO; use Ada.Strings.Unbounded.Text_IO;
48 with Ada.Text_IO;                   use Ada.Text_IO;
49
50 with GNAT.Spitbol;                  use GNAT.Spitbol;
51 with GNAT.Spitbol.Patterns;         use GNAT.Spitbol.Patterns;
52 with GNAT.Spitbol.Table_Boolean;    use GNAT.Spitbol.Table_Boolean;
53 with GNAT.Spitbol.Table_VString;    use GNAT.Spitbol.Table_VString;
54
55 procedure XTreeprs is
56
57    package TB renames GNAT.Spitbol.Table_Boolean;
58    package TV renames GNAT.Spitbol.Table_VString;
59
60    Err : exception;
61    --  Raised on fatal error
62
63    A          : VString := Nul;
64    Ffield     : VString := Nul;
65    Field      : VString := Nul;
66    Fieldno    : VString := Nul;
67    Flagno     : VString := Nul;
68    Line       : VString := Nul;
69    Name       : VString := Nul;
70    Node       : VString := Nul;
71    Outstring  : VString := Nul;
72    Prefix     : VString := Nul;
73    S          : VString := Nul;
74    S1         : VString := Nul;
75    Syn        : VString := Nul;
76    Synonym    : VString := Nul;
77    Term       : VString := Nul;
78
79    OutS : File_Type;
80    --  Output file
81
82    InS : File_Type;
83    --  Read sinfo.ads
84
85    InT : File_Type;
86    --  Read treeprs.adt
87
88    Special : TB.Table (20);
89    --  Table of special fields. These fields are not included in the table
90    --  constructed by Xtreeprs, since they are specially handled in treeprs.
91    --  This means these field definitions are completely ignored.
92
93    Names : array (1 .. 500) of VString;
94    --  Table of names of synonyms
95
96    Positions : array (1 .. 500) of Natural;
97    --  Table of starting positions in Pchars string for synonyms
98
99    Strings : TV.Table (300);
100    --  Contribution of each synonym to Pchars string, indexed by name
101
102    Count  : Natural := 0;
103    --  Number of synonyms processed so far
104
105    Curpos : Natural := 1;
106    --  Number of characters generated in Pchars string so far
107
108    Lineno : Natural := 0;
109    --  Line number in sinfo.ads
110
111    Field_Base : constant := Character'Pos ('#');
112    --  Fields 1-5 are represented by the characters #$%&' (i.e. by five
113    --  contiguous characters starting at # (16#23#)).
114
115    Flag_Base : constant := Character'Pos ('(');
116    --  Flags 1-18 are represented by the characters ()*+,-./0123456789
117    --  (i.e. by 18 contiguous characters starting at (16#28#)).
118
119    Fieldch : Character;
120    --  Field character, as per above tables
121
122    Sp : aliased Natural;
123    --  Space left on line for Pchars output
124
125    wsp : Pattern := Span (' ' & ASCII.HT);
126
127    Is_Temp  : Pattern := BreakX ('T') * A & "T e m p l a t e";
128    Get_Node : Pattern := wsp & "--  N_" & Rest * Node;
129    Tst_Punc : Pattern := Break (" ,.");
130    Get_Syn  : Pattern := Span (' ') & "--  " & Break (' ') * Synonym
131                 & " (" & Break (')') * Field;
132    Brk_Min  : Pattern := Break ('-') * Ffield;
133    Is_Flag  : Pattern := "Flag" & Rest * Flagno;
134    Is_Field : Pattern := Rtab (1) & Len (1) * Fieldno;
135    Is_Syn   : Pattern := wsp & "N_" & Break (",)") * Syn & Len (1) * Term;
136    Brk_Node : Pattern := Break (' ') * Node & ' ';
137    Chop_SP  : Pattern := Len (Sp'Unrestricted_Access) * S1;
138
139    M : Match_Result;
140
141 begin
142    Anchored_Mode := True;
143
144    if Argument_Count > 0 then
145       Create (OutS, Out_File, Argument (1));
146    else
147       Create (OutS, Out_File, "treeprs.ads");
148    end if;
149
150    Open (InS, In_File, "sinfo.ads");
151    Open (InT, In_File, "treeprs.adt");
152
153    --  Initialize special fields table
154
155    Set (Special, "Analyzed",                True);
156    Set (Special, "Cannot_Be_Constant",      True);
157    Set (Special, "Chars",                   True);
158    Set (Special, "Comes_From_Source",       True);
159    Set (Special, "Error_Posted",            True);
160    Set (Special, "Etype",                   True);
161    Set (Special, "Has_No_Side_Effects",     True);
162    Set (Special, "Is_Controlling_Actual",   True);
163    Set (Special, "Is_Overloaded",           True);
164    Set (Special, "Is_Static_Expression",    True);
165    Set (Special, "Left_Opnd",               True);
166    Set (Special, "Must_Check_Expr",         True);
167    Set (Special, "No_Overflow_Expr",        True);
168    Set (Special, "Paren_Count",             True);
169    Set (Special, "Raises_Constraint_Error", True);
170    Set (Special, "Right_Opnd",              True);
171
172    --  Read template header and generate new header
173
174    loop
175       Line := Get_Line (InT);
176
177       --  Skip lines describing the template
178
179       if Match (Line, "--  This file is a template") then
180          loop
181             Line := Get_Line (InT);
182             exit when Line = "";
183          end loop;
184       end if;
185
186       exit when Match (Line, "package");
187
188       if Match (Line, Is_Temp, M) then
189          Replace (M, A & "    S p e c    ");
190       end if;
191
192       Put_Line (OutS, Line);
193    end loop;
194
195    Put_Line (OutS, Line);
196
197    --  Copy rest of comments up to template insert point to spec
198
199    loop
200       Line := Get_Line (InT);
201       exit when Match (Line, "!!TEMPLATE INSERTION POINT");
202       Put_Line (OutS, Line);
203    end loop;
204
205    --  Here we are doing the actual insertions
206
207    Put_Line (OutS, "   Pchars : constant String :=");
208
209    --  Loop through comments describing nodes, picking up fields
210
211    loop
212       Line := Get_Line (InS);
213       Lineno := Lineno + 1;
214       exit when Match (Line, "   type Node_Kind");
215
216       if Match (Line, Get_Node)
217         and then not Match (Node, Tst_Punc)
218       then
219          Outstring := Node & ' ';
220
221          loop
222             Line := Get_Line (InS);
223             exit when Line = "";
224
225             if Match (Line, Get_Syn)
226               and then not Match (Synonym, "plus")
227               and then not Present (Special, Synonym)
228             then
229                --  Convert this field into the character used to
230                --  represent the field according to the table:
231
232                --    Field1       '#'
233                --    Field2       '$'
234                --    Field3       '%'
235                --    Field4       '&'
236                --    Field5       "'"
237                --    Flag1        "("
238                --    Flag2        ")"
239                --    Flag3        '*'
240                --    Flag4        '+'
241                --    Flag5        ','
242                --    Flag6        '-'
243                --    Flag7        '.'
244                --    Flag8        '/'
245                --    Flag9        '0'
246                --    Flag10       '1'
247                --    Flag11       '2'
248                --    Flag12       '3'
249                --    Flag13       '4'
250                --    Flag14       '5'
251                --    Flag15       '6'
252                --    Flag16       '7'
253                --    Flag17       '8'
254                --    Flag18       '9'
255
256                if Match (Field, Brk_Min) then
257                   Field := Ffield;
258                end if;
259
260                if Match (Field, Is_Flag) then
261                   Fieldch := Char (Flag_Base - 1 + N (Flagno));
262
263                elsif Match (Field, Is_Field) then
264                   Fieldch := Char (Field_Base - 1 + N (Fieldno));
265
266                else
267                   Put_Line
268                     (Standard_Error,
269                      "*** Line " &
270                       Lineno &
271                       " has unrecognized field name " &
272                       Field);
273                   raise Err;
274                end if;
275
276                Append (Outstring, Fieldch & Synonym);
277             end if;
278          end loop;
279
280          Set (Strings, Node, Outstring);
281       end if;
282    end loop;
283
284    --  Loop through actual definitions of node kind enumeration literals
285
286    loop
287       loop
288          Line := Get_Line (InS);
289          Lineno := Lineno + 1;
290          exit when Match (Line, Is_Syn);
291       end loop;
292
293       S := Get (Strings, Syn);
294       Match (S, Brk_Node, "");
295       Count := Count + 1;
296       Names (Count) := Syn;
297       Positions (Count) := Curpos;
298       Curpos := Curpos + Length (S);
299       Put_Line (OutS, "      --  " & Node);
300       Prefix := V ("      ");
301       exit when Term = ")";
302
303       --  Loop to output the string literal for Pchars
304
305       loop
306          Sp := 79 - 4 - Length (Prefix);
307          exit when (Size (S) <= Sp);
308          Match (S, Chop_SP, "");
309          Put_Line (OutS, Prefix & '"' & S1 & """ &");
310          Prefix := V ("         ");
311       end loop;
312
313       Put_Line (OutS, Prefix & '"' & S & """ &");
314    end loop;
315
316    Put_Line (OutS, "      """";");
317    Put_Line (OutS, "");
318    Put_Line
319      (OutS, "   type Pchar_Pos_Array is array (Node_Kind) of Positive;");
320    Put_Line
321      (OutS,
322       "   Pchar_Pos : constant Pchar_Pos_Array := Pchar_Pos_Array'(");
323
324    --  Output lines for Pchar_Pos_Array values
325
326    for M in 1 .. Count - 1 loop
327       Name := Rpad ("N_" & Names (M), 40);
328       Put_Line (OutS, "      " & Name & " => " & Positions (M) & ',');
329    end loop;
330
331    Name := Rpad ("N_" & Names (Count), 40);
332    Put_Line (OutS, "      " & Name & " => " & Positions (Count) & ");");
333
334    Put_Line (OutS, "");
335    Put_Line (OutS, "end Treeprs;");
336
337 exception
338    when Err =>
339       Put_Line (Standard_Error, "*** fatal error");
340       Set_Exit_Status (1);
341
342 end XTreeprs;