1 ------------------------------------------------------------------------------
3 -- GNAT SYSTEM UTILITIES --
9 -- Copyright (C) 1992-2005 Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 -- Program to construct the spec of the Treeprs package
31 -- sinfo.ads Spec of Sinfo package
32 -- treeprs.adt Template for Treeprs package
36 -- treeprs.ads Spec of Treeprs package
38 -- Note: this program assumes that sinfo.ads has passed the error checks which
39 -- are carried out by the CSinfo utility so it does not duplicate these checks
41 -- An optional argument allows the specification of an output file name to
42 -- override the default treeprs.ads file name for the generated output file.
44 with Ada.Command_Line; use Ada.Command_Line;
45 with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
46 with Ada.Strings.Unbounded.Text_IO; use Ada.Strings.Unbounded.Text_IO;
47 with Ada.Text_IO; use Ada.Text_IO;
48 with Ada.Streams.Stream_IO; use Ada.Streams.Stream_IO;
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;
57 package TB renames GNAT.Spitbol.Table_Boolean;
58 package TV renames GNAT.Spitbol.Table_VString;
61 -- Raised on fatal error
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;
76 Synonym : VString := Nul;
77 Term : VString := Nul;
79 subtype Sfile is Ada.Streams.Stream_IO.File_Type;
84 InS : Ada.Text_IO.File_Type;
87 InT : Ada.Text_IO.File_Type;
90 Special : TB.Table (20);
91 -- Table of special fields. These fields are not included in the table
92 -- constructed by Xtreeprs, since they are specially handled in treeprs.
93 -- This means these field definitions are completely ignored.
95 Names : array (1 .. 500) of VString;
96 -- Table of names of synonyms
98 Positions : array (1 .. 500) of Natural;
99 -- Table of starting positions in Pchars string for synonyms
101 Strings : TV.Table (300);
102 -- Contribution of each synonym to Pchars string, indexed by name
104 Count : Natural := 0;
105 -- Number of synonyms processed so far
107 Curpos : Natural := 1;
108 -- Number of characters generated in Pchars string so far
110 Lineno : Natural := 0;
111 -- Line number in sinfo.ads
113 Field_Base : constant := Character'Pos ('#');
114 -- Fields 1-5 are represented by the characters #$%&' (i.e. by five
115 -- contiguous characters starting at # (16#23#)).
117 Flag_Base : constant := Character'Pos ('(');
118 -- Flags 1-18 are represented by the characters ()*+,-./0123456789
119 -- (i.e. by 18 contiguous characters starting at (16#28#)).
122 -- Field character, as per above tables
124 Sp : aliased Natural;
125 -- Space left on line for Pchars output
127 wsp : Pattern := Span (' ' & ASCII.HT);
129 Is_Temp : Pattern := BreakX ('T') * A & "T e m p l a t e";
130 Get_Node : Pattern := wsp & "-- N_" & Rest * Node;
131 Tst_Punc : Pattern := Break (" ,.");
132 Get_Syn : Pattern := Span (' ') & "-- " & Break (' ') * Synonym
133 & " (" & Break (')') * Field;
134 Brk_Min : Pattern := Break ('-') * Ffield;
135 Is_Flag : Pattern := "Flag" & Rest * Flagno;
136 Is_Field : Pattern := Rtab (1) & Len (1) * Fieldno;
137 Is_Syn : Pattern := wsp & "N_" & Break (",)") * Syn & Len (1) * Term;
138 Brk_Node : Pattern := Break (' ') * Node & ' ';
139 Chop_SP : Pattern := Len (Sp'Unrestricted_Access) * S1;
143 procedure Put_Line (F : Sfile; S : String);
144 procedure Put_Line (F : Sfile; S : VString);
145 -- Local version of Put_Line ensures Unix style line endings
147 procedure Put_Line (F : Sfile; S : String) is
149 String'Write (Stream (F), S);
150 Character'Write (Stream (F), ASCII.LF);
153 procedure Put_Line (F : Sfile; S : VString) is
155 Put_Line (F, To_String (S));
158 -- Start of processing for XTreeprs
161 Anchored_Mode := True;
163 if Argument_Count > 0 then
164 Create (OutS, Out_File, Argument (1));
166 Create (OutS, Out_File, "treeprs.ads");
169 Open (InS, In_File, "sinfo.ads");
170 Open (InT, In_File, "treeprs.adt");
172 -- Initialize special fields table
174 Set (Special, "Analyzed", True);
175 Set (Special, "Cannot_Be_Constant", True);
176 Set (Special, "Chars", True);
177 Set (Special, "Comes_From_Source", True);
178 Set (Special, "Error_Posted", True);
179 Set (Special, "Etype", True);
180 Set (Special, "Has_No_Side_Effects", True);
181 Set (Special, "Is_Controlling_Actual", True);
182 Set (Special, "Is_Overloaded", True);
183 Set (Special, "Is_Static_Expression", True);
184 Set (Special, "Left_Opnd", True);
185 Set (Special, "Must_Check_Expr", True);
186 Set (Special, "No_Overflow_Expr", True);
187 Set (Special, "Paren_Count", True);
188 Set (Special, "Raises_Constraint_Error", True);
189 Set (Special, "Right_Opnd", True);
191 -- Read template header and generate new header
194 Line := Get_Line (InT);
196 -- Skip lines describing the template
198 if Match (Line, "-- This file is a template") then
200 Line := Get_Line (InT);
205 exit when Match (Line, "package");
207 if Match (Line, Is_Temp, M) then
208 Replace (M, A & " S p e c ");
211 Put_Line (OutS, Line);
214 Put_Line (OutS, Line);
216 -- Copy rest of comments up to template insert point to spec
219 Line := Get_Line (InT);
220 exit when Match (Line, "!!TEMPLATE INSERTION POINT");
221 Put_Line (OutS, Line);
224 -- Here we are doing the actual insertions
226 Put_Line (OutS, " Pchars : constant String :=");
228 -- Loop through comments describing nodes, picking up fields
231 Line := Get_Line (InS);
232 Lineno := Lineno + 1;
233 exit when Match (Line, " type Node_Kind");
235 if Match (Line, Get_Node)
236 and then not Match (Node, Tst_Punc)
238 Outstring := Node & ' ';
241 Line := Get_Line (InS);
244 if Match (Line, Get_Syn)
245 and then not Match (Synonym, "plus")
246 and then not Present (Special, Synonym)
248 -- Convert this field into the character used to
249 -- represent the field according to the table:
275 if Match (Field, Brk_Min) then
279 if Match (Field, Is_Flag) then
280 Fieldch := Char (Flag_Base - 1 + N (Flagno));
282 elsif Match (Field, Is_Field) then
283 Fieldch := Char (Field_Base - 1 + N (Fieldno));
290 " has unrecognized field name " &
295 Append (Outstring, Fieldch & Synonym);
299 Set (Strings, Node, Outstring);
303 -- Loop through actual definitions of node kind enumeration literals
307 Line := Get_Line (InS);
308 Lineno := Lineno + 1;
309 exit when Match (Line, Is_Syn);
312 S := Get (Strings, Syn);
313 Match (S, Brk_Node, "");
315 Names (Count) := Syn;
316 Positions (Count) := Curpos;
317 Curpos := Curpos + Length (S);
318 Put_Line (OutS, " -- " & Node);
320 exit when Term = ")";
322 -- Loop to output the string literal for Pchars
325 Sp := 79 - 4 - Length (Prefix);
326 exit when Size (S) <= Sp;
327 Match (S, Chop_SP, "");
328 Put_Line (OutS, Prefix & '"' & S1 & """ &");
332 Put_Line (OutS, Prefix & '"' & S & """ &");
335 Put_Line (OutS, " """";");
338 (OutS, " type Pchar_Pos_Array is array (Node_Kind) of Positive;");
341 " Pchar_Pos : constant Pchar_Pos_Array := Pchar_Pos_Array'(");
343 -- Output lines for Pchar_Pos_Array values
345 for M in 1 .. Count - 1 loop
346 Name := Rpad ("N_" & Names (M), 40);
347 Put_Line (OutS, " " & Name & " => " & Positions (M) & ',');
350 Name := Rpad ("N_" & Names (Count), 40);
351 Put_Line (OutS, " " & Name & " => " & Positions (Count) & ");");
354 Put_Line (OutS, "end Treeprs;");
358 Put_Line (Standard_Error, "*** fatal error");