Skip to content

Refactoring, better ability to use cssparser-color as a separate crate. #377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
-
  • Loading branch information
chipnertkj committed Dec 7, 2023
commit 0d8f0bc5cdebcf17ed587d76f6c9f1dfe5aecba0
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cssparser"
version = "0.33.0"
version = "0.34.0"
authors = ["Simon Sapin <simon.sapin@exyr.org>"]

description = "Rust implementation of CSS Syntax Level 3"
Expand Down
2 changes: 1 addition & 1 deletion color/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cssparser-color"
version = "0.1.0"
version = "0.2.0"
authors = ["Emilio Cobos Álvarez <emilio@crisal.io>"]
description = "Color implementation based on cssparser"
documentation = "https://docs.rs/cssparser-color/"
Expand Down
6 changes: 3 additions & 3 deletions color/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ where
P: ColorParser<'i>,
{
let location = input.current_source_location();
let token = input.next()?;
let token = input.try_next()?;
match *token {
Token::Hash(ref value) | Token::IDHash(ref value) => {
parse_hash_color(value.as_bytes()).map(|(r, g, b, a)| P::Output::from_rgba(r, g, b, a))
Expand Down Expand Up @@ -1033,7 +1033,7 @@ pub trait ColorParser<'i> {
input: &mut Parser<'i, 't>,
) -> Result<AngleOrNumber, ParseError<'i, Self::Error>> {
let location = input.current_source_location();
Ok(match *input.next()? {
Ok(match *input.try_next()? {
Token::Number { value, .. } => AngleOrNumber::Number { value },
Token::Dimension {
value: v, ref unit, ..
Expand Down Expand Up @@ -1078,7 +1078,7 @@ pub trait ColorParser<'i> {
input: &mut Parser<'i, 't>,
) -> Result<NumberOrPercentage, ParseError<'i, Self::Error>> {
let location = input.current_source_location();
Ok(match *input.next()? {
Ok(match *input.try_next()? {
Token::Number { value, .. } => NumberOrPercentage::Number { value },
Token::Percentage { unit_value, .. } => NumberOrPercentage::Percentage { unit_value },
ref t => return Err(location.new_unexpected_token_error(t.clone())),
Expand Down
10 changes: 5 additions & 5 deletions src/nth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::{BasicParseError, Parser, ParserInput, Token};
/// in which case the caller needs to check if the arguments’ parser is exhausted.
/// Return `Ok((A, B))`, or `Err(())` for a syntax error.
pub fn parse_nth<'i>(input: &mut Parser<'i, '_>) -> Result<(i32, i32), BasicParseError<'i>> {
match *input.next()? {
match *input.try_next()? {
Token::Number {
int_value: Some(b), ..
} => Ok((0, b)),
Expand Down Expand Up @@ -55,7 +55,7 @@ pub fn parse_nth<'i>(input: &mut Parser<'i, '_>) -> Result<(i32, i32), BasicPars
}
}
}
Token::Delim('+') => match *input.next_including_whitespace()? {
Token::Delim('+') => match *input.try_next_including_whitespace()? {
Token::Ident(ref value) => {
match_ignore_ascii_case! { value,
"n" => parse_b(input, 1),
Expand Down Expand Up @@ -83,7 +83,7 @@ pub fn parse_nth<'i>(input: &mut Parser<'i, '_>) -> Result<(i32, i32), BasicPars

fn parse_b<'i>(input: &mut Parser<'i, '_>, a: i32) -> Result<(i32, i32), BasicParseError<'i>> {
let start = input.state();
match input.next() {
match input.try_next() {
Ok(&Token::Delim('+')) => parse_signless_b(input, a, 1),
Ok(&Token::Delim('-')) => parse_signless_b(input, a, -1),
Ok(&Token::Number {
Expand All @@ -104,7 +104,7 @@ fn parse_signless_b<'i>(
b_sign: i32,
) -> Result<(i32, i32), BasicParseError<'i>> {
// FIXME: remove .clone() when lifetimes are non-lexical.
match input.next()?.clone() {
match input.try_next()?.clone() {
Token::Number {
has_sign: false,
int_value: Some(b),
Expand Down Expand Up @@ -132,7 +132,7 @@ fn parse_number_saturate(string: &str) -> Result<i32, ()> {
let int = if let Ok(&Token::Number {
int_value: Some(int),
..
}) = parser.next_including_whitespace_and_comments()
}) = parser.try_next_including_whitespace_and_comments()
{
int
} else {
Expand Down
22 changes: 11 additions & 11 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ macro_rules! expect {
($parser: ident, $($branches: tt)+) => {
{
let start_location = $parser.current_source_location();
match *$parser.next()? {
match *$parser.try_next()? {
$($branches)+
ref token => {
return Err(start_location.new_basic_unexpected_token_error(token.clone()))
Expand Down Expand Up @@ -401,7 +401,7 @@ impl<'i: 't, 't> Parser<'i, 't> {
#[inline]
pub fn expect_exhausted(&mut self) -> Result<(), BasicParseError<'i>> {
let start = self.state();
let result = match self.next() {
let result = match self.try_next() {
Err(BasicParseError {
kind: BasicParseErrorKind::EndOfInput,
..
Expand Down Expand Up @@ -486,7 +486,7 @@ impl<'i: 't, 't> Parser<'i, 't> {
/// Create a new unexpected token or EOF ParseError at the current location
#[inline]
pub fn new_error_for_next_token<E>(&mut self) -> ParseError<'i, E> {
let token = match self.next() {
let token = match self.try_next() {
Ok(token) => token.clone(),
Err(e) => return e.into(),
};
Expand Down Expand Up @@ -606,15 +606,15 @@ impl<'i: 't, 't> Parser<'i, 't> {
/// See the `Parser::parse_nested_block` method to parse the content of functions or blocks.
///
/// This only returns a closing token when it is unmatched (and therefore an error).
pub fn next(&mut self) -> Result<&Token<'i>, BasicParseError<'i>> {
pub fn try_next(&mut self) -> Result<&Token<'i>, BasicParseError<'i>> {
self.skip_whitespace();
self.next_including_whitespace_and_comments()
self.try_next_including_whitespace_and_comments()
}

/// Same as `Parser::next`, but does not skip whitespace tokens.
pub fn next_including_whitespace(&mut self) -> Result<&Token<'i>, BasicParseError<'i>> {
pub fn try_next_including_whitespace(&mut self) -> Result<&Token<'i>, BasicParseError<'i>> {
loop {
match self.next_including_whitespace_and_comments() {
match self.try_next_including_whitespace_and_comments() {
Err(e) => return Err(e),
Ok(&Token::Comment(_)) => {}
_ => break,
Expand All @@ -629,7 +629,7 @@ impl<'i: 't, 't> Parser<'i, 't> {
/// where comments are preserved.
/// When parsing higher-level values, per the CSS Syntax specification,
/// comments should always be ignored between tokens.
pub fn next_including_whitespace_and_comments(
pub fn try_next_including_whitespace_and_comments(
&mut self,
) -> Result<&Token<'i>, BasicParseError<'i>> {
if let Some(block_type) = self.at_start_of.take() {
Expand Down Expand Up @@ -749,7 +749,7 @@ impl<'i: 't, 't> Parser<'i, 't> {
Err(e) if !ignore_errors => return Err(e),
Err(_) => {}
}
match self.next() {
match self.try_next() {
Err(_) => return Ok(values),
Ok(&Token::Comma) => continue,
Ok(_) => unreachable!(),
Expand Down Expand Up @@ -817,7 +817,7 @@ impl<'i: 't, 't> Parser<'i, 't> {
#[inline]
pub fn expect_whitespace(&mut self) -> Result<&'i str, BasicParseError<'i>> {
let start_location = self.current_source_location();
match *self.next_including_whitespace()? {
match *self.try_next_including_whitespace()? {
Token::WhiteSpace(value) => Ok(value),
ref t => Err(start_location.new_basic_unexpected_token_error(t.clone())),
}
Expand Down Expand Up @@ -1016,7 +1016,7 @@ impl<'i: 't, 't> Parser<'i, 't> {
#[inline]
pub fn expect_no_error_token(&mut self) -> Result<(), BasicParseError<'i>> {
loop {
match self.next_including_whitespace_and_comments() {
match self.try_next_including_whitespace_and_comments() {
Ok(&Token::Function(_))
| Ok(&Token::ParenthesisBlock)
| Ok(&Token::SquareBracketBlock)
Expand Down
14 changes: 9 additions & 5 deletions src/rules_and_declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,11 @@ where
loop {
self.input.skip_whitespace();
let start = self.input.state();
match self.input.next_including_whitespace_and_comments().ok()? {
match self
.input
.try_next_including_whitespace_and_comments()
.ok()?
{
Token::CloseCurlyBracket
| Token::WhiteSpace(..)
| Token::Semicolon
Expand Down Expand Up @@ -366,7 +370,7 @@ where
self.input.skip_cdc_and_cdo();
let start = self.input.state();
let at_keyword = match self.input.next_byte()? {
b'@' => match self.input.next_including_whitespace_and_comments() {
b'@' => match self.input.try_next_including_whitespace_and_comments() {
Ok(Token::AtKeyword(name)) => Some(name.clone()),
_ => {
self.input.reset(&start);
Expand Down Expand Up @@ -436,7 +440,7 @@ where
input.skip_whitespace();
let start = input.state();
let at_keyword = if input.next_byte() == Some(b'@') {
match *input.next_including_whitespace_and_comments()? {
match *input.try_next_including_whitespace_and_comments()? {
Token::AtKeyword(ref name) => Some(name.clone()),
_ => {
input.reset(&start);
Expand Down Expand Up @@ -468,7 +472,7 @@ where
let result = input.parse_until_before(delimiters, |input| parser.parse_prelude(name, input));
match result {
Ok(prelude) => {
let result = match input.next() {
let result = match input.try_next() {
Ok(&Token::Semicolon) | Err(_) => parser
.rule_without_block(prelude, start)
.map_err(|()| input.new_unexpected_token_error(Token::Semicolon)),
Expand All @@ -481,7 +485,7 @@ where
}
Err(error) => {
let end_position = input.position();
match input.next() {
match input.try_next() {
Ok(&Token::CurlyBracketBlock) | Ok(&Token::Semicolon) | Err(_) => {}
_ => unreachable!(),
};
Expand Down
Loading