1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 pub use self::BinOpToken::*;
12 pub use self::Nonterminal::*;
13 pub use self::DelimToken::*;
15 pub use self::Token::*;
21 use serialize::{Decodable, Decoder, Encodable, Encoder};
23 use syntax::parse::parse_stream_from_source_str;
24 use syntax_pos::{self, Span, FileName};
25 use tokenstream::{TokenStream, TokenTree};
30 use rustc_data_structures::sync::Lrc;
32 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash, Debug, Copy)]
47 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash, Debug, Copy)]
49 /// A round parenthesis: `(` or `)`
51 /// A square bracket: `[` or `]`
53 /// A curly brace: `{` or `}`
55 /// An empty delimiter
60 pub fn len(self) -> usize {
61 if self == NoDelim { 0 } else { 1 }
64 pub fn is_empty(self) -> bool {
69 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash, Debug, Copy)]
76 StrRaw(ast::Name, usize), /* raw str delimited by n hash symbols */
78 ByteStrRaw(ast::Name, usize), /* raw byte str delimited by n hash symbols */
82 pub fn short_name(&self) -> &'static str {
86 Integer(_) => "integer",
88 Str_(_) | StrRaw(..) => "string",
89 ByteStr(_) | ByteStrRaw(..) => "byte string"
94 fn ident_can_begin_expr(ident: ast::Ident) -> bool {
95 let ident_token: Token = Ident(ident);
97 !ident_token.is_reserved_ident() ||
98 ident_token.is_path_segment_keyword() ||
101 keywords::Box.name(),
102 keywords::Break.name(),
103 keywords::Continue.name(),
104 keywords::False.name(),
105 keywords::For.name(),
107 keywords::Loop.name(),
108 keywords::Match.name(),
109 keywords::Move.name(),
110 keywords::Return.name(),
111 keywords::True.name(),
112 keywords::Unsafe.name(),
113 keywords::While.name(),
114 keywords::Yield.name(),
115 keywords::Static.name(),
116 ].contains(&ident.name)
119 fn ident_can_begin_type(ident: ast::Ident) -> bool {
120 let ident_token: Token = Ident(ident);
122 !ident_token.is_reserved_ident() ||
123 ident_token.is_path_segment_keyword() ||
125 keywords::For.name(),
126 keywords::Impl.name(),
128 keywords::Unsafe.name(),
129 keywords::Extern.name(),
130 keywords::Typeof.name(),
131 ].contains(&ident.name)
134 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash, Debug)]
136 /* Expression-operator symbols. */
151 /* Structural symbols */
157 DotEq, // HACK(durka42) never produced by the parser, only used for libproc_macro
168 /// An opening delimiter, eg. `{`
169 OpenDelim(DelimToken),
170 /// A closing delimiter, eg. `}`
171 CloseDelim(DelimToken),
174 Literal(Lit, Option<ast::Name>),
176 /* Name components */
179 Lifetime(ast::Ident),
181 // The `LazyTokenStream` is a pure function of the `Nonterminal`,
182 // and so the `LazyTokenStream` can be ignored by Eq, Hash, etc.
183 Interpolated(Lrc<(Nonterminal, LazyTokenStream)>),
184 // Can be expanded into several tokens.
186 DocComment(ast::Name),
188 // Junk. These carry no data because we don't really care about the data
189 // they *would* carry, and don't really want to allocate a new ident for
190 // them. Instead, users could extract that from the associated span.
202 pub fn interpolated(nt: Nonterminal) -> Token {
203 Token::Interpolated(Lrc::new((nt, LazyTokenStream::new())))
206 /// Returns `true` if the token starts with '>'.
207 pub fn is_like_gt(&self) -> bool {
209 BinOp(Shr) | BinOpEq(Shr) | Gt | Ge => true,
214 /// Returns `true` if the token can appear at the start of an expression.
215 pub fn can_begin_expr(&self) -> bool {
217 Ident(ident) => ident_can_begin_expr(ident), // value name or keyword
218 OpenDelim(..) | // tuple, array or block
219 Literal(..) | // literal
220 Not | // operator not
221 BinOp(Minus) | // unary minus
222 BinOp(Star) | // dereference
223 BinOp(Or) | OrOr | // closure
224 BinOp(And) | // reference
225 AndAnd | // double reference
226 // DotDotDot is no longer supported, but we need some way to display the error
227 DotDot | DotDotDot | DotDotEq | // range notation
228 Lt | BinOp(Shl) | // associated path
229 ModSep | // global path
230 Pound => true, // expression attributes
231 Interpolated(ref nt) => match nt.0 {
232 NtIdent(..) | NtExpr(..) | NtBlock(..) | NtPath(..) => true,
239 /// Returns `true` if the token can appear at the start of a type.
240 pub fn can_begin_type(&self) -> bool {
242 Ident(ident) => ident_can_begin_type(ident), // type name or keyword
243 OpenDelim(Paren) | // tuple
244 OpenDelim(Bracket) | // array
245 Underscore | // placeholder
247 BinOp(Star) | // raw pointer
248 BinOp(And) | // reference
249 AndAnd | // double reference
250 Question | // maybe bound in trait object
251 Lifetime(..) | // lifetime bound in trait object
252 Lt | BinOp(Shl) | // associated path
253 ModSep => true, // global path
254 Interpolated(ref nt) => match nt.0 {
255 NtIdent(..) | NtTy(..) | NtPath(..) | NtLifetime(..) => true,
262 /// Returns `true` if the token can appear at the start of a generic bound.
263 pub fn can_begin_bound(&self) -> bool {
264 self.is_path_start() || self.is_lifetime() || self.is_keyword(keywords::For) ||
265 self == &Question || self == &OpenDelim(Paren)
268 /// Returns `true` if the token is any literal
269 pub fn is_lit(&self) -> bool {
276 pub fn ident(&self) -> Option<ast::Ident> {
278 Ident(ident) => Some(ident),
279 Interpolated(ref nt) => match nt.0 {
280 NtIdent(ident) => Some(ident.node),
287 /// Returns `true` if the token is an identifier.
288 pub fn is_ident(&self) -> bool {
289 self.ident().is_some()
292 /// Returns `true` if the token is a documentation comment.
293 pub fn is_doc_comment(&self) -> bool {
295 DocComment(..) => true,
300 /// Returns `true` if the token is interpolated.
301 pub fn is_interpolated(&self) -> bool {
303 Interpolated(..) => true,
308 /// Returns `true` if the token is an interpolated path.
309 pub fn is_path(&self) -> bool {
310 if let Interpolated(ref nt) = *self {
311 if let NtPath(..) = nt.0 {
318 /// Returns a lifetime with the span and a dummy id if it is a lifetime,
319 /// or the original lifetime if it is an interpolated lifetime, ignoring
321 pub fn lifetime(&self, span: Span) -> Option<ast::Lifetime> {
324 Some(ast::Lifetime { ident: ident, span: span, id: ast::DUMMY_NODE_ID }),
325 Interpolated(ref nt) => match nt.0 {
326 NtLifetime(lifetime) => Some(lifetime),
333 /// Returns `true` if the token is a lifetime.
334 pub fn is_lifetime(&self) -> bool {
335 self.lifetime(syntax_pos::DUMMY_SP).is_some()
338 /// Returns `true` if the token is either the `mut` or `const` keyword.
339 pub fn is_mutability(&self) -> bool {
340 self.is_keyword(keywords::Mut) ||
341 self.is_keyword(keywords::Const)
344 pub fn is_qpath_start(&self) -> bool {
345 self == &Lt || self == &BinOp(Shl)
348 pub fn is_path_start(&self) -> bool {
349 self == &ModSep || self.is_qpath_start() || self.is_path() ||
350 self.is_path_segment_keyword() || self.is_ident() && !self.is_reserved_ident()
353 /// Returns `true` if the token is a given keyword, `kw`.
354 pub fn is_keyword(&self, kw: keywords::Keyword) -> bool {
355 self.ident().map(|ident| ident.name == kw.name()).unwrap_or(false)
358 pub fn is_path_segment_keyword(&self) -> bool {
360 Some(id) => id.name == keywords::Super.name() ||
361 id.name == keywords::SelfValue.name() ||
362 id.name == keywords::SelfType.name() ||
363 id.name == keywords::Extern.name() ||
364 id.name == keywords::Crate.name() ||
365 id.name == keywords::DollarCrate.name(),
370 // Returns true for reserved identifiers used internally for elided lifetimes,
371 // unnamed method parameters, crate root module, error recovery etc.
372 pub fn is_special_ident(&self) -> bool {
374 Some(id) => id.name <= keywords::DollarCrate.name(),
379 /// Returns `true` if the token is a keyword used in the language.
380 pub fn is_used_keyword(&self) -> bool {
382 Some(id) => id.name >= keywords::As.name() && id.name <= keywords::While.name(),
387 /// Returns `true` if the token is a keyword reserved for possible future use.
388 pub fn is_unused_keyword(&self) -> bool {
390 Some(id) => id.name >= keywords::Abstract.name() && id.name <= keywords::Yield.name(),
395 pub fn glue(self, joint: Token) -> Option<Token> {
406 BinOp(Minus) => LArrow,
419 BinOp(op) => match joint {
421 BinOp(And) if op == And => AndAnd,
422 BinOp(Or) if op == Or => OrOr,
423 Gt if op == Minus => RArrow,
432 DotDot => match joint {
437 Colon => match joint {
442 Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | BinOpEq(..) | At | DotDotDot | DotEq |
443 DotDotEq | Comma | Semi | ModSep | RArrow | LArrow | FatArrow | Pound | Dollar |
444 Question | OpenDelim(..) | CloseDelim(..) | Underscore => return None,
446 Literal(..) | Ident(..) | Lifetime(..) | Interpolated(..) | DocComment(..) |
447 Whitespace | Comment | Shebang(..) | Eof => return None,
451 /// Returns tokens that are likely to be typed accidentally instead of the current token.
452 /// Enables better error recovery when the wrong token is found.
453 pub fn similar_tokens(&self) -> Option<Vec<Token>> {
455 Comma => Some(vec![Dot, Lt]),
456 Semi => Some(vec![Colon]),
461 /// Returns `true` if the token is either a special identifier or a keyword.
462 pub fn is_reserved_ident(&self) -> bool {
463 self.is_special_ident() || self.is_used_keyword() || self.is_unused_keyword()
466 pub fn interpolated_to_tokenstream(&self, sess: &ParseSess, span: Span)
469 let nt = match *self {
470 Token::Interpolated(ref nt) => nt,
471 _ => panic!("only works on interpolated tokens"),
474 // An `Interpolated` token means that we have a `Nonterminal`
475 // which is often a parsed AST item. At this point we now need
476 // to convert the parsed AST to an actual token stream, e.g.
477 // un-parse it basically.
479 // Unfortunately there's not really a great way to do that in a
480 // guaranteed lossless fashion right now. The fallback here is
481 // to just stringify the AST node and reparse it, but this loses
482 // all span information.
484 // As a result, some AST nodes are annotated with the token
485 // stream they came from. Attempt to extract these lossless
486 // token streams before we fall back to the stringification.
487 let mut tokens = None;
490 Nonterminal::NtItem(ref item) => {
491 tokens = prepend_attrs(sess, &item.attrs, item.tokens.as_ref(), span);
493 Nonterminal::NtTraitItem(ref item) => {
494 tokens = prepend_attrs(sess, &item.attrs, item.tokens.as_ref(), span);
496 Nonterminal::NtImplItem(ref item) => {
497 tokens = prepend_attrs(sess, &item.attrs, item.tokens.as_ref(), span);
499 Nonterminal::NtIdent(ident) => {
500 let token = Token::Ident(ident.node);
501 tokens = Some(TokenTree::Token(ident.span, token).into());
503 Nonterminal::NtLifetime(lifetime) => {
504 let token = Token::Lifetime(lifetime.ident);
505 tokens = Some(TokenTree::Token(lifetime.span, token).into());
507 Nonterminal::NtTT(ref tt) => {
508 tokens = Some(tt.clone().into());
513 tokens.unwrap_or_else(|| {
515 // FIXME(jseyfried): Avoid this pretty-print + reparse hack
516 let source = pprust::token_to_string(self);
517 parse_stream_from_source_str(FileName::MacroExpansion, source, sess, Some(span))
523 #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash)]
524 /// For interpolation during macro expansion.
525 pub enum Nonterminal {
526 NtItem(P<ast::Item>),
527 NtBlock(P<ast::Block>),
530 NtExpr(P<ast::Expr>),
532 NtIdent(ast::SpannedIdent),
533 /// Stuff inside brackets for attributes
534 NtMeta(ast::MetaItem),
536 NtVis(ast::Visibility),
538 // These are not exposed to macros, but are used by quasiquote.
540 NtImplItem(ast::ImplItem),
541 NtTraitItem(ast::TraitItem),
542 NtGenerics(ast::Generics),
543 NtWhereClause(ast::WhereClause),
545 NtLifetime(ast::Lifetime),
548 impl fmt::Debug for Nonterminal {
549 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
551 NtItem(..) => f.pad("NtItem(..)"),
552 NtBlock(..) => f.pad("NtBlock(..)"),
553 NtStmt(..) => f.pad("NtStmt(..)"),
554 NtPat(..) => f.pad("NtPat(..)"),
555 NtExpr(..) => f.pad("NtExpr(..)"),
556 NtTy(..) => f.pad("NtTy(..)"),
557 NtIdent(..) => f.pad("NtIdent(..)"),
558 NtMeta(..) => f.pad("NtMeta(..)"),
559 NtPath(..) => f.pad("NtPath(..)"),
560 NtTT(..) => f.pad("NtTT(..)"),
561 NtArm(..) => f.pad("NtArm(..)"),
562 NtImplItem(..) => f.pad("NtImplItem(..)"),
563 NtTraitItem(..) => f.pad("NtTraitItem(..)"),
564 NtGenerics(..) => f.pad("NtGenerics(..)"),
565 NtWhereClause(..) => f.pad("NtWhereClause(..)"),
566 NtArg(..) => f.pad("NtArg(..)"),
567 NtVis(..) => f.pad("NtVis(..)"),
568 NtLifetime(..) => f.pad("NtLifetime(..)"),
573 pub fn is_op(tok: &Token) -> bool {
575 OpenDelim(..) | CloseDelim(..) | Literal(..) | DocComment(..) |
576 Ident(..) | Underscore | Lifetime(..) | Interpolated(..) |
577 Whitespace | Comment | Shebang(..) | Eof => false,
582 pub struct LazyTokenStream(Cell<Option<TokenStream>>);
584 impl Clone for LazyTokenStream {
585 fn clone(&self) -> Self {
586 let opt_stream = self.0.take();
587 self.0.set(opt_stream.clone());
588 LazyTokenStream(Cell::new(opt_stream))
592 impl cmp::Eq for LazyTokenStream {}
593 impl PartialEq for LazyTokenStream {
594 fn eq(&self, _other: &LazyTokenStream) -> bool {
599 impl fmt::Debug for LazyTokenStream {
600 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
601 fmt::Debug::fmt(&self.clone().0.into_inner(), f)
605 impl LazyTokenStream {
606 pub fn new() -> Self {
607 LazyTokenStream(Cell::new(None))
610 pub fn force<F: FnOnce() -> TokenStream>(&self, f: F) -> TokenStream {
611 let mut opt_stream = self.0.take();
612 if opt_stream.is_none() {
613 opt_stream = Some(f());
615 self.0.set(opt_stream.clone());
616 opt_stream.clone().unwrap()
620 impl Encodable for LazyTokenStream {
621 fn encode<S: Encoder>(&self, _: &mut S) -> Result<(), S::Error> {
626 impl Decodable for LazyTokenStream {
627 fn decode<D: Decoder>(_: &mut D) -> Result<LazyTokenStream, D::Error> {
628 Ok(LazyTokenStream::new())
632 impl ::std::hash::Hash for LazyTokenStream {
633 fn hash<H: ::std::hash::Hasher>(&self, _hasher: &mut H) {}
636 fn prepend_attrs(sess: &ParseSess,
637 attrs: &[ast::Attribute],
638 tokens: Option<&tokenstream::TokenStream>,
639 span: syntax_pos::Span)
640 -> Option<tokenstream::TokenStream>
642 let tokens = tokens?;
643 if attrs.len() == 0 {
644 return Some(tokens.clone())
646 let mut builder = tokenstream::TokenStreamBuilder::new();
648 assert_eq!(attr.style, ast::AttrStyle::Outer,
649 "inner attributes should prevent cached tokens from existing");
650 // FIXME: Avoid this pretty-print + reparse hack as bove
651 let name = FileName::MacroExpansion;
652 let source = pprust::attr_to_string(attr);
653 let stream = parse_stream_from_source_str(name, source, sess, Some(span));
654 builder.push(stream);
656 builder.push(tokens.clone());
657 Some(builder.build())