33 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44
55use std:: str:: ToStr ;
6+ use std:: vec;
67
78
89#[ deriving( Eq ) ]
@@ -20,13 +21,16 @@ pub struct SourceLocation {
2021}
2122
2223
24+ pub type Node = ( ComponentValue , SourceLocation ) ; // TODO this is not a good name
25+
26+
2327#[ deriving( Eq ) ]
2428pub enum ComponentValue {
25- // Preserved tokens. Same as in the tokenizer.
29+ // Preserved tokens.
2630 Ident ( ~str ) ,
2731 AtKeyword ( ~str ) ,
2832 Hash ( ~str ) ,
29- IDHash ( ~str ) , // Hash token that is a valid ID selector.
33+ IDHash ( ~str ) , // Hash that is a valid ID selector.
3034 String ( ~str ) ,
3135 URL ( ~str ) ,
3236 Delim ( char ) ,
@@ -39,7 +43,7 @@ pub enum ComponentValue {
3943 Colon , // :
4044 Semicolon , // ;
4145 Comma , // ,
42- IncludeMath , // ~=
46+ IncludeMatch , // ~=
4347 DashMatch , // |=
4448 PrefixMatch , // ^=
4549 SuffixMatch , // $=
@@ -49,12 +53,12 @@ pub enum ComponentValue {
4953 CDC , // -->
5054
5155 // Function
52- Function ( ~str , ~[ ( ComponentValue , SourceLocation ) ] ) , // name, arguments
56+ Function ( ~str , ~[ ComponentValue ] ) , // name, arguments
5357
5458 // Simple block
55- ParenthesisBlock ( ~[ ( ComponentValue , SourceLocation ) ] ) , // (…)
56- SquareBracketBlock ( ~[ ( ComponentValue , SourceLocation ) ] ) , // […]
57- CurlyBracketBlock ( ~[ ( ComponentValue , SourceLocation ) ] ) , // {…}
59+ ParenthesisBlock ( ~[ ComponentValue ] ) , // (…)
60+ SquareBracketBlock ( ~[ ComponentValue ] ) , // […]
61+ CurlyBracketBlock ( ~[ Node ] ) , // {…}
5862
5963 // These are always invalid
6064 BadURL ,
@@ -69,23 +73,23 @@ pub enum ComponentValue {
6973pub struct Declaration {
7074 location : SourceLocation ,
7175 name : ~str ,
72- value : ~[ ( ComponentValue , SourceLocation ) ] ,
76+ value : ~[ ComponentValue ] ,
7377 important : bool ,
7478}
7579
7680#[ deriving( Eq ) ]
7781pub struct QualifiedRule {
7882 location : SourceLocation ,
79- prelude : ~[ ( ComponentValue , SourceLocation ) ] ,
80- block : ~[ ( ComponentValue , SourceLocation ) ] ,
83+ prelude : ~[ ComponentValue ] ,
84+ block : ~[ Node ] ,
8185}
8286
8387#[ deriving( Eq ) ]
8488pub struct AtRule {
8589 location : SourceLocation ,
8690 name : ~str ,
87- prelude : ~[ ( ComponentValue , SourceLocation ) ] ,
88- block : Option < ~[ ( ComponentValue , SourceLocation ) ] > ,
91+ prelude : ~[ ComponentValue ] ,
92+ block : Option < ~[ Node ] > ,
8993}
9094
9195#[ deriving( Eq ) ]
@@ -101,6 +105,12 @@ pub enum Rule {
101105 AtRule ( AtRule ) ,
102106}
103107
108+ #[ deriving( Eq ) ]
109+ pub struct SyntaxError {
110+ location : SourceLocation ,
111+ reason : ErrorReason ,
112+ }
113+
104114#[ deriving( Eq ) ]
105115pub enum ErrorReason {
106116 ErrEmptyInput , // Parsing a single "thing", found only whitespace.
@@ -111,6 +121,56 @@ pub enum ErrorReason {
111121 // This is meant to be extended
112122}
113123
114- impl ToStr for ErrorReason {
115- fn to_str ( & self ) -> ~str { fmt ! ( "%?" , self ) }
124+ impl ToStr for SyntaxError {
125+ fn to_str ( & self ) -> ~str {
126+ fmt ! ( "%u:%u %?" , self . location. line, self . location. column, self . reason)
127+ }
128+ }
129+
130+
131+ pub trait SkipWhitespaceIterable < ' self > {
132+ pub fn skip_whitespace ( self ) -> SkipWhitespaceIterator < ' self > ;
133+ }
134+
135+ impl < ' self > SkipWhitespaceIterable < ' self > for & ' self [ ComponentValue ] {
136+ pub fn skip_whitespace ( self ) -> SkipWhitespaceIterator < ' self > {
137+ SkipWhitespaceIterator { iter_with_whitespace : self . iter ( ) }
138+ }
139+ }
140+
141+ pub struct SkipWhitespaceIterator < ' self > {
142+ iter_with_whitespace : vec:: VecIterator < ' self , ComponentValue > ,
143+ }
144+
145+ impl < ' self > Iterator < & ' self ComponentValue > for SkipWhitespaceIterator < ' self > {
146+ fn next ( & mut self ) -> Option < & ' self ComponentValue > {
147+ for component_value in self . iter_with_whitespace {
148+ if component_value != & WhiteSpace { return Some ( component_value) }
149+ }
150+ None
151+ }
152+ }
153+
154+
155+ pub trait MoveSkipWhitespaceIterable {
156+ pub fn move_skip_whitespace ( self ) -> MoveSkipWhitespaceIterator ;
157+ }
158+
159+ impl MoveSkipWhitespaceIterable for ~[ ComponentValue ] {
160+ pub fn move_skip_whitespace ( self ) -> MoveSkipWhitespaceIterator {
161+ MoveSkipWhitespaceIterator { iter_with_whitespace : self . move_iter ( ) }
162+ }
163+ }
164+
165+ pub struct MoveSkipWhitespaceIterator {
166+ iter_with_whitespace : vec:: MoveIterator < ComponentValue > ,
167+ }
168+
169+ impl Iterator < ComponentValue > for MoveSkipWhitespaceIterator {
170+ fn next ( & mut self ) -> Option < ComponentValue > {
171+ for component_value in self . iter_with_whitespace {
172+ if component_value != WhiteSpace { return Some ( component_value) }
173+ }
174+ None
175+ }
116176}
0 commit comments