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.
|
|
|
|
|
2012-12-23 22:41:37 +00:00
|
|
|
use ast;
|
2014-09-13 16:06:01 +00:00
|
|
|
use ast::{Ident, Name, Mrk};
|
2014-02-24 20:47:19 +00:00
|
|
|
use ext::mtwt;
|
2012-12-23 22:41:37 +00:00
|
|
|
use parse::token;
|
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;
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 17:34:51 +00:00
|
|
|
use std::mem;
|
2014-05-16 07:16:13 +00:00
|
|
|
use std::path::BytesContainer;
|
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)]
|
2014-05-31 17:43:52 +00:00
|
|
|
#[deriving(Clone, Encodable, Decodable, PartialEq, Eq, Hash, Show)]
|
2014-01-09 13:05:33 +00:00
|
|
|
pub enum BinOp {
|
2012-01-20 01:56:05 +00:00
|
|
|
PLUS,
|
|
|
|
MINUS,
|
|
|
|
STAR,
|
|
|
|
SLASH,
|
|
|
|
PERCENT,
|
|
|
|
CARET,
|
|
|
|
AND,
|
|
|
|
OR,
|
2012-05-22 21:59:15 +00:00
|
|
|
SHL,
|
|
|
|
SHR,
|
2010-09-09 22:59:29 +00:00
|
|
|
}
|
|
|
|
|
2014-01-09 13:05:33 +00:00
|
|
|
#[allow(non_camel_case_types)]
|
2014-05-31 17:43:52 +00:00
|
|
|
#[deriving(Clone, Encodable, Decodable, PartialEq, Eq, Hash, Show)]
|
2013-01-29 21:54:06 +00:00
|
|
|
pub enum Token {
|
2010-09-09 22:59:29 +00:00
|
|
|
/* Expression-operator symbols. */
|
2012-01-20 01:56:05 +00:00
|
|
|
EQ,
|
|
|
|
LT,
|
|
|
|
LE,
|
|
|
|
EQEQ,
|
|
|
|
NE,
|
|
|
|
GE,
|
|
|
|
GT,
|
|
|
|
ANDAND,
|
|
|
|
OROR,
|
|
|
|
NOT,
|
|
|
|
TILDE,
|
2014-01-09 13:05:33 +00:00
|
|
|
BINOP(BinOp),
|
|
|
|
BINOPEQ(BinOp),
|
2010-09-09 22:59:29 +00:00
|
|
|
|
|
|
|
/* Structural symbols */
|
2012-01-20 01:56:05 +00:00
|
|
|
AT,
|
|
|
|
DOT,
|
2012-08-04 01:01:30 +00:00
|
|
|
DOTDOT,
|
2013-10-25 05:56:34 +00:00
|
|
|
DOTDOTDOT,
|
2012-01-20 01:56:05 +00:00
|
|
|
COMMA,
|
|
|
|
SEMI,
|
|
|
|
COLON,
|
|
|
|
MOD_SEP,
|
|
|
|
RARROW,
|
|
|
|
LARROW,
|
2012-06-05 01:34:10 +00:00
|
|
|
FAT_ARROW,
|
2012-01-20 01:56:05 +00:00
|
|
|
LPAREN,
|
|
|
|
RPAREN,
|
|
|
|
LBRACKET,
|
|
|
|
RBRACKET,
|
|
|
|
LBRACE,
|
|
|
|
RBRACE,
|
|
|
|
POUND,
|
2012-04-22 23:58:04 +00:00
|
|
|
DOLLAR,
|
2014-07-08 02:26:02 +00:00
|
|
|
QUESTION,
|
2012-01-25 23:38:09 +00:00
|
|
|
|
2010-09-09 22:59:29 +00:00
|
|
|
/* Literals */
|
2014-07-06 08:17:59 +00:00
|
|
|
LIT_BYTE(Name),
|
|
|
|
LIT_CHAR(Name),
|
|
|
|
LIT_INTEGER(Name),
|
|
|
|
LIT_FLOAT(Name),
|
|
|
|
LIT_STR(Name),
|
|
|
|
LIT_STR_RAW(Name, uint), /* raw str delimited by n hash symbols */
|
|
|
|
LIT_BINARY(Name),
|
|
|
|
LIT_BINARY_RAW(Name, uint), /* raw binary str delimited by n hash symbols */
|
2010-09-09 22:59:29 +00:00
|
|
|
|
|
|
|
/* Name components */
|
2014-06-09 20:12:30 +00:00
|
|
|
/// An identifier contains an "is_mod_name" boolean,
|
|
|
|
/// indicating whether :: follows this token with no
|
|
|
|
/// whitespace in between.
|
2014-06-09 20:19:38 +00:00
|
|
|
IDENT(Ident, bool),
|
2012-01-20 01:56:05 +00:00
|
|
|
UNDERSCORE,
|
2014-06-09 20:19:38 +00:00
|
|
|
LIFETIME(Ident),
|
2012-06-12 17:50:17 +00:00
|
|
|
|
2012-06-30 01:26:34 +00:00
|
|
|
/* For interpolation */
|
2014-01-09 13:05:33 +00:00
|
|
|
INTERPOLATED(Nonterminal),
|
2014-07-06 08:17:59 +00:00
|
|
|
DOC_COMMENT(Name),
|
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
|
|
|
|
WS,
|
|
|
|
/// Comment
|
|
|
|
COMMENT,
|
2014-07-06 08:17:59 +00:00
|
|
|
SHEBANG(Name),
|
2014-07-05 05:30:39 +00:00
|
|
|
|
2012-01-20 01:56:05 +00:00
|
|
|
EOF,
|
2010-09-09 22:59:29 +00:00
|
|
|
}
|
2010-08-18 18:35:12 +00:00
|
|
|
|
2014-05-31 17:43:52 +00:00
|
|
|
#[deriving(Clone, Encodable, Decodable, 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-09-13 16:06:01 +00:00
|
|
|
NtItem( P<ast::Item>),
|
2014-01-09 13:05:33 +00:00
|
|
|
NtBlock(P<ast::Block>),
|
2014-09-13 16:06:01 +00:00
|
|
|
NtStmt( P<ast::Stmt>),
|
|
|
|
NtPat( P<ast::Pat>),
|
|
|
|
NtExpr( P<ast::Expr>),
|
|
|
|
NtTy( P<ast::Ty>),
|
2014-06-09 20:19:38 +00:00
|
|
|
/// See IDENT, above, for meaning of bool in NtIdent:
|
|
|
|
NtIdent(Box<Ident>, bool),
|
|
|
|
/// Stuff inside brackets for attributes
|
2014-09-13 16:06:01 +00:00
|
|
|
NtMeta( P<ast::MetaItem>),
|
2014-05-06 01:56:44 +00:00
|
|
|
NtPath(Box<ast::Path>),
|
2014-09-13 16:06:01 +00:00
|
|
|
NtTT( P<ast::TokenTree>), // needs P'ed to break a circularity
|
|
|
|
NtMatchers(Vec<ast::Matcher>)
|
2012-06-12 17:50:17 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 09:23:06 +00:00
|
|
|
impl fmt::Show for Nonterminal {
|
|
|
|
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(..)"),
|
|
|
|
NtMatchers(..) => f.pad("NtMatchers(..)"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-21 10:39:03 +00:00
|
|
|
pub fn binop_to_string(o: BinOp) -> &'static str {
|
2012-08-06 19:34:08 +00:00
|
|
|
match o {
|
2014-05-20 17:37:08 +00:00
|
|
|
PLUS => "+",
|
|
|
|
MINUS => "-",
|
|
|
|
STAR => "*",
|
|
|
|
SLASH => "/",
|
|
|
|
PERCENT => "%",
|
|
|
|
CARET => "^",
|
|
|
|
AND => "&",
|
|
|
|
OR => "|",
|
|
|
|
SHL => "<<",
|
|
|
|
SHR => ">>"
|
2010-08-20 22:57:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-21 10:39:03 +00:00
|
|
|
pub fn to_string(t: &Token) -> String {
|
2013-02-25 03:54:37 +00:00
|
|
|
match *t {
|
2014-09-13 05:13:38 +00:00
|
|
|
EQ => "=".into_string(),
|
|
|
|
LT => "<".into_string(),
|
|
|
|
LE => "<=".into_string(),
|
|
|
|
EQEQ => "==".into_string(),
|
|
|
|
NE => "!=".into_string(),
|
|
|
|
GE => ">=".into_string(),
|
|
|
|
GT => ">".into_string(),
|
|
|
|
NOT => "!".into_string(),
|
|
|
|
TILDE => "~".into_string(),
|
|
|
|
OROR => "||".into_string(),
|
|
|
|
ANDAND => "&&".into_string(),
|
|
|
|
BINOP(op) => binop_to_string(op).into_string(),
|
2014-05-07 23:33:43 +00:00
|
|
|
BINOPEQ(op) => {
|
2014-09-13 05:13:38 +00:00
|
|
|
let mut s = binop_to_string(op).into_string();
|
2014-05-07 23:33:43 +00:00
|
|
|
s.push_str("=");
|
|
|
|
s
|
|
|
|
}
|
2011-09-02 22:34:58 +00:00
|
|
|
|
2011-07-27 12:19:39 +00:00
|
|
|
/* Structural symbols */
|
2014-09-13 05:13:38 +00:00
|
|
|
AT => "@".into_string(),
|
|
|
|
DOT => ".".into_string(),
|
|
|
|
DOTDOT => "..".into_string(),
|
|
|
|
DOTDOTDOT => "...".into_string(),
|
|
|
|
COMMA => ",".into_string(),
|
|
|
|
SEMI => ";".into_string(),
|
|
|
|
COLON => ":".into_string(),
|
|
|
|
MOD_SEP => "::".into_string(),
|
|
|
|
RARROW => "->".into_string(),
|
|
|
|
LARROW => "<-".into_string(),
|
|
|
|
FAT_ARROW => "=>".into_string(),
|
|
|
|
LPAREN => "(".into_string(),
|
|
|
|
RPAREN => ")".into_string(),
|
|
|
|
LBRACKET => "[".into_string(),
|
|
|
|
RBRACKET => "]".into_string(),
|
|
|
|
LBRACE => "{".into_string(),
|
|
|
|
RBRACE => "}".into_string(),
|
|
|
|
POUND => "#".into_string(),
|
|
|
|
DOLLAR => "$".into_string(),
|
|
|
|
QUESTION => "?".into_string(),
|
2012-01-25 23:38:09 +00:00
|
|
|
|
2011-07-27 12:19:39 +00:00
|
|
|
/* Literals */
|
2014-06-06 15:04:04 +00:00
|
|
|
LIT_BYTE(b) => {
|
2014-07-06 08:17:59 +00:00
|
|
|
format!("b'{}'", b.as_str())
|
2014-06-06 15:04:04 +00:00
|
|
|
}
|
2013-09-03 23:24:12 +00:00
|
|
|
LIT_CHAR(c) => {
|
2014-07-06 08:17:59 +00:00
|
|
|
format!("'{}'", c.as_str())
|
2011-12-07 20:06:12 +00:00
|
|
|
}
|
2014-06-18 17:44:20 +00:00
|
|
|
LIT_INTEGER(c) | LIT_FLOAT(c) => {
|
2014-09-13 05:13:38 +00:00
|
|
|
c.as_str().into_string()
|
2012-11-08 02:40:34 +00:00
|
|
|
}
|
2014-06-18 17:44:20 +00:00
|
|
|
|
2014-02-14 05:07:09 +00:00
|
|
|
LIT_STR(s) => {
|
2014-07-06 08:17:59 +00:00
|
|
|
format!("\"{}\"", s.as_str())
|
2014-01-31 23:30:19 +00:00
|
|
|
}
|
2014-02-14 05:07:09 +00:00
|
|
|
LIT_STR_RAW(s, n) => {
|
2014-06-07 14:32:01 +00:00
|
|
|
format!("r{delim}\"{string}\"{delim}",
|
2014-07-06 08:17:59 +00:00
|
|
|
delim="#".repeat(n), string=s.as_str())
|
2014-06-07 14:32:01 +00:00
|
|
|
}
|
2014-07-03 07:47:30 +00:00
|
|
|
LIT_BINARY(v) => {
|
2014-07-06 08:17:59 +00:00
|
|
|
format!("b\"{}\"", v.as_str())
|
2013-10-02 01:32:29 +00:00
|
|
|
}
|
2014-07-03 07:47:30 +00:00
|
|
|
LIT_BINARY_RAW(s, n) => {
|
2014-06-13 17:56:24 +00:00
|
|
|
format!("br{delim}\"{string}\"{delim}",
|
2014-07-06 08:17:59 +00:00
|
|
|
delim="#".repeat(n), string=s.as_str())
|
2014-06-13 17:56:24 +00:00
|
|
|
}
|
2012-06-30 10:54:54 +00:00
|
|
|
|
2011-07-27 12:19:39 +00:00
|
|
|
/* Name components */
|
2014-09-13 05:13:38 +00:00
|
|
|
IDENT(s, _) => get_ident(s).get().into_string(),
|
2014-02-01 00:42:33 +00:00
|
|
|
LIFETIME(s) => {
|
2014-06-07 14:32:01 +00:00
|
|
|
format!("{}", get_ident(s))
|
2014-02-01 00:42:33 +00:00
|
|
|
}
|
2014-09-13 05:13:38 +00:00
|
|
|
UNDERSCORE => "_".into_string(),
|
2012-06-30 10:54:54 +00:00
|
|
|
|
|
|
|
/* Other */
|
2014-09-13 05:13:38 +00:00
|
|
|
DOC_COMMENT(s) => s.as_str().into_string(),
|
|
|
|
EOF => "<eof>".into_string(),
|
|
|
|
WS => " ".into_string(),
|
|
|
|
COMMENT => "/* */".into_string(),
|
2014-07-05 05:30:39 +00:00
|
|
|
SHEBANG(s) => format!("/* shebang: {}*/", s.as_str()),
|
|
|
|
|
2012-12-04 18:50:00 +00:00
|
|
|
INTERPOLATED(ref nt) => {
|
2013-01-23 00:00:07 +00:00
|
|
|
match nt {
|
2014-06-21 10:39:03 +00:00
|
|
|
&NtExpr(ref e) => ::print::pprust::expr_to_string(&**e),
|
|
|
|
&NtMeta(ref e) => ::print::pprust::meta_item_to_string(&**e),
|
2014-07-19 09:59:44 +00:00
|
|
|
&NtTy(ref e) => ::print::pprust::ty_to_string(&**e),
|
|
|
|
&NtPath(ref e) => ::print::pprust::path_to_string(&**e),
|
2013-01-23 00:00:07 +00:00
|
|
|
_ => {
|
2014-09-13 05:13:38 +00:00
|
|
|
let mut s = "an interpolated ".into_string();
|
2014-05-07 23:33:43 +00:00
|
|
|
match *nt {
|
|
|
|
NtItem(..) => s.push_str("item"),
|
|
|
|
NtBlock(..) => s.push_str("block"),
|
|
|
|
NtStmt(..) => s.push_str("statement"),
|
|
|
|
NtPat(..) => s.push_str("pattern"),
|
|
|
|
NtMeta(..) => fail!("should have been handled"),
|
2014-07-19 09:59:44 +00:00
|
|
|
NtExpr(..) => fail!("should have been handled"),
|
|
|
|
NtTy(..) => fail!("should have been handled"),
|
2014-05-07 23:33:43 +00:00
|
|
|
NtIdent(..) => s.push_str("identifier"),
|
2014-07-19 09:59:44 +00:00
|
|
|
NtPath(..) => fail!("should have been handled"),
|
2014-05-07 23:33:43 +00:00
|
|
|
NtTT(..) => s.push_str("tt"),
|
|
|
|
NtMatchers(..) => s.push_str("matcher sequence")
|
|
|
|
};
|
|
|
|
s
|
2012-07-06 21:48:01 +00:00
|
|
|
}
|
2013-01-23 00:00:07 +00:00
|
|
|
}
|
2012-06-30 01:26:34 +00:00
|
|
|
}
|
2010-08-20 18:41:34 +00:00
|
|
|
}
|
|
|
|
}
|
2011-07-03 18:48:14 +00:00
|
|
|
|
2013-03-22 18:09:13 +00:00
|
|
|
pub fn can_begin_expr(t: &Token) -> bool {
|
2013-02-25 01:24:28 +00:00
|
|
|
match *t {
|
2012-08-04 02:59:04 +00:00
|
|
|
LPAREN => true,
|
|
|
|
LBRACE => true,
|
|
|
|
LBRACKET => true,
|
|
|
|
IDENT(_, _) => true,
|
|
|
|
UNDERSCORE => true,
|
|
|
|
TILDE => true,
|
2014-06-06 15:04:04 +00:00
|
|
|
LIT_BYTE(_) => true,
|
2013-09-03 23:24:12 +00:00
|
|
|
LIT_CHAR(_) => true,
|
2014-06-18 17:44:20 +00:00
|
|
|
LIT_INTEGER(_) => true,
|
|
|
|
LIT_FLOAT(_) => true,
|
2012-08-04 02:59:04 +00:00
|
|
|
LIT_STR(_) => true,
|
2013-10-02 01:32:29 +00:00
|
|
|
LIT_STR_RAW(_, _) => true,
|
2014-06-07 14:32:01 +00:00
|
|
|
LIT_BINARY(_) => true,
|
2014-06-13 17:56:24 +00:00
|
|
|
LIT_BINARY_RAW(_, _) => true,
|
2012-08-04 02:59:04 +00:00
|
|
|
POUND => true,
|
|
|
|
AT => true,
|
|
|
|
NOT => true,
|
|
|
|
BINOP(MINUS) => true,
|
|
|
|
BINOP(STAR) => true,
|
|
|
|
BINOP(AND) => true,
|
|
|
|
BINOP(OR) => true, // in lambda syntax
|
|
|
|
OROR => true, // in lambda syntax
|
|
|
|
MOD_SEP => true,
|
2014-01-09 13:05:33 +00:00
|
|
|
INTERPOLATED(NtExpr(..))
|
|
|
|
| INTERPOLATED(NtIdent(..))
|
|
|
|
| INTERPOLATED(NtBlock(..))
|
|
|
|
| INTERPOLATED(NtPath(..)) => true,
|
2012-08-04 02:59:04 +00:00
|
|
|
_ => false
|
2011-07-03 18:48:14 +00:00
|
|
|
}
|
|
|
|
}
|
2011-10-07 14:22:53 +00:00
|
|
|
|
2014-03-16 21:46:04 +00:00
|
|
|
/// Returns the matching close delimiter if this is an open delimiter,
|
|
|
|
/// otherwise `None`.
|
|
|
|
pub fn close_delimiter_for(t: &Token) -> Option<Token> {
|
2013-02-24 18:00:25 +00:00
|
|
|
match *t {
|
2014-03-16 21:46:04 +00:00
|
|
|
LPAREN => Some(RPAREN),
|
|
|
|
LBRACE => Some(RBRACE),
|
|
|
|
LBRACKET => Some(RBRACKET),
|
|
|
|
_ => None
|
2012-07-31 20:53:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-25 01:24:28 +00:00
|
|
|
pub fn is_lit(t: &Token) -> bool {
|
|
|
|
match *t {
|
2014-06-06 15:04:04 +00:00
|
|
|
LIT_BYTE(_) => true,
|
2013-09-03 23:24:12 +00:00
|
|
|
LIT_CHAR(_) => true,
|
2014-06-18 17:44:20 +00:00
|
|
|
LIT_INTEGER(_) => true,
|
|
|
|
LIT_FLOAT(_) => true,
|
2012-08-04 02:59:04 +00:00
|
|
|
LIT_STR(_) => true,
|
2013-10-02 01:32:29 +00:00
|
|
|
LIT_STR_RAW(_, _) => true,
|
2014-06-07 14:32:01 +00:00
|
|
|
LIT_BINARY(_) => true,
|
2014-06-13 17:56:24 +00:00
|
|
|
LIT_BINARY_RAW(_, _) => true,
|
2012-08-04 02:59:04 +00:00
|
|
|
_ => false
|
2012-06-11 23:49:35 +00:00
|
|
|
}
|
2012-04-22 21:59:04 +00:00
|
|
|
}
|
|
|
|
|
2013-03-22 18:09:13 +00:00
|
|
|
pub fn is_ident(t: &Token) -> bool {
|
2013-02-25 01:24:28 +00:00
|
|
|
match *t { IDENT(_, _) => true, _ => false }
|
2012-04-18 04:14:40 +00:00
|
|
|
}
|
|
|
|
|
2013-03-22 18:09:13 +00:00
|
|
|
pub fn is_ident_or_path(t: &Token) -> bool {
|
2013-02-25 01:24:28 +00:00
|
|
|
match *t {
|
2014-01-09 13:05:33 +00:00
|
|
|
IDENT(_, _) | INTERPOLATED(NtPath(..)) => true,
|
2012-08-06 20:09:10 +00:00
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-22 18:09:13 +00:00
|
|
|
pub fn is_plain_ident(t: &Token) -> bool {
|
2013-02-25 01:24:28 +00:00
|
|
|
match *t { IDENT(_, false) => true, _ => false }
|
2012-04-18 04:14:40 +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
|
|
|
}
|
|
|
|
) => {
|
2014-07-06 08:17:59 +00:00
|
|
|
static STRICT_KEYWORD_START: Name = first!($( Name($sk_name), )*);
|
|
|
|
static STRICT_KEYWORD_FINAL: Name = last!($( Name($sk_name), )*);
|
|
|
|
static RESERVED_KEYWORD_START: Name = first!($( Name($rk_name), )*);
|
|
|
|
static RESERVED_KEYWORD_FINAL: Name = last!($( Name($rk_name), )*);
|
2013-12-11 04:15:55 +00:00
|
|
|
|
2013-12-10 06:08:48 +00:00
|
|
|
pub mod special_idents {
|
2014-07-06 08:17:59 +00:00
|
|
|
use ast::{Ident, Name};
|
2014-09-13 01:55:37 +00:00
|
|
|
$(
|
|
|
|
#[allow(non_uppercase_statics)]
|
2014-10-06 23:33:44 +00:00
|
|
|
pub const $si_static: Ident = Ident { name: Name($si_name), ctxt: 0 };
|
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 {
|
|
|
|
use ast::Name;
|
2014-10-06 23:33:44 +00:00
|
|
|
$( #[allow(non_uppercase_statics)] pub const $si_static: Name = Name($si_name); )*
|
2014-07-06 23:02:48 +00:00
|
|
|
}
|
|
|
|
|
2013-12-10 06:08:48 +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.
|
|
|
|
*/
|
|
|
|
pub mod keywords {
|
2014-07-06 22:19:29 +00:00
|
|
|
use ast::Name;
|
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-07-06 22:19:29 +00:00
|
|
|
pub fn to_name(&self) -> Name {
|
2013-12-10 06:08:48 +00:00
|
|
|
match *self {
|
2014-07-06 08:17:59 +00:00
|
|
|
$( $sk_variant => Name($sk_name), )*
|
|
|
|
$( $rk_variant => 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 {
|
2013-12-10 06:08:48 +00:00
|
|
|
// The indices here must correspond to the numbers in
|
2014-07-06 22:19:29 +00:00
|
|
|
// special_idents, in Keyword to_name(), and in static
|
2013-12-10 06:08:48 +00:00
|
|
|
// constants below.
|
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);)*
|
|
|
|
interner::StrInterner::prefill(init_vec.as_slice())
|
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-06 23:33:44 +00:00
|
|
|
pub const SELF_KEYWORD_NAME: Name = Name(SELF_KEYWORD_NAME_NUM);
|
|
|
|
const STATIC_KEYWORD_NAME: Name = Name(STATIC_KEYWORD_NAME_NUM);
|
|
|
|
const SUPER_KEYWORD_NAME: Name = Name(SUPER_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;
|
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>");
|
|
|
|
(10, type_self, "Self");
|
|
|
|
(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:
|
2014-09-18 23:25:07 +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");
|
2014-09-24 17:58:53 +00:00
|
|
|
(28, Move, "move");
|
|
|
|
(29, Mut, "mut");
|
|
|
|
(30, Once, "once");
|
|
|
|
(31, Pub, "pub");
|
|
|
|
(32, Ref, "ref");
|
|
|
|
(33, 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");
|
|
|
|
(super::SELF_KEYWORD_NAME_NUM, Self, "self");
|
2014-09-24 17:58:53 +00:00
|
|
|
(34, Struct, "struct");
|
2014-09-18 23:25:07 +00:00
|
|
|
(super::SUPER_KEYWORD_NAME_NUM, Super, "super");
|
2014-09-24 17:58:53 +00:00
|
|
|
(35, True, "true");
|
|
|
|
(36, Trait, "trait");
|
|
|
|
(37, Type, "type");
|
|
|
|
(38, Unsafe, "unsafe");
|
|
|
|
(39, Use, "use");
|
|
|
|
(40, Virtual, "virtual");
|
|
|
|
(41, While, "while");
|
|
|
|
(42, Continue, "continue");
|
|
|
|
(43, Proc, "proc");
|
|
|
|
(44, Box, "box");
|
|
|
|
(45, Const, "const");
|
|
|
|
(46, Where, "where");
|
2013-12-11 03:46:16 +00:00
|
|
|
|
2013-12-11 04:15:55 +00:00
|
|
|
'reserved:
|
2014-09-24 17:58:53 +00:00
|
|
|
(47, Alignof, "alignof");
|
|
|
|
(48, Be, "be");
|
|
|
|
(49, Offsetof, "offsetof");
|
|
|
|
(50, Priv, "priv");
|
|
|
|
(51, Pure, "pure");
|
|
|
|
(52, Sizeof, "sizeof");
|
|
|
|
(53, Typeof, "typeof");
|
|
|
|
(54, Unsized, "unsized");
|
|
|
|
(55, Yield, "yield");
|
|
|
|
(56, Do, "do");
|
2014-10-08 02:17:54 +00:00
|
|
|
(57, Abstract, "abstract");
|
|
|
|
(58, Final, "final");
|
|
|
|
(59, Override, "override");
|
2013-12-10 06:08:48 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-03 22:15:45 +00:00
|
|
|
|
2013-03-29 17:04:48 +00:00
|
|
|
/**
|
|
|
|
* Maps a token to a record specifying the corresponding binary
|
|
|
|
* operator
|
|
|
|
*/
|
2013-09-02 01:45:37 +00:00
|
|
|
pub fn token_to_binop(tok: &Token) -> Option<ast::BinOp> {
|
2013-07-02 19:47:32 +00:00
|
|
|
match *tok {
|
2013-09-02 01:45:37 +00:00
|
|
|
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),
|
2013-03-29 17:04:48 +00:00
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2014-03-27 17:28:38 +00:00
|
|
|
// FIXME(eddyb) #8726 This should probably use a task-local reference.
|
|
|
|
pub fn get_ident_interner() -> Rc<IdentInterner> {
|
|
|
|
local_data_key!(key: Rc<::parse::token::IdentInterner>)
|
2014-04-29 03:36:08 +00:00
|
|
|
match key.get() {
|
|
|
|
Some(interner) => interner.clone(),
|
2013-07-12 08:38:44 +00:00
|
|
|
None => {
|
2014-03-27 17:28:38 +00:00
|
|
|
let interner = Rc::new(mk_fresh_ident_interner());
|
2014-04-29 03:36:08 +00:00
|
|
|
key.replace(Some(interner.clone()));
|
2013-07-12 08:38:44 +00:00
|
|
|
interner
|
2012-10-08 17:41:02 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-02 21:33:26 +00:00
|
|
|
}
|
|
|
|
|
2014-01-08 18:35:15 +00:00
|
|
|
/// Represents a string stored in the task-local interner. Because the
|
|
|
|
/// interner lives for the life of the task, this can be safely treated as an
|
|
|
|
/// immortal string, as long as it never crosses between tasks.
|
|
|
|
///
|
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
|
|
|
|
/// be fixed in the future by just leaking all strings until task death
|
|
|
|
/// somehow.
|
2014-05-31 17:43:52 +00:00
|
|
|
#[deriving(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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn get<'a>(&'a self) -> &'a str {
|
|
|
|
self.string.as_slice()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-10 22:02:36 +00:00
|
|
|
impl BytesContainer for InternedString {
|
|
|
|
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
|
2014-02-01 02:25:08 +00:00
|
|
|
// FIXME(pcwalton): This is a workaround for the incorrect signature
|
|
|
|
// of `BytesContainer`, which is itself a workaround for the lack of
|
|
|
|
// DST.
|
2014-01-10 22:02:36 +00:00
|
|
|
unsafe {
|
|
|
|
let this = self.get();
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 17:34:51 +00:00
|
|
|
mem::transmute(this.container_as_bytes())
|
2014-01-10 22:02:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-29 13:46:37 +00:00
|
|
|
impl fmt::Show for InternedString {
|
2014-02-05 12:55:13 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2014-05-10 21:05:06 +00:00
|
|
|
write!(f, "{}", self.string.as_slice())
|
2014-01-08 18:35:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Equiv<&'a str> for InternedString {
|
|
|
|
fn equiv(&self, other: & &'a str) -> bool {
|
|
|
|
(*other) == self.string.as_slice()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-18 17:58:26 +00:00
|
|
|
impl<D:Decoder<E>, E> Decodable<D, E> for InternedString {
|
|
|
|
fn decode(d: &mut D) -> Result<InternedString, E> {
|
2014-05-15 04:16:44 +00:00
|
|
|
Ok(get_name(get_ident_interner().intern(
|
|
|
|
try!(d.read_str()).as_slice())))
|
2014-03-18 17:58:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S:Encoder<E>, E> Encodable<S, E> for InternedString {
|
|
|
|
fn encode(&self, s: &mut S) -> Result<(), E> {
|
|
|
|
s.emit_str(self.string.as_slice())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-14 05:07:09 +00:00
|
|
|
/// Returns the string contents of a name, using the task-local interner.
|
|
|
|
#[inline]
|
|
|
|
pub fn get_name(name: Name) -> InternedString {
|
|
|
|
let interner = get_ident_interner();
|
|
|
|
InternedString::new_from_rc_str(interner.get(name))
|
|
|
|
}
|
|
|
|
|
2014-01-08 18:35:15 +00:00
|
|
|
/// Returns the string contents of an identifier, using the task-local
|
|
|
|
/// interner.
|
|
|
|
#[inline]
|
2014-02-14 05:07:09 +00:00
|
|
|
pub fn get_ident(ident: Ident) -> InternedString {
|
|
|
|
get_name(ident.name)
|
2014-01-08 18:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Interns and returns the string contents of an identifier, using the
|
|
|
|
/// task-local interner.
|
|
|
|
#[inline]
|
|
|
|
pub fn intern_and_get_ident(s: &str) -> InternedString {
|
2014-02-14 05:07:09 +00:00
|
|
|
get_name(intern(s))
|
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-02-14 05:07:09 +00:00
|
|
|
pub fn intern(s: &str) -> Name {
|
|
|
|
get_ident_interner().intern(s)
|
2013-06-04 19:34:25 +00:00
|
|
|
}
|
|
|
|
|
2014-02-14 05:07:09 +00:00
|
|
|
/// gensym's a new uint, using the current interner.
|
|
|
|
#[inline]
|
|
|
|
pub fn gensym(s: &str) -> Name {
|
|
|
|
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-06-09 20:19:38 +00:00
|
|
|
pub fn str_to_ident(s: &str) -> Ident {
|
|
|
|
Ident::new(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-06-09 20:19:38 +00:00
|
|
|
pub fn gensym_ident(s: &str) -> Ident {
|
|
|
|
Ident::new(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.
|
2014-06-09 20:19:38 +00:00
|
|
|
pub fn fresh_name(src: &Ident) -> 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-03-02 01:59:35 +00:00
|
|
|
/*let num = rand::task_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.
|
|
|
|
pub fn fresh_mark() -> Mrk {
|
2014-07-06 08:17:59 +00:00
|
|
|
gensym("mark").uint() as u32
|
2013-06-25 18:43:52 +00:00
|
|
|
}
|
|
|
|
|
2013-12-10 06:08:48 +00:00
|
|
|
// See the macro above about the types of keywords
|
2013-05-25 15:45:45 +00:00
|
|
|
|
|
|
|
pub fn is_keyword(kw: keywords::Keyword, tok: &Token) -> bool {
|
|
|
|
match *tok {
|
2014-07-06 22:19:29 +00:00
|
|
|
token::IDENT(sid, false) => { kw.to_name() == sid.name }
|
2013-05-25 15:45:45 +00:00
|
|
|
_ => { false }
|
2013-05-10 22:15:06 +00:00
|
|
|
}
|
2013-05-25 15:45:45 +00:00
|
|
|
}
|
2013-05-10 22:15:06 +00:00
|
|
|
|
2013-06-20 01:12:40 +00:00
|
|
|
pub fn is_any_keyword(tok: &Token) -> bool {
|
|
|
|
match *tok {
|
2014-07-06 08:17:59 +00:00
|
|
|
token::IDENT(sid, false) => {
|
|
|
|
let n = sid.name;
|
|
|
|
|
|
|
|
n == SELF_KEYWORD_NAME
|
|
|
|
|| n == STATIC_KEYWORD_NAME
|
2014-09-18 23:25:07 +00:00
|
|
|
|| n == SUPER_KEYWORD_NAME
|
2014-07-06 08:17:59 +00:00
|
|
|
|| STRICT_KEYWORD_START <= n
|
|
|
|
&& n <= RESERVED_KEYWORD_FINAL
|
2013-06-20 01:12:40 +00:00
|
|
|
},
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_strict_keyword(tok: &Token) -> bool {
|
|
|
|
match *tok {
|
2014-07-06 08:17:59 +00:00
|
|
|
token::IDENT(sid, false) => {
|
|
|
|
let n = sid.name;
|
|
|
|
|
|
|
|
n == SELF_KEYWORD_NAME
|
|
|
|
|| n == STATIC_KEYWORD_NAME
|
2014-09-18 23:25:07 +00:00
|
|
|
|| n == SUPER_KEYWORD_NAME
|
2014-07-06 08:17:59 +00:00
|
|
|
|| STRICT_KEYWORD_START <= n
|
|
|
|
&& n <= STRICT_KEYWORD_FINAL
|
2013-06-20 01:12:40 +00:00
|
|
|
},
|
2014-09-18 23:25:07 +00:00
|
|
|
token::IDENT(sid, true) => {
|
|
|
|
let n = sid.name;
|
|
|
|
|
|
|
|
n != SELF_KEYWORD_NAME
|
|
|
|
&& n != SUPER_KEYWORD_NAME
|
|
|
|
&& STRICT_KEYWORD_START <= n
|
|
|
|
&& n <= STRICT_KEYWORD_FINAL
|
|
|
|
}
|
2013-06-20 01:12:40 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_reserved_keyword(tok: &Token) -> bool {
|
|
|
|
match *tok {
|
2014-07-06 08:17:59 +00:00
|
|
|
token::IDENT(sid, false) => {
|
|
|
|
let n = sid.name;
|
|
|
|
|
|
|
|
RESERVED_KEYWORD_START <= n
|
|
|
|
&& n <= RESERVED_KEYWORD_FINAL
|
2013-06-20 01:12:40 +00:00
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-05 21:15:00 +00:00
|
|
|
pub fn mtwt_token_eq(t1 : &Token, t2 : &Token) -> bool {
|
|
|
|
match (t1,t2) {
|
2014-02-15 08:54:32 +00:00
|
|
|
(&IDENT(id1,_),&IDENT(id2,_)) | (&LIFETIME(id1),&LIFETIME(id2)) =>
|
2014-02-24 20:47:19 +00:00
|
|
|
mtwt::resolve(id1) == mtwt::resolve(id2),
|
2013-09-05 21:15:00 +00:00
|
|
|
_ => *t1 == *t2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-14 18:34:17 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
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 {
|
|
|
|
ast::Ident { name: id.name, ctxt:mtwt::apply_mark(m, id.ctxt) }
|
2013-09-05 21:15:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test] fn mtwt_token_eq_test() {
|
|
|
|
assert!(mtwt_token_eq(>,>));
|
|
|
|
let a = str_to_ident("bac");
|
|
|
|
let a1 = mark_ident(a,92);
|
|
|
|
assert!(mtwt_token_eq(&IDENT(a,true),&IDENT(a1,false)));
|
|
|
|
}
|
2013-05-14 18:34:17 +00:00
|
|
|
}
|