Struct proc_macro2::Group
source · pub struct Group { /* private fields */ }
Expand description
A delimited token stream.
A Group
internally contains a TokenStream
which is surrounded by
Delimiter
s.
Implementations§
source§impl Group
impl Group
sourcepub fn new(delimiter: Delimiter, stream: TokenStream) -> Self
pub fn new(delimiter: Delimiter, stream: TokenStream) -> Self
Creates a new Group
with the given delimiter and token stream.
This constructor will set the span for this group to
Span::call_site()
. To change the span you can use the set_span
method below.
sourcepub fn stream(&self) -> TokenStream
pub fn stream(&self) -> TokenStream
Returns the TokenStream
of tokens that are delimited in this Group
.
Note that the returned token stream does not include the delimiter returned above.
sourcepub fn span(&self) -> Span
pub fn span(&self) -> Span
Returns the span for the delimiters of this token stream, spanning the
entire Group
.
pub fn span(&self) -> Span {
^^^^^^^
sourcepub fn span_open(&self) -> Span
pub fn span_open(&self) -> Span
Returns the span pointing to the opening delimiter of this group.
pub fn span_open(&self) -> Span {
^
sourcepub fn span_close(&self) -> Span
pub fn span_close(&self) -> Span
Returns the span pointing to the closing delimiter of this group.
pub fn span_close(&self) -> Span {
^
sourcepub fn set_span(&mut self, span: Span)
pub fn set_span(&mut self, span: Span)
Configures the span for this Group
’s delimiters, but not its internal
tokens.
This method will not set the span of all the internal tokens spanned
by this group, but rather it will only set the span of the delimiter
tokens at the level of the Group
.
Examples found in repository?
More examples
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835
fn doc_comment<'a>(input: Cursor<'a>, trees: &mut TokenStreamBuilder) -> PResult<'a, ()> {
#[cfg(span_locations)]
let lo = input.off;
let (rest, (comment, inner)) = doc_comment_contents(input)?;
let span = crate::Span::_new_stable(Span {
#[cfg(span_locations)]
lo,
#[cfg(span_locations)]
hi: rest.off,
});
let mut scan_for_bare_cr = comment;
while let Some(cr) = scan_for_bare_cr.find('\r') {
let rest = &scan_for_bare_cr[cr + 1..];
if !rest.starts_with('\n') {
return Err(Reject);
}
scan_for_bare_cr = rest;
}
let mut pound = Punct::new('#', Spacing::Alone);
pound.set_span(span);
trees.push_token_from_parser(TokenTree::Punct(pound));
if inner {
let mut bang = Punct::new('!', Spacing::Alone);
bang.set_span(span);
trees.push_token_from_parser(TokenTree::Punct(bang));
}
let doc_ident = crate::Ident::new("doc", span);
let mut equal = Punct::new('=', Spacing::Alone);
equal.set_span(span);
let mut literal = crate::Literal::string(comment);
literal.set_span(span);
let mut bracketed = TokenStreamBuilder::with_capacity(3);
bracketed.push_token_from_parser(TokenTree::Ident(doc_ident));
bracketed.push_token_from_parser(TokenTree::Punct(equal));
bracketed.push_token_from_parser(TokenTree::Literal(literal));
let group = Group::new(Delimiter::Bracket, bracketed.build());
let mut group = crate::Group::_new_stable(group);
group.set_span(span);
trees.push_token_from_parser(TokenTree::Group(group));
Ok((rest, ()))
}