2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// 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 16:41:37 -06:00
|
|
|
use ast;
|
|
|
|
use ast_util;
|
|
|
|
use parse::token;
|
2013-05-02 03:16:07 -05:00
|
|
|
use util::interner::StrInterner;
|
2012-12-23 16:41:37 -06:00
|
|
|
use util::interner;
|
|
|
|
|
2013-04-02 18:20:02 -05:00
|
|
|
use core::cmp::Equiv;
|
2013-04-03 08:28:36 -05:00
|
|
|
use core::hashmap::HashSet;
|
2013-04-02 18:20:02 -05:00
|
|
|
use core::to_bytes;
|
2010-08-18 13:35:12 -05:00
|
|
|
|
2013-05-15 17:55:57 -05:00
|
|
|
#[deriving(Encodable, Decodable, Eq)]
|
2013-01-29 15:54:06 -06:00
|
|
|
pub enum binop {
|
2012-01-19 19:56:05 -06:00
|
|
|
PLUS,
|
|
|
|
MINUS,
|
|
|
|
STAR,
|
|
|
|
SLASH,
|
|
|
|
PERCENT,
|
|
|
|
CARET,
|
|
|
|
AND,
|
|
|
|
OR,
|
2012-05-22 16:59:15 -05:00
|
|
|
SHL,
|
|
|
|
SHR,
|
2010-09-09 17:59:29 -05:00
|
|
|
}
|
|
|
|
|
2013-05-15 17:55:57 -05:00
|
|
|
#[deriving(Encodable, Decodable, Eq)]
|
2013-01-29 15:54:06 -06:00
|
|
|
pub enum Token {
|
2010-09-09 17:59:29 -05:00
|
|
|
/* Expression-operator symbols. */
|
2012-01-19 19:56:05 -06:00
|
|
|
EQ,
|
|
|
|
LT,
|
|
|
|
LE,
|
|
|
|
EQEQ,
|
|
|
|
NE,
|
|
|
|
GE,
|
|
|
|
GT,
|
|
|
|
ANDAND,
|
|
|
|
OROR,
|
|
|
|
NOT,
|
|
|
|
TILDE,
|
|
|
|
BINOP(binop),
|
|
|
|
BINOPEQ(binop),
|
2010-09-09 17:59:29 -05:00
|
|
|
|
|
|
|
/* Structural symbols */
|
2012-01-19 19:56:05 -06:00
|
|
|
AT,
|
|
|
|
DOT,
|
2012-08-03 20:01:30 -05:00
|
|
|
DOTDOT,
|
2012-01-19 19:56:05 -06:00
|
|
|
COMMA,
|
|
|
|
SEMI,
|
|
|
|
COLON,
|
|
|
|
MOD_SEP,
|
|
|
|
RARROW,
|
|
|
|
LARROW,
|
|
|
|
DARROW,
|
2012-06-04 20:34:10 -05:00
|
|
|
FAT_ARROW,
|
2012-01-19 19:56:05 -06:00
|
|
|
LPAREN,
|
|
|
|
RPAREN,
|
|
|
|
LBRACKET,
|
|
|
|
RBRACKET,
|
|
|
|
LBRACE,
|
|
|
|
RBRACE,
|
|
|
|
POUND,
|
2012-04-22 18:58:04 -05:00
|
|
|
DOLLAR,
|
2012-01-25 17:38:09 -06:00
|
|
|
|
2010-09-09 17:59:29 -05:00
|
|
|
/* Literals */
|
2012-01-19 19:56:05 -06:00
|
|
|
LIT_INT(i64, ast::int_ty),
|
|
|
|
LIT_UINT(u64, ast::uint_ty),
|
2012-06-14 21:41:40 -05:00
|
|
|
LIT_INT_UNSUFFIXED(i64),
|
2012-09-19 20:50:24 -05:00
|
|
|
LIT_FLOAT(ast::ident, ast::float_ty),
|
2012-11-07 20:40:34 -06:00
|
|
|
LIT_FLOAT_UNSUFFIXED(ast::ident),
|
2012-09-19 20:50:24 -05:00
|
|
|
LIT_STR(ast::ident),
|
2010-09-09 17:59:29 -05:00
|
|
|
|
|
|
|
/* Name components */
|
2013-02-26 12:15:29 -06:00
|
|
|
// an identifier contains an "is_mod_name" boolean,
|
|
|
|
// indicating whether :: follows this token with no
|
|
|
|
// whitespace in between.
|
2012-09-19 20:50:24 -05:00
|
|
|
IDENT(ast::ident, bool),
|
2012-01-19 19:56:05 -06:00
|
|
|
UNDERSCORE,
|
2013-02-08 08:02:35 -06:00
|
|
|
LIFETIME(ast::ident),
|
2012-06-12 12:50:17 -05:00
|
|
|
|
2012-06-29 20:26:34 -05:00
|
|
|
/* For interpolation */
|
2012-07-27 21:14:46 -05:00
|
|
|
INTERPOLATED(nonterminal),
|
2012-06-12 12:50:17 -05:00
|
|
|
|
2012-09-19 20:50:24 -05:00
|
|
|
DOC_COMMENT(ast::ident),
|
2012-01-19 19:56:05 -06:00
|
|
|
EOF,
|
2010-09-09 17:59:29 -05:00
|
|
|
}
|
2010-08-18 13:35:12 -05:00
|
|
|
|
2013-05-15 17:55:57 -05:00
|
|
|
#[deriving(Encodable, Decodable, Eq)]
|
2012-07-04 16:53:12 -05:00
|
|
|
/// For interpolation during macro expansion.
|
2013-01-29 15:54:06 -06:00
|
|
|
pub enum nonterminal {
|
2012-07-27 21:14:46 -05:00
|
|
|
nt_item(@ast::item),
|
|
|
|
nt_block(ast::blk),
|
|
|
|
nt_stmt(@ast::stmt),
|
|
|
|
nt_pat( @ast::pat),
|
|
|
|
nt_expr(@ast::expr),
|
2012-10-15 16:56:42 -05:00
|
|
|
nt_ty( @ast::Ty),
|
2012-09-19 20:50:24 -05:00
|
|
|
nt_ident(ast::ident, bool),
|
2013-03-26 19:00:35 -05:00
|
|
|
nt_path(@ast::Path),
|
2012-07-27 21:14:46 -05:00
|
|
|
nt_tt( @ast::token_tree), //needs @ed to break a circularity
|
|
|
|
nt_matchers(~[ast::matcher])
|
2012-06-12 12:50:17 -05:00
|
|
|
}
|
|
|
|
|
2013-03-22 13:09:13 -05:00
|
|
|
pub fn binop_to_str(o: binop) -> ~str {
|
2012-08-06 14:34:08 -05:00
|
|
|
match o {
|
2012-08-03 21:59:04 -05:00
|
|
|
PLUS => ~"+",
|
|
|
|
MINUS => ~"-",
|
|
|
|
STAR => ~"*",
|
|
|
|
SLASH => ~"/",
|
|
|
|
PERCENT => ~"%",
|
|
|
|
CARET => ~"^",
|
|
|
|
AND => ~"&",
|
|
|
|
OR => ~"|",
|
|
|
|
SHL => ~"<<",
|
|
|
|
SHR => ~">>"
|
2010-08-20 17:57:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-24 21:54:37 -06:00
|
|
|
pub fn to_str(in: @ident_interner, t: &Token) -> ~str {
|
|
|
|
match *t {
|
2012-08-03 21:59:04 -05:00
|
|
|
EQ => ~"=",
|
|
|
|
LT => ~"<",
|
|
|
|
LE => ~"<=",
|
|
|
|
EQEQ => ~"==",
|
|
|
|
NE => ~"!=",
|
|
|
|
GE => ~">=",
|
|
|
|
GT => ~">",
|
|
|
|
NOT => ~"!",
|
|
|
|
TILDE => ~"~",
|
|
|
|
OROR => ~"||",
|
|
|
|
ANDAND => ~"&&",
|
|
|
|
BINOP(op) => binop_to_str(op),
|
|
|
|
BINOPEQ(op) => binop_to_str(op) + ~"=",
|
2011-09-02 17:34:58 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
/* Structural symbols */
|
2012-08-03 21:59:04 -05:00
|
|
|
AT => ~"@",
|
|
|
|
DOT => ~".",
|
|
|
|
DOTDOT => ~"..",
|
|
|
|
COMMA => ~",",
|
|
|
|
SEMI => ~";",
|
|
|
|
COLON => ~":",
|
|
|
|
MOD_SEP => ~"::",
|
|
|
|
RARROW => ~"->",
|
|
|
|
LARROW => ~"<-",
|
|
|
|
DARROW => ~"<->",
|
|
|
|
FAT_ARROW => ~"=>",
|
|
|
|
LPAREN => ~"(",
|
|
|
|
RPAREN => ~")",
|
|
|
|
LBRACKET => ~"[",
|
|
|
|
RBRACKET => ~"]",
|
|
|
|
LBRACE => ~"{",
|
|
|
|
RBRACE => ~"}",
|
|
|
|
POUND => ~"#",
|
|
|
|
DOLLAR => ~"$",
|
2012-01-25 17:38:09 -06:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
/* Literals */
|
2012-08-03 21:59:04 -05:00
|
|
|
LIT_INT(c, ast::ty_char) => {
|
2012-07-14 00:57:48 -05:00
|
|
|
~"'" + char::escape_default(c as char) + ~"'"
|
2011-12-07 14:06:12 -06:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
LIT_INT(i, t) => {
|
2012-11-29 14:08:59 -06:00
|
|
|
i.to_str() + ast_util::int_ty_to_str(t)
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
LIT_UINT(u, t) => {
|
2012-11-29 14:08:59 -06:00
|
|
|
u.to_str() + ast_util::uint_ty_to_str(t)
|
2012-06-11 18:31:03 -05:00
|
|
|
}
|
2012-11-29 14:08:59 -06:00
|
|
|
LIT_INT_UNSUFFIXED(i) => { i.to_str() }
|
2012-08-03 21:59:04 -05:00
|
|
|
LIT_FLOAT(s, t) => {
|
2013-02-26 08:35:36 -06:00
|
|
|
let mut body = copy *in.get(s);
|
2013-05-19 00:07:44 -05:00
|
|
|
if body.ends_with(".") {
|
2012-08-03 13:22:35 -05:00
|
|
|
body = body + ~"0"; // `10.f` is not a float literal
|
2012-07-31 13:49:20 -05:00
|
|
|
}
|
|
|
|
body + ast_util::float_ty_to_str(t)
|
|
|
|
}
|
2012-11-07 20:40:34 -06:00
|
|
|
LIT_FLOAT_UNSUFFIXED(s) => {
|
2013-02-26 08:35:36 -06:00
|
|
|
let mut body = copy *in.get(s);
|
2013-05-19 00:07:44 -05:00
|
|
|
if body.ends_with(".") {
|
2012-11-07 20:40:34 -06:00
|
|
|
body = body + ~"0"; // `10.f` is not a float literal
|
|
|
|
}
|
|
|
|
body
|
|
|
|
}
|
2012-09-19 20:50:24 -05:00
|
|
|
LIT_STR(s) => { ~"\"" + str::escape_default(*in.get(s)) + ~"\"" }
|
2012-06-30 05:54:54 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
/* Name components */
|
2013-02-26 08:35:36 -06:00
|
|
|
IDENT(s, _) => copy *in.get(s),
|
2013-02-08 08:02:35 -06:00
|
|
|
LIFETIME(s) => fmt!("'%s", *in.get(s)),
|
2012-08-03 21:59:04 -05:00
|
|
|
UNDERSCORE => ~"_",
|
2012-06-30 05:54:54 -05:00
|
|
|
|
|
|
|
/* Other */
|
2013-02-26 08:35:36 -06:00
|
|
|
DOC_COMMENT(s) => copy *in.get(s),
|
2012-08-03 21:59:04 -05:00
|
|
|
EOF => ~"<eof>",
|
2012-12-04 12:50:00 -06:00
|
|
|
INTERPOLATED(ref nt) => {
|
2013-01-22 18:00:07 -06:00
|
|
|
match nt {
|
|
|
|
&nt_expr(e) => ::print::pprust::expr_to_str(e, in),
|
|
|
|
_ => {
|
|
|
|
~"an interpolated " +
|
|
|
|
match (*nt) {
|
|
|
|
nt_item(*) => ~"item",
|
|
|
|
nt_block(*) => ~"block",
|
|
|
|
nt_stmt(*) => ~"statement",
|
|
|
|
nt_pat(*) => ~"pattern",
|
2013-05-05 17:18:51 -05:00
|
|
|
nt_expr(*) => fail!("should have been handled above"),
|
2013-01-22 18:00:07 -06:00
|
|
|
nt_ty(*) => ~"type",
|
|
|
|
nt_ident(*) => ~"identifier",
|
|
|
|
nt_path(*) => ~"path",
|
|
|
|
nt_tt(*) => ~"tt",
|
|
|
|
nt_matchers(*) => ~"matcher sequence"
|
|
|
|
}
|
2012-07-06 16:48:01 -05:00
|
|
|
}
|
2013-01-22 18:00:07 -06:00
|
|
|
}
|
2012-06-29 20:26:34 -05:00
|
|
|
}
|
2010-08-20 13:41:34 -05:00
|
|
|
}
|
|
|
|
}
|
2011-07-03 13:48:14 -05:00
|
|
|
|
2013-03-22 13:09:13 -05:00
|
|
|
pub fn can_begin_expr(t: &Token) -> bool {
|
2013-02-24 19:24:28 -06:00
|
|
|
match *t {
|
2012-08-03 21:59:04 -05:00
|
|
|
LPAREN => true,
|
|
|
|
LBRACE => true,
|
|
|
|
LBRACKET => true,
|
|
|
|
IDENT(_, _) => true,
|
|
|
|
UNDERSCORE => true,
|
|
|
|
TILDE => true,
|
|
|
|
LIT_INT(_, _) => true,
|
|
|
|
LIT_UINT(_, _) => true,
|
|
|
|
LIT_INT_UNSUFFIXED(_) => true,
|
|
|
|
LIT_FLOAT(_, _) => true,
|
2012-11-07 20:40:34 -06:00
|
|
|
LIT_FLOAT_UNSUFFIXED(_) => true,
|
2012-08-03 21:59:04 -05:00
|
|
|
LIT_STR(_) => true,
|
|
|
|
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,
|
2012-07-27 21:14:46 -05:00
|
|
|
INTERPOLATED(nt_expr(*))
|
|
|
|
| INTERPOLATED(nt_ident(*))
|
|
|
|
| INTERPOLATED(nt_block(*))
|
2012-08-03 21:59:04 -05:00
|
|
|
| INTERPOLATED(nt_path(*)) => true,
|
|
|
|
_ => false
|
2011-07-03 13:48:14 -05:00
|
|
|
}
|
|
|
|
}
|
2011-10-07 09:22:53 -05:00
|
|
|
|
2012-07-31 15:53:00 -05:00
|
|
|
/// what's the opposite delimiter?
|
2013-02-24 12:00:25 -06:00
|
|
|
pub fn flip_delimiter(t: &token::Token) -> token::Token {
|
|
|
|
match *t {
|
2013-02-24 19:24:28 -06:00
|
|
|
LPAREN => RPAREN,
|
|
|
|
LBRACE => RBRACE,
|
|
|
|
LBRACKET => RBRACKET,
|
|
|
|
RPAREN => LPAREN,
|
|
|
|
RBRACE => LBRACE,
|
|
|
|
RBRACKET => LBRACKET,
|
2013-02-11 21:26:38 -06:00
|
|
|
_ => fail!()
|
2012-07-31 15:53:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-24 19:24:28 -06:00
|
|
|
pub fn is_lit(t: &Token) -> bool {
|
|
|
|
match *t {
|
2012-08-03 21:59:04 -05:00
|
|
|
LIT_INT(_, _) => true,
|
|
|
|
LIT_UINT(_, _) => true,
|
|
|
|
LIT_INT_UNSUFFIXED(_) => true,
|
|
|
|
LIT_FLOAT(_, _) => true,
|
2012-11-07 20:40:34 -06:00
|
|
|
LIT_FLOAT_UNSUFFIXED(_) => true,
|
2012-08-03 21:59:04 -05:00
|
|
|
LIT_STR(_) => true,
|
|
|
|
_ => false
|
2012-06-11 18:49:35 -05:00
|
|
|
}
|
2012-04-22 16:59:04 -05:00
|
|
|
}
|
|
|
|
|
2013-03-22 13:09:13 -05:00
|
|
|
pub fn is_ident(t: &Token) -> bool {
|
2013-02-24 19:24:28 -06:00
|
|
|
match *t { IDENT(_, _) => true, _ => false }
|
2012-04-17 23:14:40 -05:00
|
|
|
}
|
|
|
|
|
2013-03-22 13:09:13 -05:00
|
|
|
pub fn is_ident_or_path(t: &Token) -> bool {
|
2013-02-24 19:24:28 -06:00
|
|
|
match *t {
|
2012-08-06 15:09:10 -05:00
|
|
|
IDENT(_, _) | INTERPOLATED(nt_path(*)) => true,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-22 13:09:13 -05:00
|
|
|
pub fn is_plain_ident(t: &Token) -> bool {
|
2013-02-24 19:24:28 -06:00
|
|
|
match *t { IDENT(_, false) => true, _ => false }
|
2012-04-17 23:14:40 -05:00
|
|
|
}
|
|
|
|
|
2013-03-22 13:09:13 -05:00
|
|
|
pub fn is_bar(t: &Token) -> bool {
|
2013-02-24 19:24:28 -06:00
|
|
|
match *t { BINOP(OR) | OROR => true, _ => false }
|
2012-04-17 23:14:40 -05:00
|
|
|
}
|
|
|
|
|
2012-08-02 16:33:26 -05:00
|
|
|
|
2013-01-29 15:54:06 -06:00
|
|
|
pub mod special_idents {
|
2012-09-07 20:08:21 -05:00
|
|
|
use ast::ident;
|
2013-01-29 15:54:06 -06:00
|
|
|
|
2013-05-01 00:42:36 -05:00
|
|
|
pub static underscore : ident = ident { repr: 0, ctxt: 0};
|
|
|
|
pub static anon : ident = ident { repr: 1, ctxt: 0};
|
|
|
|
pub static invalid : ident = ident { repr: 2, ctxt: 0}; // ''
|
|
|
|
pub static unary : ident = ident { repr: 3, ctxt: 0};
|
|
|
|
pub static not_fn : ident = ident { repr: 4, ctxt: 0};
|
|
|
|
pub static idx_fn : ident = ident { repr: 5, ctxt: 0};
|
|
|
|
pub static unary_minus_fn : ident = ident { repr: 6, ctxt: 0};
|
|
|
|
pub static clownshoes_extensions : ident = ident { repr: 7, ctxt: 0};
|
|
|
|
|
|
|
|
pub static self_ : ident = ident { repr: 8, ctxt: 0}; // 'self'
|
2012-07-18 18:18:02 -05:00
|
|
|
|
|
|
|
/* for matcher NTs */
|
2013-05-01 00:42:36 -05:00
|
|
|
pub static item : ident = ident { repr: 9, ctxt: 0};
|
|
|
|
pub static block : ident = ident { repr: 10, ctxt: 0};
|
|
|
|
pub static stmt : ident = ident { repr: 11, ctxt: 0};
|
|
|
|
pub static pat : ident = ident { repr: 12, ctxt: 0};
|
|
|
|
pub static expr : ident = ident { repr: 13, ctxt: 0};
|
|
|
|
pub static ty : ident = ident { repr: 14, ctxt: 0};
|
|
|
|
pub static ident : ident = ident { repr: 15, ctxt: 0};
|
|
|
|
pub static path : ident = ident { repr: 16, ctxt: 0};
|
|
|
|
pub static tt : ident = ident { repr: 17, ctxt: 0};
|
|
|
|
pub static matchers : ident = ident { repr: 18, ctxt: 0};
|
|
|
|
|
|
|
|
pub static str : ident = ident { repr: 19, ctxt: 0}; // for the type
|
2012-07-18 18:18:02 -05:00
|
|
|
|
|
|
|
/* outside of libsyntax */
|
2013-05-01 00:42:36 -05:00
|
|
|
pub static ty_visitor : ident = ident { repr: 20, ctxt: 0};
|
|
|
|
pub static arg : ident = ident { repr: 21, ctxt: 0};
|
|
|
|
pub static descrim : ident = ident { repr: 22, ctxt: 0};
|
|
|
|
pub static clownshoe_abi : ident = ident { repr: 23, ctxt: 0};
|
|
|
|
pub static clownshoe_stack_shim : ident = ident { repr: 24, ctxt: 0};
|
|
|
|
pub static tydesc : ident = ident { repr: 25, ctxt: 0};
|
|
|
|
pub static main : ident = ident { repr: 26, ctxt: 0};
|
|
|
|
pub static opaque : ident = ident { repr: 27, ctxt: 0};
|
|
|
|
pub static blk : ident = ident { repr: 28, ctxt: 0};
|
2013-05-10 17:15:06 -05:00
|
|
|
pub static statik : ident = ident { repr: 29, ctxt: 0};
|
2013-05-01 00:42:36 -05:00
|
|
|
pub static intrinsic : ident = ident { repr: 30, ctxt: 0};
|
|
|
|
pub static clownshoes_foreign_mod: ident = ident { repr: 31, ctxt: 0};
|
|
|
|
pub static unnamed_field: ident = ident { repr: 32, ctxt: 0};
|
|
|
|
pub static c_abi: ident = ident { repr: 33, ctxt: 0};
|
|
|
|
pub static type_self: ident = ident { repr: 34, ctxt: 0}; // `Self`
|
2012-08-02 17:34:13 -05:00
|
|
|
}
|
|
|
|
|
2013-04-02 18:20:02 -05:00
|
|
|
pub struct StringRef<'self>(&'self str);
|
|
|
|
|
|
|
|
impl<'self> Equiv<@~str> for StringRef<'self> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn equiv(&self, other: &@~str) -> bool { str::eq_slice(**self, **other) }
|
|
|
|
}
|
|
|
|
|
2013-05-02 17:33:33 -05:00
|
|
|
impl<'self> to_bytes::IterBytes for StringRef<'self> {
|
2013-05-14 13:32:49 -05:00
|
|
|
#[inline(always)]
|
2013-05-02 17:33:33 -05:00
|
|
|
fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) -> bool {
|
|
|
|
(**self).iter_bytes(lsb0, f)
|
|
|
|
}
|
|
|
|
}
|
2013-04-02 18:20:02 -05:00
|
|
|
|
2013-03-29 12:04:48 -05:00
|
|
|
/**
|
|
|
|
* Maps a token to a record specifying the corresponding binary
|
|
|
|
* operator
|
|
|
|
*/
|
|
|
|
pub fn token_to_binop(tok: Token) -> Option<ast::binop> {
|
|
|
|
match tok {
|
|
|
|
BINOP(STAR) => Some(ast::mul),
|
2013-05-01 00:40:05 -05:00
|
|
|
BINOP(SLASH) => Some(ast::div),
|
2013-03-29 12:04:48 -05:00
|
|
|
BINOP(PERCENT) => Some(ast::rem),
|
|
|
|
BINOP(PLUS) => Some(ast::add),
|
|
|
|
BINOP(MINUS) => Some(ast::subtract),
|
|
|
|
BINOP(SHL) => Some(ast::shl),
|
|
|
|
BINOP(SHR) => Some(ast::shr),
|
|
|
|
BINOP(AND) => Some(ast::bitand),
|
|
|
|
BINOP(CARET) => Some(ast::bitxor),
|
|
|
|
BINOP(OR) => Some(ast::bitor),
|
|
|
|
LT => Some(ast::lt),
|
|
|
|
LE => Some(ast::le),
|
|
|
|
GE => Some(ast::ge),
|
|
|
|
GT => Some(ast::gt),
|
|
|
|
EQEQ => Some(ast::eq),
|
|
|
|
NE => Some(ast::ne),
|
|
|
|
ANDAND => Some(ast::and),
|
|
|
|
OROR => Some(ast::or),
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-29 15:54:06 -06:00
|
|
|
pub struct ident_interner {
|
2013-05-02 03:16:07 -05:00
|
|
|
priv interner: StrInterner,
|
2012-09-19 20:50:24 -05:00
|
|
|
}
|
|
|
|
|
2013-01-29 15:54:06 -06:00
|
|
|
pub impl ident_interner {
|
2013-05-02 03:16:07 -05:00
|
|
|
fn intern(&self, val: &str) -> ast::ident {
|
2013-04-19 17:57:31 -05:00
|
|
|
ast::ident { repr: self.interner.intern(val), ctxt: 0 }
|
2012-09-19 20:50:24 -05:00
|
|
|
}
|
2013-05-02 03:16:07 -05:00
|
|
|
fn gensym(&self, val: &str) -> ast::ident {
|
2013-04-19 17:57:31 -05:00
|
|
|
ast::ident { repr: self.interner.gensym(val), ctxt: 0 }
|
2012-09-19 20:50:24 -05:00
|
|
|
}
|
2013-03-22 13:09:13 -05:00
|
|
|
fn get(&self, idx: ast::ident) -> @~str {
|
2012-09-19 20:50:24 -05:00
|
|
|
self.interner.get(idx.repr)
|
|
|
|
}
|
2013-03-02 15:02:27 -06:00
|
|
|
fn len(&self) -> uint {
|
2012-09-19 20:50:24 -05:00
|
|
|
self.interner.len()
|
|
|
|
}
|
2013-04-02 18:20:02 -05:00
|
|
|
fn find_equiv<Q:Hash + IterBytes + Equiv<@~str>>(&self, val: &Q)
|
2013-04-19 17:57:31 -05:00
|
|
|
-> Option<ast::ident> {
|
2013-04-02 18:20:02 -05:00
|
|
|
match self.interner.find_equiv(val) {
|
2013-04-19 17:57:31 -05:00
|
|
|
Some(v) => Some(ast::ident { repr: v, ctxt: 0 }),
|
2013-04-02 18:20:02 -05:00
|
|
|
None => None,
|
|
|
|
}
|
|
|
|
}
|
2012-09-19 20:50:24 -05:00
|
|
|
}
|
2012-07-18 18:18:02 -05:00
|
|
|
|
2013-04-23 12:57:41 -05:00
|
|
|
// return a fresh interner, preloaded with special identifiers.
|
2013-05-07 14:34:52 -05:00
|
|
|
fn mk_fresh_ident_interner() -> @ident_interner {
|
2013-04-23 12:57:41 -05:00
|
|
|
// the indices here must correspond to the numbers in
|
|
|
|
// special_idents.
|
|
|
|
let init_vec = ~[
|
2013-05-02 03:16:07 -05:00
|
|
|
"_", // 0
|
|
|
|
"anon", // 1
|
|
|
|
"", // 2
|
|
|
|
"unary", // 3
|
|
|
|
"!", // 4
|
|
|
|
"[]", // 5
|
|
|
|
"unary-", // 6
|
|
|
|
"__extensions__", // 7
|
|
|
|
"self", // 8
|
|
|
|
"item", // 9
|
|
|
|
"block", // 10
|
|
|
|
"stmt", // 11
|
|
|
|
"pat", // 12
|
|
|
|
"expr", // 13
|
|
|
|
"ty", // 14
|
|
|
|
"ident", // 15
|
|
|
|
"path", // 16
|
|
|
|
"tt", // 17
|
|
|
|
"matchers", // 18
|
|
|
|
"str", // 19
|
|
|
|
"TyVisitor", // 20
|
|
|
|
"arg", // 21
|
|
|
|
"descrim", // 22
|
|
|
|
"__rust_abi", // 23
|
|
|
|
"__rust_stack_shim", // 24
|
|
|
|
"TyDesc", // 25
|
|
|
|
"main", // 26
|
|
|
|
"<opaque>", // 27
|
|
|
|
"blk", // 28
|
|
|
|
"static", // 29
|
|
|
|
"intrinsic", // 30
|
|
|
|
"__foreign_mod__", // 31
|
|
|
|
"__field__", // 32
|
|
|
|
"C", // 33
|
|
|
|
"Self", // 34
|
2013-04-23 12:57:41 -05:00
|
|
|
];
|
|
|
|
|
2013-05-07 14:34:52 -05:00
|
|
|
@ident_interner {
|
2013-05-02 03:16:07 -05:00
|
|
|
interner: interner::StrInterner::prefill(init_vec)
|
2013-04-23 12:57:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if an interner exists in TLS, return it. Otherwise, prepare a
|
|
|
|
// fresh one.
|
2013-05-07 14:34:52 -05:00
|
|
|
pub fn get_ident_interner() -> @ident_interner {
|
2012-09-19 19:29:54 -05:00
|
|
|
unsafe {
|
2013-05-07 14:34:52 -05:00
|
|
|
let key =
|
|
|
|
(cast::transmute::<(uint, uint),
|
|
|
|
&fn(v: @@::parse::token::ident_interner)>(
|
|
|
|
(-3 as uint, 0u)));
|
|
|
|
match local_data::local_data_get(key) {
|
2012-10-08 12:41:02 -05:00
|
|
|
Some(interner) => *interner,
|
|
|
|
None => {
|
2013-05-07 14:34:52 -05:00
|
|
|
let interner = mk_fresh_ident_interner();
|
|
|
|
unsafe {
|
|
|
|
local_data::local_data_set(key, @interner);
|
|
|
|
}
|
|
|
|
interner
|
2012-10-08 12:41:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-02 16:33:26 -05:00
|
|
|
}
|
|
|
|
|
2012-07-18 18:18:02 -05:00
|
|
|
/* for when we don't care about the contents; doesn't interact with TLD or
|
|
|
|
serialization */
|
2013-01-29 15:54:06 -06:00
|
|
|
pub fn mk_fake_ident_interner() -> @ident_interner {
|
2013-05-02 03:16:07 -05:00
|
|
|
@ident_interner { interner: interner::StrInterner::new() }
|
2012-07-18 18:18:02 -05:00
|
|
|
}
|
|
|
|
|
2013-05-07 14:34:52 -05:00
|
|
|
// maps a string to its interned representation
|
|
|
|
pub fn intern(str : &str) -> ast::ident {
|
|
|
|
let interner = get_ident_interner();
|
|
|
|
interner.intern(str)
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* All the valid words that have meaning in the Rust language.
|
|
|
|
*
|
2012-09-19 13:06:50 -05:00
|
|
|
* Rust keywords are either 'temporary', 'strict' or 'reserved'. Temporary
|
2012-09-12 17:22:34 -05:00
|
|
|
* keywords are contextual and may be used as identifiers anywhere. They are
|
2012-09-19 13:06:50 -05:00
|
|
|
* expected to disappear from the grammar soon. Strict keywords may not
|
|
|
|
* appear as identifiers at all. Reserved keywords are not used anywhere in
|
|
|
|
* the language and may not appear as identifiers.
|
2012-07-04 16:53:12 -05:00
|
|
|
*/
|
2013-04-03 08:28:36 -05:00
|
|
|
pub fn keyword_table() -> HashSet<~str> {
|
|
|
|
let mut keywords = HashSet::new();
|
2013-03-21 14:41:37 -05:00
|
|
|
let mut strict = strict_keyword_table();
|
|
|
|
let mut reserved = reserved_keyword_table();
|
|
|
|
|
2013-05-10 17:15:06 -05:00
|
|
|
do strict.consume |word| {
|
|
|
|
keywords.insert(word);
|
2012-04-17 23:14:40 -05:00
|
|
|
}
|
2013-05-10 17:15:06 -05:00
|
|
|
do reserved.consume |word| {
|
|
|
|
keywords.insert(word);
|
|
|
|
}
|
|
|
|
|
|
|
|
keywords
|
2012-04-17 23:14:40 -05:00
|
|
|
}
|
|
|
|
|
2012-09-09 19:35:56 -05:00
|
|
|
/// Full keywords. May not appear anywhere else.
|
2013-04-03 08:28:36 -05:00
|
|
|
pub fn strict_keyword_table() -> HashSet<~str> {
|
|
|
|
let mut words = HashSet::new();
|
2012-09-09 19:35:56 -05:00
|
|
|
let keys = ~[
|
2013-03-22 15:11:03 -05:00
|
|
|
~"as",
|
2012-09-10 01:03:46 -05:00
|
|
|
~"break",
|
2012-09-12 16:51:38 -05:00
|
|
|
~"const", ~"copy",
|
2012-09-10 01:03:46 -05:00
|
|
|
~"do", ~"drop",
|
2013-01-30 19:20:02 -06:00
|
|
|
~"else", ~"enum", ~"extern",
|
2013-01-31 20:24:09 -06:00
|
|
|
~"false", ~"fn", ~"for",
|
2012-09-11 21:28:14 -05:00
|
|
|
~"if", ~"impl",
|
2013-03-08 20:32:03 -06:00
|
|
|
~"let", ~"__log", ~"loop",
|
2013-02-15 04:49:32 -06:00
|
|
|
~"match", ~"mod", ~"mut",
|
2012-11-02 15:33:51 -05:00
|
|
|
~"once",
|
2012-09-11 20:17:13 -05:00
|
|
|
~"priv", ~"pub", ~"pure",
|
2012-09-10 19:26:54 -05:00
|
|
|
~"ref", ~"return",
|
2013-05-10 17:15:06 -05:00
|
|
|
~"static", ~"self", ~"struct", ~"super",
|
2012-09-10 01:03:46 -05:00
|
|
|
~"true", ~"trait", ~"type",
|
2012-09-19 13:03:34 -05:00
|
|
|
~"unsafe", ~"use",
|
2012-09-10 01:03:46 -05:00
|
|
|
~"while"
|
2012-09-09 19:35:56 -05:00
|
|
|
];
|
2013-03-21 14:41:37 -05:00
|
|
|
do vec::consume(keys) |_, w| {
|
|
|
|
words.insert(w);
|
2012-09-09 19:35:56 -05:00
|
|
|
}
|
2013-03-21 14:41:37 -05:00
|
|
|
return words;
|
2012-09-09 19:35:56 -05:00
|
|
|
}
|
|
|
|
|
2013-04-03 08:28:36 -05:00
|
|
|
pub fn reserved_keyword_table() -> HashSet<~str> {
|
|
|
|
let mut words = HashSet::new();
|
2012-09-11 21:26:48 -05:00
|
|
|
let keys = ~[
|
|
|
|
~"be"
|
|
|
|
];
|
2013-03-21 14:41:37 -05:00
|
|
|
do vec::consume(keys) |_, s| {
|
|
|
|
words.insert(s);
|
2012-09-11 21:26:48 -05:00
|
|
|
}
|
2013-03-21 14:41:37 -05:00
|
|
|
return words;
|
2012-09-11 21:26:48 -05:00
|
|
|
}
|