1111
1212
1313use std:: iterator:: Iterator ;
14- use std:: vec;
1514use std:: ascii:: eq_ignore_ascii_case;
1615
1716use ast:: * ;
18- use tokenizer:: * ;
19-
20-
21- // TODO: Use a trait?
22- enum ComponentValueIterator {
23- ParserIter ( Parser ) ,
24- VectorIter ( vec:: ConsumeIterator < ( ComponentValue , SourceLocation ) > ) ,
25- }
26-
27-
28- impl ComponentValueIterator {
29- #[ inline]
30- pub fn from_str ( input : ~str ) -> ComponentValueIterator {
31- ParserIter ( Parser :: from_str ( input) )
32- }
33-
34- #[ inline]
35- pub fn from_vector ( values : ~[ ( ComponentValue , SourceLocation ) ] ) -> ComponentValueIterator {
36- VectorIter ( values. consume_iter ( ) )
37- }
38-
39- #[ inline]
40- pub fn next_non_whitespace ( & mut self ) -> Option < ( ComponentValue , SourceLocation ) > {
41- for ( component_value, location) in * self {
42- if component_value != WhiteSpace { return Some ( ( component_value, location) ) }
43- }
44- None
45- }
46- }
47-
48-
49- impl Iterator < ( ComponentValue , SourceLocation ) > for ComponentValueIterator {
50- fn next ( & mut self ) -> Option < ( ComponentValue , SourceLocation ) > {
51- match self {
52- & ParserIter ( ref mut parser) => next_component_value ( parser) ,
53- & VectorIter ( ref mut iter) => iter. next ( )
54- }
55- }
56- }
5717
5818
5919// Work around "error: cannot borrow `*iter` as mutable more than once at a time"
@@ -68,7 +28,7 @@ macro_rules! for_iter(
6828
6929
7030/// Call repeatedly for the top-level of a CSS stylesheet
71- pub fn parse_stylesheet_rule ( iter : & mut ComponentValueIterator ) -> Option < Result < Rule , ErrorReason > > {
31+ pub fn parse_stylesheet_rule < T : Iterator < Node > > ( iter : & mut T ) -> Option < Result < Rule , ErrorReason > > {
7232 for_iter ! ( iter, ( component_value, location) , {
7333 match component_value {
7434 WhiteSpace | CDO | CDC => ( ) ,
@@ -85,7 +45,7 @@ pub fn parse_stylesheet_rule(iter: &mut ComponentValueIterator) -> Option<Result
8545
8646/// Call repeatedly for a non-top level list of rules eg. the content of an @media rule.
8747/// Same as parse_stylesheet() except for the handling of top-level CDO and CDC
88- pub fn parse_rule ( iter : & mut ComponentValueIterator ) -> Option < Result < Rule , ErrorReason > > {
48+ pub fn parse_rule < T : Iterator < Node > > ( iter : & mut T ) -> Option < Result < Rule , ErrorReason > > {
8949 for_iter ! ( iter, ( component_value, location) , {
9050 match component_value {
9151 WhiteSpace => ( ) ,
@@ -101,19 +61,19 @@ pub fn parse_rule(iter: &mut ComponentValueIterator) -> Option<Result<Rule, Erro
10161
10262
10363/// Used eg. for CSSRuleList.insertRule()
104- pub fn parse_one_rule ( iter : & mut ComponentValueIterator ) -> Result < Rule , ErrorReason > {
64+ pub fn parse_one_rule < T : Iterator < Node > > ( iter : & mut T ) -> Result < Rule , ErrorReason > {
10565 match parse_rule ( iter) {
10666 None => Err ( ErrEmptyInput ) ,
107- Some ( result) => if result. is_err ( ) || iter . next_non_whitespace ( ) . is_none ( ) { result }
67+ Some ( result) => if result. is_err ( ) || next_non_whitespace ( iter ) . is_none ( ) { result }
10868 else { Err ( ErrExtraInput ) }
10969 }
11070}
11171
11272
11373/// Call repeatedly of a list of declarations.
11474/// @page in CSS 2.1, all declaration lists in level 3
115- pub fn parse_declaration_or_at_rule ( iter : & mut ComponentValueIterator )
116- -> Option < Result < DeclarationListItem , ErrorReason > > {
75+ pub fn parse_declaration_or_at_rule < T : Iterator < Node > > ( iter : & mut T )
76+ -> Option < Result < DeclarationListItem , ErrorReason > > {
11777 for_iter ! ( iter, ( component_value, location) , {
11878 match component_value {
11979 WhiteSpace | Semicolon => ( ) ,
@@ -133,25 +93,24 @@ pub fn parse_declaration_or_at_rule(iter: &mut ComponentValueIterator)
13393
13494
13595/// Used eg. in @supports
136- pub fn parse_one_declaration ( iter : & mut ComponentValueIterator ) -> Result < Declaration , ErrorReason > {
137- match iter . next_non_whitespace ( ) {
96+ pub fn parse_one_declaration < T : Iterator < Node > > ( iter : & mut T ) -> Result < Declaration , ErrorReason > {
97+ match next_non_whitespace ( iter ) {
13898 None => Err ( ErrEmptyInput ) ,
13999 Some ( item) => {
140100 let result = parse_declaration ( iter, item) ;
141- if result. is_err ( ) || iter . next_non_whitespace ( ) . is_none ( ) { result }
101+ if result. is_err ( ) || next_non_whitespace ( iter ) . is_none ( ) { result }
142102 else { Err ( ErrExtraInput ) }
143103 }
144104 }
145105}
146106
147107
148108/// Used eg. in attr(foo, color)
149- pub fn parse_one_component_value ( iter : & mut ComponentValueIterator )
150- -> Result < ( ComponentValue , SourceLocation ) , ErrorReason > {
151- match iter. next_non_whitespace ( ) {
109+ pub fn parse_one_component_value < T : Iterator < Node > > ( iter : & mut T ) -> Result < Node , ErrorReason > {
110+ match next_non_whitespace ( iter) {
152111 None => Err ( ErrEmptyInput ) ,
153112 Some ( item) => {
154- if iter . next_non_whitespace ( ) . is_none ( ) { Ok ( item) }
113+ if next_non_whitespace ( iter ) . is_none ( ) { Ok ( item) }
155114 else { Err ( ErrExtraInput ) }
156115 }
157116 }
@@ -161,7 +120,7 @@ pub fn parse_one_component_value(iter: &mut ComponentValueIterator)
161120// *********** End of public API ***********
162121
163122
164- fn parse_at_rule ( iter : & mut ComponentValueIterator , name : ~str , location : SourceLocation )
123+ fn parse_at_rule < T : Iterator < Node > > ( iter : & mut T , name : ~str , location : SourceLocation )
165124 -> AtRule {
166125 let mut prelude = ~[ ] ;
167126 let mut block = None ;
@@ -176,8 +135,8 @@ fn parse_at_rule(iter: &mut ComponentValueIterator, name: ~str, location: Source
176135}
177136
178137
179- fn parse_qualified_rule ( iter : & mut ComponentValueIterator , first : ( ComponentValue , SourceLocation ) )
180- -> Result < QualifiedRule , ErrorReason > {
138+ fn parse_qualified_rule < T : Iterator < Node > > ( iter : & mut T , first : Node )
139+ -> Result < QualifiedRule , ErrorReason > {
181140 match first {
182141 ( CurlyBracketBlock ( content) , location)
183142 => return Ok ( QualifiedRule { location : location, prelude : ~[ ] , block : content } ) ,
@@ -195,13 +154,13 @@ fn parse_qualified_rule(iter: &mut ComponentValueIterator, first: (ComponentValu
195154}
196155
197156
198- fn parse_declaration ( iter : & mut ComponentValueIterator , first : ( ComponentValue , SourceLocation ) )
199- -> Result < Declaration , ErrorReason > {
157+ fn parse_declaration < T : Iterator < Node > > ( iter : & mut T , first : Node )
158+ -> Result < Declaration , ErrorReason > {
200159 let ( name, location) = match first {
201160 ( Ident ( name) , location) => ( name, location) ,
202161 _ => return Err ( ErrInvalidDeclarationSyntax )
203162 } ;
204- match iter . next_non_whitespace ( ) {
163+ match next_non_whitespace ( iter ) {
205164 Some ( ( Colon , _) ) => ( ) ,
206165 _ => return Err ( ErrInvalidDeclarationSyntax ) ,
207166 }
@@ -224,15 +183,24 @@ fn parse_declaration(iter: &mut ComponentValueIterator, first: (ComponentValue,
224183
225184
226185#[ inline]
227- fn parse_declaration_important ( iter : & mut ComponentValueIterator ) -> bool {
228- let ident_value = match iter . next_non_whitespace ( ) {
186+ fn parse_declaration_important < T : Iterator < Node > > ( iter : & mut T ) -> bool {
187+ let ident_value = match next_non_whitespace ( iter ) {
229188 Some ( ( Ident ( value) , _) ) => value,
230189 _ => return false ,
231190 } ;
232191 if !eq_ignore_ascii_case ( ident_value, "important" ) { return false }
233- match iter . next_non_whitespace ( ) {
192+ match next_non_whitespace ( iter ) {
234193 Some ( ( Semicolon , _) ) => true ,
235194 None => true ,
236195 _ => false
237196 }
238197}
198+
199+
200+ #[ inline]
201+ fn next_non_whitespace < T : Iterator < Node > > ( iter : & mut T ) -> Option < Node > {
202+ for ( component_value, location) in * iter {
203+ if component_value != WhiteSpace { return Some ( ( component_value, location) ) }
204+ }
205+ None
206+ }
0 commit comments