about summary refs log tree commit diff
path: root/src/comp/syntax
diff options
context:
space:
mode:
authorBrian Anderson <banderson@mozilla.com>2011-08-16 15:21:30 -0700
committerBrian Anderson <banderson@mozilla.com>2011-08-17 11:04:56 -0700
commitcd54e7772040cd268f911e9dfd088ad5f659f552 (patch)
treed12ac71b3f3ec50fe345d8064c87f8a5b44a9010 /src/comp/syntax
parentc4ce463f3709c7a8a92941d2e4bfab6db5da550f (diff)
downloadrust-cd54e7772040cd268f911e9dfd088ad5f659f552.tar.gz
rust-cd54e7772040cd268f911e9dfd088ad5f659f552.zip
Allow multiple imports in a single statement
Like so: import foo::{bar, baz};

Issue #817
Diffstat (limited to 'src/comp/syntax')
-rw-r--r--src/comp/syntax/ast.rs5
-rw-r--r--src/comp/syntax/parse/parser.rs39
-rw-r--r--src/comp/syntax/print/pprust.rs13
3 files changed, 55 insertions, 2 deletions
diff --git a/src/comp/syntax/ast.rs b/src/comp/syntax/ast.rs
index b286fdaf484..066a0b9675b 100644
--- a/src/comp/syntax/ast.rs
+++ b/src/comp/syntax/ast.rs
@@ -564,10 +564,15 @@ type view_item = spanned<view_item_>;
 // 'import ::foo'
 type simple_path = [ident];
 
+type import_ident_ = {name: ident, id: node_id};
+
+type import_ident = spanned<import_ident_>;
+
 tag view_item_ {
     view_item_use(ident, [@meta_item], node_id);
     view_item_import(ident, simple_path, node_id);
     view_item_import_glob(simple_path, node_id);
+    view_item_import_from(simple_path, [import_ident], node_id);
     view_item_export([ident], node_id);
 }
 
diff --git a/src/comp/syntax/parse/parser.rs b/src/comp/syntax/parse/parser.rs
index e6b65444029..f92ab2ca6bd 100644
--- a/src/comp/syntax/parse/parser.rs
+++ b/src/comp/syntax/parse/parser.rs
@@ -2288,11 +2288,17 @@ fn parse_rest_import_name(p: &parser, first: ast::ident,
    ast::view_item_ {
     let identifiers: [ast::ident] = ~[first];
     let glob: bool = false;
+    let from_idents = option::none::<[ast::import_ident]>;
     while true {
         alt p.peek() {
           token::SEMI. { break; }
           token::MOD_SEP. {
-            if glob { p.fatal("cannot path into a glob"); }
+            if glob {
+                p.fatal("cannot path into a glob");
+            }
+            if option::is_some(from_idents) {
+                p.fatal("cannot path into import list");
+            }
             p.bump();
           }
           _ { p.fatal("expecting '::' or ';'"); }
@@ -2305,17 +2311,46 @@ fn parse_rest_import_name(p: &parser, first: ast::ident,
             glob = true;
             p.bump();
           }
+
+          token::LBRACE. {
+            fn parse_import_ident(p: &parser) -> ast::import_ident {
+                let lo = p.get_lo_pos();
+                let ident = parse_ident(p);
+                let hi = p.get_hi_pos();
+                ret spanned(lo, hi, {name: ident,
+                                     id: p.get_id()});
+            }
+            let from_idents_ = parse_seq(token::LBRACE,
+                                         token::RBRACE,
+                                         some(token::COMMA),
+                                         parse_import_ident,
+                                         p).node;
+            if vec::is_empty(from_idents_) {
+                p.fatal("at least one import is required");
+            }
+            from_idents = some(from_idents_);
+          }
+
           _ { p.fatal("expecting an identifier, or '*'"); }
         }
     }
     alt def_ident {
       some(i) {
-        if glob { p.fatal("globbed imports can't be renamed"); }
+        if glob {
+            p.fatal("globbed imports can't be renamed");
+        }
+        if option::is_some(from_idents) {
+            p.fatal("can't rename import list");
+        }
         ret ast::view_item_import(i, identifiers, p.get_id());
       }
       _ {
         if glob {
             ret ast::view_item_import_glob(identifiers, p.get_id());
+        } else if option::is_some(from_idents) {
+            ret ast::view_item_import_from(identifiers,
+                                           option::get(from_idents),
+                                           p.get_id());
         } else {
             let len = vec::len(identifiers);
             ret ast::view_item_import(identifiers.(len - 1u), identifiers,
diff --git a/src/comp/syntax/print/pprust.rs b/src/comp/syntax/print/pprust.rs
index 43ab6c5c823..a8f694e6755 100644
--- a/src/comp/syntax/print/pprust.rs
+++ b/src/comp/syntax/print/pprust.rs
@@ -1267,6 +1267,19 @@ fn print_view_item(s: &ps, item: &@ast::view_item) {
             word(s.s, elt);
         }
       }
+      ast::view_item_import_from(mod_path, idents, _) {
+        head(s, "import");
+        for elt: str in mod_path {
+            word(s.s, elt);
+            word(s.s, "::");
+        }
+        word(s.s, "{");
+        commasep(s, inconsistent, idents,
+                 fn(s: &ps, w: &ast::import_ident) {
+                     word(s.s, w.node.name)
+                 });
+        word(s.s, "}");
+      }
       ast::view_item_import_glob(ids, _) {
         head(s, "import");
         let first = true;