Struct cssparser::Parser
[−]
[src]
pub struct Parser<'i: 't, 't> { // some fields omitted }
A CSS parser that borrows its &str
input,
yields Token
s,
and keeps track of nested blocks and functions.
Methods
impl<'i, 't> Parser<'i, 't>
[src]
fn new(input: &'i str) -> Parser<'i, 'i>
Create a new parser
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.
fn expect_exhausted(&mut self) -> Result<(), ()>
Check whether the input is exhausted. That is, if .next()
would return a token.
Return a Result
so that the try!
macro can be used: try!(input.expect_exhausted())
This ignores whitespace and comments.
fn position(&self) -> SourcePosition
Return the current internal state of the parser (including position within the input).
This state can later be restored with the Parser::reset
method.
fn reset(&mut self, new_position: SourcePosition)
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.
fn look_for_var_functions(&mut self)
Start looking for var()
functions. (See the .seen_var_functions()
method.)
fn seen_var_functions(&mut self) -> bool
Return whether a var()
function has been seen by the tokenizer since
either look_for_var_functions
was called, and stop looking.
fn look_for_viewport_percentages(&mut self)
Start looking for viewport percentage lengths. (See the seen_viewport_percentages
method.)
fn seen_viewport_percentages(&mut self) -> bool
Return whether a vh
, vw
, vmin
, or vmax
dimension has been seen by the tokenizer
since look_for_viewport_percentages
was called, and stop looking.
fn try<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.
fn slice(&self, range: Range<SourcePosition>) -> &'i str
Return a slice of the CSS input
fn slice_from(&self, start_position: SourcePosition) -> &'i str
Return a slice of the CSS input, from the given position to the current one.
fn current_source_location(&self) -> SourceLocation
Return the line and column number within the input for the current position.
fn source_location(&self, target: SourcePosition) -> SourceLocation
Return the line and column number within the input for the given position.
fn next(&mut self) -> Result<Token<'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).
fn next_including_whitespace(&mut self) -> Result<Token<'i>, ()>
Same as Parser::next
, but does not skip whitespace tokens.
fn next_including_whitespace_and_comments(&mut self) -> Result<Token<'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.
fn parse_entirely<F, T>(&mut self, parse: F) -> Result<T, ()> where F: FnOnce(&mut Parser<'i, 't>) -> Result<T, ()>
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;
fn parse_comma_separated<F, T>(&mut self, parse_one: F) -> Result<Vec<T>, ()> where F: FnMut(&mut Parser) -> Result<T, ()>
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.
fn parse_nested_block<F, T>(&mut self, parse: F) -> Result<T, ()> where F: for<'tt> FnOnce(&mut Parser<'i, 'tt>) -> Result<T, ()>
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.
fn parse_until_before<F, T>(&mut self, delimiters: Delimiters, parse: F) -> Result<T, ()> where F: for<'tt> FnOnce(&mut Parser<'i, 'tt>) -> Result<T, ()>
Limit parsing to until a given delimiter. (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.
The result is overridden to Err(())
if the closure leaves some input before that point.
fn parse_until_after<F, T>(&mut self, delimiters: Delimiters, parse: F) -> Result<T, ()> where F: for<'tt> FnOnce(&mut Parser<'i, 'tt>) -> Result<T, ()>
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).
fn expect_whitespace(&mut self) -> Result<&'i str, ()>
Parse a
fn expect_ident(&mut self) -> Result<Cow<'i, str>, ()>
Parse a
fn expect_ident_matching(&mut self, expected_value: &str) -> Result<(), ()>
Parse a
fn expect_string(&mut self) -> Result<Cow<'i, str>, ()>
Parse a
fn expect_ident_or_string(&mut self) -> Result<Cow<'i, str>, ()>
Parse either a
fn expect_url(&mut self) -> Result<Cow<'i, str>, ()>
Parse a
fn expect_url_or_string(&mut self) -> Result<Cow<'i, str>, ()>
Parse either a
fn expect_number(&mut self) -> Result<f32, ()>
Parse a
fn expect_integer(&mut self) -> Result<i32, ()>
Parse a
fn expect_percentage(&mut self) -> Result<f32, ()>
Parse a 0%
and 100%
map to 0.0
and 1.0
(not 100.0
), respectively.
fn expect_colon(&mut self) -> Result<(), ()>
Parse a :
fn expect_semicolon(&mut self) -> Result<(), ()>
Parse a ;
fn expect_comma(&mut self) -> Result<(), ()>
Parse a ,
fn expect_delim(&mut self, expected_value: char) -> Result<(), ()>
Parse a
fn expect_curly_bracket_block(&mut self) -> Result<(), ()>
Parse a { /* ... */ }
curly brackets block.
If the result is Ok
, you can then call the Parser::parse_nested_block
method.
fn expect_square_bracket_block(&mut self) -> Result<(), ()>
Parse a [ /* ... */ ]
square brackets block.
If the result is Ok
, you can then call the Parser::parse_nested_block
method.
fn expect_parenthesis_block(&mut self) -> Result<(), ()>
Parse a ( /* ... */ )
parenthesis block.
If the result is Ok
, you can then call the Parser::parse_nested_block
method.
fn expect_function(&mut self) -> Result<Cow<'i, str>, ()>
Parse a
If the result is Ok
, you can then call the Parser::parse_nested_block
method.
fn expect_function_matching(&mut self, expected_name: &str) -> Result<(), ()>
Parse a
If the result is Ok
, you can then call the Parser::parse_nested_block
method.
fn expect_no_error_token(&mut self) -> Result<(), ()>
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.
Trait Implementations
impl<'i: 't, 't> Clone for Parser<'i, 't>
[src]
fn clone(&self) -> Parser<'i, 't>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more