about summary refs log tree commit diff
path: root/src/libsyntax
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-02-13 16:32:01 -0800
committerbors <bors@rust-lang.org>2014-02-13 16:32:01 -0800
commit89b1686bd7db25b5dd948b1a4d9dfd0c68084c29 (patch)
treecfde530d295ebf159fc538115565d5ce1409bea3 /src/libsyntax
parent94d453e459107ed1c5d76f693686b29d31cdc58c (diff)
parent5deb3c9ca06509ce261f69fd01426825e32d61c1 (diff)
downloadrust-89b1686bd7db25b5dd948b1a4d9dfd0c68084c29.tar.gz
rust-89b1686bd7db25b5dd948b1a4d9dfd0c68084c29.zip
auto merge of #12017 : FlaPer87/rust/replace-mod-crate, r=alexcrichton
The first setp for #9880 is to add a new `crate` keyword. This PR does exactly that. I took a chance to refactor `parse_item_foreign_mod` and I broke it down into 2 separate methods to isolate each feature.

The next step will be to push a new stage0 snapshot and then get rid of all `extern mod` around the code.
Diffstat (limited to 'src/libsyntax')
-rw-r--r--src/libsyntax/ast.rs2
-rw-r--r--src/libsyntax/ast_map.rs4
-rw-r--r--src/libsyntax/ast_util.rs4
-rw-r--r--src/libsyntax/ext/base.rs2
-rw-r--r--src/libsyntax/ext/expand.rs18
-rw-r--r--src/libsyntax/ext/registrar.rs6
-rw-r--r--src/libsyntax/fold.rs4
-rw-r--r--src/libsyntax/parse/obsolete.rs12
-rw-r--r--src/libsyntax/parse/parser.rs160
-rw-r--r--src/libsyntax/parse/token.rs53
-rw-r--r--src/libsyntax/print/pprust.rs8
-rw-r--r--src/libsyntax/visit.rs4
12 files changed, 134 insertions, 143 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs
index ca876054fa8..a5a49b78906 100644
--- a/src/libsyntax/ast.rs
+++ b/src/libsyntax/ast.rs
@@ -181,7 +181,7 @@ pub type NodeId = u32;
 
 #[deriving(Clone, TotalEq, TotalOrd, Eq, Encodable, Decodable, IterBytes, ToStr)]
 pub struct DefId {
-    crate: CrateNum,
+    krate: CrateNum,
     node: NodeId,
 }
 
diff --git a/src/libsyntax/ast_map.rs b/src/libsyntax/ast_map.rs
index 817668f2232..9d3fe4f0c4d 100644
--- a/src/libsyntax/ast_map.rs
+++ b/src/libsyntax/ast_map.rs
@@ -406,7 +406,7 @@ pub fn map_crate<F: 'static + FoldOps>(diag: @SpanHandler, c: Crate,
         diag: diag,
         fold_ops: fold_ops
     };
-    let crate = cx.fold_crate(c);
+    let krate = cx.fold_crate(c);
 
     if log_enabled!(logging::DEBUG) {
         let map = cx.map.map.borrow();
@@ -421,7 +421,7 @@ pub fn map_crate<F: 'static + FoldOps>(diag: @SpanHandler, c: Crate,
               entries, vector_length, (entries as f64 / vector_length as f64) * 100.);
     }
 
-    (crate, cx.map)
+    (krate, cx.map)
 }
 
 // Used for items loaded from external crate that are being inlined into this
diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs
index afedb62105b..5f72a255d71 100644
--- a/src/libsyntax/ast_util.rs
+++ b/src/libsyntax/ast_util.rs
@@ -38,10 +38,10 @@ pub fn path_to_ident(path: &Path) -> Ident {
 }
 
 pub fn local_def(id: NodeId) -> DefId {
-    ast::DefId { crate: LOCAL_CRATE, node: id }
+    ast::DefId { krate: LOCAL_CRATE, node: id }
 }
 
