No subject

Robert_William HUTTON rwh at ecr.mu.oz.au
Thu Feb 3 15:36:42 AEDT 2000


Hi,

If no-one has any problems with this diff, I'll commit it tomorrow.

===================================================================

This change implements predicates for accessing the offset into a
source file as it is parsed or output.

Estimated hours taken: 2

./NEWS
	Report added predicates.

runtime/mercury_library_types.h:
	Add a field to the mercury_file struct to store the offset
	into the file.

library/io.m:
	Add predicates to allow access to the offset field.  Note
	that these are just modified versions of their equivalents
	for accessing the line_number.



Index: NEWS
===================================================================
RCS file: /home/mercury1/repository/mercury/NEWS,v
retrieving revision 1.160
diff -u -b -r1.160 NEWS
--- NEWS	2000/01/25 04:09:36	1.160
+++ NEWS	2000/01/31 10:56:19
@@ -209,6 +209,17 @@
   `integer__xor/2', and will be removed in a future release.
   The operator `^' will be used by record syntax.
 
+* Added predicates to the io module for returning the offset into a text
+  file as it is being parsed or output.  They are:
+  	io__get_offset/3
+	io__get_offset/4
+	io__set_offset/3
+	io__set_offset/4
+	io__get_output_offset/3
+	io__get_output_offset/4
+	io__set_output_offset/3
+	io__set_output_offset/4
+
 New library packages in the Mercury extras distribution:
 ********************************************************
 
