Skip to main content

rust_css_parser/parser/utils/
at_rule_generic.rs

1//! Port of `src/parser/utils/at-rule-generic.ts`.
2
3use crate::ast::arena::AstArena;
4use crate::ast::types::EnumToken;
5use crate::options::ParserOptions;
6use crate::syntax::constants::is_function_def_type;
7use crate::types::ErrorDescription;
8use crate::validation::match_::{drop_error, token_type_name};
9
10/// validate the `and` / `or` / `,` structure of a generic at-rule prelude
11pub fn match_generic_syntax(arena: &AstArena, stream: &[u32], options: &ParserOptions) -> (bool, Vec<ErrorDescription>) {
12    let t = |x: u32| arena.data_types.get(x);
13    let mut stack: Vec<u32> = Vec::new();
14    let mut success = true;
15    let mut expect_and_or = false;
16    let mut expect_comma = false;
17    let mut errors: Vec<ErrorDescription> = Vec::new();
18    let mut scopes: Vec<(bool, bool)> = vec![(false, false)];
19    let unexpected = |token: u32| drop_error(arena, format!("unexpected token {}", token_type_name(t(token))), token, options);
20    for &token in stream {
21        let tt = t(token);
22        if tt == EnumToken::WhitespaceTokenType || tt == EnumToken::CommentTokenType {
23            continue;
24        }
25        if tt == EnumToken::StartParensTokenType || is_function_def_type(tt) {
26            stack.push(token);
27            scopes.push((false, false));
28            expect_and_or = false;
29            continue;
30        }
31        if tt == EnumToken::EndParensTokenType {
32            let top = stack.last().map(|&s| t(s));
33            if top.is_none_or(|s| s != EnumToken::StartParensTokenType && !is_function_def_type(s)) {
34                errors.push(unexpected(token));
35                success = false;
36                break;
37            }
38            stack.pop();
39            scopes.pop();
40            continue;
41        }
42        if tt == EnumToken::IdenTokenType && arena.string_equals_ignore_case(arena.string_value.get(token), "and") {
43            if !expect_and_or || scopes.last().is_some_and(|s| s.1) {
44                errors.push(unexpected(token));
45                success = false;
46                break;
47            }
48            arena.data_types.set(token, EnumToken::AndTokenType);
49            if let Some(s) = scopes.last_mut() {
50                s.0 = true;
51            }
52            expect_comma = false;
53            continue;
54        }
55        if tt == EnumToken::IdenTokenType && arena.string_equals_ignore_case(arena.string_value.get(token), "or") {
56            if !expect_and_or || scopes.last().is_some_and(|s| s.0) {
57                errors.push(unexpected(token));
58                success = false;
59                break;
60            }
61            expect_comma = false;
62            arena.data_types.set(token, EnumToken::OrTokenType);
63            if let Some(s) = scopes.last_mut() {
64                s.1 = true;
65            }
66            continue;
67        }
68        if tt == EnumToken::CommaTokenType {
69            if !expect_comma {
70                errors.push(unexpected(token));
71                success = false;
72                break;
73            }
74            stack.push(token);
75            expect_comma = false;
76            continue;
77        }
78        expect_and_or = true;
79        expect_comma = true;
80        if stack.last().is_some_and(|&s| t(s) == EnumToken::CommaTokenType) {
81            stack.pop();
82        }
83    }
84    if let Some(&last) = stack.last() {
85        errors.push(drop_error(arena, format!("unexpected token {}", token_type_name(t(last))), last, options));
86        success = false;
87    }
88    (success, errors)
89}