[m-rev.] diff: use the new pretty_printer module in the debugger

Peter Ross pro at missioncriticalit.com
Tue Aug 14 15:28:37 AEST 2007


Hi,

None of the test cases have been updated because ralph needs to
adjust the pretty printer to fix some issues that he has identified.


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


Estimated hours taken: 3
Branches: main

Use the pretty_print module to pretty print terms in
the debugger, rather than pprint.

browser/browse.m:
	Use the new pretty_printer.
	
browser/browser_info.m:
	Make the debugger type a member of the stream
	typeclass, so that we can use it from the new
	pretty printer.

Index: browser/browse.m
===================================================================
RCS file: /home/mercury1/repository/mercury/browser/browse.m,v
retrieving revision 1.68
diff -u -r1.68 browse.m
--- browser/browse.m	21 Dec 2006 11:11:23 -0000	1.68
+++ browser/browse.m	14 Aug 2007 05:20:48 -0000
@@ -179,7 +179,7 @@
 :- import_module int.
 :- import_module map.
 :- import_module pair.
-:- import_module pprint.
+:- import_module pretty_printer.
 :- import_module require.
 :- import_module stream.
 :- import_module stream.string_writer.
@@ -900,9 +900,8 @@
     io::di, io::uo) is det.
 
 portray_pretty(Debugger, BrowserTerm, Params, !IO) :-
-    browser_term_to_string_pretty(BrowserTerm, Params ^ width,
-        Params ^ depth, Str),
-    write_string_debugger(Debugger, Str, !IO).
+    browser_term_to_string_pretty(Debugger, BrowserTerm, Params ^ width,
+        Params ^ lines, Params ^ size, Params ^ depth, !IO).
 
 :- pred portray_raw_pretty(debugger::in, browser_term::in, format_params::in,
     io::di, io::uo) is cc_multi.
@@ -1196,20 +1195,33 @@
 %---------------------------------------------------------------------------%
 
     % Print using the pretty printer from the standard library.
-    % XXX The size of the term is not limited -- the pretty printer
-    % provides no way of doing this.
+    % XXX because the pretty printer doesn't support a combination
+    % of both size and depth, we use the depth except for when depth
+    % is 0 then we use the size.
+    %
     %
-:- pred browser_term_to_string_pretty(browser_term::in, int::in, int::in,
-    string::out) is det.
+:- pred browser_term_to_string_pretty(S::in,
+    browser_term::in, int::in, int::in,
+    int::in, int::in, io::di, io::uo) is det
+    <= stream.writer(S, string, io).
 
-browser_term_to_string_pretty(plain_term(Univ), Width, MaxDepth, Str) :-
-    Value = univ_value(Univ),
-    Doc = to_doc(MaxDepth, Value),
-    Str = to_string(Width, Doc).
-browser_term_to_string_pretty(synthetic_term(Functor, Args, MaybeReturn),
-        Width, MaxDepth, Str) :-
-    Doc = synthetic_term_to_doc(MaxDepth, Functor, Args, MaybeReturn),
-    Str = to_string(Width, Doc).
+browser_term_to_string_pretty(S, Term, Width, Lines, Size, Depth, !IO) :-
+    (
+        Term = plain_term(Univ),
+        Doc = format_univ(Univ)
+    ;
+        Term = synthetic_term(Functor, Args, MaybeReturn),
+        Doc = synthetic_term_to_doc(Functor, Args, MaybeReturn)
+    ),
+    get_default_formatter_map(Formatters, !IO),
+
+    ( Depth > 0 ->
+        Limit = triangular(Depth)
+    ;
+        Limit = linear(Size)
+    ),
+
+    format(S, Formatters, Width, Lines, Limit, Doc, !IO).
 
 %---------------------------------------------------------------------------%
 
@@ -1676,48 +1688,10 @@
     % of arguments, and if the synthetic term is a function application, then
     % the result of that function application.
     %
