Improve handling of trailing comments.

This commit is contained in:
Graydon Hoare 2011-05-30 16:22:58 -07:00
parent b48cab962a
commit cf57553679
2 changed files with 30 additions and 4 deletions

View file

@ -5,6 +5,8 @@ import std::option;
import driver::session::session;
import front::ast;
import front::lexer;
import front::codemap;
import front::codemap::codemap;
import middle::ty;
import util::common;
import pp;
@ -34,6 +36,7 @@ tag mode {
}
type ps = @rec(pp::printer s,
option::t[codemap] cm,
option::t[vec[lexer::cmnt]] comments,
mutable uint cur_cmnt,
mode mode);
@ -42,6 +45,7 @@ fn print_file(session sess, ast::_mod _mod, str filename, io::writer out,
mode mode) {
auto cmnts = lexer::gather_comments(sess, filename);
auto s = @rec(s=pp::mk_printer(out, default_columns),
cm=option::some[codemap](sess.get_codemap()),
comments=option::some[vec[lexer::cmnt]](cmnts),
mutable cur_cmnt=0u,
mode=mode);
@ -52,6 +56,7 @@ fn print_file(session sess, ast::_mod _mod, str filename, io::writer out,
fn ty_to_str(&@ast::ty ty) -> str {
auto writer = io::string_writer();
auto s = @rec(s=pp::mk_printer(writer.get_writer(), default_columns),
cm=option::none[codemap],
comments=option::none[vec[lexer::cmnt]],
mutable cur_cmnt=0u,
mode=mo_untyped);
@ -63,6 +68,7 @@ fn ty_to_str(&@ast::ty ty) -> str {
fn block_to_str(&ast::block blk) -> str {
auto writer = io::string_writer();
auto s = @rec(s=pp::mk_printer(writer.get_writer(), default_columns),
cm=option::none[codemap],
comments=option::none[vec[lexer::cmnt]],
mutable cur_cmnt=0u,
mode=mo_untyped);
@ -76,6 +82,7 @@ fn block_to_str(&ast::block blk) -> str {
fn pat_to_str(&@ast::pat p) -> str {
auto writer = io::string_writer();
auto s = @rec(s=pp::mk_printer(writer.get_writer(), default_columns),
cm=option::none[codemap],
comments=option::none[vec[lexer::cmnt]],
mutable cur_cmnt=0u,
mode=mo_untyped);
@ -1099,9 +1106,22 @@ fn maybe_print_comment(ps s, uint pos) {
}
fn maybe_print_line_comment(ps s, common::span span) -> bool {
auto cm;
alt (s.cm) {
case (option::some[codemap](?ccm)) {
cm = ccm;
}
case (_) {
ret false;
}
}
alt (next_comment(s)) {
case (option::some[lexer::cmnt](?cmnt)) {
if (span.hi + 4u >= cmnt.pos) {
if (cmnt.style != lexer::trailing) { ret false; }
auto span_line = codemap::lookup_pos(cm, span.hi);
auto comment_line = codemap::lookup_pos(cm, cmnt.pos);
if (span_line.line == comment_line.line) {
word(s.s, " ");
print_comment(s, cmnt);
s.cur_cmnt += 1u;
@ -1128,12 +1148,11 @@ fn print_remaining_comments(ps s) {
fn print_comment(ps s, lexer::cmnt cmnt) {
alt (cmnt.style) {
case (lexer::isolated) {
cbox(s.s, 0u);
zerobreak(s.s);
for (str line in cmnt.lines) {
zerobreak(s.s);
word_and_eol(s.s, line);
zerobreak(s.s);
}
end(s.s);
zerobreak(s.s);
}
case (lexer::trailing) {

View file

@ -8,6 +8,7 @@ import std::option::some;
import front::ast;
import front::ast::ty;
import front::ast::pat;
import front::codemap::codemap;
import middle::walk;
import std::io::stdout;
@ -129,6 +130,7 @@ fn expr_to_str(&@ast::expr e) -> str {
let str_writer s = string_writer();
auto out_ = mk_printer(s.get_writer(), 80u);
auto out = @rec(s=out_,
cm=none[codemap],
comments=none[vec[front::lexer::cmnt]],
mutable cur_cmnt=0u,
mode=mo_untyped);
@ -140,6 +142,7 @@ fn ty_to_str(&ty t) -> str {
let str_writer s = string_writer();
auto out_ = mk_printer(s.get_writer(), 80u);
auto out = @rec(s=out_,
cm=none[codemap],
comments=none[vec[front::lexer::cmnt]],
mutable cur_cmnt=0u,
mode=mo_untyped);
@ -167,6 +170,7 @@ fn block_to_str(&ast::block b) -> str {
let str_writer s = string_writer();
auto out_ = mk_printer(s.get_writer(), 80u);
auto out = @rec(s=out_,
cm=none[codemap],
comments=none[vec[front::lexer::cmnt]],
mutable cur_cmnt=0u,
mode=mo_untyped);
@ -179,6 +183,7 @@ fn item_to_str(&@ast::item i) -> str {
let str_writer s = string_writer();
auto out_ = mk_printer(s.get_writer(), 80u);
auto out = @rec(s=out_,
cm=none[codemap],
comments=none[vec[front::lexer::cmnt]],
mutable cur_cmnt=0u,
mode=mo_untyped);
@ -202,6 +207,7 @@ fn fun_to_str(&ast::_fn f, str name, vec[ast::ty_param] params) -> str {
let str_writer s = string_writer();
auto out_ = mk_printer(s.get_writer(), 80u);
auto out = @rec(s=out_,
cm=none[codemap],
comments=none[vec[front::lexer::cmnt]],
mutable cur_cmnt=0u,
mode=mo_untyped);
@ -222,6 +228,7 @@ fn stmt_to_str(&ast::stmt st) -> str {
let str_writer s = string_writer();
auto out_ = mk_printer(s.get_writer(), 80u);
auto out = @rec(s=out_,
cm=none[codemap],
comments=none[vec[front::lexer::cmnt]],
mutable cur_cmnt=0u,
mode=mo_untyped);