Crates.io | tieliehin_css_parser |
lib.rs | tieliehin_css_parser |
version | 0.1.0 |
source | src |
created_at | 2024-11-12 21:10:44.427414 |
updated_at | 2024-11-12 21:10:44.427414 |
description | A simple CSS parser using the Pest library for Rust. |
homepage | |
repository | https://github.com/Nikita-ukma/css_parser |
max_upload_size | |
id | 1445552 |
size | 28,546 |
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 | "-")+ }