Skip to content

Commit

Permalink
Update to nom 8.0 (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaj authored Jan 28, 2025
2 parents d39dd9e + 4090c62 commit 392adab
Show file tree
Hide file tree
Showing 32 changed files with 601 additions and 500 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ jobs:
rust:
- 1.70.0
- 1.68.2
- 1.63.0
- 1.65.0
steps:
- uses: actions/checkout@v4
- uses: actions-rust-lang/setup-rust-toolchain@v1
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ project adheres to
This removes integration with `num-rational`, `num-bigint`,
`num-integer` and `num-traits`.
* Improved `@for` loop evaluation and error handling (#206).
* Msrv is now 1.63.0 for rsass (and 1.74 for rsass-cli).
* Msrv is now 1.65.0 for rsass (and 1.74 for rsass-cli).

### Other changes:

Expand All @@ -36,6 +36,7 @@ project adheres to
Also allow "loud" comments in more places.
* Pure css `round()` may take an expression argument.
* Minor changes in agrument syntax errors.
* Updated `nom` to 8.0.0 and added `nom-language` 0.1.0.
* Updated sass-spec test suite to 2024-12-12.


Expand Down
2 changes: 1 addition & 1 deletion rsass-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repository = "https://github.com/kaj/rsass"
readme = "README.md"
license = "MIT OR Apache-2.0"
edition = "2021"
rust-version = "1.63.0"
rust-version = "1.65.0"

[lib]
proc-macro = true
Expand Down
5 changes: 3 additions & 2 deletions rsass/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ repository = "https://github.com/kaj/rsass"
readme = "README.md"
license = "MIT OR Apache-2.0"
edition = "2021"
rust-version = "1.63.0"
rust-version = "1.65.0"

[dependencies]
arc-swap = "1.5.0"
fastrand = "2.0"
lazy_static = "1.0"
nom = "7.0"
nom = "8.0"
nom-language = "0.1.0"
tracing = "0.1.34"

[badges]
Expand Down
10 changes: 6 additions & 4 deletions rsass/src/css/selectors/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,16 @@ pub(super) mod parser {
use nom::bytes::complete::tag;
use nom::character::complete::one_of;
use nom::combinator::{map, map_res, opt};
use nom::sequence::{delimited, pair, tuple};
use nom::sequence::{delimited, pair};
use nom::Parser as _;

pub(crate) fn attribute(input: Span) -> PResult<Attribute> {
map(
delimited(
term_opt_space(tag("[")),
pair(
term_opt_space(name_opt_ns),
opt(tuple((
opt((
term_opt_space(map_res(
alt((
tag("*="),
Expand All @@ -69,7 +70,7 @@ pub(super) mod parser {
"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
abcdefghijklmnopqrstuvwxyz",
))),
))),
)),
),
tag("]"),
),
Expand All @@ -90,6 +91,7 @@ pub(super) mod parser {
}
}
},
)(input)
)
.parse(input)
}
}
16 changes: 10 additions & 6 deletions rsass/src/css/selectors/compound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ pub(crate) mod parser {
use nom::bytes::complete::tag;
use nom::combinator::{opt, value, verify};
use nom::sequence::preceded;
use nom::Parser as _;

pub(crate) fn compound_selector(
input: Span,
Expand All @@ -336,25 +337,28 @@ pub(crate) mod parser {
result.element = Some(stop);
return Ok((rest, result));
}
let (rest, backref) = opt(value((), tag("&")))(input)?;
let (rest, backref) = opt(value((), tag("&"))).parse(input)?;
result.backref = backref;
let (mut rest, elem) = opt(elem_name)(rest)?;
let (mut rest, elem) = opt(elem_name).parse(rest)?;
result.element = elem;

loop {
rest = match rest.first() {
Some(b'#') => {
let (r, id) = preceded(tag("#"), css_string)(rest)?;
let (r, id) =
preceded(tag("#"), css_string).parse(rest)?;
result.id = Some(id);
r
}
Some(b'%') => {
let (r, p) = preceded(tag("%"), css_string)(rest)?;
let (r, p) =
preceded(tag("%"), css_string).parse(rest)?;
result.placeholders.push(p);
r
}
Some(b'.') => {
let (r, c) = preceded(tag("."), css_string)(rest)?;
let (r, c) =
preceded(tag("."), css_string).parse(rest)?;
result.classes.push(c);
r
}
Expand All @@ -371,7 +375,7 @@ pub(crate) mod parser {
_ => break,
};
}
verify(tag(""), |_| !result.is_empty())(rest)?;
verify(tag(""), |_| !result.is_empty()).parse(rest)?;
Ok((rest, result))
}
}
19 changes: 11 additions & 8 deletions rsass/src/css/selectors/elemtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,11 @@ pub(super) mod parser {
use nom::branch::alt;
use nom::bytes::complete::{is_a, tag};
use nom::combinator::{map, opt, recognize, value};
use nom::sequence::{pair, preceded, tuple};
use nom::sequence::{pair, preceded};
use nom::Parser as _;

pub(crate) fn elem_name(input: Span) -> PResult<ElemType> {
map(name_opt_ns, |s| ElemType { s })(input)
map(name_opt_ns, |s| ElemType { s }).parse(input)
}

/// Recognize a keyframe stop as an element selector.
Expand All @@ -90,20 +91,21 @@ pub(super) mod parser {
/// in the future.
pub(crate) fn keyframe_stop(input: Span) -> PResult<ElemType> {
map(
recognize(tuple((
recognize((
is_a("0123456789."),
opt(tuple((is_a("eE"), opt(tag("-")), is_a("0123456789")))),
opt((is_a("eE"), opt(tag("-")), is_a("0123456789"))),
tag("%"),
))),
)),
|stop: Span| ElemType {
s: String::from_utf8_lossy(stop.fragment()).to_string(),
},
)(input)
)
.parse(input)
}

pub(crate) fn name_opt_ns(input: Span) -> PResult<String> {
fn name_part(input: Span) -> PResult<String> {
alt((value(String::from("*"), tag("*")), css_string))(input)
alt((value(String::from("*"), tag("*")), css_string)).parse(input)
}
alt((
map(preceded(tag("|"), name_part), |mut s| {
Expand All @@ -120,6 +122,7 @@ pub(super) mod parser {
}
},
),
))(input)
))
.parse(input)
}
}
10 changes: 6 additions & 4 deletions rsass/src/css/selectors/pseudo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,12 @@ pub(super) mod parser {
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::combinator::{map, value};
use nom::sequence::{delimited, tuple};
use nom::sequence::delimited;
use nom::Parser as _;

pub(crate) fn pseudo(input: Span) -> PResult<Pseudo> {
map(
tuple((
(
alt((value(true, tag("::")), value(false, tag(":")))),
css_string_nohash,
// Note: The accepted type of selector should probably
Expand All @@ -229,8 +230,9 @@ pub(super) mod parser {
),
map(tag(""), |_| Arg::None),
)),
)),
),
|(element, name, arg)| Pseudo { name, arg, element },
)(input)
)
.parse(input)
}
}
16 changes: 10 additions & 6 deletions rsass/src/css/selectors/selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,19 +666,20 @@ pub(crate) mod parser {
use nom::combinator::{into, opt, value, verify};
use nom::multi::fold_many0;
use nom::sequence::{delimited, pair, preceded, terminated};
use nom::Parser as _;

pub(crate) fn selector(input: Span) -> PResult<Selector> {
let (input, prerel) =
preceded(opt_spacelike, opt(explicit_rel_kind))(input)?;
preceded(opt_spacelike, opt(explicit_rel_kind)).parse(input)?;
let (input, first) = if let Some(prerel) = prerel {
let (input, first) = opt(compound_selector)(input)?;
let (input, first) = opt(compound_selector).parse(input)?;
let first = Selector {
rel_of: Some(Box::new((prerel, Selector::default()))),
compound: first.unwrap_or_default(),
};
(input, first)
} else {
into(compound_selector)(input)?
into(compound_selector).parse(input)?
};
terminated(
fold_many0(
Expand All @@ -692,7 +693,8 @@ pub(crate) mod parser {
},
),
opt_spacelike,
)(input)
)
.parse(input)
}

