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;
|
2013-12-01 00:00:39 +02:00
|
|
|
use ast::{P, Name, Mrk};
|
2012-12-23 17:41:37 -05:00
|
|
|
use ast_util;
|
|
|
|
use parse::token;
|
2013-05-02 10:16:07 +02:00
|
|
|
use util::interner::StrInterner;
|
2012-12-23 17:41:37 -05:00
|
|
|
use util::interner;
|
|
|
|
|
2013-06-26 10:11:19 -07:00
|
|
|
use std::cast;
|
2013-09-03 19:24:12 -04:00
|
|
|
use std::char;
|
2013-06-24 20:40:33 -04:00
|
|
|
use std::local_data;
|
2010-08-18 11:35:12 -07:00
|
|
|
|
2013-07-02 12:47:32 -07:00
|
|
|
#[deriving(Clone, Encodable, Decodable, Eq, IterBytes)]
|
2013-01-29 13:54:06 -08: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
|
|
|
}
|
|
|
|
|
2013-07-02 12:47:32 -07:00
|
|
|
#[deriving(Clone, Encodable, Decodable, Eq, IterBytes)]
|
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,
|
|
|
|
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,
|
|
|
|
DARROW,
|
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,
|
2012-01-25 16:38:09 -07:00
|
|
|
|
2010-09-09 15:59:29 -07:00
|
|
|
/* Literals */
|
2013-09-03 19:24:12 -04:00
|
|
|
LIT_CHAR(u32),
|
2012-01-19 17:56:05 -08:00
|
|
|
LIT_INT(i64, ast::int_ty),
|
|
|
|
LIT_UINT(u64, ast::uint_ty),
|
2012-06-14 19:41:40 -07:00
|
|
|
LIT_INT_UNSUFFIXED(i64),
|
2013-09-02 02:50:59 +02:00
|
|
|
LIT_FLOAT(ast::Ident, ast::float_ty),
|
|
|
|
LIT_FLOAT_UNSUFFIXED(ast::Ident),
|
|
|
|
LIT_STR(ast::Ident),
|
2013-10-02 03:32:29 +02:00
|
|
|
LIT_STR_RAW(ast::Ident, uint), /* raw str delimited by n hash symbols */
|
2010-09-09 15:59:29 -07:00
|
|
|
|
|
|
|
/* Name components */
|
2013-02-26 10:15:29 -08:00
|
|
|
// an identifier contains an "is_mod_name" boolean,
|
|
|
|
// indicating whether :: follows this token with no
|
|
|
|
// whitespace in between.
|
2013-09-02 02:50:59 +02:00
|
|
|
IDENT(ast::Ident, bool),
|
2012-01-19 17:56:05 -08:00
|
|
|
UNDERSCORE,
|
2013-09-02 02:50:59 +02:00
|
|
|
LIFETIME(ast::Ident),
|
2012-06-12 10:50:17 -07:00
|
|
|
|
2012-06-29 18:26:34 -07:00
|
|
|
/* For interpolation */
|
2012-07-27 19:14:46 -07:00
|
|
|
INTERPOLATED(nonterminal),
|
2012-06-12 10:50:17 -07:00
|
|
|
|
2013-09-02 02:50:59 +02:00
|
|
|
DOC_COMMENT(ast::Ident),
|
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
|
|
|
|
2013-07-02 12:47:32 -07:00
|
|
|
#[deriving(Clone, Encodable, Decodable, Eq, IterBytes)]
|
2012-07-04 22:53:12 +01:00
|
|
|
/// For interpolation during macro expansion.
|
2013-01-29 13:54:06 -08:00
|
|
|
pub enum nonterminal {
|
2012-07-27 19:14:46 -07:00
|
|
|
nt_item(@ast::item),
|
2013-12-01 00:00:39 +02:00
|
|
|
nt_block(P<ast::Block>),
|
2013-09-02 03:45:37 +02:00
|
|
|
nt_stmt(@ast::Stmt),
|
|
|
|
nt_pat( @ast::Pat),
|
|
|
|
nt_expr(@ast::Expr),
|
2013-12-01 00:00:39 +02:00
|
|
|
nt_ty( P<ast::Ty>),
|
2013-09-02 02:50:59 +02:00
|
|
|
nt_ident(~ast::Ident, bool),
|
2013-08-08 13:28:06 -04:00
|
|
|
nt_attr(@ast::Attribute), // #[foo]
|
2013-08-09 19:55:15 +02:00
|
|
|
nt_path(~ast::Path),
|
2012-07-27 19:14:46 -07:00
|
|
|
nt_tt( @ast::token_tree), //needs @ed to break a circularity
|
|
|
|
nt_matchers(~[ast::matcher])
|
2012-06-12 10:50:17 -07:00
|
|
|
}
|
|
|
|
|
2013-03-22 11:09:13 -07:00
|
|
|
pub fn binop_to_str(o: binop) -> ~str {
|
2012-08-06 12:34:08 -07:00
|
|
|
match o {
|
2012-08-03 19:59:04 -07:00
|
|
|
PLUS => ~"+",
|
|
|
|
MINUS => ~"-",
|
|
|
|
STAR => ~"*",
|
|
|
|
SLASH => ~"/",
|
|
|
|
PERCENT => ~"%",
|
|
|
|
CARET => ~"^",
|
|
|
|
AND => ~"&",
|
|
|
|
OR => ~"|",
|
|
|
|
SHL => ~"<<",
|
|
|
|
SHR => ~">>"
|
2010-08-20 15:57:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-31 17:59:59 -04:00
|
|
|
pub fn to_str(input: @ident_interner, t: &Token) -> ~str {
|
2013-02-24 19:54:37 -08:00
|
|
|
match *t {
|
2012-08-03 19:59:04 -07:00
|
|
|
EQ => ~"=",
|
|
|
|
LT => ~"<",
|
|
|
|
LE => ~"<=",
|
|
|
|
EQEQ => ~"==",
|
|
|
|
NE => ~"!=",
|
|
|
|
GE => ~">=",
|
|
|
|
GT => ~">",
|
|
|
|
NOT => ~"!",
|
|
|
|
TILDE => ~"~",
|
|
|
|
OROR => ~"||",
|
|
|
|
ANDAND => ~"&&",
|
|
|
|
BINOP(op) => binop_to_str(op),
|
2013-05-24 01:09:11 +09:00
|
|
|
BINOPEQ(op) => binop_to_str(op) + "=",
|
2011-09-02 15:34:58 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
/* Structural symbols */
|
2012-08-03 19:59:04 -07:00
|
|
|
AT => ~"@",
|
|
|
|
DOT => ~".",
|
|
|
|
DOTDOT => ~"..",
|
2013-10-25 01:56:34 -04:00
|
|
|
DOTDOTDOT => ~"...",
|
2012-08-03 19:59:04 -07:00
|
|
|
COMMA => ~",",
|
|
|
|
SEMI => ~";",
|
|
|
|
COLON => ~":",
|
|
|
|
MOD_SEP => ~"::",
|
|
|
|
RARROW => ~"->",
|
|
|
|
LARROW => ~"<-",
|
|
|
|
DARROW => ~"<->",
|
|
|
|
FAT_ARROW => ~"=>",
|
|
|
|
LPAREN => ~"(",
|
|
|
|
RPAREN => ~")",
|
|
|
|
LBRACKET => ~"[",
|
|
|
|
RBRACKET => ~"]",
|
|
|
|
LBRACE => ~"{",
|
|
|
|
RBRACE => ~"}",
|
|
|
|
POUND => ~"#",
|
|
|
|
DOLLAR => ~"$",
|
2012-01-25 16:38:09 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
/* Literals */
|
2013-09-03 19:24:12 -04:00
|
|
|
LIT_CHAR(c) => {
|
2013-06-28 14:04:13 -07:00
|
|
|
let mut res = ~"'";
|
2013-11-20 16:23:04 -08:00
|
|
|
char::from_u32(c).unwrap().escape_default(|c| {
|
2013-06-28 14:04:13 -07:00
|
|
|
res.push_char(c);
|
2013-11-20 16:23:04 -08:00
|
|
|
});
|
2013-06-28 14:04:13 -07:00
|
|
|
res.push_char('\'');
|
|
|
|
res
|
2011-12-07 21:06:12 +01:00
|
|
|
}
|
2012-08-03 19:59:04 -07:00
|
|
|
LIT_INT(i, t) => {
|
2012-11-29 12:08:59 -08:00
|
|
|
i.to_str() + ast_util::int_ty_to_str(t)
|
2011-07-27 14:19:39 +02:00
|
|
|
}
|
2012-08-03 19:59:04 -07:00
|
|
|
LIT_UINT(u, t) => {
|
2012-11-29 12:08:59 -08:00
|
|
|
u.to_str() + ast_util::uint_ty_to_str(t)
|
2012-06-11 16:31:03 -07:00
|
|
|
}
|
2012-11-29 12:08:59 -08:00
|
|
|
LIT_INT_UNSUFFIXED(i) => { i.to_str() }
|
2013-06-04 12:34:25 -07:00
|
|
|
LIT_FLOAT(ref s, t) => {
|
2013-06-13 03:02:55 +10:00
|
|
|
let mut body = ident_to_str(s).to_owned();
|
2013-05-19 01:07:44 -04:00
|
|
|
if body.ends_with(".") {
|
2013-06-11 19:13:42 -07:00
|
|
|
body.push_char('0'); // `10.f` is not a float literal
|
2012-07-31 11:49:20 -07:00
|
|
|
}
|
|
|
|
body + ast_util::float_ty_to_str(t)
|
|
|
|
}
|
2013-06-04 12:34:25 -07:00
|
|
|
LIT_FLOAT_UNSUFFIXED(ref s) => {
|
2013-06-13 03:02:55 +10:00
|
|
|
let mut body = ident_to_str(s).to_owned();
|
2013-05-19 01:07:44 -04:00
|
|
|
if body.ends_with(".") {
|
2013-06-11 19:13:42 -07:00
|
|
|
body.push_char('0'); // `10.f` is not a float literal
|
2012-11-07 18:40:34 -08:00
|
|
|
}
|
|
|
|
body
|
|
|
|
}
|
2013-09-27 21:01:58 -07:00
|
|
|
LIT_STR(ref s) => { format!("\"{}\"", ident_to_str(s).escape_default()) }
|
2013-10-02 03:32:29 +02:00
|
|
|
LIT_STR_RAW(ref s, n) => {
|
|
|
|
format!("r{delim}\"{string}\"{delim}",
|
|
|
|
delim="#".repeat(n), string=ident_to_str(s))
|
|
|
|
}
|
2012-06-30 11:54:54 +01:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
/* Name components */
|
2013-07-31 17:59:59 -04:00
|
|
|
IDENT(s, _) => input.get(s.name).to_owned(),
|
2013-09-27 21:01:58 -07:00
|
|
|
LIFETIME(s) => format!("'{}", input.get(s.name)),
|
2012-08-03 19:59:04 -07:00
|
|
|
UNDERSCORE => ~"_",
|
2012-06-30 11:54:54 +01:00
|
|
|
|
|
|
|
/* Other */
|
2013-06-13 03:02:55 +10:00
|
|
|
DOC_COMMENT(ref s) => ident_to_str(s).to_owned(),
|
2012-08-03 19:59:04 -07:00
|
|
|
EOF => ~"<eof>",
|
2012-12-04 10:50:00 -08:00
|
|
|
INTERPOLATED(ref nt) => {
|
2013-01-22 16:00:07 -08:00
|
|
|
match nt {
|
2013-07-31 17:59:59 -04:00
|
|
|
&nt_expr(e) => ::print::pprust::expr_to_str(e, input),
|
2013-08-08 13:28:06 -04:00
|
|
|
&nt_attr(e) => ::print::pprust::attribute_to_str(e, input),
|
2013-01-22 16:00:07 -08:00
|
|
|
_ => {
|
|
|
|
~"an interpolated " +
|
|
|
|
match (*nt) {
|
2013-11-28 12:22:53 -08:00
|
|
|
nt_item(..) => ~"item",
|
|
|
|
nt_block(..) => ~"block",
|
|
|
|
nt_stmt(..) => ~"statement",
|
|
|
|
nt_pat(..) => ~"pattern",
|
|
|
|
nt_attr(..) => fail!("should have been handled"),
|
|
|
|
nt_expr(..) => fail!("should have been handled above"),
|
|
|
|
nt_ty(..) => ~"type",
|
|
|
|
nt_ident(..) => ~"identifier",
|
|
|
|
nt_path(..) => ~"path",
|
|
|
|
nt_tt(..) => ~"tt",
|
|
|
|
nt_matchers(..) => ~"matcher sequence"
|
2013-01-22 16:00:07 -08:00
|
|
|
}
|
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,
|
2013-09-03 19:24:12 -04:00
|
|
|
LIT_CHAR(_) => true,
|
2012-08-03 19:59:04 -07:00
|
|
|
LIT_INT(_, _) => true,
|
|
|
|
LIT_UINT(_, _) => true,
|
|
|
|
LIT_INT_UNSUFFIXED(_) => true,
|
|
|
|
LIT_FLOAT(_, _) => true,
|
2012-11-07 18:40:34 -08:00
|
|
|
LIT_FLOAT_UNSUFFIXED(_) => true,
|
2012-08-03 19:59:04 -07:00
|
|
|
LIT_STR(_) => true,
|
2013-10-02 03:32:29 +02:00
|
|
|
LIT_STR_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,
|
2013-11-28 12:22:53 -08:00
|
|
|
INTERPOLATED(nt_expr(..))
|
|
|
|
| INTERPOLATED(nt_ident(..))
|
|
|
|
| INTERPOLATED(nt_block(..))
|
|
|
|
| INTERPOLATED(nt_path(..)) => 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
|
|
|
|
2012-07-31 13:53:00 -07:00
|
|
|
/// what's the opposite delimiter?
|
2013-02-24 10:00:25 -08:00
|
|
|
pub fn flip_delimiter(t: &token::Token) -> token::Token {
|
|
|
|
match *t {
|
2013-02-24 17:24:28 -08:00
|
|
|
LPAREN => RPAREN,
|
|
|
|
LBRACE => RBRACE,
|
|
|
|
LBRACKET => RBRACKET,
|
|
|
|
RPAREN => LPAREN,
|
|
|
|
RBRACE => LBRACE,
|
|
|
|
RBRACKET => LBRACKET,
|
2013-10-21 13:08:31 -07:00
|
|
|
_ => fail!()
|
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 {
|
2013-09-03 19:24:12 -04:00
|
|
|
LIT_CHAR(_) => true,
|
2012-08-03 19:59:04 -07:00
|
|
|
LIT_INT(_, _) => true,
|
|
|
|
LIT_UINT(_, _) => true,
|
|
|
|
LIT_INT_UNSUFFIXED(_) => true,
|
|
|
|
LIT_FLOAT(_, _) => true,
|
2012-11-07 18:40:34 -08:00
|
|
|
LIT_FLOAT_UNSUFFIXED(_) => true,
|
2012-08-03 19:59:04 -07:00
|
|
|
LIT_STR(_) => true,
|
2013-10-02 03:32:29 +02:00
|
|
|
LIT_STR_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 {
|
2013-11-28 12:22:53 -08:00
|
|
|
IDENT(_, _) | INTERPOLATED(nt_path(..)) => 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-03-22 11:09:13 -07:00
|
|
|
pub fn is_bar(t: &Token) -> bool {
|
2013-02-24 17:24:28 -08:00
|
|
|
match *t { BINOP(OR) | OROR => true, _ => false }
|
2012-04-17 21:14:40 -07:00
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
$( ($k_name:expr, $k_variant:ident, $k_str:expr); )*
|
|
|
|
}
|
|
|
|
) => {
|
|
|
|
pub mod special_idents {
|
|
|
|
use ast::Ident;
|
|
|
|
$( pub static $si_static: Ident = Ident { name: $si_name, ctxt: 0 }; )*
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
|
|
|
use ast::Ident;
|
|
|
|
|
|
|
|
pub enum Keyword {
|
|
|
|
$( $k_variant, )*
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Keyword {
|
|
|
|
pub fn to_ident(&self) -> Ident {
|
|
|
|
match *self {
|
|
|
|
$( $k_variant => Ident { name: $k_name, ctxt: 0 }, )*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mk_fresh_ident_interner() -> @ident_interner {
|
|
|
|
// The indices here must correspond to the numbers in
|
|
|
|
// special_idents, in Keyword to_ident(), and in static
|
|
|
|
// constants below.
|
|
|
|
let init_vec = ~[
|
|
|
|
$( $si_str, )*
|
|
|
|
$( $k_str, )*
|
|
|
|
];
|
|
|
|
|
|
|
|
@interner::StrInterner::prefill(init_vec)
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
|
|
|
|
// If modifying the numbers below, remember to modify these as appropriate
|
|
|
|
static SELF_KEYWORD_NAME: Name = 8;
|
|
|
|
static STATIC_KEYWORD_NAME: Name = 27;
|
|
|
|
static STRICT_KEYWORD_START: Name = 32;
|
|
|
|
static STRICT_KEYWORD_FINAL: Name = 65;
|
|
|
|
static RESERVED_KEYWORD_START: Name = 66;
|
|
|
|
static RESERVED_KEYWORD_FINAL: Name = 72;
|
2012-08-02 15:34:13 -07:00
|
|
|
|
2013-12-10 17:08:48 +11:00
|
|
|
declare_special_idents_and_keywords! {
|
|
|
|
pub mod special_idents {
|
|
|
|
// These ones are statics
|
|
|
|
(0, underscore, "_"); // apparently unused?
|
|
|
|
(1, anon, "anon");
|
|
|
|
(2, invalid, ""); // ''
|
|
|
|
(3, unary, "unary"); // apparently unused?
|
|
|
|
(4, not_fn, "!"); // apparently unused?
|
|
|
|
(5, idx_fn, "[]"); // apparently unused?
|
|
|
|
(6, unary_minus_fn, "unary-"); // apparently unused?
|
|
|
|
(7, clownshoes_extensions, "__extensions__");
|
|
|
|
|
|
|
|
(super::SELF_KEYWORD_NAME, self_, "self"); // 'self'
|
|
|
|
|
|
|
|
/* for matcher NTs */
|
|
|
|
// none of these appear to be used, but perhaps references to
|
|
|
|
// these are artificially fabricated by the macro system....
|
|
|
|
(9, item, "item");
|
|
|
|
(10, block, "block");
|
|
|
|
(11, stmt, "stmt");
|
|
|
|
(12, pat, "pat");
|
|
|
|
(13, expr, "expr");
|
|
|
|
(14, ty, "ty");
|
|
|
|
(15, ident, "ident");
|
|
|
|
(16, path, "path");
|
|
|
|
(17, tt, "tt");
|
|
|
|
(18, matchers, "matchers");
|
|
|
|
|
|
|
|
// for the type // apparently unused?
|
|
|
|
(19, str, "str");
|
|
|
|
|
|
|
|
/* outside of libsyntax */
|
|
|
|
(20, arg, "arg");
|
|
|
|
(21, descrim, "descrim");
|
|
|
|
(22, clownshoe_abi, "__rust_abi");
|
|
|
|
(23, clownshoe_stack_shim, "__rust_stack_shim");
|
|
|
|
(24, main, "main");
|
|
|
|
(25, opaque, "<opaque>");
|
|
|
|
(26, blk, "blk");
|
|
|
|
(super::STATIC_KEYWORD_NAME, statik, "static");
|
|
|
|
(28, clownshoes_foreign_mod, "__foreign_mod__");
|
|
|
|
(29, unnamed_field, "<unnamed_field>");
|
|
|
|
(30, c_abi, "C"); // apparently unused?
|
|
|
|
(31, type_self, "Self"); // `Self`
|
|
|
|
|
|
|
|
// here are the ones that actually occur in the source. Maybe the rest
|
|
|
|
// should be removed?
|
|
|
|
/*
|
|
|
|
special_idents::anon
|
|
|
|
special_idents::arg
|
|
|
|
special_idents::blk
|
|
|
|
special_idents::clownshoe_abi
|
|
|
|
special_idents::clownshoe_stack_shim
|
|
|
|
special_idents::clownshoes_extensions
|
|
|
|
special_idents::clownshoes_foreign_mod
|
|
|
|
special_idents::descrim
|
|
|
|
special_idents::invalid
|
|
|
|
special_idents::main
|
|
|
|
special_idents::matchers
|
|
|
|
special_idents::opaque
|
|
|
|
special_idents::self_
|
|
|
|
special_idents::statik
|
|
|
|
special_idents::tt
|
|
|
|
special_idents::type_self
|
|
|
|
special_idents::unnamed_field
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
pub mod keywords {
|
|
|
|
// These ones are variants of the Keyword enum
|
|
|
|
(32, As, "as");
|
|
|
|
(33, Break, "break");
|
|
|
|
(34, Const, "const");
|
|
|
|
(35, Do, "do");
|
|
|
|
(36, Else, "else");
|
|
|
|
(37, Enum, "enum");
|
|
|
|
(38, Extern, "extern");
|
|
|
|
(39, False, "false");
|
|
|
|
(40, Fn, "fn");
|
|
|
|
(41, For, "for");
|
|
|
|
(42, If, "if");
|
|
|
|
(43, Impl, "impl");
|
|
|
|
(44, In, "in");
|
|
|
|
(45, Let, "let");
|
|
|
|
(46, __LogLevel, "__log_level");
|
|
|
|
(47, Loop, "loop");
|
|
|
|
(48, Match, "match");
|
|
|
|
(49, Mod, "mod");
|
|
|
|
(50, Mut, "mut");
|
|
|
|
(51, Once, "once");
|
|
|
|
(52, Priv, "priv");
|
|
|
|
(53, Pub, "pub");
|
|
|
|
(54, Ref, "ref");
|
|
|
|
(55, Return, "return");
|
|
|
|
// Static and Self are also special idents (prefill de-dupes)
|
|
|
|
(super::STATIC_KEYWORD_NAME, Static, "static");
|
|
|
|
(super::SELF_KEYWORD_NAME, Self, "self");
|
|
|
|
(56, Struct, "struct");
|
|
|
|
(57, Super, "super");
|
|
|
|
(58, True, "true");
|
|
|
|
(59, Trait, "trait");
|
|
|
|
(60, Type, "type");
|
|
|
|
(61, Unsafe, "unsafe");
|
|
|
|
(62, Use, "use");
|
|
|
|
(63, While, "while");
|
|
|
|
(64, Continue, "continue");
|
|
|
|
(65, Proc, "proc");
|
|
|
|
|
|
|
|
(66, Alignof, "alignof");
|
|
|
|
(67, Be, "be");
|
|
|
|
(68, Offsetof, "offsetof");
|
|
|
|
(69, Pure, "pure");
|
|
|
|
(70, Sizeof, "sizeof");
|
|
|
|
(71, Typeof, "typeof");
|
|
|
|
(72, Yield, "yield");
|
|
|
|
}
|
|
|
|
}
|
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...
|
|
|
|
pub type ident_interner = 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.
|
2013-05-07 12:34:52 -07:00
|
|
|
pub fn get_ident_interner() -> @ident_interner {
|
2013-09-16 23:34:40 -07:00
|
|
|
local_data_key!(key: @@::parse::token::ident_interner)
|
2013-09-20 02:08:47 -04:00
|
|
|
match local_data::get(key, |k| k.map(|k| *k)) {
|
2013-07-12 01:38:44 -07:00
|
|
|
Some(interner) => *interner,
|
|
|
|
None => {
|
|
|
|
let interner = mk_fresh_ident_interner();
|
|
|
|
local_data::set(key, @interner);
|
|
|
|
interner
|
2012-10-08 10:41:02 -07:00
|
|
|
}
|
|
|
|
}
|
2012-08-02 14:33:26 -07:00
|
|
|
}
|
|
|
|
|
2012-07-18 16:18:02 -07:00
|
|
|
/* for when we don't care about the contents; doesn't interact with TLD or
|
|
|
|
serialization */
|
2013-01-29 13:54:06 -08:00
|
|
|
pub fn mk_fake_ident_interner() -> @ident_interner {
|
2013-06-26 10:11:19 -07:00
|
|
|
@interner::StrInterner::new()
|
2012-07-18 16:18:02 -07:00
|
|
|
}
|
|
|
|
|
2013-05-07 12:34:52 -07:00
|
|
|
// maps a string to its interned representation
|
2013-06-04 12:34:25 -07:00
|
|
|
pub fn intern(str : &str) -> Name {
|
|
|
|
let interner = get_ident_interner();
|
|
|
|
interner.intern(str)
|
|
|
|
}
|
|
|
|
|
|
|
|
// gensyms a new uint, using the current interner
|
|
|
|
pub fn gensym(str : &str) -> Name {
|
2013-05-07 12:34:52 -07:00
|
|
|
let interner = get_ident_interner();
|
2013-06-04 12:34:25 -07:00
|
|
|
interner.gensym(str)
|
|
|
|
}
|
|
|
|
|
|
|
|
// map an interned representation back to a string
|
2013-06-13 03:02:55 +10:00
|
|
|
pub fn interner_get(name : Name) -> @str {
|
2013-06-04 12:34:25 -07:00
|
|
|
get_ident_interner().get(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// maps an identifier to the string that it corresponds to
|
2013-09-02 02:50:59 +02:00
|
|
|
pub fn ident_to_str(id : &ast::Ident) -> @str {
|
2013-06-04 12:34:25 -07:00
|
|
|
interner_get(id.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// maps a string to an identifier with an empty syntax context
|
2013-09-02 02:50:59 +02:00
|
|
|
pub fn str_to_ident(str : &str) -> ast::Ident {
|
|
|
|
ast::Ident::new(intern(str))
|
2013-06-04 12:34:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// maps a string to a gensym'ed identifier
|
2013-09-02 02:50:59 +02:00
|
|
|
pub fn gensym_ident(str : &str) -> ast::Ident {
|
|
|
|
ast::Ident::new(gensym(str))
|
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.
|
2013-06-26 10:11:19 -07:00
|
|
|
// note that this guarantees that str_ptr_eq(ident_to_str(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.
|
2013-06-07 10:41:18 -07:00
|
|
|
pub fn fresh_name(src : &ast::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.
|
2013-06-07 14:53:53 -07:00
|
|
|
/*let num = rand::rng().gen_uint_range(0,0xffff);
|
2013-09-27 21:01:58 -07:00
|
|
|
gensym(format!("{}_{}",ident_to_str(src),num))*/
|
2013-05-14 11:34:17 -07:00
|
|
|
}
|
|
|
|
|
2013-06-25 16:48:03 -07:00
|
|
|
// it looks like there oughta be a str_ptr_eq fn, but no one bothered to implement it?
|
|
|
|
|
2013-06-26 10:11:19 -07:00
|
|
|
// determine whether two @str values are pointer-equal
|
|
|
|
pub fn str_ptr_eq(a : @str, b : @str) -> bool {
|
|
|
|
unsafe {
|
|
|
|
let p : uint = cast::transmute(a);
|
|
|
|
let q : uint = cast::transmute(b);
|
|
|
|
let result = p == q;
|
|
|
|
// got to transmute them back, to make sure the ref count is correct:
|
2013-07-11 23:07:34 -07:00
|
|
|
let _junk1 : @str = cast::transmute(p);
|
|
|
|
let _junk2 : @str = cast::transmute(q);
|
2013-06-26 10:11:19 -07:00
|
|
|
result
|
|
|
|
}
|
|
|
|
}
|
2013-06-25 16:48:03 -07:00
|
|
|
|
|
|
|
// return true when two identifiers refer (through the intern table) to the same ptr_eq
|
|
|
|
// string. This is used to compare identifiers in places where hygienic comparison is
|
|
|
|
// not wanted (i.e. not lexical vars).
|
2013-06-26 10:11:19 -07:00
|
|
|
pub fn ident_spelling_eq(a : &ast::Ident, b : &ast::Ident) -> bool {
|
|
|
|
str_ptr_eq(interner_get(a.name),interner_get(b.name))
|
|
|
|
}
|
2013-06-25 16:48:03 -07:00
|
|
|
|
2013-06-25 11:43:52 -07:00
|
|
|
// create a fresh mark.
|
|
|
|
pub fn fresh_mark() -> Mrk {
|
|
|
|
gensym("mark")
|
|
|
|
}
|
|
|
|
|
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 {
|
2013-05-17 10:18:09 -07:00
|
|
|
token::IDENT(sid, false) => { kw.to_ident().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 {
|
|
|
|
token::IDENT(sid, false) => match sid.name {
|
2013-10-08 12:45:41 +02:00
|
|
|
SELF_KEYWORD_NAME | STATIC_KEYWORD_NAME |
|
|
|
|
STRICT_KEYWORD_START .. RESERVED_KEYWORD_FINAL => true,
|
2013-06-19 21:12:40 -04:00
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_strict_keyword(tok: &Token) -> bool {
|
|
|
|
match *tok {
|
|
|
|
token::IDENT(sid, false) => match sid.name {
|
2013-10-08 12:45:41 +02:00
|
|
|
SELF_KEYWORD_NAME | STATIC_KEYWORD_NAME |
|
|
|
|
STRICT_KEYWORD_START .. STRICT_KEYWORD_FINAL => true,
|
2013-06-19 21:12:40 -04:00
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_reserved_keyword(tok: &Token) -> bool {
|
|
|
|
match *tok {
|
|
|
|
token::IDENT(sid, false) => match sid.name {
|
2013-10-08 12:45:41 +02:00
|
|
|
RESERVED_KEYWORD_START .. RESERVED_KEYWORD_FINAL => true,
|
2013-06-19 21:12:40 -04:00
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-05 14:15:00 -07:00
|
|
|
pub fn mtwt_token_eq(t1 : &Token, t2 : &Token) -> bool {
|
|
|
|
match (t1,t2) {
|
|
|
|
(&IDENT(id1,_),&IDENT(id2,_)) =>
|
|
|
|
ast_util::mtwt_resolve(id1) == ast_util::mtwt_resolve(id2),
|
|
|
|
_ => *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;
|
|
|
|
use ast_util;
|
|
|
|
|
2013-07-10 16:40:09 -07:00
|
|
|
fn mark_ident(id : ast::Ident, m : ast::Mrk) -> ast::Ident {
|
|
|
|
ast::Ident{name:id.name,ctxt:ast_util::new_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-06-25 16:48:03 -07:00
|
|
|
|
2013-06-26 10:11:19 -07:00
|
|
|
#[test] fn str_ptr_eq_tests(){
|
|
|
|
let a = @"abc";
|
|
|
|
let b = @"abc";
|
|
|
|
let c = a;
|
|
|
|
assert!(str_ptr_eq(a,c));
|
|
|
|
assert!(!str_ptr_eq(a,b));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test] fn fresh_name_pointer_sharing() {
|
2013-06-25 16:48:03 -07:00
|
|
|
let ghi = str_to_ident("ghi");
|
|
|
|
assert_eq!(ident_to_str(&ghi),@"ghi");
|
2013-06-26 10:11:19 -07:00
|
|
|
assert!(str_ptr_eq(ident_to_str(&ghi),ident_to_str(&ghi)))
|
2013-06-25 16:48:03 -07:00
|
|
|
let fresh = ast::Ident::new(fresh_name(&ghi));
|
|
|
|
assert_eq!(ident_to_str(&fresh),@"ghi");
|
|
|
|
assert!(str_ptr_eq(ident_to_str(&ghi),ident_to_str(&fresh)));
|
|
|
|
}
|
2013-06-25 11:40:51 -07:00
|
|
|
|
2013-05-14 11:34:17 -07:00
|
|
|
}
|