Skip to content

Commit 47d4089

Browse files
committed
TokenTree: Op -> Punct, Term -> Ident
1 parent decc619 commit 47d4089

File tree

7 files changed

+115
-131
lines changed

7 files changed

+115
-131
lines changed

src/libproc_macro/lib.rs

Lines changed: 70 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ pub mod token_stream {
246246

247247
/// `quote!(..)` accepts arbitrary tokens and expands into a `TokenStream` describing the input.
248248
/// For example, `quote!(a + b)` will produce a expression, that, when evaluated, constructs
249-
/// the `TokenStream` `[Word("a"), Op('+', Alone), Word("b")]`.
249+
/// the `TokenStream` `[Word("a"), Punct('+', Alone), Word("b")]`.
250250
///
251251
/// Unquoting is done with `$`, and works by taking the single next ident as the unquoted term.
252252
/// To quote `$` itself, use `$$`.
@@ -499,17 +499,9 @@ pub enum TokenTree {
499499
/// A token stream surrounded by bracket delimiters.
500500
Group(Group),
501501
/// An identifier or lifetime identifier.
502-
///
503-
/// REVIEW Maybe let's name it `Ident` instead of inventing a new term, it's named "identifier"
504-
/// REVIEW everywhere in the compiler, including `ident` in `macro`/`macro_rules!` DSL.
505-
Term(Term),
502+
Ident(Ident),
506503
/// A single punctuation character (`+`, `,`, `$`, etc.).
507-
///
508-
/// REVIEW This is not an operator, operators are more narrow set, they also can be
509-
/// REVIEW multicharacter, this is punctuation, even the comment says so!
510-
/// REVIEW @dtolnay suggested `Punct` in the original implementation PR too, and it was
511-
/// REVIEW received positively, but the renaming never actually happened.
512-
Op(Op),
504+
Punct(Punct),
513505
/// A literal character (`'a'`), string (`"hello"`), number (`2.3`), etc.
514506
Literal(Literal),
515507
}
@@ -526,8 +518,8 @@ impl TokenTree {
526518
pub fn span(&self) -> Span {
527519
match *self {
528520
TokenTree::Group(ref t) => t.span(),
529-
TokenTree::Term(ref t) => t.span(),
530-
TokenTree::Op(ref t) => t.span(),
521+
TokenTree::Ident(ref t) => t.span(),
522+
TokenTree::Punct(ref t) => t.span(),
531523
TokenTree::Literal(ref t) => t.span(),
532524
}
533525
}
@@ -541,8 +533,8 @@ impl TokenTree {
541533
pub fn set_span(&mut self, span: Span) {
542534
match *self {
543535
TokenTree::Group(ref mut t) => t.set_span(span),
544-
TokenTree::Term(ref mut t) => t.set_span(span),
545-
TokenTree::Op(ref mut t) => t.set_span(span),
536+
TokenTree::Ident(ref mut t) => t.set_span(span),
537+
TokenTree::Punct(ref mut t) => t.set_span(span),
546538
TokenTree::Literal(ref mut t) => t.set_span(span),
547539
}
548540
}
@@ -556,16 +548,16 @@ impl fmt::Debug for TokenTree {
556548
// so don't bother with an extra layer of indirection
557549
match *self {
558550
TokenTree::Group(ref tt) => tt.fmt(f),
559-
TokenTree::Term(ref tt) => tt.fmt(f),
560-
TokenTree::Op(ref tt) => tt.fmt(f),
551+
TokenTree::Ident(ref tt) => tt.fmt(f),
552+
TokenTree::Punct(ref tt) => tt.fmt(f),
561553
TokenTree::Literal(ref tt) => tt.fmt(f),
562554
}
563555
}
564556
}
565557

566558
/// REVIEW the impls below are kind of `From<T> for Option<T>`, not strictly necessary,
567559
/// REVIEW but convenient. No harm, I guess. I'd actually like to see impls
568-
/// REVIEW `From<Group/Term/Op/Literal> for TokenStream` to avoid stuttering like
560+
/// REVIEW `From<Group/Ident/Punct/Literal> for TokenStream` to avoid stuttering like
569561
/// REVIEW `TokenTree::Literal(Literal::string("lalala")).into()`.
570562
#[unstable(feature = "proc_macro", issue = "38356")]
571563
impl From<Group> for TokenTree {
@@ -575,16 +567,16 @@ impl From<Group> for TokenTree {
575567
}
576568

577569
#[unstable(feature = "proc_macro", issue = "38356")]
578-
impl From<Term> for TokenTree {
579-
fn from(g: Term) -> TokenTree {
580-
TokenTree::Term(g)
570+
impl From<Ident> for TokenTree {
571+
fn from(g: Ident) -> TokenTree {
572+
TokenTree::Ident(g)
581573
}
582574
}
583575

584576
#[unstable(feature = "proc_macro", issue = "38356")]
585-
impl From<Op> for TokenTree {
586-
fn from(g: Op) -> TokenTree {
587-
TokenTree::Op(g)
577+
impl From<Punct> for TokenTree {
578+
fn from(g: Punct) -> TokenTree {
579+
TokenTree::Punct(g)
588580
}
589581
}
590582

@@ -603,8 +595,8 @@ impl fmt::Display for TokenTree {
603595
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
604596
match *self {
605597
TokenTree::Group(ref t) => t.fmt(f),
606-
TokenTree::Term(ref t) => t.fmt(f),
607-
TokenTree::Op(ref t) => t.fmt(f),
598+
TokenTree::Ident(ref t) => t.fmt(f),
599+
TokenTree::Punct(ref t) => t.fmt(f),
608600
TokenTree::Literal(ref t) => t.fmt(f),
609601
}
610602
}
@@ -703,37 +695,32 @@ impl fmt::Display for Group {
703695
}
704696
}
705697

706-
/// An `Op` is an single punctuation character like `+`, `-` or `#`.
698+
/// An `Punct` is an single punctuation character like `+`, `-` or `#`.
707699
///
708-
/// Multicharacter operators like `+=` are represented as two instances of `Op` with different
700+
/// Multicharacter operators like `+=` are represented as two instances of `Punct` with different
709701
/// forms of `Spacing` returned.
710702
///
711-
/// REVIEW This is not an operator, operators are more narrow set, they also can be
712-
/// REVIEW multicharacter, this is punctuation, even the comment says so!
713-
/// REVIEW @dtolnay suggested `Punct` in the original implementation PR too, and it was
714-
/// REVIEW received positively, but the renaming never actually happened.
715-
///
716-
/// REVIEW We should guarantee that `Op` contains a valid punctuation character permitted by
703+
/// REVIEW We should guarantee that `Punct` contains a valid punctuation character permitted by
717704
/// REVIEW the language and not a random unicode code point. The check is already performed in
718705
/// REVIEW `TokenTree::to_internal`, but we should do it on construction.
719-
/// REVIEW `Op` can also avoid using `char` internally and keep an u8-like enum.
706+
/// REVIEW `Punct` can also avoid using `char` internally and keep an u8-like enum.
720707
///
721708
/// REVIEW ATTENTION: `Copy` impl on a struct with private fields.
722-
/// REVIEW Do we want to guarantee `Op` to be `Copy`?
709+
/// REVIEW Do we want to guarantee `Punct` to be `Copy`?
723710
#[unstable(feature = "proc_macro", issue = "38356")]
724711
#[derive(Copy, Clone, Debug)]
725-
pub struct Op {
726-
op: char,
712+
pub struct Punct {
713+
ch: char,
727714
spacing: Spacing,
728715
span: Span,
729716
}
730717

731718
#[unstable(feature = "proc_macro", issue = "38356")]
732-
impl !Send for Op {}
719+
impl !Send for Punct {}
733720
#[unstable(feature = "proc_macro", issue = "38356")]
734-
impl !Sync for Op {}
721+
impl !Sync for Punct {}
735722

736-
/// Whether an `Op` is followed immediately by another `Op` or
723+
/// Whether an `Punct` is followed immediately by another `Punct` or
737724
/// followed by another token or whitespace.
738725
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
739726
#[unstable(feature = "proc_macro", issue = "38356")]
@@ -744,10 +731,10 @@ pub enum Spacing {
744731
Joint,
745732
}
746733

747-
impl Op {
748-
/// Creates a new `Op` from the given character and spacing.
734+
impl Punct {
735+
/// Creates a new `Punct` from the given character and spacing.
749736
///
750-
/// The returned `Op` will have the default span of `Span::call_site()`
737+
/// The returned `Punct` will have the default span of `Span::call_site()`
751738
/// which can be further configured with the `set_span` method below.
752739
///
753740
/// REVIEW Why we even use `char` here? There's no reason to use unicode here.
@@ -756,9 +743,9 @@ impl Op {
756743
/// REVIEW TO_DO Do input validation on construction, the argument should be a valid punctuation
757744
/// REVIEW character permitted by the language.
758745
#[unstable(feature = "proc_macro", issue = "38356")]
759-
pub fn new(op: char, spacing: Spacing) -> Op {
760-
Op {
761-
op: op,
746+
pub fn new(ch: char, spacing: Spacing) -> Punct {
747+
Punct {
748+
ch: ch,
762749
spacing: spacing,
763750
span: Span::call_site(),
764751
}
@@ -770,12 +757,12 @@ impl Op {
770757
/// REVIEW except for maybe future compatibility in case Rust turns into APL,
771758
/// REVIEW but if it's more convenient to use `char` then that's okay.
772759
#[unstable(feature = "proc_macro", issue = "38356")]
773-
pub fn op(&self) -> char {
774-
self.op
760+
pub fn as_char(&self) -> char {
761+
self.ch
775762
}
776763

777764
/// Returns the spacing of this punctuation character, indicating whether it's immediately
778-
/// followed by another `Op` in the token stream, so they can potentially be combined into
765+
/// followed by another `Punct` in the token stream, so they can potentially be combined into
779766
/// a multicharacter operator (`Joint`), or it's followed by some other token or whitespace
780767
/// (`Alone`) so the operator has certainly ended.
781768
#[unstable(feature = "proc_macro", issue = "38356")]
@@ -799,43 +786,40 @@ impl Op {
799786
/// Prints the punctuation character as a string that should be losslessly convertible
800787
/// back into the same character.
801788
#[unstable(feature = "proc_macro", issue = "38356")]
802-
impl fmt::Display for Op {
789+
impl fmt::Display for Punct {
803790
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
804791
TokenStream::from(TokenTree::from(self.clone())).fmt(f)
805792
}
806793
}
807794

808795
/// An identifier (`ident`) or lifetime identifier (`'ident`).
809796
///
810-
/// REVIEW We should guarantee that `Term` contains a valid identifier permitted by
797+
/// REVIEW We should guarantee that `Ident` contains a valid identifier permitted by
811798
/// REVIEW the language and not a random unicode string, at least for a start.
812799
///
813-
/// REVIEW Maybe let's name it `Ident` instead of inventing a new term, it's named "identifier"
814-
/// REVIEW everywhere in the compiler, including `ident` in `macro`/`macro_rules!` DSL.
815-
///
816800
/// REVIEW We need to support raw identifiers here (`r#ident`) or at least be future compatible
817801
/// REVIEW with them. Currently they are supported using "string typing" - if string "r#ident" is
818-
/// REVIEW passed to `Term::new` it will be interpreted as a raw identifier later on, we should add
819-
/// REVIEW a field `is_raw` and a separate constructor for it (`Term::new_raw` or something) and
802+
/// REVIEW passed to `Ident::new` it will be interpreted as a raw identifier later on, we should add
803+
/// REVIEW a field `is_raw` and a separate constructor for it (`Ident::new_raw` or something) and
820804
/// REVIEW keep it unstable until raw identifiers are stabilized.
821805
///
822806
/// REVIEW ATTENTION: `Copy` impl on a struct with private fields.
823-
/// REVIEW Do we want to guarantee `Term` to be `Copy`?
807+
/// REVIEW Do we want to guarantee `Ident` to be `Copy`?
824808
#[derive(Copy, Clone, Debug)]
825809
#[unstable(feature = "proc_macro", issue = "38356")]
826-
pub struct Term {
810+
pub struct Ident {
827811
// REVIEW(INTERNAL) Symbol + Span is actually `ast::Ident`! We can use it here.
828812
sym: Symbol,
829813
span: Span,
830814
}
831815

832816
#[unstable(feature = "proc_macro", issue = "38356")]
833-
impl !Send for Term {}
817+
impl !Send for Ident {}
834818
#[unstable(feature = "proc_macro", issue = "38356")]
835-
impl !Sync for Term {}
819+
impl !Sync for Ident {}
836820

837-
impl Term {
838-
/// Creates a new `Term` with the given `string` as well as the specified
821+
impl Ident {
822+
/// Creates a new `Ident` with the given `string` as well as the specified
839823
/// `span`.
840824
///
841825
/// Note that `span`, currently in rustc, configures the hygiene information
@@ -856,8 +840,8 @@ impl Term {
856840
/// REVIEW TO_DO Do input validation, the argument should be a valid identifier or
857841
/// REVIEW lifetime identifier.
858842
#[unstable(feature = "proc_macro", issue = "38356")]
859-
pub fn new(string: &str, span: Span) -> Term {
860-
Term {
843+
pub fn new(string: &str, span: Span) -> Ident {
844+
Ident {
861845
sym: Symbol::intern(string),
862846
span,
863847
}
@@ -870,14 +854,14 @@ impl Term {
870854
unsafe { &*(&*self.sym.as_str() as *const str) }
871855
}
872856

873-
/// Returns the span of this `Term`, encompassing the entire string returned
857+
/// Returns the span of this `Ident`, encompassing the entire string returned
874858
/// by `as_str`.
875859
#[unstable(feature = "proc_macro", issue = "38356")]
876860
pub fn span(&self) -> Span {
877861
self.span
878862
}
879863

880-
/// Configures the span of this `Term`, possibly changing its hygiene context.
864+
/// Configures the span of this `Ident`, possibly changing its hygiene context.
881865
#[unstable(feature = "proc_macro", issue = "38356")]
882866
pub fn set_span(&mut self, span: Span) {
883867
self.span = span;
@@ -887,7 +871,7 @@ impl Term {
887871
/// Prints the identifier as a string that should be losslessly convertible
888872
/// back into the same identifier.
889873
#[unstable(feature = "proc_macro", issue = "38356")]
890-
impl fmt::Display for Term {
874+
impl fmt::Display for Ident {
891875
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
892876
self.sym.as_str().fmt(f)
893877
}
@@ -896,7 +880,7 @@ impl fmt::Display for Term {
896880
/// A literal string (`"hello"`), byte string (`b"hello"`),
897881
/// character (`'a'`), byte character (`b'a'`), an integer or floating point number
898882
/// with or without a suffix (`1`, `1u8`, `2.3`, `2.3f32`).
899-
/// Boolean literals like `true` and `false` do not belong here, they are `Term`s.
883+
/// Boolean literals like `true` and `false` do not belong here, they are `Ident`s.
900884
#[derive(Clone, Debug)]
901885
#[unstable(feature = "proc_macro", issue = "38356")]
902886
pub struct Literal {
@@ -1182,15 +1166,15 @@ impl TokenTree {
11821166
})
11831167
}
11841168
macro_rules! op {
1185-
($a:expr) => (tt!(Op::new($a, op_kind)));
1169+
($a:expr) => (tt!(Punct::new($a, op_kind)));
11861170
($a:expr, $b:expr) => ({
1187-
stack.push(tt!(Op::new($b, op_kind)));
1188-
tt!(Op::new($a, Spacing::Joint))
1171+
stack.push(tt!(Punct::new($b, op_kind)));
1172+
tt!(Punct::new($a, Spacing::Joint))
11891173
});
11901174
($a:expr, $b:expr, $c:expr) => ({
1191-
stack.push(tt!(Op::new($c, op_kind)));
1192-
stack.push(tt!(Op::new($b, Spacing::Joint)));
1193-
tt!(Op::new($a, Spacing::Joint))
1175+
stack.push(tt!(Punct::new($c, op_kind)));
1176+
stack.push(tt!(Punct::new($b, Spacing::Joint)));
1177+
tt!(Punct::new($a, Spacing::Joint))
11941178
})
11951179
}
11961180

@@ -1243,25 +1227,25 @@ impl TokenTree {
12431227
Question => op!('?'),
12441228

12451229
Ident(ident, false) | Lifetime(ident) => {
1246-
tt!(Term::new(&ident.name.as_str(), Span(span)))
1230+
tt!(self::Ident::new(&ident.name.as_str(), Span(span)))
12471231
}
12481232
Ident(ident, true) => {
1249-
tt!(Term::new(&format!("r#{}", ident), Span(span)))
1233+
tt!(self::Ident::new(&format!("r#{}", ident), Span(span)))
12501234
}
12511235
Literal(lit, suffix) => tt!(self::Literal { lit, suffix, span: Span(span) }),
12521236
DocComment(c) => {
12531237
let style = comments::doc_comment_style(&c.as_str());
12541238
let stripped = comments::strip_doc_comment_decoration(&c.as_str());
12551239
let stream = vec![
1256-
tt!(Term::new("doc", Span(span))),
1257-
tt!(Op::new('=', Spacing::Alone)),
1240+
tt!(self::Ident::new("doc", Span(span))),
1241+
tt!(Punct::new('=', Spacing::Alone)),
12581242
tt!(self::Literal::string(&stripped)),
12591243
].into_iter().collect();
12601244
stack.push(tt!(Group::new(Delimiter::Bracket, stream)));
12611245
if style == ast::AttrStyle::Inner {
1262-
stack.push(tt!(Op::new('!', Spacing::Alone)));
1246+
stack.push(tt!(Punct::new('!', Spacing::Alone)));
12631247
}
1264-
tt!(Op::new('#', Spacing::Alone))
1248+
tt!(Punct::new('#', Spacing::Alone))
12651249
}
12661250

12671251
Interpolated(_) => {
@@ -1281,15 +1265,15 @@ impl TokenTree {
12811265
use syntax::parse::token::*;
12821266
use syntax::tokenstream::{TokenTree, Delimited};
12831267

1284-
let (op, kind, span) = match self {
1285-
self::TokenTree::Op(tt) => (tt.op(), tt.spacing(), tt.span()),
1268+
let (ch, kind, span) = match self {
1269+
self::TokenTree::Punct(tt) => (tt.as_char(), tt.spacing(), tt.span()),
12861270
self::TokenTree::Group(tt) => {
12871271
return TokenTree::Delimited(tt.span.0, Delimited {
12881272
delim: tt.delimiter.to_internal(),
12891273
tts: tt.stream.0.into(),
12901274
}).into();
12911275
},
1292-
self::TokenTree::Term(tt) => {
1276+
self::TokenTree::Ident(tt) => {
12931277
let ident = ast::Ident::new(tt.sym, tt.span.0);
12941278
let sym_str = tt.sym.to_string();
12951279
let token = if sym_str.starts_with("'") {
@@ -1337,7 +1321,7 @@ impl TokenTree {
13371321
}
13381322
};
13391323

1340-
let token = match op {
1324+
let token = match ch {
13411325
'=' => Eq,
13421326
'<' => Lt,
13431327
'>' => Gt,
@@ -1359,7 +1343,7 @@ impl TokenTree {
13591343
'#' => Pound,
13601344
'$' => Dollar,
13611345
'?' => Question,
1362-
_ => panic!("unsupported character {}", op),
1346+
_ => panic!("unsupported character {}", ch),
13631347
};
13641348

13651349
let tree = TokenTree::Token(span.0, token);

0 commit comments

Comments
 (0)