Debian Codegraph @

gcc/m2/gm2-libs-iso/WholeConv.mod

raw
1 (* WholeConv.mod implement the ISO WholeConv specification.
2
3 Copyright (C) 2008-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
21
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. *)
26
27 IMPLEMENTATION MODULE WholeConv ;
28
29 FROM CharClass IMPORT IsNumeric, IsWhiteSpace ;
30 IMPORT EXCEPTIONS ;
31
32 FROM ConvTypes IMPORT ScanClass ;
33
34
35 TYPE
36 WholeConvException = (noException, invalidSigned, invalidUnsigned) ;
37
38 VAR
39 wholeConv: EXCEPTIONS.ExceptionSource ;
40
41
42 (*
43 ScanInt - represents the start state of a finite state scanner
44 for signed whole numbers - assigns class of inputCh
45 to chClass and a procedure representing the next state
46 to nextState.
47 *)
48
49 PROCEDURE ScanInt (inputCh: CHAR;
50 VAR chClass: ConvTypes.ScanClass;
51 VAR nextState: ConvTypes.ScanState) ;
52 BEGIN
53 IF IsNumeric(inputCh)
54 THEN
55 nextState := scanRemainingDigits ;
56 chClass := valid
57 ELSIF (inputCh='+') OR (inputCh='-')
58 THEN
59 nextState := scanFirstDigit ;
60 chClass := valid
61 ELSIF IsWhiteSpace(inputCh)
62 THEN
63 nextState := scanSpace ;
64 chClass := padding
65 ELSE
66 nextState := ScanInt ;
67 chClass := invalid
68 END
69 END ScanInt ;
70
71
72 PROCEDURE scanFirstDigit (ch: CHAR;
73 VAR chClass: ConvTypes.ScanClass;
74 VAR nextState: ConvTypes.ScanState) ;
75 BEGIN
76 IF IsNumeric(ch)
77 THEN
78 chClass := valid ;
79 nextState := scanRemainingDigits
80 ELSE
81 chClass := invalid
82 END
83 END scanFirstDigit ;
84
85
86 PROCEDURE scanRemainingDigits (ch: CHAR;
87 VAR chClass: ConvTypes.ScanClass;
88 VAR nextState: ConvTypes.ScanState) ;
89 BEGIN
90 IF IsNumeric(ch)
91 THEN
92 chClass := valid
93 ELSE
94 chClass := terminator
95 END
96 END scanRemainingDigits ;
97
98
99 PROCEDURE scanSpace (ch: CHAR;
100 VAR chClass: ConvTypes.ScanClass;
101 VAR nextState: ConvTypes.ScanState) ;
102 BEGIN
103 IF IsWhiteSpace(ch)
104 THEN
105 chClass := padding
106 ELSIF (ch='+') OR (ch='-')
107 THEN
108 chClass := valid ;
109 nextState := scanFirstDigit
110 ELSE
111 chClass := invalid
112 END
113 END scanSpace ;
114
115
116 (*
117 FormatInt - returns the format of the string value for
118 conversion to INTEGER.
119 *)
120
121 PROCEDURE FormatInt (str: ARRAY OF CHAR) : ConvResults ;
122 VAR
123 proc : ConvTypes.ScanState ;
124 chClass: ConvTypes.ScanClass ;
125 i, h : CARDINAL ;
126 BEGIN
127 i := 1 ;
128 h := LENGTH(str) ;
129 ScanInt(str[0], chClass, proc) ;
130 WHILE (i<h) AND (chClass=padding) DO
131 proc(str[i], chClass, proc) ;
132 INC(i)
133 END ;
134 IF chClass=terminator
135 THEN
136 RETURN( strEmpty )
137 END ;
138 WHILE (i<h) AND (chClass=valid) DO
139 proc(str[i], chClass, proc) ;
140 INC(i)
141 END ;
142 CASE chClass OF
143
144 padding : RETURN( strWrongFormat ) |
145 terminator,
146 valid : RETURN( strAllRight ) |
147 invalid : RETURN( strWrongFormat )
148
149 END
150 END FormatInt ;
151
152
153 (*
154 ValueInt - returns the value corresponding to the signed whole
155 number string value str if str is well-formed;
156 otherwise raises the WholeConv exception.
157 *)
158
159 PROCEDURE ValueInt (str: ARRAY OF CHAR) : INTEGER;
160 VAR
161 proc : ConvTypes.ScanState ;
162 chClass: ConvTypes.ScanClass ;
163 i, h : CARDINAL ;
164 v : INTEGER ;
165 value : CARDINAL ;
166 neg : BOOLEAN ;
167 BEGIN
168 IF FormatInt(str)=strAllRight
169 THEN
170 value := 0 ;
171 neg := FALSE ;
172 i := 0 ;
173 h := LENGTH(str) ;
174 proc := ScanInt ;
175 chClass := valid ;
176 WHILE (i<h) AND ((chClass=valid) OR (chClass=padding)) DO
177 IF str[i]='-'
178 THEN
179 neg := NOT neg
180 ELSIF str[i]='+'
181 THEN
182 (* ignore *)
183 ELSIF IsNumeric(str[i])
184 THEN
185 value := value*10+(ORD(str[i])-ORD('0'))
186 END ;
187 proc(str[i], chClass, proc) ;
188 INC(i)
189 END ;
190 IF neg
191 THEN
192 v := -value
193 ELSE
194 v := value
195 END ;
196 RETURN( v )
197 ELSE
198 EXCEPTIONS.RAISE(wholeConv, ORD(invalidSigned),
199 'WholeConv.' + __FUNCTION__ + ': signed number is invalid') ;
200 RETURN 0
201 END
202 END ValueInt ;
203
204
205 (*
206 LengthInt - returns the number of characters in the string
207 representation of int.
208 *)
209
210 PROCEDURE LengthInt (int: INTEGER) : CARDINAL ;
211 VAR
212 c, l: CARDINAL ;
213 BEGIN
214 IF int<0
215 THEN
216 l := 2 ;
217 IF int=MIN(INTEGER)
218 THEN
219 c := VAL(CARDINAL, MAX(INTEGER))+1
220 ELSE
221 c := -int
222 END
223 ELSE
224 l := 1 ;
225 c := int
226 END ;
227 WHILE c>9 DO
228 c := c DIV 10 ;
229 INC(l)
230 END ;
231 RETURN( l )
232 END LengthInt ;
233
234
235 (*
236 ScanCard - represents the start state of a finite state scanner for
237 unsigned whole numbers - assigns class of inputCh to
238 chClass and a procedure representing the next state to
239 nextState.
240 *)
241
242 PROCEDURE ScanCard (inputCh: CHAR;
243 VAR chClass: ConvTypes.ScanClass;
244 VAR nextState: ConvTypes.ScanState) ;
245 BEGIN
246 IF IsNumeric(inputCh)
247 THEN
248 nextState := scanRemainingDigits ;
249 chClass := valid
250 ELSIF inputCh='+'
251 THEN
252 nextState := scanFirstDigit ;
253 chClass := valid
254 ELSIF IsWhiteSpace(inputCh)
255 THEN
256 nextState := scanSpace ;
257 chClass := padding
258 ELSE
259 nextState := ScanCard ;
260 chClass := invalid
261 END
262 END ScanCard ;
263
264
265 (*
266 FormatCard - returns the format of the string value for
267 conversion to CARDINAL.
268 *)
269
270 PROCEDURE FormatCard (str: ARRAY OF CHAR) : ConvResults ;
271 VAR
272 proc : ConvTypes.ScanState ;
273 chClass: ConvTypes.ScanClass ;
274 i, h : CARDINAL ;
275 BEGIN
276 i := 1 ;
277 h := LENGTH(str) ;
278 ScanCard(str[0], chClass, proc) ;
279 WHILE (i<h) AND (chClass=padding) DO
280 proc(str[i], chClass, proc) ;
281 INC(i)
282 END ;
283 IF chClass=terminator
284 THEN
285 RETURN( strEmpty )
286 END ;
287 WHILE (i<h) AND (chClass=valid) DO
288 proc(str[i], chClass, proc) ;
289 INC(i)
290 END ;
291 CASE chClass OF
292
293 padding : RETURN( strWrongFormat ) |
294 terminator,
295 valid : RETURN( strAllRight ) |
296 invalid : RETURN( strWrongFormat )
297
298 END
299 END FormatCard ;
300
301
302 (*
303 ValueCard - returns the value corresponding to the unsigned
304 whole number string value str if str is well-formed;
305 otherwise raises the WholeConv exception.
306 *)
307
308 PROCEDURE ValueCard (str: ARRAY OF CHAR) : CARDINAL ;
309 VAR
310 proc : ConvTypes.ScanState ;
311 chClass: ConvTypes.ScanClass ;
312 i, h : CARDINAL ;
313 value : CARDINAL ;
314 BEGIN
315 IF FormatCard(str)=strAllRight
316 THEN
317 value := 0 ;
318 i := 0 ;
319 h := LENGTH(str) ;
320 ScanCard(str[0], chClass, proc) ;
321 proc := ScanCard ;
322 chClass := valid ;
323 WHILE (i<h) AND ((chClass=valid) OR (chClass=padding)) DO
324 IF str[i]='+'
325 THEN
326 (* ignore *)
327 ELSIF IsNumeric(str[i])
328 THEN
329 value := value*10+(ORD(str[i])-ORD('0'))
330 END ;
331 proc(str[i], chClass, proc) ;
332 INC(i)
333 END ;
334 RETURN( value )
335 ELSE
336 EXCEPTIONS.RAISE(wholeConv, ORD(invalidUnsigned),
337 'WholeConv:' + __FUNCTION__ + ': unsigned number is invalid') ;
338 RETURN 0
339 END
340 END ValueCard ;
341
342
343 (*
344 LengthCard - returns the number of characters in the string
345 representation of, card.
346 *)
347
348 PROCEDURE LengthCard (card: CARDINAL) : CARDINAL ;
349 VAR
350 l: CARDINAL ;
351 BEGIN
352 l := 1 ;
353 WHILE card>9 DO
354 card := card DIV 10 ;
355 INC(l)
356 END ;
357 RETURN( l )
358 END LengthCard ;
359
360
361 (*
362 IsWholeConvException - returns TRUE if the current coroutine is
363 in the exceptional execution state because
364 of the raising of an exception in a routine
365 from this module; otherwise returns FALSE.
366 *)
367
368 PROCEDURE IsWholeConvException () : BOOLEAN ;
369 BEGIN
370 RETURN( EXCEPTIONS.IsCurrentSource(wholeConv) )
371 END IsWholeConvException ;
372
373
374 BEGIN
375 EXCEPTIONS.AllocateSource(wholeConv)
376 END WholeConv.