for review: make `.opt' files more readable

Fergus Henderson fjh at cs.mu.OZ.AU
Sat Jan 24 21:00:53 AEDT 1998


Hi,

Tyson, could you please review this one?

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

Estimated hours taken: 0.75

Make the generated `.opt' and `.trans_opt' files a little bit more readable.

compiler/mercury_to_mercury.m:
	When printing out strings for `pragma c_code' 
	and `pragma c_header_code', print out literal tabs
	and newlines, rather than \t and \n.

library/term_io.m:
	Add some comments warning that changes here may require
	corresponding changes to similar code in mercury_to_mercury.m.

cvs diff  compiler/mercury_to_mercury.m library/term_io.m
Index: compiler/mercury_to_mercury.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mercury_to_mercury.m,v
retrieving revision 1.128
diff -u -r1.128 mercury_to_mercury.m
--- mercury_to_mercury.m	1998/01/21 05:41:13	1.128
+++ mercury_to_mercury.m	1998/01/24 09:46:50
@@ -171,6 +171,7 @@
 :- import_module prog_out, prog_util, hlds_pred, hlds_out, instmap.
 :- import_module globals, options, termination.
 :- import_module bool, int, string, set, term_io, lexer, std_util, require.
+:- import_module char.
 
 %-----------------------------------------------------------------------------%
 
@@ -1790,11 +1791,128 @@
 
 mercury_output_pragma_c_header(C_HeaderString) -->
 	io__write_string(":- pragma c_header_code("),
-	term_io__quote_string(C_HeaderString),
+	mercury_output_c_code_string(C_HeaderString),
 	io__write_string(").\n").
 
 %-----------------------------------------------------------------------------%
 