Index: ./runtime/mercury_library_types.h
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_library_types.h,v
retrieving revision 1.3
diff -u -b -r1.3 mercury_library_types.h
--- ./runtime/mercury_library_types.h	1999/10/18 15:46:56	1.3
+++ ./runtime/mercury_library_types.h	2000/01/26 10:44:40
@@ -26,6 +26,7 @@
 typedef struct mercury_file {
 	FILE *file;
 	int line_number;
+	int offset;
 } MercuryFile;
 
 /*
Index: ./library/io.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/io.m,v
retrieving revision 1.193
diff -u -b -r1.193 io.m
--- ./library/io.m	2000/01/08 06:42:51	1.193
+++ ./library/io.m	2000/01/31 10:03:26
@@ -472,6 +472,26 @@
 :- mode io__set_line_number(in, in, di, uo) is det.
 %	Set the line number of the specified input stream.
 
+:- pred io__get_offset(int, io__state, io__state).
+:- mode io__get_offset(out, di, uo) is det.
+%	Return the offset of the current input stream.
+%	Offsets are normally numbered starting at 0
+%	(but this can be overridden by calling io__set_offset).
+
+:- pred io__get_offset(io__input_stream, int, io__state, io__state).
+:- mode io__get_offset(in, out, di, uo) is det.
+%	Return the offset of the specified input stream.
+%	Offsets are normally numbered starting at 0
+%	(but this can be overridden by calling io__set_offset).
+
+:- pred io__set_offset(int, io__state, io__state).
+:- mode io__set_offset(in, di, uo) is det.
+%	Set the offset of the current input stream.
+
+:- pred io__set_offset(io__input_stream, int, io__state, io__state).
+:- mode io__set_offset(in, in, di, uo) is det.
+%	Set the offset of the specified input stream.
+
 %-----------------------------------------------------------------------------%
 
 % Output text stream predicates.
@@ -573,6 +593,26 @@
 :- mode io__set_output_line_number(in, in, di, uo) is det.
 %	Set the line number of the specified output stream.
 
+:- pred io__get_output_offset(int, io__state, io__state).
+:- mode io__get_output_offset(out, di, uo) is det.
+%	Return the offset of the current output stream.
+%	Offsets are normally numbered starting at 0
+%	(but this can be overridden by calling io__set_output_offset).
+
+:- pred io__get_output_offset(io__output_stream, int, io__state, io__state).
+:- mode io__get_output_offset(in, out, di, uo) is det.
+%	Return the offset of the specified output stream.
+%	Offsets are normally numbered starting at 0
+%	(but this can be overridden by calling io__set_output_offset).
+
+:- pred io__set_output_offset(int, io__state, io__state).
+:- mode io__set_output_offset(in, di, uo) is det.
+%	Set the offset of the current output stream.
+
+:- pred io__set_output_offset(io__output_stream, int, io__state, io__state).
+:- mode io__set_output_offset(in, in, di, uo) is det.
+%	Set the offset of the specified output stream.
+
 
 %-----------------------------------------------------------------------------%
 
@@ -2738,6 +2778,7 @@
 	mf = MR_GC_NEW(MercuryFile);
 	mf->file = f;
 	mf->line_number = 1;
+	mf->offset = 0;
 	return mf;
 }
 
@@ -2797,6 +2838,7 @@
 		if (*s++ == '\\n') {
 			mf->line_number++;
 		}
+	mf->offset++;
 	}
 }
 
@@ -2823,6 +2865,7 @@
 	if (c == '\\n') {
 		mf->line_number++;
 	}
+	mf->offset++;
 	return c;
 }
 
@@ -2865,6 +2908,7 @@
 	if (ungetc(Character, mf->file) == EOF) {
 		mercury_io_error(mf, ""io__putback_char: ungetc failed"");
 	}
+	mf->offset--;
 	update_io(IO0, IO);
 }").
 
@@ -2894,6 +2938,7 @@
 	if (Character == '\\n') {
 		mercury_current_text_output->line_number++;
 	}
+	mercury_current_text_output->offset++;
 	update_io(IO0, IO);
 ").
 
@@ -3000,6 +3045,7 @@
 	if (Character == '\\n') {
 		stream->line_number++;
 	}
+	stream->offset++;
 	update_io(IO0, IO);
 }").
 
@@ -3172,6 +3218,63 @@
 	update_io(IO0, IO);
 }").
 	
+
+:- pragma c_code(io__get_offset(Offset::out, IO0::di, IO::uo),
+		will_not_call_mercury, "
+	Offset = mercury_current_text_input->offset;
+	update_io(IO0, IO);
+").
+	
+:- pragma c_code(
+	io__get_offset(Stream::in, Offset::out, IO0::di, IO::uo),
+		will_not_call_mercury, "{
+	MercuryFile *stream = (MercuryFile *) Stream;
+	Offset = stream->offset;
+	update_io(IO0, IO);
+}").
+	
+:- pragma c_code(io__set_offset(Offset::in, IO0::di, IO::uo),
+		will_not_call_mercury, "
+	mercury_current_text_input->offset = Offset;
+	update_io(IO0, IO);
+").
+	
+:- pragma c_code(
+	io__set_offset(Stream::in, Offset::in, IO0::di, IO::uo),
+		will_not_call_mercury, "{
+	MercuryFile *stream = (MercuryFile *) Stream;
+	stream->offset = Offset;
+	update_io(IO0, IO);
+}").
+	
+:- pragma c_code(io__get_output_offset(Offset::out, IO0::di, IO::uo),
+		will_not_call_mercury, "
+	Offset = mercury_current_text_output->offset;
+	update_io(IO0, IO);
+").
+	
+:- pragma c_code(
+	io__get_output_offset(Stream::in, Offset::out, IO0::di, IO::uo),
+		will_not_call_mercury, "{
+	MercuryFile *stream = (MercuryFile *) Stream;
+	Offset = stream->offset;
+	update_io(IO0, IO);
+}").
+
+:- pragma c_code(io__set_output_offset(Offset::in, IO0::di, IO::uo),
+		will_not_call_mercury, "
+	mercury_current_text_output->offset = Offset;
+	update_io(IO0, IO);
+").
+	
+:- pragma c_code(
+	io__set_output_offset(Stream::in, Offset::in, IO0::di, IO::uo),
+		will_not_call_mercury, "{
+	MercuryFile *stream = (MercuryFile *) Stream;
+	stream->offset = Offset;
+	update_io(IO0, IO);
+}").
+
 % io__set_input_stream(NewStream, OldStream, IO0, IO1)
 %	Changes the current input stream to the stream specified.
 %	Returns the previous stream.

-- 
Robert Hutton <rwh at ecr.mu.oz.au>     | War doesn't demonstrate who's
WWW: <http://www.ecr.mu.oz.au/~rwh>  | right... just who's left.

--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list