aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_cfg/src/cfg_expr.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_cfg/src/cfg_expr.rs')
-rw-r--r--crates/ra_cfg/src/cfg_expr.rs21
1 files changed, 10 insertions, 11 deletions
diff --git a/crates/ra_cfg/src/cfg_expr.rs b/crates/ra_cfg/src/cfg_expr.rs
index 85b100c6a..f48928aee 100644
--- a/crates/ra_cfg/src/cfg_expr.rs
+++ b/crates/ra_cfg/src/cfg_expr.rs
@@ -5,7 +5,6 @@
5use std::slice::Iter as SliceIter; 5use std::slice::Iter as SliceIter;
6 6
7use ra_syntax::SmolStr; 7use ra_syntax::SmolStr;
8use tt::{Leaf, Subtree, TokenTree};
9 8
10#[derive(Debug, Clone, PartialEq, Eq)] 9#[derive(Debug, Clone, PartialEq, Eq)]
11pub enum CfgExpr { 10pub enum CfgExpr {
@@ -18,6 +17,9 @@ pub enum CfgExpr {
18} 17}
19 18
20impl CfgExpr { 19impl CfgExpr {
20 pub fn parse(tt: &tt::Subtree) -> CfgExpr {
21 next_cfg_expr(&mut tt.token_trees.iter()).unwrap_or(CfgExpr::Invalid)
22 }
21 /// Fold the cfg by querying all basic `Atom` and `KeyValue` predicates. 23 /// Fold the cfg by querying all basic `Atom` and `KeyValue` predicates.
22 pub fn fold(&self, query: &dyn Fn(&SmolStr, Option<&SmolStr>) -> bool) -> Option<bool> { 24 pub fn fold(&self, query: &dyn Fn(&SmolStr, Option<&SmolStr>) -> bool) -> Option<bool> {
23 match self { 25 match self {
@@ -35,22 +37,18 @@ impl CfgExpr {
35 } 37 }
36} 38}
37 39
38pub fn parse_cfg(tt: &Subtree) -> CfgExpr {
39 next_cfg_expr(&mut tt.token_trees.iter()).unwrap_or(CfgExpr::Invalid)
40}
41
42fn next_cfg_expr(it: &mut SliceIter<tt::TokenTree>) -> Option<CfgExpr> { 40fn next_cfg_expr(it: &mut SliceIter<tt::TokenTree>) -> Option<CfgExpr> {
43 let name = match it.next() { 41 let name = match it.next() {
44 None => return None, 42 None => return None,
45 Some(TokenTree::Leaf(Leaf::Ident(ident))) => ident.text.clone(), 43 Some(tt::TokenTree::Leaf(tt::Leaf::Ident(ident))) => ident.text.clone(),
46 Some(_) => return Some(CfgExpr::Invalid), 44 Some(_) => return Some(CfgExpr::Invalid),
47 }; 45 };
48 46
49 // Peek 47 // Peek
50 let ret = match it.as_slice().first() { 48 let ret = match it.as_slice().first() {
51 Some(TokenTree::Leaf(Leaf::Punct(punct))) if punct.char == '=' => { 49 Some(tt::TokenTree::Leaf(tt::Leaf::Punct(punct))) if punct.char == '=' => {
52 match it.as_slice().get(1) { 50 match it.as_slice().get(1) {
53 Some(TokenTree::Leaf(Leaf::Literal(literal))) => { 51 Some(tt::TokenTree::Leaf(tt::Leaf::Literal(literal))) => {
54 it.next(); 52 it.next();
55 it.next(); 53 it.next();
56 // FIXME: escape? raw string? 54 // FIXME: escape? raw string?
@@ -61,7 +59,7 @@ fn next_cfg_expr(it: &mut SliceIter<tt::TokenTree>) -> Option<CfgExpr> {
61 _ => return Some(CfgExpr::Invalid), 59 _ => return Some(CfgExpr::Invalid),
62 } 60 }
63 } 61 }
64 Some(TokenTree::Subtree(subtree)) => { 62 Some(tt::TokenTree::Subtree(subtree)) => {
65 it.next(); 63 it.next();
66 let mut sub_it = subtree.token_trees.iter(); 64 let mut sub_it = subtree.token_trees.iter();
67 let mut subs = std::iter::from_fn(|| next_cfg_expr(&mut sub_it)).collect(); 65 let mut subs = std::iter::from_fn(|| next_cfg_expr(&mut sub_it)).collect();
@@ -76,7 +74,7 @@ fn next_cfg_expr(it: &mut SliceIter<tt::TokenTree>) -> Option<CfgExpr> {
76 }; 74 };
77 75
78 // Eat comma separator 76 // Eat comma separator
79 if let Some(TokenTree::Leaf(Leaf::Punct(punct))) = it.as_slice().first() { 77 if let Some(tt::TokenTree::Leaf(tt::Leaf::Punct(punct))) = it.as_slice().first() {
80 if punct.char == ',' { 78 if punct.char == ',' {
81 it.next(); 79 it.next();
82 } 80 }
@@ -99,7 +97,8 @@ mod tests {
99 97
100 fn assert_parse_result(input: &str, expected: CfgExpr) { 98 fn assert_parse_result(input: &str, expected: CfgExpr) {
101 let (tt, _) = get_token_tree_generated(input); 99 let (tt, _) = get_token_tree_generated(input);
102 assert_eq!(parse_cfg(&tt), expected); 100 let cfg = CfgExpr::parse(&tt);
101 assert_eq!(cfg, expected);
103 } 102 }
104 103
105 #[test] 104 #[test]