2013-05-30 10:16:33 +00:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-04 00:48:01 +00:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-11-06 08:05:53 +00:00
|
|
|
pub use self::BinOpToken::*;
|
|
|
|
pub use self::Nonterminal::*;
|
|
|
|
pub use self::DelimToken::*;
|
2014-11-18 23:17:40 +00:00
|
|
|
pub use self::Lit::*;
|
2014-11-06 08:05:53 +00:00
|
|
|
pub use self::Token::*;
|
|
|
|
|
2016-02-08 12:16:12 +00:00
|
|
|
use ast::{self, BinOpKind};
|
2014-09-13 16:06:01 +00:00
|
|
|
use ptr::P;
|
2016-07-11 08:00:48 +00:00
|
|
|
use util::interner::Interner;
|
2016-06-20 15:49:33 +00:00
|
|
|
use tokenstream;
|
2012-12-23 22:41:37 +00:00
|
|
|
|
2014-02-05 16:52:54 +00:00
|
|
|
use serialize::{Decodable, Decoder, Encodable, Encoder};
|
2016-07-11 20:00:07 +00:00
|
|
|
use std::cell::RefCell;
|
2014-01-08 18:35:15 +00:00
|
|
|
use std::fmt;
|
2014-12-22 17:04:23 +00:00
|
|
|
use std::ops::Deref;
|
2014-03-27 17:28:38 +00:00
|
|
|
use std::rc::Rc;
|
2010-08-18 18:35:12 +00:00
|
|
|
|
2015-01-28 13:34:18 +00:00
|
|
|
#[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash, Debug, Copy)]
|
2014-10-27 08:22:52 +00:00
|
|
|
pub enum BinOpToken {
|
|
|
|
Plus,
|
|
|
|
Minus,
|
|
|
|
Star,
|
|
|
|
Slash,
|
|
|
|
Percent,
|
|
|
|
Caret,
|
|
|
|
And,
|
|
|
|
Or,
|
|
|
|
Shl,
|
|
|
|
Shr,
|
2010-09-09 22:59:29 +00:00
|
|
|
}
|
|
|
|
|
2015-01-07 01:53:18 +00:00
|
|
|
/// A delimiter token
|
2015-01-28 13:34:18 +00:00
|
|
|
#[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash, Debug, Copy)]
|
2014-10-29 10:37:54 +00:00
|
|
|
pub enum DelimToken {
|
|
|
|
/// A round parenthesis: `(` or `)`
|
|
|
|
Paren,
|
|
|
|
/// A square bracket: `[` or `]`
|
|
|
|
Bracket,
|
|
|
|
/// A curly brace: `{` or `}`
|
|
|
|
Brace,
|
2016-07-19 20:00:45 +00:00
|
|
|
/// An empty delimiter
|
|
|
|
NoDelim,
|
2014-10-29 10:37:54 +00:00
|
|
|
}
|
|
|
|
|
2015-01-28 13:34:18 +00:00
|
|
|
#[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash, Debug, Copy)]
|
2014-09-16 01:27:28 +00:00
|
|
|
pub enum SpecialMacroVar {
|
|
|
|
/// `$crate` will be filled in with the name of the crate a macro was
|
|
|
|
/// imported from, if any.
|
|
|
|
CrateMacroVar,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SpecialMacroVar {
|
|
|
|
pub fn as_str(self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
SpecialMacroVar::CrateMacroVar => "crate",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-28 13:34:18 +00:00
|
|
|
#[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash, Debug, Copy)]
|
2014-11-18 23:17:40 +00:00
|
|
|
pub enum Lit {
|
|
|
|
Byte(ast::Name),
|
|
|
|
Char(ast::Name),
|
|
|
|
Integer(ast::Name),
|
|
|
|
Float(ast::Name),
|
|
|
|
Str_(ast::Name),
|
2015-01-17 23:33:05 +00:00
|
|
|
StrRaw(ast::Name, usize), /* raw str delimited by n hash symbols */
|
2015-09-03 07:54:53 +00:00
|
|
|
ByteStr(ast::Name),
|
|
|
|
ByteStrRaw(ast::Name, usize), /* raw byte str delimited by n hash symbols */
|
2014-11-18 23:17:40 +00:00
|
|
|
}
|
|
|
|
|
2014-11-19 04:48:38 +00:00
|
|
|
impl Lit {
|
|
|
|
pub fn short_name(&self) -> &'static str {
|
|
|
|
match *self {
|
|
|
|
Byte(_) => "byte",
|
|
|
|
Char(_) => "char",
|
|
|
|
Integer(_) => "integer",
|
|
|
|
Float(_) => "float",
|
2015-09-03 07:54:53 +00:00
|
|
|
Str_(_) | StrRaw(..) => "string",
|
|
|
|
ByteStr(_) | ByteStrRaw(..) => "byte string"
|
2014-11-19 04:48:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-28 13:34:18 +00:00
|
|
|
#[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash, Debug)]
|
2013-01-29 21:54:06 +00:00
|
|
|
pub enum Token {
|
2010-09-09 22:59:29 +00:00
|
|
|
/* Expression-operator symbols. */
|
2014-10-27 08:22:52 +00:00
|
|
|
Eq,
|
|
|
|
Lt,
|
|
|
|
Le,
|
|
|
|
EqEq,
|
|
|
|
Ne,
|
|
|
|
Ge,
|
|
|
|
Gt,
|
|
|
|
AndAnd,
|
|
|
|
OrOr,
|
|
|
|
Not,
|
|
|
|
Tilde,
|
|
|
|
BinOp(BinOpToken),
|
|
|
|
BinOpEq(BinOpToken),
|
2010-09-09 22:59:29 +00:00
|
|
|
|
|
|
|
/* Structural symbols */
|
2014-10-27 08:22:52 +00:00
|
|
|
At,
|
|
|
|
Dot,
|
|
|
|
DotDot,
|
|
|
|
DotDotDot,
|
|
|
|
Comma,
|
|
|
|
Semi,
|
|
|
|
Colon,
|
|
|
|
ModSep,
|
|
|
|
RArrow,
|
|
|
|
LArrow,
|
|
|
|
FatArrow,
|
|
|
|
Pound,
|
|
|
|
Dollar,
|
|
|
|
Question,
|
2015-01-07 01:53:18 +00:00
|
|
|
/// An opening delimiter, eg. `{`
|
2014-10-29 10:37:54 +00:00
|
|
|
OpenDelim(DelimToken),
|
2015-01-07 01:53:18 +00:00
|
|
|
/// A closing delimiter, eg. `}`
|
2014-10-29 10:37:54 +00:00
|
|
|
CloseDelim(DelimToken),
|
2012-01-25 23:38:09 +00:00
|
|
|
|
2010-09-09 22:59:29 +00:00
|
|
|
/* Literals */
|
2014-11-19 04:48:38 +00:00
|
|
|
Literal(Lit, Option<ast::Name>),
|
2010-09-09 22:59:29 +00:00
|
|
|
|
|
|
|
/* Name components */
|
2016-04-16 01:12:02 +00:00
|
|
|
Ident(ast::Ident),
|
2014-10-27 08:22:52 +00:00
|
|
|
Underscore,
|
|
|
|
Lifetime(ast::Ident),
|
2012-06-12 17:50:17 +00:00
|
|
|
|
2012-06-30 01:26:34 +00:00
|
|
|
/* For interpolation */
|
2014-10-27 08:22:52 +00:00
|
|
|
Interpolated(Nonterminal),
|
2014-10-06 22:00:56 +00:00
|
|
|
// Can be expanded into several tokens.
|
|
|
|
/// Doc comment
|
2014-10-27 08:22:52 +00:00
|
|
|
DocComment(ast::Name),
|
2014-10-06 22:00:56 +00:00
|
|
|
// In left-hand-sides of MBE macros:
|
2016-04-18 19:53:50 +00:00
|
|
|
/// Parse a nonterminal (name to bind, name of NT)
|
2016-04-16 01:12:02 +00:00
|
|
|
MatchNt(ast::Ident, ast::Ident),
|
2014-10-06 22:00:56 +00:00
|
|
|
// In right-hand-sides of MBE macros:
|
|
|
|
/// A syntactic variable that will be filled in by macro expansion.
|
2016-04-16 01:12:02 +00:00
|
|
|
SubstNt(ast::Ident),
|
2014-09-16 01:27:28 +00:00
|
|
|
/// A macro variable with special meaning.
|
|
|
|
SpecialVarNt(SpecialMacroVar),
|
2014-07-05 05:30:39 +00:00
|
|
|
|
|
|
|
// Junk. These carry no data because we don't really care about the data
|
|
|
|
// they *would* carry, and don't really want to allocate a new ident for
|
|
|
|
// them. Instead, users could extract that from the associated span.
|
|
|
|
|
|
|
|
/// Whitespace
|
2014-10-27 08:22:52 +00:00
|
|
|
Whitespace,
|
2014-07-05 05:30:39 +00:00
|
|
|
/// Comment
|
2014-10-27 08:22:52 +00:00
|
|
|
Comment,
|
|
|
|
Shebang(ast::Name),
|
2014-07-05 05:30:39 +00:00
|
|
|
|
2014-10-27 08:22:52 +00:00
|
|
|
Eof,
|
2010-09-09 22:59:29 +00:00
|
|
|
}
|
2010-08-18 18:35:12 +00:00
|
|
|
|
2014-10-27 12:33:30 +00:00
|
|
|
impl Token {
|
2015-04-18 01:18:46 +00:00
|
|
|
/// Returns `true` if the token starts with '>'.
|
|
|
|
pub fn is_like_gt(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
BinOp(Shr) | BinOpEq(Shr) | Gt | Ge => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-27 12:33:30 +00:00
|
|
|
/// Returns `true` if the token can appear at the start of an expression.
|
|
|
|
pub fn can_begin_expr(&self) -> bool {
|
|
|
|
match *self {
|
2014-10-29 10:37:54 +00:00
|
|
|
OpenDelim(_) => true,
|
2016-04-16 01:10:59 +00:00
|
|
|
Ident(..) => true,
|
2014-10-27 12:33:30 +00:00
|
|
|
Underscore => true,
|
|
|
|
Tilde => true,
|
2016-08-26 16:23:42 +00:00
|
|
|
Literal(..) => true,
|
2014-10-27 12:33:30 +00:00
|
|
|
Not => true,
|
|
|
|
BinOp(Minus) => true,
|
|
|
|
BinOp(Star) => true,
|
|
|
|
BinOp(And) => true,
|
|
|
|
BinOp(Or) => true, // in lambda syntax
|
|
|
|
OrOr => true, // in lambda syntax
|
2015-01-10 23:14:03 +00:00
|
|
|
AndAnd => true, // double borrow
|
2016-01-13 06:23:31 +00:00
|
|
|
DotDot | DotDotDot => true, // range notation
|
2014-10-27 12:33:30 +00:00
|
|
|
ModSep => true,
|
|
|
|
Interpolated(NtExpr(..)) => true,
|
|
|
|
Interpolated(NtIdent(..)) => true,
|
|
|
|
Interpolated(NtBlock(..)) => true,
|
|
|
|
Interpolated(NtPath(..)) => true,
|
2015-11-03 16:39:51 +00:00
|
|
|
Pound => true, // for expression attributes
|
2014-10-27 12:33:30 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if the token is any literal
|
|
|
|
pub fn is_lit(&self) -> bool {
|
|
|
|
match *self {
|
2016-08-26 16:23:42 +00:00
|
|
|
Literal(..) => true,
|
|
|
|
_ => false,
|
2014-10-27 12:33:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if the token is an identifier.
|
|
|
|
pub fn is_ident(&self) -> bool {
|
|
|
|
match *self {
|
2016-04-16 01:10:59 +00:00
|
|
|
Ident(..) => true,
|
2014-10-27 12:33:30 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-28 02:05:22 +00:00
|
|
|
/// Returns `true` if the token is a documentation comment.
|
|
|
|
pub fn is_doc_comment(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
DocComment(..) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-24 21:46:39 +00:00
|
|
|
/// Returns `true` if the token is interpolated.
|
|
|
|
pub fn is_interpolated(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
Interpolated(..) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-27 12:33:30 +00:00
|
|
|
/// Returns `true` if the token is an interpolated path.
|
|
|
|
pub fn is_path(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
Interpolated(NtPath(..)) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if the token is a lifetime.
|
|
|
|
pub fn is_lifetime(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
Lifetime(..) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if the token is either the `mut` or `const` keyword.
|
|
|
|
pub fn is_mutability(&self) -> bool {
|
|
|
|
self.is_keyword(keywords::Mut) ||
|
|
|
|
self.is_keyword(keywords::Const)
|
|
|
|
}
|
|
|
|
|
2016-04-20 23:03:29 +00:00
|
|
|
pub fn is_path_start(&self) -> bool {
|
|
|
|
self == &ModSep || self == &Lt || self.is_path() ||
|
|
|
|
self.is_path_segment_keyword() || self.is_ident() && !self.is_any_keyword()
|
|
|
|
}
|
|
|
|
|
2014-10-27 12:33:30 +00:00
|
|
|
/// Maps a token to its corresponding binary operator.
|
2016-02-08 12:16:12 +00:00
|
|
|
pub fn to_binop(&self) -> Option<BinOpKind> {
|
2014-10-27 12:33:30 +00:00
|
|
|
match *self {
|
2016-02-08 12:16:12 +00:00
|
|
|
BinOp(Star) => Some(BinOpKind::Mul),
|
|
|
|
BinOp(Slash) => Some(BinOpKind::Div),
|
|
|
|
BinOp(Percent) => Some(BinOpKind::Rem),
|
|
|
|
BinOp(Plus) => Some(BinOpKind::Add),
|
|
|
|
BinOp(Minus) => Some(BinOpKind::Sub),
|
|
|
|
BinOp(Shl) => Some(BinOpKind::Shl),
|
|
|
|
BinOp(Shr) => Some(BinOpKind::Shr),
|
|
|
|
BinOp(And) => Some(BinOpKind::BitAnd),
|
|
|
|
BinOp(Caret) => Some(BinOpKind::BitXor),
|
|
|
|
BinOp(Or) => Some(BinOpKind::BitOr),
|
|
|
|
Lt => Some(BinOpKind::Lt),
|
|
|
|
Le => Some(BinOpKind::Le),
|
|
|
|
Ge => Some(BinOpKind::Ge),
|
|
|
|
Gt => Some(BinOpKind::Gt),
|
|
|
|
EqEq => Some(BinOpKind::Eq),
|
|
|
|
Ne => Some(BinOpKind::Ne),
|
|
|
|
AndAnd => Some(BinOpKind::And),
|
|
|
|
OrOr => Some(BinOpKind::Or),
|
2014-10-27 12:33:30 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if the token is a given keyword, `kw`.
|
|
|
|
pub fn is_keyword(&self, kw: keywords::Keyword) -> bool {
|
|
|
|
match *self {
|
2016-04-18 19:53:50 +00:00
|
|
|
Ident(id) => id.name == kw.name(),
|
2016-04-16 01:10:59 +00:00
|
|
|
_ => false,
|
2014-10-27 12:33:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-16 01:10:59 +00:00
|
|
|
pub fn is_path_segment_keyword(&self) -> bool {
|
2014-09-16 01:27:28 +00:00
|
|
|
match *self {
|
2016-04-18 19:53:50 +00:00
|
|
|
Ident(id) => id.name == keywords::Super.name() ||
|
|
|
|
id.name == keywords::SelfValue.name() ||
|
|
|
|
id.name == keywords::SelfType.name(),
|
2016-04-16 01:10:59 +00:00
|
|
|
_ => false,
|
2014-09-16 01:27:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 19:53:50 +00:00
|
|
|
/// Returns `true` if the token is either a strict or reserved keyword.
|
2014-10-27 12:33:30 +00:00
|
|
|
pub fn is_any_keyword(&self) -> bool {
|
2016-04-18 19:53:50 +00:00
|
|
|
self.is_strict_keyword() || self.is_reserved_keyword()
|
2014-10-27 12:33:30 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 19:53:50 +00:00
|
|
|
/// Returns `true` if the token is a strict keyword.
|
|
|
|
pub fn is_strict_keyword(&self) -> bool {
|
2014-10-27 12:33:30 +00:00
|
|
|
match *self {
|
2016-04-18 19:53:50 +00:00
|
|
|
Ident(id) => id.name >= keywords::As.name() &&
|
|
|
|
id.name <= keywords::While.name(),
|
2014-10-27 12:33:30 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-16 15:05:06 +00:00
|
|
|
/// Returns `true` if the token is a keyword reserved for possible future use.
|
2014-10-27 12:33:30 +00:00
|
|
|
pub fn is_reserved_keyword(&self) -> bool {
|
|
|
|
match *self {
|
2016-04-18 19:53:50 +00:00
|
|
|
Ident(id) => id.name >= keywords::Abstract.name() &&
|
|
|
|
id.name <= keywords::Yield.name(),
|
2014-10-27 12:33:30 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-04 03:54:18 +00:00
|
|
|
#[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash)]
|
2012-07-04 21:53:12 +00:00
|
|
|
/// For interpolation during macro expansion.
|
2014-01-09 13:05:33 +00:00
|
|
|
pub enum Nonterminal {
|
2014-10-29 21:44:41 +00:00
|
|
|
NtItem(P<ast::Item>),
|
2014-01-09 13:05:33 +00:00
|
|
|
NtBlock(P<ast::Block>),
|
2014-10-29 21:44:41 +00:00
|
|
|
NtStmt(P<ast::Stmt>),
|
|
|
|
NtPat(P<ast::Pat>),
|
|
|
|
NtExpr(P<ast::Expr>),
|
|
|
|
NtTy(P<ast::Ty>),
|
2016-04-16 01:12:02 +00:00
|
|
|
NtIdent(Box<ast::SpannedIdent>),
|
2014-06-09 20:19:38 +00:00
|
|
|
/// Stuff inside brackets for attributes
|
2014-10-29 21:44:41 +00:00
|
|
|
NtMeta(P<ast::MetaItem>),
|
2014-05-06 01:56:44 +00:00
|
|
|
NtPath(Box<ast::Path>),
|
2016-06-20 15:49:33 +00:00
|
|
|
NtTT(P<tokenstream::TokenTree>), // needs P'ed to break a circularity
|
2015-11-11 20:19:01 +00:00
|
|
|
// These are not exposed to macros, but are used by quasiquote.
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 20:06:49 +00:00
|
|
|
NtArm(ast::Arm),
|
|
|
|
NtImplItem(P<ast::ImplItem>),
|
|
|
|
NtTraitItem(P<ast::TraitItem>),
|
2015-05-02 17:55:41 +00:00
|
|
|
NtGenerics(ast::Generics),
|
|
|
|
NtWhereClause(ast::WhereClause),
|
2015-11-11 20:19:01 +00:00
|
|
|
NtArg(ast::Arg),
|
2012-06-12 17:50:17 +00:00
|
|
|
}
|
|
|
|
|
2015-01-20 23:45:07 +00:00
|
|
|
impl fmt::Debug for Nonterminal {
|
2014-02-28 09:23:06 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
|
|
|
NtItem(..) => f.pad("NtItem(..)"),
|
|
|
|
NtBlock(..) => f.pad("NtBlock(..)"),
|
|
|
|
NtStmt(..) => f.pad("NtStmt(..)"),
|
|
|
|
NtPat(..) => f.pad("NtPat(..)"),
|
|
|
|
NtExpr(..) => f.pad("NtExpr(..)"),
|
|
|
|
NtTy(..) => f.pad("NtTy(..)"),
|
|
|
|
NtIdent(..) => f.pad("NtIdent(..)"),
|
2014-03-26 23:14:07 +00:00
|
|
|
NtMeta(..) => f.pad("NtMeta(..)"),
|
2014-02-28 09:23:06 +00:00
|
|
|
NtPath(..) => f.pad("NtPath(..)"),
|
|
|
|
NtTT(..) => f.pad("NtTT(..)"),
|
Interpolate AST nodes in quasiquote.
This changes the `ToTokens` implementations for expressions, statements,
etc. with almost-trivial ones that produce `Interpolated(*Nt(...))`
pseudo-tokens. In this way, quasiquote now works the same way as macros
do: already-parsed AST fragments are used as-is, not reparsed.
The `ToSource` trait is removed. Quasiquote no longer involves
pretty-printing at all, which removes the need for the
`encode_with_hygiene` hack. All associated machinery is removed.
A new `Nonterminal` is added, NtArm, which the parser now interpolates.
This is just for quasiquote, not macros (although it could be in the
future).
`ToTokens` is no longer implemented for `Arg` (although this could be
added again) and `Generics` (which I don't think makes sense).
This breaks any compiler extensions that relied on the ability of
`ToTokens` to turn AST fragments back into inspectable token trees. For
this reason, this closes #16987.
As such, this is a [breaking-change].
Fixes #16472.
Fixes #15962.
Fixes #17397.
Fixes #16617.
2015-03-05 20:06:49 +00:00
|
|
|
NtArm(..) => f.pad("NtArm(..)"),
|
|
|
|
NtImplItem(..) => f.pad("NtImplItem(..)"),
|
|
|
|
NtTraitItem(..) => f.pad("NtTraitItem(..)"),
|
2015-05-02 17:55:41 +00:00
|
|
|
NtGenerics(..) => f.pad("NtGenerics(..)"),
|
|
|
|
NtWhereClause(..) => f.pad("NtWhereClause(..)"),
|
2015-11-11 20:19:01 +00:00
|
|
|
NtArg(..) => f.pad("NtArg(..)"),
|
2014-02-28 09:23:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-10 06:08:48 +00:00
|
|
|
// In this macro, there is the requirement that the name (the number) must be monotonically
|
|
|
|
// increasing by one in the special identifiers, starting at 0; the same holds for the keywords,
|
2016-04-16 15:05:06 +00:00
|
|
|
// except starting from the next number instead of zero.
|
2016-04-18 19:53:50 +00:00
|
|
|
macro_rules! declare_keywords {(
|
|
|
|
$( ($index: expr, $konst: ident, $string: expr) )*
|
2013-12-10 06:08:48 +00:00
|
|
|
) => {
|
|
|
|
pub mod keywords {
|
2014-10-27 08:22:52 +00:00
|
|
|
use ast;
|
2016-04-16 15:05:06 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub struct Keyword {
|
2016-04-18 19:53:50 +00:00
|
|
|
ident: ast::Ident,
|
|
|
|
}
|
|
|
|
impl Keyword {
|
|
|
|
#[inline] pub fn ident(self) -> ast::Ident { self.ident }
|
|
|
|
#[inline] pub fn name(self) -> ast::Name { self.ident.name }
|
2013-12-10 06:08:48 +00:00
|
|
|
}
|
2016-04-16 15:05:06 +00:00
|
|
|
$(
|
|
|
|
#[allow(non_upper_case_globals)]
|
2016-04-18 19:53:50 +00:00
|
|
|
pub const $konst: Keyword = Keyword {
|
|
|
|
ident: ast::Ident::with_empty_ctxt(ast::Name($index))
|
2016-04-16 15:05:06 +00:00
|
|
|
};
|
|
|
|
)*
|
2013-12-10 06:08:48 +00:00
|
|
|
}
|
|
|
|
|
2014-02-14 05:07:09 +00:00
|
|
|
fn mk_fresh_ident_interner() -> IdentInterner {
|
2016-07-11 08:00:48 +00:00
|
|
|
Interner::prefill(&[$($string,)*])
|
2013-12-10 06:08:48 +00:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
|
2014-05-20 18:59:07 +00:00
|
|
|
// NB: leaving holes in the ident table is bad! a different ident will get
|
|
|
|
// interned with the id from the hole, but it will be between the min and max
|
|
|
|
// of the reserved words, and thus tagged as "reserved".
|
2016-04-18 19:53:50 +00:00
|
|
|
// After modifying this list adjust `is_strict_keyword`/`is_reserved_keyword`,
|
|
|
|
// this should be rarely necessary though if the keywords are kept in alphabetic order.
|
|
|
|
declare_keywords! {
|
|
|
|
// Invalid identifier
|
|
|
|
(0, Invalid, "")
|
|
|
|
|
|
|
|
// Strict keywords used in the language.
|
|
|
|
(1, As, "as")
|
|
|
|
(2, Box, "box")
|
|
|
|
(3, Break, "break")
|
|
|
|
(4, Const, "const")
|
|
|
|
(5, Continue, "continue")
|
|
|
|
(6, Crate, "crate")
|
|
|
|
(7, Else, "else")
|
|
|
|
(8, Enum, "enum")
|
|
|
|
(9, Extern, "extern")
|
|
|
|
(10, False, "false")
|
|
|
|
(11, Fn, "fn")
|
|
|
|
(12, For, "for")
|
|
|
|
(13, If, "if")
|
|
|
|
(14, Impl, "impl")
|
|
|
|
(15, In, "in")
|
|
|
|
(16, Let, "let")
|
|
|
|
(17, Loop, "loop")
|
|
|
|
(18, Match, "match")
|
|
|
|
(19, Mod, "mod")
|
|
|
|
(20, Move, "move")
|
|
|
|
(21, Mut, "mut")
|
|
|
|
(22, Pub, "pub")
|
|
|
|
(23, Ref, "ref")
|
|
|
|
(24, Return, "return")
|
|
|
|
(25, SelfValue, "self")
|
|
|
|
(26, SelfType, "Self")
|
|
|
|
(27, Static, "static")
|
|
|
|
(28, Struct, "struct")
|
|
|
|
(29, Super, "super")
|
|
|
|
(30, Trait, "trait")
|
|
|
|
(31, True, "true")
|
|
|
|
(32, Type, "type")
|
|
|
|
(33, Unsafe, "unsafe")
|
|
|
|
(34, Use, "use")
|
|
|
|
(35, Where, "where")
|
|
|
|
(36, While, "while")
|
|
|
|
|
|
|
|
// Keywords reserved for future use.
|
|
|
|
(37, Abstract, "abstract")
|
|
|
|
(38, Alignof, "alignof")
|
|
|
|
(39, Become, "become")
|
|
|
|
(40, Do, "do")
|
|
|
|
(41, Final, "final")
|
|
|
|
(42, Macro, "macro")
|
|
|
|
(43, Offsetof, "offsetof")
|
|
|
|
(44, Override, "override")
|
|
|
|
(45, Priv, "priv")
|
|
|
|
(46, Proc, "proc")
|
|
|
|
(47, Pure, "pure")
|
|
|
|
(48, Sizeof, "sizeof")
|
|
|
|
(49, Typeof, "typeof")
|
|
|
|
(50, Unsized, "unsized")
|
|
|
|
(51, Virtual, "virtual")
|
|
|
|
(52, Yield, "yield")
|
|
|
|
|
|
|
|
// Weak keywords, have special meaning only in specific contexts.
|
|
|
|
(53, Default, "default")
|
|
|
|
(54, StaticLifetime, "'static")
|
|
|
|
(55, Union, "union")
|
2013-12-10 06:08:48 +00:00
|
|
|
}
|
2013-07-03 22:15:45 +00:00
|
|
|
|
2013-06-26 17:11:19 +00:00
|
|
|
// looks like we can get rid of this completely...
|
2016-07-11 08:00:48 +00:00
|
|
|
pub type IdentInterner = Interner;
|
2013-06-20 01:12:40 +00:00
|
|
|
|
2013-04-23 17:57:41 +00:00
|
|
|
// if an interner exists in TLS, return it. Otherwise, prepare a
|
|
|
|
// fresh one.
|
2015-05-08 15:12:29 +00:00
|
|
|
// FIXME(eddyb) #8726 This should probably use a thread-local reference.
|
2016-07-11 20:00:07 +00:00
|
|
|
pub fn with_ident_interner<T, F: FnOnce(&mut IdentInterner) -> T>(f: F) -> T {
|
|
|
|
thread_local!(static KEY: RefCell<IdentInterner> = {
|
|
|
|
RefCell::new(mk_fresh_ident_interner())
|
2014-11-14 17:18:10 +00:00
|
|
|
});
|
2016-07-11 20:00:07 +00:00
|
|
|
KEY.with(|interner| f(&mut *interner.borrow_mut()))
|
2012-08-02 21:33:26 +00:00
|
|
|
}
|
|
|
|
|
2014-11-29 04:56:09 +00:00
|
|
|
/// Reset the ident interner to its initial state.
|
|
|
|
pub fn reset_ident_interner() {
|
2016-07-11 20:00:07 +00:00
|
|
|
with_ident_interner(|interner| *interner = mk_fresh_ident_interner());
|
2016-07-11 19:33:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn clear_ident_interner() {
|
2016-07-11 20:00:07 +00:00
|
|
|
with_ident_interner(|interner| *interner = IdentInterner::new());
|
2014-11-29 04:56:09 +00:00
|
|
|
}
|
|
|
|
|
2015-05-08 15:12:29 +00:00
|
|
|
/// Represents a string stored in the thread-local interner. Because the
|
|
|
|
/// interner lives for the life of the thread, this can be safely treated as an
|
|
|
|
/// immortal string, as long as it never crosses between threads.
|
2014-01-08 18:35:15 +00:00
|
|
|
///
|
2014-02-01 02:25:08 +00:00
|
|
|
/// FIXME(pcwalton): You must be careful about what you do in the destructors
|
|
|
|
/// of objects stored in TLS, because they may run after the interner is
|
2014-01-08 18:35:15 +00:00
|
|
|
/// destroyed. In particular, they must not access string contents. This can
|
2015-05-08 15:12:29 +00:00
|
|
|
/// be fixed in the future by just leaking all strings until thread death
|
2014-01-08 18:35:15 +00:00
|
|
|
/// somehow.
|
2015-01-04 03:54:18 +00:00
|
|
|
#[derive(Clone, PartialEq, Hash, PartialOrd, Eq, Ord)]
|
2014-01-08 18:35:15 +00:00
|
|
|
pub struct InternedString {
|
2016-07-11 07:55:54 +00:00
|
|
|
string: Rc<String>,
|
2014-01-08 18:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl InternedString {
|
|
|
|
#[inline]
|
|
|
|
pub fn new(string: &'static str) -> InternedString {
|
|
|
|
InternedString {
|
2016-07-11 07:55:54 +00:00
|
|
|
string: Rc::new(string.to_owned()),
|
2014-01-08 18:35:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2016-07-11 07:55:54 +00:00
|
|
|
fn new_from_rc_str(string: Rc<String>) -> InternedString {
|
2014-01-08 18:35:15 +00:00
|
|
|
InternedString {
|
|
|
|
string: string,
|
|
|
|
}
|
|
|
|
}
|
2015-07-28 16:07:20 +00:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn new_from_name(name: ast::Name) -> InternedString {
|
2016-07-11 19:33:40 +00:00
|
|
|
with_ident_interner(|interner| InternedString::new_from_rc_str(interner.get(name)))
|
2015-07-28 16:07:20 +00:00
|
|
|
}
|
2015-01-22 11:28:44 +00:00
|
|
|
}
|
|
|
|
|
2015-01-01 19:53:20 +00:00
|
|
|
impl Deref for InternedString {
|
|
|
|
type Target = str;
|
|
|
|
|
2016-02-08 22:55:55 +00:00
|
|
|
fn deref(&self) -> &str { &self.string }
|
2014-12-11 03:46:38 +00:00
|
|
|
}
|
|
|
|
|
2015-01-20 23:45:07 +00:00
|
|
|
impl fmt::Debug for InternedString {
|
2014-12-20 08:09:35 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-02-20 19:08:14 +00:00
|
|
|
fmt::Debug::fmt(&self.string, f)
|
2014-12-20 08:09:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-20 23:45:07 +00:00
|
|
|
impl fmt::Display for InternedString {
|
2014-02-05 12:55:13 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2015-02-20 19:08:14 +00:00
|
|
|
fmt::Display::fmt(&self.string, f)
|
2014-01-08 18:35:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-21 05:14:05 +00:00
|
|
|
impl<'a> PartialEq<&'a str> for InternedString {
|
|
|
|
#[inline(always)]
|
|
|
|
fn eq(&self, other: & &'a str) -> bool {
|
2015-02-20 19:08:14 +00:00
|
|
|
PartialEq::eq(&self.string[..], *other)
|
2014-11-21 05:14:05 +00:00
|
|
|
}
|
|
|
|
#[inline(always)]
|
|
|
|
fn ne(&self, other: & &'a str) -> bool {
|
2015-02-20 19:08:14 +00:00
|
|
|
PartialEq::ne(&self.string[..], *other)
|
2014-11-21 05:14:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-28 16:07:20 +00:00
|
|
|
impl<'a> PartialEq<InternedString> for &'a str {
|
2014-11-21 05:14:05 +00:00
|
|
|
#[inline(always)]
|
|
|
|
fn eq(&self, other: &InternedString) -> bool {
|
2015-02-20 19:08:14 +00:00
|
|
|
PartialEq::eq(*self, &other.string[..])
|
2014-11-21 05:14:05 +00:00
|
|
|
}
|
|
|
|
#[inline(always)]
|
|
|
|
fn ne(&self, other: &InternedString) -> bool {
|
2015-02-20 19:08:14 +00:00
|
|
|
PartialEq::ne(*self, &other.string[..])
|
2014-11-21 05:14:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-26 14:27:10 +00:00
|
|
|
impl PartialEq<str> for InternedString {
|
|
|
|
#[inline(always)]
|
|
|
|
fn eq(&self, other: &str) -> bool {
|
|
|
|
PartialEq::eq(&self.string[..], other)
|
|
|
|
}
|
|
|
|
#[inline(always)]
|
|
|
|
fn ne(&self, other: &str) -> bool {
|
|
|
|
PartialEq::ne(&self.string[..], other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq<InternedString> for str {
|
|
|
|
#[inline(always)]
|
|
|
|
fn eq(&self, other: &InternedString) -> bool {
|
|
|
|
PartialEq::eq(self, &other.string[..])
|
|
|
|
}
|
|
|
|
#[inline(always)]
|
|
|
|
fn ne(&self, other: &InternedString) -> bool {
|
|
|
|
PartialEq::ne(self, &other.string[..])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-04 06:24:50 +00:00
|
|
|
impl Decodable for InternedString {
|
|
|
|
fn decode<D: Decoder>(d: &mut D) -> Result<InternedString, D::Error> {
|
2016-03-23 03:01:37 +00:00
|
|
|
Ok(intern(d.read_str()?.as_ref()).as_str())
|
2015-01-04 06:24:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Encodable for InternedString {
|
|
|
|
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
|
2015-02-20 19:08:14 +00:00
|
|
|
s.emit_str(&self.string)
|
2015-01-04 06:24:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-08 18:35:15 +00:00
|
|
|
/// Interns and returns the string contents of an identifier, using the
|
2015-05-08 15:12:29 +00:00
|
|
|
/// thread-local interner.
|
2014-01-08 18:35:15 +00:00
|
|
|
#[inline]
|
|
|
|
pub fn intern_and_get_ident(s: &str) -> InternedString {
|
2015-07-28 16:07:20 +00:00
|
|
|
intern(s).as_str()
|
2012-07-18 23:18:02 +00:00
|
|
|
}
|
|
|
|
|
2014-02-14 05:07:09 +00:00
|
|
|
/// Maps a string to its interned representation.
|
2014-01-08 18:35:15 +00:00
|
|
|
#[inline]
|
2014-10-27 08:22:52 +00:00
|
|
|
pub fn intern(s: &str) -> ast::Name {
|
2016-07-11 19:33:40 +00:00
|
|
|
with_ident_interner(|interner| interner.intern(s))
|
2013-06-04 19:34:25 +00:00
|
|
|
}
|
|
|
|
|
2015-01-17 23:33:05 +00:00
|
|
|
/// gensym's a new usize, using the current interner.
|
2014-02-14 05:07:09 +00:00
|
|
|
#[inline]
|
2014-10-27 08:22:52 +00:00
|
|
|
pub fn gensym(s: &str) -> ast::Name {
|
2016-07-11 19:33:40 +00:00
|
|
|
with_ident_interner(|interner| interner.gensym(s))
|
2013-06-04 19:34:25 +00:00
|
|
|
}
|
|
|
|
|
2014-02-14 05:07:09 +00:00
|
|
|
/// Maps a string to an identifier with an empty syntax context.
|
|
|
|
#[inline]
|
2014-10-27 08:22:52 +00:00
|
|
|
pub fn str_to_ident(s: &str) -> ast::Ident {
|
2015-09-24 20:05:02 +00:00
|
|
|
ast::Ident::with_empty_ctxt(intern(s))
|
2013-06-04 19:34:25 +00:00
|
|
|
}
|
|
|
|
|
2014-02-14 05:07:09 +00:00
|
|
|
/// Maps a string to a gensym'ed identifier.
|
|
|
|
#[inline]
|
2014-10-27 08:22:52 +00:00
|
|
|
pub fn gensym_ident(s: &str) -> ast::Ident {
|
2015-09-24 20:05:02 +00:00
|
|
|
ast::Ident::with_empty_ctxt(gensym(s))
|
2013-05-07 19:34:52 +00:00
|
|
|
}
|
|
|
|
|
2013-06-07 17:41:18 +00:00
|
|
|
// create a fresh name that maps to the same string as the old one.
|
2014-06-21 10:39:03 +00:00
|
|
|
// note that this guarantees that str_ptr_eq(ident_to_string(src),interner_get(fresh_name(src)));
|
2013-06-25 23:48:03 +00:00
|
|
|
// that is, that the new name and the old one are connected to ptr_eq strings.
|
2015-09-24 20:05:02 +00:00
|
|
|
pub fn fresh_name(src: ast::Ident) -> ast::Name {
|
2016-07-11 19:33:40 +00:00
|
|
|
with_ident_interner(|interner| interner.gensym_copy(src.name))
|
2013-06-07 21:53:53 +00:00
|
|
|
// following: debug version. Could work in final except that it's incompatible with
|
|
|
|
// good error messages and uses of struct names in ambiguous could-be-binding
|
2013-06-25 23:48:03 +00:00
|
|
|
// locations. Also definitely destroys the guarantee given above about ptr_eq.
|
2014-12-28 00:20:47 +00:00
|
|
|
/*let num = rand::thread_rng().gen_uint_range(0,0xffff);
|
2014-06-21 10:39:03 +00:00
|
|
|
gensym(format!("{}_{}",ident_to_string(src),num))*/
|
2013-05-14 18:34:17 +00:00
|
|
|
}
|