Skip to content

Commit

Permalink
auto merge of #19044 : murarth/rust/libsyntax-view-item, r=alexcrichton
Browse files Browse the repository at this point in the history
Allows parsing view items (`use` and `extern crate`) individually. Does not change behavior of any existing functions.

Closes #19024
  • Loading branch information
bors committed Nov 18, 2014
2 parents 1628b98 + 2293a04 commit e09d986
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/libsyntax/parse/mod.rs
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();

Expand Down
8 changes: 8 additions & 0 deletions src/libsyntax/parse/parser.rs
Expand Up @@ -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());
Expand Down
7 changes: 7 additions & 0 deletions src/libsyntax/util/parser_testing.rs
Expand Up @@ -67,6 +67,13 @@ pub fn string_to_stmt(source_str : String) -> P<ast::Stmt> {
})
}

/// Parse a string, return a view item
pub fn string_to_view_item (source_str : String) -> ast::ViewItem {
with_error_checking_parse(source_str, |p| {
p.parse_view_item(Vec::new())
})
}

/// Parse a string, return a pat. Uses "irrefutable"... which doesn't
/// (currently) affect parsing.
pub fn string_to_pat(source_str: String) -> P<ast::Pat> {
Expand Down

0 comments on commit e09d986

Please sign in to comment.