Skip to content

Commit 8d4343b

Browse files
committed
Remove SourceLocation in functions, (), [], preludes, and declaration values.
1 parent 8a61436 commit 8d4343b

File tree

5 files changed

+54
-37
lines changed

5 files changed

+54
-37
lines changed

ast.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ pub enum ComponentValue {
5353
CDC, // -->
5454

5555
// Function
56-
Function(~str, ~[Node]), // name, arguments
56+
Function(~str, ~[ComponentValue]), // name, arguments
5757

5858
// Simple block
59-
ParenthesisBlock(~[Node]), // (…)
60-
SquareBracketBlock(~[Node]), // […]
59+
ParenthesisBlock(~[ComponentValue]), // (…)
60+
SquareBracketBlock(~[ComponentValue]), // […]
6161
CurlyBracketBlock(~[Node]), // {…}
6262

6363
// These are always invalid
@@ -73,22 +73,22 @@ pub enum ComponentValue {
7373
pub struct Declaration {
7474
location: SourceLocation,
7575
name: ~str,
76-
value: ~[Node],
76+
value: ~[ComponentValue],
7777
important: bool,
7878
}
7979

8080
#[deriving(Eq)]
8181
pub struct QualifiedRule {
8282
location: SourceLocation,
83-
prelude: ~[Node],
83+
prelude: ~[ComponentValue],
8484
block: ~[Node],
8585
}
8686

8787
#[deriving(Eq)]
8888
pub struct AtRule {
8989
location: SourceLocation,
9090
name: ~str,
91-
prelude: ~[Node],
91+
prelude: ~[ComponentValue],
9292
block: Option<~[Node]>,
9393
}
9494

@@ -124,19 +124,19 @@ pub trait SkipWhitespaceIterable<'self> {
124124
pub fn skip_whitespace(self) -> SkipWhitespaceIterator<'self>;
125125
}
126126

127-
impl<'self> SkipWhitespaceIterable<'self> for &'self [Node] {
127+
impl<'self> SkipWhitespaceIterable<'self> for &'self [ComponentValue] {
128128
pub fn skip_whitespace(self) -> SkipWhitespaceIterator<'self> {
129129
SkipWhitespaceIterator{ iter: self.iter() }
130130
}
131131
}
132132

133133
struct SkipWhitespaceIterator<'self> {
134-
iter: vec::VecIterator<'self, Node>,
134+
iter: vec::VecIterator<'self, ComponentValue>,
135135
}
136136

