提交 04acba79 编写于 作者: M Marijn Haverbeke

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.)
上级 001397da
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
import middle::ty; import middle::ty;
import middle::typeck; import middle::typeck;
import middle::tstate::ck; import middle::tstate::ck;
import pretty::pp;
import pretty::pprust; import pretty::pprust;
import pretty::ppaux; import pretty::ppaux;
import back::link; import back::link;
...@@ -24,6 +25,7 @@ ...@@ -24,6 +25,7 @@
import std::option::none; import std::option::none;
import std::str; import std::str;
import std::vec; import std::vec;
import std::int;
import std::io; import std::io;
import std::run; import std::run;
import std::getopts; import std::getopts;
...@@ -127,22 +129,67 @@ fn compile_input(session::session sess, ast::crate_cfg cfg, str input, ...@@ -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, fn pretty_print_input(session::session sess, ast::crate_cfg cfg,
str input, pp_mode ppm) { 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 p = front::parser::new_parser(sess, cfg, input, 0u, 0);
auto crate = parse_input(sess, p, input); auto crate = parse_input(sess, p, input);
auto mode; auto ann;
alt (ppm) { alt (ppm) {
case (ppm_typed) { case (ppm_typed) {
auto amap = middle::ast_map::map_crate(*crate); auto amap = middle::ast_map::map_crate(*crate);
auto d = resolve::resolve_crate(sess, amap, crate); auto d = resolve::resolve_crate(sess, amap, crate);
auto ty_cx = ty::mk_ctxt(sess, d._0, d._1, amap); auto ty_cx = ty::mk_ctxt(sess, d._0, d._1, amap);
typeck::check_crate(ty_cx, crate); 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(), pprust::print_crate(sess, crate, input, std::io::stdout(), ann);
mode);
} }
fn version(str argv0) { fn version(str argv0) {
......
...@@ -337,6 +337,22 @@ fn next_comment(&ps s) -> option::t[lexer::cmnt] { ...@@ -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 = type ps =
@rec(pp::printer s, @rec(pp::printer s,
option::t[codemap] cm, option::t[codemap] cm,
...@@ -345,7 +361,7 @@ fn next_comment(&ps s) -> option::t[lexer::cmnt] { ...@@ -345,7 +361,7 @@ fn next_comment(&ps s) -> option::t[lexer::cmnt] {
mutable uint cur_cmnt, mutable uint cur_cmnt,
mutable uint cur_lit, mutable uint cur_lit,
mutable vec[pp::breaks] boxes, mutable vec[pp::breaks] boxes,
mode mode); pp_ann ann);
fn ibox(&ps s, uint u) { fn ibox(&ps s, uint u) {
vec::push(s.boxes, pp::inconsistent); vec::push(s.boxes, pp::inconsistent);
...@@ -354,8 +370,6 @@ fn ibox(&ps s, uint u) { ...@@ -354,8 +370,6 @@ fn ibox(&ps s, uint u) {
fn end(&ps s) { vec::pop(s.boxes); pp::end(s.s); } 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 { fn rust_printer(io::writer writer) -> ps {
let vec[pp::breaks] boxes = []; let vec[pp::breaks] boxes = [];
ret @rec(s=pp::mk_printer(writer, default_columns), ret @rec(s=pp::mk_printer(writer, default_columns),
...@@ -365,7 +379,7 @@ fn rust_printer(io::writer writer) -> ps { ...@@ -365,7 +379,7 @@ fn rust_printer(io::writer writer) -> ps {
mutable cur_cmnt=0u, mutable cur_cmnt=0u,
mutable cur_lit=0u, mutable cur_lit=0u,
mutable boxes=boxes, mutable boxes=boxes,
mode=mo_untyped); ann=no_ann());
} }
const uint indent_unit = 4u; const uint indent_unit = 4u;
......
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
import front::codemap; import front::codemap;
import front::codemap::codemap; import front::codemap::codemap;
import front::ast; import front::ast;
import middle::ty;
import util::common; import util::common;
import option::some; import option::some;
import option::none; import option::none;
...@@ -29,7 +28,7 @@ ...@@ -29,7 +28,7 @@
import ppaux::*; import ppaux::*;
fn print_crate(session sess, @ast::crate crate, str filename, 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 = []; let vec[pp::breaks] boxes = [];
auto r = lexer::gather_comments_and_literals(sess, filename); auto r = lexer::gather_comments_and_literals(sess, filename);
auto s = auto s =
...@@ -40,7 +39,7 @@ fn print_crate(session sess, @ast::crate crate, str filename, ...@@ -40,7 +39,7 @@ fn print_crate(session sess, @ast::crate crate, str filename,
mutable cur_cmnt=0u, mutable cur_cmnt=0u,
mutable cur_lit=0u, mutable cur_lit=0u,
mutable boxes=boxes, mutable boxes=boxes,
mode=mode); ann=ann);
print_mod(s, crate.node.module, crate.node.attrs); print_mod(s, crate.node.module, crate.node.attrs);
eof(s.s); eof(s.s);
} }
...@@ -297,6 +296,8 @@ fn print_item(&ps s, &@ast::item item) { ...@@ -297,6 +296,8 @@ fn print_item(&ps s, &@ast::item item) {
hardbreak_if_not_bol(s); hardbreak_if_not_bol(s);
maybe_print_comment(s, item.span.lo); maybe_print_comment(s, item.span.lo);
print_outer_attributes(s, item.attrs); print_outer_attributes(s, item.attrs);
auto ann_node = node_item(s, item);
s.ann.pre(ann_node);
alt (item.node) { alt (item.node) {
case (ast::item_const(?ty, ?expr)) { case (ast::item_const(?ty, ?expr)) {
head(s, "const"); head(s, "const");
...@@ -470,15 +471,7 @@ fn print_field(&ps s, &ast::obj_field field) { ...@@ -470,15 +471,7 @@ fn print_field(&ps s, &ast::obj_field field) {
print_block(s, dt.body); print_block(s, dt.body);
} }
} }
s.ann.post(ann_node);
// 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 */ }
}
} }
fn print_outer_attributes(&ps s, vec[ast::attribute] attrs) { fn print_outer_attributes(&ps s, vec[ast::attribute] attrs) {
...@@ -530,6 +523,8 @@ fn print_stmt(&ps s, &ast::stmt st) { ...@@ -530,6 +523,8 @@ fn print_stmt(&ps s, &ast::stmt st) {
fn print_block(&ps s, ast::block blk) { fn print_block(&ps s, ast::block blk) {
maybe_print_comment(s, blk.span.lo); maybe_print_comment(s, blk.span.lo);
auto ann_node = node_block(s, blk);
s.ann.pre(ann_node);
bopen(s); bopen(s);
for (@ast::stmt st in blk.node.stmts) { print_stmt(s, *st) } for (@ast::stmt st in blk.node.stmts) { print_stmt(s, *st) }
alt (blk.node.expr) { alt (blk.node.expr) {
...@@ -541,15 +536,7 @@ fn print_block(&ps s, ast::block blk) { ...@@ -541,15 +536,7 @@ fn print_block(&ps s, ast::block blk) {
case (_) { } case (_) { }
} }
bclose(s, blk.span); bclose(s, blk.span);
s.ann.post(ann_node);
// 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 */ }
}
} }
fn print_if(&ps s, &@ast::expr test, &ast::block block, 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) { ...@@ -597,11 +584,8 @@ fn do_else(&ps s, option::t[@ast::expr] els) {
fn print_expr(&ps s, &@ast::expr expr) { fn print_expr(&ps s, &@ast::expr expr) {
maybe_print_comment(s, expr.span.lo); maybe_print_comment(s, expr.span.lo);
ibox(s, indent_unit); ibox(s, indent_unit);
alt (s.mode) { auto ann_node = node_expr(s, expr);
case (mo_untyped) {/* no-op */ } s.ann.pre(ann_node);
case (mo_typed(_)) { popen(s); }
case (mo_identified) { popen(s); }
}
alt (expr.node) { alt (expr.node) {
case (ast::expr_vec(?exprs, ?mut, ?kind)) { case (ast::expr_vec(?exprs, ?mut, ?kind)) {
ibox(s, indent_unit); ibox(s, indent_unit);
...@@ -926,23 +910,7 @@ fn print_opt(&ps s, &option::t[@ast::expr] expr) { ...@@ -926,23 +910,7 @@ fn print_opt(&ps s, &option::t[@ast::expr] expr) {
} }
} }
// Print the type or node ID if necessary. s.ann.post(ann_node);
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);
}
}
end(s); end(s);
} }
...@@ -960,16 +928,6 @@ fn print_decl(&ps s, &@ast::decl decl) { ...@@ -960,16 +928,6 @@ fn print_decl(&ps s, &@ast::decl decl) {
} }
case (_) { case (_) {
word_nbsp(s, "auto"); 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); word(s.s, loc.node.ident);
...@@ -1015,6 +973,8 @@ fn print_path(&ps s, &ast::path path) { ...@@ -1015,6 +973,8 @@ fn print_path(&ps s, &ast::path path) {
fn print_pat(&ps s, &@ast::pat pat) { fn print_pat(&ps s, &@ast::pat pat) {
maybe_print_comment(s, pat.span.lo); maybe_print_comment(s, pat.span.lo);
auto ann_node = node_pat(s, pat);
s.ann.pre(ann_node);
alt (pat.node) { alt (pat.node) {
case (ast::pat_wild) { word(s.s, "_"); } case (ast::pat_wild) { word(s.s, "_"); }
case (ast::pat_bind(?id)) { word(s.s, "?" + id); } case (ast::pat_bind(?id)) { word(s.s, "?" + id); }
...@@ -1028,15 +988,7 @@ fn print_pat(&ps s, &@ast::pat pat) { ...@@ -1028,15 +988,7 @@ fn print_pat(&ps s, &@ast::pat pat) {
} }
} }
} }
s.ann.post(ann_node);
// 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 */ }
}
} }
fn print_fn(&ps s, ast::fn_decl decl, ast::proto proto, str name, fn print_fn(&ps s, ast::fn_decl decl, ast::proto proto, str name,
......
...@@ -25,7 +25,6 @@ ...@@ -25,7 +25,6 @@
import pretty::pprust::print_fn; import pretty::pprust::print_fn;
import pretty::pprust::print_type; import pretty::pprust::print_type;
import pretty::ppaux::print_literal; import pretty::ppaux::print_literal;
import pretty::ppaux::mo_untyped;
import pretty::pp::mk_printer; import pretty::pp::mk_printer;
type filename = str; type filename = str;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册