This repository has been archived by the owner on Oct 6, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from dtolnay/doc
Paste tokens inside of doc comments
- Loading branch information
Showing
3 changed files
with
155 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use proc_macro::{Delimiter, Span, TokenStream, TokenTree}; | ||
use std::iter; | ||
use std::str::FromStr; | ||
|
||
pub fn is_pasted_doc(input: &TokenStream) -> bool { | ||
#[derive(PartialEq)] | ||
enum State { | ||
Init, | ||
Doc, | ||
Equal, | ||
First, | ||
Rest, | ||
} | ||
|
||
let mut state = State::Init; | ||
for tt in input.clone() { | ||
state = match (state, &tt) { | ||
(State::Init, TokenTree::Ident(ident)) if ident.to_string() == "doc" => State::Doc, | ||
(State::Doc, TokenTree::Punct(punct)) if punct.as_char() == '=' => State::Equal, | ||
(State::Equal, tt) if is_stringlike(tt) => State::First, | ||
(State::First, tt) | (State::Rest, tt) if is_stringlike(tt) => State::Rest, | ||
_ => return false, | ||
}; | ||
} | ||
|
||
state == State::Rest | ||
} | ||
|
||
pub fn do_paste_doc(attr: &TokenStream, span: Span) -> TokenStream { | ||
let mut expanded = TokenStream::new(); | ||
let mut tokens = attr.clone().into_iter(); | ||
expanded.extend(tokens.by_ref().take(2)); // `doc =` | ||
|
||
let mut lit = String::new(); | ||
lit.push('"'); | ||
for token in tokens { | ||
lit += &escaped_string_value(&token).unwrap(); | ||
} | ||
lit.push('"'); | ||
|
||
let mut lit = TokenStream::from_str(&lit) | ||
.unwrap() | ||
.into_iter() | ||
.next() | ||
.unwrap(); | ||
lit.set_span(span); | ||
expanded.extend(iter::once(lit)); | ||
expanded | ||
} | ||
|
||
fn is_stringlike(token: &TokenTree) -> bool { | ||
escaped_string_value(token).is_some() | ||
} | ||
|
||
fn escaped_string_value(token: &TokenTree) -> Option<String> { | ||
match token { | ||
TokenTree::Ident(ident) => Some(ident.to_string()), | ||
TokenTree::Literal(literal) => { | ||
let mut repr = literal.to_string(); | ||
if repr.starts_with('b') || repr.starts_with('\'') { | ||
None | ||
} else if repr.starts_with('"') { | ||
repr.truncate(repr.len() - 1); | ||
repr.remove(0); | ||
Some(repr) | ||
} else if repr.starts_with('r') { | ||
let begin = repr.find('"').unwrap() + 1; | ||
let end = repr.rfind('"').unwrap(); | ||
Some(repr[begin..end].escape_default().to_string()) | ||
} else { | ||
Some(repr) | ||
} | ||
} | ||
TokenTree::Group(group) => { | ||
if group.delimiter() != Delimiter::None { | ||
return None; | ||
} | ||
let mut inner = group.stream().into_iter(); | ||
let first = inner.next()?; | ||
if inner.next().is_none() { | ||
escaped_string_value(&first) | ||
} else { | ||
None | ||
} | ||
} | ||
TokenTree::Punct(_) => None, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use paste::paste; | ||
|
||
#[test] | ||
fn test_paste_doc() { | ||
macro_rules! m { | ||
($ret:ident) => { | ||
paste! { | ||
#[doc = "Create a new [`" $ret "`] object."] | ||
fn new() -> $ret { todo!() } | ||
} | ||
}; | ||
} | ||
|
||
struct Paste; | ||
m!(Paste); | ||
|
||
let _ = new; | ||
} | ||
|
||
macro_rules! get_doc { | ||
(#[doc = $literal:tt]) => { | ||
$literal | ||
}; | ||
} | ||
|
||
#[test] | ||
fn test_escaping() { | ||
let doc = paste! { | ||
get_doc!(#[doc = "s\"" r#"r#""#]) | ||
}; | ||
|
||
let expected = "s\"r#\""; | ||
assert_eq!(doc, expected); | ||
} | ||
|
||
#[test] | ||
fn test_literals() { | ||
let doc = paste! { | ||
get_doc!(#[doc = "int=" 0x1 " bool=" true " float=" 0.01]) | ||
}; | ||
|
||
let expected = "int=0x1 bool=true float=0.01"; | ||
assert_eq!(doc, expected); | ||
} |