1 (* PushBackInput.mod provides a method for pushing back and consuming input.
3 Copyright (C) 2001-2024 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 This file is part of GNU Modula-2.
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)
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.
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.
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/>. *)
27 IMPLEMENTATION MODULE PushBackInput
;
30 FROM FIO
IMPORT ReadChar
, IsNoError
, EOF
, OpenToRead
, WriteChar
, StdErr
;
31 FROM DynamicStrings
IMPORT string
, Length
, char
;
32 FROM ASCII
IMPORT nul
, cr
, lf
;
33 FROM Debug
IMPORT Halt
;
34 FROM StrLib
IMPORT StrCopy
, StrLen
;
35 FROM NumberIO
IMPORT WriteCard
;
36 FROM StrIO
IMPORT WriteString
, WriteLn
;
37 FROM StdIO
IMPORT Write
, PushOutput
, PopOutput
;
38 FROM libc
IMPORT exit
;
44 MaxPushBackStack
= 8192 ;
48 FileName
: ARRAY [0..MaxFileName
] OF CHAR ;
49 CharStack
: ARRAY [0..MaxPushBackStack
] OF CHAR ;
50 ExitStatus
: CARDINAL ;
58 GetCh - gets a character from either the push back stack or
62 PROCEDURE GetCh (f
: File
) : CHAR ;
71 Write(CharStack
[StackPtr
])
73 RETURN( CharStack
[StackPtr
] )
75 IF EOF(f
) OR (NOT IsNoError(f
))
81 UNTIL (ch#cr
) OR EOF(f
) OR (NOT IsNoError(f
)) ;
100 PutStr - pushes a dynamic string onto the push back stack.
101 The string, s, is not deallocated.
104 PROCEDURE PutStr (s
: String
) ;
111 IF PutCh (char (s
, i
)) #
char (s
, i
)
113 Halt('assert failed', __FILE__
, __FUNCTION__
, __LINE__
)
120 PutString - pushes a string onto the push back stack.
123 PROCEDURE PutString (a
: ARRAY OF CHAR) ;
130 IF PutCh (a
[l
]) # a
[l
]
132 Halt ('assert failed', __FILE__
, __FUNCTION__
, __LINE__
)
139 PutCh - pushes a character onto the push back stack, it also
140 returns the character which has been pushed.
143 PROCEDURE PutCh (ch
: CHAR) : CHAR ;
145 IF StackPtr
<MaxPushBackStack
147 CharStack
[StackPtr
] := ch
;
150 Halt('max push back stack exceeded, increase MaxPushBackStack',
151 __FILE__
, __FUNCTION__
, __LINE__
)
158 Open - opens a file for reading.
161 PROCEDURE Open (a
: ARRAY OF CHAR) : File
;
164 StrCopy(a
, FileName
) ;
165 RETURN( OpenToRead(a
) )
170 Close - closes the opened file.
173 PROCEDURE Close (f
: File
) ;
180 ErrChar - writes a char, ch, to stderr.
183 PROCEDURE ErrChar (ch
: CHAR) ;
185 WriteChar(StdErr
, ch
)
190 Error - emits an error message with the appropriate file, line combination.
193 PROCEDURE Error (a
: ARRAY OF CHAR) ;
195 PushOutput(ErrChar
) ;
196 WriteString(FileName
) ; Write(':') ; WriteCard(LineNo
, 0) ; Write(':') ; WriteString(a
) ; WriteLn
;
204 WarnError - emits an error message with the appropriate file, line combination.
205 It does not terminate but when the program finishes an exit status of
209 PROCEDURE WarnError (a
: ARRAY OF CHAR) ;
211 PushOutput(ErrChar
) ;
212 WriteString(FileName
) ; Write(':') ; WriteCard(LineNo
, 0) ; Write(':') ; WriteString(a
) ; WriteLn
;
219 WarnString - emits an error message with the appropriate file, line combination.
220 It does not terminate but when the program finishes an exit status of
224 PROCEDURE WarnString (s
: String
) ;
226 p
: POINTER TO CHAR ;
229 WriteString(FileName
) ; Write(':') ; WriteCard(LineNo
, 0) ; Write(':') ;
236 WriteString(FileName
) ; Write(':') ; WriteCard(LineNo
, 0) ; Write(':')
242 UNTIL (p
=NIL) OR (p^
=nul
) ;
248 GetExitStatus - returns the exit status which will be 1 if any warnings were issued.
251 PROCEDURE GetExitStatus () : CARDINAL ;
258 SetDebug - sets the debug flag on or off.
261 PROCEDURE SetDebug (d
: BOOLEAN) ;
268 GetColumnPosition - returns the column position of the current character.
271 PROCEDURE GetColumnPosition () : CARDINAL ;
277 RETURN( Column
-StackPtr
)
279 END GetColumnPosition
;
283 GetCurrentLine - returns the current line number.
286 PROCEDURE GetCurrentLine () : CARDINAL ;
293 Init - initialize global variables.