From b1656ac127b90b7925be08210e28c5d0df6ab115 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Wed, 2 Sep 2015 22:48:27 +0200 Subject: [PATCH 1/2] Add tracking of `var()` functions. This helps implementing CSS Custom Properties in Servo. --- src/parser.rs | 13 +++++++++++++ src/tokenizer.rs | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 8b5b5f11..6f04b88d 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -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) diff --git a/src/tokenizer.rs b/src/tokenizer.rs index b083bb6d..54c78d63 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -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, } @@ -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, ()> { next_token(self).ok_or(()) @@ -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) } From 61a0b2e1a9ab7e815eb622f1adc1158680785335 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Wed, 2 Sep 2015 23:21:10 +0200 Subject: [PATCH 2/2] Update heapsize_plugin --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index da6848ab..ae879c51 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,7 +27,7 @@ version = "0.1.1" optional = true [dependencies.heapsize_plugin] -version = "0.0.1" +version = "0.1.0" optional = true [dependencies]