+% The code here is similar to the code for term_io__quote_string,
+% but \n and \t are output directly, rather than escaped.
+% Any changes here may require corresponding changes to term_io and vice versa.
+
+:- pred mercury_output_c_code_string(string::in, io__state::di, io__state::uo)
+	is det.
+mercury_output_c_code_string(S) -->
+	io__write_char('"'),
+	mercury_write_escaped_string(S),
+	io__write_char('"').
+
+:- pred mercury_write_escaped_string(string::in, io__state::di, io__state::uo)
+	is det.
+mercury_write_escaped_string(String) -->
+	string__foldl(mercury_write_escaped_char, String).
+
+:- pred mercury_write_escaped_char(char::in, io__state::di, io__state::uo)
+	is det.
+mercury_write_escaped_char(Char) -->
+	( { escape_special_char(Char, QuoteChar) } ->
+		io__write_char('\\'),
+		io__write_char(QuoteChar)
+	; { mercury_is_source_char(Char) } ->
+		io__write_char(Char)
+	;
+		{ mercury_escape_char(Char, String) },
+		io__write_string(String)
+	).
+
+:- pred mercury_escape_char(char, string).
+:- mode mercury_escape_char(in, out) is det.
+
+	% Convert a character to the corresponding octal escape code.
+
+	% XXX Note that we use C-style octal escapes rather than ISO-Prolog
+	% octal escapes.  This is for backwards compatibility with
+	% NU-Prolog and (old versions of?) SICStus Prolog.
+	% The Mercury lexer accepts either, so this should work
+	% ok so long as you don't have two escaped characters
+	% in a row :-(
+
+mercury_escape_char(Char, EscapeCode) :-
+	char__to_int(Char, Int),
+	string__int_to_base_string(Int, 8, OctalString0),
+	string__pad_left(OctalString0, '0', 3, OctalString),
+	string__first_char(EscapeCode, '\\', OctalString).
+
+:- pred mercury_is_source_char(char).
+:- mode mercury_is_source_char(in) is semidet.
+
+	% Succeed if Char is a character which is allowed in
+	% Mercury string and character literals.
+
+mercury_is_source_char(Char) :-
+	( char__is_alnum(Char)
+	; is_mercury_punctuation_char(Char)
+	; Char = '\n'
+	; Char = '\t'
+	).
+
+	% Currently we only allow the following characters.
+	% XXX should we just use is_printable(Char) instead?
+
+:- pred is_mercury_punctuation_char(char).
+:- mode is_mercury_punctuation_char(in) is semidet.
+
+is_mercury_punctuation_char(' ').
+is_mercury_punctuation_char('!').
+is_mercury_punctuation_char('@').
+is_mercury_punctuation_char('#').
+is_mercury_punctuation_char('$').
+is_mercury_punctuation_char('%').
+is_mercury_punctuation_char('^').
+is_mercury_punctuation_char('&').
+is_mercury_punctuation_char('*').
+is_mercury_punctuation_char('(').
+is_mercury_punctuation_char(')').
+is_mercury_punctuation_char('-').
+is_mercury_punctuation_char('_').
+is_mercury_punctuation_char('+').
+is_mercury_punctuation_char('=').
+is_mercury_punctuation_char('`').
+is_mercury_punctuation_char('~').
+is_mercury_punctuation_char('{').
+is_mercury_punctuation_char('}').
+is_mercury_punctuation_char('[').
+is_mercury_punctuation_char(']').
+is_mercury_punctuation_char(';').
+is_mercury_punctuation_char(':').
+is_mercury_punctuation_char('''').
+is_mercury_punctuation_char('"').
+is_mercury_punctuation_char('<').
+is_mercury_punctuation_char('>').
+is_mercury_punctuation_char('.').
+is_mercury_punctuation_char(',').
+is_mercury_punctuation_char('/').
+is_mercury_punctuation_char('?').
+is_mercury_punctuation_char('\\').
+is_mercury_punctuation_char('|').
+
+%-----------------------------------------------------------------------------%
+
+	% escape_special_char(Char, EscapeChar)
+	% is true iff Char is character for which there is a special
+	% backslash-escape character EscapeChar that can be used
+	% after a backslash in Mercury c_code string literals represent Char.
+
+:- pred escape_special_char(char, char).
+:- mode escape_special_char(in, out) is semidet.
+
+escape_special_char('''', '''').
+escape_special_char('"', '"').
+escape_special_char('\\', '\\').
+escape_special_char('\b', 'b').
+
+%-----------------------------------------------------------------------------%
+
 	% Output the given pragma source_file declaration
 :- pred mercury_output_pragma_source_file(string, io__state, io__state).
 :- mode mercury_output_pragma_source_file(in, di, uo) is det.
@@ -1812,7 +1930,7 @@
 
 mercury_output_pragma_c_body_code(C_CodeString) -->
 	io__write_string(":- pragma c_code("),
-	term_io__quote_string(C_CodeString),
+	mercury_output_c_code_string(C_CodeString),
 	io__write_string(").\n").
 
 %-----------------------------------------------------------------------------%
@@ -1855,18 +1973,18 @@
 	),
 	(
 		{ PragmaCode = ordinary(C_Code, _) },
-		term_io__quote_string(C_Code)
+		mercury_output_c_code_string(C_Code)
 	;
 		{ PragmaCode = nondet(Fields, _, First, _,
 			Later, _, Treat, Shared, _) },
 		io__write_string("local_vars("),
-		term_io__quote_string(Fields),
+		mercury_output_c_code_string(Fields),
 		io__write_string("), "),
 		io__write_string("first_code("),
-		term_io__quote_string(First),
+		mercury_output_c_code_string(First),
 		io__write_string("), "),
 		io__write_string("retry_code("),
-		term_io__quote_string(Later),
+		mercury_output_c_code_string(Later),
 		io__write_string("), "),
 		(
 			{ Treat = share },
@@ -1878,7 +1996,7 @@
 			{ Treat = automatic },
 			io__write_string("common_code(")
 		),
-		term_io__quote_string(Shared),
+		mercury_output_c_code_string(Shared),
 		io__write_string(")")
 	),
 	io__write_string(").\n").
Index: library/term_io.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/term_io.m,v
retrieving revision 1.51
diff -u -r1.51 term_io.m
--- term_io.m	1998/01/23 12:33:42	1.51
+++ term_io.m	1998/01/24 09:51:12
@@ -402,6 +402,10 @@
 		io__write_char('''')
 	).
 
+	% Note: the code here is similar to code in
+	% compiler/mercury_to_mercury.m; any changes here
+	% may require similar changes there.
+
 term_io__quote_string(S) -->
 	io__write_char('"'),
 	term_io__write_escaped_string(S),
@@ -413,6 +417,10 @@
 term_io__quote_single_char(Char) -->
 	term_io__write_escaped_char(Char).
 
+	% Note: the code here is similar to code in
+	% compiler/mercury_to_mercury.m; any changes here
+	% may require similar changes there.
+
 term_io__write_escaped_char(Char) -->
 	( { mercury_escape_special_char(Char, QuoteChar) } ->
 		io__write_char('\\'),
@@ -436,6 +444,10 @@
 	% ok so long as you don't have two escaped characters
 	% in a row :-(
 
+	% Note: the code here is similar to code in
+	% compiler/mercury_to_mercury.m; any changes here
+	% may require similar changes there.
+
 mercury_escape_char(Char, EscapeCode) :-
 	char__to_int(Char, Int),
 	string__int_to_base_string(Int, 8, OctalString0),
@@ -448,6 +460,10 @@
 	% Succeed if Char is a character which is allowed in
 	% Mercury string and character literals.
 
+	% Note: the code here is similar to code in
+	% compiler/mercury_to_mercury.m; any changes here
+	% may require similar changes there.
+
 is_mercury_source_char(Char) :-
 	( char__is_alnum(Char) ->
 		true
@@ -460,6 +476,10 @@
 	% Currently we only allow the following characters.
 	% XXX should we just use is_printable(Char) instead?
 
+	% Note: the code here is similar to code in
+	% compiler/mercury_to_mercury.m; any changes here
+	% may require similar changes there.
+
 :- pred is_mercury_punctuation_char(char).
 :- mode is_mercury_punctuation_char(in) is semidet.
 
@@ -503,6 +523,10 @@
 	% is true iff Char is character for which there is a special
 	% backslash-escape character EscapeChar that can be used
 	% after a backslash in string literals or atoms to represent Char.
+
+	% Note: the code here is similar to code in
+	% compiler/mercury_to_mercury.m; any changes here
+	% may require similar changes there.
 
 :- pred mercury_escape_special_char(char, char).
 :- mode mercury_escape_special_char(in, out) is semidet.
-- 
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