Skip to content

Commit 79e40a1

Browse files
authored
Merge pull request dtolnay#134 from dtolnay/group
Provide Group::span_open and span_close
2 parents 262c957 + f14813f commit 79e40a1

File tree

3 files changed

+234
-61
lines changed

3 files changed

+234
-61
lines changed

src/lib.rs

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -502,9 +502,7 @@ impl fmt::Debug for TokenTree {
502502
/// `Delimiter`s.
503503
#[derive(Clone)]
504504
pub struct Group {
505-
delimiter: Delimiter,
506-
stream: TokenStream,
507-
span: Span,
505+
inner: imp::Group,
508506
}
509507

510508
/// Describes how a sequence of token trees is delimited.
@@ -527,36 +525,73 @@ pub enum Delimiter {
527525
}
528526

529527
impl Group {
528+
fn _new(inner: imp::Group) -> Self {
529+
Group {
530+
inner: inner,
531+
}
532+
}
533+
534+
fn _new_stable(inner: stable::Group) -> Self {
535+
Group {
536+
inner: inner.into(),
537+
}
538+
}
539+
530540
/// Creates a new `Group` with the given delimiter and token stream.
531541
///
532542
/// This constructor will set the span for this group to
533543
/// `Span::call_site()`. To change the span you can use the `set_span`
534544
/// method below.
535545
pub fn new(delimiter: Delimiter, stream: TokenStream) -> Group {
536546
Group {
537-
delimiter: delimiter,
538-
stream: stream,
539-
span: Span::call_site(),
547+
inner: imp::Group::new(delimiter, stream.inner),
540548
}
541549
}
542550

543551
/// Returns the delimiter of this `Group`
544552
pub fn delimiter(&self) -> Delimiter {
545-
self.delimiter
553+
self.inner.delimiter()
546554
}
547555

548556
/// Returns the `TokenStream` of tokens that are delimited in this `Group`.
549557
///
550558
/// Note that the returned token stream does not include the delimiter
551559
/// returned above.
552560
pub fn stream(&self) -> TokenStream {
553-
self.stream.clone()
561+
TokenStream::_new(self.inner.stream())
554562
}
555563

556564
/// Returns the span for the delimiters of this token stream, spanning the
557565
/// entire `Group`.
566+
///
567+
/// ```text
568+
/// pub fn span(&self) -> Span {
569+
/// ^^^^^^^
570+
/// ```
558571
pub fn span(&self) -> Span {
559-
self.span
572+
Span::_new(self.inner.span())
573+
}
574+
575+
/// Returns the span pointing to the opening delimiter of this group.
576+
///
577+
/// ```text
578+
/// pub fn span_open(&self) -> Span {
579+
/// ^
580+
/// ```
581+
#[cfg(procmacro2_semver_exempt)]
582+
pub fn span_open(&self) -> Span {
583+
Span::_new(self.inner.span_open())
584+
}
585+
586+
/// Returns the span pointing to the closing delimiter of this group.
587+
///
588+
/// ```text
589+
/// pub fn span_close(&self) -> Span {
590+
/// ^
591+
/// ```
592+
#[cfg(procmacro2_semver_exempt)]
593+
pub fn span_close(&self) -> Span {
594+
Span::_new(self.inner.span_close())
560595
}
561596

562597
/// Configures the span for this `Group`'s delimiters, but not its internal
@@ -566,38 +601,22 @@ impl Group {
566601
/// by this group, but rather it will only set the span of the delimiter
567602
/// tokens at the level of the `Group`.
568603
pub fn set_span(&mut self, span: Span) {
569-
self.span = span;
604+
self.inner.set_span(span.inner)
570605
}
571606
}
572607

573608
/// Prints the group as a string that should be losslessly convertible back
574609
/// into the same group (modulo spans), except for possibly `TokenTree::Group`s
575610
/// with `Delimiter::None` delimiters.
576611
impl fmt::Display for Group {
577-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
578-
let (left, right) = match self.delimiter {
579-
Delimiter::Parenthesis => ("(", ")"),
580-
Delimiter::Brace => ("{", "}"),
581-
Delimiter::Bracket => ("[", "]"),
582-
Delimiter::None => ("", ""),
583-
};
584-
585-
f.write_str(left)?;
586-
self.stream.fmt(f)?;
587-
f.write_str(right)?;
588-
589-
Ok(())
612+
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
613+
fmt::Display::fmt(&self.inner, formatter)
590614
}
591615
}
592616

593617
impl fmt::Debug for Group {
594-
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
595-
let mut debug = fmt.debug_struct("Group");
596-
debug.field("delimiter", &self.delimiter);
597-
debug.field("stream", &self.stream);
598-
#[cfg(procmacro2_semver_exempt)]
599-
debug.field("span", &self.span);
600-
debug.finish()
618+
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
619+
fmt::Debug::fmt(&self.inner, formatter)
601620
}
602621
}
603622

