oxc-css-parser 0.0.5

Parser for CSS, SCSS, Sass, and Less.
Documentation
use super::Parser;
use crate::{Parse, ast::*, config::Syntax, error::PResult, pos::Span, tokenizer::Token};

// postcss-simple-vars variable reference: `$` <ident>
// https://github.com/postcss/postcss-simple-vars
impl<'a> Parse<'a> for PostcssSimpleVar<'a> {
    fn parse(input: &mut Parser<'a>) -> PResult<Self> {
        debug_assert!(input.syntax == Syntax::Css && input.options.allow_postcss_simple_vars);

        let (name, span) = input.parse_dollar_var_ident()?;
        Ok(PostcssSimpleVar { name, span })
    }
}

// postcss-simple-vars declaration: `$` <ident> ':' <declaration-value>
// (textual substitution; a trailing `!important` stays part of the value)
impl<'a> Parse<'a> for PostcssSimpleVarDeclaration<'a> {
    fn parse(input: &mut Parser<'a>) -> PResult<Self> {
        debug_assert!(input.syntax == Syntax::Css && input.options.allow_postcss_simple_vars);

        let name = input.parse::<PostcssSimpleVar>()?;
        let (_, colon_span) = input.cursor.expect_colon()?;
        let mut value = input.parse_declaration_value()?;
        // postcss-simple-vars is textual substitution; `!important` is just part
        // of the value, not a structural declaration modifier (unlike CSS's
        // `Declaration.important`). Keep it in the value stream.
        if let Token::Exclamation(..) = &input.cursor.peek()?.token {
            let important = input.parse::<ImportantAnnotation>()?;
            value.push(ComponentValue::ImportantAnnotation(important));
        }

        let end = value.last().map(|v| v.span().end).unwrap_or(colon_span.end);
        let span = Span { start: name.span.start, end };

        Ok(PostcssSimpleVarDeclaration { name, colon_span, value, span })
    }
}