| 1 |
// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 2 |
|
| 3 |
pub(crate) mod lexer; |
| 4 |
mod parser; |
| 5 |
|
| 6 |
use alloc::{string::String, vec::Vec}; |
| 7 |
use core::ops::Range; |
| 8 |
|
| 9 |
/// A predicate function, used to combine 1 or more predicates |
| 10 |
/// into a single value |
| 11 |
#[derive(Debug, Clone, Copy)] |
| 12 |
enum Func { |
| 13 |
/// `not()` with a configuration predicate. It is true if its predicate |
| 14 |
/// is false and false if its predicate is true. |
| 15 |
Not, |
| 16 |
/// `all()` with a comma separated list of configuration predicates. It |
| 17 |
/// is false if at least one predicate is false. If there are no predicates, |
| 18 |
/// it is true. |
| 19 |
/// |
| 20 |
/// The associated `usize` is the number of predicates inside the `all()`. |
| 21 |
All(usize), |
| 22 |
/// `any()` with a comma separated list of configuration predicates. It |
| 23 |
/// is true if at least one predicate is true. If there are no predicates, |
| 24 |
/// it is false. |
| 25 |
/// |
| 26 |
/// The associated `usize` is the number of predicates inside the `any()`. |
| 27 |
Any(usize), |
| 28 |
} |
| 29 |
|
| 30 |
/// A single predicate in a `cfg()` expression |
| 31 |
#[derive(Debug, PartialEq, Eq)] |
| 32 |
pub(crate) enum Predicate<'a> { |
| 33 |
/// A generic bare predicate key that doesn't match one of the known options, eg `cfg(bare)` |
| 34 |
Flag(&'a str), |
| 35 |
/// A generic key = "value" predicate that doesn't match one of the known options, eg `cfg(foo = "bar")` |
| 36 |
KeyValue { key: &'a str, val: &'a str }, |
| 37 |
} |
| 38 |
|
| 39 |
#[derive(Clone, Debug)] |
| 40 |
struct InnerPredicate { |
| 41 |
identifier: Range<usize>, |
| 42 |
value: Option<Range<usize>>, |
| 43 |
} |
| 44 |
|
| 45 |
impl InnerPredicate { |
| 46 |
fn to_pred<'a>(&self, s: &'a str) -> Predicate<'a> { |
| 47 |
match &self.value { |
| 48 |
Some(vs) => { |
| 49 |
Predicate::KeyValue { key: &s[self.identifier.clone()], val: &s[vs.clone()] } |
| 50 |
} |
| 51 |
None => Predicate::Flag(&s[self.identifier.clone()]), |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
#[derive(Clone, Debug)] |
| 57 |
enum ExprNode { |
| 58 |
Fn(Func), |
| 59 |
Predicate(InnerPredicate), |
| 60 |
} |
| 61 |
|
| 62 |
/// A parsed `cfg()` expression that can evaluated |
| 63 |
#[derive(Clone, Debug)] |
| 64 |
pub(crate) struct Expression { |
| 65 |
expr: Vec<ExprNode>, |
| 66 |
// We keep the original string around for providing the arbitrary |
| 67 |
// strings that can make up an expression |
| 68 |
original: String, |
| 69 |
} |
| 70 |
|
| 71 |
impl Expression { |
| 72 |
#[cfg(test)] |
| 73 |
/// An iterator over each predicate in the expression |
| 74 |
pub(crate) fn predicates(&self) -> impl Iterator<Item = Predicate<'_>> { |
| 75 |
self.expr.iter().filter_map(move |item| match item { |
| 76 |
ExprNode::Predicate(pred) => { |
| 77 |
let pred = pred.clone().to_pred(&self.original); |
| 78 |
Some(pred) |
| 79 |
} |
| 80 |
ExprNode::Fn(_) => None, |
| 81 |
}) |
| 82 |
} |
| 83 |
|
| 84 |
/// Evaluates the expression, using the provided closure to determine the value of |
| 85 |
/// each predicate, which are then combined into a final result depending on the |
| 86 |
/// functions not(), all(), or any() in the expression. |
| 87 |
/// |
| 88 |
/// `eval_predicate` typically returns `bool`, but may return any type that implements |
| 89 |
/// the `Logic` trait. |
| 90 |
pub(crate) fn eval<EP, T>(&self, mut eval_predicate: EP) -> T |
| 91 |
where |
| 92 |
EP: FnMut(&Predicate<'_>) -> T, |
| 93 |
T: Logic + core::fmt::Debug, |
| 94 |
{ |
| 95 |
let mut result_stack = Vec::with_capacity(8); |
| 96 |
|
| 97 |
// We store the expression as postfix, so just evaluate each license |
| 98 |
// requirement in the order it comes, and then combining the previous |
| 99 |
// results according to each operator as it comes |
| 100 |
for node in &self.expr { |
| 101 |
match node { |
| 102 |
ExprNode::Predicate(pred) => { |
| 103 |
let pred = pred.to_pred(&self.original); |
| 104 |
|
| 105 |
result_stack.push(eval_predicate(&pred)); |
| 106 |
} |
| 107 |
ExprNode::Fn(Func::All(count)) => { |
| 108 |
// all() with a comma separated list of configuration predicates. |
| 109 |
let mut result = T::top(); |
| 110 |
|
| 111 |
for _ in 0..*count { |
| 112 |
let r = result_stack.pop().unwrap(); |
| 113 |
result = result.and(r); |
| 114 |
} |
| 115 |
|
| 116 |
result_stack.push(result); |
| 117 |
} |
| 118 |
ExprNode::Fn(Func::Any(count)) => { |
| 119 |
// any() with a comma separated list of configuration predicates. |
| 120 |
let mut result = T::bottom(); |
| 121 |
|
| 122 |
for _ in 0..*count { |
| 123 |
let r = result_stack.pop().unwrap(); |
| 124 |
result = result.or(r); |
| 125 |
} |
| 126 |
|
| 127 |
result_stack.push(result); |
| 128 |
} |
| 129 |
ExprNode::Fn(Func::Not) => { |
| 130 |
// not() with a configuration predicate. |
| 131 |
// It is true if its predicate is false |
| 132 |
// and false if its predicate is true. |
| 133 |
let r = result_stack.pop().unwrap(); |
| 134 |
result_stack.push(r.not()); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
result_stack.pop().unwrap() |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
/// A propositional logic used to evaluate `Expression` instances. |
| 144 |
/// |
| 145 |
/// An `Expression` consists of some predicates and the `any`, `all` and `not` operators. An |
| 146 |
/// implementation of `Logic` defines how the `any`, `all` and `not` operators should be evaluated. |
| 147 |
pub(crate) trait Logic { |
| 148 |
/// The result of an `all` operation with no operands, akin to Boolean `true`. |
| 149 |
fn top() -> Self; |
| 150 |
|
| 151 |
/// The result of an `any` operation with no operands, akin to Boolean `false`. |
| 152 |
fn bottom() -> Self; |
| 153 |
|
| 154 |
/// `AND`, which corresponds to the `all` operator. |
| 155 |
fn and(self, other: Self) -> Self; |
| 156 |
|
| 157 |
/// `OR`, which corresponds to the `any` operator. |
| 158 |
fn or(self, other: Self) -> Self; |
| 159 |
|
| 160 |
/// `NOT`, which corresponds to the `not` operator. |
| 161 |
fn not(self) -> Self; |
| 162 |
} |
| 163 |
|
| 164 |
/// A boolean logic. |
| 165 |
impl Logic for bool { |
| 166 |
#[inline] |
| 167 |
fn top() -> Self { |
| 168 |
true |
| 169 |
} |
| 170 |
|
| 171 |
#[inline] |
| 172 |
fn bottom() -> Self { |
| 173 |
false |
| 174 |
} |
| 175 |
|
| 176 |
#[inline] |
| 177 |
fn and(self, other: Self) -> Self { |
| 178 |
self && other |
| 179 |
} |
| 180 |
|
| 181 |
#[inline] |
| 182 |
fn or(self, other: Self) -> Self { |
| 183 |
self || other |
| 184 |
} |
| 185 |
|
| 186 |
#[inline] |
| 187 |
fn not(self) -> Self { |
| 188 |
!self |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
/// A three-valued logic -- `None` stands for the value being unknown. |
| 193 |
/// |
| 194 |
/// The truth tables for this logic are described on |
| 195 |
/// [Wikipedia](https://en.wikipedia.org/wiki/Three-valued_logic#Kleene_and_Priest_logics). |
| 196 |
impl Logic for Option<bool> { |
| 197 |
#[inline] |
| 198 |
fn top() -> Self { |
| 199 |
Some(true) |
| 200 |
} |
| 201 |
|
| 202 |
#[inline] |
| 203 |
fn bottom() -> Self { |
| 204 |
Some(false) |
| 205 |
} |
| 206 |
|
| 207 |
#[inline] |
| 208 |
fn and(self, other: Self) -> Self { |
| 209 |
match (self, other) { |
| 210 |
// If either is false, the expression is false. |
| 211 |
(Some(false), _) | (_, Some(false)) => Some(false), |
| 212 |
// If both are true, the expression is true. |
| 213 |
(Some(true), Some(true)) => Some(true), |
| 214 |
// One or both are unknown -- the result is unknown. |
| 215 |
_ => None, |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
#[inline] |
| 220 |
fn or(self, other: Self) -> Self { |
| 221 |
match (self, other) { |
| 222 |
// If either is true, the expression is true. |
| 223 |
(Some(true), _) | (_, Some(true)) => Some(true), |
| 224 |
// If both are false, the expression is false. |
| 225 |
(Some(false), Some(false)) => Some(false), |
| 226 |
// One or both are unknown -- the result is unknown. |
| 227 |
_ => None, |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
#[inline] |
| 232 |
fn not(self) -> Self { |
| 233 |
self.map(|v| !v) |
| 234 |
} |
| 235 |
} |