[][src]Struct cssparser::Parser

pub struct Parser<'i, 't> { /* fields omitted */ }

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

Implementations

impl<'i: 't, 't> Parser<'i, 't>[src]

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

Create a new parser

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

Return the current line that is being parsed.

pub fn is_exhausted(&mut self) -> bool[src]

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

This ignores whitespace and comments.

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

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.

pub fn position(&self) -> SourcePosition[src]

Return the current position within the input.

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

pub fn current_source_location(&self) -> SourceLocation[src]

The current line number and column number.

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

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.

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

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.

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

Create a new BasicParseError at the current location

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

Create a new basic ParseError at the current location

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

Create a new custom BasicParseError at the current location

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

Create a new unexpected token BasicParseError at the current location

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

Create a new unexpected token ParseError at the current location

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

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

pub fn state(&self) -> ParserState[src]

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

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

pub fn skip_whitespace(&mut self)[src]

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

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

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.

pub fn look_for_var_or_env_functions(&mut self)[src]

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

pub fn seen_var_or_env_functions(&mut self) -> bool[src]

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.

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

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

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

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.

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

Return a slice of the CSS input

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

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

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

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

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

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

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

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.

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>>, 
[src]

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

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

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

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 retuns Err(()) 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.

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>>, 
[src]

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 Err(()) if the closure leaves some input before that point.

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>>, 
[src]

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 Err(()) if the closure leaves some input before that point.

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>>, 
[src]

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

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

Parse a and return its value.

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

Parse a and return the unescaped value.

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

expect_ident, but clone the CowRcStr

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

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

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

Parse a and return the unescaped value.

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

expect_string, but clone the CowRcStr

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

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

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

Parse a and return the unescaped value.

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

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

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

Parse a and return the integer value.

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

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

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

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

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

Parse a : .

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

Parse a ; .

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

Parse a , .

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

Parse a with the given value.

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

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

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

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

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

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

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

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

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

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

Parse a token and return its name.

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

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

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.

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

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> !RefUnwindSafe for Parser<'i, 't>[src]

impl<'i, 't> !Send for Parser<'i, 't>[src]

impl<'i, 't> !Sync for Parser<'i, 't>[src]

impl<'i, 't> Unpin for Parser<'i, 't> where
    'i: 't, 
[src]

impl<'i, 't> !UnwindSafe for Parser<'i, 't>[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.