Skip to content

Commit 1f441e2

Browse files
committed
Add parse_one_component_value()
It was previously implemented ad-hoc in tests.rs
1 parent 778f262 commit 1f441e2

File tree

3 files changed

+36
-31
lines changed

3 files changed

+36
-31
lines changed

ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ pub struct NumericValue {
1313

1414
#[deriving(Eq)]
1515
pub struct SourceLocation {
16-
line: uint,
17-
column: uint,
16+
line: uint, // First line is 1
17+
column: uint, // First character of a line is at column 1
1818
}
1919

2020

parser.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,19 @@ pub fn parse_one_declaration(iter: &mut ComponentValueIterator) -> Result<Declar
150150
}
151151

152152

153+
/// Used eg. in attr(foo, color)
154+
pub fn parse_one_component_value(iter: &mut ComponentValueIterator)
155+
-> Result<(ComponentValue, SourceLocation), ErrorReason> {
156+
match iter.next_non_whitespace() {
157+
None => Err(ErrEmptyInput),
158+
Some(item) => {
159+
if iter.next_non_whitespace().is_none() { Ok(item) }
160+
else { Err(ErrExtraInput) }
161+
}
162+
}
163+
}
164+
165+
153166
// *********** End of public API ***********
154167

155168

tests.rs

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ fn run_json_tests(json_data: &str, parse: &fn (input: ~str) -> json::Json) {
6464
#[test]
6565
fn component_value_list() {
6666
do run_json_tests(include_str!("css-parsing-tests/component_value_list.json")) |input| {
67-
let mut parser = Parser::from_str(input);
67+
let parser = &mut Parser::from_str(input);
6868
let mut results = ~[];
6969
loop {
70-
match next_component_value(&mut parser) {
70+
match next_component_value(parser) {
7171
Some((c, _)) => results.push(c),
7272
None => break,
7373
}
@@ -80,13 +80,9 @@ fn component_value_list() {
8080
#[test]
8181
fn one_component_value() {
8282
do run_json_tests(include_str!("css-parsing-tests/one_component_value.json")) |input| {
83-
let mut iter = ComponentValueIterator::from_str(input);
84-
match iter.next_non_whitespace() {
85-
None => json::List(~[json::String(~"error"), json::String(~"empty")]),
86-
Some((component_value, _)) => match iter.next_non_whitespace() {
87-
Some(_) => json::List(~[json::String(~"error"), json::String(~"extra-input")]),
88-
None => component_value.to_json(),
89-
}
83+
match parse_one_component_value(&mut ComponentValueIterator::from_str(input)) {
84+
Ok((component_value, _location)) => component_value.to_json(),
85+
Err(reason) => reason.to_json(),
9086
}
9187
}
9288
}
@@ -95,15 +91,12 @@ fn one_component_value() {
9591
#[test]
9692
fn declaration_list() {
9793
do run_json_tests(include_str!("css-parsing-tests/declaration_list.json")) |input| {
98-
let mut iter = ComponentValueIterator::from_str(input);
94+
let iter = &mut ComponentValueIterator::from_str(input);
9995
let mut declarations = ~[];
10096
loop {
101-
match parse_declaration_or_at_rule(&mut iter) {
97+
match parse_declaration_or_at_rule(iter) {
10298
None => break,
103-
Some(result) => declarations.push(match result {
104-
Ok(declaration) => declaration.to_json(),
105-
Err(reason) => reason.to_json(),
106-
})
99+
Some(result) => declarations.push(result_to_json(result)),
107100
}
108101
}
109102
json::List(declarations)
@@ -114,26 +107,20 @@ fn declaration_list() {
114107
#[test]
115108
fn one_declaration() {
116109
do run_json_tests(include_str!("css-parsing-tests/one_declaration.json")) |input| {
117-
match parse_one_declaration(&mut ComponentValueIterator::from_str(input)) {
118-
Ok(declaration) => declaration.to_json(),
119-
Err(reason) => reason.to_json(),
120-
}
110+
result_to_json(parse_one_declaration(&mut ComponentValueIterator::from_str(input)))
121111
}
122112
}
123113

124114

125115
#[test]
126116
fn rule_list() {
127117
do run_json_tests(include_str!("css-parsing-tests/rule_list.json")) |input| {
128-
let mut iter = ComponentValueIterator::from_str(input);
118+
let iter = &mut ComponentValueIterator::from_str(input);
129119
let mut rules = ~[];
130120
loop {
131-
match parse_rule(&mut iter) {
121+
match parse_rule(iter) {
132122
None => break,
133-
Some(result) => rules.push(match result {
134-
Ok(rule) => rule.to_json(),
135-
Err(reason) => reason.to_json(),
136-
})
123+
Some(result) => rules.push(result_to_json(result)),
137124
}
138125
}
139126
json::List(rules)
@@ -144,10 +131,15 @@ fn rule_list() {
144131
#[test]
145132
fn one_rule() {
146133
do run_json_tests(include_str!("css-parsing-tests/one_rule.json")) |input| {
147-
match parse_one_rule(&mut ComponentValueIterator::from_str(input)) {
148-
Ok(rule) => rule.to_json(),
149-
Err(reason) => reason.to_json(),
150-
}
134+
result_to_json(parse_one_rule(&mut ComponentValueIterator::from_str(input)))
135+
}
136+
}
137+
138+
139+
fn result_to_json<A:ToJson, B:ToJson>(result: Result<A, B>) -> json::Json {
140+
match result {
141+
Ok(ref a) => a.to_json(),
142+
Err(ref b) => b.to_json(),
151143
}
152144
}
153145

0 commit comments

Comments
 (0)