-pub fn is_local(did: ast::DefId) -> bool { did.crate == LOCAL_CRATE }
+pub fn is_local(did: ast::DefId) -> bool { did.krate == LOCAL_CRATE }
 
 pub fn stmt_id(s: &Stmt) -> NodeId {
     match s.node {
diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs
index 02c3a1b985b..7ac66ecaa37 100644
--- a/src/libsyntax/ext/base.rs
+++ b/src/libsyntax/ext/base.rs
@@ -264,7 +264,7 @@ pub struct MacroCrate {
 }
 
 pub trait CrateLoader {
-    fn load_crate(&mut self, crate: &ast::ViewItem) -> MacroCrate;
+    fn load_crate(&mut self, krate: &ast::ViewItem) -> MacroCrate;
     fn get_exported_macros(&mut self, crate_num: ast::CrateNum) -> ~[~str];
     fn get_registrar_symbol(&mut self, crate_num: ast::CrateNum) -> Option<~str>;
 }
diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs
index d79c4cbc96a..d96fc27f3dd 100644
--- a/src/libsyntax/ext/expand.rs
+++ b/src/libsyntax/ext/expand.rs
@@ -414,10 +414,10 @@ pub fn expand_view_item(vi: &ast::ViewItem,
     noop_fold_view_item(vi, fld)
 }
 
-fn load_extern_macros(crate: &ast::ViewItem, fld: &mut MacroExpander) {
-    let MacroCrate { lib, cnum } = fld.cx.loader.load_crate(crate);
+fn load_extern_macros(krate: &ast::ViewItem, fld: &mut MacroExpander) {
+    let MacroCrate { lib, cnum } = fld.cx.loader.load_crate(krate);
 
-    let crate_name = match crate.node {
+    let crate_name = match krate.node {
         ast::ViewItemExternMod(ref name, _, _) => {
             let string = token::get_ident(name.name);
             string.get().to_str()
@@ -453,19 +453,19 @@ fn load_extern_macros(crate: &ast::ViewItem, fld: &mut MacroExpander) {
         // this is fatal: there are almost certainly macros we need
         // inside this crate, so continue would spew "macro undefined"
         // errors
-        Err(err) => fld.cx.span_fatal(crate.span, err)
+        Err(err) => fld.cx.span_fatal(krate.span, err)
     };
 
     unsafe {
         let registrar: MacroCrateRegistrationFun = match lib.symbol(registrar) {
             Ok(registrar) => registrar,
             // again fatal if we can't register macros
-            Err(err) => fld.cx.span_fatal(crate.span, err)
+            Err(err) => fld.cx.span_fatal(krate.span, err)
         };
         registrar(|name, extension| {
             let extension = match extension {
-                NormalTT(ext, _) => NormalTT(ext, Some(crate.span)),
-                IdentTT(ext, _) => IdentTT(ext, Some(crate.span)),
+                NormalTT(ext, _) => NormalTT(ext, Some(krate.span)),
+                IdentTT(ext, _) => IdentTT(ext, Some(krate.span)),
                 ItemDecorator(ext) => ItemDecorator(ext),
             };
             fld.extsbox.insert(name, extension);
@@ -1036,10 +1036,10 @@ mod test {
         }
     }
 
-    //fn fake_print_crate(crate: &ast::Crate) {
+    //fn fake_print_crate(krate: &ast::Crate) {
     //    let mut out = ~std::io::stderr() as ~std::io::Writer;
     //    let mut s = pprust::rust_printer(out, get_ident_interner());
-    //    pprust::print_crate_(&mut s, crate);
+    //    pprust::print_crate_(&mut s, krate);
     //}
 
     fn expand_crate_str(crate_str: ~str) -> ast::Crate {
diff --git a/src/libsyntax/ext/registrar.rs b/src/libsyntax/ext/registrar.rs
index e831e8fbb10..f0bad1b40eb 100644
--- a/src/libsyntax/ext/registrar.rs
+++ b/src/libsyntax/ext/registrar.rs
@@ -35,16 +35,16 @@ impl Visitor<()> for MacroRegistrarContext {
 }
 
 pub fn find_macro_registrar(diagnostic: @diagnostic::SpanHandler,
-                            crate: &ast::Crate) -> Option<ast::DefId> {
+                            krate: &ast::Crate) -> Option<ast::DefId> {
     let mut ctx = MacroRegistrarContext { registrars: ~[] };
-    visit::walk_crate(&mut ctx, crate, ());
+    visit::walk_crate(&mut ctx, krate, ());
 
     match ctx.registrars.len() {
         0 => None,
         1 => {
             let (node_id, _) = ctx.registrars.pop().unwrap();
             Some(ast::DefId {
-                crate: ast::LOCAL_CRATE,
+                krate: ast::LOCAL_CRATE,
                 node: node_id
             })
         },
diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs
index 52ff3798f1b..1b14bd72382 100644
--- a/src/libsyntax/fold.rs
+++ b/src/libsyntax/fold.rs
@@ -859,8 +859,8 @@ mod test {
 
     // this version doesn't care about getting comments or docstrings in.
     fn fake_print_crate(s: &mut pprust::State,
-                        crate: &ast::Crate) -> io::IoResult<()> {
-        pprust::print_mod(s, &crate.module, crate.attrs)
+                        krate: &ast::Crate) -> io::IoResult<()> {
+        pprust::print_mod(s, &krate.module, krate.attrs)
     }
 
     // change every identifier to "zz"
diff --git a/src/libsyntax/parse/obsolete.rs b/src/libsyntax/parse/obsolete.rs
index 6f6f8d7d563..24227dea531 100644
--- a/src/libsyntax/parse/obsolete.rs
+++ b/src/libsyntax/parse/obsolete.rs
@@ -30,7 +30,6 @@ pub enum ObsoleteSyntax {
     ObsoleteSwap,
     ObsoleteUnsafeBlock,
     ObsoleteBareFnType,
-    ObsoleteNamedExternModule,
     ObsoleteMultipleLocalDecl,
     ObsoleteUnsafeExternFn,
     ObsoleteTraitFuncVisibility,
@@ -42,7 +41,6 @@ pub enum ObsoleteSyntax {
     ObsoleteBoxedClosure,
     ObsoleteClosureType,
     ObsoleteMultipleImport,
-    ObsoleteExternModAttributesInParens,
     ObsoleteManagedPattern,
     ObsoleteManagedString,
     ObsoleteManagedVec,
@@ -86,11 +84,6 @@ impl ParserObsoleteMethods for Parser {
                 "bare function type",
                 "use `|A| -> B` or `extern fn(A) -> B` instead"
             ),
-            ObsoleteNamedExternModule => (
-                "named external module",
-                "instead of `extern mod foo { ... }`, write `mod foo { \
-                 extern { ... } }`"
-            ),
             ObsoleteMultipleLocalDecl => (
                 "declaration of multiple locals at once",
                 "instead of e.g. `let a = 1, b = 2`, write \
@@ -141,11 +134,6 @@ impl ParserObsoleteMethods for Parser {
                 "multiple imports",
                 "only one import is allowed per `use` statement"
             ),
-            ObsoleteExternModAttributesInParens => (
-                "`extern mod` with linkage attribute list",
-                "use `extern mod foo = \"bar\";` instead of \
-                `extern mod foo (name = \"bar\")`"
-            ),
             ObsoleteManagedPattern => (
                 "managed pointer pattern",
                 "use a nested `match` expression instead of a managed box \
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index 4bef7dc5bb5..15608a0f28e 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -866,7 +866,10 @@ impl Parser {
 
         */
 
-        let opt_abis = self.parse_opt_abis();
+        let opt_abis = if self.eat_keyword(keywords::Extern) {
+            self.parse_opt_abis()
+        } else { None };
+
         let abis = opt_abis.unwrap_or(AbiSet::Rust());
         let purity = self.parse_unsafety();
         self.expect_keyword(keywords::Fn);
@@ -4308,91 +4311,78 @@ impl Parser {
         }
     }
 
-    // parse extern foo; or extern mod foo { ... } or extern { ... }
-    fn parse_item_foreign_mod(&mut self,
-                              lo: BytePos,
-                              opt_abis: Option<AbiSet>,
-                              visibility: Visibility,
-                              attrs: ~[Attribute],
-                              items_allowed: bool)
-                              -> ItemOrViewItem {
-        let mut must_be_named_mod = false;
-        if self.is_keyword(keywords::Mod) {
-            must_be_named_mod = true;
-            self.expect_keyword(keywords::Mod);
-        } else if self.token != token::LBRACE {
-            let token_str = self.this_token_to_str();
-            self.span_fatal(self.span,
-                            format!("expected `\\{` or `mod` but found `{}`",
-                                    token_str))
-        }
+    /// Parse extern crate links
+    ///
+    /// # Example
+    ///
+    /// extern crate extra;
+    /// extern crate foo = "bar";
+    fn parse_item_extern_crate(&mut self,
+                                lo: BytePos,
+                                visibility: Visibility,
+                                attrs: ~[Attribute])
+                                -> ItemOrViewItem {
 
-        let (named, maybe_path, ident) = match self.token {
+        let (maybe_path, ident) = match self.token {
             token::IDENT(..) => {
                 let the_ident = self.parse_ident();
+                self.expect_one_of(&[], &[token::EQ, token::SEMI]);
                 let path = if self.token == token::EQ {
                     self.bump();
                     Some(self.parse_str())
-                }
-                else { None };
-                (true, path, the_ident)
+                } else {None};
+
+                self.expect(&token::SEMI);
+                (path, the_ident)
             }
             _ => {
-                if must_be_named_mod {
-                    let token_str = self.this_token_to_str();
-                    self.span_fatal(self.span,
-                                    format!("expected foreign module name but \
-                                             found `{}`",
-                                            token_str))
-                }
-
-                (false, None,
-                 special_idents::clownshoes_foreign_mod)
+                let token_str = self.this_token_to_str();
+                self.span_fatal(self.span,
+                                format!("expected extern crate name but found `{}`",
+                                        token_str));
             }
         };
 
-        // extern mod foo { ... } or extern { ... }
-        if items_allowed && self.eat(&token::LBRACE) {
-            // `extern mod foo { ... }` is obsolete.
-            if named {
-                self.obsolete(self.last_span, ObsoleteNamedExternModule);
-            }
-
-            let abis = opt_abis.unwrap_or(AbiSet::C());
+        IoviViewItem(ast::ViewItem {
+                node: ViewItemExternMod(ident, maybe_path, ast::DUMMY_NODE_ID),
+                attrs: attrs,
+                vis: visibility,
+                span: mk_sp(lo, self.last_span.hi)
+            })
+    }
 
-            let (inner, next) = self.parse_inner_attrs_and_next();
-            let m = self.parse_foreign_mod_items(abis, next);
-            self.expect(&token::RBRACE);
+    /// Parse `extern` for foreign ABIs
+    /// modules.
+    ///
+    /// `extern` is expected to have been
+    /// consumed before calling this method
+    ///
+    /// # Examples:
+    ///
+    /// extern "C" {}
+    /// extern {}
+    fn parse_item_foreign_mod(&mut self,
+                              lo: BytePos,
+                              opt_abis: Option<AbiSet>,
+                              visibility: Visibility,
+                              attrs: ~[Attribute])
+                              -> ItemOrViewItem {
 
-            let item = self.mk_item(lo,
-                                    self.last_span.hi,
-                                    ident,
-                                    ItemForeignMod(m),
-                                    visibility,
-                                    maybe_append(attrs, Some(inner)));
-            return IoviItem(item);
-        }
+        self.expect(&token::LBRACE);
 
-        if opt_abis.is_some() {
-            self.span_err(self.span, "an ABI may not be specified here");
-        }
+        let abis = opt_abis.unwrap_or(AbiSet::C());
 
+        let (inner, next) = self.parse_inner_attrs_and_next();
+        let m = self.parse_foreign_mod_items(abis, next);
+        self.expect(&token::RBRACE);
 
-        if self.token == token::LPAREN {
-            // `extern mod foo (name = "bar"[,vers = "version"]) is obsolete,
-            // `extern mod foo = "bar#[version]";` should be used.
-            // Parse obsolete options to avoid wired parser errors
-            self.parse_optional_meta();
-            self.obsolete(self.span, ObsoleteExternModAttributesInParens);
-        }
-        // extern mod foo;
-        self.expect(&token::SEMI);
-        IoviViewItem(ast::ViewItem {
-            node: ViewItemExternMod(ident, maybe_path, ast::DUMMY_NODE_ID),
-            attrs: attrs,
-            vis: visibility,
-            span: mk_sp(lo, self.last_span.hi)
-        })
+        let item = self.mk_item(lo,
+                                self.last_span.hi,
+                                special_idents::clownshoes_foreign_mod,
+                                ItemForeignMod(m),
+                                visibility,
+                                maybe_append(attrs, Some(inner)));
+        return IoviItem(item);
     }
 
     // parse type Foo = Bar;
@@ -4504,10 +4494,6 @@ impl Parser {
     // Parses a string as an ABI spec on an extern type or module. Consumes
     // the `extern` keyword, if one is found.
     fn parse_opt_abis(&mut self) -> Option<AbiSet> {
-        if !self.eat_keyword(keywords::Extern) {
-            return None
-        }
-
         match self.token {
             token::LIT_STR(s)
             | token::LIT_STR_RAW(s, _) => {
@@ -4585,7 +4571,20 @@ impl Parser {
             });
         }
         // either a view item or an item:
-        if self.is_keyword(keywords::Extern) {
+        if self.eat_keyword(keywords::Extern) {
+            let next_is_mod = self.eat_keyword(keywords::Mod);
+
+            if next_is_mod || self.eat_keyword(keywords::Crate) {
+                // NOTE(flaper87): Uncomment this when this changes gets into stage0
+                //
+                // if next_is_mod {
+                //    self.span_err(self.span,
+                //                   format!("`extern mod` is obsolete, use `extern crate` instead \
+                //                           to refer to external crates."))
+                // }
+                return self.parse_item_extern_crate(lo, visibility, attrs);
+            }
+
             let opt_abis = self.parse_opt_abis();
 
             if self.eat_keyword(keywords::Fn) {
@@ -4600,12 +4599,15 @@ impl Parser {
                                         visibility,
                                         maybe_append(attrs, extra_attrs));
                 return IoviItem(item);
-            } else  {
-                // EXTERN MODULE ITEM (IoviViewItem)
-                return self.parse_item_foreign_mod(lo, opt_abis, visibility, attrs,
-                                                   true);
+            } else if self.token == token::LBRACE {
+                return self.parse_item_foreign_mod(lo, opt_abis, visibility, attrs);
             }
+
+            let token_str = self.this_token_to_str();
+            self.span_fatal(self.span,
+                            format!("expected `\\{` or `fn` but found `{}`", token_str));
         }
+
         // the rest are all guaranteed to be items:
         if self.is_keyword(keywords::Static) {
             // STATIC ITEM
diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs
index 1e9eab1573b..423ddb82808 100644
--- a/src/libsyntax/parse/token.rs
+++ b/src/libsyntax/parse/token.rs
@@ -462,37 +462,38 @@ declare_special_idents_and_keywords! {
         (28,                         Loop,       "loop");
         (29,                         Match,      "match");
         (30,                         Mod,        "mod");
-        (31,                         Mut,        "mut");
-        (32,                         Once,       "once");
-        (33,                         Priv,       "priv");
-        (34,                         Pub,        "pub");
-        (35,                         Ref,        "ref");
-        (36,                         Return,     "return");
+        (31,                         Crate,      "crate");
+        (32,                         Mut,        "mut");
+        (33,                         Once,       "once");
+        (34,                         Priv,       "priv");
+        (35,                         Pub,        "pub");
+        (36,                         Ref,        "ref");
+        (37,                         Return,     "return");
         // Static and Self are also special idents (prefill de-dupes)
         (super::STATIC_KEYWORD_NAME, Static,     "static");
         (super::SELF_KEYWORD_NAME,   Self,       "self");
-        (37,                         Struct,     "struct");
-        (38,                         Super,      "super");
-        (39,                         True,       "true");
-        (40,                         Trait,      "trait");
-        (41,                         Type,       "type");
-        (42,                         Unsafe,     "unsafe");
-        (43,                         Use,        "use");
-        (44,                         While,      "while");
-        (45,                         Continue,   "continue");
-        (46,                         Proc,       "proc");
-        (47,                         Box,        "box");
+        (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");
 
         'reserved:
-        (48,                         Alignof,    "alignof");
-        (49,                         Be,         "be");
-        (50,                         Offsetof,   "offsetof");
-        (51,                         Pure,       "pure");
-        (52,                         Sizeof,     "sizeof");
-        (53,                         Typeof,     "typeof");
-        (54,                         Unsized,    "unsized");
-        (55,                         Yield,      "yield");
-        (56,                         Do,         "do");
+        (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");
     }
 }
 
diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs
index 7f7f57b13a9..24aa30cb36b 100644
--- a/src/libsyntax/print/pprust.rs
+++ b/src/libsyntax/print/pprust.rs
@@ -116,7 +116,7 @@ pub static default_columns: uint = 78u;
 pub fn print_crate(cm: @CodeMap,
                    intr: @IdentInterner,
                    span_diagnostic: @diagnostic::SpanHandler,
-                   crate: &ast::Crate,
+                   krate: &ast::Crate,
                    filename: ~str,
                    input: &mut io::Reader,
                    out: ~io::Writer,
@@ -147,11 +147,11 @@ pub fn print_crate(cm: @CodeMap,
         boxes: RefCell::new(~[]),
         ann: ann
     };
-    print_crate_(&mut s, crate)
+    print_crate_(&mut s, krate)
 }
 
-pub fn print_crate_(s: &mut State, crate: &ast::Crate) -> io::IoResult<()> {
-    if_ok!(print_mod(s, &crate.module, crate.attrs));
+pub fn print_crate_(s: &mut State, krate: &ast::Crate) -> io::IoResult<()> {
+    if_ok!(print_mod(s, &krate.module, krate.attrs));
     if_ok!(print_remaining_comments(s));
     if_ok!(eof(&mut s.s));
     Ok(())
diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs
index 0c6ecf73f6a..e1fe43b8e04 100644
--- a/src/libsyntax/visit.rs
+++ b/src/libsyntax/visit.rs
@@ -132,8 +132,8 @@ pub fn walk_inlined_item<E: Clone, V: Visitor<E>>(visitor: &mut V,
 }
 
 
-pub fn walk_crate<E: Clone, V: Visitor<E>>(visitor: &mut V, crate: &Crate, env: E) {
-    visitor.visit_mod(&crate.module, crate.span, CRATE_NODE_ID, env)
+pub fn walk_crate<E: Clone, V: Visitor<E>>(visitor: &mut V, krate: &Crate, env: E) {
+    visitor.visit_mod(&krate.module, krate.span, CRATE_NODE_ID, env)
 }
 
 pub fn walk_mod<E: Clone, V: Visitor<E>>(visitor: &mut V, module: &Mod, env: E) {