]> git.lizzy.rs Git - rust.git/commitdiff
syntax: `Mod` records the span for inner contents.
authorKang Seonghoon <public+git@mearie.org>
Sat, 26 Apr 2014 20:05:45 +0000 (05:05 +0900)
committerKang Seonghoon <public+git@mearie.org>
Sun, 27 Apr 2014 05:52:30 +0000 (14:52 +0900)
this is useful when the module item and module contents are defined
from different files (like rustdoc). in most cases the original span
for the module item would be used; in other cases, the span for
module contents is available separately at the `inner` field.

src/librustc/front/config.rs
src/librustc/front/test.rs
src/libsyntax/ast.rs
src/libsyntax/ext/build.rs
src/libsyntax/fold.rs
src/libsyntax/parse/parser.rs

index 8dcc97c936cbbbb53cc5d08ab226bfb7251b7640..4fc33c2ad9b14036b8427b1daaf90190088aa24f 100644 (file)
@@ -70,6 +70,7 @@ fn fold_mod(cx: &mut Context, m: &ast::Mod) -> ast::Mod {
         filter_view_item(cx, a).map(|x| cx.fold_view_item(x))
     }).collect();
     ast::Mod {
+        inner: m.inner,
         view_items: filtered_view_items,
         items: flattened_items
     }
index 72b63ebc80d543d22a6be965908ef21024dd6f76..685714cd74a5a8f2fb00312984f52ed62eca4699 100644 (file)
@@ -143,6 +143,7 @@ fn nomain(cx: &TestCtxt, item: @ast::Item) -> @ast::Item {
         }
 
         let mod_nomain = ast::Mod {
+            inner: m.inner,
             view_items: m.view_items.clone(),
             items: m.items.iter().map(|i| nomain(&self.cx, *i)).collect(),
         };
@@ -335,6 +336,7 @@ pub fn main() {
     )).unwrap();
 
     let testmod = ast::Mod {
+        inner: DUMMY_SP,
         view_items: view_items,
         items: vec!(mainfn, tests),
     };
