Skip to content

Commit

Permalink
Workaround to have doc comments desugared only in macros
Browse files Browse the repository at this point in the history
  • Loading branch information
pczarn committed Nov 5, 2014
1 parent 6f30a4e commit a8ce669
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/libsyntax/ext/tt/macro_rules.rs
Expand Up @@ -164,11 +164,12 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt,
_ => cx.span_fatal(sp, "malformed macro lhs")
};
// `None` is because we're not interpolating
let arg_rdr = new_tt_reader(&cx.parse_sess().span_diagnostic,
None,
arg.iter()
.map(|x| (*x).clone())
.collect());
let mut arg_rdr = new_tt_reader(&cx.parse_sess().span_diagnostic,
None,
arg.iter()
.map(|x| (*x).clone())
.collect());
arg_rdr.desugar_doc_comments = true;
match parse(cx.parse_sess(), cx.cfg(), arg_rdr, lhs_tt) {
Success(named_matches) => {
let rhs = match *rhses[i] {
Expand Down
14 changes: 12 additions & 2 deletions src/libsyntax/ext/tt/transcribe.rs
Expand Up @@ -43,6 +43,8 @@ pub struct TtReader<'a> {
/* cached: */
pub cur_tok: Token,
pub cur_span: Span,
/// Transform doc comments. Only useful in macro invocations
pub desugar_doc_comments: bool,
}

/// This can do Macro-By-Example transcription. On the other hand, if
Expand All @@ -66,6 +68,7 @@ pub fn new_tt_reader<'a>(sp_diag: &'a SpanHandler,
},
repeat_idx: Vec::new(),
repeat_len: Vec::new(),
desugar_doc_comments: false,
/* dummy values, never read: */
cur_tok: token::Eof,
cur_span: DUMMY_SP,
Expand Down Expand Up @@ -279,8 +282,7 @@ pub fn tt_next_token(r: &mut TtReader) -> TokenAndSpan {
}
}
// TtDelimited or any token that can be unzipped
seq @ TtDelimited(..) | seq @ TtToken(_, DocComment(..))
| seq @ TtToken(_, MatchNt(..)) => {
seq @ TtDelimited(..) | seq @ TtToken(_, MatchNt(..)) => {
// do not advance the idx yet
r.stack.push(TtFrame {
forest: seq.expand_into_tts(),
Expand All @@ -290,6 +292,14 @@ pub fn tt_next_token(r: &mut TtReader) -> TokenAndSpan {
});
// if this could be 0-length, we'd need to potentially recur here
}
TtToken(sp, DocComment(name)) if r.desugar_doc_comments => {
r.stack.push(TtFrame {
forest: TtToken(sp, DocComment(name)).expand_into_tts(),
idx: 0,
dotdotdoted: false,
sep: None
});
}
TtToken(sp, tok) => {
r.cur_span = sp;
r.cur_tok = tok;
Expand Down

0 comments on commit a8ce669

Please sign in to comment.