diff options
Diffstat (limited to 'src/libsyntax/parse')
| -rw-r--r-- | src/libsyntax/parse/comments.rs | 3 | ||||
| -rw-r--r-- | src/libsyntax/parse/common.rs | 8 | ||||
| -rw-r--r-- | src/libsyntax/parse/obsolete.rs | 3 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 37 | ||||
| -rw-r--r-- | src/libsyntax/parse/token.rs | 221 |
5 files changed, 120 insertions, 152 deletions
diff --git a/src/libsyntax/parse/comments.rs b/src/libsyntax/parse/comments.rs index 138f9d71640..e0ae9ce2812 100644 --- a/src/libsyntax/parse/comments.rs +++ b/src/libsyntax/parse/comments.rs @@ -16,7 +16,6 @@ use parse::lexer::{StringReader, bump, is_eof, nextch_is, TokenAndSpan}; use parse::lexer::{is_line_non_doc_comment, is_block_non_doc_comment}; use parse::lexer; use parse::token; -use parse::token::{get_ident_interner}; use std::io; use std::str; @@ -385,7 +384,7 @@ pub fn gather_comments_and_literals(span_diagnostic: literals.push(Literal {lit: s.to_owned(), pos: sp.lo}); }) } else { - debug!("tok: {}", token::to_str(get_ident_interner(), &tok)); + debug!("tok: {}", token::to_str(&tok)); } first_read = false; } diff --git a/src/libsyntax/parse/common.rs b/src/libsyntax/parse/common.rs index 12e32731fcc..460ad76cffe 100644 --- a/src/libsyntax/parse/common.rs +++ b/src/libsyntax/parse/common.rs @@ -9,7 +9,6 @@ // except according to those terms. use parse::token; -use parse::token::{get_ident_interner}; // SeqSep : a sequence separator (token) // and whether a trailing separator is allowed. @@ -36,10 +35,3 @@ pub fn seq_sep_none() -> SeqSep { trailing_sep_allowed: false, } } - -// maps any token back to a string. not necessary if you know it's -// an identifier.... -pub fn token_to_str(token: &token::Token) -> ~str { - token::to_str(get_ident_interner(), token) -} - diff --git a/src/libsyntax/parse/obsolete.rs b/src/libsyntax/parse/obsolete.rs index 24227dea531..accffd374a9 100644 --- a/src/libsyntax/parse/obsolete.rs +++ b/src/libsyntax/parse/obsolete.rs @@ -175,8 +175,7 @@ impl ParserObsoleteMethods for Parser { fn is_obsolete_ident(&mut self, ident: &str) -> bool { match self.token { token::IDENT(sid, _) => { - let interned_string = token::get_ident(sid.name); - interned_string.equiv(&ident) + token::get_ident(sid).equiv(&ident) } _ => false } diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 15608a0f28e..674d41e9dbe 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -71,9 +71,9 @@ use parse::common::{seq_sep_trailing_disallowed, seq_sep_trailing_allowed}; use parse::lexer::Reader; use parse::lexer::TokenAndSpan; use parse::obsolete::*; -use parse::token::{INTERPOLATED, InternedString, can_begin_expr, get_ident}; -use parse::token::{get_ident_interner, is_ident, is_ident_or_path}; -use parse::token::{is_plain_ident, keywords, special_idents, token_to_binop}; +use parse::token::{INTERPOLATED, InternedString, can_begin_expr}; +use parse::token::{is_ident, is_ident_or_path, is_plain_ident}; +use parse::token::{keywords, special_idents, token_to_binop}; use parse::token; use parse::{new_sub_parser_from_file, ParseSess}; use opt_vec; @@ -288,7 +288,6 @@ struct ParsedItemsAndViewItems { pub fn Parser(sess: @ParseSess, cfg: ast::CrateConfig, rdr: ~Reader:) -> Parser { let tok0 = rdr.next_token(); - let interner = get_ident_interner(); let span = tok0.sp; let placeholder = TokenAndSpan { tok: token::UNDERSCORE, @@ -297,7 +296,7 @@ pub fn Parser(sess: @ParseSess, cfg: ast::CrateConfig, rdr: ~Reader:) Parser { reader: rdr, - interner: interner, + interner: token::get_ident_interner(), sess: sess, cfg: cfg, token: tok0.tok, @@ -359,7 +358,7 @@ fn is_plain_ident_or_underscore(t: &token::Token) -> bool { impl Parser { // convert a token to a string using self's reader pub fn token_to_str(token: &token::Token) -> ~str { - token::to_str(get_ident_interner(), token) + token::to_str(token) } // convert the current token to a string using self's reader @@ -531,12 +530,10 @@ impl Parser { // otherwise, eat it. pub fn expect_keyword(&mut self, kw: keywords::Keyword) { if !self.eat_keyword(kw) { - let id_ident = kw.to_ident(); - let id_interned_str = token::get_ident(id_ident.name); + let id_interned_str = token::get_ident(kw.to_ident()); let token_str = self.this_token_to_str(); self.fatal(format!("expected `{}`, found `{}`", - id_interned_str.get(), - token_str)) + id_interned_str, token_str)) } } @@ -804,7 +801,7 @@ impl Parser { } pub fn id_to_interned_str(&mut self, id: Ident) -> InternedString { - get_ident(id.name) + token::get_ident(id) } // Is the current token one of the keywords that signals a bare function @@ -3425,8 +3422,7 @@ impl Parser { loop { match self.token { token::LIFETIME(lifetime) => { - let lifetime_interned_string = - token::get_ident(lifetime.name); + let lifetime_interned_string = token::get_ident(lifetime); if lifetime_interned_string.equiv(&("static")) { result.push(RegionTyParamBound); } else { @@ -3876,10 +3872,6 @@ impl Parser { // First, parse type parameters if necessary. let generics = self.parse_generics(); - // This is a new-style impl declaration. - // FIXME: clownshoes - let ident = special_idents::clownshoes_extensions; - // Special case: if the next identifier that follows is '(', don't // allow this to be parsed as a trait. let could_be_trait = self.token != token::LPAREN; @@ -3923,6 +3915,8 @@ impl Parser { method_attrs = None; } + let ident = ast_util::impl_pretty_name(&opt_trait, ty); + (ident, ItemImpl(generics, opt_trait, ty, meths), Some(inner_attrs)) } @@ -3959,9 +3953,8 @@ impl Parser { fields.push(self.parse_struct_decl_field()); } if fields.len() == 0 { - let string = get_ident_interner().get(class_name.name); self.fatal(format!("unit-like struct definition should be written as `struct {};`", - string.as_slice())); + token::get_ident(class_name))); } self.bump(); } else if self.token == token::LPAREN { @@ -4159,7 +4152,7 @@ impl Parser { outer_attrs, "path") { Some(d) => dir_path.join(d), None => { - let mod_string = token::get_ident(id.name); + let mod_string = token::get_ident(id); let mod_name = mod_string.get().to_owned(); let default_path_str = mod_name + ".rs"; let secondary_path_str = mod_name + "/mod.rs"; @@ -4378,7 +4371,7 @@ impl Parser { let item = self.mk_item(lo, self.last_span.hi, - special_idents::clownshoes_foreign_mod, + special_idents::invalid, ItemForeignMod(m), visibility, maybe_append(attrs, Some(inner))); @@ -4498,7 +4491,7 @@ impl Parser { token::LIT_STR(s) | token::LIT_STR_RAW(s, _) => { self.bump(); - let identifier_string = token::get_ident(s.name); + let identifier_string = token::get_ident(s); let the_string = identifier_string.get(); let mut abis = AbiSet::empty(); for word in the_string.words() { diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index 423ddb82808..b264e8d7467 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -9,7 +9,7 @@ // except according to those terms. use ast; -use ast::{P, Name, Mrk}; +use ast::{P, Ident, Name, Mrk}; use ast_util; use parse::token; use util::interner::{RcStr, StrInterner}; @@ -133,7 +133,7 @@ pub fn binop_to_str(o: BinOp) -> ~str { } } -pub fn to_str(input: @IdentInterner, t: &Token) -> ~str { +pub fn to_str(t: &Token) -> ~str { match *t { EQ => ~"=", LT => ~"<", @@ -187,50 +187,42 @@ pub fn to_str(input: @IdentInterner, t: &Token) -> ~str { u.to_str() + ast_util::uint_ty_to_str(t) } LIT_INT_UNSUFFIXED(i) => { i.to_str() } - LIT_FLOAT(ref s, t) => { - let body_string = get_ident(s.name); - let mut body = body_string.get().to_str(); + LIT_FLOAT(s, t) => { + let mut body = get_ident(s).get().to_str(); if body.ends_with(".") { body.push_char('0'); // `10.f` is not a float literal } body + ast_util::float_ty_to_str(t) } - LIT_FLOAT_UNSUFFIXED(ref s) => { - let body_string = get_ident(s.name); - let mut body = body_string.get().to_owned(); + LIT_FLOAT_UNSUFFIXED(s) => { + let mut body = get_ident(s).get().to_str(); if body.ends_with(".") { body.push_char('0'); // `10.f` is not a float literal } body } - LIT_STR(ref s) => { - let literal_string = get_ident(s.name); - format!("\"{}\"", literal_string.get().escape_default()) + LIT_STR(s) => { + format!("\"{}\"", get_ident(s).get().escape_default()) } - LIT_STR_RAW(ref s, n) => { - let literal_string = get_ident(s.name); + LIT_STR_RAW(s, n) => { format!("r{delim}\"{string}\"{delim}", - delim="#".repeat(n), string=literal_string.get()) + delim="#".repeat(n), string=get_ident(s)) } /* Name components */ - IDENT(s, _) => input.get(s.name).into_owned(), + IDENT(s, _) => get_ident(s).get().to_str(), LIFETIME(s) => { - let name = input.get(s.name); - format!("'{}", name.as_slice()) + format!("'{}", get_ident(s)) } UNDERSCORE => ~"_", /* Other */ - DOC_COMMENT(ref s) => { - let comment_string = get_ident(s.name); - comment_string.get().to_str() - } + DOC_COMMENT(s) => get_ident(s).get().to_str(), EOF => ~"<eof>", INTERPOLATED(ref nt) => { match nt { - &NtExpr(e) => ::print::pprust::expr_to_str(e, input), - &NtAttr(e) => ::print::pprust::attribute_to_str(e, input), + &NtExpr(e) => ::print::pprust::expr_to_str(e), + &NtAttr(e) => ::print::pprust::attribute_to_str(e), _ => { ~"an interpolated " + match *nt { @@ -398,7 +390,7 @@ macro_rules! declare_special_idents_and_keywords {( } } - fn mk_fresh_ident_interner() -> @IdentInterner { + fn mk_fresh_ident_interner() -> IdentInterner { // The indices here must correspond to the numbers in // special_idents, in Keyword to_ident(), and in static // constants below. @@ -408,92 +400,85 @@ macro_rules! declare_special_idents_and_keywords {( $( $rk_str, )* ]; - @interner::StrInterner::prefill(init_vec) + interner::StrInterner::prefill(init_vec) } }} // If the special idents get renumbered, remember to modify these two as appropriate -static SELF_KEYWORD_NAME: Name = 3; -static STATIC_KEYWORD_NAME: Name = 10; +static SELF_KEYWORD_NAME: Name = 1; +static STATIC_KEYWORD_NAME: Name = 2; declare_special_idents_and_keywords! { pub mod special_idents { // These ones are statics - - (0, anon, "anon"); - (1, invalid, ""); // '' - (2, clownshoes_extensions, "__extensions__"); - - (super::SELF_KEYWORD_NAME, self_, "self"); // 'self' + (0, invalid, ""); + (super::SELF_KEYWORD_NAME, self_, "self"); + (super::STATIC_KEYWORD_NAME, statik, "static"); // for matcher NTs - (4, tt, "tt"); - (5, matchers, "matchers"); + (3, tt, "tt"); + (4, matchers, "matchers"); // outside of libsyntax - (6, arg, "arg"); - (7, clownshoe_abi, "__rust_abi"); - (8, main, "main"); - (9, opaque, "<opaque>"); - (super::STATIC_KEYWORD_NAME, statik, "static"); - (11, clownshoes_foreign_mod, "__foreign_mod__"); - (12, unnamed_field, "<unnamed_field>"); - (13, type_self, "Self"); // `Self` + (5, clownshoe_abi, "__rust_abi"); + (6, opaque, "<opaque>"); + (7, unnamed_field, "<unnamed_field>"); + (8, type_self, "Self"); } pub mod keywords { // These ones are variants of the Keyword enum 'strict: - (14, As, "as"); - (15, Break, "break"); - (16, Const, "const"); - (17, Else, "else"); - (18, Enum, "enum"); - (19, Extern, "extern"); - (20, False, "false"); - (21, Fn, "fn"); - (22, For, "for"); - (23, If, "if"); - (24, Impl, "impl"); - (25, In, "in"); - (26, Let, "let"); - (27, __LogLevel, "__log_level"); - (28, Loop, "loop"); - (29, Match, "match"); - (30, Mod, "mod"); - (31, Crate, "crate"); - (32, Mut, "mut"); - (33, Once, "once"); - (34, Priv, "priv"); - (35, Pub, "pub"); - (36, Ref, "ref"); - (37, Return, "return"); + (9, As, "as"); + (10, Break, "break"); + (11, Const, "const"); + (12, Crate, "crate"); + (13, Else, "else"); + (14, Enum, "enum"); + (15, Extern, "extern"); + (16, False, "false"); + (17, Fn, "fn"); + (18, For, "for"); + (19, If, "if"); + (20, Impl, "impl"); + (21, In, "in"); + (22, Let, "let"); + (23, __LogLevel, "__log_level"); + (24, Loop, "loop"); + (25, Match, "match"); + (26, Mod, "mod"); + (27, Mut, "mut"); + (28, Once, "once"); + (29, Priv, "priv"); + (30, Pub, "pub"); + (31, Ref, "ref"); + (32, Return, "return"); // Static and Self are also special idents (prefill de-dupes) (super::STATIC_KEYWORD_NAME, Static, "static"); (super::SELF_KEYWORD_NAME, Self, "self"); - (38, Struct, "struct"); - (39, Super, "super"); - (40, True, "true"); - (41, Trait, "trait"); - (42, Type, "type"); - (43, Unsafe, "unsafe"); - (44, Use, "use"); - (45, While, "while"); - (46, Continue, "continue"); - (47, Proc, "proc"); - (48, Box, "box"); + (33, Struct, "struct"); + (34, Super, "super"); + (35, True, "true"); + (36, Trait, "trait"); + (37, Type, "type"); + (38, Unsafe, "unsafe"); + (39, Use, "use"); + (40, While, "while"); + (41, Continue, "continue"); + (42, Proc, "proc"); + (43, Box, "box"); 'reserved: - (49, Alignof, "alignof"); - (50, Be, "be"); - (51, Offsetof, "offsetof"); - (52, Pure, "pure"); - (53, Sizeof, "sizeof"); - (54, Typeof, "typeof"); - (55, Unsized, "unsized"); - (56, Yield, "yield"); - (57, Do, "do"); + (44, Alignof, "alignof"); + (45, Be, "be"); + (46, Offsetof, "offsetof"); + (47, Pure, "pure"); + (48, Sizeof, "sizeof"); + (49, Typeof, "typeof"); + (50, Unsized, "unsized"); + (51, Yield, "yield"); + (52, Do, "do"); } } @@ -531,12 +516,12 @@ pub type IdentInterner = StrInterner; // if an interner exists in TLS, return it. Otherwise, prepare a // fresh one. pub fn get_ident_interner() -> @IdentInterner { - local_data_key!(key: @@::parse::token::IdentInterner) + local_data_key!(key: @::parse::token::IdentInterner) match local_data::get(key, |k| k.map(|k| *k)) { - Some(interner) => *interner, + Some(interner) => interner, None => { - let interner = mk_fresh_ident_interner(); - local_data::set(key, @interner); + let interner = @mk_fresh_ident_interner(); + local_data::set(key, interner); interner } } @@ -603,8 +588,7 @@ impl<'a> Equiv<&'a str> for InternedString { impl<D:Decoder> Decodable<D> for InternedString { fn decode(d: &mut D) -> InternedString { - let interner = get_ident_interner(); - get_ident(interner.intern(d.read_str())) + get_name(get_ident_interner().intern(d.read_str())) } } @@ -614,54 +598,55 @@ impl<E:Encoder> Encodable<E> for InternedString { } } +/// 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)) +} + /// Returns the string contents of an identifier, using the task-local /// interner. #[inline] -pub fn get_ident(idx: Name) -> InternedString { - let interner = get_ident_interner(); - InternedString::new_from_rc_str(interner.get(idx)) +pub fn get_ident(ident: Ident) -> InternedString { + get_name(ident.name) } /// Interns and returns the string contents of an identifier, using the /// task-local interner. #[inline] pub fn intern_and_get_ident(s: &str) -> InternedString { - get_ident(intern(s)) -} - -/* for when we don't care about the contents; doesn't interact with TLD or - serialization */ -pub fn mk_fake_ident_interner() -> @IdentInterner { - @interner::StrInterner::new() + get_name(intern(s)) } -// maps a string to its interned representation +/// Maps a string to its interned representation. #[inline] -pub fn intern(str : &str) -> Name { - let interner = get_ident_interner(); - interner.intern(str) +pub fn intern(s: &str) -> Name { + get_ident_interner().intern(s) } -// gensyms a new uint, using the current interner -pub fn gensym(str : &str) -> Name { - let interner = get_ident_interner(); - interner.gensym(str) +/// gensym's a new uint, using the current interner. +#[inline] +pub fn gensym(s: &str) -> Name { + get_ident_interner().gensym(s) } -// maps a string to an identifier with an empty syntax context -pub fn str_to_ident(str : &str) -> ast::Ident { - ast::Ident::new(intern(str)) +/// Maps a string to an identifier with an empty syntax context. +#[inline] +pub fn str_to_ident(s: &str) -> ast::Ident { + ast::Ident::new(intern(s)) } -// maps a string to a gensym'ed identifier -pub fn gensym_ident(str : &str) -> ast::Ident { - ast::Ident::new(gensym(str)) +/// Maps a string to a gensym'ed identifier. +#[inline] +pub fn gensym_ident(s: &str) -> ast::Ident { + ast::Ident::new(gensym(s)) } // create a fresh name that maps to the same string as the old one. // note that this guarantees that str_ptr_eq(ident_to_str(src),interner_get(fresh_name(src))); // that is, that the new name and the old one are connected to ptr_eq strings. -pub fn fresh_name(src : &ast::Ident) -> Name { +pub fn fresh_name(src: &ast::Ident) -> Name { let interner = get_ident_interner(); interner.gensym_copy(src.name) // following: debug version. Could work in final except that it's incompatible with |
