Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove SourceLocation in functions, (), [], preludes, and declaration…
… values.
  • Loading branch information
SimonSapin committed Aug 8, 2013
commit 8d4343b09bd274158467cf40d7900f4696b54e5c
18 changes: 9 additions & 9 deletions ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ pub enum ComponentValue {
CDC, // -->

// Function
Function(~str, ~[Node]), // name, arguments
Function(~str, ~[ComponentValue]), // name, arguments

// Simple block
ParenthesisBlock(~[Node]), // (…)
SquareBracketBlock(~[Node]), // […]
ParenthesisBlock(~[ComponentValue]), // (…)
SquareBracketBlock(~[ComponentValue]), // […]
CurlyBracketBlock(~[Node]), // {…}

// These are always invalid
Expand All @@ -73,22 +73,22 @@ pub enum ComponentValue {
pub struct Declaration {
location: SourceLocation,
name: ~str,
value: ~[Node],
value: ~[ComponentValue],
important: bool,
}

#[deriving(Eq)]
pub struct QualifiedRule {
location: SourceLocation,
prelude: ~[Node],
prelude: ~[ComponentValue],
block: ~[Node],
}

#[deriving(Eq)]
pub struct AtRule {
location: SourceLocation,
name: ~str,
prelude: ~[Node],
prelude: ~[ComponentValue],
block: Option<~[Node]>,
}

Expand Down Expand Up @@ -124,19 +124,19 @@ pub trait SkipWhitespaceIterable<'self> {
pub fn skip_whitespace(self) -> SkipWhitespaceIterator<'self>;
}

impl<'self> SkipWhitespaceIterable<'self> for &'self [Node] {
impl<'self> SkipWhitespaceIterable<'self> for &'self [ComponentValue] {
pub fn skip_whitespace(self) -> SkipWhitespaceIterator<'self> {
SkipWhitespaceIterator{ iter: self.iter() }
}
}

struct SkipWhitespaceIterator<'self> {
iter: vec::VecIterator<'self, Node>,
iter: vec::VecIterator<'self, ComponentValue>,
}