fn explicit_rel_kind(input: Span) -> PResult<RelKind> {
Expand All @@ -704,10 +706,12 @@ pub(crate) mod parser {
value(RelKind::Parent, tag(">")),
)),
opt_spacelike,
)(input)
)
.parse(input)
}

fn rel_kind(input: Span) -> PResult<RelKind> {
alt((explicit_rel_kind, value(RelKind::Ancestor, spacelike)))(input)
alt((explicit_rel_kind, value(RelKind::Ancestor, spacelike)))
.parse(input)
}
}
4 changes: 3 additions & 1 deletion rsass/src/css/selectors/selectorset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ pub(super) mod parser {
use nom::combinator::map;
use nom::multi::separated_list1;
use nom::sequence::delimited;
use nom::Parser as _;

pub(crate) fn selector_set(input: Span) -> PResult<SelectorSet> {
map(
Expand All @@ -181,6 +182,7 @@ pub(super) mod parser {
super::super::parser::selector,
),
|s| SelectorSet { s },
)(input)
)
.parse(input)
}
}
6 changes: 3 additions & 3 deletions rsass/src/input/sourcepos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ impl SourcePos {

/// If self is preceded (on same line) by `s`, include `s` in self.
pub(crate) fn opt_back(mut self, s: &str) -> Self {
let len = s.as_bytes().len();
if self.source.data().get(self.start - s.len()..self.start)
let len = s.len();
if self.source.data().get(self.start - len..self.start)
== Some(s.as_bytes())
{
self.start -= len;
Expand Down Expand Up @@ -276,7 +276,7 @@ impl SourcePos {
) -> Self {
let line = format!("{kind} {name}{args} {{");
Self {
start: kind.as_bytes().len() + 1,
start: kind.len() + 1,
end: line.len() - 2,
source: SourceFile::scss_bytes(line, SourceName::root(module)),
}
Expand Down
25 changes: 17 additions & 8 deletions rsass/src/parser/css/media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use nom::bytes::complete::{tag, tag_no_case};
use nom::combinator::{map, value};
use nom::multi::{fold_many0, fold_many1, separated_list1};
use nom::sequence::{delimited, pair, preceded, terminated};
use nom::Parser as _;
use std::str::from_utf8;

pub fn args(input: Span) -> PResult<MediaArgs> {
Expand All @@ -22,7 +23,8 @@ pub fn args(input: Span) -> PResult<MediaArgs> {
MediaArgs::Comma(v)
}
},
)(input)
)
.parse(input)
}
fn media_args_and(input: Span) -> PResult<MediaArgs> {
map(
Expand All @@ -37,7 +39,8 @@ fn media_args_and(input: Span) -> PResult<MediaArgs> {
MediaArgs::And(v)
}
},
)(input)
)
.parse(input)
}

