annotate gcc/ada/libgnat/g-rewdat.ads @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents 84e7813d76e9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- GNAT COMPILER COMPONENTS --
kono
parents:
diff changeset
3 -- --
kono
parents:
diff changeset
4 -- G N A T . R E W R I T E _ D A T A --
kono
parents:
diff changeset
5 -- --
kono
parents:
diff changeset
6 -- S p e c --
kono
parents:
diff changeset
7 -- --
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
8 -- Copyright (C) 2014-2019, Free Software Foundation, Inc. --
111
kono
parents:
diff changeset
9 -- --
kono
parents:
diff changeset
10 -- GNAT is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
11 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
12 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
13 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
14 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
15 -- or FITNESS FOR A PARTICULAR PURPOSE. --
kono
parents:
diff changeset
16 -- --
kono
parents:
diff changeset
17 -- As a special exception under Section 7 of GPL version 3, you are granted --
kono
parents:
diff changeset
18 -- additional permissions described in the GCC Runtime Library Exception, --
kono
parents:
diff changeset
19 -- version 3.1, as published by the Free Software Foundation. --
kono
parents:
diff changeset
20 -- --
kono
parents:
diff changeset
21 -- You should have received a copy of the GNU General Public License and --
kono
parents:
diff changeset
22 -- a copy of the GCC Runtime Library Exception along with this program; --
kono
parents:
diff changeset
23 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
kono
parents:
diff changeset
24 -- <http://www.gnu.org/licenses/>. --
kono
parents:
diff changeset
25 -- --
kono
parents:
diff changeset
26 -- GNAT was originally developed by the GNAT team at New York University. --
kono
parents:
diff changeset
27 -- Extensive contributions were provided by Ada Core Technologies Inc. --
kono
parents:
diff changeset
28 -- --
kono
parents:
diff changeset
29 ------------------------------------------------------------------------------
kono
parents:
diff changeset
30
kono
parents:
diff changeset
31 -- This package can be used to rewrite data on the fly. All occurrences of a
kono
parents:
diff changeset
32 -- string (named pattern) will be replaced by another string.
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 -- It is not necessary to load all data in memory and so this package can be
kono
parents:
diff changeset
35 -- used for large data chunks like disk files for example. The pattern is
kono
parents:
diff changeset
36 -- a standard string and not a regular expression.
kono
parents:
diff changeset
37
kono
parents:
diff changeset
38 -- There is no dynamic allocation in the implementation.
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 -- For example, to replace all occurrences of "Gnat" with "GNAT":
kono
parents:
diff changeset
41
kono
parents:
diff changeset
42 -- Rewriter : Buffer := Create (Pattern => "Gnat", Value => "GNAT");
kono
parents:
diff changeset
43
kono
parents:
diff changeset
44 -- The output procedure that will receive the rewritten data:
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 -- procedure Do (Data : Stream_Element_Array) is
kono
parents:
diff changeset
47 -- begin
kono
parents:
diff changeset
48 -- <implementation to handle Data>
kono
parents:
diff changeset
49 -- end Do;
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 -- Then:
kono
parents:
diff changeset
52
kono
parents:
diff changeset
53 -- Write (Rewriter, "Let's talk about Gnat compiler", Do'Access);
kono
parents:
diff changeset
54 -- Write (Rewriter, "Gnat is an Ada compiler", Do'Access);
kono
parents:
diff changeset
55 -- Flush (Rewriter, Do'Access);
kono
parents:
diff changeset
56
kono
parents:
diff changeset
57 -- Another possible usage is to specify a method to get the input data:
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 -- procedure Get
kono
parents:
diff changeset
60 -- (Buffer : out Stream_Element_Array;
kono
parents:
diff changeset
61 -- Last : out Stream_Element_Offset)
kono
parents:
diff changeset
62 -- is
kono
parents:
diff changeset
63 -- begin
kono
parents:
diff changeset
64 -- <get some data from a file, a socket, etc...>
kono
parents:
diff changeset
65 -- Last := ...
kono
parents:
diff changeset
66 -- Buffer := ...
kono
parents:
diff changeset
67 -- end Get;
kono
parents:
diff changeset
68
kono
parents:
diff changeset
69 -- Then we can rewrite the whole file with:
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 -- Rewrite (Rewriter, Input => Get'Access, Output => Do'Access);
kono
parents:
diff changeset
72
kono
parents:
diff changeset
73 with Ada.Streams; use Ada.Streams;
kono
parents:
diff changeset
74
kono
parents:
diff changeset
75 package GNAT.Rewrite_Data is
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77 type Buffer (<>) is limited private;
kono
parents:
diff changeset
78 type Buffer_Ref is access all Buffer;
kono
parents:
diff changeset
79
kono
parents:
diff changeset
80 function Create
kono
parents:
diff changeset
81 (Pattern, Value : String;
kono
parents:
diff changeset
82 Size : Stream_Element_Offset := 1_024) return Buffer;
kono
parents:
diff changeset
83 -- Create a rewrite buffer. Pattern is the string to be rewritten as Value.
kono
parents:
diff changeset
84 -- Size represents the size of the internal buffer used to store the data
kono
parents:
diff changeset
85 -- ready to be output. A larger buffer may improve the performance, as the
kono
parents:
diff changeset
86 -- Output routine (see Write, Rewrite below) will be called only when this
kono
parents:
diff changeset
87 -- buffer is full. Note that Size cannot be lower than Pattern'Length, and
kono
parents:
diff changeset
88 -- if this is the case, then Size value is set to Pattern'Length.
kono
parents:
diff changeset
89
kono
parents:
diff changeset
90 function Size (B : Buffer) return Natural;
kono
parents:
diff changeset
91 -- Returns the current size of the buffer (count of Stream_Array_Element)
kono
parents:
diff changeset
92
kono
parents:
diff changeset
93 procedure Flush
kono
parents:
diff changeset
94 (B : in out Buffer;
kono
parents:
diff changeset
95 Output : not null access procedure (Data : Stream_Element_Array));
kono
parents:
diff changeset
96 -- Call Output for all remaining data in the buffer. The buffer is
kono
parents:
diff changeset
97 -- reset and ready for another use after this call.
kono
parents:
diff changeset
98
kono
parents:
diff changeset
99 procedure Reset (B : in out Buffer);
kono
parents:
diff changeset
100 pragma Inline (Reset);
kono
parents:
diff changeset
101 -- Clear all data in buffer, B is ready for another use. Note that this is
kono
parents:
diff changeset
102 -- not needed after a Flush. Note: all data remaining in Buffer is lost.
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 procedure Write
kono
parents:
diff changeset
105 (B : in out Buffer;
kono
parents:
diff changeset
106 Data : Stream_Element_Array;
kono
parents:
diff changeset
107 Output : not null access procedure (Data : Stream_Element_Array));
kono
parents:
diff changeset
108 -- Write Data into the buffer, call Output for any prepared data. Flush
kono
parents:
diff changeset
109 -- must be called when the last piece of Data as been sent in the Buffer.
kono
parents:
diff changeset
110
kono
parents:
diff changeset
111 procedure Rewrite
kono
parents:
diff changeset
112 (B : in out Buffer;
kono
parents:
diff changeset
113 Input : not null access procedure
kono
parents:
diff changeset
114 (Buffer : out Stream_Element_Array;
kono
parents:
diff changeset
115 Last : out Stream_Element_Offset);
kono
parents:
diff changeset
116 Output : not null access procedure (Data : Stream_Element_Array));
kono
parents:
diff changeset
117 -- Read data from Input, rewrite it, and then call Output. When there is
kono
parents:
diff changeset
118 -- no more data to be read from Input, Last must be set to 0. Before
kono
parents:
diff changeset
119 -- leaving this routine, call Flush above to send all remaining data to
kono
parents:
diff changeset
120 -- Output.
kono
parents:
diff changeset
121
kono
parents:
diff changeset
122 procedure Link (From : in out Buffer; To : Buffer_Ref);
kono
parents:
diff changeset
123 -- Link two rewrite buffers. That is, all data sent to From buffer will be
kono
parents:
diff changeset
124 -- rewritten and then passed to the To rewrite buffer.
kono
parents:
diff changeset
125
kono
parents:
diff changeset
126 private
kono
parents:
diff changeset
127
kono
parents:
diff changeset
128 type Buffer
kono
parents:
diff changeset
129 (Size, Size_Pattern, Size_Value : Stream_Element_Offset) is
kono
parents:
diff changeset
130 limited record
kono
parents:
diff changeset
131 Pos_C : Stream_Element_Offset; -- last valid element in Current
kono
parents:
diff changeset
132 Pos_B : Stream_Element_Offset; -- last valid element in Buffer
kono
parents:
diff changeset
133
kono
parents:
diff changeset
134 Next : Buffer_Ref;
kono
parents:
diff changeset
135 -- A link to another rewriter if any
kono
parents:
diff changeset
136
kono
parents:
diff changeset
137 Buffer : Stream_Element_Array (1 .. Size);
kono
parents:
diff changeset
138 -- Fully prepared/rewritten data waiting to be output
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 Current : Stream_Element_Array (1 .. Size_Pattern);
kono
parents:
diff changeset
141 -- Current data checked, this buffer contains every piece of data
kono
parents:
diff changeset
142 -- starting with the pattern. It means that at any point:
kono
parents:
diff changeset
143 -- Current (1 .. Pos_C) = Pattern (1 .. Pos_C).
kono
parents:
diff changeset
144
kono
parents:
diff changeset
145 Pattern : Stream_Element_Array (1 .. Size_Pattern);
kono
parents:
diff changeset
146 -- The pattern to look for
kono
parents:
diff changeset
147
kono
parents:
diff changeset
148 Value : Stream_Element_Array (1 .. Size_Value);
kono
parents:
diff changeset
149 -- The value the pattern is replaced by
kono
parents:
diff changeset
150 end record;
kono
parents:
diff changeset
151
kono
parents:
diff changeset
152 end GNAT.Rewrite_Data;