Crates.io | my_rust_css_parser |
lib.rs | my_rust_css_parser |
version | 0.1.3 |
source | src |
created_at | 2024-11-13 22:04:55.141098 |
updated_at | 2024-11-13 22:19:09.222123 |
description | A simple CSS parser using the Pest library for Rust. |
homepage | |
repository | https://github.com/Nikita-ukma/css_parser |
max_upload_size | |
id | 1447144 |
size | 30,492 |
docs: https://docs.rs/tieliehin_css_parser/latest/tieliehin_css_parser/
crates: https://crates.io/crates/tieliehin_css_parser
This CSS parser is built using the pest
parsing library in Rust. It parses simple CSS rules into components such as selectors, properties, and values.
The .pest grammar file defines the rules for parsing a CSS declaration block. Below is a breakdown of each rule:
This parser can handle basic CSS declarations, such as:
footer {
background-color: #333;
color: #f4f4f9;
text-align: center;
padding: 10px 0;
margin-top: 20px;
}
WHITESPACE = _{ " " | "\t" | "\n" | "\r" }
COMMENT = _{ "/*" ~ (!"*/" ~ ANY)* ~ "*/" }
CSS_RULE = _{ (COMMENT | WHITESPACE)* ~ selector ~ "{" ~ WHITESPACE* ~ declarations ~ WHITESPACE* ~ "}" }
selector = @{ (simple_selector ~ (WHITESPACE* ~ "," ~ WHITESPACE* ~ simple_selector)*) }
simple_selector = @{ IDENTIFIER | ("#" ~ IDENTIFIER+)
| ("." ~ IDENTIFIER+)
| ASCII_ALPHANUMERIC+ }
declarations = _{ (declaration ~ WHITESPACE*)+ }
declaration = _{ property ~ ":" ~ WHITESPACE* ~ values ~ ";" ~ WHITESPACE* }
property = @{ (ASCII_ALPHANUMERIC | "-")+ }
values = _{ value ~ (WHITESPACE* ~ "," ~ WHITESPACE* ~ value)* }
value = @{ COLOR | LENGTH | PERCENTAGE | STRING | ZERO | FLOAT | IDENTIFIER | ASCII_ALPHANUMERIC+}
COLOR = @{ "#" ~ ASCII_HEX_DIGIT{3,6} }
LENGTH = @{ ASCII_DIGIT+ ~ ("px" | "em" | "rem" | "%") }
PERCENTAGE = @{ ASCII_DIGIT+ ~ "%" }
STRING = @{ "\"" ~ (!"\"" ~ ANY)* ~ "\'" | "\'" ~ (!"\'" ~ ANY)* ~ "\'" }
ZERO = @{ "0" }
FLOAT = @{ ASCII_DIGIT+ ~ "." ~ ASCII_DIGIT+ }
IDENTIFIER = @{ (ASCII_ALPHANUMERIC | "-")+ }