css_parse/
lib.rs

1#![deny(warnings)]
2//! An implementation of [CSS Syntax Level 3][1], plus various additional traits and macros to assist in parsing. It is
3//! intended to be used to build CSS or CSS-alike languages (for example SASS), but isn't able to parse the full CSS
4//! grammar itself. It relies on the foundational [css_lexer] crate.
5//!
6//! This crate provides the [Parser] struct, which builds upon [Lexer][css_lexer::Lexer]. It borrows a `&str` which it
7//! will parse to produce AST nodes (any type that implements the [Parse] and [ToCursors] traits). AST nodes should
8//! parse themselves and any children using [recursive descent][2].
9//!
10//! [1]: https://drafts.csswg.org/css-syntax-3/
11//! [2]: https://en.wikipedia.org/wiki/Recursive_descent_parser
12//!
13//! Parsing requires a heap allocator to allocate into, [bumpalo::Bump] being the allocator of choice. This needs to be
14//! created before parsing, the parser result will have a lifetime bound to the allocator.
15//!
16//! The [Parser] _may_ be configured with additional [Features][Feature] to allow for different parsing or lexing
17//! styles. All features supported by the [Lexer][css_lexer::Lexer] are supported in the [Parser] also (for example
18//! enabling [Feature::SingleLineComments] will enable [the css_lexer feature of the same
19//! name][css_lexer::Feature::SingleLineComments]).
20//!
21//! This crate provides some low level AST nodes that are likely to be common in any CSS-alike language, including the
22//! various base tokens (such as dimensions, and operators). These can be referred to via the [T!] macro, and each [T!]
23//! implements the necessary traits to be parsed as an AST node. For example [T![DashedIdent]][token_macros::DashedIdent]
24//! represents a CSS ident with two leading dashes, and can be parsed and decomposted into its constituent
25//! [Token][Token] (or [Cursor][Cursor] or [Span][Span]).
26//!
27//! Additionally some generic structs are available to implement the general-purpose parts of [CSS Syntax][1], such as
28//! [ComponentValues][syntax::ComponentValues]. More on that below in the section titled
29//! [Generic AST Nodes](#generic-ast-nodes).
30//!
31//! Lastly, traits and macros are provided to implement various parsing algorithms to make common parsing operations
32//! easier, for example the [ranged_feature] macro makes it easy to build a node that implements the [RangedFeature]
33//! trait, a trait that provides [an algorithm for parsing a media feature in a range context][3].
34//!
35//! [3]: https://drafts.csswg.org/mediaqueries/#range-context
36//!
37//! Downstream implementations will likely want to build their own AST nodes to represent specific cover grammars, for
38//! example implementing the `@property` rule or the `width:` property declaration. Here's a small guide on what is
39//! required to build such nodes:
40//!
41//! # AST Nodes
42//!
43//! To use this as a library a set of AST nodes will need to be created, the root node (and ideally all nodes) need to
44//! implement [Parse] - which will be given a mutable reference to an active [Parser]. Each Node will likely be a
45//! collection of other Nodes, calling [Parser::parse<T>()][Parser::parse] (where `T` is each child Node). Leaf Nodes will likely be
46//! wrappers around a single token (tip: use the [T!] nodes which cover all single token needs):
47//!
48//! ```
49//! use css_parse::*;
50//! struct MyProperty {
51//!   ident: T![Ident],
52//!   colon: T![Colon],
53//!   dimension: T![Dimension],
54//! }
55//! impl<'a> Parse<'a> for MyProperty {
56//!   fn parse(p: &mut Parser<'a>) -> Result<Self> {
57//!     let ident = p.parse::<T![Ident]>()?;
58//!     let colon = p.parse::<T![Colon]>()?;
59//!     let dimension = p.parse::<T![Dimension]>()?;
60//!     Ok(Self { ident, colon, dimension })
61//!   }
62//! }
63//! ```
64//!
65//! AST nodes will also need to implement [ToCursors] - which is given an abstract [CursorSink] to put the cursors back
66//! into, in order, so that they can be built back up into the original source text. Implementing [ToCursors] allows
67//! for all manner of other useful downstream operations such as concatenation, transforms (e.g. minification) and so
68//! on.
69//!
70//! ```
71//! use css_parse::*;
72//! struct MyProperty {
73//!   ident: T![Ident],
74//!   colon: T![Colon],
75//!   dimension: T![Dimension],
76//! }
77//! impl ToCursors for MyProperty {
78//!   fn to_cursors(&self, s: &mut impl CursorSink) {
79//!     s.append(self.ident.into());
80//!     s.append(self.colon.into());
81//!     s.append(self.dimension.into());
82//!   }
83//! }
84//! ```
85//!
86//! Both [Parse] and [ToCursors] are the _required_ trait implemenetations, but several more are also available and make
87//! the work of Parsing (or downstream analysis) easier...
88//!
89//! ## Peekable nodes
90//!
91//! Everything that implements [Parse] is required to implement [Parse::parse()], but gets [Parse::try_parse()] for
92//! free, which allows parent nodes to more easily branch by parsing a node, resetting during failure.
93//! [Parse::try_parse()] can be expensive though - parsing a Node is pretty much guaranteed to advance the [Parser]
94//! some number of tokens forward, and so a parser checkpoint needs to be stored so that - should
95//! [Parse::parse()] fail - the [Parser] can be rewound to that checkpoint as if the operation never happened. Reading
96//! N tokens forward only to forget that and re-do it all over can be costly and is likely the _wrong tool_ to use when
97//! faced with a set of branching Nodes with an ambiguity of which to parse. So Nodes are also encouraged to implement
98//! [Peek], which their parent nodes can call to check as an indicator that this Node may viably parse.
99//!
100//! Most nodes will know they can only accept a certain number of tokens, per their cover grammar. [Peek] is a useful
101//! way to encode this; [Peek::peek] gets an _immutable_ reference to the [Parser], from which it can call
102//! [Parser::peek_n()] (an immutable operation that can't change the position of the parser) to look ahead to other
103//! tokens and establish if they would cause [Parse::parse()] to fail. There is still a cost to this, and so
104//! [Peek::peek] should only look ahead the smallest number of tokens to confidently know that it can begin parsing,
105//! rather than looking ahead a large number of tokens. For the most part peeking 1 or two tokens should be sufficient.
106//! An easy implementation for [Peek] is to simply set the [Peek::PEEK_KINDSET] const, which the provided
107//! implementation of [Peek::peek()] will use to check the cursor matches this [KindSet][KindSet].
108//!
109//! ```
110//! use css_parse::*;
111//! use {Kind, KindSet};
112//! enum LengthOrAuto {
113//!   Length(T![Dimension]), // A Dimension, like `px`
114//!   Auto(T![Ident]),       // The Ident of `auto`
115//! }
116//! impl<'a> Peek<'a> for LengthOrAuto {
117//!   const PEEK_KINDSET: KindSet = KindSet::new(&[Kind::Dimension, Kind::Ident]);
118//! }
119//! ```
120//!
121//! ## Single token Nodes
122//!
123//! If a node represents just a single token, for example a keyword, then its [Parse] implementation
124//! should call [Parser::peek] to check if it can be parsed, then [Parser::next] to get the cursor, and construct
125//! the node from that cursor. The [Peek] trait should accurately determine if the Node can be parsed from the
126//! given [Cursor][Cursor]. Single token parsing may need to branch if it is an enum of variants:
127//!
128//! ```
129//! use css_parse::*;
130//! enum LengthOrAuto {
131//!   Length(T![Dimension]), // A Dimension, like `px`
132//!   Auto(T![Ident]),       // The Ident of `auto`
133//! }
134//! impl<'a> Peek<'a> for LengthOrAuto {
135//!   const PEEK_KINDSET: KindSet = KindSet::new(&[Kind::Dimension, Kind::Ident]);
136//! }
137//! impl<'a> Parse<'a> for LengthOrAuto {
138//!   fn parse(p: &mut Parser<'a>) -> Result<Self> {
139//!     if p.peek::<T![Dimension]>() {
140//!       p.parse::<T![Dimension]>().map(Self::Length)
141//!     } else {
142//!       p.parse::<T![Ident]>().map(Self::Auto)
143//!     }
144//!   }
145//! }
146//! ```
147//!
148//! ## Convenience algorithms
149//!
150//! For more complex algorithms where nodes might parse many child nodes or have some delicate or otherwise awkward
151//! steps, additional traits exist to make implementing AST nodes trivial for these use cases.
152//!
153//! - [StyleSheet] - AST nodes representing a stylesheet should use this to, well, [parse a stylesheet][4].
154//! - [Declaration] - AST nodes representing a declaration (aka "property") should use this to [parse a
155//!   declaration][5].
156//! - [QualifiedRule] - AST nodes representing a "Qualified Rule" (e.g. a style rule) should use this to
157//!   [parse a QualifiedRule][7].
158//! - [CompoundSelector] - AST nodes representing a CSS selector should use this to parse  a list of nodes implementing
159//!   [SelectorComponent].
160//! - [SelectorComponent] - AST nodes representing an individual selector component, such as a tag or class or pseudo
161//!   element, should use this to parse the set of specified selector components.
162//!
163//! The `*List` traits are also available to more easily parse lists of things, such as preludes or blocks:
164//!
165//! - [PreludeList] - AST nodes representing a rule's prelude should use this. It simply repeatedly parses its items
166//!   until it enounters the start of a block (<{-token> or <;-token>).
167//! - [FeatureConditionList] - AST nodes representing a prelude "condition list" should use this. It parses the complex
168//!   condition logic in rules like `@media`, `@supports` or `@container`.
169//! - [DeclarationList] - AST nodes representing a block which can only accept "Declarations" should use this. This is
170//!   an implementation of [`<declaration-list>`][8].
171//! - [DeclarationRuleList] - AST nodes representing a block which can accept either "At Rules" or "Declarations" but
172//!   cannot accept "Qualified Rules" should use this. This is an implementation of [`<declaration-rule-list>`][11]
173//! - [RuleList] - AST nodes representing a block which can accept either "At Rules" or "Qualfiied Rules" but cannot
174//!   accept "Declarations" should use this. This is an implementation of [`<rule-list>`][12].
175//!
176//! The `*Feature` traits are also available to more easily parse "features conditions", these are the conditions
177//! supports in a [FeatureConditionList], e.g. the conditions inside of `@media`, `@container` or `@supports` rules.
178//!
179//!  - [RangedFeature] - AST nodes representing a feature condition in the "ranged" context.
180//!  - [BooleanFeature] - AST nodes representing a feature condition in the "boolean" context.
181//!  - [DiscreteFeature] - AST nodes representing a feature condition with discrete keywords.
182//!
183//! [4]: https://drafts.csswg.org/css-syntax-3/#consume-stylesheet-contents
184//! [5]: https://drafts.csswg.org/css-syntax-3/#consume-declaration
185//! [6]: https://drafts.csswg.org/css-syntax-3/#consume-at-rule
186//! [7]: https://drafts.csswg.org/css-syntax-3/#consume-qualified-rule
187//! [8]: https://drafts.csswg.org/css-syntax-3/#typedef-declaration-list
188//! [9]: https://drafts.csswg.org/css-syntax-3/#typedef-qualified-rule-list
189//! [10]: https://drafts.csswg.org/css-syntax-3/#typedef-at-rule-list
190//! [11]: https://drafts.csswg.org/css-syntax-3/#typedef-declaration-rule-list
191//! [12]: https://drafts.csswg.org/css-syntax-3/#typedef-rule-list
192//!
193//! # Generic AST nodes
194//!
195//! In addition to the traits which allow for parsing bespoke AST Nodes, this crate provides a set of generic AST node
196//! structs/enums which are capable of providing "general purpose" AST nodes, useful for when an AST node fails to parse
197//! and needs to consume some tokens in a generic manner, according to the rules of :
198//!
199//!  - [syntax::QualifiedRule] provides the generic [`<qualified-rule>` grammar][14].
200//!  - [syntax::Declaration] provides the generic [`<declaration>` grammar][15].
201//!  - [syntax::BangImportant] provides the [`<!important>` grammar][16].
202//!  - [syntax::ComponentValue] provides the [`<component-value>` grammar][17], used by other generic nodes.
203//!  - [syntax::SimpleBlock] provides the generic [`<simple-block>` grammar][18].
204//!  - [syntax::FunctionBlock] provides the generic [`<function-block>` grammar][19].
205//!  - [syntax::ComponentValues] provides a list of `<component-value>` nodes, [per "parse a list of component
206//!    values"][20].
207//!  - [syntax::BadDeclaration] provides a struct to capture the [bad declaration steps][21].
208//!
209//! [13]: https://drafts.csswg.org/css-syntax-3/#at-rule-diagram
210//! [14]: https://drafts.csswg.org/css-syntax-3/#qualified-rule-diagram
211//! [15]: https://drafts.csswg.org/css-syntax-3/#declaration-diagram
212//! [16]: https://drafts.csswg.org/css-syntax-3/#!important-diagram
213//! [17]: https://drafts.csswg.org/css-syntax-3/#component-value-diagram
214//! [18]: https://drafts.csswg.org/css-syntax-3/#simple-block-diagram
215//! [19]: https://drafts.csswg.org/css-syntax-3/#function-block-diagram
216//! [20]: https://drafts.csswg.org/css-syntax-3/#parse-list-of-component-values
217//! [21]: https://drafts.csswg.org/css-syntax-3/#consume-the-remnants-of-a-bad-declaration
218//!
219//! # Test Helpers
220//!
221//! In order to make it much easier to test the functionality of AST nodes, enabling the `testing` feature will provide
222//! two testing macros which make setting up a test trivial.
223//!
224//! - [assert_parse!] will parse the given string against the given node, asserting that it parses successfully and can
225//!   be written back out to the same output.
226//!
227//! - [assert_parse_error!] will parse the given string against the node, expecting the parse to fail.
228//!
229//! It is advised to add the `testing` flag as a `dev-dependencies` feature to enable these only during test:
230//!
231//! ```toml
232//! [dependencies]
233//! css_parse = "*"
234//!
235//! [dev-dependencies]
236//! css_parse = { version = "*", features = ["testing"] }
237//! ```
238//!
239//! # Example
240//!
241//! A small example on how to define an AST node:
242//!
243//! ```
244//! use css_parse::*;
245//! #[derive(Debug)]
246//! struct MyProperty {
247//!   ident: T![Ident],
248//!   colon: T![Colon],
249//!   dimension: T![Dimension],
250//! }
251//! impl<'a> Parse<'a> for MyProperty {
252//!   fn parse(p: &mut Parser<'a>) -> Result<Self> {
253//!     let ident = p.parse::<T![Ident]>()?;
254//!     let colon = p.parse::<T![Colon]>()?;
255//!     let dimension = p.parse::<T![Dimension]>()?;
256//!     Ok(Self { ident, colon, dimension })
257//!   }
258//! }
259//! impl ToCursors for MyProperty {
260//!   fn to_cursors(&self, s: &mut impl CursorSink) {
261//!     self.ident.to_cursors(s);
262//!     self.colon.to_cursors(s);
263//!     self.dimension.to_cursors(s);
264//!   }
265//! }
266//!
267//! assert_parse!(EmptyAtomSet::ATOMS, MyProperty, "width:1px");
268//! ```
269
270// Re-export commonly used components from css_lexer:
271pub use css_lexer::{
272	AssociatedWhitespaceRules, AtomSet, Cursor, EmptyAtomSet, Kind, KindSet, PairWise, QuoteStyle, SourceCursor,
273	SourceOffset, Span, ToSpan, Token, Whitespace,
274};
275
276mod comparison;
277mod cursor_compact_write_sink;
278mod cursor_interleave_sink;
279mod cursor_ordered_sink;
280mod cursor_overlay_sink;
281mod cursor_pretty_write_sink;
282mod cursor_write_sink;
283mod diagnostics;
284mod feature;
285mod macros;
286mod parser;
287mod parser_checkpoint;
288mod parser_return;
289/// Various structs/enums that represent generic AST nodes.
290pub mod syntax;
291/// Test macros available if built with `features = ["testing"]`
292#[cfg(any(feature = "testing", test))]
293pub mod test_helpers;
294/// Various macros that expand to AST nodes that wrap [Tokens][Token].
295pub mod token_macros;
296mod traits;
297
298pub type Result<T> = std::result::Result<T, diagnostics::Diagnostic>;
299
300pub use comparison::*;
301pub use cursor_compact_write_sink::*;
302pub use cursor_interleave_sink::*;
303pub use cursor_ordered_sink::*;
304pub use cursor_overlay_sink::*;
305pub use cursor_pretty_write_sink::*;
306pub use cursor_write_sink::*;
307pub use diagnostics::*;
308pub use feature::*;
309pub use macros::optionals::*;
310#[cfg(feature = "miette")]
311pub use miette::Error;
312pub use parser::*;
313pub use parser_checkpoint::*;
314pub use parser_return::*;
315pub use syntax::*;
316pub use traits::*;