Struct cssparser::parser::Parser

source ·
pub struct Parser<'i, 't> {
    input: &'t mut ParserInput<'i>,
    at_start_of: Option<BlockType>,
    stop_before: Delimiters,
}
Expand description

A CSS parser that borrows its &str input, yields Tokens, and keeps track of nested blocks and functions.

Fields§

§input: &'t mut ParserInput<'i>§at_start_of: Option<BlockType>

If Some(_), .parse_nested_block() can be called.

§stop_before: Delimiters

For parsers from parse_until or parse_nested_block

Implementations§

source§

impl<'i: 't, 't> Parser<'i, 't>

source

pub fn new(input: &'t mut ParserInput<'i>) -> Parser<'i, 't>

Create a new parser

source

pub fn current_line(&self) -> &'i str

Return the current line that is being parsed.

source

pub fn is_exhausted(&mut self) -> bool

Check whether the input is exhausted. That is, if .next() would return a token.

This ignores whitespace and comments.

source

pub fn expect_exhausted(&mut self) -> Result<(), BasicParseError<'i>>

Check whether the input is exhausted. That is, if .next() would return a token. Return a Result so that the ? operator can be used: input.expect_exhausted()?

This ignores whitespace and comments.

source

pub fn position(&self) -> SourcePosition

Return the current position within the input.

This can be used with the Parser::slice and slice_from methods.

source

pub fn current_source_location(&self) -> SourceLocation

The current line number and column number.

source

pub fn current_source_map_url(&self) -> Option<&str>

The source map URL, if known.

The source map URL is extracted from a specially formatted comment. The last such comment is used, so this value may change as parsing proceeds.

source

pub fn current_source_url(&self) -> Option<&str>

The source URL, if known.

The source URL is extracted from a specially formatted comment. The last such comment is used, so this value may change as parsing proceeds.

source

