From a1c187eef3ba08076aedb5154929f7eda8d1b424 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 12 Aug 2020 18:26:51 +0200 Subject: Rename ra_syntax -> syntax --- crates/ra_cfg/src/cfg_expr.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_cfg/src/cfg_expr.rs') diff --git a/crates/ra_cfg/src/cfg_expr.rs b/crates/ra_cfg/src/cfg_expr.rs index f48928aee..940091465 100644 --- a/crates/ra_cfg/src/cfg_expr.rs +++ b/crates/ra_cfg/src/cfg_expr.rs @@ -4,7 +4,7 @@ use std::slice::Iter as SliceIter; -use ra_syntax::SmolStr; +use syntax::SmolStr; #[derive(Debug, Clone, PartialEq, Eq)] pub enum CfgExpr { @@ -87,7 +87,7 @@ mod tests { use super::*; use mbe::{ast_to_token_tree, TokenMap}; - use ra_syntax::ast::{self, AstNode}; + use syntax::ast::{self, AstNode}; fn get_token_tree_generated(input: &str) -> (tt::Subtree, TokenMap) { let source_file = ast::SourceFile::parse(input).ok().unwrap(); -- cgit v1.2.3 From 2f45cfc415626cfae5cba89c88a25fb3225486f7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 13 Aug 2020 10:08:11 +0200 Subject: Rename ra_mbe -> mbe --- crates/ra_cfg/src/cfg_expr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_cfg/src/cfg_expr.rs') diff --git a/crates/ra_cfg/src/cfg_expr.rs b/crates/ra_cfg/src/cfg_expr.rs index 940091465..fc93730d9 100644 --- a/crates/ra_cfg/src/cfg_expr.rs +++ b/crates/ra_cfg/src/cfg_expr.rs @@ -4,7 +4,7 @@ use std::slice::Iter as SliceIter; -use syntax::SmolStr; +use tt::SmolStr; #[derive(Debug, Clone, PartialEq, Eq)] pub enum CfgExpr { -- cgit v1.2.3 From 5734cc85868bf5fe2e4e023b40913b2063a78e31 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 13 Aug 2020 10:17:59 +0200 Subject: Simplify --- crates/ra_cfg/src/cfg_expr.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'crates/ra_cfg/src/cfg_expr.rs') diff --git a/crates/ra_cfg/src/cfg_expr.rs b/crates/ra_cfg/src/cfg_expr.rs index fc93730d9..336fe25bc 100644 --- a/crates/ra_cfg/src/cfg_expr.rs +++ b/crates/ra_cfg/src/cfg_expr.rs @@ -86,17 +86,15 @@ fn next_cfg_expr(it: &mut SliceIter) -> Option { mod tests { use super::*; - use mbe::{ast_to_token_tree, TokenMap}; + use mbe::ast_to_token_tree; use syntax::ast::{self, AstNode}; - fn get_token_tree_generated(input: &str) -> (tt::Subtree, TokenMap) { - let source_file = ast::SourceFile::parse(input).ok().unwrap(); - let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); - ast_to_token_tree(&tt).unwrap() - } - fn assert_parse_result(input: &str, expected: CfgExpr) { - let (tt, _) = get_token_tree_generated(input); + let (tt, _) = { + let source_file = ast::SourceFile::parse(input).ok().unwrap(); + let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); + ast_to_token_tree(&tt).unwrap() + }; let cfg = CfgExpr::parse(&tt); assert_eq!(cfg, expected); } -- cgit v1.2.3 From 68c223872562a8d746d4f1045d508887a0cbca5e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 13 Aug 2020 10:19:09 +0200 Subject: Rename ra_cfg -> cfg --- crates/ra_cfg/src/cfg_expr.rs | 133 ------------------------------------------ 1 file changed, 133 deletions(-) delete mode 100644 crates/ra_cfg/src/cfg_expr.rs (limited to 'crates/ra_cfg/src/cfg_expr.rs') diff --git a/crates/ra_cfg/src/cfg_expr.rs b/crates/ra_cfg/src/cfg_expr.rs deleted file mode 100644 index 336fe25bc..000000000 --- a/crates/ra_cfg/src/cfg_expr.rs +++ /dev/null @@ -1,133 +0,0 @@ -//! The condition expression used in `#[cfg(..)]` attributes. -//! -//! See: https://doc.rust-lang.org/reference/conditional-compilation.html#conditional-compilation - -use std::slice::Iter as SliceIter; - -use tt::SmolStr; - -#[derive(Debug, Clone, PartialEq, Eq)] -pub enum CfgExpr { - Invalid, - Atom(SmolStr), - KeyValue { key: SmolStr, value: SmolStr }, - All(Vec), - Any(Vec), - Not(Box), -} - -impl CfgExpr { - pub fn parse(tt: &tt::Subtree) -> CfgExpr { - next_cfg_expr(&mut tt.token_trees.iter()).unwrap_or(CfgExpr::Invalid) - } - /// Fold the cfg by querying all basic `Atom` and `KeyValue` predicates. - pub fn fold(&self, query: &dyn Fn(&SmolStr, Option<&SmolStr>) -> bool) -> Option { - match self { - CfgExpr::Invalid => None, - CfgExpr::Atom(name) => Some(query(name, None)), - CfgExpr::KeyValue { key, value } => Some(query(key, Some(value))), - CfgExpr::All(preds) => { - preds.iter().try_fold(true, |s, pred| Some(s && pred.fold(query)?)) - } - CfgExpr::Any(preds) => { - preds.iter().try_fold(false, |s, pred| Some(s || pred.fold(query)?)) - } - CfgExpr::Not(pred) => pred.fold(query).map(|s| !s), - } - } -} - -fn next_cfg_expr(it: &mut SliceIter) -> Option { - let name = match it.next() { - None => return None, - Some(tt::TokenTree::Leaf(tt::Leaf::Ident(ident))) => ident.text.clone(), - Some(_) => return Some(CfgExpr::Invalid), - }; - - // Peek - let ret = match it.as_slice().first() { - Some(tt::TokenTree::Leaf(tt::Leaf::Punct(punct))) if punct.char == '=' => { - match it.as_slice().get(1) { - Some(tt::TokenTree::Leaf(tt::Leaf::Literal(literal))) => { - it.next(); - it.next(); - // FIXME: escape? raw string? - let value = - SmolStr::new(literal.text.trim_start_matches('"').trim_end_matches('"')); - CfgExpr::KeyValue { key: name, value } - } - _ => return Some(CfgExpr::Invalid), - } - } - Some(tt::TokenTree::Subtree(subtree)) => { - it.next(); - let mut sub_it = subtree.token_trees.iter(); - let mut subs = std::iter::from_fn(|| next_cfg_expr(&mut sub_it)).collect(); - match name.as_str() { - "all" => CfgExpr::All(subs), - "any" => CfgExpr::Any(subs), - "not" => CfgExpr::Not(Box::new(subs.pop().unwrap_or(CfgExpr::Invalid))), - _ => CfgExpr::Invalid, - } - } - _ => CfgExpr::Atom(name), - }; - - // Eat comma separator - if let Some(tt::TokenTree::Leaf(tt::Leaf::Punct(punct))) = it.as_slice().first() { - if punct.char == ',' { - it.next(); - } - } - Some(ret) -} - -#[cfg(test)] -mod tests { - use super::*; - - use mbe::ast_to_token_tree; - use syntax::ast::{self, AstNode}; - - fn assert_parse_result(input: &str, expected: CfgExpr) { - let (tt, _) = { - let source_file = ast::SourceFile::parse(input).ok().unwrap(); - let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); - ast_to_token_tree(&tt).unwrap() - }; - let cfg = CfgExpr::parse(&tt); - assert_eq!(cfg, expected); - } - - #[test] - fn test_cfg_expr_parser() { - assert_parse_result("#![cfg(foo)]", CfgExpr::Atom("foo".into())); - assert_parse_result("#![cfg(foo,)]", CfgExpr::Atom("foo".into())); - assert_parse_result( - "#![cfg(not(foo))]", - CfgExpr::Not(Box::new(CfgExpr::Atom("foo".into()))), - ); - assert_parse_result("#![cfg(foo(bar))]", CfgExpr::Invalid); - - // Only take the first - assert_parse_result(r#"#![cfg(foo, bar = "baz")]"#, CfgExpr::Atom("foo".into())); - - assert_parse_result( - r#"#![cfg(all(foo, bar = "baz"))]"#, - CfgExpr::All(vec![ - CfgExpr::Atom("foo".into()), - CfgExpr::KeyValue { key: "bar".into(), value: "baz".into() }, - ]), - ); - - assert_parse_result( - r#"#![cfg(any(not(), all(), , bar = "baz",))]"#, - CfgExpr::Any(vec![ - CfgExpr::Not(Box::new(CfgExpr::Invalid)), - CfgExpr::All(vec![]), - CfgExpr::Invalid, - CfgExpr::KeyValue { key: "bar".into(), value: "baz".into() }, - ]), - ); - } -} -- cgit v1.2.3