diff: quoting bug fixes

Fergus Henderson fjh at cs.mu.OZ.AU
Wed May 20 23:09:23 AEST 1998


Hi Tom,

Turns out this was a bit more complicated than the 5-minute
fix we first thought...

Estimated hours taken: 6

compiler/mercury_to_mercury.m:
compiler/mode_errors.m:
	Make the code a little bit more readable:
	use specific types rather than `bool' in a few places.

compiler/mercury_to_mercury.m:
	Fix bugs in the generation of interface files where it was
	outputting certain graphic tokens incorrectly.  In particular,
	it was outputting things like `module:?' without any parentheses,
	space, or quotes to separate the `:' as module qualifier from
	the symbol name.

tests/hard_coded/Mmakefile:
tests/hard_coded/quoting_bug.m:
tests/hard_coded/quoting_bug_test.m:
tests/hard_coded/quoting_bug_test.exp:
	Add some tests for the above-mentioned bug fix.

tests/term/*.trans_opt_exp:
tests/misc_tests/pretty_print_test.exp:
	Update expected output for these tests to reflect the
	above changes.

Index: compiler/mercury_to_mercury.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mercury_to_mercury.m,v
retrieving revision 1.134
diff -u -r1.134 mercury_to_mercury.m
--- mercury_to_mercury.m	1998/05/15 07:07:21	1.134
+++ mercury_to_mercury.m	1998/05/20 12:36:56
@@ -14,6 +14,15 @@
 :- module mercury_to_mercury.
 :- interface.
 
+:- type needs_brackets
+	--->	needs_brackets		% needs brackets, if it is an op
+	;	does_not_need_brackets.	% doesn't need brackets
+
+:- type needs_quotes
+	--->	next_to_graphic_token		% needs quotes, if it
+						% is another graphic token
+	;	not_next_to_graphic_token.	% doesn't need quotes
+
 :- import_module hlds_goal, hlds_data, hlds_pred, prog_data, (inst), purity.
 :- import_module bool, std_util, list, io, varset, term.
 
@@ -109,7 +118,7 @@
 :- pred mercury_output_inst(inst, varset, io__state, io__state).
 :- mode mercury_output_inst(in, in, di, uo) is det.
 
-:- pred mercury_output_cons_id(cons_id, bool, io__state, io__state).
+:- pred mercury_output_cons_id(cons_id, needs_brackets, io__state, io__state).
 :- mode mercury_output_cons_id(in, in, di, uo) is det.
 
 :- pred mercury_output_mode(mode, varset, io__state, io__state).
@@ -150,9 +159,6 @@
 :- pred mercury_output_newline(int, io__state, io__state).
 :- mode mercury_output_newline(in, di, uo) is det.
 
-:- pred mercury_output_bracketed_constant(const, io__state, io__state).
-:- mode mercury_output_bracketed_constant(in, di, uo) is det.
-
 :- pred mercury_output_bracketed_sym_name(sym_name, io__state, io__state).
 :- mode mercury_output_bracketed_sym_name(in, di, uo) is det.
 
@@ -468,13 +474,13 @@
 	io__write_char('\t'),
 	(
 		{ Method = func_instance(Name1, Name2, Arity) },
-		io__write_string("func((")
+		io__write_string("func(")
 	;
 		{ Method = pred_instance(Name1, Name2, Arity) },
-		io__write_string("pred((")
+		io__write_string("pred(")
 	),
-	mercury_output_bracketed_sym_name(Name1),
-	io__write_string(")/"),
+	mercury_output_bracketed_sym_name(Name1, next_to_graphic_token),
+	io__write_string("/"),
 	io__write_int(Arity),
 	io__write_string(") is "),
 	mercury_output_bracketed_sym_name(Name2).
@@ -962,11 +968,11 @@
 	{ Indent2 is Indent1 + 1 },
 	( { Args = [] } ->
 		mercury_output_tabs(Indent1),
-		mercury_output_cons_id(ConsId, yes),
+		mercury_output_cons_id(ConsId, needs_brackets),
 		io__write_string("\n")
 	;
 		mercury_output_tabs(Indent1),
-		mercury_output_cons_id(ConsId, no),
+		mercury_output_cons_id(ConsId, does_not_need_brackets),
 		io__write_string("(\n"),
 		mercury_output_structured_inst_list(Args, Indent2, VarSet),
 		mercury_output_tabs(Indent1),
@@ -988,9 +994,9 @@
 mercury_output_bound_insts([], _) --> [].
 mercury_output_bound_insts([functor(ConsId, Args) | BoundInsts], VarSet) -->
 	( { Args = [] } ->
-		mercury_output_cons_id(ConsId, yes)
+		mercury_output_cons_id(ConsId, needs_brackets)
 	;
-		mercury_output_cons_id(ConsId, no),
+		mercury_output_cons_id(ConsId, does_not_need_brackets),
 		io__write_string("("),
 		mercury_output_inst_list(Args, VarSet),
 		io__write_string(")")
@@ -1002,8 +1008,8 @@
 		mercury_output_bound_insts(BoundInsts, VarSet)
 	).
 
-mercury_output_cons_id(cons(Name, _), Bracketed) -->
-	( { Bracketed = yes } ->
+mercury_output_cons_id(cons(Name, _), NeedsBrackets) -->
+	( { NeedsBrackets = needs_brackets } ->
 		mercury_output_bracketed_sym_name(Name)
 	;
 		mercury_output_sym_name(Name)
@@ -1216,7 +1222,7 @@
 	( { Name = "" } ->
 		[]
 	;
-		io__write_string(Name),
+		mercury_quote_atom(Name, next_to_graphic_token),
 		io__write_string(": ")
 	).
 
@@ -1509,56 +1515,6 @@
 mercury_output_det(erroneous) -->
 	io__write_string("erroneous").
 
-	%
-	% Use mercury_output_bracketed_sym_name/3 when the sym_name has
-	% no arguments, otherwise use mercury_output_sym_name/3.
-	%
-
-mercury_output_bracketed_sym_name(Name) -->
-	(	{ Name = qualified(ModuleName, Name2) },
-		mercury_output_bracketed_sym_name(ModuleName),
-		io__write_char(':')
-	;
-		{ Name = unqualified(Name2) }
-	),
-	mercury_output_bracketed_constant(term__atom(Name2)).
-
-:- pred mercury_output_sym_name(sym_name, io__state, io__state).
-:- mode mercury_output_sym_name(in, di, uo) is det.
-
-mercury_output_sym_name(Name) -->
-	(	{ Name = qualified(ModuleName, PredName) },
-		mercury_output_bracketed_sym_name(ModuleName),
-		io__write_char(':'),
-		mercury_quote_qualified_atom(PredName)
-	;
-		{ Name = unqualified(PredName) },
-		term_io__quote_atom(PredName)
-	).
-
-:- pred mercury_quote_qualified_atom(string, io__state, io__state).
-:- mode mercury_quote_qualified_atom(in, di, uo) is det.
-
-mercury_quote_qualified_atom(Name) -->
-	%
-	% If the symname is composed of only graphic token chars,
-	% then term_io__quote_atom will not quote it; but since
-	% ':' is a graphic token char, it needs to be quoted,
-	% otherwise the ':' would be considered part of the
-	% symbol name (e.g. "int:<" tokenizes as ["int", ":<"].)
-	%
-	(
-		{ string__to_char_list(Name, Chars) },
-		{ \+ (  list__member(Char, Chars),
-			\+ lexer__graphic_token_char(Char)) }
-	->
-		io__write_string("'"),
-		term_io__write_escaped_string(Name),
-		io__write_string("'")
-	;
-		term_io__quote_atom(Name)
-	).
-
 %-----------------------------------------------------------------------------%
 
 	% Output a clause.
@@ -1740,14 +1696,18 @@
 mercury_output_call(Name, Term, VarSet, _Indent) -->
 	(	
 		{ Name = qualified(ModuleName, PredName) },
-		mercury_output_bracketed_sym_name(ModuleName),
-		io__write_string(":")
+		mercury_output_bracketed_sym_name(ModuleName,
+			next_to_graphic_token),
+		io__write_string(":"),
+		{ term__context_init(Context0) },
+		mercury_output_term(term__functor(term__atom(PredName),
+			Term, Context0), VarSet, no, next_to_graphic_token)
 	;
-		{ Name = unqualified(PredName) }
-	),
-	{ term__context_init(Context0) },
-	mercury_output_term(term__functor(term__atom(PredName), Term, Context0),
-		VarSet, no).
+		{ Name = unqualified(PredName) },
+		{ term__context_init(Context0) },
+		mercury_output_term(term__functor(term__atom(PredName),
+			Term, Context0), VarSet, no)
+	).
 
 :- pred mercury_output_disj(goal, varset, int, io__state, io__state).
 :- mode mercury_output_disj(in, in, in, di, uo) is det.
@@ -2045,9 +2005,9 @@
 	},
 	io__write_string(":- pragma "),
 	io__write_string(PragmaName),
-	io__write_string("(("),
-	mercury_output_bracketed_sym_name(PredName),
-	io__write_string(")/"),
+	io__write_string("("),
+	mercury_output_bracketed_sym_name(PredName, next_to_graphic_token),
+	io__write_string("/"),
 	io__write_int(DeclaredArity),
 	io__write_string(").\n").
 
@@ -2149,9 +2109,18 @@
 
 	% write a term to standard output.
 
-mercury_output_term(term__variable(Var), VarSet, AppendVarnums) -->
+mercury_output_term(Term, VarSet, AppendVarnums) -->
+	mercury_output_term(Term, VarSet, AppendVarnums,
+		not_next_to_graphic_token).
+
+:- pred mercury_output_term(term, varset, bool, needs_quotes,
+				io__state, io__state).
+:- mode mercury_output_term(in, in, in, in, di, uo) is det.
+
+mercury_output_term(term__variable(Var), VarSet, AppendVarnums, _) -->
 	mercury_output_var(Var, VarSet, AppendVarnums).
-mercury_output_term(term__functor(Functor, Args, _), VarSet, AppendVarnums) -->
+mercury_output_term(term__functor(Functor, Args, _), VarSet, AppendVarnums,
+		NextToGraphicToken) -->
 	(
 	    	{ Functor = term__atom(".") },
 		{ Args = [X, Xs] }
@@ -2186,26 +2155,32 @@
 		{ mercury_infix_op(FunctorName) }
 	->
 		io__write_string("("),
-		mercury_output_term(Arg1, VarSet, AppendVarnums),
 		( { FunctorName = ":" } ->
-			io__write_string(":")
+			mercury_output_term(Arg1, VarSet, AppendVarnums,
+				next_to_graphic_token),
+			io__write_string(":"),
+			mercury_output_term(Arg2, VarSet, AppendVarnums,
+				next_to_graphic_token)
 		;
+			mercury_output_term(Arg1, VarSet, AppendVarnums,
+				not_next_to_graphic_token),
 			io__write_string(" "),
 			io__write_string(FunctorName),
-			io__write_string(" ")
+			io__write_string(" "),
+			mercury_output_term(Arg2, VarSet, AppendVarnums,
+				not_next_to_graphic_token)
 		),
-		mercury_output_term(Arg2, VarSet, AppendVarnums),
 		io__write_string(")")
 	;
 		{ Args = [Y | Ys] }
 	->
-		term_io__write_constant(Functor),
+		mercury_output_constant(Functor, NextToGraphicToken),
 		io__write_string("("),
 		mercury_output_term(Y, VarSet, AppendVarnums),
 		mercury_output_remaining_terms(Ys, VarSet, AppendVarnums),
 		io__write_string(")")
 	;
-		mercury_output_bracketed_constant(Functor)
+		mercury_output_bracketed_constant(Functor, NextToGraphicToken)
 	).
 
 :- pred mercury_output_list_args(term, varset, bool, io__state, io__state).
@@ -2281,13 +2256,117 @@
 		io__write_string(VarName)
 	).
 
+:- pred mercury_output_bracketed_constant(const, io__state, io__state).
+:- mode mercury_output_bracketed_constant(in, di, uo) is det.
+
 mercury_output_bracketed_constant(Const) -->
+	mercury_output_bracketed_constant(Const, not_next_to_graphic_token).
+
+:- pred mercury_output_bracketed_constant(const, needs_quotes,
+					io__state, io__state).
+:- mode mercury_output_bracketed_constant(in, in, di, uo) is det.
+
+mercury_output_bracketed_constant(Const, NextToGraphicToken) -->
 	( { Const = term__atom(Op), mercury_op(Op) } ->
 		io__write_string("("),
-		term_io__write_constant(Const),
+		term_io__quote_atom(Op),
 		io__write_string(")")
 	;
+		mercury_output_constant(Const, NextToGraphicToken)
+	).
+
+:- pred mercury_output_constant(const, needs_quotes, io__state, io__state).
+:- mode mercury_output_constant(in, in, di, uo) is det.
+
+mercury_output_constant(Const, NextToGraphicToken) -->
+	( { Const = term__atom(Atom) } ->
+		mercury_quote_atom(Atom, NextToGraphicToken)
+	;
 		term_io__write_constant(Const)
+	).
+
+:- pred mercury_output_bracketed_atom(string, needs_quotes,
+				io__state, io__state).
+:- mode mercury_output_bracketed_atom(in, in, di, uo) is det.
+
+mercury_output_bracketed_atom(Name, NextToGraphicToken) -->
+	( { mercury_op(Name) } ->
+		io__write_string("("),
+		term_io__quote_atom(Name),
+		io__write_string(")")
+	;
+		mercury_quote_atom(Name, NextToGraphicToken)
+	).
+
+	%
+	% Use mercury_output_bracketed_sym_name when the sym_name has
+	% no arguments, otherwise use mercury_output_sym_name.
+	%
+
+:- pred mercury_output_sym_name(sym_name, io__state, io__state).
+:- mode mercury_output_sym_name(in, di, uo) is det.
+
+mercury_output_sym_name(SymName) -->
+	mercury_output_sym_name(SymName, not_next_to_graphic_token).
+
+mercury_output_bracketed_sym_name(SymName) -->
+	mercury_output_bracketed_sym_name(SymName, not_next_to_graphic_token).
+
+:- pred mercury_output_bracketed_sym_name(sym_name, needs_quotes,
+					io__state, io__state).
+:- mode mercury_output_bracketed_sym_name(in, in, di, uo) is det.
+
+mercury_output_bracketed_sym_name(Name, NextToGraphicToken) -->
+	(	{ Name = qualified(ModuleName, Name2) },
+		io__write_char('('),
+		mercury_output_bracketed_sym_name(ModuleName,
+			next_to_graphic_token),
+		io__write_char(':'),
+		mercury_output_bracketed_atom(Name2, next_to_graphic_token),
+		io__write_char(')')
+	;
+		{ Name = unqualified(Name2) },
+		mercury_output_bracketed_atom(Name2, NextToGraphicToken)
+	).
+
+:- pred mercury_output_sym_name(sym_name, needs_quotes, io__state, io__state).
+:- mode mercury_output_sym_name(in, in, di, uo) is det.
+
+mercury_output_sym_name(Name, NextToGraphicToken) -->
+	(	{ Name = qualified(ModuleName, PredName) },
+		mercury_output_bracketed_sym_name(ModuleName,
+			next_to_graphic_token),
+		io__write_char(':'),
+		mercury_quote_atom(PredName,
+			next_to_graphic_token)
+	;
+		{ Name = unqualified(PredName) },
+		mercury_quote_atom(PredName, NextToGraphicToken)
+	).
+
+:- pred mercury_quote_atom(string, needs_quotes, io__state, io__state).
+:- mode mercury_quote_atom(in, in, di, uo) is det.
+
+mercury_quote_atom(Name, NextToGraphicToken) -->
+	%
+	% If the symname is composed of only graphic token chars,
+	% then term_io__quote_atom will not quote it; but if
+	% it is next another graphic token, it needs to be quoted,
+	% otherwise the two would be considered part of one
+	% symbol name (e.g. In "int:<", the ":<" parses as one token,
+	% so when writing out the "<" after the ":" we need to quote it.
+	%
+	(
+		{ NextToGraphicToken = next_to_graphic_token },
+		{ string__to_char_list(Name, Chars) },
+		{ \+ (  list__member(Char, Chars),
+			\+ lexer__graphic_token_char(Char)) }
+	->
+		io__write_string("'"),
+		term_io__write_escaped_string(Name),
+		io__write_string("'")
+	;
+		term_io__quote_atom(Name)
 	).
 
 %-----------------------------------------------------------------------------%
Index: compiler/mode_errors.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mode_errors.m,v
retrieving revision 1.57
diff -u -r1.57 mode_errors.m
--- mode_errors.m	1998/03/03 17:35:11	1.57
+++ mode_errors.m	1998/05/20 06:46:00
@@ -722,13 +722,13 @@
 		io__write_string("'\n"),
 		prog_out__write_context(Context),
 		io__write_string("  has instantiatedness `"),
