diff options
| author | Caleb Cartwright <caleb.cartwright@outlook.com> | 2020-03-26 21:25:34 -0500 |
|---|---|---|
| committer | Caleb Cartwright <caleb.cartwright@outlook.com> | 2020-03-26 21:25:34 -0500 |
| commit | bd5dff4012a73ad4c5eb5db1dbb111bd92ed31dd (patch) | |
| tree | b781aa6ed959466d2b6605ab844a2ba7fe56b6a6 /src/modules | |
| parent | 9699c96cf1e6a13e0cc12e17c55ca11d2f9a6bf2 (diff) | |
refactor: backport syntux mod
Diffstat (limited to 'src/modules')
| -rw-r--r-- | src/modules/visitor.rs | 67 |
1 files changed, 8 insertions, 59 deletions
diff --git a/src/modules/visitor.rs b/src/modules/visitor.rs index a2055b6f4ea..ad9111f7a9f 100644 --- a/src/modules/visitor.rs +++ b/src/modules/visitor.rs @@ -1,11 +1,10 @@ -use rustc_parse::{stream_to_parser_with_base_dir, Directory}; -use rustc_session::parse::ParseSess; -use rustc_span::{symbol::kw, Symbol}; +use rustc_span::Symbol; use syntax::ast; -use syntax::token::{DelimToken, TokenKind}; use syntax::visit::Visitor; use crate::attr::MetaVisitor; +use crate::syntux::parser::{Directory, Parser}; +use crate::syntux::session::ParseSess; pub(crate) struct ModItem { pub(crate) item: ast::Item, @@ -15,11 +14,11 @@ pub(crate) struct ModItem { pub(crate) struct CfgIfVisitor<'a> { parse_sess: &'a ParseSess, mods: Vec<ModItem>, - base_dir: Directory, + base_dir: &'a Directory, } impl<'a> CfgIfVisitor<'a> { - pub(crate) fn new(parse_sess: &'a ParseSess, base_dir: Directory) -> CfgIfVisitor<'a> { + pub(crate) fn new(parse_sess: &'a ParseSess, base_dir: &'a Directory) -> CfgIfVisitor<'a> { CfgIfVisitor { mods: vec![], parse_sess, @@ -65,59 +64,9 @@ impl<'a, 'ast: 'a> CfgIfVisitor<'a> { } }; - let ts = mac.args.inner_tokens(); - let mut parser = - stream_to_parser_with_base_dir(self.parse_sess, ts.clone(), self.base_dir.clone()); - parser.cfg_mods = false; - let mut process_if_cfg = true; - - while parser.token.kind != TokenKind::Eof { - if process_if_cfg { - if !parser.eat_keyword(kw::If) { - return Err("Expected `if`"); - } - parser - .parse_attribute(false) - .map_err(|_| "Failed to parse attributes")?; - } - - if !parser.eat(&TokenKind::OpenDelim(DelimToken::Brace)) { - return Err("Expected an opening brace"); - } - - while parser.token != TokenKind::CloseDelim(DelimToken::Brace) - && parser.token.kind != TokenKind::Eof - { - let item = match parser.parse_item() { - Ok(Some(item_ptr)) => item_ptr.into_inner(), - Ok(None) => continue, - Err(mut err) => { - err.cancel(); - parser.sess.span_diagnostic.reset_err_count(); - return Err( - "Expected item inside cfg_if block, but failed to parse it as an item", - ); - } - }; - if let ast::ItemKind::Mod(..) = item.kind { - self.mods.push(ModItem { item }); - } - } - - if !parser.eat(&TokenKind::CloseDelim(DelimToken::Brace)) { - return Err("Expected a closing brace"); - } - - if parser.eat(&TokenKind::Eof) { - break; - } - - if !parser.eat_keyword(kw::Else) { - return Err("Expected `else`"); - } - - process_if_cfg = parser.token.is_keyword(kw::If); - } + let items = Parser::parse_cfg_if(self.parse_sess, mac, &self.base_dir)?; + self.mods + .append(&mut items.into_iter().map(|item| ModItem { item }).collect()); Ok(()) } |
