3
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
4
5
5
use std:: str:: ToStr ;
6
+ use std:: vec;
6
7
7
8
8
9
#[ deriving( Eq ) ]
@@ -20,13 +21,16 @@ pub struct SourceLocation {
20
21
}
21
22
22
23
24
+ pub type Node = ( ComponentValue , SourceLocation ) ; // TODO this is not a good name
25
+
26
+
23
27
#[ deriving( Eq ) ]
24
28
pub enum ComponentValue {
25
- // Preserved tokens. Same as in the tokenizer.
29
+ // Preserved tokens.
26
30
Ident ( ~str ) ,
27
31
AtKeyword ( ~str ) ,
28
32
Hash ( ~str ) ,
29
- IDHash ( ~str ) , // Hash token that is a valid ID selector.
33
+ IDHash ( ~str ) , // Hash that is a valid ID selector.
30
34
String ( ~str ) ,
31
35
URL ( ~str ) ,
32
36
Delim ( char ) ,
@@ -39,7 +43,7 @@ pub enum ComponentValue {
39
43
Colon , // :
40
44
Semicolon , // ;
41
45
Comma , // ,
42
- IncludeMath , // ~=
46
+ IncludeMatch , // ~=
43
47
DashMatch , // |=
44
48
PrefixMatch , // ^=
45
49
SuffixMatch , // $=
@@ -49,12 +53,12 @@ pub enum ComponentValue {
49
53
CDC , // -->
50
54
51
55
// Function
52
- Function ( ~str , ~[ ( ComponentValue , SourceLocation ) ] ) , // name, arguments
56
+ Function ( ~str , ~[ ComponentValue ] ) , // name, arguments
53
57
54
58
// Simple block
55
- ParenthesisBlock ( ~[ ( ComponentValue , SourceLocation ) ] ) , // (…)
56
- SquareBracketBlock ( ~[ ( ComponentValue , SourceLocation ) ] ) , // […]
57
- CurlyBracketBlock ( ~[ ( ComponentValue , SourceLocation ) ] ) , // {…}
59
+ ParenthesisBlock ( ~[ ComponentValue ] ) , // (…)
60
+ SquareBracketBlock ( ~[ ComponentValue ] ) , // […]
61
+ CurlyBracketBlock ( ~[ Node ] ) , // {…}
58
62
59
63
// These are always invalid
60
64
BadURL ,
@@ -69,23 +73,23 @@ pub enum ComponentValue {
69
73
pub struct Declaration {
70
74
location : SourceLocation ,
71
75
name : ~str ,
72
- value : ~[ ( ComponentValue , SourceLocation ) ] ,
76
+ value : ~[ ComponentValue ] ,
73
77
important : bool ,
74
78
}
75
79
76
80
#[ deriving( Eq ) ]
77
81
pub struct QualifiedRule {
78
82
location : SourceLocation ,
79
- prelude : ~[ ( ComponentValue , SourceLocation ) ] ,
80
- block : ~[ ( ComponentValue , SourceLocation ) ] ,
83
+ prelude : ~[ ComponentValue ] ,
84
+ block : ~[ Node ] ,
81
85
}
82
86
83
87
#[ deriving( Eq ) ]
84
88
pub struct AtRule {
85
89
location : SourceLocation ,
86
90
name : ~str ,
87
- prelude : ~[ ( ComponentValue , SourceLocation ) ] ,
88
- block : Option < ~[ ( ComponentValue , SourceLocation ) ] > ,
91
+ prelude : ~[ ComponentValue ] ,
92
+ block : Option < ~[ Node ] > ,
89
93
}
90
94
91
95
#[ deriving( Eq ) ]
@@ -101,6 +105,12 @@ pub enum Rule {
101
105
AtRule ( AtRule ) ,
102
106
}
103
107
108
+ #[ deriving( Eq ) ]
109
+ pub struct SyntaxError {
110
+ location : SourceLocation ,
111
+ reason : ErrorReason ,
112
+ }
113
+
104
114
#[ deriving( Eq ) ]
105
115
pub enum ErrorReason {
106
116
ErrEmptyInput , // Parsing a single "thing", found only whitespace.
@@ -111,6 +121,56 @@ pub enum ErrorReason {
111
121
// This is meant to be extended
112
122
}
113
123
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
+ }
116
176
}
0 commit comments