Skip to content

Commit bc2b21c

Browse files
committed
proc_macro: move some implementation details to a rustc module.
1 parent e5e29d1 commit bc2b21c

File tree

4 files changed

+305
-264
lines changed

4 files changed

+305
-264
lines changed

src/libproc_macro/diagnostic.rs

-12
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,3 @@ impl Diagnostic {
116116
});
117117
}
118118
}
119-
120-
impl Level {
121-
fn to_internal(self) -> errors::Level {
122-
match self {
123-
Level::Error => errors::Level::Error,
124-
Level::Warning => errors::Level::Warning,
125-
Level::Note => errors::Level::Note,
126-
Level::Help => errors::Level::Help,
127-
Level::__Nonexhaustive => unreachable!("Level::__Nonexhaustive")
128-
}
129-
}
130-
}

src/libproc_macro/lib.rs

+21-246
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ extern crate syntax_pos;
4444
extern crate rustc_errors;
4545
extern crate rustc_data_structures;
4646

47+
#[unstable(feature = "proc_macro_internals", issue = "27812")]
48+
#[doc(hidden)]
49+
pub mod rustc;
50+
4751
mod diagnostic;
4852

4953
#[unstable(feature = "proc_macro_diagnostic", issue = "38356")]
@@ -54,12 +58,10 @@ use std::path::PathBuf;
5458
use rustc_data_structures::sync::Lrc;
5559
use std::str::FromStr;
5660

57-
use syntax::ast;
5861
use syntax::errors::DiagnosticBuilder;
5962
use syntax::parse::{self, token};
60-
use syntax::symbol::{keywords, Symbol};
63+
use syntax::symbol::Symbol;
6164
use syntax::tokenstream;
62-
use syntax::parse::lexer::{self, comments};
6365
use syntax_pos::{FileMap, Pos, FileName};
6466

6567
/// The main type provided by this crate, representing an abstract stream of
@@ -784,6 +786,16 @@ impl !Send for Ident {}
784786
impl !Sync for Ident {}
785787