src/stable.rs

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::vec;
1212
use strnom::{block_comment, skip_whitespace, whitespace, word_break, Cursor, PResult};
1313
use unicode_xid::UnicodeXID;
1414

15-
use {Delimiter, Group, Punct, Spacing, TokenTree};
15+
use {Delimiter, Punct, Spacing, TokenTree};
1616

1717
#[derive(Clone)]
1818
pub struct TokenStream {
@@ -431,6 +431,75 @@ impl fmt::Debug for Span {
431431
}
432432
}
433433

434+
#[derive(Clone)]
435+
pub struct Group {
436+
delimiter: Delimiter,
437+
stream: TokenStream,
438+
span: Span,
439+
}
440+
441+
impl Group {
442+
pub fn new(delimiter: Delimiter, stream: TokenStream) -> Group {
443+
Group {
444+
delimiter: delimiter,
445+
stream: stream,
446+
span: Span::call_site(),
447+
}
448+
}
449+
450+
pub fn delimiter(&self) -> Delimiter {
451+
self.delimiter
452+
}
453+
454+
pub fn stream(&self) -> TokenStream {
455+
self.stream.clone()
456+
}
457+
458+
pub fn span(&self) -> Span {
459+
self.span
460+
}
461+
462+
pub fn span_open(&self) -> Span {
463+
self.span
464+
}
465+
466+
pub fn span_close(&self) -> Span {
467+
self.span
468+
}
469+
470+
pub fn set_span(&mut self, span: Span) {
471+
self.span = span;
472+
}
473+
}
474+
475+
impl fmt::Display for Group {
476+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
477+
let (left, right) = match self.delimiter {
478+
Delimiter::Parenthesis => ("(", ")"),
479+
Delimiter::Brace => ("{", "}"),
480+
Delimiter::Bracket => ("[", "]"),
481+
Delimiter::None => ("", ""),
482+
};
483+
484+
f.write_str(left)?;
485+
self.stream.fmt(f)?;
486+
f.write_str(right)?;
487+
488+
Ok(())
489+
}
490+
}
491+
492+
impl fmt::Debug for Group {
493+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
494+
let mut debug = fmt.debug_struct("Group");
495+
debug.field("delimiter", &self.delimiter);
496+
debug.field("stream", &self.stream);
497+
#[cfg(procmacro2_semver_exempt)]
498+
debug.field("span", &self.span);
499+
debug.finish()
500+
}
501+
}
502+
434503
#[derive(Clone)]
435504
pub struct Ident {
436505
sym: String,
@@ -747,7 +816,7 @@ fn token_tree(input: Cursor) -> PResult<TokenTree> {
747816
}
748817

749818
named!(token_kind -> TokenTree, alt!(
750-
map!(group, TokenTree::Group)
819+
map!(group, |g| TokenTree::Group(::Group::_new_stable(g)))
751820
|
752821
map!(literal, |l| TokenTree::Literal(::Literal::_new_stable(l))) // must be before symbol
753822
|
@@ -761,19 +830,19 @@ named!(group -> Group, alt!(
761830
punct!("("),
762831
token_stream,
763832
punct!(")")
764-
) => { |ts| Group::new(Delimiter::Parenthesis, ::TokenStream::_new_stable(ts)) }
833+
) => { |ts| Group::new(Delimiter::Parenthesis, ts) }
765834
|
766835
delimited!(
767836
punct!("["),
768837
token_stream,
769838
punct!("]")
770-
) => { |ts| Group::new(Delimiter::Bracket, ::TokenStream::_new_stable(ts)) }
839+
) => { |ts| Group::new(Delimiter::Bracket, ts) }
771840
|
772841
delimited!(
773842
punct!("{"),
774843
token_stream,
775844
punct!("}")
776-
) => { |ts| Group::new(Delimiter::Brace, ::TokenStream::_new_stable(ts)) }
845+
) => { |ts| Group::new(Delimiter::Brace, ts) }
777846
));
778847

779848
fn symbol_leading_ws(input: Cursor) -> PResult<TokenTree> {
@@ -1288,7 +1357,8 @@ fn doc_comment(input: Cursor) -> PResult<Vec<TokenTree>> {
12881357
for tt in stream.iter_mut() {
12891358
tt.set_span(span);
12901359
}
1291-
trees.push(Group::new(Delimiter::Bracket, stream.into_iter().collect()).into());
1360+
let group = Group::new(Delimiter::Bracket, stream.into_iter().collect());
1361+
trees.push(::Group::_new_stable(group).into());
12921362
for tt in trees.iter_mut() {
12931363
tt.set_span(span);
12941364
}

0 commit comments

Comments
 (0)