1use super::{ScanLexer, Scanner};
5
6const QUOTED_CONTENT_I_SINGLE: usize = 0;
7const QUOTED_CONTENT_I_DOUBLE: usize = 1;
8const NEWLINE_BEFORE_DO: usize = 20;
9const NEWLINE_BEFORE_BINARY_OPERATOR: usize = 21;
10const NEWLINE_BEFORE_COMMENT: usize = 22;
11const BEFORE_UNARY_OPERATOR: usize = 23;
12const NOT_IN: usize = 24;
13const QUOTED_ATOM_START: usize = 25;
14
15const QUOTED: [(bool, char, u8); 20] = [
17 (true, '\'', 1),
18 (true, '"', 1),
19 (true, '\'', 3),
20 (true, '"', 3),
21 (true, ')', 1),
22 (true, '}', 1),
23 (true, ']', 1),
24 (true, '>', 1),
25 (true, '|', 1),
26 (true, '/', 1),
27 (false, '\'', 1),
28 (false, '"', 1),
29 (false, '\'', 3),
30 (false, '"', 3),
31 (false, ')', 1),
32 (false, '}', 1),
33 (false, ']', 1),
34 (false, '>', 1),
35 (false, '|', 1),
36 (false, '/', 1),
37];
38
39#[derive(Default)]
40pub struct Elixir;
41
42fn is_ws(c: char) -> bool {
43 matches!(c, ' ' | '\t' | '\n' | '\r')
44}
45
46fn is_inline_ws(c: char) -> bool {
47 matches!(c, ' ' | '\t')
48}
49
50fn is_newline(c: char) -> bool {
51 matches!(c, '\n' | '\r')
52}
53
54fn check_keyword_end(lx: &mut ScanLexer) -> bool {
55 if lx.lookahead() == ':' {
56 lx.next();
57 return is_ws(lx.lookahead());
58 }
59 false
60}
61
62fn check_operator_end(lx: &mut ScanLexer) -> bool {
63 if lx.lookahead() == ':' {
64 return !check_keyword_end(lx);
65 }
66 while is_inline_ws(lx.lookahead()) {
67 lx.next();
68 }
69 if lx.lookahead() == '/' {
70 lx.next();
71 while is_ws(lx.lookahead()) {
72 lx.next();
73 }
74 if lx.lookahead().is_ascii_digit() {
75 return false;
76 }
77 }
78 true
79}
80
81fn is_token_end(c: char) -> bool {
82 "@.+-^*/<>|~=&\\%{}[]()\"',;#".contains(c) || is_ws(c)
83}
84
85fn scan_quoted_content(lx: &mut ScanLexer, idx: usize) -> bool {
86 let (interp, end, len) = QUOTED[idx];
87 lx.set_result(idx as u16);
88 let heredoc = len == 3;
89 let mut has_content = false;
90 loop {
91 let mut newline = false;
92 if is_newline(lx.lookahead()) {
93 lx.next();
94 has_content = true;
95 newline = true;
96 while is_ws(lx.lookahead()) {
97 lx.next();
98 }
99 }
100 lx.mark_end();
101 if lx.lookahead() == end {
102 let mut length = 1;
103 while length < len {
104 lx.next();
105 if lx.lookahead() == end {
106 length += 1;
107 } else {
108 break;
109 }
110 }
111 if length == len && (!heredoc || newline) {
112 return has_content;
113 }
114 } else if lx.lookahead() == '#' {
115 lx.next();
116 if interp && lx.lookahead() == '{' {
117 return has_content;
118 }
119 } else if lx.lookahead() == '\\' {
120 lx.next();
121 if heredoc && lx.lookahead() == '\n' {
122 } else if interp || lx.lookahead() == end {
124 return has_content;
125 }
126 } else if lx.lookahead() == '\0' {
127 return has_content;
129 } else {
130 lx.next();
131 }
132 has_content = true;
133 }
134}
135
136fn eat_all(lx: &mut ScanLexer, chars: &str) -> bool {
138 for c in chars.chars() {
139 if lx.lookahead() != c {
140 return false;
141 }
142 lx.next();
143 }
144 true
145}
146
147fn scan_binary_operator(lx: &mut ScanLexer) -> bool {
149 let c = lx.lookahead();
150 match c {
151 '&' => {
152 lx.next();
153 if lx.lookahead() == '&' {
154 lx.next();
155 if lx.lookahead() == '&' {
156 lx.next();
157 }
158 return check_operator_end(lx);
159 }
160 }
161 '=' => {
162 lx.next();
163 if lx.lookahead() == '=' {
164 lx.next();
165 if lx.lookahead() == '=' {
166 lx.next();
167 }
168 } else if matches!(lx.lookahead(), '~' | '>') {
169 lx.next();
170 }
171 return check_operator_end(lx);
172 }
173 ':' => {
174 lx.next();
175 if lx.lookahead() == ':' {
176 lx.next();
177 if lx.lookahead() == ':' {
178 return false;
179 }
180 return check_operator_end(lx);
181 }
182 }
183 '+' => {
184 lx.next();
185 if lx.lookahead() == '+' {
186 lx.next();
187 if lx.lookahead() == '+' {
188 lx.next();
189 }
190 return check_operator_end(lx);
191 }
192 }
193 '-' => {
194 lx.next();
195 if lx.lookahead() == '-' {
196 lx.next();
197 if lx.lookahead() == '-' {
198 lx.next();
199 }
200 return check_operator_end(lx);
201 } else if lx.lookahead() == '>' {
202 lx.next();
203 return check_operator_end(lx);
204 }
205 }
206 '<' => {
207 lx.next();
208 match lx.lookahead() {
209 '=' | '-' | '>' => {
210 lx.next();
211 return check_operator_end(lx);
212 }
213 '~' => {
214 lx.next();
215 if lx.lookahead() == '>' {
216 lx.next();
217 }
218 return check_operator_end(lx);
219 }
220 '|' => {
221 lx.next();
222 if lx.lookahead() == '>' {
223 lx.next();
224 return check_operator_end(lx);
225 }
226 }
227 '<' => {
228 lx.next();
229 if matches!(lx.lookahead(), '<' | '~') {
230 lx.next();
231 return check_operator_end(lx);
232 }
233 }
234 _ => return check_operator_end(lx),
235 }
236 }
237 '>' => {
238 lx.next();
239 if lx.lookahead() == '=' {
240 lx.next();
241 return check_operator_end(lx);
242 } else if lx.lookahead() == '>' {
243 lx.next();
244 if lx.lookahead() == '>' {
245 lx.next();
246 return check_operator_end(lx);
247 }
248 } else {
249 return check_operator_end(lx);
250 }
251 }
252 '^' if eat_all(lx, "^^^") => {
253 return check_operator_end(lx);
254 }
255 '!' => {
256 lx.next();
257 if lx.lookahead() == '=' {
258 lx.next();
259 if lx.lookahead() == '=' {
260 lx.next();
261 }
262 return check_operator_end(lx);
263 }
264 }
265 '~' => {
266 lx.next();
267 if lx.lookahead() == '>' {
268 lx.next();
269 if lx.lookahead() == '>' {
270 lx.next();
271 }
272 return check_operator_end(lx);
273 }
274 }
275 '|' => {
276 lx.next();
277 if lx.lookahead() == '|' {
278 lx.next();
279 if lx.lookahead() == '|' {
280 lx.next();
281 }
282 } else if lx.lookahead() == '>' {
283 lx.next();
284 }
285 return check_operator_end(lx);
286 }
287 '*' => {
288 lx.next();
289 if lx.lookahead() == '*' {
290 lx.next();
291 }
292 return check_operator_end(lx);
293 }
294 '/' => {
295 lx.next();
296 if lx.lookahead() == '/' {
297 lx.next();
298 }
299 return check_operator_end(lx);
300 }
301 '.' => {
302 lx.next();
303 if lx.lookahead() == '.' {
304 lx.next();
305 if lx.lookahead() == '.' {
306 return false;
307 }
308 }
309 return check_operator_end(lx);
310 }
311 '\\' => {
312 lx.next();
313 if lx.lookahead() == '\\' {
314 lx.next();
315 return check_operator_end(lx);
316 }
317 }
318 'w' if eat_all(lx, "when") => {
319 return is_token_end(lx.lookahead()) && check_operator_end(lx);
320 }
321 'a' if eat_all(lx, "and") => {
322 return is_token_end(lx.lookahead()) && check_operator_end(lx);
323 }
324 'o' if eat_all(lx, "or") => {
325 return is_token_end(lx.lookahead()) && check_operator_end(lx);
326 }
327 'i' if eat_all(lx, "in") => {
328 return is_token_end(lx.lookahead()) && check_operator_end(lx);
329 }
330 'n' if eat_all(lx, "not") => {
331 while is_inline_ws(lx.lookahead()) {
332 lx.next();
333 }
334 if eat_all(lx, "in") {
335 return is_token_end(lx.lookahead()) && check_operator_end(lx);
336 }
337 }
338 _ => {}
339 }
340 false
341}
342
343fn scan_newline(lx: &mut ScanLexer, v: &[bool]) -> bool {
344 lx.next();
345 while is_ws(lx.lookahead()) {
346 lx.next();
347 }
348 lx.mark_end();
350 if lx.lookahead() == '#' {
351 lx.set_result(NEWLINE_BEFORE_COMMENT as u16);
352 return true;
353 }
354 if lx.lookahead() == 'd' && v[NEWLINE_BEFORE_DO] {
355 lx.set_result(NEWLINE_BEFORE_DO as u16);
356 lx.next();
357 if lx.lookahead() == 'o' {
358 lx.next();
359 return is_token_end(lx.lookahead());
360 }
361 return false;
362 }
363 if v[NEWLINE_BEFORE_BINARY_OPERATOR] {
364 lx.set_result(NEWLINE_BEFORE_BINARY_OPERATOR as u16);
365 return scan_binary_operator(lx);
366 }
367 false
368}
369
370impl Scanner for Elixir {
371 fn scan(&mut self, lx: &mut ScanLexer, v: &[bool]) -> bool {
372 if !(v[QUOTED_CONTENT_I_SINGLE] && v[QUOTED_CONTENT_I_DOUBLE])
374 && let Some(idx) = (0..QUOTED.len()).find(|&i| v[i])
375 {
376 return scan_quoted_content(lx, idx);
377 }
378 let mut skipped = false;
379 while is_inline_ws(lx.lookahead()) {
380 skipped = true;
381 lx.skip();
382 }
383 if is_newline(lx.lookahead())
384 && (v[NEWLINE_BEFORE_DO]
385 || v[NEWLINE_BEFORE_BINARY_OPERATOR]
386 || v[NEWLINE_BEFORE_COMMENT])
387 {
388 return scan_newline(lx, v);
389 }
390 match lx.lookahead() {
391 '+' if skipped && v[BEFORE_UNARY_OPERATOR] => {
392 lx.mark_end();
393 lx.next();
394 if matches!(lx.lookahead(), '+' | ':' | '/') || is_ws(lx.lookahead()) {
395 return false;
396 }
397 lx.set_result(BEFORE_UNARY_OPERATOR as u16);
398 return true;
399 }
400 '-' if skipped && v[BEFORE_UNARY_OPERATOR] => {
401 lx.mark_end();
402 lx.set_result(BEFORE_UNARY_OPERATOR as u16);
403 lx.next();
404 if matches!(lx.lookahead(), '-' | '>' | ':' | '/') || is_ws(lx.lookahead()) {
405 return false;
406 }
407 return true;
408 }
409 'n' if v[NOT_IN] => {
410 lx.set_result(NOT_IN as u16);
411 if eat_all(lx, "not") {
412 while is_inline_ws(lx.lookahead()) {
413 lx.next();
414 }
415 if eat_all(lx, "in") {
416 return is_token_end(lx.lookahead());
417 }
418 }
419 }
420 ':' if v[QUOTED_ATOM_START] => {
421 lx.next();
422 lx.mark_end();
423 lx.set_result(QUOTED_ATOM_START as u16);
424 if matches!(lx.lookahead(), '"' | '\'') {
425 return true;
426 }
427 }
428 _ => {}
429 }
430 false
431 }
432
433 fn serialize(&self, _out: &mut Vec<u8>) {}
434
435 fn deserialize(&mut self, _data: &[u8]) {}
436}