Skip to content

Commit

Permalink
Improve whitespace handling around plus/minus operators.
Browse files Browse the repository at this point in the history
  • Loading branch information
kaj committed Sep 18, 2022
1 parent 5eb018f commit 082f935
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 51 deletions.
4 changes: 2 additions & 2 deletions src/parser/css_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn sum_expression(input: Span) -> PResult<Value> {
value(Operator::Plus, tag("+")),
value(Operator::Minus, tag("-")),
)),
map(multispace0, |s: Span| !s.fragment().is_empty()),
ignore_comments,
term,
)),
tuple((
Expand All @@ -42,7 +42,7 @@ fn sum_expression(input: Span) -> PResult<Value> {
value(Operator::Plus, tag("+")),
value(Operator::Minus, terminated(tag("-"), spacelike2)),
)),
alt((value(true, spacelike2), value(false, tag("")))),
ignore_comments,
term,
)),
))(rest)
Expand Down
8 changes: 4 additions & 4 deletions src/parser/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn spacelike(input: Span) -> PResult<()> {
}

pub fn spacelike2(input: Span) -> PResult<()> {
terminated(spacelike, ignore_comments)(input)
map_res(ignore_comments, |s| if s { Ok(()) } else { Err(()) })(input)
}

pub fn opt_spacelike(input: Span) -> PResult<()> {
Expand All @@ -31,11 +31,11 @@ pub fn opt_spacelike(input: Span) -> PResult<()> {
)
}

pub fn ignore_comments(input: Span) -> PResult<()> {
pub fn ignore_comments(input: Span) -> PResult<bool> {
fold_many0(
alt((ignore_space, ignore_lcomment, map(comment, |_| ()))),
|| (),
|(), ()| (),
|| false,
|_, ()| true,
)(input)
}

Expand Down
84 changes: 45 additions & 39 deletions src/parser/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::strings::{
special_function_misc, special_url, var_name,
};
use super::unit::unit;
use super::util::{ignore_comments, opt_spacelike, spacelike2};
use super::util::{comment, ignore_comments, opt_spacelike, spacelike2};
use super::{input_to_string, sass_string, PResult, SourcePos, Span};
use crate::sass::{SassString, Value};
use crate::value::{ListSeparator, Number, Numeric, Operator, Rgba};
Expand Down Expand Up @@ -135,51 +135,57 @@ fn logic_expression(input: Span) -> PResult<Value> {
}

fn sum_expression(input: Span) -> PResult<Value> {
let (mut rest, mut v) = term_value(input)?;
while let Ok((nrest, (s1, op, s2, v2))) = alt((
tuple((
value(false, tag("")),
alt((
value(Operator::Plus, tag("+")),
value(Operator::Minus, tag("-")),
let (rest, v) = term_value(input)?;
fold_many0(
alt((
tuple((
map(opt(comment), |_| false),
alt((
value(Operator::Plus, tag("+")),
value(Operator::Minus, tag("-")),
)),
ignore_comments,
term_value,
)),
map(multispace0, |s: Span| !s.fragment().is_empty()),
term_value,
)),
tuple((
value(true, spacelike2),
alt((
value(Operator::Plus, tag("+")),
value(Operator::Minus, terminated(tag("-"), spacelike2)),
tuple((
ignore_comments,
alt((
value(Operator::Plus, tag("+")),
value(Operator::Minus, terminated(tag("-"), spacelike2)),
)),
ignore_comments,
term_value,
)),
alt((value(true, spacelike2), value(false, tag("")))),
term_value,
)),
))(rest)
{
v = Value::BinOp(Box::new(v), s1, op, s2, Box::new(v2));
rest = nrest;
}
Ok((rest, v))
move || v.clone(),
|v, (s1, op, s2, v2)| {
let s1 = s1 && s2;
Value::BinOp(Box::new(v), s1, op, s2, Box::new(v2))
},
)(rest)
}

fn term_value(input: Span) -> PResult<Value> {
let (mut rest, mut v) = single_value(input)?;
while let Ok((nrest, (s1, op, s2, v2))) = tuple((
map(multispace0, |s: Span| !s.fragment().is_empty()),
alt((
value(Operator::Multiply, tag("*")),
value(Operator::Div, terminated(tag("/"), peek(not(tag("/"))))),
value(Operator::Modulo, tag("%")),
let (rest, v) = single_value(input)?;
fold_many0(
tuple((
ignore_comments,
alt((
value(Operator::Multiply, tag("*")),
value(
Operator::Div,
terminated(tag("/"), peek(not(tag("/")))),
),
value(Operator::Modulo, tag("%")),
)),
ignore_comments,
single_value,
)),
map(multispace0, |s: Span| !s.fragment().is_empty()),
single_value,
))(rest)
{
rest = nrest;
v = Value::BinOp(Box::new(v), s1, op, s2, Box::new(v2));
}
Ok((rest, v))
move || v.clone(),
|v1, (s1, op, s2, v2)| {
Value::BinOp(Box::new(v1), s1, op, s2, Box::new(v2))
},
)(rest)
}

pub fn single_value(input: Span) -> PResult<Value> {
Expand Down
3 changes: 0 additions & 3 deletions tests/spec/operators/minus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ mod syntax {
use super::runner;

#[test]
#[ignore] // wrong result
fn both() {
assert_eq!(
runner().ok("a {b: c/**/-/**/d}\n"),
Expand All @@ -24,7 +23,6 @@ mod syntax {
);
}
#[test]
#[ignore] // wrong result
fn left() {
assert_eq!(
runner().ok("a {b: c/**/-(d)}\n"),
Expand All @@ -34,7 +32,6 @@ mod syntax {
);
}
#[test]
#[ignore] // wrong result
fn right() {
assert_eq!(
runner().ok("a {b: (c)-/**/d}\n"),
Expand Down
3 changes: 0 additions & 3 deletions tests/spec/operators/plus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ mod syntax {
use super::runner;

#[test]
#[ignore] // wrong result
fn both() {
assert_eq!(
runner().ok("a {b: c/**/+/**/d}\n"),
Expand All @@ -24,7 +23,6 @@ mod syntax {
);
}
#[test]
#[ignore] // wrong result
fn left() {
assert_eq!(
runner().ok("a {b: c/**/+d}\n"),
Expand All @@ -34,7 +32,6 @@ mod syntax {
);
}
#[test]
#[ignore] // wrong result
fn right() {
assert_eq!(
runner().ok("a {b: c+/**/d}\n"),
Expand Down

0 comments on commit 082f935

Please sign in to comment.