Skip to content

Commit 9707ade

Browse files
committed
Started on a rust-cssparser frontend.
Also imported the test suite from less.js
1 parent 08e2d67 commit 9707ade

File tree

272 files changed

+8180
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

272 files changed

+8180
-4
lines changed

Cargo.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
[package]
2-
name = "rust"
2+
name = "cssparser"
33
version = "0.0.0"
44

55
authors = ["Your Name <your@email.com>"]
66
tags = []
77

88
[[lib]]
9-
name = "rust"
10-
path = "src/lib.rs"
9+
name = "cssparser"
10+
11+
[[bin]]
12+
name = "rust-less"
1113

1214
[dependencies.encoding]
1315
git = "https://github.com/lifthrasiir/rust-encoding.git"

src/ast.rs

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5+
use std::str;
56
use std::fmt;
67
use std::slice;
78
use std::vec;
9+
use serialize::{json};
10+
use serialize::json::ToJson;
811

912

1013
#[deriving(PartialEq)]
@@ -175,3 +178,201 @@ impl Iterator<ComponentValue> for MoveSkipWhitespaceIterator {
175178
None
176179
}
177180
}
181+
182+
/* ToJson implementations for all the AST nodes
183+
*
184+
* These need to be implemented here because they can't be implemented in a
185+
* crate that doesn't define these nodes
186+
*/
187+
188+
macro_rules! JString {
189+
($e: expr) => { json::String($e.to_string()) }
190+
}
191+
192+
macro_rules! JList {
193+
($($e: expr),*) => { json::List(vec!( $($e),* )) }
194+
}
195+
196+
fn list_to_json(list: &Vec<(ComponentValue, SourceLocation)>) -> Vec<json::Json> {
197+
list.iter().map(|tuple| {
198+
match *tuple {
199+
(ref c, _) => c.to_json()
200+
}
201+
}).collect()
202+
}
203+
204+
impl ToJson for Result<Rule, SyntaxError> {
205+
fn to_json(&self) -> json::Json {
206+
match *self {
207+
Ok(ref a) => a.to_json(),
208+
Err(ref b) => b.to_json(),
209+
}
210+
}
211+
}
212+
213+
214+
impl ToJson for Result<DeclarationListItem, SyntaxError> {
215+
fn to_json(&self) -> json::Json {
216+
match *self {
217+
Ok(ref a) => a.to_json(),
218+
Err(ref b) => b.to_json(),
219+
}
220+
}
221+
}
222+
223+
224+
impl ToJson for Result<Declaration, SyntaxError> {
225+
fn to_json(&self) -> json::Json {
226+
match *self {
227+
Ok(ref a) => a.to_json(),
228+
Err(ref b) => b.to_json(),
229+
}
230+
}
231+
}
232+
233+
234+
impl ToJson for Result<ComponentValue, SyntaxError> {
235+
fn to_json(&self) -> json::Json {
236+
match *self {
237+
Ok(ref a) => a.to_json(),
238+
Err(ref b) => b.to_json(),
239+
}
240+
}
241+
}
242+
243+
244+
impl ToJson for SyntaxError {
245+
fn to_json(&self) -> json::Json {
246+
json::List(vec!(JString!("error"), JString!(match self.reason {
247+
ErrEmptyInput => "empty",
248+
ErrExtraInput => "extra-input",
249+
_ => "invalid",
250+
})))
251+
}
252+
}
253+
254+
255+
impl ToJson for super::Color {
256+
fn to_json(&self) -> json::Json {
257+
use super::{RGBA, CurrentColor};
258+
259+
match *self {
260+
RGBA(RGBA { red: r, green: g, blue: b, alpha: a }) => vec!(r, g, b, a).to_json(),
261+
CurrentColor => JString!("currentColor"),
262+
}
263+
}
264+
}
265+
266+
267+
impl ToJson for Rule {
268+
fn to_json(&self) -> json::Json {
269+
match *self {
270+
QualifiedRule(ref rule) => rule.to_json(),
271+
AtRule(ref rule) => rule.to_json(),
272+
}
273+
}
274+
}
275+
276+
277+
impl ToJson for DeclarationListItem {
278+
fn to_json(&self) -> json::Json {
279+
match *self {
280+
Declaration(ref declaration) => declaration.to_json(),
281+
DeclAtRule(ref at_rule) => at_rule.to_json(),
282+
}
283+
}
284+
}
285+
286+
impl ToJson for ComponentValue {
287+
fn to_json(&self) -> json::Json {
288+
fn numeric(value: &NumericValue) -> Vec<json::Json> {
289+
match *value {
290+
NumericValue{representation: ref r, value: ref v, int_value: ref i}
291+
=> vec!(r.to_json(), v.to_json(),
292+
JString!(match *i { Some(_) => "integer", _ => "number" }))
293+
}
294+
}
295+
296+
match *self {
297+
Ident(ref value) => JList!(JString!("ident"), value.to_json()),
298+
AtKeyword(ref value) => JList!(JString!("at-keyword"), value.to_json()),
299+
Hash(ref value) => JList!(JString!("hash"), value.to_json(),
300+
JString!("unrestricted")),
301+
IDHash(ref value) => JList!(JString!("hash"), value.to_json(), JString!("id")),
302+
String(ref value) => JList!(JString!("string"), value.to_json()),
303+
URL(ref value) => JList!(JString!("url"), value.to_json()),
304+
Delim('\\') => JString!("\\"),
305+
Delim(value) => json::String(str::from_char(value)),
306+
307+
Number(ref value) => json::List(vec!(JString!("number")) + numeric(value)),
308+
Percentage(ref value) => json::List(vec!(JString!("percentage")) + numeric(value)),
309+
Dimension(ref value, ref unit)
310+
=> json::List(vec!(JString!("dimension")) + numeric(value) + &[unit.to_json()]),
311+
312+
UnicodeRange(start, end)
313+
=> JList!(JString!("unicode-range"), start.to_json(), end.to_json()),
314+
315+
WhiteSpace => JString!(" "),
316+
Colon => JString!(":"),
317+
Semicolon => JString!(";"),
318+
Comma => JString!(","),
319+
IncludeMatch => JString!("~="),
320+
DashMatch => JString!("|="),
321+
PrefixMatch => JString!("^="),
322+
SuffixMatch => JString!("$="),
323+
SubstringMatch => JString!("*="),
324+
Column => JString!("||"),
325+
CDO => JString!("<!--"),
326+
CDC => JString!("-->"),
327+
328+
Function(ref name, ref arguments)
329+
=> json::List(vec!(JString!("function"), name.to_json()) +
330+
arguments.iter().map(|a| a.to_json()).collect::<Vec<json::Json>>()),
331+
ParenthesisBlock(ref content)
332+
=> json::List(vec!(JString!("()")) + content.iter().map(|c| c.to_json()).collect::<Vec<json::Json>>()),
333+
SquareBracketBlock(ref content)
334+
=> json::List(vec!(JString!("[]")) + content.iter().map(|c| c.to_json()).collect::<Vec<json::Json>>()),
335+
CurlyBracketBlock(ref content)
336+
=> json::List(vec!(JString!("{}")) + list_to_json(content)),
337+
338+
BadURL => JList!(JString!("error"), JString!("bad-url")),
339+
BadString => JList!(JString!("error"), JString!("bad-string")),
340+
CloseParenthesis => JList!(JString!("error"), JString!(")")),
341+
CloseSquareBracket => JList!(JString!("error"), JString!("]")),
342+
CloseCurlyBracket => JList!(JString!("error"), JString!("}")),
343+
}
344+
}
345+
}
346+
347+
impl ToJson for AtRule {
348+
fn to_json(&self) -> json::Json {
349+
match *self {
350+
AtRule{name: ref name, prelude: ref prelude, block: ref block, ..}
351+
=> json::List(vec!(JString!("at-rule"), name.to_json(),
352+
prelude.to_json(), block.as_ref().map(list_to_json).to_json()))
353+
}
354+
}
355+
}
356+
357+
358+
impl ToJson for QualifiedRule {
359+
fn to_json(&self) -> json::Json {
360+
match *self {
361+
QualifiedRule{prelude: ref prelude, block: ref block, ..}
362+
=> json::List(vec!(JString!("qualified rule"),
363+
prelude.to_json(), json::List(list_to_json(block))))
364+
}
365+
}
366+
}
367+
368+
369+
impl ToJson for Declaration {
370+
fn to_json(&self) -> json::Json {
371+
match *self {
372+
Declaration{name: ref name, value: ref value, important: ref important, ..}
373+
=> json::List(vec!(JString!("declaration"), name.to_json(),
374+
value.to_json(), important.to_json()))
375+
}
376+
}
377+
}
378+

0 commit comments

Comments
 (0)