Skip to content

Commit

Permalink
Simplify redundant parens.
Browse files Browse the repository at this point in the history
  • Loading branch information
emilio committed Oct 4, 2021
1 parent 39ae91c commit 2c1abf3
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/parser.rs
Expand Up @@ -30,7 +30,7 @@ pub enum Condition {
fn simplify_and_or(conds: &mut Vec<Condition>, redundant_value: bool) -> Option<Condition> {
let mut discarded = false;
conds.retain_mut(|cond| {
cond.simplify();
cond.simplify_internal();
let known = cond.known_value();
discarded |= known == Some(!redundant_value);
known != Some(redundant_value)
Expand All @@ -43,6 +43,10 @@ fn simplify_and_or(conds: &mut Vec<Condition>, redundant_value: bool) -> Option<
Some(Condition::Simple(
if redundant_value { "true" } else { "false" }.into(),
))
} else if conds.len() == 1 {
let mut cond = conds.drain(..).next().unwrap();
cond.peel_parens();
Some(cond)
} else {
None
}
Expand Down Expand Up @@ -92,17 +96,29 @@ impl Condition {
}
}

fn peel_parens(&mut self) {
while let Self::Paren(ref mut inner) = *self {
let inner = std::mem::replace(&mut **inner, Condition::Simple(String::new()));
*self = inner;
}
}

fn simplify(&mut self) {
self.simplify_internal();
self.peel_parens();
}

fn simplify_internal(&mut self) {
match *self {
Self::Simple(..) => return,
Self::Neg(ref mut inner) => {
inner.simplify();
inner.simplify_internal();
if let Some(v) = inner.known_value() {
*self = Self::Simple(if v { "false" } else { "true" }.into())
}
}
Self::Paren(ref mut inner) => {
inner.simplify();
inner.simplify_internal();
if let Some(v) = inner.known_value() {
*self = Self::Simple(if v { "true" } else { "false" }.into())
} else if let Self::Simple(..) = **inner {
Expand Down Expand Up @@ -332,7 +348,7 @@ enum IsRedundant {

fn simplify_cond(cond: &mut Option<Condition>) -> IsRedundant {
if let Some(ref mut c) = *cond {
c.simplify();
c.simplify_internal();
match c.known_value() {
Some(true) => {}
Some(false) => return IsRedundant::Yes,
Expand Down Expand Up @@ -959,4 +975,15 @@ fn test_simplify() {
"fails-if(somethingElse) fails-if(Android||true) == foo.html bar.html",
"fails == foo.html bar.html"
);

test_simplified_line!(
"skip-if((something||otherthing)&&true) == foo.html bar.html",
"skip-if(something||otherthing) == foo.html bar.html"
);


test_simplified_line!(
"skip-if(isDebugBuild||(winWidget&&(!is64Bit))) == 256180-2.html 256180-2-ref.html",
"skip-if(isDebugBuild||(winWidget&&(!is64Bit))) == 256180-2.html 256180-2-ref.html"
);
}

0 comments on commit 2c1abf3

Please sign in to comment.