(* WholeConv.mod implement the ISO WholeConv specification. Copyright (C) 2008-2023 Free Software Foundation, Inc. Contributed by Gaius Mulley . This file is part of GNU Modula-2. GNU Modula-2 is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version. GNU Modula-2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. Under Section 7 of GPL version 3, you are granted additional permissions described in the GCC Runtime Library Exception, version 3.1, as published by the Free Software Foundation. You should have received a copy of the GNU General Public License and a copy of the GCC Runtime Library Exception along with this program; see the files COPYING3 and COPYING.RUNTIME respectively. If not, see . *) IMPLEMENTATION MODULE WholeConv ; FROM CharClass IMPORT IsNumeric, IsWhiteSpace ; IMPORT EXCEPTIONS ; FROM ConvTypes IMPORT ScanClass ; TYPE WholeConvException = (noException, invalidSigned, invalidUnsigned) ; VAR wholeConv: EXCEPTIONS.ExceptionSource ; (* ScanInt - represents the start state of a finite state scanner for signed whole numbers - assigns class of inputCh to chClass and a procedure representing the next state to nextState. *) PROCEDURE ScanInt (inputCh: CHAR; VAR chClass: ConvTypes.ScanClass; VAR nextState: ConvTypes.ScanState) ; BEGIN IF IsNumeric(inputCh) THEN nextState := scanRemainingDigits ; chClass := valid ELSIF (inputCh='+') OR (inputCh='-') THEN nextState := scanFirstDigit ; chClass := valid ELSIF IsWhiteSpace(inputCh) THEN nextState := scanSpace ; chClass := padding ELSE nextState := ScanInt ; chClass := invalid END END ScanInt ; PROCEDURE scanFirstDigit (ch: CHAR; VAR chClass: ConvTypes.ScanClass; VAR nextState: ConvTypes.ScanState) ; BEGIN IF IsNumeric(ch) THEN chClass := valid ; nextState := scanRemainingDigits ELSE chClass := invalid END END scanFirstDigit ; PROCEDURE scanRemainingDigits (ch: CHAR; VAR chClass: ConvTypes.ScanClass; VAR nextState: ConvTypes.ScanState) ; BEGIN IF IsNumeric(ch) THEN chClass := valid ELSE chClass := terminator END END scanRemainingDigits ; PROCEDURE scanSpace (ch: CHAR; VAR chClass: ConvTypes.ScanClass; VAR nextState: ConvTypes.ScanState) ; BEGIN IF IsWhiteSpace(ch) THEN chClass := padding ELSIF (ch='+') OR (ch='-') THEN chClass := valid ; nextState := scanFirstDigit ELSE chClass := invalid END END scanSpace ; (* FormatInt - returns the format of the string value for conversion to INTEGER. *) PROCEDURE FormatInt (str: ARRAY OF CHAR) : ConvResults ; VAR proc : ConvTypes.ScanState ; chClass: ConvTypes.ScanClass ; i, h : CARDINAL ; BEGIN i := 1 ; h := LENGTH(str) ; ScanInt(str[0], chClass, proc) ; WHILE (i9 DO c := c DIV 10 ; INC(l) END ; RETURN( l ) END LengthInt ; (* ScanCard - represents the start state of a finite state scanner for unsigned whole numbers - assigns class of inputCh to chClass and a procedure representing the next state to nextState. *) PROCEDURE ScanCard (inputCh: CHAR; VAR chClass: ConvTypes.ScanClass; VAR nextState: ConvTypes.ScanState) ; BEGIN IF IsNumeric(inputCh) THEN nextState := scanRemainingDigits ; chClass := valid ELSIF inputCh='+' THEN nextState := scanFirstDigit ; chClass := valid ELSIF IsWhiteSpace(inputCh) THEN nextState := scanSpace ; chClass := padding ELSE nextState := ScanCard ; chClass := invalid END END ScanCard ; (* FormatCard - returns the format of the string value for conversion to CARDINAL. *) PROCEDURE FormatCard (str: ARRAY OF CHAR) : ConvResults ; VAR proc : ConvTypes.ScanState ; chClass: ConvTypes.ScanClass ; i, h : CARDINAL ; BEGIN i := 1 ; h := LENGTH(str) ; ScanCard(str[0], chClass, proc) ; WHILE (i9 DO card := card DIV 10 ; INC(l) END ; RETURN( l ) END LengthCard ; (* IsWholeConvException - returns TRUE if the current coroutine is in the exceptional execution state because of the raising of an exception in a routine from this module; otherwise returns FALSE. *) PROCEDURE IsWholeConvException () : BOOLEAN ; BEGIN RETURN( EXCEPTIONS.IsCurrentSource(wholeConv) ) END IsWholeConvException ; BEGIN EXCEPTIONS.AllocateSource(wholeConv) END WholeConv.