-		mercury_output_cons_id(ConsId, no),
+		mercury_output_cons_id(ConsId, does_not_need_brackets),
 		io__write_string("("),
 		output_inst_list(ArgInsts, InstVarSet),
 		io__write_string(")")
 	;
 		io__write_string("' has instantiatedness `"),
-		mercury_output_cons_id(ConsId, no)
+		mercury_output_cons_id(ConsId, does_not_need_brackets)
 	),
 	io__write_string("'.\n").
 
Index: tests/hard_coded/Mmakefile
===================================================================
RCS file: /home/mercury1/repository/tests/hard_coded/Mmakefile,v
retrieving revision 1.28
diff -u -r1.28 Mmakefile
--- Mmakefile	1998/05/04 05:15:47	1.28
+++ Mmakefile	1998/05/20 07:40:27
@@ -59,6 +59,7 @@
 	qual_adv_test \
 	qual_basic_test \
 	qual_is_test \
+	quoting_bug_test \
 	rational_test \
 	relation_test \
 	remove_file \
Index: tests/hard_coded/quoting_bug.m
===================================================================
RCS file: quoting_bug.m
diff -N quoting_bug.m
--- /dev/null	Wed May 20 23:05:01 1998
+++ quoting_bug.m	Fri May 15 17:21:10 1998
@@ -0,0 +1,4 @@
+:- module quoting_bug.
+:- interface.
+:- type token ---> '?' ; ('+') ; (*) ; && ; += ; -= .
+:- inst '?' ---> '?' ; ('+') ; (*) ; && ; += ; -= .
Index: tests/hard_coded/quoting_bug_test.exp
===================================================================
RCS file: quoting_bug_test.exp
diff -N quoting_bug_test.exp
--- /dev/null	Wed May 20 23:05:01 1998
+++ quoting_bug_test.exp	Wed May 20 17:40:05 1998
@@ -0,0 +1,6 @@
+*
+&&
+-=
++=
+?
+* && -= += ?
Index: tests/hard_coded/quoting_bug_test.m
===================================================================
RCS file: quoting_bug_test.m
diff -N quoting_bug_test.m
--- /dev/null	Wed May 20 23:05:01 1998
+++ quoting_bug_test.m	Wed May 20 16:55:04 1998
@@ -0,0 +1,24 @@
+:- module quoting_bug_test.
+:- interface.
+:- import_module io.
+
+:- pred main(state::di, state::uo) is det.
+
+:- implementation.
+:- import_module quoting_bug, list.
+
+main -->
+	write_token(*), nl,
+	write_token(&&), nl,
+	write_token(-=), nl,
+	write_token(+=), nl,
+	write_token(?), nl,
+	test([*, &&, -=, +=, ?]).
+
+:- pred write_token(token::in, state::di, state::uo) is det.
+write_token(T) --> write(T).
+
+:- pred test(list(token)::in, state::di, state::uo) is det.
+test(List) -->
+	write_list(List, " ", write_token), nl.
+
Index: tests/misc_tests/pretty_print_test.exp
===================================================================
RCS file: /home/mercury1/repository/tests/misc_tests/pretty_print_test.exp,v
retrieving revision 1.2
diff -u -r1.2 pretty_print_test.exp
--- pretty_print_test.exp	1998/04/27 11:07:54	1.2
+++ pretty_print_test.exp	1998/05/20 13:03:27
@@ -5,11 +5,11 @@
 :- mode pretty_print_test:main(di, uo) is det.
 :- implementation.
 :- type (pretty_print_test:foobar)
