Skip to content

Commit cb67994

Browse files
committed
Add a "skip whitespace" iterator that also ignores source locations.
1 parent 1424ac5 commit cb67994

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

ast.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
use std::str::ToStr;
6+
use std::vec;
67

78

89
#[deriving(Eq)]
@@ -114,3 +115,30 @@ pub enum ErrorReason {
114115
impl ToStr for ErrorReason {
115116
fn to_str(&self) -> ~str { fmt!("%?", self) }
116117
}
118+
119+
120+
pub trait SkipWhitespaceIterable<'self> {
121+
pub fn skip_whitespace(self) -> SkipWhitespaceIterator<'self>;
122+
}
123+
124+
impl<'self> SkipWhitespaceIterable<'self> for &'self [(ComponentValue, SourceLocation)] {
125+
pub fn skip_whitespace(self) -> SkipWhitespaceIterator<'self> {
126+
SkipWhitespaceIterator{ iter: self.iter() }
127+
}
128+
}
129+
130+
struct SkipWhitespaceIterator<'self> {
131+
iter: vec::VecIterator<'self, (ComponentValue, SourceLocation)>,
132+
}
133+
134+
impl<'self> Iterator<&'self ComponentValue> for SkipWhitespaceIterator<'self> {
135+
fn next(&mut self) -> Option<&'self ComponentValue> {
136+
loop {
137+
match self.iter.next() {
138+
Some(&(WhiteSpace, _)) => (),
139+
Some(&(ref component_value, _)) => return Some(component_value),
140+
None => return None
141+
}
142+
}
143+
}
144+
}

color.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,7 @@ fn parse_color_function(name: &str, arguments: &[(ComponentValue, SourceLocation
8787
else if "hsla" == lower_name { (false, true) }
8888
else { return None };
8989

90-
let mut iter = do arguments.iter().filter_map |&(ref c, _)| {
91-
if c != &WhiteSpace { Some(c) } else { None }
92-
};
90+
let mut iter = arguments.skip_whitespace();
9391
macro_rules! expect_comma(
9492
() => ( if iter.next() != Some(&Comma) { return None } );
9593
)

0 commit comments

Comments
 (0)