annotate gcc/ada/libgnat/g-awk.ads @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- --
kono
parents:
diff changeset
3 -- GNAT COMPILER COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
5 -- G N A T . A W K --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- S p e c --
kono
parents:
diff changeset
8 -- --
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
9 -- Copyright (C) 2000-2018, AdaCore --
111
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- GNAT is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
12 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
kono
parents:
diff changeset
17 -- --
kono
parents:
diff changeset
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
kono
parents:
diff changeset
19 -- additional permissions described in the GCC Runtime Library Exception, --
kono
parents:
diff changeset
20 -- version 3.1, as published by the Free Software Foundation. --
kono
parents:
diff changeset
21 -- --
kono
parents:
diff changeset
22 -- You should have received a copy of the GNU General Public License and --
kono
parents:
diff changeset
23 -- a copy of the GCC Runtime Library Exception along with this program; --
kono
parents:
diff changeset
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
kono
parents:
diff changeset
25 -- <http://www.gnu.org/licenses/>. --
kono
parents:
diff changeset
26 -- --
kono
parents:
diff changeset
27 -- GNAT was originally developed by the GNAT team at New York University. --
kono
parents:
diff changeset
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
kono
parents:
diff changeset
29 -- --
kono
parents:
diff changeset
30 ------------------------------------------------------------------------------
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 -- This is an AWK-like unit. It provides an easy interface for parsing one
kono
parents:
diff changeset
33 -- or more files containing formatted data. The file can be viewed seen as
kono
parents:
diff changeset
34 -- a database where each record is a line and a field is a data element in
kono
parents:
diff changeset
35 -- this line. In this implementation an AWK record is a line. This means
kono
parents:
diff changeset
36 -- that a record cannot span multiple lines. The operating procedure is to
kono
parents:
diff changeset
37 -- read files line by line, with each line being presented to the user of
kono
parents:
diff changeset
38 -- the package. The interface provides services to access specific fields
kono
parents:
diff changeset
39 -- in the line. Thus it is possible to control actions taken on a line based
kono
parents:
diff changeset
40 -- on values of some fields. This can be achieved directly or by registering
kono
parents:
diff changeset
41 -- callbacks triggered on programmed conditions.
kono
parents:
diff changeset
42 --
kono
parents:
diff changeset
43 -- The state of an AWK run is recorded in an object of type session.
kono
parents:
diff changeset
44 -- The following is the procedure for using a session to control an
kono
parents:
diff changeset
45 -- AWK run:
kono
parents:
diff changeset
46 --
kono
parents:
diff changeset
47 -- 1) Specify which session is to be used. It is possible to use the
kono
parents:
diff changeset
48 -- default session or to create a new one by declaring an object of
kono
parents:
diff changeset
49 -- type Session_Type. For example:
kono
parents:
diff changeset
50 --
kono
parents:
diff changeset
51 -- Computers : Session_Type;
kono
parents:
diff changeset
52 --
kono
parents:
diff changeset
53 -- 2) Specify how to cut a line into fields. There are two modes: using
kono
parents:
diff changeset
54 -- character fields separators or column width. This is done by using
kono
parents:
diff changeset
55 -- Set_Fields_Separators or Set_Fields_Width. For example by:
kono
parents:
diff changeset
56 --
kono
parents:
diff changeset
57 -- AWK.Set_Field_Separators (";,", Computers);
kono
parents:
diff changeset
58 --
kono
parents:
diff changeset
59 -- or by using iterators' Separators parameter.
kono
parents:
diff changeset
60 --
kono
parents:
diff changeset
61 -- 3) Specify which files to parse. This is done with Add_File/Add_Files
kono
parents:
diff changeset
62 -- services, or by using the iterators' Filename parameter. For
kono
parents:
diff changeset
63 -- example:
kono
parents:
diff changeset
64 --
kono
parents:
diff changeset
65 -- AWK.Add_File ("myfile.db", Computers);
kono
parents:
diff changeset
66 --
kono
parents:
diff changeset
67 -- 4) Run the AWK session using one of the provided iterators.
kono
parents:
diff changeset
68 --
kono
parents:
diff changeset
69 -- Parse
kono
parents:
diff changeset
70 -- This is the most automated iterator. You can gain control on
kono
parents:
diff changeset
71 -- the session only by registering one or more callbacks (see
kono
parents:
diff changeset
72 -- Register).
kono
parents:
diff changeset
73 --
kono
parents:
diff changeset
74 -- Get_Line/End_Of_Data
kono
parents:
diff changeset
75 -- This is a manual iterator to be used with a loop. You have
kono
parents:
diff changeset
76 -- complete control on the session. You can use callbacks but
kono
parents:
diff changeset
77 -- this is not required.
kono
parents:
diff changeset
78 --
kono
parents:
diff changeset
79 -- For_Every_Line
kono
parents:
diff changeset
80 -- This provides a mixture of manual/automated iterator action.
kono
parents:
diff changeset
81 --
kono
parents:
diff changeset
82 -- Examples of these three approaches appear below
kono
parents:
diff changeset
83 --
kono
parents:
diff changeset
84 -- There are many ways to use this package. The following discussion shows
kono
parents:
diff changeset
85 -- three approaches to using this package, using the three iterator forms.
kono
parents:
diff changeset
86 -- All examples will use the following file (computer.db):
kono
parents:
diff changeset
87 --
kono
parents:
diff changeset
88 -- Pluton;Windows-NT;Pentium III
kono
parents:
diff changeset
89 -- Mars;Linux;Pentium Pro
kono
parents:
diff changeset
90 -- Venus;Solaris;Sparc
kono
parents:
diff changeset
91 -- Saturn;OS/2;i486
kono
parents:
diff changeset
92 -- Jupiter;MacOS;PPC
kono
parents:
diff changeset
93 --
kono
parents:
diff changeset
94 -- 1) Using Parse iterator
kono
parents:
diff changeset
95 --
kono
parents:
diff changeset
96 -- Here the first step is to register some action associated to a pattern
kono
parents:
diff changeset
97 -- and then to call the Parse iterator (this is the simplest way to use
kono
parents:
diff changeset
98 -- this unit). The default session is used here. For example to output the
kono
parents:
diff changeset
99 -- second field (the OS) of computer "Saturn".
kono
parents:
diff changeset
100 --
kono
parents:
diff changeset
101 -- procedure Action is
kono
parents:
diff changeset
102 -- begin
kono
parents:
diff changeset
103 -- Put_Line (AWK.Field (2));
kono
parents:
diff changeset
104 -- end Action;
kono
parents:
diff changeset
105 --
kono
parents:
diff changeset
106 -- begin
kono
parents:
diff changeset
107 -- AWK.Register (1, "Saturn", Action'Access);
kono
parents:
diff changeset
108 -- AWK.Parse (";", "computer.db");
kono
parents:
diff changeset
109 --
kono
parents:
diff changeset
110 --
kono
parents:
diff changeset
111 -- 2) Using the Get_Line/End_Of_Data iterator
kono
parents:
diff changeset
112 --
kono
parents:
diff changeset
113 -- Here you have full control. For example to do the same as
kono
parents:
diff changeset
114 -- above but using a specific session, you could write:
kono
parents:
diff changeset
115 --
kono
parents:
diff changeset
116 -- Computer_File : Session_Type;
kono
parents:
diff changeset
117 --
kono
parents:
diff changeset
118 -- begin
kono
parents:
diff changeset
119 -- AWK.Set_Current (Computer_File);
kono
parents:
diff changeset
120 -- AWK.Open (Separators => ";",
kono
parents:
diff changeset
121 -- Filename => "computer.db");
kono
parents:
diff changeset
122 --
kono
parents:
diff changeset
123 -- -- Display Saturn OS
kono
parents:
diff changeset
124 --
kono
parents:
diff changeset
125 -- while not AWK.End_Of_File loop
kono
parents:
diff changeset
126 -- AWK.Get_Line;
kono
parents:
diff changeset
127 --
kono
parents:
diff changeset
128 -- if AWK.Field (1) = "Saturn" then
kono
parents:
diff changeset
129 -- Put_Line (AWK.Field (2));
kono
parents:
diff changeset
130 -- end if;
kono
parents:
diff changeset
131 -- end loop;
kono
parents:
diff changeset
132 --
kono
parents:
diff changeset
133 -- AWK.Close (Computer_File);
kono
parents:
diff changeset
134 --
kono
parents:
diff changeset
135 --
kono
parents:
diff changeset
136 -- 3) Using For_Every_Line iterator
kono
parents:
diff changeset
137 --
kono
parents:
diff changeset
138 -- In this case you use a provided iterator and you pass the procedure
kono
parents:
diff changeset
139 -- that must be called for each record. You could code the previous
kono
parents:
diff changeset
140 -- example could be coded as follows (using the iterator quick interface
kono
parents:
diff changeset
141 -- but without using the current session):
kono
parents:
diff changeset
142 --
kono
parents:
diff changeset
143 -- Computer_File : Session_Type;
kono
parents:
diff changeset
144 --
kono
parents:
diff changeset
145 -- procedure Action (Quit : in out Boolean) is
kono
parents:
diff changeset
146 -- begin
kono
parents:
diff changeset
147 -- if AWK.Field (1, Computer_File) = "Saturn" then
kono
parents:
diff changeset
148 -- Put_Line (AWK.Field (2, Computer_File));
kono
parents:
diff changeset
149 -- end if;
kono
parents:
diff changeset
150 -- end Action;
kono
parents:
diff changeset
151 --
kono
parents:
diff changeset
152 -- procedure Look_For_Saturn is
kono
parents:
diff changeset
153 -- new AWK.For_Every_Line (Action);
kono
parents:
diff changeset
154 --
kono
parents:
diff changeset
155 -- begin
kono
parents:
diff changeset
156 -- Look_For_Saturn (Separators => ";",
kono
parents:
diff changeset
157 -- Filename => "computer.db",
kono
parents:
diff changeset
158 -- Session => Computer_File);
kono
parents:
diff changeset
159 --
kono
parents:
diff changeset
160 -- Integer_Text_IO.Put
kono
parents:
diff changeset
161 -- (Integer (AWK.NR (Session => Computer_File)));
kono
parents:
diff changeset
162 -- Put_Line (" line(s) have been processed.");
kono
parents:
diff changeset
163 --
kono
parents:
diff changeset
164 -- You can also use a regular expression for the pattern. Let us output
kono
parents:
diff changeset
165 -- the computer name for all computer for which the OS has a character
kono
parents:
diff changeset
166 -- O in its name.
kono
parents:
diff changeset
167 --
kono
parents:
diff changeset
168 -- Regexp : String := ".*O.*";
kono
parents:
diff changeset
169 --
kono
parents:
diff changeset
170 -- Matcher : Regpat.Pattern_Matcher := Regpat.Compile (Regexp);
kono
parents:
diff changeset
171 --
kono
parents:
diff changeset
172 -- procedure Action is
kono
parents:
diff changeset
173 -- begin
kono
parents:
diff changeset
174 -- Text_IO.Put_Line (AWK.Field (2));
kono
parents:
diff changeset
175 -- end Action;
kono
parents:
diff changeset
176 --
kono
parents:
diff changeset
177 -- begin
kono
parents:
diff changeset
178 -- AWK.Register (2, Matcher, Action'Unrestricted_Access);
kono
parents:
diff changeset
179 -- AWK.Parse (";", "computer.db");
kono
parents:
diff changeset
180 --
kono
parents:
diff changeset
181
kono
parents:
diff changeset
182 with Ada.Finalization;
kono
parents:
diff changeset
183 with GNAT.Regpat;
kono
parents:
diff changeset
184
kono
parents:
diff changeset
185 package GNAT.AWK is
kono
parents:
diff changeset
186
kono
parents:
diff changeset
187 Session_Error : exception;
kono
parents:
diff changeset
188 -- Raised when a Session is reused but is not closed
kono
parents:
diff changeset
189
kono
parents:
diff changeset
190 File_Error : exception;
kono
parents:
diff changeset
191 -- Raised when there is a file problem (see below)
kono
parents:
diff changeset
192
kono
parents:
diff changeset
193 End_Error : exception;
kono
parents:
diff changeset
194 -- Raised when an attempt is made to read beyond the end of the last
kono
parents:
diff changeset
195 -- file of a session.
kono
parents:
diff changeset
196
kono
parents:
diff changeset
197 Field_Error : exception;
kono
parents:
diff changeset
198 -- Raised when accessing a field value which does not exist
kono
parents:
diff changeset
199
kono
parents:
diff changeset
200 Data_Error : exception;
kono
parents:
diff changeset
201 -- Raised when it is impossible to convert a field value to a specific type
kono
parents:
diff changeset
202
kono
parents:
diff changeset
203 type Count is new Natural;
kono
parents:
diff changeset
204
kono
parents:
diff changeset
205 type Widths_Set is array (Positive range <>) of Positive;
kono
parents:
diff changeset
206 -- Used to store a set of columns widths
kono
parents:
diff changeset
207
kono
parents:
diff changeset
208 Default_Separators : constant String := " " & ASCII.HT;
kono
parents:
diff changeset
209
kono
parents:
diff changeset
210 Use_Current : constant String := "";
kono
parents:
diff changeset
211 -- Value used when no separator or filename is specified in iterators
kono
parents:
diff changeset
212
kono
parents:
diff changeset
213 type Session_Type is limited private;
kono
parents:
diff changeset
214 -- This is the main exported type. A session is used to keep the state of
kono
parents:
diff changeset
215 -- a full AWK run. The state comprises a list of files, the current file,
kono
parents:
diff changeset
216 -- the number of line processed, the current line, the number of fields in
kono
parents:
diff changeset
217 -- the current line... A default session is provided (see Set_Current,
kono
parents:
diff changeset
218 -- Current_Session and Default_Session below).
kono
parents:
diff changeset
219
kono
parents:
diff changeset
220 ----------------------------
kono
parents:
diff changeset
221 -- Package initialization --
kono
parents:
diff changeset
222 ----------------------------
kono
parents:
diff changeset
223
kono
parents:
diff changeset
224 -- To be thread safe it is not possible to use the default provided
kono
parents:
diff changeset
225 -- session. Each task must used a specific session and specify it
kono
parents:
diff changeset
226 -- explicitly for every services.
kono
parents:
diff changeset
227
kono
parents:
diff changeset
228 procedure Set_Current (Session : Session_Type);
kono
parents:
diff changeset
229 -- Set the session to be used by default. This file will be used when the
kono
parents:
diff changeset
230 -- Session parameter in following services is not specified.
kono
parents:
diff changeset
231
kono
parents:
diff changeset
232 function Current_Session return not null access Session_Type;
kono
parents:
diff changeset
233 -- Returns the session used by default by all services. This is the
kono
parents:
diff changeset
234 -- latest session specified by Set_Current service or the session
kono
parents:
diff changeset
235 -- provided by default with this implementation.
kono
parents:
diff changeset
236
kono
parents:
diff changeset
237 function Default_Session return not null access Session_Type;
kono
parents:
diff changeset
238 -- Returns the default session provided by this package. Note that this is
kono
parents:
diff changeset
239 -- the session return by Current_Session if Set_Current has not been used.
kono
parents:
diff changeset
240
kono
parents:
diff changeset
241 procedure Set_Field_Separators
kono
parents:
diff changeset
242 (Separators : String := Default_Separators;
kono
parents:
diff changeset
243 Session : Session_Type);
kono
parents:
diff changeset
244 procedure Set_Field_Separators
kono
parents:
diff changeset
245 (Separators : String := Default_Separators);
kono
parents:
diff changeset
246 -- Set the field separators. Each character in the string is a field
kono
parents:
diff changeset
247 -- separator. When a line is read it will be split by field using the
kono
parents:
diff changeset
248 -- separators set here. Separators can be changed at any point and in this
kono
parents:
diff changeset
249 -- case the current line is split according to the new separators. In the
kono
parents:
diff changeset
250 -- special case that Separators is a space and a tabulation
kono
parents:
diff changeset
251 -- (Default_Separators), fields are separated by runs of spaces and/or
kono
parents:
diff changeset
252 -- tabs.
kono
parents:
diff changeset
253
kono
parents:
diff changeset
254 procedure Set_FS
kono
parents:
diff changeset
255 (Separators : String := Default_Separators;
kono
parents:
diff changeset
256 Session : Session_Type)
kono
parents:
diff changeset
257 renames Set_Field_Separators;
kono
parents:
diff changeset
258 procedure Set_FS
kono
parents:
diff changeset
259 (Separators : String := Default_Separators)
kono
parents:
diff changeset
260 renames Set_Field_Separators;
kono
parents:
diff changeset
261 -- FS is the AWK abbreviation for above service
kono
parents:
diff changeset
262
kono
parents:
diff changeset
263 procedure Set_Field_Widths
kono
parents:
diff changeset
264 (Field_Widths : Widths_Set;
kono
parents:
diff changeset
265 Session : Session_Type);
kono
parents:
diff changeset
266 procedure Set_Field_Widths
kono
parents:
diff changeset
267 (Field_Widths : Widths_Set);
kono
parents:
diff changeset
268 -- This is another way to split a line by giving the length (in number of
kono
parents:
diff changeset
269 -- characters) of each field in a line. Field widths can be changed at any
kono
parents:
diff changeset
270 -- point and in this case the current line is split according to the new
kono
parents:
diff changeset
271 -- field lengths. A line split with this method must have a length equal or
kono
parents:
diff changeset
272 -- greater to the total of the field widths. All characters remaining on
kono
parents:
diff changeset
273 -- the line after the latest field are added to a new automatically
kono
parents:
diff changeset
274 -- created field.
kono
parents:
diff changeset
275
kono
parents:
diff changeset
276 procedure Add_File
kono
parents:
diff changeset
277 (Filename : String;
kono
parents:
diff changeset
278 Session : Session_Type);
kono
parents:
diff changeset
279 procedure Add_File
kono
parents:
diff changeset
280 (Filename : String);
kono
parents:
diff changeset
281 -- Add Filename to the list of file to be processed. There is no limit on
kono
parents:
diff changeset
282 -- the number of files that can be added. Files are processed in the order
kono
parents:
diff changeset
283 -- they have been added (i.e. the filename list is FIFO). If Filename does
kono
parents:
diff changeset
284 -- not exist or if it is not readable, File_Error is raised.
kono
parents:
diff changeset
285
kono
parents:
diff changeset
286 procedure Add_Files
kono
parents:
diff changeset
287 (Directory : String;
kono
parents:
diff changeset
288 Filenames : String;
kono
parents:
diff changeset
289 Number_Of_Files_Added : out Natural;
kono
parents:
diff changeset
290 Session : Session_Type);
kono
parents:
diff changeset
291 procedure Add_Files
kono
parents:
diff changeset
292 (Directory : String;
kono
parents:
diff changeset
293 Filenames : String;
kono
parents:
diff changeset
294 Number_Of_Files_Added : out Natural);
kono
parents:
diff changeset
295 -- Add all files matching the regular expression Filenames in the specified
kono
parents:
diff changeset
296 -- directory to the list of file to be processed. There is no limit on
kono
parents:
diff changeset
297 -- the number of files that can be added. Each file is processed in
kono
parents:
diff changeset
298 -- the same order they have been added (i.e. the filename list is FIFO).
kono
parents:
diff changeset
299 -- The number of files (possibly 0) added is returned in
kono
parents:
diff changeset
300 -- Number_Of_Files_Added.
kono
parents:
diff changeset
301
kono
parents:
diff changeset
302 -------------------------------------
kono
parents:
diff changeset
303 -- Information about current state --
kono
parents:
diff changeset
304 -------------------------------------
kono
parents:
diff changeset
305
kono
parents:
diff changeset
306 function Number_Of_Fields
kono
parents:
diff changeset
307 (Session : Session_Type) return Count;
kono
parents:
diff changeset
308 function Number_Of_Fields
kono
parents:
diff changeset
309 return Count;
kono
parents:
diff changeset
310 pragma Inline (Number_Of_Fields);
kono
parents:
diff changeset
311 -- Returns the number of fields in the current record. It returns 0 when
kono
parents:
diff changeset
312 -- no file is being processed.
kono
parents:
diff changeset
313
kono
parents:
diff changeset
314 function NF
kono
parents:
diff changeset
315 (Session : Session_Type) return Count
kono
parents:
diff changeset
316 renames Number_Of_Fields;
kono
parents:
diff changeset
317 function NF
kono
parents:
diff changeset
318 return Count
kono
parents:
diff changeset
319 renames Number_Of_Fields;
kono
parents:
diff changeset
320 -- AWK abbreviation for above service
kono
parents:
diff changeset
321
kono
parents:
diff changeset
322 function Number_Of_File_Lines
kono
parents:
diff changeset
323 (Session : Session_Type) return Count;
kono
parents:
diff changeset
324 function Number_Of_File_Lines
kono
parents:
diff changeset
325 return Count;
kono
parents:
diff changeset
326 pragma Inline (Number_Of_File_Lines);
kono
parents:
diff changeset
327 -- Returns the current line number in the processed file. It returns 0 when
kono
parents:
diff changeset
328 -- no file is being processed.
kono
parents:
diff changeset
329
kono
parents:
diff changeset
330 function FNR (Session : Session_Type) return Count
kono
parents:
diff changeset
331 renames Number_Of_File_Lines;
kono
parents:
diff changeset
332 function FNR return Count
kono
parents:
diff changeset
333 renames Number_Of_File_Lines;
kono
parents:
diff changeset
334 -- AWK abbreviation for above service
kono
parents:
diff changeset
335
kono
parents:
diff changeset
336 function Number_Of_Lines
kono
parents:
diff changeset
337 (Session : Session_Type) return Count;
kono
parents:
diff changeset
338 function Number_Of_Lines
kono
parents:
diff changeset
339 return Count;
kono
parents:
diff changeset
340 pragma Inline (Number_Of_Lines);
kono
parents:
diff changeset
341 -- Returns the number of line processed until now. This is equal to number
kono
parents:
diff changeset
342 -- of line in each already processed file plus FNR. It returns 0 when
kono
parents:
diff changeset
343 -- no file is being processed.
kono
parents:
diff changeset
344
kono
parents:
diff changeset
345 function NR (Session : Session_Type) return Count
kono
parents:
diff changeset
346 renames Number_Of_Lines;
kono
parents:
diff changeset
347 function NR return Count
kono
parents:
diff changeset
348 renames Number_Of_Lines;
kono
parents:
diff changeset
349 -- AWK abbreviation for above service
kono
parents:
diff changeset
350
kono
parents:
diff changeset
351 function Number_Of_Files
kono
parents:
diff changeset
352 (Session : Session_Type) return Natural;
kono
parents:
diff changeset
353 function Number_Of_Files
kono
parents:
diff changeset
354 return Natural;
kono
parents:
diff changeset
355 pragma Inline (Number_Of_Files);
kono
parents:
diff changeset
356 -- Returns the number of files associated with Session. This is the total
kono
parents:
diff changeset
357 -- number of files added with Add_File and Add_Files services.
kono
parents:
diff changeset
358
kono
parents:
diff changeset
359 function File (Session : Session_Type) return String;
kono
parents:
diff changeset
360 function File return String;
kono
parents:
diff changeset
361 -- Returns the name of the file being processed. It returns the empty
kono
parents:
diff changeset
362 -- string when no file is being processed.
kono
parents:
diff changeset
363
kono
parents:
diff changeset
364 ---------------------
kono
parents:
diff changeset
365 -- Field accessors --
kono
parents:
diff changeset
366 ---------------------
kono
parents:
diff changeset
367
kono
parents:
diff changeset
368 function Field
kono
parents:
diff changeset
369 (Rank : Count;
kono
parents:
diff changeset
370 Session : Session_Type) return String;
kono
parents:
diff changeset
371 function Field
kono
parents:
diff changeset
372 (Rank : Count) return String;
kono
parents:
diff changeset
373 -- Returns field number Rank value of the current record. If Rank = 0 it
kono
parents:
diff changeset
374 -- returns the current record (i.e. the line as read in the file). It
kono
parents:
diff changeset
375 -- raises Field_Error if Rank > NF or if Session is not open.
kono
parents:
diff changeset
376
kono
parents:
diff changeset
377 function Field
kono
parents:
diff changeset
378 (Rank : Count;
kono
parents:
diff changeset
379 Session : Session_Type) return Integer;
kono
parents:
diff changeset
380 function Field
kono
parents:
diff changeset
381 (Rank : Count) return Integer;
kono
parents:
diff changeset
382 -- Returns field number Rank value of the current record as an integer. It
kono
parents:
diff changeset
383 -- raises Field_Error if Rank > NF or if Session is not open. It
kono
parents:
diff changeset
384 -- raises Data_Error if the field value cannot be converted to an integer.
kono
parents:
diff changeset
385
kono
parents:
diff changeset
386 function Field
kono
parents:
diff changeset
387 (Rank : Count;
kono
parents:
diff changeset
388 Session : Session_Type) return Float;
kono
parents:
diff changeset
389 function Field
kono
parents:
diff changeset
390 (Rank : Count) return Float;
kono
parents:
diff changeset
391 -- Returns field number Rank value of the current record as a float. It
kono
parents:
diff changeset
392 -- raises Field_Error if Rank > NF or if Session is not open. It
kono
parents:
diff changeset
393 -- raises Data_Error if the field value cannot be converted to a float.
kono
parents:
diff changeset
394
kono
parents:
diff changeset
395 generic
kono
parents:
diff changeset
396 type Discrete is (<>);
kono
parents:
diff changeset
397 function Discrete_Field
kono
parents:
diff changeset
398 (Rank : Count;
kono
parents:
diff changeset
399 Session : Session_Type) return Discrete;
kono
parents:
diff changeset
400 generic
kono
parents:
diff changeset
401 type Discrete is (<>);
kono
parents:
diff changeset
402 function Discrete_Field_Current_Session
kono
parents:
diff changeset
403 (Rank : Count) return Discrete;
kono
parents:
diff changeset
404 -- Returns field number Rank value of the current record as a type
kono
parents:
diff changeset
405 -- Discrete. It raises Field_Error if Rank > NF. It raises Data_Error if
kono
parents:
diff changeset
406 -- the field value cannot be converted to type Discrete.
kono
parents:
diff changeset
407
kono
parents:
diff changeset
408 --------------------
kono
parents:
diff changeset
409 -- Pattern/Action --
kono
parents:
diff changeset
410 --------------------
kono
parents:
diff changeset
411
kono
parents:
diff changeset
412 -- AWK defines rules like "PATTERN { ACTION }". Which means that ACTION
kono
parents:
diff changeset
413 -- will be executed if PATTERN match. A pattern in this implementation can
kono
parents:
diff changeset
414 -- be a simple string (match function is equality), a regular expression,
kono
parents:
diff changeset
415 -- a function returning a boolean. An action is associated to a pattern
kono
parents:
diff changeset
416 -- using the Register services.
kono
parents:
diff changeset
417 --
kono
parents:
diff changeset
418 -- Each procedure Register will add a rule to the set of rules for the
kono
parents:
diff changeset
419 -- session. Rules are examined in the order they have been added.
kono
parents:
diff changeset
420
kono
parents:
diff changeset
421 type Pattern_Callback is access function return Boolean;
kono
parents:
diff changeset
422 -- This is a pattern function pointer. When it returns True the associated
kono
parents:
diff changeset
423 -- action will be called.
kono
parents:
diff changeset
424
kono
parents:
diff changeset
425 type Action_Callback is access procedure;
kono
parents:
diff changeset
426 -- A simple action pointer
kono
parents:
diff changeset
427
kono
parents:
diff changeset
428 type Match_Action_Callback is
kono
parents:
diff changeset
429 access procedure (Matches : GNAT.Regpat.Match_Array);
kono
parents:
diff changeset
430 -- An advanced action pointer used with a regular expression pattern. It
kono
parents:
diff changeset
431 -- returns an array of all the matches. See GNAT.Regpat for further
kono
parents:
diff changeset
432 -- information.
kono
parents:
diff changeset
433
kono
parents:
diff changeset
434 procedure Register
kono
parents:
diff changeset
435 (Field : Count;
kono
parents:
diff changeset
436 Pattern : String;
kono
parents:
diff changeset
437 Action : Action_Callback;
kono
parents:
diff changeset
438 Session : Session_Type);
kono
parents:
diff changeset
439 procedure Register
kono
parents:
diff changeset
440 (Field : Count;
kono
parents:
diff changeset
441 Pattern : String;
kono
parents:
diff changeset
442 Action : Action_Callback);
kono
parents:
diff changeset
443 -- Register an Action associated with a Pattern. The pattern here is a
kono
parents:
diff changeset
444 -- simple string that must match exactly the field number specified.
kono
parents:
diff changeset
445
kono
parents:
diff changeset
446 procedure Register
kono
parents:
diff changeset
447 (Field : Count;
kono
parents:
diff changeset
448 Pattern : GNAT.Regpat.Pattern_Matcher;
kono
parents:
diff changeset
449 Action : Action_Callback;
kono
parents:
diff changeset
450 Session : Session_Type);
kono
parents:
diff changeset
451 procedure Register
kono
parents:
diff changeset
452 (Field : Count;
kono
parents:
diff changeset
453 Pattern : GNAT.Regpat.Pattern_Matcher;
kono
parents:
diff changeset
454 Action : Action_Callback);
kono
parents:
diff changeset
455 -- Register an Action associated with a Pattern. The pattern here is a
kono
parents:
diff changeset
456 -- simple regular expression which must match the field number specified.
kono
parents:
diff changeset
457
kono
parents:
diff changeset
458 procedure Register
kono
parents:
diff changeset
459 (Field : Count;
kono
parents:
diff changeset
460 Pattern : GNAT.Regpat.Pattern_Matcher;
kono
parents:
diff changeset
461 Action : Match_Action_Callback;
kono
parents:
diff changeset
462 Session : Session_Type);
kono
parents:
diff changeset
463 procedure Register
kono
parents:
diff changeset
464 (Field : Count;
kono
parents:
diff changeset
465 Pattern : GNAT.Regpat.Pattern_Matcher;
kono
parents:
diff changeset
466 Action : Match_Action_Callback);
kono
parents:
diff changeset
467 -- Same as above but it pass the set of matches to the action
kono
parents:
diff changeset
468 -- procedure. This is useful to analyze further why and where a regular
kono
parents:
diff changeset
469 -- expression did match.
kono
parents:
diff changeset
470
kono
parents:
diff changeset
471 procedure Register
kono
parents:
diff changeset
472 (Pattern : Pattern_Callback;
kono
parents:
diff changeset
473 Action : Action_Callback;
kono
parents:
diff changeset
474 Session : Session_Type);
kono
parents:
diff changeset
475 procedure Register
kono
parents:
diff changeset
476 (Pattern : Pattern_Callback;
kono
parents:
diff changeset
477 Action : Action_Callback);
kono
parents:
diff changeset
478 -- Register an Action associated with a Pattern. The pattern here is a
kono
parents:
diff changeset
479 -- function that must return a boolean. Action callback will be called if
kono
parents:
diff changeset
480 -- the pattern callback returns True and nothing will happen if it is
kono
parents:
diff changeset
481 -- False. This version is more general, the two other register services
kono
parents:
diff changeset
482 -- trigger an action based on the value of a single field only.
kono
parents:
diff changeset
483
kono
parents:
diff changeset
484 procedure Register
kono
parents:
diff changeset
485 (Action : Action_Callback;
kono
parents:
diff changeset
486 Session : Session_Type);
kono
parents:
diff changeset
487 procedure Register
kono
parents:
diff changeset
488 (Action : Action_Callback);
kono
parents:
diff changeset
489 -- Register an Action that will be called for every line. This is
kono
parents:
diff changeset
490 -- equivalent to a Pattern_Callback function always returning True.
kono
parents:
diff changeset
491
kono
parents:
diff changeset
492 --------------------
kono
parents:
diff changeset
493 -- Parse iterator --
kono
parents:
diff changeset
494 --------------------
kono
parents:
diff changeset
495
kono
parents:
diff changeset
496 procedure Parse
kono
parents:
diff changeset
497 (Separators : String := Use_Current;
kono
parents:
diff changeset
498 Filename : String := Use_Current;
kono
parents:
diff changeset
499 Session : Session_Type);
kono
parents:
diff changeset
500 procedure Parse
kono
parents:
diff changeset
501 (Separators : String := Use_Current;
kono
parents:
diff changeset
502 Filename : String := Use_Current);
kono
parents:
diff changeset
503 -- Launch the iterator, it will read every line in all specified
kono
parents:
diff changeset
504 -- session's files. Registered callbacks are then called if the associated
kono
parents:
diff changeset
505 -- pattern match. It is possible to specify a filename and a set of
kono
parents:
diff changeset
506 -- separators directly. This offer a quick way to parse a single
kono
parents:
diff changeset
507 -- file. These parameters will override those specified by Set_FS and
kono
parents:
diff changeset
508 -- Add_File. The Session will be opened and closed automatically.
kono
parents:
diff changeset
509 -- File_Error is raised if there is no file associated with Session, or if
kono
parents:
diff changeset
510 -- a file associated with Session is not longer readable. It raises
kono
parents:
diff changeset
511 -- Session_Error is Session is already open.
kono
parents:
diff changeset
512
kono
parents:
diff changeset
513 -----------------------------------
kono
parents:
diff changeset
514 -- Get_Line/End_Of_Data Iterator --
kono
parents:
diff changeset
515 -----------------------------------
kono
parents:
diff changeset
516
kono
parents:
diff changeset
517 type Callback_Mode is (None, Only, Pass_Through);
kono
parents:
diff changeset
518 -- These mode are used for Get_Line/End_Of_Data and For_Every_Line
kono
parents:
diff changeset
519 -- iterators. The associated semantic is:
kono
parents:
diff changeset
520 --
kono
parents:
diff changeset
521 -- None
kono
parents:
diff changeset
522 -- callbacks are not active. This is the default mode for
kono
parents:
diff changeset
523 -- Get_Line/End_Of_Data and For_Every_Line iterators.
kono
parents:
diff changeset
524 --
kono
parents:
diff changeset
525 -- Only
kono
parents:
diff changeset
526 -- callbacks are active, if at least one pattern match, the associated
kono
parents:
diff changeset
527 -- action is called and this line will not be passed to the user. In
kono
parents:
diff changeset
528 -- the Get_Line case the next line will be read (if there is some
kono
parents:
diff changeset
529 -- line remaining), in the For_Every_Line case Action will
kono
parents:
diff changeset
530 -- not be called for this line.
kono
parents:
diff changeset
531 --
kono
parents:
diff changeset
532 -- Pass_Through
kono
parents:
diff changeset
533 -- callbacks are active, for patterns which match the associated
kono
parents:
diff changeset
534 -- action is called. Then the line is passed to the user. It means
kono
parents:
diff changeset
535 -- that Action procedure is called in the For_Every_Line case and
kono
parents:
diff changeset
536 -- that Get_Line returns with the current line active.
kono
parents:
diff changeset
537 --
kono
parents:
diff changeset
538
kono
parents:
diff changeset
539 procedure Open
kono
parents:
diff changeset
540 (Separators : String := Use_Current;
kono
parents:
diff changeset
541 Filename : String := Use_Current;
kono
parents:
diff changeset
542 Session : Session_Type);
kono
parents:
diff changeset
543 procedure Open
kono
parents:
diff changeset
544 (Separators : String := Use_Current;
kono
parents:
diff changeset
545 Filename : String := Use_Current);
kono
parents:
diff changeset
546 -- Open the first file and initialize the unit. This must be called once
kono
parents:
diff changeset
547 -- before using Get_Line. It is possible to specify a filename and a set of
kono
parents:
diff changeset
548 -- separators directly. This offer a quick way to parse a single file.
kono
parents:
diff changeset
549 -- These parameters will override those specified by Set_FS and Add_File.
kono
parents:
diff changeset
550 -- File_Error is raised if there is no file associated with Session, or if
kono
parents:
diff changeset
551 -- the first file associated with Session is no longer readable. It raises
kono
parents:
diff changeset
552 -- Session_Error is Session is already open.
kono
parents:
diff changeset
553
kono
parents:
diff changeset
554 procedure Get_Line
kono
parents:
diff changeset
555 (Callbacks : Callback_Mode := None;
kono
parents:
diff changeset
556 Session : Session_Type);
kono
parents:
diff changeset
557 procedure Get_Line
kono
parents:
diff changeset
558 (Callbacks : Callback_Mode := None);
kono
parents:
diff changeset
559 -- Read a line from the current input file. If the file index is at the
kono
parents:
diff changeset
560 -- end of the current input file (i.e. End_Of_File is True) then the
kono
parents:
diff changeset
561 -- following file is opened. If there is no more file to be processed,
kono
parents:
diff changeset
562 -- exception End_Error will be raised. File_Error will be raised if Open
kono
parents:
diff changeset
563 -- has not been called. Next call to Get_Line will return the following
kono
parents:
diff changeset
564 -- line in the file. By default the registered callbacks are not called by
kono
parents:
diff changeset
565 -- Get_Line, this can activated by setting Callbacks (see Callback_Mode
kono
parents:
diff changeset
566 -- description above). File_Error may be raised if a file associated with
kono
parents:
diff changeset
567 -- Session is not readable.
kono
parents:
diff changeset
568 --
kono
parents:
diff changeset
569 -- When Callbacks is not None, it is possible to exhaust all the lines
kono
parents:
diff changeset
570 -- of all the files associated with Session. In this case, File_Error
kono
parents:
diff changeset
571 -- is not raised.
kono
parents:
diff changeset
572 --
kono
parents:
diff changeset
573 -- This procedure can be used from a subprogram called by procedure Parse
kono
parents:
diff changeset
574 -- or by an instantiation of For_Every_Line (see below).
kono
parents:
diff changeset
575
kono
parents:
diff changeset
576 function End_Of_Data
kono
parents:
diff changeset
577 (Session : Session_Type) return Boolean;
kono
parents:
diff changeset
578 function End_Of_Data
kono
parents:
diff changeset
579 return Boolean;
kono
parents:
diff changeset
580 pragma Inline (End_Of_Data);
kono
parents:
diff changeset
581 -- Returns True if there is no more data to be processed in Session. It
kono
parents:
diff changeset
582 -- means that the latest session's file is being processed and that
kono
parents:
diff changeset
583 -- there is no more data to be read in this file (End_Of_File is True).
kono
parents:
diff changeset
584
kono
parents:
diff changeset
585 function End_Of_File
kono
parents:
diff changeset
586 (Session : Session_Type) return Boolean;
kono
parents:
diff changeset
587 function End_Of_File
kono
parents:
diff changeset
588 return Boolean;
kono
parents:
diff changeset
589 pragma Inline (End_Of_File);
kono
parents:
diff changeset
590 -- Returns True when there is no more data to be processed on the current
kono
parents:
diff changeset
591 -- session's file.
kono
parents:
diff changeset
592
kono
parents:
diff changeset
593 procedure Close (Session : Session_Type);
kono
parents:
diff changeset
594 -- Release all associated data with Session. All memory allocated will
kono
parents:
diff changeset
595 -- be freed, the current file will be closed if needed, the callbacks
kono
parents:
diff changeset
596 -- will be unregistered. Close is convenient in reestablishing a session
kono
parents:
diff changeset
597 -- for new use. Get_Line is no longer usable (will raise File_Error)
kono
parents:
diff changeset
598 -- except after a successful call to Open, Parse or an instantiation
kono
parents:
diff changeset
599 -- of For_Every_Line.
kono
parents:
diff changeset
600
kono
parents:
diff changeset
601 -----------------------------
kono
parents:
diff changeset
602 -- For_Every_Line iterator --
kono
parents:
diff changeset
603 -----------------------------
kono
parents:
diff changeset
604
kono
parents:
diff changeset
605 generic
kono
parents:
diff changeset
606 with procedure Action (Quit : in out Boolean);
kono
parents:
diff changeset
607 procedure For_Every_Line
kono
parents:
diff changeset
608 (Separators : String := Use_Current;
kono
parents:
diff changeset
609 Filename : String := Use_Current;
kono
parents:
diff changeset
610 Callbacks : Callback_Mode := None;
kono
parents:
diff changeset
611 Session : Session_Type);
kono
parents:
diff changeset
612 generic
kono
parents:
diff changeset
613 with procedure Action (Quit : in out Boolean);
kono
parents:
diff changeset
614 procedure For_Every_Line_Current_Session
kono
parents:
diff changeset
615 (Separators : String := Use_Current;
kono
parents:
diff changeset
616 Filename : String := Use_Current;
kono
parents:
diff changeset
617 Callbacks : Callback_Mode := None);
kono
parents:
diff changeset
618 -- This is another iterator. Action will be called for each new
kono
parents:
diff changeset
619 -- record. The iterator's termination can be controlled by setting Quit
kono
parents:
diff changeset
620 -- to True. It is by default set to False. It is possible to specify a
kono
parents:
diff changeset
621 -- filename and a set of separators directly. This offer a quick way to
kono
parents:
diff changeset
622 -- parse a single file. These parameters will override those specified by
kono
parents:
diff changeset
623 -- Set_FS and Add_File. By default the registered callbacks are not called
kono
parents:
diff changeset
624 -- by For_Every_Line, this can activated by setting Callbacks (see
kono
parents:
diff changeset
625 -- Callback_Mode description above). The Session will be opened and
kono
parents:
diff changeset
626 -- closed automatically. File_Error is raised if there is no file
kono
parents:
diff changeset
627 -- associated with Session. It raises Session_Error is Session is already
kono
parents:
diff changeset
628 -- open.
kono
parents:
diff changeset
629
kono
parents:
diff changeset
630 private
kono
parents:
diff changeset
631 type Session_Data;
kono
parents:
diff changeset
632 type Session_Data_Access is access Session_Data;
kono
parents:
diff changeset
633
kono
parents:
diff changeset
634 type Session_Type is new Ada.Finalization.Limited_Controlled with record
kono
parents:
diff changeset
635 Data : Session_Data_Access;
kono
parents:
diff changeset
636 Self : not null access Session_Type := Session_Type'Unchecked_Access;
kono
parents:
diff changeset
637 end record;
kono
parents:
diff changeset
638
kono
parents:
diff changeset
639 procedure Initialize (Session : in out Session_Type);
kono
parents:
diff changeset
640 procedure Finalize (Session : in out Session_Type);
kono
parents:
diff changeset
641
kono
parents:
diff changeset
642 end GNAT.AWK;