about summary refs log tree commit diff
path: root/src/libsyntax/parse
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-11-18 23:51:43 +0000
committerbors <bors@rust-lang.org>2014-11-18 23:51:43 +0000
commite09d98603e608c9e47d4c89f7b4dca87a4b56da3 (patch)
tree830bdec7e6fc4c19726398108bbf760a64ba0131 /src/libsyntax/parse
parent1628b98183bfec13e9407a905324dda047c3a550 (diff)
parent2293a04b4936a11fc3e09f3df8a8ab591e034f29 (diff)
downloadrust-e09d98603e608c9e47d4c89f7b4dca87a4b56da3.tar.gz
rust-e09d98603e608c9e47d4c89f7b4dca87a4b56da3.zip
auto merge of #19044 : murarth/rust/libsyntax-view-item, r=alexcrichton
Allows parsing view items (`use` and `extern crate`) individually. Does not change behavior of any existing functions.

Closes #19024
Diffstat (limited to 'src/libsyntax/parse')
-rw-r--r--src/libsyntax/parse/mod.rs27
-rw-r--r--src/libsyntax/parse/parser.rs8
2 files changed, 34 insertions, 1 deletions
diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs
index 2810db4eadd..3ce49b9d7a3 100644
--- a/src/libsyntax/parse/mod.rs
+++ b/src/libsyntax/parse/mod.rs
@@ -730,10 +730,11 @@ mod test {
     use attr::AttrMetaMethods;
     use parse::parser::Parser;
     use parse::token::{str_to_ident};
+    use print::pprust::view_item_to_string;
     use ptr::P;
     use util::parser_testing::{string_to_tts, string_to_parser};
     use util::parser_testing::{string_to_expr, string_to_item};
-    use util::parser_testing::string_to_stmt;
+    use util::parser_testing::{string_to_stmt, string_to_view_item};
 
     // produce a codemap::span
     fn sp(a: u32, b: u32) -> Span {
@@ -1083,6 +1084,30 @@ mod test {
                             span: sp(0,21)})));
     }
 
+    #[test] fn parse_use() {
+        let use_s = "use foo::bar::baz;";
+        let vitem = string_to_view_item(use_s.to_string());
+        let vitem_s = view_item_to_string(&vitem);
+        assert_eq!(vitem_s.as_slice(), use_s);
+
+        let use_s = "use foo::bar as baz;";
+        let vitem = string_to_view_item(use_s.to_string());
+        let vitem_s = view_item_to_string(&vitem);
+        assert_eq!(vitem_s.as_slice(), use_s);
+    }
+
+    #[test] fn parse_extern_crate() {
+        let ex_s = "extern crate foo;";
+        let vitem = string_to_view_item(ex_s.to_string());
+        let vitem_s = view_item_to_string(&vitem);
+        assert_eq!(vitem_s.as_slice(), ex_s);
+
+        let ex_s = "extern crate \"foo\" as bar;";
+        let vitem = string_to_view_item(ex_s.to_string());
+        let vitem_s = view_item_to_string(&vitem);
+        assert_eq!(vitem_s.as_slice(), ex_s);
+    }
+
     fn get_spans_of_pat_idents(src: &str) -> Vec<Span> {
         let item = string_to_item(src.to_string()).unwrap();
 
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index 40c4ac9f8c0..50b1a2204b0 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -5620,6 +5620,14 @@ impl<'a> Parser<'a> {
         }
     }
 
+    /// Parse a ViewItem, e.g. `use foo::bar` or `extern crate foo`
+    pub fn parse_view_item(&mut self, attrs: Vec<Attribute>) -> ViewItem {
+        match self.parse_item_or_view_item(attrs, false) {
+            IoviViewItem(vi) => vi,
+            _ => self.fatal("expected `use` or `extern crate`"),
+        }
+    }
+
     /// Parse, e.g., "use a::b::{z,y}"
     fn parse_use(&mut self) -> ViewItem_ {
         return ViewItemUse(self.parse_view_path());