fn media_args_or(input: Span) -> PResult<MediaArgs> {
Expand All @@ -53,7 +56,8 @@ fn media_args_or(input: Span) -> PResult<MediaArgs> {
MediaArgs::Or(v)
}
},
)(input)
)
.parse(input)
}

fn media_args_one(input: Span) -> PResult<MediaArgs> {
Expand Down Expand Up @@ -94,7 +98,8 @@ fn media_args_one(input: Span) -> PResult<MediaArgs> {
map(media_args_one, |v| MediaArgs::Bracket(Box::new(v))),
tag("]"),
),
))(input)
))
.parse(input)
}

fn media_relation(input: Span) -> PResult<Vec<(Operator, Value)>> {
Expand All @@ -109,7 +114,8 @@ fn media_relation(input: Span) -> PResult<Vec<(Operator, Value)>> {
acc.push(item);
acc
},
)(rest)
)
.parse(rest)
}

pub fn relational_operator(input: Span) -> PResult<Operator> {
Expand All @@ -121,7 +127,8 @@ pub fn relational_operator(input: Span) -> PResult<Operator> {
value(Operator::Greater, tag(">")),
value(Operator::LesserE, tag("<=")),
value(Operator::Lesser, tag("<")),
))(input)
))
.parse(input)
}

/// Any css value that is allowd in a media relation / range.
Expand All @@ -133,7 +140,8 @@ fn media_value(input: Span) -> PResult<Value> {
media_value,
preceded(opt_spacelike, tag(")")),
),
))(input)
))
.parse(input)
}
pub fn media_slash_list_no_space(input: Span) -> PResult<Value> {
let (input, first) = values::single(input)?;
Expand All @@ -144,7 +152,8 @@ pub fn media_slash_list_no_space(input: Span) -> PResult<Value> {
list.push(item);
list
},
)(input)?;
)
.parse(input)?;
Ok((
input,
if list.len() == 1 {
Expand Down
Loading

0 comments on commit 392adab

Please sign in to comment.