Skip to main content

rust_css_parser/validation/utils/
list.rs

1//! Port of `src/validation/utils/list.ts`.
2
3use crate::ast::arena::AstArena;
4use crate::ast::types::EnumToken;
5
6/// remove comma and comment tokens
7pub fn strip_comma_token(arena: &AstArena, token_list: &[u32]) -> Vec<u32> {
8    token_list
9        .iter()
10        .copied()
11        .filter(|&t| !matches!(arena.data_types.get(t), EnumToken::CommentTokenType | EnumToken::CommaTokenType))
12        .collect()
13}
14
15/// split a token list. `split` defaults to `[CommaTokenType]`
16pub fn split_token_list(arena: &AstArena, token_list: &[u32], split: &[u8], include_split_token: bool) -> Vec<Vec<u32>> {
17    let mut result: Vec<Vec<u32>> = vec![Vec::new()];
18    for &curr in token_list {
19        if split.contains(&arena.data_types.get(curr)) {
20            if include_split_token {
21                result.last_mut().unwrap().push(curr);
22            }
23            result.push(Vec::new());
24        } else {
25            result.last_mut().unwrap().push(curr);
26        }
27    }
28    result
29}
30
31/// `splitTokenList(arena, tokens)` with the default comma separator
32pub fn split_token_list_comma(arena: &AstArena, token_list: &[u32]) -> Vec<Vec<u32>> {
33    split_token_list(arena, token_list, &[EnumToken::CommaTokenType], false)
34}