From 04acba7968e13c51b0ce2125e7fc749b4851ed9c Mon Sep 17 00:00:00 2001 From: Marijn Haverbeke Date: Tue, 5 Jul 2011 10:38:35 +0200 Subject: [PATCH] Move pretty-printing 'modes' into a callback hook This way, the pretty-printer does not have to know about middle::ty. (This is a preparation for separating the AST functionality into a separate crate.) --- src/comp/driver/rustc.rs | 59 ++++++++++++++++++++++++++---- src/comp/pretty/ppaux.rs | 22 +++++++++--- src/comp/pretty/pprust.rs | 76 ++++++++------------------------------- src/comp/util/common.rs | 1 - 4 files changed, 85 insertions(+), 73 deletions(-) diff --git a/src/comp/driver/rustc.rs b/src/comp/driver/rustc.rs index fc3cb7ce542..10f374d2392 100644 --- a/src/comp/driver/rustc.rs +++ b/src/comp/driver/rustc.rs @@ -12,6 +12,7 @@ import middle::ty; import middle::typeck; import middle::tstate::ck; +import pretty::pp; import pretty::pprust; import pretty::ppaux; import back::link; @@ -24,6 +25,7 @@ import std::option::none; import std::str; import std::vec; +import std::int; import std::io; import std::run; import std::getopts; @@ -127,22 +129,67 @@ fn compile_input(session::session sess, ast::crate_cfg cfg, str input, fn pretty_print_input(session::session sess, ast::crate_cfg cfg, str input, pp_mode ppm) { + fn ann_paren_for_expr(&ppaux::ann_node node) { + alt (node) { + case (ppaux::node_expr(?s, ?expr)) { + pprust::popen(s); + } + case (_) {} + } + } + fn ann_typed_post(&ty::ctxt tcx, &ppaux::ann_node node) { + alt (node) { + case (ppaux::node_expr(?s, ?expr)) { + pp::space(s.s); + pp::word(s.s, "as"); + pp::space(s.s); + pp::word(s.s, ppaux::ty_to_str(tcx, ty::expr_ty(tcx, expr))); + pprust::pclose(s); + } + case (_) {} + } + } + fn ann_identified_post(&ppaux::ann_node node) { + alt (node) { + case (ppaux::node_item(?s, ?item)) { + pp::space(s.s); + pprust::synth_comment(s, int::to_str(item.id, 10u)); + } + case (ppaux::node_block(?s, ?blk)) { + pp::space(s.s); + pprust::synth_comment(s, "block " + + int::to_str(blk.node.id, 10u)); + } + case (ppaux::node_expr(?s, ?expr)) { + pp::space(s.s); + pprust::synth_comment(s, int::to_str(expr.id, 10u)); + pprust::pclose(s); + } + case (_) {} + } + } + auto p = front::parser::new_parser(sess, cfg, input, 0u, 0); auto crate = parse_input(sess, p, input); - auto mode; + auto ann; alt (ppm) { case (ppm_typed) { auto amap = middle::ast_map::map_crate(*crate); auto d = resolve::resolve_crate(sess, amap, crate); auto ty_cx = ty::mk_ctxt(sess, d._0, d._1, amap); typeck::check_crate(ty_cx, crate); - mode = ppaux::mo_typed(ty_cx); + ann = rec(pre=ann_paren_for_expr, + post=bind ann_typed_post(ty_cx, _)); + } + case (ppm_identified) { + ann = rec(pre=ann_paren_for_expr, + post=ann_identified_post); + } + case (ppm_normal) { + ann = ppaux::no_ann(); } - case (ppm_normal) { mode = ppaux::mo_untyped; } - case (ppm_identified) { mode = ppaux::mo_identified; } } - pprust::print_crate(sess, crate, input, std::io::stdout(), - mode); + pprust::print_crate(sess, crate, input, std::io::stdout(), ann); } fn version(str argv0) { diff --git a/src/comp/pretty/ppaux.rs b/src/comp/pretty/ppaux.rs index 396d37a8090..ea4136182ba 100644 --- a/src/comp/pretty/ppaux.rs +++ b/src/comp/pretty/ppaux.rs @@ -337,6 +337,22 @@ fn next_comment(&ps s) -> option::t[lexer::cmnt] { } } +// The ps is stored here to prevent recursive type. +// FIXME use a nominal tag instead +tag ann_node { + node_block(ps, ast::block); + node_item(ps, @ast::item); + node_expr(ps, @ast::expr); + node_pat(ps, @ast::pat); +} +type pp_ann = rec(fn(&ann_node node) pre, + fn(&ann_node node) post); + +fn no_ann() -> pp_ann { + fn ignore(&ann_node node) {} + ret rec(pre=ignore, post=ignore); +} + type ps = @rec(pp::printer s, option::t[codemap] cm, @@ -345,7 +361,7 @@ fn next_comment(&ps s) -> option::t[lexer::cmnt] { mutable uint cur_cmnt, mutable uint cur_lit, mutable vec[pp::breaks] boxes, - mode mode); + pp_ann ann); fn ibox(&ps s, uint u) { vec::push(s.boxes, pp::inconsistent); @@ -354,8 +370,6 @@ fn ibox(&ps s, uint u) { fn end(&ps s) { vec::pop(s.boxes); pp::end(s.s); } -tag mode { mo_untyped; mo_typed(ctxt); mo_identified; } - fn rust_printer(io::writer writer) -> ps { let vec[pp::breaks] boxes = []; ret @rec(s=pp::mk_printer(writer, default_columns), @@ -365,7 +379,7 @@ fn rust_printer(io::writer writer) -> ps { mutable cur_cmnt=0u, mutable cur_lit=0u, mutable boxes=boxes, - mode=mo_untyped); + ann=no_ann()); } const uint indent_unit = 4u; diff --git a/src/comp/pretty/pprust.rs b/src/comp/pretty/pprust.rs index 04f7522a7d8..104362aa26e 100644 --- a/src/comp/pretty/pprust.rs +++ b/src/comp/pretty/pprust.rs @@ -10,7 +10,6 @@ import front::codemap; import front::codemap::codemap; import front::ast; -import middle::ty; import util::common; import option::some; import option::none; @@ -29,7 +28,7 @@ import ppaux::*; fn print_crate(session sess, @ast::crate crate, str filename, - io::writer out, mode mode) { + io::writer out, &pp_ann ann) { let vec[pp::breaks] boxes = []; auto r = lexer::gather_comments_and_literals(sess, filename); auto s = @@ -40,7 +39,7 @@ fn print_crate(session sess, @ast::crate crate, str filename, mutable cur_cmnt=0u, mutable cur_lit=0u, mutable boxes=boxes, - mode=mode); + ann=ann); print_mod(s, crate.node.module, crate.node.attrs); eof(s.s); } @@ -297,6 +296,8 @@ fn print_item(&ps s, &@ast::item item) { hardbreak_if_not_bol(s); maybe_print_comment(s, item.span.lo); print_outer_attributes(s, item.attrs); + auto ann_node = node_item(s, item); + s.ann.pre(ann_node); alt (item.node) { case (ast::item_const(?ty, ?expr)) { head(s, "const"); @@ -470,15 +471,7 @@ fn print_field(&ps s, &ast::obj_field field) { print_block(s, dt.body); } } - - // Print the node ID if necessary. TODO: type as well. - alt (s.mode) { - case (mo_identified) { - space(s.s); - synth_comment(s, int::to_str(item.id, 10u)); - } - case (_) {/* no-op */ } - } + s.ann.post(ann_node); } fn print_outer_attributes(&ps s, vec[ast::attribute] attrs) { @@ -530,6 +523,8 @@ fn print_stmt(&ps s, &ast::stmt st) { fn print_block(&ps s, ast::block blk) { maybe_print_comment(s, blk.span.lo); + auto ann_node = node_block(s, blk); + s.ann.pre(ann_node); bopen(s); for (@ast::stmt st in blk.node.stmts) { print_stmt(s, *st) } alt (blk.node.expr) { @@ -541,15 +536,7 @@ fn print_block(&ps s, ast::block blk) { case (_) { } } bclose(s, blk.span); - - // Print the node ID if necessary: TODO: type as well. - alt (s.mode) { - case (mo_identified) { - space(s.s); - synth_comment(s, "block " + int::to_str(blk.node.id, 10u)); - } - case (_) {/* no-op */ } - } + s.ann.post(ann_node); } fn print_if(&ps s, &@ast::expr test, &ast::block block, @@ -597,11 +584,8 @@ fn do_else(&ps s, option::t[@ast::expr] els) { fn print_expr(&ps s, &@ast::expr expr) { maybe_print_comment(s, expr.span.lo); ibox(s, indent_unit); - alt (s.mode) { - case (mo_untyped) {/* no-op */ } - case (mo_typed(_)) { popen(s); } - case (mo_identified) { popen(s); } - } + auto ann_node = node_expr(s, expr); + s.ann.pre(ann_node); alt (expr.node) { case (ast::expr_vec(?exprs, ?mut, ?kind)) { ibox(s, indent_unit); @@ -926,23 +910,7 @@ fn print_opt(&ps s, &option::t[@ast::expr] expr) { } } - // Print the type or node ID if necessary. - - alt (s.mode) { - case (mo_untyped) {/* no-op */ } - case (mo_typed(?tcx)) { - space(s.s); - word(s.s, "as"); - space(s.s); - word(s.s, ppaux::ty_to_str(tcx, ty::expr_ty(tcx, expr))); - pclose(s); - } - case (mo_identified) { - space(s.s); - synth_comment(s, int::to_str(expr.id, 10u)); - pclose(s); - } - } + s.ann.post(ann_node); end(s); } @@ -960,16 +928,6 @@ fn print_decl(&ps s, &@ast::decl decl) { } case (_) { word_nbsp(s, "auto"); - - // Print the type or node ID if necessary. - alt (s.mode) { - case (mo_untyped) {/* no-op */ } - case (mo_typed(?tcx)) { - auto lty = ty::node_id_to_type(tcx, loc.node.id); - word_space(s, ppaux::ty_to_str(tcx, lty)); - } - case (mo_identified) {/* no-op */ } - } } } word(s.s, loc.node.ident); @@ -1015,6 +973,8 @@ fn print_path(&ps s, &ast::path path) { fn print_pat(&ps s, &@ast::pat pat) { maybe_print_comment(s, pat.span.lo); + auto ann_node = node_pat(s, pat); + s.ann.pre(ann_node); alt (pat.node) { case (ast::pat_wild) { word(s.s, "_"); } case (ast::pat_bind(?id)) { word(s.s, "?" + id); } @@ -1028,15 +988,7 @@ fn print_pat(&ps s, &@ast::pat pat) { } } } - - // Print the node ID if necessary. TODO: type as well. - alt (s.mode) { - case (mo_identified) { - space(s.s); - synth_comment(s, int::to_str(pat.id, 10u)); - } - case (_) {/* no-op */ } - } + s.ann.post(ann_node); } fn print_fn(&ps s, ast::fn_decl decl, ast::proto proto, str name, diff --git a/src/comp/util/common.rs b/src/comp/util/common.rs index 302eec6f52c..9eca5c1f9b5 100644 --- a/src/comp/util/common.rs +++ b/src/comp/util/common.rs @@ -25,7 +25,6 @@ import pretty::pprust::print_fn; import pretty::pprust::print_type; import pretty::ppaux::print_literal; -import pretty::ppaux::mo_untyped; import pretty::pp::mk_printer; type filename = str; -- GitLab