for review: term_io quoting bug fix

Fergus Henderson fjh at cs.mu.OZ.AU
Mon Nov 9 12:49:32 AEDT 1998


Tom, can you please review this one?

--------------------

Estimated hours taken: 0.5

Fix a bug reported by Tom Conway where term_io__write_term, io__write
and io__write_binary were not properly quoting graphic tokens.

library/term_io.m:
library/io.m:
	Change term_io__write_term and io__write to quote graphic
	tokens where necessary, so that the output will parse correctly
	when followed by ".\n".

tests/hard_coded/write.m:
tests/hard_coded/write.exp:
	Add a new test to test the changes to io__write,
	and update the expected output for one of the existing tests.

Index: library/io.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/io.m,v
retrieving revision 1.169
diff -u -r1.169 io.m
--- io.m	1998/11/05 15:19:22	1.169
+++ io.m	1998/11/09 01:01:24
@@ -1966,7 +1966,8 @@
 			term_io__quote_atom(Functor),
 			io__write_char(')')
 		;
-			term_io__quote_atom(Functor)
+			term_io__quote_atom(Functor,
+				maybe_adjacent_to_graphic_token)
 		),
 		(
 			{ Args = [X|Xs] }
Index: library/term_io.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/term_io.m,v
retrieving revision 1.53
diff -u -r1.53 term_io.m
--- term_io.m	1998/03/03 17:26:09	1.53
+++ term_io.m	1998/11/09 01:26:47
@@ -55,7 +55,8 @@
 
 :- pred term_io__write_constant(const, io__state, io__state).
 :- mode term_io__write_constant(in, di, uo) is det.
-%		Writes a constant (integer, float, or atom) to stdout.
+%		Writes a constant (integer, float, string, or atom)
+%		to stdout.
 
 :- pred term_io__write_variable(var, varset, io__state, io__state).
 :- mode term_io__write_variable(in, in, di, uo) is det.
@@ -94,6 +95,24 @@
 :- mode term_io__quote_single_char(in, di, uo) is det.
 
 %-----------------------------------------------------------------------------%
+:- implementation.
+
+% Everything below here is not intended to be part of the public interface,
+% and will not be included in the Mercury library reference manual.
+
+%-----------------------------------------------------------------------------%
+:- interface.
+
+	% for use by io.m.
+
+:- type adjacent_to_graphic_token
+	--->	maybe_adjacent_to_graphic_token
+	;	not_adjacent_to_graphic_token.
+
+:- pred term_io__quote_atom(string, adjacent_to_graphic_token,
+		io__state, io__state).
+:- mode term_io__quote_atom(in, in, di, uo) is det.
+
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
 
@@ -275,7 +294,8 @@
 			term_io__write_constant(Functor),
 			io__write_char(')')
 		;
-			term_io__write_constant(Functor)
+			term_io__write_constant(Functor,
+				maybe_adjacent_to_graphic_token)
 		),
 		(
 			{ Args = [X|Xs] }
@@ -348,14 +368,20 @@
 
 %-----------------------------------------------------------------------------%
 
-	% write the functor
-term_io__write_constant(term__integer(I)) -->
+term_io__write_constant(Const) -->
+	term_io__write_constant(Const, not_adjacent_to_graphic_token).
+
+:- pred term_io__write_constant(const, adjacent_to_graphic_token,
+	io__state, io__state).
+:- mode term_io__write_constant(in, in, di, uo) is det.
+
+term_io__write_constant(term__integer(I), _) -->
 	io__write_int(I).
-term_io__write_constant(term__float(F)) -->
+term_io__write_constant(term__float(F), _) -->
 	io__write_float(F).
-term_io__write_constant(term__atom(A))  -->
-	term_io__quote_atom(A).
-term_io__write_constant(term__string(S)) -->
+term_io__write_constant(term__atom(A), NextToGraphicToken)  -->
+	term_io__quote_atom(A, NextToGraphicToken).
+term_io__write_constant(term__string(S), _) -->
 	term_io__quote_string(S).
 
 %-----------------------------------------------------------------------------%
@@ -366,6 +392,9 @@
 	io__write_char('''').
 
 term_io__quote_atom(S) -->
+	term_io__quote_atom(S, not_adjacent_to_graphic_token).
+
+term_io__quote_atom(S, NextToGraphicToken) -->
 	(
 		% I didn't make these rules up: see ISO Prolog 6.3.1.3
 		% and 6.4.2.
@@ -385,7 +414,17 @@
 			{ string__to_char_list(S, Chars) },
 			{ \+ (  list__member(Char, Chars),
 				\+ lexer__graphic_token_char(Char)) },
-			{ Chars \= [] }
+			{ Chars \= [] },
+			%
+			% If the token could be the last token in a term,
+			% and the term could be followed with ".\n",
+			% then we need to quote the token, otherwise
+			% the "." would be considered part of the
+			% same graphic token.  We can only leave it
+			% unquoted if we're sure it won't be adjacent
+			% to any graphic token.
+			%
+			{ NextToGraphicToken = not_adjacent_to_graphic_token }
 		;
 			% 6.3.1.3: atom = open list, close list ;
 			{ S = "[]" }
Index: tests/hard_coded/write.m
===================================================================
RCS file: /home/mercury1/repository/tests/hard_coded/write.m,v
retrieving revision 1.4
diff -u -r1.4 write.m
--- write.m	1997/07/25 05:16:31	1.4
+++ write.m	1998/11/09 01:38:23
@@ -60,6 +60,7 @@
 	io__write(var("X") + int(3) * var("X^2") ; (type)), newline,
 	io__write({type}), newline,
 	io__write({:-}), newline,
+	io__write((:-)), newline,
 	io__write({blah}), newline,
 	io__write((blah ; (type), (type) * blah ; (type))), newline,
 	io__write(((blah ; blah), blah) * blah ; blah), newline,
Index: tests/hard_coded/write.exp
===================================================================
RCS file: /home/mercury1/repository/tests/hard_coded/write.exp,v
retrieving revision 1.7
diff -u -r1.7 write.exp
--- write.exp	1997/08/05 04:38:18	1.7
+++ write.exp	1998/11/09 01:39:39
@@ -1,6 +1,7 @@
 var("X") + int(3) * var("X^2") ; (type)
 { type }
-{ :- }
+{ ':-' }
+':-'
 { blah }
 blah ; (type), (type) * blah ; (type)
 ((blah ; blah), blah) * blah ; blah

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "Binaries may die
WWW: <http://www.cs.mu.oz.au/~fjh>  |   but source code lives forever"
PGP: finger fjh at 128.250.37.3        |     -- leaked Microsoft memo.



More information about the developers mailing list