diff options
author | uHOOCCOOHu <[email protected]> | 2019-09-29 23:52:15 +0100 |
---|---|---|
committer | uHOOCCOOHu <[email protected]> | 2019-10-02 19:28:02 +0100 |
commit | b1ed887d813bf5775a16624694939fdf836f97b1 (patch) | |
tree | b69daa2ca7d29a3362e9608ad0596de18caffa18 /crates/ra_cfg/src/cfg_expr.rs | |
parent | ffe179a73663b111e4b3ee8a3f525fb3e461c78e (diff) |
Introduce ra_cfg to parse and evaluate CfgExpr
Diffstat (limited to 'crates/ra_cfg/src/cfg_expr.rs')
-rw-r--r-- | crates/ra_cfg/src/cfg_expr.rs | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/crates/ra_cfg/src/cfg_expr.rs b/crates/ra_cfg/src/cfg_expr.rs new file mode 100644 index 000000000..efeadf462 --- /dev/null +++ b/crates/ra_cfg/src/cfg_expr.rs | |||
@@ -0,0 +1,128 @@ | |||
1 | use std::slice::Iter as SliceIter; | ||
2 | |||
3 | use ra_syntax::SmolStr; | ||
4 | use tt::{Leaf, Subtree, TokenTree}; | ||
5 | |||
6 | #[derive(Debug, Clone, PartialEq, Eq)] | ||
7 | pub enum CfgExpr { | ||
8 | Invalid, | ||
9 | Atom(SmolStr), | ||
10 | KeyValue { key: SmolStr, value: SmolStr }, | ||
11 | All(Vec<CfgExpr>), | ||
12 | Any(Vec<CfgExpr>), | ||
13 | Not(Box<CfgExpr>), | ||
14 | } | ||
15 | |||
16 | impl CfgExpr { | ||
17 | /// Fold the cfg by querying all basic `Atom` and `KeyValue` predicates. | ||
18 | pub fn fold(&self, query: &impl Fn(&SmolStr, Option<&SmolStr>) -> bool) -> Option<bool> { | ||
19 | match self { | ||
20 | CfgExpr::Invalid => None, | ||
21 | CfgExpr::Atom(name) => Some(query(name, None)), | ||
22 | CfgExpr::KeyValue { key, value } => Some(query(key, Some(value))), | ||
23 | CfgExpr::All(preds) => { | ||
24 | preds.iter().try_fold(true, |s, pred| Some(s && pred.fold(query)?)) | ||
25 | } | ||
26 | CfgExpr::Any(preds) => { | ||
27 | preds.iter().try_fold(false, |s, pred| Some(s || pred.fold(query)?)) | ||
28 | } | ||
29 | CfgExpr::Not(pred) => pred.fold(query).map(|s| !s), | ||
30 | } | ||
31 | } | ||
32 | } | ||
33 | |||
34 | pub fn parse_cfg(tt: &Subtree) -> CfgExpr { | ||
35 | next_cfg_expr(&mut tt.token_trees.iter()).unwrap_or(CfgExpr::Invalid) | ||
36 | } | ||
37 | |||
38 | fn next_cfg_expr(it: &mut SliceIter<tt::TokenTree>) -> Option<CfgExpr> { | ||
39 | let name = match it.next() { | ||
40 | None => return None, | ||
41 | Some(TokenTree::Leaf(Leaf::Ident(ident))) => ident.text.clone(), | ||
42 | Some(_) => return Some(CfgExpr::Invalid), | ||
43 | }; | ||
44 | |||
45 | // Peek | ||
46 | let ret = match it.as_slice().first() { | ||
47 | Some(TokenTree::Leaf(Leaf::Punct(punct))) if punct.char == '=' => { | ||
48 | match it.as_slice().get(1) { | ||
49 | Some(TokenTree::Leaf(Leaf::Literal(literal))) => { | ||
50 | it.next(); | ||
51 | it.next(); | ||
52 | // FIXME: escape? raw string? | ||
53 | let value = | ||
54 | SmolStr::new(literal.text.trim_start_matches('"').trim_end_matches('"')); | ||
55 | CfgExpr::KeyValue { key: name, value } | ||
56 | } | ||
57 | _ => return Some(CfgExpr::Invalid), | ||
58 | } | ||
59 | } | ||
60 | Some(TokenTree::Subtree(subtree)) => { | ||
61 | it.next(); | ||
62 | let mut sub_it = subtree.token_trees.iter(); | ||
63 | let mut subs = std::iter::from_fn(|| next_cfg_expr(&mut sub_it)).collect(); | ||
64 | match name.as_str() { | ||
65 | "all" => CfgExpr::All(subs), | ||
66 | "any" => CfgExpr::Any(subs), | ||
67 | "not" => CfgExpr::Not(Box::new(subs.pop().unwrap_or(CfgExpr::Invalid))), | ||
68 | _ => CfgExpr::Invalid, | ||
69 | } | ||
70 | } | ||
71 | _ => CfgExpr::Atom(name), | ||
72 | }; | ||
73 | |||
74 | // Eat comma separator | ||
75 | if let Some(TokenTree::Leaf(Leaf::Punct(punct))) = it.as_slice().first() { | ||
76 | if punct.char == ',' { | ||
77 | it.next(); | ||
78 | } | ||
79 | } | ||
80 | Some(ret) | ||
81 | } | ||
82 | |||
83 | #[cfg(test)] | ||
84 | mod tests { | ||
85 | use super::*; | ||
86 | |||
87 | use mbe::ast_to_token_tree; | ||
88 | use ra_syntax::ast::{self, AstNode}; | ||
89 | |||
90 | fn assert_parse_result(input: &str, expected: CfgExpr) { | ||
91 | let source_file = ast::SourceFile::parse(input).ok().unwrap(); | ||
92 | let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap(); | ||
93 | let (tt, _) = ast_to_token_tree(&tt).unwrap(); | ||
94 | assert_eq!(parse_cfg(&tt), expected); | ||
95 | } | ||
96 | |||
97 | #[test] | ||
98 | fn test_cfg_expr_parser() { | ||
99 | assert_parse_result("#![cfg(foo)]", CfgExpr::Atom("foo".into())); | ||
100 | assert_parse_result("#![cfg(foo,)]", CfgExpr::Atom("foo".into())); | ||
101 | assert_parse_result( | ||
102 | "#![cfg(not(foo))]", | ||
103 | CfgExpr::Not(Box::new(CfgExpr::Atom("foo".into()))), | ||
104 | ); | ||
105 | assert_parse_result("#![cfg(foo(bar))]", CfgExpr::Invalid); | ||
106 | |||
107 | // Only take the first | ||
108 | assert_parse_result(r#"#![cfg(foo, bar = "baz")]"#, CfgExpr::Atom("foo".into())); | ||
109 | |||
110 | assert_parse_result( | ||
111 | r#"#![cfg(all(foo, bar = "baz"))]"#, | ||
112 | CfgExpr::All(vec![ | ||
113 | CfgExpr::Atom("foo".into()), | ||
114 | CfgExpr::KeyValue { key: "bar".into(), value: "baz".into() }, | ||
115 | ]), | ||
116 | ); | ||
117 | |||
118 | assert_parse_result( | ||
119 | r#"#![cfg(any(not(), all(), , bar = "baz",))]"#, | ||
120 | CfgExpr::Any(vec![ | ||
121 | CfgExpr::Not(Box::new(CfgExpr::Invalid)), | ||
122 | CfgExpr::All(vec![]), | ||
123 | CfgExpr::Invalid, | ||
124 | CfgExpr::KeyValue { key: "bar".into(), value: "baz".into() }, | ||
125 | ]), | ||
126 | ); | ||
127 | } | ||
128 | } | ||