pub fn new_basic_error( &self, kind: BasicParseErrorKind<'i>, ) -> BasicParseError<'i>

Create a new BasicParseError at the current location

source

pub fn new_error<E>(&self, kind: BasicParseErrorKind<'i>) -> ParseError<'i, E>

Create a new basic ParseError at the current location

source

pub fn new_custom_error<E1: Into<E2>, E2>( &self, error: E1, ) -> ParseError<'i, E2>

Create a new custom BasicParseError at the current location

source

pub fn new_basic_unexpected_token_error( &self, token: Token<'i>, ) -> BasicParseError<'i>

Create a new unexpected token BasicParseError at the current location

source

pub fn new_unexpected_token_error<E>( &self, token: Token<'i>, ) -> ParseError<'i, E>

Create a new unexpected token ParseError at the current location

source

pub fn new_error_for_next_token<E>(&mut self) -> ParseError<'i, E>

Create a new unexpected token or EOF ParseError at the current location

source

pub fn state(&self) -> ParserState

Return the current internal state of the parser (including position within the input).

This state can later be restored with the Parser::reset method.

source

pub fn skip_whitespace(&mut self)

Advance the input until the next token that’s not whitespace or a comment.

source

pub(crate) fn skip_cdc_and_cdo(&mut self)

source

pub(crate) fn next_byte(&self) -> Option<u8>

source

pub fn reset(&mut self, state: &ParserState)

Restore the internal state of the parser (including position within the input) to what was previously saved by the Parser::position method.

Should only be used with SourcePosition values from the same Parser instance.

source

pub fn look_for_var_or_env_functions(&mut self)

Start looking for var() / env() functions. (See the .seen_var_or_env_functions() method.)

source

pub fn seen_var_or_env_functions(&mut self) -> bool

Return whether a var() or env() function has been seen by the tokenizer since either look_for_var_or_env_functions was called, and stop looking.

source

pub fn try<F, T, E>(&mut self, thing: F) -> Result<T, E>
where F: FnOnce(&mut Parser<'i, 't>) -> Result<T, E>,

The old name of try_parse, which requires raw identifiers in the Rust 2018 edition.

source

pub fn try_parse<F, T, E>(&mut self, thing: F) -> Result<T, E>
where F: FnOnce(&mut Parser<'i, 't>) -> Result<T, E>,

Execute the given closure, passing it the parser. If the result (returned unchanged) is Err, the internal state of the parser (including position within the input) is restored to what it was before the call.

source

pub fn slice(&self, range: Range<SourcePosition>) -> &'i str

Return a slice of the CSS input

source

pub fn slice_from(&self, start_position: SourcePosition) -> &'i str

Return a slice of the CSS input, from the given position to the current one.

source

pub fn next(&mut self) -> Result<&Token<'i>, BasicParseError<'i>>

Return the next token in the input that is neither whitespace or a comment, and advance the position accordingly.

After returning a Function, ParenthesisBlock, CurlyBracketBlock, or SquareBracketBlock token, the next call will skip until after the matching CloseParenthesis, CloseCurlyBracket, or CloseSquareBracket token.

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).

source

pub fn next_including_whitespace( &mut self, ) -> Result<&Token<'i>, BasicParseError<'i>>

Same as Parser::next, but does not skip whitespace tokens.

source

pub fn next_including_whitespace_and_comments( &mut self, ) -> Result<&Token<'i>, BasicParseError<'i>>

Same as Parser::next, but does not skip whitespace or comment tokens.

Note: This should only be used in contexts like a CSS pre-processor where comments are preserved. When parsing higher-level values, per the CSS Syntax specification, comments should always be ignored between tokens.

source

pub fn parse_entirely<F, T, E>( &mut self, parse: F, ) -> Result<T, ParseError<'i, E>>
where F: FnOnce(&mut Parser<'i, 't>) -> Result<T, ParseError<'i, E>>,

Have the given closure parse something, then check the the input is exhausted. The result is overridden to an Err(..) if some input remains.

This can help tell e.g. color: green; from color: green 4px;

source

pub fn parse_comma_separated<F, T, E>( &mut self, parse_one: F, ) -> Result<Vec<T>, ParseError<'i, E>>
where F: for<'tt> FnMut(&mut Parser<'i, 'tt>) -> Result<T, ParseError<'i, E>>,

Parse a list of comma-separated values, all with the same syntax.

The given closure is called repeatedly with a “delimited” parser (see the Parser::parse_until_before method) so that it can over consume the input past a comma at this block/function nesting level.

Successful results are accumulated in a vector.

This method returns anErr(..) the first time that a closure call does, or if a closure call leaves some input before the next comma or the end of the input.

source

pub fn parse_comma_separated_ignoring_errors<F, T, E: 'i>( &mut self, parse_one: F, ) -> Vec<T>
where F: for<'tt> FnMut(&mut Parser<'i, 'tt>) -> Result<T, ParseError<'i, E>>,

Like parse_comma_separated, but ignores errors on unknown components, rather than erroring out in the whole list.

Caller must deal with the fact that the resulting list might be empty, if there’s no valid component on the list.

source

fn parse_comma_separated_internal<F, T, E>( &mut self, parse_one: F, ignore_errors: bool, ) -> Result<Vec<T>, ParseError<'i, E>>
where F: for<'tt> FnMut(&mut Parser<'i, 'tt>) -> Result<T, ParseError<'i, E>>,

source

pub fn parse_nested_block<F, T, E>( &mut self, parse: F, ) -> Result<T, ParseError<'i, E>>
where F: for<'tt> FnOnce(&mut Parser<'i, 'tt>) -> Result<T, ParseError<'i, E>>,

Parse the content of a block or function.

This method panics if the last token yielded by this parser (from one of the next* methods) is not a on that marks the start of a block or function: a Function, ParenthesisBlock, CurlyBracketBlock, or SquareBracketBlock.

The given closure is called with a “delimited” parser that stops at the end of the block or function (at the matching closing token).

The result is overridden to an Err(..) if the closure leaves some input before that point.

source

pub fn parse_until_before<F, T, E>( &mut self, delimiters: Delimiters, parse: F, ) -> Result<T, ParseError<'i, E>>
where F: for<'tt> FnOnce(&mut Parser<'i, 'tt>) -> Result<T, ParseError<'i, E>>,

Limit parsing to until a given delimiter or the end of the input. (E.g. a semicolon for a property value.)

The given closure is called with a “delimited” parser that stops before the first character at this block/function nesting level that matches the given set of delimiters, or at the end of the input.

The result is overridden to an Err(..) if the closure leaves some input before that point.

source

pub fn parse_until_after<F, T, E>( &mut self, delimiters: Delimiters, parse: F, ) -> Result<T, ParseError<'i, E>>
where F: for<'tt> FnOnce(&mut Parser<'i, 'tt>) -> Result<T, ParseError<'i, E>>,

Like parse_until_before, but also consume the delimiter token.

This can be useful when you don’t need to know which delimiter it was (e.g. if these is only one in the given set) or if it was there at all (as opposed to reaching the end of the input).

source

pub fn expect_whitespace(&mut self) -> Result<&'i str, BasicParseError<'i>>

Parse a and return its value.

source

pub fn expect_ident(&mut self) -> Result<&CowRcStr<'i>, BasicParseError<'i>>

Parse a and return the unescaped value.

source

pub fn expect_ident_cloned( &mut self, ) -> Result<CowRcStr<'i>, BasicParseError<'i>>

expect_ident, but clone the CowRcStr

source

pub fn expect_ident_matching( &mut self, expected_value: &str, ) -> Result<(), BasicParseError<'i>>

Parse a whose unescaped value is an ASCII-insensitive match for the given value.

source

pub fn expect_string(&mut self) -> Result<&CowRcStr<'i>, BasicParseError<'i>>

Parse a and return the unescaped value.

source

pub fn expect_string_cloned( &mut self, ) -> Result<CowRcStr<'i>, BasicParseError<'i>>

expect_string, but clone the CowRcStr

source

pub fn expect_ident_or_string( &mut self, ) -> Result<&CowRcStr<'i>, BasicParseError<'i>>

Parse either a or a , and return the unescaped value.

source

pub fn expect_url(&mut self) -> Result<CowRcStr<'i>, BasicParseError<'i>>

Parse a and return the unescaped value.

source

pub fn expect_url_or_string( &mut self, ) -> Result<CowRcStr<'i>, BasicParseError<'i>>

Parse either a or a , and return the unescaped value.

source

pub fn expect_number(&mut self) -> Result<f32, BasicParseError<'i>>

Parse a and return the integer value.

source

pub fn expect_integer(&mut self) -> Result<i32, BasicParseError<'i>>

Parse a that does not have a fractional part, and return the integer value.

source

pub fn expect_percentage(&mut self) -> Result<f32, BasicParseError<'i>>

Parse a and return the value. 0% and 100% map to 0.0 and 1.0 (not 100.0), respectively.

source

pub fn expect_colon(&mut self) -> Result<(), BasicParseError<'i>>

Parse a : .

source

pub fn expect_semicolon(&mut self) -> Result<(), BasicParseError<'i>>

Parse a ; .

source

pub fn expect_comma(&mut self) -> Result<(), BasicParseError<'i>>

Parse a , .

source

pub fn expect_delim( &mut self, expected_value: char, ) -> Result<(), BasicParseError<'i>>

Parse a with the given value.

source

pub fn expect_curly_bracket_block(&mut self) -> Result<(), BasicParseError<'i>>

Parse a { /* ... */ } curly brackets block.

If the result is Ok, you can then call the Parser::parse_nested_block method.

source

pub fn expect_square_bracket_block(&mut self) -> Result<(), BasicParseError<'i>>

Parse a [ /* ... */ ] square brackets block.

If the result is Ok, you can then call the Parser::parse_nested_block method.

source

pub fn expect_parenthesis_block(&mut self) -> Result<(), BasicParseError<'i>>

Parse a ( /* ... */ ) parenthesis block.

If the result is Ok, you can then call the Parser::parse_nested_block method.

source

pub fn expect_function(&mut self) -> Result<&CowRcStr<'i>, BasicParseError<'i>>

Parse a token and return its name.

If the result is Ok, you can then call the Parser::parse_nested_block method.

source

pub fn expect_function_matching( &mut self, expected_name: &str, ) -> Result<(), BasicParseError<'i>>

Parse a token whose name is an ASCII-insensitive match for the given value.

If the result is Ok, you can then call the Parser::parse_nested_block method.

source

pub fn expect_no_error_token(&mut self) -> Result<(), BasicParseError<'i>>

Parse the input until exhaustion and check that it contains no “error” token.

See Token::is_parse_error. This also checks nested blocks and functions recursively.

Auto Trait Implementations§

§

impl<'i, 't> Freeze for Parser<'i, 't>

§

impl<'i, 't> RefUnwindSafe for Parser<'i, 't>

§

impl<'i, 't> !Send for Parser<'i, 't>

§

impl<'i, 't> !Sync for Parser<'i, 't>

§

impl<'i, 't> Unpin for Parser<'i, 't>

§

impl<'i, 't> !UnwindSafe for Parser<'i, 't>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.