-	--->	pretty_print_test:foo
+	--->	(pretty_print_test:foo)
 	;	pretty_print_test:bar(int)
 	;	pretty_print_test:baz(int, int).
 :- type (pretty_print_test:cont(T))
-	--->	pretty_print_test:foo
+	--->	(pretty_print_test:foo)
 	;	pretty_print_test:cont(T, cont(T)).
 :- type (pretty_print_test:eq(T1, T2)) == foobar.
 pretty_print_test:main(DCG_0, DCG_2) :-
@@ -25,10 +25,10 @@
 	mode pretty_print_test:type_num_4(in, out)
 ].
 :- instance (numbered_type(int)) where [
-	func((type_num)/1) is foo_type_num,
-	func((type_num_2)/1) is foo_type_num,
-	pred((type_num_3)/2) is foo_type_num_p,
-	pred((type_num_4)/2) is foo_type_num_p
+	func(type_num/1) is foo_type_num,
+	func(type_num_2/1) is foo_type_num,
+	pred(type_num_3/2) is foo_type_num_p,
+	pred(type_num_4/2) is foo_type_num_p
 ].
 :- func pretty_print_test:foo_type_num(int) = int.
 pretty_print_test:foo_type_num(V_1) = 42.
Index: tests/term/ack.trans_opt_exp
===================================================================
RCS file: /home/mercury1/repository/tests/term/ack.trans_opt_exp,v
retrieving revision 1.1
diff -u -r1.1 ack.trans_opt_exp
--- ack.trans_opt_exp	1997/12/22 09:57:46	1.1
+++ ack.trans_opt_exp	1998/05/20 13:02:47
@@ -1,2 +1,2 @@
 :- module ack.
-:- pragma termination_info(ack:ack(mercury_builtin:in, mercury_builtin:in, mercury_builtin:out), infinite, can_loop).
+:- pragma termination_info(ack:ack((mercury_builtin:in), (mercury_builtin:in), (mercury_builtin:out)), infinite, can_loop).

All the other changes to tests/term/*.trans_opt_exp are the same,
I've deleted them since they're very boring.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3        |     -- the last words of T. S. Garp.



More information about the developers mailing list