11
11
12
12
13
13
use std:: iterator:: Iterator ;
14
- use std:: vec;
15
14
use std:: ascii:: eq_ignore_ascii_case;
16
15
17
16
use 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
- }
57
17
58
18
59
19
// Work around "error: cannot borrow `*iter` as mutable more than once at a time"
@@ -68,7 +28,7 @@ macro_rules! for_iter(
68
28
69
29
70
30
/// 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 > > {
72
32
for_iter ! ( iter, ( component_value, location) , {
73
33
match component_value {
74
34
WhiteSpace | CDO | CDC => ( ) ,
@@ -85,7 +45,7 @@ pub fn parse_stylesheet_rule(iter: &mut ComponentValueIterator) -> Option<Result
85
45
86
46
/// Call repeatedly for a non-top level list of rules eg. the content of an @media rule.
87
47
/// 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 > > {
89
49
for_iter ! ( iter, ( component_value, location) , {
90
50
match component_value {
91
51
WhiteSpace => ( ) ,
@@ -101,19 +61,19 @@ pub fn parse_rule(iter: &mut ComponentValueIterator) -> Option<Result<Rule, Erro
101
61
102
62
103
63
/// 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 > {
105
65
match parse_rule ( iter) {
106
66
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 }
108
68
else { Err ( ErrExtraInput ) }
109
69
}
110
70
}
111
71
112
72
113
73
/// Call repeatedly of a list of declarations.
114
74
/// @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 > > {
117
77
for_iter ! ( iter, ( component_value, location) , {
118
78
match component_value {
119
79
WhiteSpace | Semicolon => ( ) ,
@@ -133,25 +93,24 @@ pub fn parse_declaration_or_at_rule(iter: &mut ComponentValueIterator)
133
93
134
94
135
95
/// 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 ) {
138
98
None => Err ( ErrEmptyInput ) ,
139
99
Some ( item) => {
140
100
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 }
142
102
else { Err ( ErrExtraInput ) }
143
103
}
144
104
}
145
105
}
146
106
147
107
148
108
/// 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) {
152
111
None => Err ( ErrEmptyInput ) ,
153
112
Some ( item) => {
154
- if iter . next_non_whitespace ( ) . is_none ( ) { Ok ( item) }
113
+ if next_non_whitespace ( iter ) . is_none ( ) { Ok ( item) }
155
114
else { Err ( ErrExtraInput ) }
156
115
}
157
116
}
@@ -161,7 +120,7 @@ pub fn parse_one_component_value(iter: &mut ComponentValueIterator)
161
120
// *********** End of public API ***********
162
121
163
122
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 )
165
124
-> AtRule {
166
125
let mut prelude = ~[ ] ;
167
126
let mut block = None ;
@@ -176,8 +135,8 @@ fn parse_at_rule(iter: &mut ComponentValueIterator, name: ~str, location: Source
176
135
}
177
136
178
137
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 > {
181
140
match first {
182
141
( CurlyBracketBlock ( content) , location)
183
142
=> return Ok ( QualifiedRule { location : location, prelude : ~[ ] , block : content } ) ,
@@ -195,13 +154,13 @@ fn parse_qualified_rule(iter: &mut ComponentValueIterator, first: (ComponentValu
195
154
}
196
155
197
156
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 > {
200
159
let ( name, location) = match first {
201
160
( Ident ( name) , location) => ( name, location) ,
202
161
_ => return Err ( ErrInvalidDeclarationSyntax )
203
162
} ;
204
- match iter . next_non_whitespace ( ) {
163
+ match next_non_whitespace ( iter ) {
205
164
Some ( ( Colon , _) ) => ( ) ,
206
165
_ => return Err ( ErrInvalidDeclarationSyntax ) ,
207
166
}
@@ -224,15 +183,24 @@ fn parse_declaration(iter: &mut ComponentValueIterator, first: (ComponentValue,
224
183
225
184
226
185
#[ 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 ) {
229
188
Some ( ( Ident ( value) , _) ) => value,
230
189
_ => return false ,
231
190
} ;
232
191
if !eq_ignore_ascii_case ( ident_value, "important" ) { return false }
233
- match iter . next_non_whitespace ( ) {
192
+ match next_non_whitespace ( iter ) {
234
193
Some ( ( Semicolon , _) ) => true ,
235
194
None => true ,
236
195
_ => false
237
196
}
238
197
}
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