137137
impl<'self> Iterator<&'self ComponentValue> for SkipWhitespaceIterator<'self> {
138138
fn next(&mut self) -> Option<&'self ComponentValue> {
139-
for &(ref component_value, _) in self.iter {
139+
for component_value in self.iter {
140140
if component_value != &WhiteSpace { return Some(component_value) }
141141
}
142142
None

color.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ fn parse_color_hash(value: &str) -> Option<Color> {
7676

7777

7878
#[inline]
79-
fn parse_color_function(name: &str, arguments: &[(ComponentValue, SourceLocation)])
79+
fn parse_color_function(name: &str, arguments: &[ComponentValue])
8080
-> Option<Color> {
8181
let lower_name = to_ascii_lower(name);
8282

parser.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ pub fn parse_one_rule<T: Iterator<Node>>(iter: T) -> Result<Rule, ErrorReason> {
6464
pub fn parse_one_declaration<T: Iterator<Node>>(mut iter: T) -> Result<Declaration, ErrorReason> {
6565
match next_non_whitespace(&mut iter) {
6666
None => Err(ErrEmptyInput),
67-
Some(item) => {
68-
let result = parse_declaration(&mut iter, item);
67+
Some((component_value, location)) => {
68+
let result = parse_declaration(&mut iter, component_value, location);
6969
if result.is_err() || next_non_whitespace(&mut iter).is_none() { result }
7070
else { Err(ErrExtraInput) }
7171
}
@@ -113,7 +113,7 @@ impl<T: Iterator<Node>> Iterator<Result<Rule, ErrorReason>> for StylesheetParser
113113
match component_value {
114114
WhiteSpace | CDO | CDC => (),
115115
AtKeyword(name) => return Some(Ok(AtRule(parse_at_rule(iter, name, location)))),
116-
_ => return Some(match parse_qualified_rule(iter, (component_value, location)) {
116+
_ => return Some(match parse_qualified_rule(iter, component_value, location) {
117117
Ok(rule) => Ok(QualifiedRule(rule)),
118118
Err(reason) => Err(reason),
119119
}),
@@ -131,7 +131,7 @@ impl<T: Iterator<Node>> Iterator<Result<Rule, ErrorReason>> for RuleListParser<T
131131
match component_value {
132132
WhiteSpace => (),
133133
AtKeyword(name) => return Some(Ok(AtRule(parse_at_rule(iter, name, location)))),
134-
_ => return Some(match parse_qualified_rule(iter, (component_value, location)) {
134+
_ => return Some(match parse_qualified_rule(iter, component_value, location) {
135135
Ok(rule) => Ok(QualifiedRule(rule)),
136136
Err(reason) => Err(reason),
137137
}),
@@ -151,7 +151,7 @@ for DeclarationListParser<T> {
151151
WhiteSpace | Semicolon => (),
152152
AtKeyword(name)
153153
=> return Some(Ok(Decl_AtRule(parse_at_rule(iter, name, location)))),
154-
_ => return Some(match parse_declaration(iter, (component_value, location)) {
154+
_ => return Some(match parse_declaration(iter, component_value, location) {
155155
Ok(declaration) => Ok(Declaration(declaration)),
156156
Err(reason) => {
157157
// Find the end of the declaration
@@ -170,40 +170,42 @@ fn parse_at_rule<T: Iterator<Node>>(iter: &mut T, name: ~str, location: SourceLo
170170
-> AtRule {
171171
let mut prelude = ~[];
172172
let mut block = None;
173-
for_iter!(iter, (component_value, location), {
173+
for_iter!(iter, (component_value, _location), {
174174
match component_value {
175175
CurlyBracketBlock(content) => { block = Some(content); break },
176176
Semicolon => break,
177-
component_value => prelude.push((component_value, location)),
177+
component_value => prelude.push(component_value),
178178
}
179179
})
180180
AtRule {location: location, name: name, prelude: prelude, block: block}
181181
}
182182

183183

184-
fn parse_qualified_rule<T: Iterator<Node>>(iter: &mut T, first: Node)
185-
-> Result<QualifiedRule, ErrorReason> {
184+
fn parse_qualified_rule<T: Iterator<Node>>(iter: &mut T, first: ComponentValue,
185+
location: SourceLocation)
186+
-> Result<QualifiedRule, ErrorReason> {
186187
match first {
187-
(CurlyBracketBlock(content), location)
188+
CurlyBracketBlock(content)
188189
=> return Ok(QualifiedRule { location: location, prelude: ~[], block: content }),
189190
_ => (),
190191
}
191192
let mut prelude = ~[first];
192-
for_iter!(iter, (component_value, location), {
193+
for_iter!(iter, (component_value, _location), {
193194
match component_value {
194195
CurlyBracketBlock(content)
195196
=> return Ok(QualifiedRule {location: location, prelude: prelude, block: content}),
196-
component_value => prelude.push((component_value, location)),
197+
component_value => prelude.push(component_value),
197198
}
198199
})
199200
Err(ErrMissingQualifiedRuleBlock)
200201
}
201202

202203

203-
fn parse_declaration<T: Iterator<Node>>(iter: &mut T, first: Node)
204-
-> Result<Declaration, ErrorReason> {
205-
let (name, location) = match first {
206-
(Ident(name), location) => (name, location),
204+
fn parse_declaration<T: Iterator<Node>>(iter: &mut T, first: ComponentValue,
205+
location: SourceLocation)
206+
-> Result<Declaration, ErrorReason> {
207+
let name = match first {
208+
Ident(name) => name,
207209
_ => return Err(ErrInvalidDeclarationSyntax)
208210
};
209211
match next_non_whitespace(iter) {
@@ -212,7 +214,7 @@ fn parse_declaration<T: Iterator<Node>>(iter: &mut T, first: Node)
212214
}
213215
let mut value = ~[];
214216
let mut important = false;
215-
for_iter!(iter, (component_value, location), {
217+
for_iter!(iter, (component_value, _location), {
216218
match component_value {
217219
Semicolon => break,
218220
Delim('!') => if parse_declaration_important(iter) {
@@ -221,7 +223,7 @@ fn parse_declaration<T: Iterator<Node>>(iter: &mut T, first: Node)
221223
} else {
222224
return Err(ErrInvalidBangImportantSyntax)
223225
},
224-
component_value => value.push((component_value, location)),
226+
component_value => value.push(component_value),
225227
}
226228
})
227229
Ok(Declaration{location: location, name: name, value: value, important: important})

tests.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,7 @@ impl ToJson for AtRule {
259259
match *self {
260260
AtRule{name: ref name, prelude: ref prelude, block: ref block, _}
261261
=> json::List(~[json::String(~"at-rule"), name.to_json(),
262-
json::List(list_to_json(prelude)),
263-
block.map(list_to_json).to_json()])
262+
prelude.to_json(), block.map(list_to_json).to_json()])
264263
}
265264
}
266265
}
@@ -271,7 +270,7 @@ impl ToJson for QualifiedRule {
271270
match *self {
272271
QualifiedRule{prelude: ref prelude, block: ref block, _}
273272
=> json::List(~[json::String(~"qualified rule"),
274-
json::List(list_to_json(prelude)), json::List(list_to_json(block))])
273+
prelude.to_json(), json::List(list_to_json(block))])
275274
}
276275
}
277276
}
@@ -282,7 +281,7 @@ impl ToJson for Declaration {
282281
match *self {
283282
Declaration{name: ref name, value: ref value, important: ref important, _}
284283
=> json::List(~[json::String(~"declaration"), name.to_json(),
285-
json::List(list_to_json(value)), important.to_json()])
284+
value.to_json(), important.to_json()])
286285
}
287286
}
288287
}
@@ -335,11 +334,11 @@ impl ToJson for ComponentValue {
335334
CDC => JString(~"-->"),
336335
337336
Function(ref name, ref arguments)
338-
=> JList(~[JString(~"function"), name.to_json()] + list_to_json(arguments)),
337+
=> JList(~[JString(~"function"), name.to_json()] + arguments.map(|a| a.to_json())),
339338
ParenthesisBlock(ref content)
340-
=> JList(~[JString(~"()")] + list_to_json(content)),
339+
=> JList(~[JString(~"()")] + content.map(|c| c.to_json())),
341340
SquareBracketBlock(ref content)
342-
=> JList(~[JString(~"[]")] + list_to_json(content)),
341+
=> JList(~[JString(~"[]")] + content.map(|c| c.to_json())),
343342
CurlyBracketBlock(ref content)
344343
=> JList(~[JString(~"{}")] + list_to_json(content)),
345344

tokenizer.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ fn next_component_value(tokenizer: &mut Tokenizer) -> Option<Node> {
230230
if tokenizer.starts_with("^=") { tokenizer.position += 2; PrefixMatch }
231231
else { tokenizer.position += 1; Delim(c) }
232232
},
233-
'{' => CurlyBracketBlock(consume_block(tokenizer, CloseCurlyBracket)),
233+
'{' => CurlyBracketBlock(consume_block_with_location(tokenizer, CloseCurlyBracket)),
234234
'|' => {
235235
if tokenizer.starts_with("|=") { tokenizer.position += 2; DashMatch }
236236
else if tokenizer.starts_with("||") { tokenizer.position += 2; Column }
@@ -274,7 +274,23 @@ fn consume_comments(tokenizer: &mut Tokenizer) {
274274
}
275275

276276

277-
fn consume_block(tokenizer: &mut Tokenizer, ending_token: ComponentValue) -> ~[Node] {
277+
fn consume_block(tokenizer: &mut Tokenizer, ending_token: ComponentValue) -> ~[ComponentValue] {
278+
tokenizer.position += 1; // Skip the initial {[(
279+
let mut content = ~[];
280+
loop {
281+
match next_component_value(tokenizer) {
282+
Some((component_value, _location)) => {
283+
if component_value == ending_token { break }
284+
else { content.push(component_value) }
285+
},
286+
None => break,
287+
}
288+
}
289+
content
290+
}
291+
292+
293+
fn consume_block_with_location(tokenizer: &mut Tokenizer, ending_token: ComponentValue) -> ~[Node] {
278294
tokenizer.position += 1; // Skip the initial {[(
279295
let mut content = ~[];
280296
loop {

0 commit comments

Comments
 (0)