rust_css_parser/parser/utils/
at_rule_import.rs1use super::at_rule_media::parse_mediaquery_list;
4use super::at_rule_support::parse_at_rule_support_syntax;
5use crate::ast::arena::AstArena;
6use crate::ast::types::EnumToken;
7use crate::options::ParserOptions;
8use crate::syntax::constants::{is_function_def_type, tokens_func_def_map};
9use crate::types::ErrorDescription;
10use crate::validation::config::get_syntax_rule;
11use crate::validation::match_::{token_location, trim_array};
12use crate::validation::parser::typedef::ValidationSyntaxGroupEnum;
13
14fn import_error(arena: &AstArena, message: &str, node: Option<u32>, options: &ParserOptions) -> ErrorDescription {
15 let mut error = ErrorDescription::drop(message).with_syntax("@import");
16 error.node = node;
17 error.location = token_location(arena, node.unwrap_or(0), options);
18 error
19}
20
21fn splice_trimmed(arena: &AstArena, v: &mut Vec<u32>, start: isize, count: isize) -> Vec<u32> {
23 let len = v.len() as isize;
24 let s = if start < 0 { (len + start).max(0) } else { start.min(len) } as usize;
25 let c = count.clamp(0, len - s as isize) as usize;
26 let mut out: Vec<u32> = v.drain(s..s + c).collect();
27 trim_array(arena, &mut out);
28 out
29}
30
31fn remove_at(v: &mut Vec<u32>, index: usize) {
33 if index < v.len() {
34 v.remove(index);
35 }
36}
37
38pub fn match_at_rule_import_syntax(arena: &AstArena, at_rule: u32, stream: &mut Vec<u32>, options: &ParserOptions) -> (bool, Vec<ErrorDescription>) {
40 let t = |x: Option<u32>| x.map_or(0u8, |x| arena.data_types.get(x));
41 let at = |stream: &Vec<u32>, i: usize| stream.get(i).copied();
42 let mut success = true;
43 let mut index = 0usize;
44 let mut errors: Vec<ErrorDescription> = Vec::new();
45 let mut tokens: Vec<u32> = Vec::new();
46 let mut stack: Vec<u32> = Vec::new();
47 let prelude_length = get_syntax_rule(ValidationSyntaxGroupEnum::AtRules, "@import")
48 .and_then(|r| r.get_prelude_rules())
49 .map_or(0, |p| p.len().saturating_sub(1));
50 trim_array(arena, stream);
51 if t(at(stream, index)) == EnumToken::StringTokenType {
52 tokens.push(stream[index]);
53 index += 1;
54 } else if t(at(stream, index)) == EnumToken::UrlFunctionTokenDefType {
55 let mut match_count = 0;
56 let mut k = 0;
57 while k < stream.len() {
58 let tk = arena.data_types.get(stream[k]);
59 if tk == EnumToken::EndParensTokenType {
60 match_count -= 1;
61 } else if tk == EnumToken::StartParensTokenType || is_function_def_type(tk) {
62 match_count += 1;
63 }
64 if match_count == 0 {
65 break;
66 }
67 k += 1;
68 }
69 let mut slice: Vec<u32> = stream[(index + 1).min(stream.len())..k.min(stream.len()).max((index + 1).min(stream.len()))].to_vec();
70 if stream.len() > 1 {
71 arena.span_location(stream[0], stream[0], stream[1]);
72 }
73 arena.data_types.set(stream[0], tokens_func_def_map(arena.data_types.get(stream[0])).unwrap_or(0));
74 trim_array(arena, &mut slice);
75 arena.set_token_list(stream[0], &slice);
76 tokens.push(stream[0]);
77 index = k + 1;
78 } else {
79 return (false, vec![import_error(arena, "Expected string or url()", stream.first().copied(), options)]);
80 }
81 while index + 1 < prelude_length && matches!(t(at(stream, index)), EnumToken::WhitespaceTokenType | EnumToken::CommentTokenType) {
82 tokens.push(stream[index]);
83 index += 1;
84 }
85 let is_layer = |i: usize| at(stream, i).is_some_and(|x| arena.string_equals(arena.string_value.get(x), "layer"));
86 if t(at(stream, index)) == EnumToken::IdenTokenType && is_layer(index) {
87 tokens.push(stream[index]);
88 index += 1;
89 } else if is_function_def_type(t(at(stream, index))) && is_layer(index) {
90 stack.push(stream[index]);
91 tokens.push(stream[index]);
92 index += 1;
93 if t(at(stream, index)) == EnumToken::EndParensTokenType {
94 remove_at(&mut tokens, index);
95 return (false, vec![import_error(arena, "Expected <layer-name>", at(stream, index), options)]);
96 } else if t(at(stream, index)) == EnumToken::IdenTokenType {
97 tokens.push(stream[index]);
98 index += 1;
99 while t(at(stream, index)) == EnumToken::ClassSelectorTokenType {
100 tokens.push(stream[index]);
101 index += 1;
102 }
103 let func = *stack.last().unwrap();
104 let i = tokens.iter().position(|&x| x == func).map_or(-1, |p| p as isize);
105 remove_at(&mut tokens, index);
106 arena.data_types.set(func, EnumToken::FunctionTokenType);
107 let count = index as isize - i - 1;
108 index += 1;
109 let inner = splice_trimmed(arena, &mut tokens, i + 1, count);
110 arena.set_token_list(func, &inner);
111 stack.pop();
112 } else {
113 return (false, vec![import_error(arena, "Expected <layer-name>", at(stream, index), options)]);
114 }
115 }
116 while matches!(t(at(stream, index)), EnumToken::WhitespaceTokenType | EnumToken::CommentTokenType) {
117 tokens.push(stream[index]);
118 index += 1;
119 }
120 if index < stream.len()
121 && is_function_def_type(t(at(stream, index)))
122 && arena.string_equals(arena.string_value.get(stream[index]), "supports")
123 {
124 stack.push(stream[index]);
125 tokens.push(stream[index]);
126 index += 1;
127 let mut match_count = 1;
128 while index < stream.len() && match_count > 0 {
129 let ti = arena.data_types.get(stream[index]);
130 if ti == EnumToken::StartParensTokenType || is_function_def_type(ti) {
131 match_count += 1;
132 } else if ti == EnumToken::EndParensTokenType {
133 match_count -= 1;
134 if match_count == 0 {
135 remove_at(&mut tokens, index);
136 let func = *stack.last().unwrap();
137 let i = tokens.iter().position(|&x| x == func).map_or(-1, |p| p as isize);
138 arena.data_types.set(func, tokens_func_def_map(arena.data_types.get(func)).unwrap_or(0));
139 let count = index as isize - i - 1;
140 index += 1;
141 let inner = splice_trimmed(arena, &mut tokens, i + 1, count);
142 arena.set_token_list(func, &inner);
143 stack.pop();
144 break;
145 }
146 }
147 tokens.push(stream[index]);
148 index += 1;
149 }
150 if let Some(&supports) = tokens.last() {
151 let mut chi = arena.tokens(supports);
152 let result = parse_at_rule_support_syntax(arena, &mut chi, at_rule, options);
153 arena.set_token_list(supports, &chi);
154 if !result.0 && !result.1.is_empty() {
155 errors.extend(result.1);
156 return (false, errors);
157 }
158 }
159 }
160 let mut splice: Vec<u32> = if index < stream.len() { stream.drain(index..).collect() } else { Vec::new() };
161 let sliced = parse_mediaquery_list(arena, &mut splice, options);
162 tokens.extend(splice);
163 errors.extend(sliced.1);
164 if !sliced.0 {
165 success = false;
166 }
167 trim_array(arena, &mut tokens);
168 *stream = tokens;
169 (success, errors)
170}