impl<'self> Iterator<&'self ComponentValue> for SkipWhitespaceIterator<'self> {
fn next(&mut self) -> Option<&'self ComponentValue> {
for &(ref component_value, _) in self.iter {
for component_value in self.iter {
if component_value != &WhiteSpace { return Some(component_value) }
}
None
Expand Down
2 changes: 1 addition & 1 deletion color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fn parse_color_hash(value: &str) -> Option<Color> {


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

Expand Down
38 changes: 20 additions & 18 deletions parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ pub fn parse_one_rule<T: Iterator<Node>>(iter: T) -> Result<Rule, ErrorReason> {
pub fn parse_one_declaration<T: Iterator<Node>>(mut iter: T) -> Result<Declaration, ErrorReason> {
match next_non_whitespace(&mut iter) {
None => Err(ErrEmptyInput),
Some(item) => {
let result = parse_declaration(&mut iter, item);
Some((component_value, location)) => {
let result = parse_declaration(&mut iter, component_value, location);
if result.is_err() || next_non_whitespace(&mut iter).is_none() { result }
else { Err(ErrExtraInput) }
}
Expand Down Expand Up @@ -113,7 +113,7 @@ impl<T: Iterator<Node>> Iterator<Result<Rule, ErrorReason>> for StylesheetParser
match component_value {
WhiteSpace | CDO | CDC => (),
AtKeyword(name) => return Some(Ok(AtRule(parse_at_rule(iter, name, location)))),
_ => return Some(match parse_qualified_rule(iter, (component_value, location)) {
_ => return Some(match parse_qualified_rule(iter, component_value, location) {
Ok(rule) => Ok(QualifiedRule(rule)),
Err(reason) => Err(reason),
}),
Expand All @@ -131,7 +131,7 @@ impl<T: Iterator<Node>> Iterator<Result<Rule, ErrorReason>> for RuleListParser<T
match component_value {
WhiteSpace => (),
AtKeyword(name) => return Some(Ok(AtRule(parse_at_rule(iter, name, location)))),
_ => return Some(match parse_qualified_rule(iter, (component_value, location)) {
_ => return Some(match parse_qualified_rule(iter, component_value, location) {
Ok(rule) => Ok(QualifiedRule(rule)),
Err(reason) => Err(reason),
}),
Expand All @@ -151,7 +151,7 @@ for DeclarationListParser<T> {
WhiteSpace | Semicolon => (),
AtKeyword(name)
=> return Some(Ok(Decl_AtRule(parse_at_rule(iter, name, location)))),
_ => return Some(match parse_declaration(iter, (component_value, location)) {
_ => return Some(match parse_declaration(iter, component_value, location) {
Ok(declaration) => Ok(Declaration(declaration)),
Err(reason) => {
// Find the end of the declaration
Expand All @@ -170,40 +170,42 @@ fn parse_at_rule<T: Iterator<Node>>(iter: &mut T, name: ~str, location: SourceLo
-> AtRule {
let mut prelude = ~[];
let mut block = None;
for_iter!(iter, (component_value, location), {
for_iter!(iter, (component_value, _location), {
match component_value {
CurlyBracketBlock(content) => { block = Some(content); break },
Semicolon => break,
component_value => prelude.push((component_value, location)),
component_value => prelude.push(component_value),
}
})
AtRule {location: location, name: name, prelude: prelude, block: block}
}


fn parse_qualified_rule<T: Iterator<Node>>(iter: &mut T, first: Node)
-> Result<QualifiedRule, ErrorReason> {
fn parse_qualified_rule<T: Iterator<Node>>(iter: &mut T, first: ComponentValue,
location: SourceLocation)
-> Result<QualifiedRule, ErrorReason> {
match first {
(CurlyBracketBlock(content), location)
CurlyBracketBlock(content)
=> return Ok(QualifiedRule { location: location, prelude: ~[], block: content }),
_ => (),
}
let mut prelude = ~[first];
for_iter!(iter, (component_value, location), {
for_iter!(iter, (component_value, _location), {
match component_value {
CurlyBracketBlock(content)
=> return Ok(QualifiedRule {location: location, prelude: prelude, block: content}),
component_value => prelude.push((component_value, location)),
component_value => prelude.push(component_value),
}
})
Err(ErrMissingQualifiedRuleBlock)
}


fn parse_declaration<T: Iterator<Node>>(iter: &mut T, first: Node)
-> Result<Declaration, ErrorReason> {
let (name, location) = match first {
(Ident(name), location) => (name, location),
fn parse_declaration<T: Iterator<Node>>(iter: &mut T, first: ComponentValue,
location: SourceLocation)
-> Result<Declaration, ErrorReason> {
let name = match first {
Ident(name) => name,
_ => return Err(ErrInvalidDeclarationSyntax)
};
match next_non_whitespace(iter) {
Expand All @@ -212,7 +214,7 @@ fn parse_declaration<T: Iterator<Node>>(iter: &mut T, first: Node)
}
let mut value = ~[];
let mut important = false;
for_iter!(iter, (component_value, location), {
for_iter!(iter, (component_value, _location), {
match component_value {
Semicolon => break,
Delim('!') => if parse_declaration_important(iter) {
Expand All @@ -221,7 +223,7 @@ fn parse_declaration<T: Iterator<Node>>(iter: &mut T, first: Node)
} else {
return Err(ErrInvalidBangImportantSyntax)
},
component_value => value.push((component_value, location)),
component_value => value.push(component_value),
}
})
Ok(Declaration{location: location, name: name, value: value, important: important})
Expand Down
13 changes: 6 additions & 7 deletions tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,7 @@ impl ToJson for AtRule {
match *self {
AtRule{name: ref name, prelude: ref prelude, block: ref block, _}
=> json::List(~[json::String(~"at-rule"), name.to_json(),
json::List(list_to_json(prelude)),
block.map(list_to_json).to_json()])
prelude.to_json(), block.map(list_to_json).to_json()])
}
}
}
Expand All @@ -271,7 +270,7 @@ impl ToJson for QualifiedRule {
match *self {
QualifiedRule{prelude: ref prelude, block: ref block, _}
=> json::List(~[json::String(~"qualified rule"),
json::List(list_to_json(prelude)), json::List(list_to_json(block))])
prelude.to_json(), json::List(list_to_json(block))])
}
}
}
Expand All @@ -282,7 +281,7 @@ impl ToJson for Declaration {
match *self {
Declaration{name: ref name, value: ref value, important: ref important, _}
=> json::List(~[json::String(~"declaration"), name.to_json(),
json::List(list_to_json(value)), important.to_json()])
value.to_json(), important.to_json()])
}
}
}
Expand Down Expand Up @@ -335,11 +334,11 @@ impl ToJson for ComponentValue {
CDC => JString(~"-->"),

Function(ref name, ref arguments)
=> JList(~[JString(~"function"), name.to_json()] + list_to_json(arguments)),
=> JList(~[JString(~"function"), name.to_json()] + arguments.map(|a| a.to_json())),
ParenthesisBlock(ref content)
=> JList(~[JString(~"()")] + list_to_json(content)),
=> JList(~[JString(~"()")] + content.map(|c| c.to_json())),
SquareBracketBlock(ref content)
=> JList(~[JString(~"[]")] + list_to_json(content)),
=> JList(~[JString(~"[]")] + content.map(|c| c.to_json())),
CurlyBracketBlock(ref content)
=> JList(~[JString(~"{}")] + list_to_json(content)),

Expand Down
20 changes: 18 additions & 2 deletions tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ fn next_component_value(tokenizer: &mut Tokenizer) -> Option<Node> {
if tokenizer.starts_with("^=") { tokenizer.position += 2; PrefixMatch }
else { tokenizer.position += 1; Delim(c) }
},
'{' => CurlyBracketBlock(consume_block(tokenizer, CloseCurlyBracket)),
'{' => CurlyBracketBlock(consume_block_with_location(tokenizer, CloseCurlyBracket)),
'|' => {
if tokenizer.starts_with("|=") { tokenizer.position += 2; DashMatch }
else if tokenizer.starts_with("||") { tokenizer.position += 2; Column }
Expand Down Expand Up @@ -274,7 +274,23 @@ fn consume_comments(tokenizer: &mut Tokenizer) {
}


fn consume_block(tokenizer: &mut Tokenizer, ending_token: ComponentValue) -> ~[Node] {
fn consume_block(tokenizer: &mut Tokenizer, ending_token: ComponentValue) -> ~[ComponentValue] {
tokenizer.position += 1; // Skip the initial {[(
let mut content = ~[];
loop {
match next_component_value(tokenizer) {
Some((component_value, _location)) => {
if component_value == ending_token { break }
else { content.push(component_value) }
},
None => break,
}
}
content
}


fn consume_block_with_location(tokenizer: &mut Tokenizer, ending_token: ComponentValue) -> ~[Node] {
tokenizer.position += 1; // Skip the initial {[(
let mut content = ~[];
loop {
Expand Down