rust_css_parser/parser/utils/
token.rs1use crate::ast::arena::AstArena;
4use crate::ast::types::EnumToken;
5use crate::syntax::constants::is_function_type;
6
7fn trim_token_space(t: u8) -> bool {
8 matches!(
9 t,
10 EnumToken::CommaTokenType
11 | EnumToken::DelimTokenType
12 | EnumToken::GtTokenType
13 | EnumToken::GteTokenType
14 | EnumToken::LtTokenType
15 | EnumToken::LteTokenType
16 | EnumToken::ChildCombinatorTokenType
17 | EnumToken::PipeTokenType
18 | EnumToken::ColonTokenType
19 )
20}
21
22fn trim_space_after(t: u8) -> bool {
23 is_function_type(t)
24 || matches!(
25 t,
26 EnumToken::ChildCombinatorTokenType
27 | EnumToken::SupportsQueryConditionTokenType
28 | EnumToken::SupportsQueryUnaryConditionTokenType
29 | EnumToken::CommaTokenType
30 | EnumToken::PipeTokenType
31 )
32}
33
34pub fn trim_white_space_tokens(arena: &AstArena, tokens: &mut Vec<u32>) {
36 let ws = |t: u32| arena.data_types.get(t) == EnumToken::WhitespaceTokenType;
37 if tokens.first().is_some_and(|&t| ws(t)) {
38 tokens.remove(0);
39 }
40 if tokens.last().is_some_and(|&t| ws(t)) {
41 tokens.pop();
42 }
43 if tokens.len() == 1 {
44 if ws(tokens[0]) {
45 tokens.clear();
46 }
47 return;
48 }
49 let mut i: isize = 0;
50 while (i as usize) < tokens.len() {
51 let iu = i as usize;
52 let t = arena.data_types.get(tokens[iu]);
53 if trim_token_space(t) {
54 if iu + 1 < tokens.len() && ws(tokens[iu + 1]) {
55 tokens.remove(iu + 1);
56 }
57 if iu > 0 && ws(tokens[iu - 1]) {
58 i -= 1;
59 tokens.remove(i as usize);
60 i += 1;
61 continue;
62 }
63 }
64 let iu = i as usize;
65 if trim_space_after(arena.data_types.get(tokens[iu])) && iu + 1 < tokens.len() && ws(tokens[iu + 1]) {
66 tokens.remove(iu + 1);
67 }
68 i += 1;
69 }
70}