@@ -21,15 +21,15 @@ use ast::*;
21
21
22
22
23
23
/// Parse top-level of a CSS stylesheet.
24
- /// Return a Iterator<Result<Rule, ErrorReason >>
24
+ /// Return a Iterator<Result<Rule, SyntaxError >>
25
25
#[ inline]
26
26
pub fn parse_stylesheet_rules < T : Iterator < Node > > ( iter : T ) -> StylesheetParser < T > {
27
27
StylesheetParser ( iter)
28
28
}
29
29
30
30
31
31
/// Parse a non-top level list of rules eg. the content of an @media rule.
32
- /// Return a Iterator<Result<Rule, ErrorReason >>
32
+ /// Return a Iterator<Result<Rule, SyntaxError >>
33
33
#[ inline]
34
34
pub fn parse_rule_list < T : Iterator < Node > > ( iter : T ) -> RuleListParser < T > {
35
35
RuleListParser ( iter)
@@ -38,7 +38,7 @@ pub fn parse_rule_list<T: Iterator<Node>>(iter: T) -> RuleListParser<T> {
38
38
39
39
/// Parse a list of declarations and at-rules,
40
40
/// like @page in CSS 2.1, all declaration lists in level 3
41
- /// Return a Iterator<Result<DeclarationListItem, ErrorReason >>
41
+ /// Return a Iterator<Result<DeclarationListItem, SyntaxError >>
42
42
#[ inline]
43
43
pub fn parse_declaration_list < T : Iterator < Node > > ( iter : T ) -> DeclarationListParser < T > {
44
44
DeclarationListParser ( iter)
@@ -47,27 +47,33 @@ pub fn parse_declaration_list<T: Iterator<Node>>(iter: T) -> DeclarationListPars
47
47
48
48
/// Parse a single rule.
49
49
/// Used eg. for CSSRuleList.insertRule()
50
- pub fn parse_one_rule < T : Iterator < Node > > ( iter : T ) -> Result < Rule , ErrorReason > {
50
+ pub fn parse_one_rule < T : Iterator < Node > > ( iter : T ) -> Result < Rule , SyntaxError > {
51
51
let mut parser = RuleListParser ( iter) ;
52
52
match parser. next ( ) {
53
- None => Err ( ErrEmptyInput ) ,
53
+ None => error ( START_LOCATION , ErrEmptyInput ) ,
54
54
Some ( result) => {
55
- if result. is_err ( ) || next_non_whitespace ( & mut * parser) . is_none ( ) { result }
56
- else { Err ( ErrExtraInput ) }
55
+ if result. is_err ( ) { result }
56
+ else { match next_non_whitespace ( & mut * parser) {
57
+ None => result,
58
+ Some ( ( _component_value, location) ) => error ( location, ErrExtraInput ) ,
59
+ } }
57
60
}
58
61
}
59
62
}
60
63
61
64
62
65
/// Parse a single declaration (not an at-rule)
63
66
/// Used eg. in @supports
64
- pub fn parse_one_declaration < T : Iterator < Node > > ( mut iter : T ) -> Result < Declaration , ErrorReason > {
67
+ pub fn parse_one_declaration < T : Iterator < Node > > ( mut iter : T ) -> Result < Declaration , SyntaxError > {
65
68
match next_non_whitespace ( & mut iter) {
66
- None => Err ( ErrEmptyInput ) ,
69
+ None => error ( START_LOCATION , ErrEmptyInput ) ,
67
70
Some ( ( component_value, location) ) => {
68
71
let result = parse_declaration ( & mut iter, component_value, location) ;
69
- if result. is_err ( ) || next_non_whitespace ( & mut iter) . is_none ( ) { result }
70
- else { Err ( ErrExtraInput ) }
72
+ if result. is_err ( ) { result }
73
+ else { match next_non_whitespace ( & mut iter) {
74
+ None => result,
75
+ Some ( ( _component_value, location) ) => error ( location, ErrExtraInput ) ,
76
+ } }
71
77
}
72
78
}
73
79
}
@@ -76,12 +82,14 @@ pub fn parse_one_declaration<T: Iterator<Node>>(mut iter: T) -> Result<Declarati
76
82
/// Parse a single component value.
77
83
/// Used eg. in attr(foo, color)
78
84
pub fn parse_one_component_value < T : Iterator < Node > > ( mut iter : T )
79
- -> Result < ComponentValue , ErrorReason > {
85
+ -> Result < ComponentValue , SyntaxError > {
80
86
match next_non_whitespace ( & mut iter) {
81
- None => Err ( ErrEmptyInput ) ,
87
+ None => error ( START_LOCATION , ErrEmptyInput ) ,
82
88
Some ( ( component_value, _location) ) => {
83
- if next_non_whitespace ( & mut iter) . is_none ( ) { Ok ( component_value) }
84
- else { Err ( ErrExtraInput ) }
89
+ match next_non_whitespace ( & mut iter) {
90
+ None => Ok ( component_value) ,
91
+ Some ( ( _component_value, location) ) => error ( location, ErrExtraInput ) ,
92
+ }
85
93
}
86
94
}
87
95
}
@@ -106,16 +114,16 @@ macro_rules! for_iter(
106
114
)
107
115
108
116
109
- impl < T : Iterator < Node > > Iterator < Result < Rule , ErrorReason > > for StylesheetParser < T > {
110
- fn next ( & mut self ) -> Option < Result < Rule , ErrorReason > > {
117
+ impl < T : Iterator < Node > > Iterator < Result < Rule , SyntaxError > > for StylesheetParser < T > {
118
+ fn next ( & mut self ) -> Option < Result < Rule , SyntaxError > > {
111
119
let iter = & mut * * self ;
112
120
for_iter ! ( iter, ( component_value, location) , {
113
121
match component_value {
114
122
WhiteSpace | CDO | CDC => ( ) ,
115
123
AtKeyword ( name) => return Some ( Ok ( AtRule ( parse_at_rule( iter, name, location) ) ) ) ,
116
124
_ => return Some ( match parse_qualified_rule( iter, component_value, location) {
117
125
Ok ( rule) => Ok ( QualifiedRule ( rule) ) ,
118
- Err ( reason ) => Err ( reason ) ,
126
+ Err ( e ) => Err ( e ) ,
119
127
} ) ,
120
128
}
121
129
} )
@@ -124,16 +132,16 @@ impl<T: Iterator<Node>> Iterator<Result<Rule, ErrorReason>> for StylesheetParser
124
132
}
125
133
126
134
127
- impl < T : Iterator < Node > > Iterator < Result < Rule , ErrorReason > > for RuleListParser < T > {
128
- fn next ( & mut self ) -> Option < Result < Rule , ErrorReason > > {
135
+ impl < T : Iterator < Node > > Iterator < Result < Rule , SyntaxError > > for RuleListParser < T > {
136
+ fn next ( & mut self ) -> Option < Result < Rule , SyntaxError > > {
129
137
let iter = & mut * * self ;
130
138
for_iter ! ( iter, ( component_value, location) , {
131
139
match component_value {
132
140
WhiteSpace => ( ) ,
133
141
AtKeyword ( name) => return Some ( Ok ( AtRule ( parse_at_rule( iter, name, location) ) ) ) ,
134
142
_ => return Some ( match parse_qualified_rule( iter, component_value, location) {
135
143
Ok ( rule) => Ok ( QualifiedRule ( rule) ) ,
136
- Err ( reason ) => Err ( reason ) ,
144
+ Err ( e ) => Err ( e ) ,
137
145
} ) ,
138
146
}
139
147
} )
@@ -142,9 +150,9 @@ impl<T: Iterator<Node>> Iterator<Result<Rule, ErrorReason>> for RuleListParser<T
142
150
}
143
151
144
152
145
- impl < T : Iterator < Node > > Iterator < Result < DeclarationListItem , ErrorReason > >
153
+ impl < T : Iterator < Node > > Iterator < Result < DeclarationListItem , SyntaxError > >
146
154
for DeclarationListParser < T > {
147
- fn next ( & mut self ) -> Option < Result < DeclarationListItem , ErrorReason > > {
155
+ fn next ( & mut self ) -> Option < Result < DeclarationListItem , SyntaxError > > {
148
156
let iter = & mut * * self ;
149
157
for_iter ! ( iter, ( component_value, location) , {
150
158
match component_value {
@@ -153,10 +161,10 @@ for DeclarationListParser<T> {
153
161
=> return Some ( Ok ( Decl_AtRule ( parse_at_rule( iter, name, location) ) ) ) ,
154
162
_ => return Some ( match parse_declaration( iter, component_value, location) {
155
163
Ok ( declaration) => Ok ( Declaration ( declaration) ) ,
156
- Err ( reason ) => {
164
+ Err ( e ) => {
157
165
// Find the end of the declaration
158
166
for ( v, _) in * iter { if v == Semicolon { break } }
159
- Err ( reason )
167
+ Err ( e )
160
168
}
161
169
} ) ,
162
170
}
@@ -183,7 +191,7 @@ fn parse_at_rule<T: Iterator<Node>>(iter: &mut T, name: ~str, location: SourceLo
183
191
184
192
fn parse_qualified_rule < T : Iterator < Node > > ( iter : & mut T , first : ComponentValue ,
185
193
location : SourceLocation )
186
- -> Result < QualifiedRule , ErrorReason > {
194
+ -> Result < QualifiedRule , SyntaxError > {
187
195
match first {
188
196
CurlyBracketBlock ( content)
189
197
=> return Ok ( QualifiedRule { location : location, prelude : ~[ ] , block : content } ) ,
@@ -197,20 +205,20 @@ fn parse_qualified_rule<T: Iterator<Node>>(iter: &mut T, first: ComponentValue,
197
205
component_value => prelude. push( component_value) ,
198
206
}
199
207
} )
200
- Err ( ErrMissingQualifiedRuleBlock )
208
+ error ( location , ErrMissingQualifiedRuleBlock )
201
209
}
202
210
203
211
204
212
fn parse_declaration < T : Iterator < Node > > ( iter : & mut T , first : ComponentValue ,
205
213
location : SourceLocation )
206
- -> Result < Declaration , ErrorReason > {
214
+ -> Result < Declaration , SyntaxError > {
207
215
let name = match first {
208
216
Ident ( name) => name,
209
- _ => return Err ( ErrInvalidDeclarationSyntax )
217
+ _ => return error ( location , ErrInvalidDeclarationSyntax )
210
218
} ;
211
219
match next_non_whitespace ( iter) {
212
220
Some ( ( Colon , _) ) => ( ) ,
213
- _ => return Err ( ErrInvalidDeclarationSyntax ) ,
221
+ _ => return error ( location , ErrInvalidDeclarationSyntax ) ,
214
222
}
215
223
let mut value = ~[ ] ;
216
224
let mut important = false ;
@@ -221,7 +229,7 @@ fn parse_declaration<T: Iterator<Node>>(iter: &mut T, first: ComponentValue,
221
229
important = true ;
222
230
break
223
231
} else {
224
- return Err ( ErrInvalidBangImportantSyntax )
232
+ return error ( location , ErrInvalidBangImportantSyntax )
225
233
} ,
226
234
component_value => value. push( component_value) ,
227
235
}
@@ -252,3 +260,13 @@ fn next_non_whitespace<T: Iterator<Node>>(iter: &mut T) -> Option<Node> {
252
260
}
253
261
None
254
262
}
263
+
264
+
265
+ #[ inline]
266
+ fn error < T > ( location : SourceLocation , reason : ErrorReason ) -> Result < T , SyntaxError > {
267
+ Err ( SyntaxError { location : location, reason : reason} )
268
+ }
269
+
270
+
271
+ // When parsing one thing on an empty input
272
+ static START_LOCATION : SourceLocation = SourceLocation { line : 1 , column : 1 } ;
0 commit comments