Skip to content

Add tracking of var() functions. #87

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

Merged
merged 2 commits into from
Sep 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ version = "0.1.1"
optional = true

[dependencies.heapsize_plugin]
version = "0.0.1"
version = "0.1.0"
optional = true

[dependencies]
Expand Down
13 changes: 13 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,19 @@ impl<'i, 't> Parser<'i, 't> {
self.at_start_of = new_position.at_start_of;
}

/// Start looking for `var()` functions. (See the `.seen_var_functions()` method.)
#[inline]
pub fn look_for_var_functions(&mut self) {
self.tokenizer.look_for_var_functions()
}

/// Return whether a `var()` function has been seen by the tokenizer since
/// either `look_for_var_functions` was called, and stop looking.
#[inline]
pub fn seen_var_functions(&mut self) -> bool {
self.tokenizer.seen_var_functions()
}

/// 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)
Expand Down
32 changes: 30 additions & 2 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ pub struct Tokenizer<'a> {
position: usize,
/// Cache for `source_location()`
last_known_line_break: Cell<(usize, usize)>,
var_functions: VarFunctions,
}

#[derive(Copy, Clone, PartialEq, Eq)]
enum VarFunctions {
DontCare,
LookingForThem,
SeenAtLeastOne,
}


Expand All @@ -221,9 +229,22 @@ impl<'a> Tokenizer<'a> {
input: input,
position: 0,
last_known_line_break: Cell::new((1, 0)),
var_functions: VarFunctions::DontCare,
}
}

#[inline]
pub fn look_for_var_functions(&mut self) {
self.var_functions = VarFunctions::LookingForThem;
}

#[inline]
pub fn seen_var_functions(&mut self) -> bool {
let seen = self.var_functions == VarFunctions::SeenAtLeastOne;
self.var_functions = VarFunctions::DontCare;
seen
}

#[inline]
pub fn next(&mut self) -> Result<Token<'a>, ()> {
next_token(self).ok_or(())
Expand Down Expand Up @@ -606,8 +627,15 @@ fn consume_ident_like<'a>(tokenizer: &mut Tokenizer<'a>) -> Token<'a> {
let value = consume_name(tokenizer);
if !tokenizer.is_eof() && tokenizer.next_char() == '(' {
tokenizer.advance(1);
if value.eq_ignore_ascii_case("url") { consume_url(tokenizer) }
else { Function(value) }
if value.eq_ignore_ascii_case("url") {
consume_url(tokenizer)
} else {
if tokenizer.var_functions == VarFunctions::LookingForThem &&
value.eq_ignore_ascii_case("var") {
tokenizer.var_functions = VarFunctions::SeenAtLeastOne;
}
Function(value)
}
} else {
Ident(value)
}
Expand Down