-:- func synthetic_term_to_doc(string, list(univ), maybe(univ))      = doc.
-:- func synthetic_term_to_doc(int, string, list(univ), maybe(univ)) = doc.
-
-synthetic_term_to_doc(Functor, Args, MaybeReturn) =
-    synthetic_term_to_doc(int.max_int, Functor, Args, MaybeReturn).
+:- func synthetic_term_to_doc(string, list(univ), maybe(univ)) = doc.
 
-synthetic_term_to_doc(Depth, Functor, Args, MaybeReturn) = Doc :-
-    Arity = list.length(Args),
-    ( Depth =< 0 ->
-        ( Arity = 0 ->
-            Doc = text(Functor)
-        ;
-            (
-                MaybeReturn = yes(_),
-                Doc = text(Functor) `<>` text("/") `<>`
-                    poly(i(Arity)) `<>` text("+1")
-            ;
-                MaybeReturn = no,
-                Doc = text(Functor) `<>` text("/") `<>` poly(i(Arity))
-            )
-        )
-    ;
-        ( Arity = 0 ->
-            Doc = text(Functor)
-        ;
-            ArgDocs = packed_cs_univ_args(Depth - 1, Args),
-            (
-                MaybeReturn = yes(Return),
-                Doc = group(
-                    text(Functor) `<>`
-                    parentheses(nest(2, ArgDocs)) `<>`
-                    nest(2, text(" = ") `<>`
-                        to_doc(Depth - 1, univ_value(Return))
-                    )
-                )
-            ;
-                MaybeReturn = no,
-                Doc = group(
-                    text(Functor) `<>` parentheses(nest(2, ArgDocs))
-                )
-            )
-        )
-    ).
+synthetic_term_to_doc(Functor, Args, no) = format_term(Functor, Args).
+synthetic_term_to_doc(Functor, Args, yes(Return)) =
+    docs([format_term(Functor, Args), str(" = "), format_univ(Return)]).
 
 %---------------------------------------------------------------------------%
Index: browser/browser_info.m
===================================================================
RCS file: /home/mercury1/repository/mercury/browser/browser_info.m,v
retrieving revision 1.33
diff -u -r1.33 browser_info.m
--- browser/browser_info.m	30 May 2007 03:49:18 -0000	1.33
+++ browser/browser_info.m	14 Aug 2007 05:20:48 -0000
@@ -27,6 +27,7 @@
 :- import_module io.
 :- import_module list.
 :- import_module maybe.
+:- import_module stream.
 :- import_module univ.
 
 %---------------------------------------------------------------------------%
@@ -283,6 +284,11 @@
     ;       browser_end_command
     ;       browser_quit.
 
+:- instance stream.stream(debugger, io).
+:- instance stream.output(debugger, io).
+:- instance stream.writer(debugger, string, io).
+:- instance stream.writer(debugger, int, io).
+
 :- pred run_param_command(debugger::in, param_cmd::in, bool::in,
     browser_info::in, browser_info::out, io::di, io::uo) is det.
 
@@ -1031,6 +1037,34 @@
 
 %---------------------------------------------------------------------------%
 
+:- instance stream.stream(debugger, io) where [
+    stream.name(_, "debugger", !IO)
+].
+
+:- instance stream.output(debugger, io) where [
+    (flush(internal, !IO) :-
+        io.flush_output(!IO)
+    ),
+    (flush(external, !IO) :-
+        % XXX
+        true
+    )
+].
+
+:- instance stream.writer(debugger, string, io) where [
+    (put(D, S, !IO) :-
+        write_string_debugger(D, S, !IO)
+    )
+].
+
+:- instance stream.writer(debugger, int, io) where [
+    (put(D, I, !IO) :-
+        write_int_debugger(D, I, !IO)
+    )
+].
+
+%---------------------------------------------------------------------------%
+
 :- pragma foreign_export("C", browser_params_to_string(in, in, out),
     "ML_BROWSE_browser_params_to_string").
 

--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list