about summary refs log tree commit diff
path: root/src/comp/pretty
diff options
context:
space:
mode:
authorBrian Anderson <andersrb@gmail.com>2011-03-13 19:40:25 -0400
committerBrian Anderson <andersrb@gmail.com>2011-03-13 19:40:25 -0400
commitbbb6836da003be71744b6e6ea7af1fd4674f8291 (patch)
treefc7780f767666cced8406818806216bee3863c91 /src/comp/pretty
parent2a662944a4d87c6d82299a181996ba14170b2ebb (diff)
parentfdc22ef1a1cef77dedb9c0429c746a81688a5979 (diff)
downloadrust-bbb6836da003be71744b6e6ea7af1fd4674f8291.tar.gz
rust-bbb6836da003be71744b6e6ea7af1fd4674f8291.zip
Merge branch 'master' into recursive-elseif
Conflicts:

	src/comp/middle/typeck.rs
Diffstat (limited to 'src/comp/pretty')
-rw-r--r--src/comp/pretty/pp.rs43
-rw-r--r--src/comp/pretty/pprust.rs75
2 files changed, 84 insertions, 34 deletions
diff --git a/src/comp/pretty/pp.rs b/src/comp/pretty/pp.rs
index 43a9220f4e5..d3145180282 100644
--- a/src/comp/pretty/pp.rs
+++ b/src/comp/pretty/pp.rs
@@ -17,17 +17,19 @@ type context = rec(contexttype tp, uint indent);
 
 type ps = @rec(mutable vec[context] context,
                uint width,
+               io.writer out,
                mutable vec[token] buffered,
                mutable uint scandepth,
                mutable uint bufferedcol,
                mutable uint col,
                mutable bool start_of_line);
 
