2013-05-30 03:16:33 -07:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08: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 17:41:37 -05:00
|
|
|
use ast;
|
2014-09-13 19:06:01 +03:00
|
|
|
use ast::{Ident, Name, Mrk};
|
2014-02-25 04:47:19 +08:00
|
|
|
use ext::mtwt;
|
2012-12-23 17:41:37 -05:00
|
|
|
use parse::token;
|
2014-09-13 19:06:01 +03:00
|
|
|
use ptr::P;
|
2014-01-31 16:42:33 -08:00
|
|
|
use util::interner::{RcStr, StrInterner};
|
2012-12-23 17:41:37 -05:00
|
|
|
use util::interner;
|
|
|
|
|
2014-02-05 08:52:54 -08:00
|
|
|
use serialize::{Decodable, Decoder, Encodable, Encoder};
|
2014-01-08 10:35:15 -08: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 10:34:51 -07:00
|
|
|
use std::mem;
|
2014-05-16 00:16:13 -07:00
|
|
|
use std::path::BytesContainer;
|
2014-03-27 19:28:38 +02:00
|
|
|
use std::rc::Rc;
|
2010-08-18 11:35:12 -07:00
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
#[allow(non_camel_case_types)]
|
2014-05-31 10:43:52 -07:00
|
|
|
#[deriving(Clone, Encodable, Decodable, PartialEq, Eq, Hash, Show)]
|
2014-01-09 15:05:33 +02:00
|
|
|
pub enum BinOp {
|
2012-01-19 17:56:05 -08:00
|
|
|
PLUS,
|
|
|
|
MINUS,
|
|
|
|
STAR,
|
|
|
|
SLASH,
|
|
|
|
PERCENT,
|
|
|
|
CARET,
|
|
|
|
AND,
|
|
|
|
OR,
|
2012-05-22 14:59:15 -07:00
|
|
|
SHL,
|
|
|
|
SHR,
|
2010-09-09 15:59:29 -07:00
|
|
|
}
|
|
|
|
|
2014-01-09 15:05:33 +02:00
|
|
|
#[allow(non_camel_case_types)]
|
2014-05-31 10:43:52 -07:00
|
|
|
#[deriving(Clone, Encodable, Decodable, PartialEq, Eq, Hash, Show)]
|
2013-01-29 13:54:06 -08:00
|
|
|
pub enum Token {
|
2010-09-09 15:59:29 -07:00
|
|
|
/* Expression-operator symbols. */
|
2012-01-19 17:56:05 -08:00
|
|
|
EQ,
|
|
|
|
LT,
|
|
|
|
LE,
|
|
|
|
EQEQ,
|
|
|
|
NE,
|
|
|
|
GE,
|
|
|
|
GT,
|
|
|
|
ANDAND,
|
|
|
|
OROR,
|
|
|
|
NOT,
|
|
|
|
TILDE,
|
2014-01-09 15:05:33 +02:00
|
|
|
BINOP(BinOp),
|
|
|
|
BINOPEQ(BinOp),
|
2010-09-09 15:59:29 -07:00
|
|
|
|
|
|
|
/* Structural symbols */
|
2012-01-19 17:56:05 -08:00
|
|
|
AT,
|
|
|
|
DOT,
|
2012-08-03 18:01:30 -07:00
|
|
|
DOTDOT,
|
2013-10-25 01:56:34 -04:00
|
|
|
DOTDOTDOT,
|
2012-01-19 17:56:05 -08:00
|
|
|
COMMA,
|
|
|
|
SEMI,
|
|
|
|
COLON,
|
|
|
|
MOD_SEP,
|
|
|
|
RARROW,
|
|
|
|
LARROW,
|
2012-06-04 18:34:10 -07:00
|
|
|
FAT_ARROW,
|
2012-01-19 17:56:05 -08:00
|
|
|
LPAREN,
|
|
|
|
RPAREN,
|
|
|
|
LBRACKET,
|
|
|
|
RBRACKET,
|
|
|
|
LBRACE,
|
|
|
|
RBRACE,
|
|
|
|
POUND,
|
2012-04-22 16:58:04 -07:00
|
|
|
DOLLAR,
|
2014-07-08 14:26:02 +12:00
|
|
|
QUESTION,
|
2012-01-25 16:38:09 -07:00
|
|
|
|
2010-09-09 15:59:29 -07:00
|
|
|
/* Literals */
|
2014-07-06 01:17:59 -07: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 15:59:29 -07:00
|
|
|
|
|
|
|
/* Name components */
|
2014-06-09 13:12:30 -07:00
|
|
|
/// An identifier contains an "is_mod_name" boolean,
|
|
|
|
/// indicating whether :: follows this token with no
|
|
|
|
/// whitespace in between.
|
2014-06-09 13:19:38 -07:00
|
|
|
IDENT(Ident, bool),
|
2012-01-19 17:56:05 -08:00
|
|
|
UNDERSCORE,
|
2014-06-09 13:19:38 -07:00
|
|
|
LIFETIME(Ident),
|
2012-06-12 10:50:17 -07:00
|
|
|
|
2012-06-29 18:26:34 -07:00
|
|
|
/* For interpolation */
|
2014-01-09 15:05:33 +02:00
|
|
|
INTERPOLATED(Nonterminal),
|
2014-07-06 01:17:59 -07:00
|
|
|
DOC_COMMENT(Name),
|
2014-07-04 22:30:39 -07: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 01:17:59 -07:00
|
|
|
SHEBANG(Name),
|
2014-07-04 22:30:39 -07:00
|
|
|
|
2012-01-19 17:56:05 -08:00
|
|
|
EOF,
|
2010-09-09 15:59:29 -07:00
|
|
|
}
|
2010-08-18 11:35:12 -07:00
|
|
|
|
2014-05-31 10:43:52 -07:00
|
|
|
#[deriving(Clone, Encodable, Decodable, PartialEq, Eq, Hash)]
|
2012-07-04 22:53:12 +01:00
|
|
|
/// For interpolation during macro expansion.
|
2014-01-09 15:05:33 +02:00
|
|
|
pub enum Nonterminal {
|
2014-09-13 19:06:01 +03:00
|
|
|
NtItem( P<ast::Item>),
|
2014-01-09 15:05:33 +02:00
|
|
|
NtBlock(P<ast::Block>),
|
2014-09-13 19:06:01 +03:00
|
|
|
NtStmt( P<ast::Stmt>),
|
|
|
|
NtPat( P<ast::Pat>),
|
|
|
|
NtExpr( P<ast::Expr>),
|
|
|
|
NtTy( P<ast::Ty>),
|
2014-06-09 13:19:38 -07:00
|
|
|
/// See IDENT, above, for meaning of bool in NtIdent:
|
|
|
|
NtIdent(Box<Ident>, bool),
|
|
|
|
/// Stuff inside brackets for attributes
|
2014-09-13 19:06:01 +03:00
|
|
|
NtMeta( P<ast::MetaItem>),
|
2014-05-05 18:56:44 -07:00
|
|
|
NtPath(Box<ast::Path>),
|
2014-09-13 19:06:01 +03:00
|
|
|
NtTT( P<ast::TokenTree>), // needs P'ed to break a circularity
|
|
|
|
NtMatchers(Vec<ast::Matcher>)
|
2012-06-12 10:50:17 -07:00
|
|
|
}
|
|
|
|
|
2014-02-28 01:23:06 -08: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 16:14:07 -07:00
|
|
|
NtMeta(..) => f.pad("NtMeta(..)"),
|
2014-02-28 01:23:06 -08:00
|
|
|
NtPath(..) => f.pad("NtPath(..)"),
|
|
|
|
NtTT(..) => f.pad("NtTT(..)"),
|
|
|
|
NtMatchers(..) => f.pad("NtMatchers(..)"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-21 03:39:03 -07:00
|
|
|
pub fn binop_to_string(o: BinOp) -> &'static str {
|
2012-08-06 12:34:08 -07:00
|
|
|
match o {
|
2014-05-20 10:37:08 -07:00
|
|
|
PLUS => "+",
|
|
|
|
MINUS => "-",
|
|
|
|
STAR => "*",
|
|
|
|
SLASH => "/",
|
|
|
|
PERCENT => "%",
|
|
|
|
CARET => "^",
|
|
|
|
AND => "&",
|
|
|
|
OR => "|",
|
|
|
|
SHL => "<<",
|
|
|
|
SHR => ">>"
|
2010-08-20 15:57:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-21 03:39:03 -07:00
|
|
|
pub fn to_string(t: &Token) -> String {
|
2013-02-24 19:54:37 -08:00
|
|
|
match *t {
|
2014-09-13 01:13:38 -04: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 16:33:43 -07:00
|
|
|
BINOPEQ(op) => {
|
2014-09-13 01:13:38 -04:00
|
|
|
let mut s = binop_to_string(op).into_string();
|
2014-05-07 16:33:43 -07:00
|
|
|
s.push_str("=");
|
|
|
|
s
|
|
|
|
}
|
2011-09-02 15:34:58 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
/* Structural symbols */
|
2014-09-13 01:13:38 -04: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 16:38:09 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
/* Literals */
|
2014-06-06 16:04:04 +01:00
|
|
|
LIT_BYTE(b) => {
|
2014-07-06 01:17:59 -07:00
|
|
|
format!("b'{}'", b.as_str())
|
2014-06-06 16:04:04 +01:00
|
|
|
}
|
2013-09-03 19:24:12 -04:00
|
|
|
LIT_CHAR(c) => {
|
2014-07-06 01:17:59 -07:00
|
|
|
format!("'{}'", c.as_str())
|
2011-12-07 21:06:12 +01:00
|
|
|
}
|
2014-06-18 10:44:20 -07:00
|
|
|
LIT_INTEGER(c) | LIT_FLOAT(c) => {
|
2014-09-13 01:13:38 -04:00
|
|
|
c.as_str().into_string()
|
2012-11-07 18:40:34 -08:00
|
|
|
}
|
2014-06-18 10:44:20 -07:00
|
|
|
|
2014-02-14 07:07:09 +02:00
|
|
|
LIT_STR(s) => {
|
2014-07-06 01:17:59 -07:00
|
|
|
format!("\"{}\"", s.as_str())
|
2014-01-31 15:30:19 -08:00
|
|
|
}
|
2014-02-14 07:07:09 +02:00
|
|
|
LIT_STR_RAW(s, n) => {
|
2014-06-07 15:32:01 +01:00
|
|
|
format!("r{delim}\"{string}\"{delim}",
|
2014-07-06 01:17:59 -07:00
|
|
|
delim="#".repeat(n), string=s.as_str())
|
2014-06-07 15:32:01 +01:00
|
|
|
}
|
2014-07-03 00:47:30 -07:00
|
|
|
LIT_BINARY(v) => {
|
2014-07-06 01:17:59 -07:00
|
|
|
format!("b\"{}\"", v.as_str())
|
2013-10-02 03:32:29 +02:00
|
|
|
}
|
2014-07-03 00:47:30 -07:00
|
|
|
LIT_BINARY_RAW(s, n) => {
|
2014-06-13 18:56:24 +01:00
|
|
|
format!("br{delim}\"{string}\"{delim}",
|
2014-07-06 01:17:59 -07:00
|
|
|
delim="#".repeat(n), string=s.as_str())
|
2014-06-13 18:56:24 +01:00
|
|
|
}
|
2012-06-30 11:54:54 +01:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
/* Name components */
|
2014-09-13 01:13:38 -04:00
|
|
|
IDENT(s, _) => get_ident(s).get().into_string(),
|
2014-01-31 16:42:33 -08:00
|
|
|
LIFETIME(s) => {
|
2014-06-07 15:32:01 +01:00
|
|
|
format!("{}", get_ident(s))
|
2014-01-31 16:42:33 -08:00
|
|
|
}
|
2014-09-13 01:13:38 -04:00
|
|
|
UNDERSCORE => "_".into_string(),
|
2012-06-30 11:54:54 +01:00
|
|
|
|
|
|
|
/* Other */
|
2014-09-13 01:13:38 -04:00
|
|
|
DOC_COMMENT(s) => s.as_str().into_string(),
|
|
|
|
EOF => "<eof>".into_string(),
|
|
|
|
WS => " ".into_string(),
|
|
|
|
COMMENT => "/* */".into_string(),
|
2014-07-04 22:30:39 -07:00
|
|
|
SHEBANG(s) => format!("/* shebang: {}*/", s.as_str()),
|
|
|
|
|
2012-12-04 10:50:00 -08:00
|
|
|
INTERPOLATED(ref nt) => {
|
2013-01-22 16:00:07 -08:00
|
|
|
match nt {
|
2014-06-21 03:39:03 -07:00
|
|
|
&NtExpr(ref e) => ::print::pprust::expr_to_string(&**e),
|
|
|
|
&NtMeta(ref e) => ::print::pprust::meta_item_to_string(&**e),
|
2014-07-19 11:59:44 +02:00
|
|
|
&NtTy(ref e) => ::print::pprust::ty_to_string(&**e),
|
|
|
|
&NtPath(ref e) => ::print::pprust::path_to_string(&**e),
|
2013-01-22 16:00:07 -08:00
|
|
|
_ => {
|
2014-09-13 01:13:38 -04:00
|
|
|
let mut s = "an interpolated ".into_string();
|
2014-05-07 16:33:43 -07: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 11:59:44 +02:00
|
|
|
NtExpr(..) => fail!("should have been handled"),
|
|
|
|
NtTy(..) => fail!("should have been handled"),
|
2014-05-07 16:33:43 -07:00
|
|
|
NtIdent(..) => s.push_str("identifier"),
|
2014-07-19 11:59:44 +02:00
|
|
|
NtPath(..) => fail!("should have been handled"),
|
2014-05-07 16:33:43 -07:00
|
|
|
NtTT(..) => s.push_str("tt"),
|
|
|
|
NtMatchers(..) => s.push_str("matcher sequence")
|
|
|
|
};
|
|
|
|
s
|
2012-07-06 14:48:01 -07:00
|
|
|
}
|
2013-01-22 16:00:07 -08:00
|
|
|
}
|
2012-06-29 18:26:34 -07:00
|
|
|
}
|
2010-08-20 11:41:34 -07:00
|
|
|
}
|
|
|
|
}
|
2011-07-03 11:48:14 -07:00
|
|
|
|
2013-03-22 11:09:13 -07:00
|
|
|
pub fn can_begin_expr(t: &Token) -> bool {
|
2013-02-24 17:24:28 -08:00
|
|
|
match *t {
|
2012-08-03 19:59:04 -07:00
|
|
|
LPAREN => true,
|
|
|
|
LBRACE => true,
|
|
|
|
LBRACKET => true,
|
|
|
|
IDENT(_, _) => true,
|
|
|
|
UNDERSCORE => true,
|
|
|
|
TILDE => true,
|
2014-06-06 16:04:04 +01:00
|
|
|
LIT_BYTE(_) => true,
|
2013-09-03 19:24:12 -04:00
|
|
|
LIT_CHAR(_) => true,
|
2014-06-18 10:44:20 -07:00
|
|
|
LIT_INTEGER(_) => true,
|
|
|
|
LIT_FLOAT(_) => true,
|
2012-08-03 19:59:04 -07:00
|
|
|
LIT_STR(_) => true,
|
2013-10-02 03:32:29 +02:00
|
|
|
LIT_STR_RAW(_, _) => true,
|
2014-06-07 15:32:01 +01:00
|
|
|
LIT_BINARY(_) => true,
|
2014-06-13 18:56:24 +01:00
|
|
|
LIT_BINARY_RAW(_, _) => true,
|
2012-08-03 19:59:04 -07: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 15:05:33 +02:00
|
|
|
INTERPOLATED(NtExpr(..))
|
|
|
|
| INTERPOLATED(NtIdent(..))
|
|
|
|
| INTERPOLATED(NtBlock(..))
|
|
|
|
| INTERPOLATED(NtPath(..)) => true,
|
2012-08-03 19:59:04 -07:00
|
|
|
_ => false
|
2011-07-03 11:48:14 -07:00
|
|
|
}
|
|
|
|
}
|
2011-10-07 16:22:53 +02:00
|
|
|
|
2014-03-16 22:46:04 +01: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 10:00:25 -08:00
|
|
|
match *t {
|
2014-03-16 22:46:04 +01:00
|
|
|
LPAREN => Some(RPAREN),
|
|
|
|
LBRACE => Some(RBRACE),
|
|
|
|
LBRACKET => Some(RBRACKET),
|
|
|
|
_ => None
|
2012-07-31 13:53:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-24 17:24:28 -08:00
|
|
|
pub fn is_lit(t: &Token) -> bool {
|
|
|
|
match *t {
|
2014-06-06 16:04:04 +01:00
|
|
|
LIT_BYTE(_) => true,
|
2013-09-03 19:24:12 -04:00
|
|
|
LIT_CHAR(_) => true,
|
2014-06-18 10:44:20 -07:00
|
|
|
LIT_INTEGER(_) => true,
|
|
|
|
LIT_FLOAT(_) => true,
|
2012-08-03 19:59:04 -07:00
|
|
|
LIT_STR(_) => true,
|
2013-10-02 03:32:29 +02:00
|
|
|
LIT_STR_RAW(_, _) => true,
|
2014-06-07 15:32:01 +01:00
|
|
|
LIT_BINARY(_) => true,
|
2014-06-13 18:56:24 +01:00
|
|
|
LIT_BINARY_RAW(_, _) => true,
|
2012-08-03 19:59:04 -07:00
|
|
|
_ => false
|
2012-06-11 16:49:35 -07:00
|
|
|
}
|
2012-04-22 14:59:04 -07:00
|
|
|
}
|
|
|
|
|
2013-03-22 11:09:13 -07:00
|
|
|
pub fn is_ident(t: &Token) -> bool {
|
2013-02-24 17:24:28 -08:00
|
|
|
match *t { IDENT(_, _) => true, _ => false }
|
2012-04-17 21:14:40 -07:00
|
|
|
}
|
|
|
|
|
2013-03-22 11:09:13 -07:00
|
|
|
pub fn is_ident_or_path(t: &Token) -> bool {
|
2013-02-24 17:24:28 -08:00
|
|
|
match *t {
|
2014-01-09 15:05:33 +02:00
|
|
|
IDENT(_, _) | INTERPOLATED(NtPath(..)) => true,
|
2012-08-06 13:09:10 -07:00
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-22 11:09:13 -07:00
|
|
|
pub fn is_plain_ident(t: &Token) -> bool {
|
2013-02-24 17:24:28 -08:00
|
|
|
match *t { IDENT(_, false) => true, _ => false }
|
2012-04-17 21:14:40 -07:00
|
|
|
}
|
|
|
|
|
2013-12-11 15:15:55 +11: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 17:08:48 +11: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 15:15:55 +11:00
|
|
|
'strict:
|
|
|
|
$( ($sk_name:expr, $sk_variant:ident, $sk_str:expr); )*
|
|
|
|
'reserved:
|
|
|
|
$( ($rk_name:expr, $rk_variant:ident, $rk_str:expr); )*
|
2013-12-10 17:08:48 +11:00
|
|
|
}
|
|
|
|
) => {
|
2014-07-06 01:17:59 -07: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 15:15:55 +11:00
|
|
|
|
2013-12-10 17:08:48 +11:00
|
|
|
pub mod special_idents {
|
2014-07-06 01:17:59 -07:00
|
|
|
use ast::{Ident, Name};
|
|
|
|
$( pub static $si_static: Ident = Ident { name: Name($si_name), ctxt: 0 }; )*
|
2013-12-10 17:08:48 +11:00
|
|
|
}
|
|
|
|
|
2014-07-06 16:02:48 -07:00
|
|
|
pub mod special_names {
|
|
|
|
use ast::Name;
|
2014-07-06 01:17:59 -07:00
|
|
|
$( pub static $si_static: Name = Name($si_name); )*
|
2014-07-06 16:02:48 -07:00
|
|
|
}
|
|
|
|
|
2013-12-10 17:08:48 +11: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 15:19:29 -07:00
|
|
|
use ast::Name;
|
2013-12-10 17:08:48 +11:00
|
|
|
|
|
|
|
pub enum Keyword {
|
2013-12-11 15:15:55 +11:00
|
|
|
$( $sk_variant, )*
|
|
|
|
$( $rk_variant, )*
|
2013-12-10 17:08:48 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Keyword {
|
2014-07-06 15:19:29 -07:00
|
|
|
pub fn to_name(&self) -> Name {
|
2013-12-10 17:08:48 +11:00
|
|
|
match *self {
|
2014-07-06 01:17:59 -07:00
|
|
|
$( $sk_variant => Name($sk_name), )*
|
|
|
|
$( $rk_variant => Name($rk_name), )*
|
2013-12-10 17:08:48 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-14 07:07:09 +02:00
|
|
|
fn mk_fresh_ident_interner() -> IdentInterner {
|
2013-12-10 17:08:48 +11:00
|
|
|
// The indices here must correspond to the numbers in
|
2014-07-06 15:19:29 -07:00
|
|
|
// special_idents, in Keyword to_name(), and in static
|
2013-12-10 17:08:48 +11:00
|
|
|
// constants below.
|
2014-02-28 12:54:01 -08: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 17:08:48 +11:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
|
2013-12-11 15:15:55 +11:00
|
|
|
// If the special idents get renumbered, remember to modify these two as appropriate
|
2014-07-06 01:17:59 -07:00
|
|
|
pub static SELF_KEYWORD_NAME: Name = Name(SELF_KEYWORD_NAME_NUM);
|
|
|
|
static STATIC_KEYWORD_NAME: Name = Name(STATIC_KEYWORD_NAME_NUM);
|
|
|
|
|
|
|
|
pub static SELF_KEYWORD_NAME_NUM: u32 = 1;
|
|
|
|
static STATIC_KEYWORD_NAME_NUM: u32 = 2;
|
2012-08-02 15:34:13 -07:00
|
|
|
|
2014-05-20 11:59:07 -07: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 17:08:48 +11:00
|
|
|
declare_special_idents_and_keywords! {
|
|
|
|
pub mod special_idents {
|
|
|
|
// These ones are statics
|
2014-02-14 07:07:09 +02:00
|
|
|
(0, invalid, "");
|
2014-07-06 01:17:59 -07:00
|
|
|
(super::SELF_KEYWORD_NAME_NUM, self_, "self");
|
|
|
|
(super::STATIC_KEYWORD_NAME_NUM, statik, "static");
|
2014-06-10 13:54:13 -07:00
|
|
|
(3, static_lifetime, "'static");
|
2013-12-10 17:08:48 +11:00
|
|
|
|
2013-12-11 14:46:16 +11:00
|
|
|
// for matcher NTs
|
2014-06-10 13:54:13 -07:00
|
|
|
(4, tt, "tt");
|
|
|
|
(5, matchers, "matchers");
|
2013-12-11 14:46:16 +11:00
|
|
|
|
|
|
|
// outside of libsyntax
|
2014-06-10 13:54:13 -07:00
|
|
|
(6, clownshoe_abi, "__rust_abi");
|
|
|
|
(7, opaque, "<opaque>");
|
|
|
|
(8, unnamed_field, "<unnamed_field>");
|
|
|
|
(9, type_self, "Self");
|
2014-08-12 20:31:30 -07:00
|
|
|
(10, prelude_import, "prelude_import");
|
2013-12-10 17:08:48 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
pub mod keywords {
|
|
|
|
// These ones are variants of the Keyword enum
|
2013-12-11 14:46:16 +11:00
|
|
|
|
2013-12-11 15:15:55 +11:00
|
|
|
'strict:
|
2014-08-12 20:31:30 -07:00
|
|
|
(11, As, "as");
|
|
|
|
(12, Break, "break");
|
|
|
|
(13, Crate, "crate");
|
|
|
|
(14, Else, "else");
|
|
|
|
(15, Enum, "enum");
|
|
|
|
(16, Extern, "extern");
|
|
|
|
(17, False, "false");
|
|
|
|
(18, Fn, "fn");
|
|
|
|
(19, For, "for");
|
|
|
|
(20, If, "if");
|
|
|
|
(21, Impl, "impl");
|
|
|
|
(22, In, "in");
|
|
|
|
(23, Let, "let");
|
|
|
|
(24, Loop, "loop");
|
|
|
|
(25, Match, "match");
|
|
|
|
(26, Mod, "mod");
|
|
|
|
(27, Mut, "mut");
|
|
|
|
(28, Once, "once");
|
|
|
|
(29, Pub, "pub");
|
|
|
|
(30, Ref, "ref");
|
|
|
|
(31, Return, "return");
|
2013-12-10 17:08:48 +11:00
|
|
|
// Static and Self are also special idents (prefill de-dupes)
|
2014-07-06 01:17:59 -07:00
|
|
|
(super::STATIC_KEYWORD_NAME_NUM, Static, "static");
|
|
|
|
(super::SELF_KEYWORD_NAME_NUM, Self, "self");
|
2014-08-12 20:31:30 -07:00
|
|
|
(32, Struct, "struct");
|
|
|
|
(33, Super, "super");
|
|
|
|
(34, True, "true");
|
|
|
|
(35, Trait, "trait");
|
|
|
|
(36, Type, "type");
|
|
|
|
(37, Unsafe, "unsafe");
|
|
|
|
(38, Use, "use");
|
|
|
|
(39, Virtual, "virtual");
|
|
|
|
(40, While, "while");
|
|
|
|
(41, Continue, "continue");
|
|
|
|
(42, Proc, "proc");
|
|
|
|
(43, Box, "box");
|
|
|
|
(44, Const, "const");
|
|
|
|
(45, Where, "where");
|
2013-12-11 14:46:16 +11:00
|
|
|
|
2013-12-11 15:15:55 +11:00
|
|
|
'reserved:
|
2014-08-12 20:31:30 -07:00
|
|
|
(46, Alignof, "alignof");
|
|
|
|
(47, Be, "be");
|
|
|
|
(48, Offsetof, "offsetof");
|
|
|
|
(49, Priv, "priv");
|
|
|
|
(50, Pure, "pure");
|
|
|
|
(51, Sizeof, "sizeof");
|
|
|
|
(52, Typeof, "typeof");
|
|
|
|
(53, Unsized, "unsized");
|
|
|
|
(54, Yield, "yield");
|
|
|
|
(55, Do, "do");
|
2013-12-10 17:08:48 +11:00
|
|
|
}
|
|
|
|
}
|
2013-07-03 15:15:45 -07:00
|
|
|
|
2013-03-29 10:04:48 -07:00
|
|
|
/**
|
|
|
|
* Maps a token to a record specifying the corresponding binary
|
|
|
|
* operator
|
|
|
|
*/
|
2013-09-02 03:45:37 +02:00
|
|
|
pub fn token_to_binop(tok: &Token) -> Option<ast::BinOp> {
|
2013-07-02 12:47:32 -07:00
|
|
|
match *tok {
|
2013-09-02 03:45:37 +02: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 10:04:48 -07:00
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-26 10:11:19 -07:00
|
|
|
// looks like we can get rid of this completely...
|
2014-01-09 15:05:33 +02:00
|
|
|
pub type IdentInterner = StrInterner;
|
2013-06-19 21:12:40 -04:00
|
|
|
|
2013-04-23 10:57:41 -07:00
|
|
|
// if an interner exists in TLS, return it. Otherwise, prepare a
|
|
|
|
// fresh one.
|
2014-03-27 19:28:38 +02: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-28 20:36:08 -07:00
|
|
|
match key.get() {
|
|
|
|
Some(interner) => interner.clone(),
|
2013-07-12 01:38:44 -07:00
|
|
|
None => {
|
2014-03-27 19:28:38 +02:00
|
|
|
let interner = Rc::new(mk_fresh_ident_interner());
|
2014-04-28 20:36:08 -07:00
|
|
|
key.replace(Some(interner.clone()));
|
2013-07-12 01:38:44 -07:00
|
|
|
interner
|
2012-10-08 10:41:02 -07:00
|
|
|
}
|
|
|
|
}
|
2012-08-02 14:33:26 -07:00
|
|
|
}
|
|
|
|
|
2014-01-08 10:35:15 -08: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-01-31 18:25:08 -08: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 10:35:15 -08: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 10:43:52 -07:00
|
|
|
#[deriving(Clone, PartialEq, Hash, PartialOrd, Eq, Ord)]
|
2014-01-08 10:35:15 -08:00
|
|
|
pub struct InternedString {
|
2014-03-27 15:39:48 -07:00
|
|
|
string: RcStr,
|
2014-01-08 10:35:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl InternedString {
|
|
|
|
#[inline]
|
|
|
|
pub fn new(string: &'static str) -> InternedString {
|
|
|
|
InternedString {
|
2014-01-31 16:42:33 -08:00
|
|
|
string: RcStr::new(string),
|
2014-01-08 10:35:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-01-31 16:42:33 -08:00
|
|
|
fn new_from_rc_str(string: RcStr) -> InternedString {
|
2014-01-08 10:35:15 -08:00
|
|
|
InternedString {
|
|
|
|
string: string,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn get<'a>(&'a self) -> &'a str {
|
|
|
|
self.string.as_slice()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-10 14:02:36 -08:00
|
|
|
impl BytesContainer for InternedString {
|
|
|
|
fn container_as_bytes<'a>(&'a self) -> &'a [u8] {
|
2014-01-31 18:25:08 -08: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 14:02:36 -08: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 10:34:51 -07:00
|
|
|
mem::transmute(this.container_as_bytes())
|
2014-01-10 14:02:36 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-30 00:46:37 +11:00
|
|
|
impl fmt::Show for InternedString {
|
2014-02-05 23:55:13 +11:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2014-05-10 14:05:06 -07:00
|
|
|
write!(f, "{}", self.string.as_slice())
|
2014-01-08 10:35:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Equiv<&'a str> for InternedString {
|
|
|
|
fn equiv(&self, other: & &'a str) -> bool {
|
|
|
|
(*other) == self.string.as_slice()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-18 10:58:26 -07:00
|
|
|
impl<D:Decoder<E>, E> Decodable<D, E> for InternedString {
|
|
|
|
fn decode(d: &mut D) -> Result<InternedString, E> {
|
2014-05-14 21:16:44 -07:00
|
|
|
Ok(get_name(get_ident_interner().intern(
|
|
|
|
try!(d.read_str()).as_slice())))
|
2014-03-18 10:58:26 -07: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 07:07:09 +02: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 10:35:15 -08:00
|
|
|
/// Returns the string contents of an identifier, using the task-local
|
|
|
|
/// interner.
|
|
|
|
#[inline]
|
2014-02-14 07:07:09 +02:00
|
|
|
pub fn get_ident(ident: Ident) -> InternedString {
|
|
|
|
get_name(ident.name)
|
2014-01-08 10:35:15 -08: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 07:07:09 +02:00
|
|
|
get_name(intern(s))
|
2012-07-18 16:18:02 -07:00
|
|
|
}
|
|
|
|
|
2014-02-14 07:07:09 +02:00
|
|
|
/// Maps a string to its interned representation.
|
2014-01-08 10:35:15 -08:00
|
|
|
#[inline]
|
2014-02-14 07:07:09 +02:00
|
|
|
pub fn intern(s: &str) -> Name {
|
|
|
|
get_ident_interner().intern(s)
|
2013-06-04 12:34:25 -07:00
|
|
|
}
|
|
|
|
|
2014-02-14 07:07:09 +02: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 12:34:25 -07:00
|
|
|
}
|
|
|
|
|
2014-02-14 07:07:09 +02:00
|
|
|
/// Maps a string to an identifier with an empty syntax context.
|
|
|
|
#[inline]
|
2014-06-09 13:19:38 -07:00
|
|
|
pub fn str_to_ident(s: &str) -> Ident {
|
|
|
|
Ident::new(intern(s))
|
2013-06-04 12:34:25 -07:00
|
|
|
}
|
|
|
|
|
2014-02-14 07:07:09 +02:00
|
|
|
/// Maps a string to a gensym'ed identifier.
|
|
|
|
#[inline]
|
2014-06-09 13:19:38 -07:00
|
|
|
pub fn gensym_ident(s: &str) -> Ident {
|
|
|
|
Ident::new(gensym(s))
|
2013-05-07 12:34:52 -07:00
|
|
|
}
|
|
|
|
|
2013-06-07 10:41:18 -07:00
|
|
|
// create a fresh name that maps to the same string as the old one.
|
2014-06-21 03:39:03 -07:00
|
|
|
// note that this guarantees that str_ptr_eq(ident_to_string(src),interner_get(fresh_name(src)));
|
2013-06-25 16:48:03 -07:00
|
|
|
// that is, that the new name and the old one are connected to ptr_eq strings.
|
2014-06-09 13:19:38 -07:00
|
|
|
pub fn fresh_name(src: &Ident) -> Name {
|
2013-06-26 10:11:19 -07:00
|
|
|
let interner = get_ident_interner();
|
|
|
|
interner.gensym_copy(src.name)
|
2013-06-07 14:53:53 -07: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 16:48:03 -07:00
|
|
|
// locations. Also definitely destroys the guarantee given above about ptr_eq.
|
2014-03-02 12:59:35 +11:00
|
|
|
/*let num = rand::task_rng().gen_uint_range(0,0xffff);
|
2014-06-21 03:39:03 -07:00
|
|
|
gensym(format!("{}_{}",ident_to_string(src),num))*/
|
2013-05-14 11:34:17 -07:00
|
|
|
}
|
|
|
|
|
2013-06-25 11:43:52 -07:00
|
|
|
// create a fresh mark.
|
|
|
|
pub fn fresh_mark() -> Mrk {
|
2014-07-06 01:17:59 -07:00
|
|
|
gensym("mark").uint() as u32
|
2013-06-25 11:43:52 -07:00
|
|
|
}
|
|
|
|
|
2013-12-10 17:08:48 +11:00
|
|
|
// See the macro above about the types of keywords
|
2013-05-25 17:45:45 +02:00
|
|
|
|
|
|
|
pub fn is_keyword(kw: keywords::Keyword, tok: &Token) -> bool {
|
|
|
|
match *tok {
|
2014-07-06 15:19:29 -07:00
|
|
|
token::IDENT(sid, false) => { kw.to_name() == sid.name }
|
2013-05-25 17:45:45 +02:00
|
|
|
_ => { false }
|
2013-05-10 15:15:06 -07:00
|
|
|
}
|
2013-05-25 17:45:45 +02:00
|
|
|
}
|
2013-05-10 15:15:06 -07:00
|
|
|
|
2013-06-19 21:12:40 -04:00
|
|
|
pub fn is_any_keyword(tok: &Token) -> bool {
|
|
|
|
match *tok {
|
2014-07-06 01:17:59 -07:00
|
|
|
token::IDENT(sid, false) => {
|
|
|
|
let n = sid.name;
|
|
|
|
|
|
|
|
n == SELF_KEYWORD_NAME
|
|
|
|
|| n == STATIC_KEYWORD_NAME
|
|
|
|
|| STRICT_KEYWORD_START <= n
|
|
|
|
&& n <= RESERVED_KEYWORD_FINAL
|
2013-06-19 21:12:40 -04:00
|
|
|
},
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_strict_keyword(tok: &Token) -> bool {
|
|
|
|
match *tok {
|
2014-07-06 01:17:59 -07:00
|
|
|
token::IDENT(sid, false) => {
|
|
|
|
let n = sid.name;
|
|
|
|
|
|
|
|
n == SELF_KEYWORD_NAME
|
|
|
|
|| n == STATIC_KEYWORD_NAME
|
|
|
|
|| STRICT_KEYWORD_START <= n
|
|
|
|
&& n <= STRICT_KEYWORD_FINAL
|
2013-06-19 21:12:40 -04:00
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_reserved_keyword(tok: &Token) -> bool {
|
|
|
|
match *tok {
|
2014-07-06 01:17:59 -07:00
|
|
|
token::IDENT(sid, false) => {
|
|
|
|
let n = sid.name;
|
|
|
|
|
|
|
|
RESERVED_KEYWORD_START <= n
|
|
|
|
&& n <= RESERVED_KEYWORD_FINAL
|
2013-06-19 21:12:40 -04:00
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-05 14:15:00 -07:00
|
|
|
pub fn mtwt_token_eq(t1 : &Token, t2 : &Token) -> bool {
|
|
|
|
match (t1,t2) {
|
2014-02-15 16:54:32 +08:00
|
|
|
(&IDENT(id1,_),&IDENT(id2,_)) | (&LIFETIME(id1),&LIFETIME(id2)) =>
|
2014-02-25 04:47:19 +08:00
|
|
|
mtwt::resolve(id1) == mtwt::resolve(id2),
|
2013-09-05 14:15:00 -07:00
|
|
|
_ => *t1 == *t2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-14 11:34:17 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
2013-06-25 16:48:03 -07:00
|
|
|
use ast;
|
2014-02-25 04:47:19 +08:00
|
|
|
use ext::mtwt;
|
2013-06-25 16:48:03 -07:00
|
|
|
|
2014-07-08 22:28:52 -07: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 14:15:00 -07: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 11:34:17 -07:00
|
|
|
}
|