1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use crate::{
error::SassResult,
utils::{is_name_start, peek_ident_no_interpolation, read_until_closing_paren},
{Cow, Token},
};
use super::Parser;
impl<'a> Parser<'a> {
pub fn scan_identifier(&mut self, ident: &str) -> SassResult<bool> {
let peeked_identifier =
match peek_ident_no_interpolation(self.toks, false, self.span_before) {
Ok(v) => v.node,
Err(..) => return Ok(false),
};
if peeked_identifier == ident {
self.toks.truncate_iterator_to_cursor();
self.toks.next();
return Ok(true);
}
self.toks.reset_cursor();
Ok(false)
}
pub fn expect_char(&mut self, c: char) -> SassResult<()> {
if let Some(Token { kind, .. }) = self.toks.peek() {
if *kind == c {
self.toks.next();
return Ok(());
}
}
Err((format!("expected \"{}\".", c), self.span_before).into())
}
pub fn scan_char(&mut self, c: char) -> bool {
if let Some(Token { kind, .. }) = self.toks.peek() {
if *kind == c {
self.toks.next();
return true;
}
}
false
}
pub fn expression_until_comparison(&mut self) -> SassResult<Cow<'static, str>> {
let mut toks = Vec::new();
while let Some(tok) = self.toks.peek().cloned() {
match tok.kind {
'=' => {
self.toks.advance_cursor();
if matches!(self.toks.peek(), Some(Token { kind: '=', .. })) {
self.toks.reset_cursor();
break;
}
self.toks.reset_cursor();
toks.push(tok);
toks.push(tok);
self.toks.next();
self.toks.next();
}
'>' | '<' | ':' => {
break;
}
_ => {
toks.push(tok);
self.toks.next();
}
}
}
self.parse_value_as_string_from_vec(toks)
}
pub(super) fn parse_media_query_list(&mut self) -> SassResult<String> {
let mut buf = String::new();
loop {
self.whitespace();
buf.push_str(&self.parse_single_media_query()?);
if !self.scan_char(',') {
break;
}
buf.push(',');
buf.push(' ');
}
Ok(buf)
}
fn parse_media_feature(&mut self) -> SassResult<String> {
if let Some(Token { kind: '#', .. }) = self.toks.peek() {
if let Some(Token { kind: '{', .. }) = self.toks.peek_forward(1) {
self.toks.next();
self.toks.next();
return Ok(self.parse_interpolation_as_string()?.into_owned());
}
todo!()
}
let mut buf = String::with_capacity(2);
self.expect_char('(')?;
buf.push('(');
self.whitespace();
buf.push_str(&self.expression_until_comparison()?);
if let Some(Token { kind: ':', .. }) = self.toks.peek() {
self.toks.next();
self.whitespace();
buf.push(':');
buf.push(' ');
let mut toks = read_until_closing_paren(self.toks)?;
if let Some(tok) = toks.pop() {
if tok.kind != ')' {
todo!()
}
}
buf.push_str(&self.parse_value_as_string_from_vec(toks)?);
self.whitespace();
buf.push(')');
return Ok(buf);
} else {
let next_tok = self.toks.peek().cloned();
let is_angle = next_tok.map_or(false, |t| t.kind == '<' || t.kind == '>');
if is_angle || matches!(next_tok, Some(Token { kind: '=', .. })) {
buf.push(' ');
buf.push(self.toks.next().unwrap().kind);
if is_angle && self.scan_char('=') {
buf.push('=');
}
buf.push(' ');
self.whitespace();
buf.push_str(&self.expression_until_comparison()?);
}
}
self.expect_char(')')?;
self.whitespace();
buf.push(')');
Ok(buf)
}
fn parse_single_media_query(&mut self) -> SassResult<String> {
let mut buf = String::new();
if !matches!(self.toks.peek(), Some(Token { kind: '(', .. })) {
buf.push_str(&self.parse_identifier()?);
self.whitespace();
if let Some(tok) = self.toks.peek() {
if !is_name_start(tok.kind) {
return Ok(buf);
}
}
let ident = self.parse_identifier()?;
self.whitespace();
if ident.to_ascii_lowercase() == "and" {
buf.push_str(" and ");
} else {
buf.push_str(&ident);
if self.scan_identifier("and")? {
self.whitespace();
buf.push_str(" and ");
} else {
return Ok(buf);
}
}
}
loop {
self.whitespace();
buf.push_str(&self.parse_media_feature()?);
self.whitespace();
if !self.scan_identifier("and")? {
break;
}
buf.push_str(" and ");
}
Ok(buf)
}
}