-fn mkstate(uint width) -> ps {
+fn mkstate(io.writer out, uint width) -> ps {
   let vec[context] stack = vec(rec(tp=cx_v, indent=0u));
   let vec[token] buff = vec();
   ret @rec(mutable context=stack,
            width=width,
+           out=out,
            mutable buffered=buff,
            mutable scandepth=0u,
            mutable bufferedcol=0u,
@@ -46,10 +48,22 @@ impure fn pop_context(ps p) {
 }
 
 impure fn add_token(ps p, token tok) {
-  if (p.scandepth == 0u) {do_token(p, tok);}
+  if (p.width == 0u) {direct_token(p, tok);}
+  else if (p.scandepth == 0u) {do_token(p, tok);}
   else {buffer_token(p, tok);}
 }
 
+impure fn direct_token(ps p, token tok) {
+  alt (tok) {
+    case (brk(?sz)) {
+      while (sz > 0u) {p.out.write_str(" "); sz -= 1u;}
+    }
+    case (word(?w)) {p.out.write_str(w);}
+    case (cword(?w)) {p.out.write_str(w);}
+    case (_) {}
+  }
+}
+
 impure fn buffer_token(ps p, token tok) {
   p.buffered += vec(tok);
   p.bufferedcol += token_size(tok);
@@ -101,14 +115,13 @@ impure fn finish_block_scan(ps p, contexttype tp) {
 
 impure fn finish_break_scan(ps p) {
   if (p.bufferedcol > p.width) {
-    write_str("\n");
-    p.col = 0u;
+    line_break(p);
   }
   else {
     auto width;
     alt (p.buffered.(0)) {case(brk(?w)) {width = w;}}
     auto i = 0u;
-    while (i < width) {write_str(" "); i+=1u;}
+    while (i < width) {p.out.write_str(" "); i+=1u;}
     p.col += width;
   }
   p.scandepth = 0u;
@@ -142,20 +155,18 @@ impure fn do_token(ps p, token tok) {
           start_scan(p, tok);
         }
         case (cx_v) {
-          write_str("\n");
-          p.col = 0u;
-          p.start_of_line = true;
+          line_break(p);
         }
       }
     }
     case (word(?w)) {
       before_print(p, false);
-      write_str(w);
+      p.out.write_str(w);
       p.col += _str.byte_len(w); // TODO char_len
     }
     case (cword(?w)) {
       before_print(p, true);
-      write_str(w);
+      p.out.write_str(w);
       p.col += _str.byte_len(w); // TODO char_len
     }
     case (open(?tp, ?indent)) {
@@ -170,6 +181,12 @@ impure fn do_token(ps p, token tok) {
   }
 }
 
+impure fn line_break(ps p) {
+  p.out.write_str("\n");
+  p.col = 0u;
+  p.start_of_line = true;
+}
+
 impure fn before_print(ps p, bool closing) {
   if (p.start_of_line) {
     p.start_of_line = false;
@@ -177,14 +194,10 @@ impure fn before_print(ps p, bool closing) {
     if (closing) {ind = base_indent(p);}
     else {ind = cur_context(p).indent;}
     p.col = ind;
-    while (ind > 0u) {write_str(" "); ind -= 1u;}
+    while (ind > 0u) {p.out.write_str(" "); ind -= 1u;}
   }
 }
 
-fn write_str(str s) {
-  io.writefd(1, _str.bytes(s));
-}
-
 fn token_size(token tok) -> uint {
   alt (tok) {
     case (brk(?sz)) {ret sz;}
diff --git a/src/comp/pretty/pprust.rs b/src/comp/pretty/pprust.rs
index cab778f1732..2b64b8e4169 100644
--- a/src/comp/pretty/pprust.rs
+++ b/src/comp/pretty/pprust.rs
@@ -11,13 +11,19 @@ import foo = std.io;
 const uint indent_unit = 2u;
 const int as_prec = 5;
 
-impure fn print_ast(ast._mod _mod) {
-  auto s = pp.mkstate(80u);
+impure fn print_ast(ast._mod _mod, std.io.writer out) {
+  auto s = pp.mkstate(out, 80u);
   for (@ast.view_item vitem in _mod.view_items) {print_view_item(s, vitem);}
   line(s);
   for (@ast.item item in _mod.items) {print_item(s, item);}
 }
 
+fn ty_to_str(&@ast.ty ty) -> str {
+  auto writer = std.io.string_writer();
+  print_type(pp.mkstate(writer.get_writer(), 0u), ty);
+  ret writer.get_str();
+}
+
 impure fn hbox(ps s) {
   pp.hbox(s, indent_unit);
 }
@@ -85,24 +91,21 @@ impure fn print_type(ps s, @ast.ty ty) {
       commasep[ast.ty_field](s, fields, f);
       pclose(s);
     }
-    case (ast.ty_fn(?proto,?inputs,?output)) {
-      if (proto == ast.proto_fn) {wrd(s, "fn");}
-      else {wrd(s, "iter");}
-      popen(s);
-      impure fn print_arg(ps s, ast.ty_arg input) {
-        if (middle.ty.mode_is_alias(input.mode)) {wrd(s, "&");}
-        print_type(s, input.ty);
-      }
-      auto f = print_arg;
-      commasep[ast.ty_arg](s, inputs, f);
-      pclose(s);
-      if (output.node != ast.ty_nil) {
-        space(s);
+    case (ast.ty_obj(?methods)) {
+      wrd1(s, "obj");
+      bopen(s);
+      for (ast.ty_method m in methods) {
         hbox(s);
-        wrd1(s, "->");
-        print_type(s, output);
+        print_ty_fn(s, m.proto, option.some[str](m.ident),
+                    m.inputs, m.output);
+        wrd(s, ";");
         end(s);
+        line(s);
       }
+      bclose(s);
+    }
+    case (ast.ty_fn(?proto,?inputs,?output)) {
+      print_ty_fn(s, proto, option.none[str], inputs, output);
     }
     case (ast.ty_path(?path,_)) {
       print_path(s, path);
@@ -376,6 +379,7 @@ impure fn print_expr(ps s, @ast.expr expr) {
           wrd1(s, "else");
           print_expr(s, _else);
         }
+        case (_) { /* fall through */ }
       }
     }
     case (ast.expr_while(?test,?block,_)) {
@@ -503,8 +507,16 @@ impure fn print_expr(ps s, @ast.expr expr) {
       wrd1(s, "check");
       print_expr(s, expr);
     }
-    case (_) {wrd(s, "X");}
-    // TODO expr_ext(path, vec[@expr], option.t[@expr], @expr, ann);
+    case (ast.expr_ext(?path, ?args, ?body, _, _)) {
+      wrd(s, "#");
+      print_path(s, path);
+      if (_vec.len[@ast.expr](args) > 0u) {
+        popen(s);
+        commasep[@ast.expr](s, args, pe);
+        pclose(s);
+      }
+      // TODO: extension 'body'
+    }
   }
   end(s);
 }
@@ -706,3 +718,28 @@ fn escape_str(str st, char to_escape) -> str {
 impure fn print_string(ps s, str st) {
   wrd(s, "\""); wrd(s, escape_str(st, '"')); wrd(s, "\"");
 }
+
+impure fn print_ty_fn(ps s, ast.proto proto, option.t[str] id,
+                      vec[ast.ty_arg] inputs, @ast.ty output) {
+  if (proto == ast.proto_fn) {wrd(s, "fn");}
+  else {wrd(s, "iter");}
+  alt (id) {
+    case (option.some[str](?id)) {space(s); wrd(s, id);}
+    case (_) {}
+  }
+  popen(s);
+  impure fn print_arg(ps s, ast.ty_arg input) {
+    if (middle.ty.mode_is_alias(input.mode)) {wrd(s, "&");}
+    print_type(s, input.ty);
+  }
+  auto f = print_arg;
+  commasep[ast.ty_arg](s, inputs, f);
+  pclose(s);
+  if (output.node != ast.ty_nil) {
+    space(s);
+    hbox(s);
+    wrd1(s, "->");
+    print_type(s, output);
+    end(s);
+  }
+}