index ccb25239f6ce494097e2e1a8e785cdf13c0ce177..3afc4b0e118bcddfbbecc9fbe273492a7fb14581 100644 (file)
@@ -921,8 +921,12 @@ pub struct Method {
 
 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
 pub struct Mod {
-    pub view_items: Vec<ViewItem> ,
-    pub items: Vec<@Item> ,
+    /// A span from the first token past `{` to the last token until `}`.
+    /// For `mod foo;`, the inner span ranges from the first token
+    /// to the last token in the external file.
+    pub inner: Span,
+    pub view_items: Vec<ViewItem>,
+    pub items: Vec<@Item>,
 }
 
 #[deriving(Clone, Eq, TotalEq, Encodable, Decodable, Hash)]
@@ -1165,7 +1169,15 @@ mod test {
     fn check_asts_encodable() {
         use std::io;
         let e = Crate {
-            module: Mod {view_items: Vec::new(), items: Vec::new()},
+            module: Mod {
+                inner: Span {
+                    lo: BytePos(11),
+                    hi: BytePos(19),
+                    expn_info: None,
+                },
+                view_items: Vec::new(),
+                items: Vec::new(),
+            },
             attrs: Vec::new(),
             config: Vec::new(),
             span: Span {
index 1a160cb33aa768ca9c5c289c2aadd03531e78625..7e084ac263de40cd8c7166fac520a221b6fb52fd 100644 (file)
@@ -220,7 +220,7 @@ fn item_struct_poly(&self,
                         generics: Generics) -> @ast::Item;
     fn item_struct(&self, span: Span, name: Ident, struct_def: ast::StructDef) -> @ast::Item;
 
-    fn item_mod(&self, span: Span,
+    fn item_mod(&self, span: Span, inner_span: Span,
                 name: Ident, attrs: Vec<ast::Attribute> ,
                 vi: Vec<ast::ViewItem> , items: Vec<@ast::Item> ) -> @ast::Item;
 
@@ -898,7 +898,7 @@ fn item_struct_poly(&self, span: Span, name: Ident,
         self.item(span, name, Vec::new(), ast::ItemStruct(@struct_def, generics))
     }
 
-    fn item_mod(&self, span: Span, name: Ident,
+    fn item_mod(&self, span: Span, inner_span: Span, name: Ident,
                 attrs: Vec<ast::Attribute> ,
                 vi: Vec<ast::ViewItem> ,
                 items: Vec<@ast::Item> ) -> @ast::Item {
@@ -907,6 +907,7 @@ fn item_mod(&self, span: Span, name: Ident,
             name,
             attrs,
             ast::ItemMod(ast::Mod {
+                inner: inner_span,
                 view_items: vi,
                 items: items,
             })
index ae82a07601baf27182376555bff002b7b30495cf..1e21c0d09869cd4b5a685cba4804a0feb0b30a2a 100644 (file)
@@ -654,6 +654,7 @@ pub fn noop_fold_type_method<T: Folder>(m: &TypeMethod, fld: &mut T) -> TypeMeth
 
 pub fn noop_fold_mod<T: Folder>(m: &Mod, folder: &mut T) -> Mod {
     ast::Mod {
+        inner: folder.new_span(m.inner),
         view_items: m.view_items
                      .iter()
                      .map(|x| folder.fold_view_item(x)).collect(),
index 974077956d1c744fcab415bca23577bfc6975978..68dd38604d847f5bff6cd586ee91e9cb34b3e890 100644 (file)
@@ -4036,7 +4036,8 @@ fn parse_for_sized(&mut self) -> Sized {
     // attributes (of length 0 or 1), parse all of the items in a module
     fn parse_mod_items(&mut self,
                        term: token::Token,
-                       first_item_attrs: Vec<Attribute> )
+                       first_item_attrs: Vec<Attribute>,
+                       inner_lo: BytePos)
                        -> Mod {
         // parse all of the items up to closing or an attribute.
         // view items are legal here.
@@ -4081,7 +4082,11 @@ fn parse_mod_items(&mut self,
             self.span_err(self.last_span, "expected item after attributes");
         }
 
-        ast::Mod { view_items: view_items, items: items }
+        ast::Mod {
+            inner: mk_sp(inner_lo, self.span.lo),
+            view_items: view_items,
+            items: items
+        }
     }
 
     fn parse_item_const(&mut self) -> ItemInfo {
@@ -4107,8 +4112,9 @@ fn parse_item_mod(&mut self, outer_attrs: &[Attribute]) -> ItemInfo {
         } else {
             self.push_mod_path(id, outer_attrs);
             self.expect(&token::LBRACE);
+            let mod_inner_lo = self.span.lo;
             let (inner, next) = self.parse_inner_attrs_and_next();
-            let m = self.parse_mod_items(token::RBRACE, next);
+            let m = self.parse_mod_items(token::RBRACE, next, mod_inner_lo);
             self.expect(&token::RBRACE);
             self.pop_mod_path();
             (id, ItemMod(m), Some(inner))
@@ -4197,10 +4203,11 @@ fn eval_src_mod_from_path(&mut self,
                                      self.cfg.clone(),
                                      &path,
                                      id_sp);
+        let mod_inner_lo = p0.span.lo;
         let (inner, next) = p0.parse_inner_attrs_and_next();
         let mod_attrs = outer_attrs.append(inner.as_slice());
         let first_item_outer_attrs = next;
-        let m0 = p0.parse_mod_items(token::EOF, first_item_outer_attrs);
+        let m0 = p0.parse_mod_items(token::EOF, first_item_outer_attrs, mod_inner_lo);
         self.sess.included_mod_stack.borrow_mut().pop();
         return (ast::ItemMod(m0), mod_attrs);
     }
@@ -5061,7 +5068,7 @@ pub fn parse_crate_mod(&mut self) -> Crate {
         let (inner, next) = self.parse_inner_attrs_and_next();
         let first_item_outer_attrs = next;
         // parse the items inside the crate:
-        let m = self.parse_mod_items(token::EOF, first_item_outer_attrs);
+        let m = self.parse_mod_items(token::EOF, first_item_outer_attrs, lo);
 
         ast::Crate {
             module: m,