pagesift_syntax/scanner/
d.rs1use super::{ScanLexer, Scanner, is_space};
5
6const DIRECTIVE: usize = 0;
7const L_INT: usize = 1;
8const L_FLOAT: usize = 2;
9const L_STRING: usize = 3;
10const NOT_IN: usize = 4;
11const NOT_IS: usize = 5;
12const AFTER_EOF: usize = 6;
13const ERROR: usize = 7;
14
15const HEREDOC_DELIM_MAX: usize = 256;
16
17#[derive(Default)]
18pub struct D;
19
20fn is_eol(c: char) -> bool {
21 matches!(c, '\n' | '\r' | '\u{2028}' | '\u{2029}')
22}
23
24fn alnum(c: char) -> bool {
25 c.is_alphanumeric()
26}
27
28fn match_string_suffix(lx: &mut ScanLexer) {
30 if matches!(lx.lookahead(), 'c' | 'd' | 'w') {
31 lx.next();
32 }
33 lx.mark_end();
34}
35
36fn match_delimited_string(lx: &mut ScanLexer, start: char, end: char) -> bool {
37 let mut nest = 0u32;
38 let mut first = true;
39 lx.next();
40 while lx.lookahead() != '\0' {
41 let c = lx.lookahead();
42 if c == start && start != '\0' {
43 nest += 1;
44 }
45 if c == end {
46 if nest > 0 {
47 nest -= 1;
48 } else if !first {
49 lx.next();
50 if lx.lookahead() != '"' {
51 continue;
53 }
54 lx.next();
55 lx.set_result(L_STRING as u16);
56 match_string_suffix(lx);
57 return true;
58 }
59 }
60 first = false;
61 lx.next();
62 }
63 false
64}
65
66fn match_heredoc_string(lx: &mut ScanLexer) -> bool {
67 let mut id: Vec<char> = Vec::new();
68 while id.len() < HEREDOC_DELIM_MAX {
69 let c = lx.lookahead();
70 if is_eol(c) || (!alnum(c) && c != '_') {
71 break;
72 }
73 id.push(c);
74 lx.next();
75 }
76 if id.is_empty() {
77 return false;
78 }
79 let c = lx.lookahead();
81 if id.len() == HEREDOC_DELIM_MAX && !is_eol(c) && (alnum(c) || c == '_') {
82 return false;
83 }
84 id.push('"');
85 while lx.lookahead() != '\0' {
86 while !is_eol(lx.lookahead()) && lx.lookahead() != '\0' {
87 lx.next();
88 }
89 lx.next();
90 let mut j = 0;
91 while lx.lookahead() != '\0' && j < id.len() {
92 if lx.lookahead() != id[j] {
93 break;
94 }
95 lx.next();
96 j += 1;
97 }
98 if j == id.len() {
99 match_string_suffix(lx);
100 lx.set_result(L_STRING as u16);
101 return true;
102 }
103 }
104 false
105}
106
107fn match_directive(lx: &mut ScanLexer, v: &[bool]) -> bool {
108 if !v[DIRECTIVE] {
109 return false;
110 }
111 lx.next();
112 if lx.lookahead() == '!' {
113 return false;
114 }
115 while (is_space(lx.lookahead()) || is_eol(lx.lookahead())) && lx.lookahead() != '\0' {
116 if is_eol(lx.lookahead()) {
117 return false;
118 }
119 lx.next();
120 }
121 while !is_eol(lx.lookahead()) && lx.lookahead() != '\0' {
122 lx.next();
123 }
124 lx.next();
125 lx.mark_end();
126 lx.set_result(DIRECTIVE as u16);
127 true
128}
129
130fn match_number_suffix(lx: &mut ScanLexer, v: &[bool], is_float: bool) -> bool {
131 let (mut seen_l, mut seen_i, mut seen_u, mut seen_f) = (false, false, false, false);
132 let mut tok = 0usize;
133 loop {
134 let c = lx.lookahead();
135 if c == '\0' {
136 break;
137 }
138 match c {
139 'u' | 'U' => {
140 if seen_u || seen_i || seen_f || is_float {
141 return false;
142 }
143 seen_u = true;
144 tok = L_INT;
145 }
146 'f' | 'F' => {
147 if seen_u || seen_f || seen_i {
148 return false;
149 }
150 seen_f = true;
151 tok = L_FLOAT;
152 }
153 'i' => {
154 if seen_i || seen_u {
156 return false;
157 }
158 tok = L_FLOAT;
159 seen_i = true;
160 }
161 'L' => {
162 if seen_l || seen_f || seen_i {
163 return false;
164 }
165 seen_l = true;
166 }
167 _ => break,
168 }
169 lx.next();
170 }
171 let c = lx.lookahead();
173 if alnum(c) || (c as u32 > 0x7f && !is_eol(c)) {
174 return false;
175 }
176 if is_float {
177 tok = L_FLOAT;
178 }
179 if v[L_INT] && tok != L_FLOAT {
180 lx.set_result(L_INT as u16);
181 lx.mark_end();
182 return true;
183 }
184 if v[L_FLOAT] && tok != L_INT {
185 lx.set_result(L_FLOAT as u16);
186 lx.mark_end();
187 return true;
188 }
189 false
190}
191
192fn match_number(lx: &mut ScanLexer, v: &[bool]) -> bool {
193 let mut c = lx.lookahead();
194 let (mut is_hex, mut is_bin, mut has_digit, mut has_dot, mut in_exp) =
195 (false, false, false, false, false);
196 if c == '.' {
197 lx.next();
198 if !lx.lookahead().is_ascii_digit() {
199 return false;
200 }
201 has_dot = true;
202 } else if c == '0' {
203 lx.next();
204 match lx.lookahead() {
205 'b' | 'B' => {
206 is_bin = true;
207 lx.next();
208 }
209 'x' | 'X' => {
210 is_hex = true;
211 lx.next();
212 }
213 _ => has_digit = true,
214 }
215 }
216 if !(v[L_INT] || v[L_FLOAT]) {
217 return false;
218 }
219 let mut done = false;
220 while lx.lookahead() != '\0' && !done {
221 c = lx.lookahead();
222 if c as u32 > 0x7f || is_space(c) || c == ';' {
223 break;
224 }
225 if (is_bin && matches!(c, '0' | '1'))
226 || c.is_ascii_digit()
227 || (is_hex && !in_exp && c.is_ascii_hexdigit())
228 {
229 lx.next();
230 lx.mark_end();
231 has_digit = true;
232 continue;
233 }
234 match c {
235 '.' => {
236 if !has_digit || has_dot || in_exp || is_bin {
237 lx.mark_end();
238 done = true;
239 continue;
240 }
241 lx.mark_end();
242 lx.next();
243 c = lx.lookahead();
244 if c.is_ascii_digit() || (is_hex && c.is_ascii_hexdigit()) {
245 has_dot = true;
246 continue;
247 }
248 if alnum(c) || c == '_' || c == '.' || (c as u32 > 0x7f && !is_eol(c)) {
250 lx.set_result(L_INT as u16);
251 return v[L_INT];
252 }
253 lx.set_result(L_FLOAT as u16);
254 lx.mark_end();
255 return v[L_FLOAT];
256 }
257 '_' => lx.next(),
258 'e' | 'E' | 'p' | 'P' => {
259 if in_exp || is_bin {
260 return false;
261 }
262 if is_hex && matches!(c, 'e' | 'E') {
263 return false;
264 }
265 if !is_hex && matches!(c, 'p' | 'P') {
266 return false;
267 }
268 lx.next();
269 if matches!(lx.lookahead(), '+' | '-') {
270 lx.next();
271 }
272 has_digit = false;
273 in_exp = true;
274 }
275 _ => done = true,
276 }
277 }
278 if !has_digit {
279 return false;
280 }
281 match_number_suffix(lx, v, has_dot || in_exp)
282}
283
284fn match_not_in_is(lx: &mut ScanLexer, v: &[bool]) -> bool {
285 if !v[NOT_IN] && !v[NOT_IS] {
286 return false;
287 }
288 lx.next();
289 while lx.lookahead() != '\0' && (is_space(lx.lookahead()) || is_eol(lx.lookahead())) {
290 lx.next();
291 }
292 if lx.lookahead() != 'i' {
293 return false;
294 }
295 lx.next();
296 let token = match lx.lookahead() {
297 'n' => NOT_IN,
298 's' => NOT_IS,
299 _ => return false,
300 };
301 if !v[token] {
302 return false;
303 }
304 lx.next();
305 let c = lx.lookahead();
306 if alnum(c) || (c as u32 > 0x7f && !is_eol(c)) {
307 return false;
308 }
309 lx.set_result(token as u16);
310 lx.mark_end();
311 true
312}
313
314impl Scanner for D {
315 fn scan(&mut self, lx: &mut ScanLexer, v: &[bool]) -> bool {
316 let mut c = lx.lookahead();
317 let mut start_of_line = lx.column() == 0;
318 if v[AFTER_EOF] && !v[ERROR] {
319 while lx.lookahead() != '\0' {
320 lx.skip();
321 }
322 lx.mark_end();
323 lx.set_result(AFTER_EOF as u16);
324 return true;
325 }
326 while (is_space(c) || is_eol(c)) && c != '\0' {
327 if is_eol(c) {
328 start_of_line = true;
329 }
330 lx.skip();
331 c = lx.lookahead();
332 }
333 if c == '#' && start_of_line {
334 return match_directive(lx, v);
335 }
336 if lx.eof() {
337 return false;
338 }
339 if c == '.' || c.is_ascii_digit() {
340 return match_number(lx, v);
341 }
342 if c == '!' {
344 return match_not_in_is(lx, v);
345 }
346 if c == 'q' && v[L_STRING] {
347 lx.next();
348 if lx.lookahead() != '"' {
349 return false;
350 }
351 lx.next();
352 let c = lx.lookahead();
353 return match c {
354 '(' => match_delimited_string(lx, '(', ')'),
355 '[' => match_delimited_string(lx, '[', ']'),
356 '{' => match_delimited_string(lx, '{', '}'),
357 '<' => match_delimited_string(lx, '<', '>'),
358 _ => {
359 if alnum(c) || c == '_' {
360 match_heredoc_string(lx)
361 } else {
362 match_delimited_string(lx, '\0', c)
363 }
364 }
365 };
366 }
367 false
368 }
369
370 fn serialize(&self, _out: &mut Vec<u8>) {}
371
372 fn deserialize(&mut self, _data: &[u8]) {}
373}