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 Token
s,
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>
impl<'i: 't, 't> Parser<'i, 't>
sourcepub fn new(input: &'t mut ParserInput<'i>) -> Parser<'i, 't>
pub fn new(input: &'t mut ParserInput<'i>) -> Parser<'i, 't>
Create a new parser
sourcepub fn current_line(&self) -> &'i str
pub fn current_line(&self) -> &'i str
Return the current line that is being parsed.
sourcepub fn is_exhausted(&mut self) -> bool
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.
sourcepub fn expect_exhausted(&mut self) -> Result<(), BasicParseError<'i>>
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.
sourcepub fn position(&self) -> SourcePosition
pub fn position(&self) -> SourcePosition
Return the current position within the input.
This can be used with the Parser::slice
and slice_from
methods.
sourcepub fn current_source_location(&self) -> SourceLocation
pub fn current_source_location(&self) -> SourceLocation
The current line number and column number.
sourcepub fn current_source_map_url(&self) -> Option<&str>
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.
sourcepub fn current_source_url(&self) -> Option<&str>
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.
sourcepub fn new_basic_error(
&self,
kind: BasicParseErrorKind<'i>,
) -> BasicParseError<'i>
pub fn new_basic_error( &self, kind: BasicParseErrorKind<'i>, ) -> BasicParseError<'i>
Create a new BasicParseError at the current location
sourcepub fn new_error<E>(&self, kind: BasicParseErrorKind<'i>) -> ParseError<'i, E>
pub fn new_error<E>(&self, kind: BasicParseErrorKind<'i>) -> ParseError<'i, E>
Create a new basic ParseError at the current location
sourcepub fn new_custom_error<E1: Into<E2>, E2>(
&self,
error: E1,
) -> ParseError<'i, E2>
pub fn new_custom_error<E1: Into<E2>, E2>( &self, error: E1, ) -> ParseError<'i, E2>
Create a new custom BasicParseError at the current location
sourcepub fn new_basic_unexpected_token_error(
&self,
token: Token<'i>,
) -> BasicParseError<'i>
pub fn new_basic_unexpected_token_error( &self, token: Token<'i>, ) -> BasicParseError<'i>
Create a new unexpected token BasicParseError at the current location
sourcepub fn new_unexpected_token_error<E>(
&self,
token: Token<'i>,
) -> ParseError<'i, E>
pub fn new_unexpected_token_error<E>( &self, token: Token<'i>, ) -> ParseError<'i, E>
Create a new unexpected token ParseError at the current location
sourcepub fn new_error_for_next_token<E>(&mut self) -> ParseError<'i, E>
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
sourcepub fn state(&self) -> ParserState
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.
sourcepub fn skip_whitespace(&mut self)
pub fn skip_whitespace(&mut self)
Advance the input until the next token that’s not whitespace or a comment.
pub(crate) fn skip_cdc_and_cdo(&mut self)
pub(crate) fn next_byte(&self) -> Option<u8>
sourcepub fn reset(&mut self, state: &ParserState)
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.
sourcepub fn look_for_var_or_env_functions(&mut self)
pub fn look_for_var_or_env_functions(&mut self)
Start looking for var()
/ env()
functions. (See the
.seen_var_or_env_functions()
method.)
sourcepub fn seen_var_or_env_functions(&mut self) -> bool
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.
sourcepub fn try<F, T, E>(&mut self, thing: F) -> Result<T, E>
pub fn try<F, T, E>(&mut self, thing: F) -> Result<T, E>
The old name of try_parse
, which requires raw identifiers in the Rust 2018 edition.
sourcepub fn try_parse<F, T, E>(&mut self, thing: F) -> Result<T, E>
pub fn try_parse<F, T, E>(&mut self, thing: F) -> 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.
sourcepub fn slice(&self, range: Range<SourcePosition>) -> &'i str
pub fn slice(&self, range: Range<SourcePosition>) -> &'i str
Return a slice of the CSS input
sourcepub fn slice_from(&self, start_position: SourcePosition) -> &'i str
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.
sourcepub fn next(&mut self) -> Result<&Token<'i>, BasicParseError<'i>>
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).
sourcepub fn next_including_whitespace(
&mut self,
) -> Result<&Token<'i>, BasicParseError<'i>>
pub fn next_including_whitespace( &mut self, ) -> Result<&Token<'i>, BasicParseError<'i>>
Same as Parser::next
, but does not skip whitespace tokens.
sourcepub fn next_including_whitespace_and_comments(
&mut self,
) -> Result<&Token<'i>, BasicParseError<'i>>
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.
sourcepub fn parse_entirely<F, T, E>(
&mut self,
parse: F,
) -> Result<T, ParseError<'i, E>>
pub fn parse_entirely<F, T, E>( &mut self, parse: F, ) -> 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;
sourcepub fn parse_comma_separated<F, T, E>(
&mut self,
parse_one: F,
) -> Result<Vec<T>, ParseError<'i, E>>
pub fn parse_comma_separated<F, T, E>( &mut self, parse_one: F, ) -> Result<Vec<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.
sourcepub fn parse_comma_separated_ignoring_errors<F, T, E: 'i>(
&mut self,
parse_one: F,
) -> Vec<T>
pub fn parse_comma_separated_ignoring_errors<F, T, E: 'i>( &mut self, parse_one: F, ) -> Vec<T>
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.
fn parse_comma_separated_internal<F, T, E>( &mut self, parse_one: F, ignore_errors: bool, ) -> Result<Vec<T>, ParseError<'i, E>>
sourcepub fn parse_nested_block<F, T, E>(
&mut self,
parse: F,
) -> Result<T, ParseError<'i, E>>
pub fn parse_nested_block<F, T, E>( &mut self, parse: F, ) -> 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.
sourcepub fn parse_until_before<F, T, E>(
&mut self,
delimiters: Delimiters,
parse: F,
) -> Result<T, ParseError<'i, E>>
pub fn parse_until_before<F, T, E>( &mut self, delimiters: Delimiters, parse: F, ) -> 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.
sourcepub fn parse_until_after<F, T, E>(
&mut self,
delimiters: Delimiters,
parse: F,
) -> Result<T, ParseError<'i, E>>
pub fn parse_until_after<F, T, E>( &mut self, delimiters: Delimiters, parse: F, ) -> 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).
sourcepub fn expect_whitespace(&mut self) -> Result<&'i str, BasicParseError<'i>>
pub fn expect_whitespace(&mut self) -> Result<&'i str, BasicParseError<'i>>
Parse a
sourcepub fn expect_ident(&mut self) -> Result<&CowRcStr<'i>, BasicParseError<'i>>
pub fn expect_ident(&mut self) -> Result<&CowRcStr<'i>, BasicParseError<'i>>
Parse a
sourcepub fn expect_ident_cloned(
&mut self,
) -> Result<CowRcStr<'i>, BasicParseError<'i>>
pub fn expect_ident_cloned( &mut self, ) -> Result<CowRcStr<'i>, BasicParseError<'i>>
expect_ident, but clone the CowRcStr
sourcepub fn expect_ident_matching(
&mut self,
expected_value: &str,
) -> Result<(), BasicParseError<'i>>
pub fn expect_ident_matching( &mut self, expected_value: &str, ) -> Result<(), BasicParseError<'i>>
Parse a
sourcepub fn expect_string(&mut self) -> Result<&CowRcStr<'i>, BasicParseError<'i>>
pub fn expect_string(&mut self) -> Result<&CowRcStr<'i>, BasicParseError<'i>>
Parse a
sourcepub fn expect_string_cloned(
&mut self,
) -> Result<CowRcStr<'i>, BasicParseError<'i>>
pub fn expect_string_cloned( &mut self, ) -> Result<CowRcStr<'i>, BasicParseError<'i>>
expect_string, but clone the CowRcStr
sourcepub fn expect_ident_or_string(
&mut self,
) -> Result<&CowRcStr<'i>, BasicParseError<'i>>
pub fn expect_ident_or_string( &mut self, ) -> Result<&CowRcStr<'i>, BasicParseError<'i>>
Parse either a
sourcepub fn expect_url(&mut self) -> Result<CowRcStr<'i>, BasicParseError<'i>>
pub fn expect_url(&mut self) -> Result<CowRcStr<'i>, BasicParseError<'i>>
Parse a
sourcepub fn expect_url_or_string(
&mut self,
) -> Result<CowRcStr<'i>, BasicParseError<'i>>
pub fn expect_url_or_string( &mut self, ) -> Result<CowRcStr<'i>, BasicParseError<'i>>
Parse either a
sourcepub fn expect_number(&mut self) -> Result<f32, BasicParseError<'i>>
pub fn expect_number(&mut self) -> Result<f32, BasicParseError<'i>>
Parse a
sourcepub fn expect_integer(&mut self) -> Result<i32, BasicParseError<'i>>
pub fn expect_integer(&mut self) -> Result<i32, BasicParseError<'i>>
Parse a
sourcepub fn expect_percentage(&mut self) -> Result<f32, BasicParseError<'i>>
pub fn expect_percentage(&mut self) -> Result<f32, BasicParseError<'i>>
Parse a 0%
and 100%
map to 0.0
and 1.0
(not 100.0
), respectively.
sourcepub fn expect_colon(&mut self) -> Result<(), BasicParseError<'i>>
pub fn expect_colon(&mut self) -> Result<(), BasicParseError<'i>>
Parse a :
sourcepub fn expect_semicolon(&mut self) -> Result<(), BasicParseError<'i>>
pub fn expect_semicolon(&mut self) -> Result<(), BasicParseError<'i>>
Parse a ;
sourcepub fn expect_comma(&mut self) -> Result<(), BasicParseError<'i>>
pub fn expect_comma(&mut self) -> Result<(), BasicParseError<'i>>
Parse a ,
sourcepub fn expect_delim(
&mut self,
expected_value: char,
) -> Result<(), BasicParseError<'i>>
pub fn expect_delim( &mut self, expected_value: char, ) -> Result<(), BasicParseError<'i>>
Parse a
sourcepub fn expect_curly_bracket_block(&mut self) -> Result<(), BasicParseError<'i>>
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.
sourcepub fn expect_square_bracket_block(&mut self) -> Result<(), BasicParseError<'i>>
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.
sourcepub fn expect_parenthesis_block(&mut self) -> Result<(), BasicParseError<'i>>
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.
sourcepub fn expect_function(&mut self) -> Result<&CowRcStr<'i>, BasicParseError<'i>>
pub fn expect_function(&mut self) -> Result<&CowRcStr<'i>, BasicParseError<'i>>
Parse a
If the result is Ok
, you can then call the Parser::parse_nested_block
method.
sourcepub fn expect_function_matching(
&mut self,
expected_name: &str,
) -> Result<(), BasicParseError<'i>>
pub fn expect_function_matching( &mut self, expected_name: &str, ) -> Result<(), BasicParseError<'i>>
Parse a
If the result is Ok
, you can then call the Parser::parse_nested_block
method.
sourcepub fn expect_no_error_token(&mut self) -> Result<(), BasicParseError<'i>>
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.