786788
impl Ident {
789+
fn is_valid(string: &str) -> bool {
790+
let mut chars = string.chars();
791+
if let Some(start) = chars.next() {
792+
(start == '_' || start.is_xid_start())
793+
&& chars.all(|cont| cont == '_' || cont.is_xid_continue())
794+
} else {
795+
false
796+
}
797+
}
798+
787799
/// Creates a new `Ident` with the given `string` as well as the specified
788800
/// `span`.
789801
/// The `string` argument must be a valid identifier permitted by the
@@ -805,26 +817,19 @@ impl Ident {
805817
/// tokens, requires a `Span` to be specified at construction.
806818
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
807819
pub fn new(string: &str, span: Span) -> Ident {
808-
if !lexer::is_valid_ident(string) {
820+
if !Ident::is_valid(string) {
809821
panic!("`{:?}` is not a valid identifier", string)
810822
}
811-
Ident {
812-
sym: Symbol::intern(string),
813-
span,
814-
is_raw: false,
815-
}
823+
Ident::new_maybe_raw(string, span, false)
816824
}
817825

818826
/// Same as `Ident::new`, but creates a raw identifier (`r#ident`).
819827
#[unstable(feature = "proc_macro_raw_ident", issue = "38356")]
820828
pub fn new_raw(string: &str, span: Span) -> Ident {
821-
let mut ident = Ident::new(string, span);
822-
if ident.sym == keywords::Underscore.name() ||
823-
ast::Ident::with_empty_ctxt(ident.sym).is_path_segment_keyword() {
824-
panic!("`{:?}` is not a valid raw identifier", string)
829+
if !Ident::is_valid(string) {
830+
panic!("`{:?}` is not a valid identifier", string)
825831
}
826-
ident.is_raw = true;
827-
ident
832+
Ident::new_maybe_raw(string, span, true)
828833
}
829834

830835
/// Returns the span of this `Ident`, encompassing the entire string returned
@@ -861,7 +866,7 @@ impl fmt::Display for Ident {
861866
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
862867
pub struct Literal {
863868
lit: token::Lit,
864-
suffix: Option<ast::Name>,
869+
suffix: Option<Symbol>,
865870
span: Span,
866871
}
867872

@@ -1109,236 +1114,6 @@ impl fmt::Display for Literal {
11091114
}
11101115
}
11111116

1112-
impl Delimiter {
1113-
fn from_internal(delim: token::DelimToken) -> Delimiter {
1114-
match delim {
1115-
token::Paren => Delimiter::Parenthesis,
1116-
token::Brace => Delimiter::Brace,
1117-
token::Bracket => Delimiter::Bracket,
1118-
token::NoDelim => Delimiter::None,
1119-
}
1120-
}
1121-
1122-
fn to_internal(self) -> token::DelimToken {
1123-
match self {
1124-
Delimiter::Parenthesis => token::Paren,
1125-
Delimiter::Brace => token::Brace,
1126-
Delimiter::Bracket => token::Bracket,
1127-
Delimiter::None => token::NoDelim,
1128-
}
1129-
}
1130-
}
1131-
1132-
impl TokenTree {
1133-
fn from_internal(stream: tokenstream::TokenStream, stack: &mut Vec<TokenTree>)
1134-
-> TokenTree {
1135-
use syntax::parse::token::*;
1136-
1137-
let (tree, is_joint) = stream.as_tree();
1138-
let (span, token) = match tree {
1139-
tokenstream::TokenTree::Token(span, token) => (span, token),
1140-
tokenstream::TokenTree::Delimited(span, delimed) => {
1141-
let delimiter = Delimiter::from_internal(delimed.delim);
1142-
let mut g = Group::new(delimiter, TokenStream(delimed.tts.into()));
1143-
g.set_span(Span(span));
1144-
return g.into()
1145-
}
1146-
};
1147-
1148-
let op_kind = if is_joint { Spacing::Joint } else { Spacing::Alone };
1149-
macro_rules! tt {
1150-
($e:expr) => ({
1151-
let mut x = TokenTree::from($e);
1152-
x.set_span(Span(span));
1153-
x
1154-
})
1155-
}
1156-
macro_rules! op {
1157-
($a:expr) => (tt!(Punct::new($a, op_kind)));
1158-
($a:expr, $b:expr) => ({
1159-
stack.push(tt!(Punct::new($b, op_kind)));
1160-
tt!(Punct::new($a, Spacing::Joint))
1161-
});
1162-
($a:expr, $b:expr, $c:expr) => ({
1163-
stack.push(tt!(Punct::new($c, op_kind)));
1164-
stack.push(tt!(Punct::new($b, Spacing::Joint)));
1165-
tt!(Punct::new($a, Spacing::Joint))
1166-
})
1167-
}
1168-
1169-
match token {
1170-
Eq => op!('='),
1171-
Lt => op!('<'),
1172-
Le => op!('<', '='),
1173-
EqEq => op!('=', '='),
1174-
Ne => op!('!', '='),
1175-
Ge => op!('>', '='),
1176-
Gt => op!('>'),
1177-
AndAnd => op!('&', '&'),
1178-
OrOr => op!('|', '|'),
1179-
Not => op!('!'),
1180-
Tilde => op!('~'),
1181-
BinOp(Plus) => op!('+'),
1182-
BinOp(Minus) => op!('-'),
1183-
BinOp(Star) => op!('*'),
1184-
BinOp(Slash) => op!('/'),
1185-
BinOp(Percent) => op!('%'),
1186-
BinOp(Caret) => op!('^'),
1187-
BinOp(And) => op!('&'),
1188-
BinOp(Or) => op!('|'),
1189-
BinOp(Shl) => op!('<', '<'),
1190-
BinOp(Shr) => op!('>', '>'),
1191-
BinOpEq(Plus) => op!('+', '='),
1192-
BinOpEq(Minus) => op!('-', '='),
1193-
BinOpEq(Star) => op!('*', '='),
1194-
BinOpEq(Slash) => op!('/', '='),
1195-
BinOpEq(Percent) => op!('%', '='),
1196-
BinOpEq(Caret) => op!('^', '='),
1197-
BinOpEq(And) => op!('&', '='),
1198-
BinOpEq(Or) => op!('|', '='),
1199-
BinOpEq(Shl) => op!('<', '<', '='),
1200-
BinOpEq(Shr) => op!('>', '>', '='),
1201-
At => op!('@'),
1202-
Dot => op!('.'),
1203-
DotDot => op!('.', '.'),
1204-
DotDotDot => op!('.', '.', '.'),
1205-
DotDotEq => op!('.', '.', '='),
1206-
Comma => op!(','),
1207-
Semi => op!(';'),
1208-
Colon => op!(':'),
1209-
ModSep => op!(':', ':'),
1210-
RArrow => op!('-', '>'),
1211-
LArrow => op!('<', '-'),
1212-
FatArrow => op!('=', '>'),
1213-
Pound => op!('#'),
1214-
Dollar => op!('$'),
1215-
Question => op!('?'),
1216-
SingleQuote => op!('\''),
1217-
1218-
Ident(ident, false) => {
1219-
tt!(self::Ident::new(&ident.as_str(), Span(span)))
1220-
}
1221-
Ident(ident, true) => {
1222-
tt!(self::Ident::new_raw(&ident.as_str(), Span(span)))
1223-
}
1224-
Lifetime(ident) => {
1225-
let ident = ident.without_first_quote();
1226-
stack.push(tt!(self::Ident::new(&ident.as_str(), Span(span))));
1227-
tt!(Punct::new('\'', Spacing::Joint))
1228-
}
1229-
Literal(lit, suffix) => tt!(self::Literal { lit, suffix, span: Span(span) }),
1230-
DocComment(c) => {
1231-
let style = comments::doc_comment_style(&c.as_str());
1232-
let stripped = comments::strip_doc_comment_decoration(&c.as_str());
1233-
let stream = vec![
1234-
tt!(self::Ident::new("doc", Span(span))),
1235-
tt!(Punct::new('=', Spacing::Alone)),
1236-
tt!(self::Literal::string(&stripped)),
1237-
].into_iter().collect();
1238-
stack.push(tt!(Group::new(Delimiter::Bracket, stream)));
1239-
if style == ast::AttrStyle::Inner {
1240-
stack.push(tt!(Punct::new('!', Spacing::Alone)));
1241-
}
1242-
tt!(Punct::new('#', Spacing::Alone))
1243-
}
1244-
1245-
Interpolated(_) => {
1246-
__internal::with_sess(|sess, _| {
1247-
let tts = token.interpolated_to_tokenstream(sess, span);
1248-
tt!(Group::new(Delimiter::None, TokenStream(tts)))
1249-
})
1250-
}
1251-
1252-
DotEq => op!('.', '='),
1253-
OpenDelim(..) | CloseDelim(..) => unreachable!(),
1254-
Whitespace | Comment | Shebang(..) | Eof => unreachable!(),
1255-
}
1256-
}
1257-
1258-
fn to_internal(self) -> tokenstream::TokenStream {
1259-
use syntax::parse::token::*;
1260-
use syntax::tokenstream::{TokenTree, Delimited};
1261-
1262-
let (ch, kind, span) = match self {
1263-
self::TokenTree::Punct(tt) => (tt.as_char(), tt.spacing(), tt.span()),
1264-
self::TokenTree::Group(tt) => {
1265-
return TokenTree::Delimited(tt.span.0, Delimited {
1266-
delim: tt.delimiter.to_internal(),
1267-
tts: tt.stream.0.into(),
1268-
}).into();
1269-
},
1270-
self::TokenTree::Ident(tt) => {
1271-
let token = Ident(ast::Ident::new(tt.sym, tt.span.0), tt.is_raw);
1272-
return TokenTree::Token(tt.span.0, token).into();
1273-
}
1274-
self::TokenTree::Literal(self::Literal {
1275-
lit: Lit::Integer(ref a),
1276-
suffix,
1277-
span,
1278-
})
1279-
if a.as_str().starts_with("-") =>
1280-
{
1281-
let minus = BinOp(BinOpToken::Minus);
1282-
let integer = Symbol::intern(&a.as_str()[1..]);
1283-
let integer = Literal(Lit::Integer(integer), suffix);
1284-
let a = TokenTree::Token(span.0, minus);
1285-
let b = TokenTree::Token(span.0, integer);
1286-
return vec![a, b].into_iter().collect()
1287-
}
1288-
self::TokenTree::Literal(self::Literal {
1289-
lit: Lit::Float(ref a),
1290-
suffix,
1291-
span,
1292-
})
1293-
if a.as_str().starts_with("-") =>
1294-
{
1295-
let minus = BinOp(BinOpToken::Minus);
1296-
let float = Symbol::intern(&a.as_str()[1..]);
1297-
let float = Literal(Lit::Float(float), suffix);
1298-
let a = TokenTree::Token(span.0, minus);
1299-
let b = TokenTree::Token(span.0, float);
1300-
return vec![a, b].into_iter().collect()
1301-
}
1302-
self::TokenTree::Literal(tt) => {
1303-
let token = Literal(tt.lit, tt.suffix);
1304-
return TokenTree::Token(tt.span.0, token).into()
1305-
}
1306-
};
1307-
1308-
let token = match ch {
1309-
'=' => Eq,
1310-
'<' => Lt,
1311-
'>' => Gt,
1312-
'!' => Not,
1313-
'~' => Tilde,
1314-
'+' => BinOp(Plus),
1315-
'-' => BinOp(Minus),
1316-
'*' => BinOp(Star),
1317-
'/' => BinOp(Slash),
1318-
'%' => BinOp(Percent),
1319-
'^' => BinOp(Caret),
1320-
'&' => BinOp(And),
1321-
'|' => BinOp(Or),
1322-
'@' => At,
1323-
'.' => Dot,
1324-
',' => Comma,
1325-
';' => Semi,
1326-
':' => Colon,
1327-
'#' => Pound,
1328-
'$' => Dollar,
1329-
'?' => Question,
1330-
'\'' => SingleQuote,
1331-
_ => unreachable!(),
1332-
};
1333-
1334-
let tree = TokenTree::Token(span.0, token);
1335-
match kind {
1336-
Spacing::Alone => tree.into(),
1337-
Spacing::Joint => tree.joint(),
1338-
}
1339-
}
1340-
}
1341-
13421117
/// Permanently unstable internal implementation details of this crate. This
13431118
/// should not be used.
13441119
///

0 commit comments

Comments
 (0)