]>
Commit | Line | Data |
---|---|---|
ea8adc8c XL |
1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
2 | * License, v. 2.0. If a copy of the MPL was not distributed with this | |
3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | |
4 | ||
5 | #![crate_name = "cssparser"] | |
6 | #![crate_type = "rlib"] | |
7 | ||
8 | #![cfg_attr(feature = "bench", feature(test))] | |
9 | #![deny(missing_docs)] | |
10 | ||
11 | /*! | |
12 | ||
13 | Implementation of [CSS Syntax Module Level 3](https://drafts.csswg.org/css-syntax/) for Rust. | |
14 | ||
15 | # Input | |
16 | ||
17 | Everything is based on `Parser` objects, which borrow a `&str` input. | |
18 | If you have bytes (from a file, the network, or something) | |
19 | and want to support character encodings other than UTF-8, | |
20 | see the `stylesheet_encoding` function, | |
21 | which can be used together with rust-encoding or encoding-rs. | |
22 | ||
23 | # Conventions for parsing functions | |
24 | ||
25 | * Take (at least) a `input: &mut cssparser::Parser` parameter | |
26 | * Return `Result<_, ()>` | |
27 | * When returning `Ok(_)`, | |
28 | the function must have consumed exactly the amount of input that represents the parsed value. | |
29 | * When returning `Err(())`, any amount of input may have been consumed. | |
30 | ||
31 | As a consequence, when calling another parsing function, either: | |
32 | ||
33 | * Any `Err(())` return value must be propagated. | |
34 | This happens by definition for tail calls, | |
35 | and can otherwise be done with the `try!` macro. | |
36 | * Or the call must be wrapped in a `Parser::try` call. | |
37 | `try` takes a closure that takes a `Parser` and returns a `Result`, | |
38 | calls it once, | |
39 | and returns itself that same result. | |
40 | If the result is `Err`, | |
41 | it restores the position inside the input to the one saved before calling the closure. | |
42 | ||
43 | Examples: | |
44 | ||
45 | ```{rust,ignore} | |
46 | // 'none' | <image> | |
47 | fn parse_background_image(context: &ParserContext, input: &mut Parser) | |
48 | -> Result<Option<Image>, ()> { | |
49 | if input.try(|input| input.expect_ident_matching("none")).is_ok() { | |
50 | Ok(None) | |
51 | } else { | |
52 | Image::parse(context, input).map(Some) // tail call | |
53 | } | |
54 | } | |
55 | ``` | |
56 | ||
57 | ```{rust,ignore} | |
58 | // [ <length> | <percentage> ] [ <length> | <percentage> ]? | |
59 | fn parse_border_spacing(_context: &ParserContext, input: &mut Parser) | |
60 | -> Result<(LengthOrPercentage, LengthOrPercentage), ()> { | |
61 | let first = try!(LengthOrPercentage::parse); | |
62 | let second = input.try(LengthOrPercentage::parse).unwrap_or(first); | |
63 | (first, second) | |
64 | } | |
65 | ``` | |
66 | ||
67 | */ | |
68 | ||
69 | #![recursion_limit="200"] // For color::parse_color_keyword | |
70 | ||
71 | #[macro_use] extern crate cssparser_macros; | |
72 | #[macro_use] extern crate matches; | |
73 | #[macro_use] extern crate procedural_masquerade; | |
74 | #[doc(hidden)] pub extern crate phf as _internal__phf; | |
75 | #[cfg(test)] extern crate encoding_rs; | |
76 | #[cfg(test)] extern crate difference; | |
77 | #[cfg(test)] extern crate rustc_serialize; | |
78 | #[cfg(feature = "serde")] extern crate serde; | |
79 | #[cfg(feature = "heapsize")] #[macro_use] extern crate heapsize; | |
80 | ||
81 | pub use cssparser_macros::*; | |
82 | ||
83 | pub use tokenizer::{Token, NumericValue, PercentageValue, SourceLocation}; | |
84 | pub use rules_and_declarations::{parse_important}; | |
85 | pub use rules_and_declarations::{DeclarationParser, DeclarationListParser, parse_one_declaration}; | |
86 | pub use rules_and_declarations::{RuleListParser, parse_one_rule}; | |
87 | pub use rules_and_declarations::{AtRuleType, QualifiedRuleParser, AtRuleParser}; | |
88 | pub use from_bytes::{stylesheet_encoding, EncodingSupport}; | |
89 | pub use color::{RGBA, Color, parse_color_keyword}; | |
90 | pub use nth::parse_nth; | |
91 | pub use serializer::{ToCss, CssStringWriter, serialize_identifier, serialize_string, TokenSerializationType}; | |
92 | pub use parser::{Parser, Delimiter, Delimiters, SourcePosition}; | |
93 | pub use unicode_range::UnicodeRange; | |
94 | ||
95 | // For macros | |
96 | #[doc(hidden)] pub use macros::_internal__to_lowercase; | |
97 | ||
98 | // For macros when used in this crate. Unsure how $crate works with procedural-masquerade. | |
99 | mod cssparser { pub use _internal__phf; } | |
100 | ||
101 | #[macro_use] | |
102 | mod macros; | |
103 | ||
104 | mod rules_and_declarations; | |
105 | ||
106 | #[cfg(feature = "dummy_match_byte")] | |
107 | mod tokenizer; | |
108 | ||
109 | #[cfg(not(feature = "dummy_match_byte"))] | |
110 | mod tokenizer { | |
111 | include!(concat!(env!("OUT_DIR"), "/tokenizer.rs")); | |
112 | } | |
113 | mod parser; | |
114 | mod from_bytes; | |
115 | mod color; | |
116 | mod nth; | |
117 | mod serializer; | |
118 | mod unicode_range; | |
119 | ||
120 | #[cfg(test)] | |
121 | mod tests; |