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::*;
|
|
|
|
pub use self::IdentStyle::*;
|
2014-11-18 23:17:40 +00:00
|
|
|
pub use self::Lit::*;
|
2014-11-06 08:05:53 +00:00
|
|
|
pub use self::Token::*;
|
|
|
|
|
2012-12-23 22:41:37 +00:00
|
|
|
use ast;
|
2014-02-24 20:47:19 +00:00
|
|
|
use ext::mtwt;
|
2014-09-13 16:06:01 +00:00
|
|
|
use ptr::P;
|
2014-02-01 00:42:33 +00:00
|
|
|
use util::interner::{RcStr, StrInterner};
|
2012-12-23 22:41:37 +00:00
|
|
|
use util::interner;
|
|
|
|
|
2014-02-05 16:52:54 +00:00
|
|
|
use serialize::{Decodable, Decoder, Encodable, Encoder};
|
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
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
#[allow(non_camel_case_types)]
|
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,
|
|
|
|
}
|
|
|
|
|
2015-01-28 13:34:18 +00:00
|
|
|
#[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash, Debug, Copy)]
|
2014-10-27 15:01:44 +00:00
|
|
|
pub enum IdentStyle {
|
|
|
|
/// `::` follows the identifier with no whitespace in-between.
|
|
|
|
ModName,
|
|
|
|
Plain,
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
#[allow(non_camel_case_types)]
|
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 */
|
2014-10-27 15:01:44 +00:00
|
|
|
Ident(ast::Ident, IdentStyle),
|
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:
|
|
|
|
/// Parse a nonterminal (name to bind, name of NT, styles of their idents)
|
|
|
|
MatchNt(ast::Ident, ast::Ident, IdentStyle, IdentStyle),
|
|
|
|
// In right-hand-sides of MBE macros:
|
|
|
|
/// A syntactic variable that will be filled in by macro expansion.
|
|
|
|
SubstNt(ast::Ident, IdentStyle),
|
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,
|
2014-10-27 12:33:30 +00:00
|
|
|
Ident(_, _) => true,
|
|
|
|
Underscore => true,
|
|
|
|
Tilde => true,
|
2014-11-19 04:48:38 +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
|
|
|
|
DotDot => 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 {
|
2014-11-19 04:48:38 +00:00
|
|
|
Literal(_, _) => true,
|
2014-11-18 23:17:40 +00:00
|
|
|
_ => 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 {
|
|
|
|
Ident(_, _) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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 path that is not followed by a `::`
|
|
|
|
/// token.
|
2014-10-30 16:13:02 +00:00
|
|
|
#[allow(non_upper_case_globals)]
|
2014-10-27 12:33:30 +00:00
|
|
|
pub fn is_plain_ident(&self) -> bool {
|
|
|
|
match *self {
|
2014-10-27 15:01:44 +00:00
|
|
|
Ident(_, Plain) => true,
|
2014-10-29 21:44:41 +00:00
|
|
|
_ => false,
|
2014-10-27 12:33:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Maps a token to its corresponding binary operator.
|
2015-01-13 03:24:37 +00:00
|
|
|
pub fn to_binop(&self) -> Option<ast::BinOp_> {
|
2014-10-27 12:33:30 +00:00
|
|
|
match *self {
|
|
|
|
BinOp(Star) => Some(ast::BiMul),
|
|
|
|
BinOp(Slash) => Some(ast::BiDiv),
|
|
|
|
BinOp(Percent) => Some(ast::BiRem),
|
|
|
|
BinOp(Plus) => Some(ast::BiAdd),
|
|
|
|
BinOp(Minus) => Some(ast::BiSub),
|
|
|
|
BinOp(Shl) => Some(ast::BiShl),
|
|
|
|
BinOp(Shr) => Some(ast::BiShr),
|
|
|
|
BinOp(And) => Some(ast::BiBitAnd),
|
|
|
|
BinOp(Caret) => Some(ast::BiBitXor),
|
|
|
|
BinOp(Or) => Some(ast::BiBitOr),
|
|
|
|
Lt => Some(ast::BiLt),
|
|
|
|
Le => Some(ast::BiLe),
|
|
|
|
Ge => Some(ast::BiGe),
|
|
|
|
Gt => Some(ast::BiGt),
|
|
|
|
EqEq => Some(ast::BiEq),
|
|
|
|
Ne => Some(ast::BiNe),
|
|
|
|
AndAnd => Some(ast::BiAnd),
|
|
|
|
OrOr => Some(ast::BiOr),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if the token is a given keyword, `kw`.
|
2014-10-30 16:13:02 +00:00
|
|
|
#[allow(non_upper_case_globals)]
|
2014-10-27 12:33:30 +00:00
|
|
|
pub fn is_keyword(&self, kw: keywords::Keyword) -> bool {
|
|
|
|
match *self {
|
2014-10-27 15:01:44 +00:00
|
|
|
Ident(sid, Plain) => kw.to_name() == sid.name,
|
|
|
|
_ => false,
|
2014-10-27 12:33:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-16 01:27:28 +00:00
|
|
|
pub fn is_keyword_allow_following_colon(&self, kw: keywords::Keyword) -> bool {
|
|
|
|
match *self {
|
|
|
|
Ident(sid, _) => { kw.to_name() == sid.name }
|
|
|
|
_ => { false }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-27 12:33:30 +00:00
|
|
|
/// Returns `true` if the token is either a special identifier, or a strict
|
|
|
|
/// or reserved keyword.
|
2014-10-30 16:13:02 +00:00
|
|
|
#[allow(non_upper_case_globals)]
|
2014-10-27 12:33:30 +00:00
|
|
|
pub fn is_any_keyword(&self) -> bool {
|
|
|
|
match *self {
|
2014-10-27 15:01:44 +00:00
|
|
|
Ident(sid, Plain) => {
|
2014-10-27 12:33:30 +00:00
|
|
|
let n = sid.name;
|
|
|
|
|
|
|
|
n == SELF_KEYWORD_NAME
|
|
|
|
|| n == STATIC_KEYWORD_NAME
|
|
|
|
|| n == SUPER_KEYWORD_NAME
|
2015-02-10 13:55:45 +00:00
|
|
|
|| n == SELF_TYPE_KEYWORD_NAME
|
2014-10-27 12:33:30 +00:00
|
|
|
|| STRICT_KEYWORD_START <= n
|
|
|
|
&& n <= RESERVED_KEYWORD_FINAL
|
|
|
|
},
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if the token may not appear as an identifier.
|
2014-10-30 16:13:02 +00:00
|
|
|
#[allow(non_upper_case_globals)]
|
2014-10-27 12:33:30 +00:00
|
|
|
pub fn is_strict_keyword(&self) -> bool {
|
|
|
|
match *self {
|
2014-10-27 15:01:44 +00:00
|
|
|
Ident(sid, Plain) => {
|
2014-10-27 12:33:30 +00:00
|
|
|
let n = sid.name;
|
|
|
|
|
|
|
|
n == SELF_KEYWORD_NAME
|
|
|
|
|| n == STATIC_KEYWORD_NAME
|
|
|
|
|| n == SUPER_KEYWORD_NAME
|
2015-02-10 13:55:45 +00:00
|
|
|
|| n == SELF_TYPE_KEYWORD_NAME
|
2014-10-27 12:33:30 +00:00
|
|
|
|| STRICT_KEYWORD_START <= n
|
|
|
|
&& n <= STRICT_KEYWORD_FINAL
|
|
|
|
},
|
2014-10-27 15:01:44 +00:00
|
|
|
Ident(sid, ModName) => {
|
2014-10-27 12:33:30 +00:00
|
|
|
let n = sid.name;
|
|
|
|
|
|
|
|
n != SELF_KEYWORD_NAME
|
|
|
|
&& n != SUPER_KEYWORD_NAME
|
|
|
|
&& STRICT_KEYWORD_START <= n
|
|
|
|
&& n <= STRICT_KEYWORD_FINAL
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if the token is a keyword that has been reserved for
|
|
|
|
/// possible future use.
|
2014-10-30 16:13:02 +00:00
|
|
|
#[allow(non_upper_case_globals)]
|
2014-10-27 12:33:30 +00:00
|
|
|
pub fn is_reserved_keyword(&self) -> bool {
|
|
|
|
match *self {
|
2014-10-27 15:01:44 +00:00
|
|
|
Ident(sid, Plain) => {
|
2014-10-27 12:33:30 +00:00
|
|
|
let n = sid.name;
|
|
|
|
|
|
|
|
RESERVED_KEYWORD_START <= n
|
|
|
|
&& n <= RESERVED_KEYWORD_FINAL
|
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Hygienic identifier equality comparison.
|
|
|
|
///
|
|
|
|
/// See `styntax::ext::mtwt`.
|
|
|
|
pub fn mtwt_eq(&self, other : &Token) -> bool {
|
|
|
|
match (self, other) {
|
|
|
|
(&Ident(id1,_), &Ident(id2,_)) | (&Lifetime(id1), &Lifetime(id2)) =>
|
|
|
|
mtwt::resolve(id1) == mtwt::resolve(id2),
|
|
|
|
_ => *self == *other
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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>),
|
2015-12-15 04:31:54 +00:00
|
|
|
NtIdent(Box<ast::SpannedIdent>, IdentStyle),
|
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>),
|
2014-10-29 21:44:41 +00:00
|
|
|
NtTT(P<ast::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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-02 21:41:24 +00:00
|
|
|
|
2013-12-11 04:15:55 +00:00
|
|
|
// Get the first "argument"
|
|
|
|
macro_rules! first {
|
|
|
|
( $first:expr, $( $remainder:expr, )* ) => ( $first )
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the last "argument" (has to be done recursively to avoid phoney local ambiguity error)
|
|
|
|
macro_rules! last {
|
|
|
|
( $first:expr, $( $remainder:expr, )+ ) => ( last!( $( $remainder, )+ ) );
|
|
|
|
( $first:expr, ) => ( $first )
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
// except starting from the next number instead of zero, and with the additional exception that
|
|
|
|
// special identifiers are *also* allowed (they are deduplicated in the important place, the
|
|
|
|
// interner), an exception which is demonstrated by "static" and "self".
|
|
|
|
macro_rules! declare_special_idents_and_keywords {(
|
|
|
|
// So now, in these rules, why is each definition parenthesised?
|
|
|
|
// Answer: otherwise we get a spurious local ambiguity bug on the "}"
|
|
|
|
pub mod special_idents {
|
|
|
|
$( ($si_name:expr, $si_static:ident, $si_str:expr); )*
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod keywords {
|
2013-12-11 04:15:55 +00:00
|
|
|
'strict:
|
|
|
|
$( ($sk_name:expr, $sk_variant:ident, $sk_str:expr); )*
|
|
|
|
'reserved:
|
|
|
|
$( ($rk_name:expr, $rk_variant:ident, $rk_str:expr); )*
|
2013-12-10 06:08:48 +00:00
|
|
|
}
|
|
|
|
) => {
|
2015-02-27 14:36:53 +00:00
|
|
|
const STRICT_KEYWORD_START: ast::Name = first!($( ast::Name($sk_name), )*);
|
|
|
|
const STRICT_KEYWORD_FINAL: ast::Name = last!($( ast::Name($sk_name), )*);
|
|
|
|
const RESERVED_KEYWORD_START: ast::Name = first!($( ast::Name($rk_name), )*);
|
|
|
|
const RESERVED_KEYWORD_FINAL: ast::Name = last!($( ast::Name($rk_name), )*);
|
2013-12-11 04:15:55 +00:00
|
|
|
|
2013-12-10 06:08:48 +00:00
|
|
|
pub mod special_idents {
|
2014-10-27 08:22:52 +00:00
|
|
|
use ast;
|
2014-09-13 01:55:37 +00:00
|
|
|
$(
|
2014-10-27 22:37:07 +00:00
|
|
|
#[allow(non_upper_case_globals)]
|
2014-10-27 08:22:52 +00:00
|
|
|
pub const $si_static: ast::Ident = ast::Ident {
|
|
|
|
name: ast::Name($si_name),
|
2015-09-24 20:05:02 +00:00
|
|
|
ctxt: ast::EMPTY_CTXT,
|
2014-10-27 08:22:52 +00:00
|
|
|
};
|
2014-09-13 01:55:37 +00:00
|
|
|
)*
|
2013-12-10 06:08:48 +00:00
|
|
|
}
|
|
|
|
|
2014-07-06 23:02:48 +00:00
|
|
|
pub mod special_names {
|
2014-10-27 08:22:52 +00:00
|
|
|
use ast;
|
|
|
|
$(
|
2014-10-30 16:02:07 +00:00
|
|
|
#[allow(non_upper_case_globals)]
|
2015-09-24 20:05:02 +00:00
|
|
|
pub const $si_static: ast::Name = ast::Name($si_name);
|
2014-10-27 08:22:52 +00:00
|
|
|
)*
|
2014-07-06 23:02:48 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 01:06:06 +00:00
|
|
|
/// All the valid words that have meaning in the Rust language.
|
|
|
|
///
|
|
|
|
/// Rust keywords are either 'strict' or 'reserved'. Strict keywords may not
|
|
|
|
/// appear as identifiers at all. Reserved keywords are not used anywhere in
|
|
|
|
/// the language and may not appear as identifiers.
|
2013-12-10 06:08:48 +00:00
|
|
|
pub mod keywords {
|
2014-11-06 08:05:53 +00:00
|
|
|
pub use self::Keyword::*;
|
2014-10-27 08:22:52 +00:00
|
|
|
use ast;
|
2013-12-10 06:08:48 +00:00
|
|
|
|
2015-01-16 03:04:28 +00:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
2013-12-10 06:08:48 +00:00
|
|
|
pub enum Keyword {
|
2013-12-11 04:15:55 +00:00
|
|
|
$( $sk_variant, )*
|
|
|
|
$( $rk_variant, )*
|
2013-12-10 06:08:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Keyword {
|
2014-10-27 08:22:52 +00:00
|
|
|
pub fn to_name(&self) -> ast::Name {
|
2013-12-10 06:08:48 +00:00
|
|
|
match *self {
|
2014-10-27 08:22:52 +00:00
|
|
|
$( $sk_variant => ast::Name($sk_name), )*
|
|
|
|
$( $rk_variant => ast::Name($rk_name), )*
|
2013-12-10 06:08:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-14 05:07:09 +00:00
|
|
|
fn mk_fresh_ident_interner() -> IdentInterner {
|
2014-02-28 20:54:01 +00:00
|
|
|
let mut init_vec = Vec::new();
|
|
|
|
$(init_vec.push($si_str);)*
|
|
|
|
$(init_vec.push($sk_str);)*
|
|
|
|
$(init_vec.push($rk_str);)*
|
2015-02-18 19:48:57 +00:00
|
|
|
interner::StrInterner::prefill(&init_vec[..])
|
2013-12-10 06:08:48 +00:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
|
2013-12-11 04:15:55 +00:00
|
|
|
// If the special idents get renumbered, remember to modify these two as appropriate
|
2014-10-27 08:22:52 +00:00
|
|
|
pub const SELF_KEYWORD_NAME: ast::Name = ast::Name(SELF_KEYWORD_NAME_NUM);
|
|
|
|
const STATIC_KEYWORD_NAME: ast::Name = ast::Name(STATIC_KEYWORD_NAME_NUM);
|
|
|
|
const SUPER_KEYWORD_NAME: ast::Name = ast::Name(SUPER_KEYWORD_NAME_NUM);
|
2015-02-10 13:55:45 +00:00
|
|
|
const SELF_TYPE_KEYWORD_NAME: ast::Name = ast::Name(SELF_TYPE_KEYWORD_NAME_NUM);
|
2014-07-06 08:17:59 +00:00
|
|
|
|
2014-10-06 23:33:44 +00:00
|
|
|
pub const SELF_KEYWORD_NAME_NUM: u32 = 1;
|
|
|
|
const STATIC_KEYWORD_NAME_NUM: u32 = 2;
|
|
|
|
const SUPER_KEYWORD_NAME_NUM: u32 = 3;
|
2015-02-10 13:55:45 +00:00
|
|
|
const SELF_TYPE_KEYWORD_NAME_NUM: u32 = 10;
|
2012-08-02 22:34:13 +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".
|
|
|
|
|
2013-12-10 06:08:48 +00:00
|
|
|
declare_special_idents_and_keywords! {
|
|
|
|
pub mod special_idents {
|
|
|
|
// These ones are statics
|
2014-02-14 05:07:09 +00:00
|
|
|
(0, invalid, "");
|
2014-09-18 23:25:07 +00:00
|
|
|
(super::SELF_KEYWORD_NAME_NUM, self_, "self");
|
|
|
|
(super::STATIC_KEYWORD_NAME_NUM, statik, "static");
|
|
|
|
(super::SUPER_KEYWORD_NAME_NUM, super_, "super");
|
|
|
|
(4, static_lifetime, "'static");
|
2013-12-10 06:08:48 +00:00
|
|
|
|
2013-12-11 03:46:16 +00:00
|
|
|
// for matcher NTs
|
2014-09-18 23:25:07 +00:00
|
|
|
(5, tt, "tt");
|
|
|
|
(6, matchers, "matchers");
|
2013-12-11 03:46:16 +00:00
|
|
|
|
|
|
|
// outside of libsyntax
|
2014-09-18 23:25:07 +00:00
|
|
|
(7, clownshoe_abi, "__rust_abi");
|
|
|
|
(8, opaque, "<opaque>");
|
|
|
|
(9, unnamed_field, "<unnamed_field>");
|
2015-02-10 13:55:45 +00:00
|
|
|
(super::SELF_TYPE_KEYWORD_NAME_NUM, type_self, "Self");
|
2014-09-18 23:25:07 +00:00
|
|
|
(11, prelude_import, "prelude_import");
|
2013-12-10 06:08:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub mod keywords {
|
|
|
|
// These ones are variants of the Keyword enum
|
2013-12-11 03:46:16 +00:00
|
|
|
|
2013-12-11 04:15:55 +00:00
|
|
|
'strict:
|
2015-01-28 01:27:28 +00:00
|
|
|
(12, As, "as");
|
|
|
|
(13, Break, "break");
|
|
|
|
(14, Crate, "crate");
|
|
|
|
(15, Else, "else");
|
|
|
|
(16, Enum, "enum");
|
|
|
|
(17, Extern, "extern");
|
|
|
|
(18, False, "false");
|
|
|
|
(19, Fn, "fn");
|
|
|
|
(20, For, "for");
|
|
|
|
(21, If, "if");
|
|
|
|
(22, Impl, "impl");
|
|
|
|
(23, In, "in");
|
|
|
|
(24, Let, "let");
|
|
|
|
(25, Loop, "loop");
|
|
|
|
(26, Match, "match");
|
|
|
|
(27, Mod, "mod");
|
|
|
|
(28, Move, "move");
|
|
|
|
(29, Mut, "mut");
|
|
|
|
(30, Pub, "pub");
|
|
|
|
(31, Ref, "ref");
|
|
|
|
(32, Return, "return");
|
2013-12-10 06:08:48 +00:00
|
|
|
// Static and Self are also special idents (prefill de-dupes)
|
2014-09-18 23:25:07 +00:00
|
|
|
(super::STATIC_KEYWORD_NAME_NUM, Static, "static");
|
2015-02-10 13:55:45 +00:00
|
|
|
(super::SELF_KEYWORD_NAME_NUM, SelfValue, "self");
|
|
|
|
(super::SELF_TYPE_KEYWORD_NAME_NUM, SelfType, "Self");
|
2015-01-28 01:27:28 +00:00
|
|
|
(33, Struct, "struct");
|
2014-09-18 23:25:07 +00:00
|
|
|
(super::SUPER_KEYWORD_NAME_NUM, Super, "super");
|
2015-01-28 01:27:28 +00:00
|
|
|
(34, True, "true");
|
|
|
|
(35, Trait, "trait");
|
|
|
|
(36, Type, "type");
|
|
|
|
(37, Unsafe, "unsafe");
|
|
|
|
(38, Use, "use");
|
2015-09-11 07:09:22 +00:00
|
|
|
(39, While, "while");
|
|
|
|
(40, Continue, "continue");
|
|
|
|
(41, Box, "box");
|
|
|
|
(42, Const, "const");
|
|
|
|
(43, Where, "where");
|
2013-12-11 04:15:55 +00:00
|
|
|
'reserved:
|
2015-09-11 07:09:22 +00:00
|
|
|
(44, Virtual, "virtual");
|
2015-03-07 23:58:45 +00:00
|
|
|
(45, Proc, "proc");
|
2015-01-28 01:27:28 +00:00
|
|
|
(46, Alignof, "alignof");
|
2015-01-17 16:00:32 +00:00
|
|
|
(47, Become, "become");
|
2015-01-28 01:27:28 +00:00
|
|
|
(48, Offsetof, "offsetof");
|
|
|
|
(49, Priv, "priv");
|
|
|
|
(50, Pure, "pure");
|
|
|
|
(51, Sizeof, "sizeof");
|
|
|
|
(52, Typeof, "typeof");
|
|
|
|
(53, Unsized, "unsized");
|
|
|
|
(54, Yield, "yield");
|
|
|
|
(55, Do, "do");
|
|
|
|
(56, Abstract, "abstract");
|
|
|
|
(57, Final, "final");
|
|
|
|
(58, Override, "override");
|
|
|
|
(59, Macro, "macro");
|
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...
|
2014-01-09 13:05:33 +00:00
|
|
|
pub type IdentInterner = StrInterner;
|
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.
|
2014-03-27 17:28:38 +00:00
|
|
|
pub fn get_ident_interner() -> Rc<IdentInterner> {
|
2014-11-14 22:20:57 +00:00
|
|
|
thread_local!(static KEY: Rc<::parse::token::IdentInterner> = {
|
|
|
|
Rc::new(mk_fresh_ident_interner())
|
2014-11-14 17:18:10 +00:00
|
|
|
});
|
2014-11-14 22:20:57 +00:00
|
|
|
KEY.with(|k| k.clone())
|
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() {
|
|
|
|
let interner = get_ident_interner();
|
|
|
|
interner.reset(mk_fresh_ident_interner());
|
|
|
|
}
|
|
|
|
|
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 {
|
2014-03-27 22:39:48 +00:00
|
|
|
string: RcStr,
|
2014-01-08 18:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl InternedString {
|
|
|
|
#[inline]
|
|
|
|
pub fn new(string: &'static str) -> InternedString {
|
|
|
|
InternedString {
|
2014-02-01 00:42:33 +00:00
|
|
|
string: RcStr::new(string),
|
2014-01-08 18:35:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-02-01 00:42:33 +00:00
|
|
|
fn new_from_rc_str(string: RcStr) -> 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 {
|
|
|
|
let interner = get_ident_interner();
|
|
|
|
InternedString::new_from_rc_str(interner.get(name))
|
|
|
|
}
|
2015-01-22 11:28:44 +00:00
|
|
|
}
|
|
|
|
|
2015-01-01 19:53:20 +00:00
|
|
|
impl Deref for InternedString {
|
|
|
|
type Target = str;
|
|
|
|
|
2014-12-11 03:46:38 +00:00
|
|
|
fn deref(&self) -> &str { &*self.string }
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-04 06:24:50 +00:00
|
|
|
impl Decodable for InternedString {
|
|
|
|
fn decode<D: Decoder>(d: &mut D) -> Result<InternedString, D::Error> {
|
2015-07-28 16:07:20 +00:00
|
|
|
Ok(intern(try!(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 {
|
2014-02-14 05:07:09 +00:00
|
|
|
get_ident_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 {
|
2014-02-14 05:07:09 +00:00
|
|
|
get_ident_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 {
|
2013-06-26 17:11:19 +00:00
|
|
|
let interner = get_ident_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
|
|
|
}
|
|
|
|
|
2013-06-25 18:43:52 +00:00
|
|
|
// create a fresh mark.
|
2014-10-27 08:22:52 +00:00
|
|
|
pub fn fresh_mark() -> ast::Mrk {
|
2015-09-24 20:05:02 +00:00
|
|
|
gensym("mark").0
|
2013-06-25 18:43:52 +00:00
|
|
|
}
|
|
|
|
|
2013-05-14 18:34:17 +00:00
|
|
|
#[cfg(test)]
|
2015-04-24 15:30:41 +00:00
|
|
|
mod tests {
|
2013-05-14 18:34:17 +00:00
|
|
|
use super::*;
|
2013-06-25 23:48:03 +00:00
|
|
|
use ast;
|
2014-02-24 20:47:19 +00:00
|
|
|
use ext::mtwt;
|
2013-06-25 23:48:03 +00:00
|
|
|
|
2014-07-09 05:28:52 +00:00
|
|
|
fn mark_ident(id : ast::Ident, m : ast::Mrk) -> ast::Ident {
|
2015-09-24 20:05:02 +00:00
|
|
|
ast::Ident::new(id.name, mtwt::apply_mark(m, id.ctxt))
|
2013-09-05 21:15:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test] fn mtwt_token_eq_test() {
|
2014-10-27 12:33:30 +00:00
|
|
|
assert!(Gt.mtwt_eq(&Gt));
|
2013-09-05 21:15:00 +00:00
|
|
|
let a = str_to_ident("bac");
|
|
|
|
let a1 = mark_ident(a,92);
|
2014-10-27 15:01:44 +00:00
|
|
|
assert!(Ident(a, ModName).mtwt_eq(&Ident(a1, Plain)));
|
2013-09-05 21:15:00 +00:00
|
|
|
}
|
2013-05-14 18:34:17 +00:00
|
|
|
}
|