yolowsyntax/scanner/
ruby.rs1mod content;
5
6use super::{MAX_SERIALIZED, ScanLexer, Scanner, is_alpha, is_space};
7
8const LINE_BREAK: usize = 0;
9const NO_LINE_BREAK: usize = 1;
10const SIMPLE_SYMBOL: usize = 2;
11const STRING_START: usize = 3;
12const SYMBOL_START: usize = 4;
13const SUBSHELL_START: usize = 5;
14const REGEX_START: usize = 6;
15const STRING_ARRAY_START: usize = 7;
16const SYMBOL_ARRAY_START: usize = 8;
17const HEREDOC_BODY_START: usize = 9;
18const STRING_CONTENT: usize = 10;
19const HEREDOC_CONTENT: usize = 11;
20const STRING_END: usize = 12;
21const HEREDOC_BODY_END: usize = 13;
22const HEREDOC_START: usize = 14;
23const FORWARD_SLASH: usize = 15;
24const BLOCK_AMPERSAND: usize = 16;
25const SPLAT_STAR: usize = 17;
26const UNARY_MINUS: usize = 18;
27const UNARY_MINUS_NUM: usize = 19;
28const BINARY_MINUS: usize = 20;
29const BINARY_STAR: usize = 21;
30const SINGLETON_CLASS_LEFT_ANGLE_LEFT_ANGLE: usize = 22;
31const HASH_KEY_SYMBOL: usize = 23;
32const IDENTIFIER_SUFFIX: usize = 24;
33const CONSTANT_SUFFIX: usize = 25;
34const HASH_SPLAT_STAR_STAR: usize = 26;
35const BINARY_STAR_STAR: usize = 27;
36const ELEMENT_REFERENCE_BRACKET: usize = 28;
37const SHORT_INTERPOLATION: usize = 29;
38const NONE: u16 = 30;
39
40const NON_IDENTIFIER_CHARS: &[u8] = b"\0\n\r\t :;`\"'@$#.,|^&<=>+-*/\\%?!~()[]{}";
41
42#[derive(Clone, Default)]
43struct Literal {
44 kind: u8,
45 open: i32,
46 close: i32,
47 depth: i32,
48 interp: bool,
49}
50
51#[derive(Clone, Default)]
52struct Heredoc {
53 word: Vec<u8>,
55 indent_ok: bool,
56 interp: bool,
57 started: bool,
58}
59
60#[derive(Default)]
61pub struct Ruby {
62 leading_ws: bool,
63 literals: Vec<Literal>,
64 heredocs: Vec<Heredoc>,
65}
66
67fn alnum(c: char) -> bool {
68 c.is_alphanumeric()
69}
70
71fn is_iden_char(c: char) -> bool {
73 !NON_IDENTIFIER_CHARS.contains(&(c as u32 as u8))
74}
75
76fn scan_operator(lx: &mut ScanLexer) -> bool {
77 match lx.lookahead() {
78 '<' => {
79 lx.next();
80 if lx.lookahead() == '<' {
81 lx.next();
82 } else if lx.lookahead() == '=' {
83 lx.next();
84 if lx.lookahead() == '>' {
85 lx.next();
86 }
87 }
88 true
89 }
90 '>' => {
91 lx.next();
92 if matches!(lx.lookahead(), '>' | '=') {
93 lx.next();
94 }
95 true
96 }
97 '=' => {
98 lx.next();
99 if lx.lookahead() == '~' {
100 lx.next();
101 return true;
102 }
103 if lx.lookahead() == '=' {
104 lx.next();
105 if lx.lookahead() == '=' {
106 lx.next();
107 }
108 return true;
109 }
110 false
111 }
112 '+' | '-' | '~' => {
113 lx.next();
114 if lx.lookahead() == '@' {
115 lx.next();
116 }
117 true
118 }
119 '.' => {
120 lx.next();
121 if lx.lookahead() == '.' {
122 lx.next();
123 return true;
124 }
125 false
126 }
127 '&' | '^' | '|' | '/' | '%' | '`' => {
128 lx.next();
129 true
130 }
131 '!' => {
132 lx.next();
133 if matches!(lx.lookahead(), '=' | '~') {
134 lx.next();
135 }
136 true
137 }
138 '*' => {
139 lx.next();
140 if lx.lookahead() == '*' {
141 lx.next();
142 }
143 true
144 }
145 '[' => {
146 lx.next();
147 if lx.lookahead() == ']' {
148 lx.next();
149 } else {
150 return false;
151 }
152 if lx.lookahead() == '=' {
153 lx.next();
154 }
155 true
156 }
157 _ => false,
158 }
159}
160
161fn scan_symbol_identifier(lx: &mut ScanLexer) -> bool {
162 if lx.lookahead() == '@' {
163 lx.next();
164 if lx.lookahead() == '@' {
165 lx.next();
166 }
167 } else if lx.lookahead() == '$' {
168 lx.next();
169 }
170 if is_iden_char(lx.lookahead()) {
171 lx.next();
172 } else if !scan_operator(lx) {
173 return false;
174 }
175 while is_iden_char(lx.lookahead()) {
176 lx.next();
177 }
178 if matches!(lx.lookahead(), '?' | '!') {
179 lx.next();
180 }
181 if lx.lookahead() == '=' {
182 lx.mark_end();
183 lx.next();
184 if lx.lookahead() != '>' {
185 lx.mark_end();
186 }
187 }
188 true
189}
190
191fn scan_heredoc_word(lx: &mut ScanLexer, h: &mut Heredoc) {
192 let mut quote = '\0';
193 let mut word = Vec::new();
194 match lx.lookahead() {
195 q @ ('\'' | '"' | '`') => {
196 quote = q;
197 lx.next();
198 while lx.lookahead() != quote && !lx.eof() {
199 word.push(lx.lookahead() as u32 as u8);
200 lx.next();
201 }
202 lx.next();
203 }
204 c if alnum(c) || c == '_' => {
205 while alnum(lx.lookahead()) || lx.lookahead() == '_' {
206 word.push(lx.lookahead() as u32 as u8);
207 lx.next();
208 }
209 }
210 _ => {}
211 }
212 h.word = word;
213 h.interp = quote != '\'';
214}
215
216fn scan_short_interpolation(lx: &mut ScanLexer, has_content: bool, content: usize) -> bool {
217 let start = lx.lookahead();
218 if start != '@' && start != '$' {
219 return false;
220 }
221 if has_content {
222 lx.set_result(content as u16);
223 return true;
224 }
225 lx.mark_end();
226 lx.next();
227 let mut short = false;
228 if start == '$' {
229 let c = lx.lookahead();
231 if c == '\0'
232 || "!@&`'+~=/\\,;.<>*$?:\""
233 .as_bytes()
234 .contains(&(c as u32 as u8))
235 {
236 short = true;
237 } else if c == '-' {
238 lx.next();
239 short = is_alpha(lx.lookahead()) || lx.lookahead() == '_';
240 } else {
241 short = alnum(c) || c == '_';
242 }
243 }
244 if start == '@' {
245 if lx.lookahead() == '@' {
246 lx.next();
247 }
248 short = is_iden_char(lx.lookahead()) && !lx.lookahead().is_ascii_digit();
249 }
250 if short {
251 lx.set_result(SHORT_INTERPOLATION as u16);
252 }
253 short
254}
255
256impl Ruby {
257 fn skip(&mut self, lx: &mut ScanLexer) {
258 self.leading_ws = true;
259 lx.skip();
260 }
261
262 fn scan_whitespace(&mut self, lx: &mut ScanLexer, v: &[bool]) -> bool {
263 let hd_start =
264 !self.heredocs.is_empty() && !self.heredocs[0].started && v[HEREDOC_BODY_START];
265 let mut crossed = false;
266 loop {
267 match lx.lookahead() {
268 ' ' | '\t' => self.skip(lx),
269 '\r' => {
270 if hd_start {
271 lx.set_result(HEREDOC_BODY_START as u16);
272 self.heredocs[0].started = true;
273 return true;
274 }
275 self.skip(lx);
276 }
277 '\n' => {
278 if hd_start {
279 lx.set_result(HEREDOC_BODY_START as u16);
280 self.heredocs[0].started = true;
281 return true;
282 } else if !v[NO_LINE_BREAK] && v[LINE_BREAK] && !crossed {
283 lx.mark_end();
284 lx.next();
285 crossed = true;
286 } else {
287 self.skip(lx);
288 }
289 }
290 '\\' => {
291 lx.next();
292 if lx.lookahead() == '\r' {
293 self.skip(lx);
294 }
295 if is_space(lx.lookahead()) {
296 self.skip(lx);
297 } else {
298 return false;
299 }
300 }
301 c => {
302 if crossed {
303 if !matches!(c, '.' | '&' | '#') {
304 lx.set_result(LINE_BREAK as u16);
305 } else if c == '.' {
306 lx.next();
308 if !lx.eof() && lx.lookahead() == '.' {
309 lx.set_result(LINE_BREAK as u16);
310 } else {
311 return false;
312 }
313 }
314 }
315 return true;
316 }
317 }
318 }
319 }
320}
321
322impl Ruby {
323 fn run(&mut self, lx: &mut ScanLexer, v: &[bool]) -> bool {
324 self.leading_ws = false;
325 if !v[STRING_START] {
326 if (v[STRING_CONTENT] || v[STRING_END]) && !self.literals.is_empty() {
327 return self.literal_content(lx);
328 }
329 if (v[HEREDOC_CONTENT] || v[HEREDOC_BODY_END]) && !self.heredocs.is_empty() {
330 return self.heredoc_content(lx);
331 }
332 }
333 lx.set_result(NONE);
334 if !self.scan_whitespace(lx, v) {
335 return false;
336 }
337 if lx.result != NONE {
338 return true;
339 }
340 let ws = self.leading_ws;
341 match lx.lookahead() {
342 '&' if v[BLOCK_AMPERSAND] => {
343 lx.next();
344 if !matches!(lx.lookahead(), '&' | '.' | '=') && !is_space(lx.lookahead()) {
345 lx.set_result(BLOCK_AMPERSAND as u16);
346 return true;
347 }
348 return false;
349 }
350 '<' if v[SINGLETON_CLASS_LEFT_ANGLE_LEFT_ANGLE] => {
351 lx.next();
352 if lx.lookahead() == '<' {
353 lx.next();
354 lx.set_result(SINGLETON_CLASS_LEFT_ANGLE_LEFT_ANGLE as u16);
355 return true;
356 }
357 return false;
358 }
359 '*' if v[SPLAT_STAR]
360 || v[BINARY_STAR]
361 || v[HASH_SPLAT_STAR_STAR]
362 || v[BINARY_STAR_STAR] =>
363 {
364 lx.next();
365 if lx.lookahead() == '=' {
366 return false;
367 }
368 if lx.lookahead() == '*' {
369 if v[HASH_SPLAT_STAR_STAR] || v[BINARY_STAR_STAR] {
370 lx.next();
371 if lx.lookahead() == '=' {
372 return false;
373 }
374 let r = if v[BINARY_STAR_STAR] && !ws {
375 BINARY_STAR_STAR
376 } else if v[HASH_SPLAT_STAR_STAR] && !is_space(lx.lookahead()) {
377 HASH_SPLAT_STAR_STAR
378 } else if v[BINARY_STAR_STAR] {
379 BINARY_STAR_STAR
380 } else if v[HASH_SPLAT_STAR_STAR] {
381 HASH_SPLAT_STAR_STAR
382 } else {
383 return false;
384 };
385 lx.set_result(r as u16);
386 return true;
387 }
388 return false;
389 }
390 let r = if v[BINARY_STAR] && !ws {
391 BINARY_STAR
392 } else if v[SPLAT_STAR] && !is_space(lx.lookahead()) {
393 SPLAT_STAR
394 } else if v[BINARY_STAR] {
395 BINARY_STAR
396 } else if v[SPLAT_STAR] {
397 SPLAT_STAR
398 } else {
399 return false;
400 };
401 lx.set_result(r as u16);
402 return true;
403 }
404 '-' if v[UNARY_MINUS] || v[UNARY_MINUS_NUM] || v[BINARY_MINUS] => {
405 lx.next();
406 if !matches!(lx.lookahead(), '=' | '>') {
407 if v[UNARY_MINUS_NUM]
408 && (!v[BINARY_STAR] || ws)
409 && lx.lookahead().is_ascii_digit()
410 {
411 lx.set_result(UNARY_MINUS_NUM as u16);
412 return true;
413 }
414 let r = if v[UNARY_MINUS] && ws && !is_space(lx.lookahead()) {
415 UNARY_MINUS
416 } else if v[BINARY_MINUS] {
417 BINARY_MINUS
418 } else {
419 UNARY_MINUS
420 };
421 lx.set_result(r as u16);
422 return true;
423 }
424 return false;
425 }
426 ':' if v[SYMBOL_START] => {
427 let mut lit = Literal {
428 kind: SYMBOL_START as u8,
429 depth: 1,
430 ..Literal::default()
431 };
432 lx.next();
433 match lx.lookahead() {
434 q @ ('"' | '\'') => {
435 lx.next();
436 lit.open = q as i32;
437 lit.close = q as i32;
438 lit.interp = q == '"';
439 self.literals.push(lit);
440 lx.set_result(SYMBOL_START as u16);
441 return true;
442 }
443 _ => {
444 if scan_symbol_identifier(lx) {
445 lx.set_result(SIMPLE_SYMBOL as u16);
446 return true;
447 }
448 }
449 }
450 return false;
451 }
452 '[' if v[ELEMENT_REFERENCE_BRACKET] && (!ws || !v[STRING_START]) => {
453 lx.next();
454 lx.set_result(ELEMENT_REFERENCE_BRACKET as u16);
455 return true;
456 }
457 _ => {}
458 }
459
460 let c = lx.lookahead();
461 if ((v[HASH_KEY_SYMBOL] || v[IDENTIFIER_SUFFIX]) && (is_alpha(c) || c == '_'))
462 || (v[CONSTANT_SUFFIX] && c.is_uppercase())
463 {
464 let suffix = if c.is_uppercase() {
465 CONSTANT_SUFFIX
466 } else {
467 IDENTIFIER_SUFFIX
468 };
469 while alnum(lx.lookahead()) || lx.lookahead() == '_' {
470 lx.next();
471 }
472 if v[HASH_KEY_SYMBOL] && lx.lookahead() == ':' {
473 lx.mark_end();
474 lx.next();
475 if lx.lookahead() != ':' {
476 lx.set_result(HASH_KEY_SYMBOL as u16);
477 return true;
478 }
479 } else if v[suffix] && lx.lookahead() == '!' {
480 lx.next();
481 if lx.lookahead() != '=' {
482 lx.set_result(suffix as u16);
483 return true;
484 }
485 }
486 return false;
487 }
488
489 if v[STRING_START] {
490 let mut lit = Literal {
491 depth: 1,
492 ..Literal::default()
493 };
494 if lx.lookahead() == '<' {
495 lx.next();
496 if lx.lookahead() != '<' {
497 return false;
498 }
499 lx.next();
500 let mut h = Heredoc::default();
501 if matches!(lx.lookahead(), '-' | '~') {
502 lx.next();
503 h.indent_ok = true;
504 }
505 scan_heredoc_word(lx, &mut h);
506 if h.word.is_empty() {
507 return false;
508 }
509 self.heredocs.push(h);
510 lx.set_result(HEREDOC_START as u16);
511 return true;
512 }
513 if self.open_delimiter(lx, &mut lit, v) {
514 lx.set_result(lit.kind as u16);
515 self.literals.push(lit);
516 return true;
517 }
518 return false;
519 }
520 false
521 }
522}
523
524impl Scanner for Ruby {
525 fn scan(&mut self, lx: &mut ScanLexer, v: &[bool]) -> bool {
526 self.run(lx, v)
527 }
528
529 fn serialize(&self, out: &mut Vec<u8>) {
530 if self.literals.len() * 5 + 2 >= MAX_SERIALIZED {
531 return;
532 }
533 out.push(self.literals.len() as u8);
534 for l in &self.literals {
535 out.extend_from_slice(&[
536 l.kind,
537 l.open as u8,
538 l.close as u8,
539 l.depth as u8,
540 l.interp as u8,
541 ]);
542 }
543 out.push(self.heredocs.len() as u8);
544 for h in &self.heredocs {
545 if out.len() + 3 + 4 + h.word.len() >= MAX_SERIALIZED {
546 out.clear();
547 return;
548 }
549 out.extend_from_slice(&[h.indent_ok as u8, h.interp as u8, h.started as u8]);
550 out.extend_from_slice(&(h.word.len() as u32).to_le_bytes());
551 out.extend_from_slice(&h.word);
552 }
553 }
554
555 fn deserialize(&mut self, d: &[u8]) {
556 self.leading_ws = false;
557 self.literals.clear();
558 self.heredocs.clear();
559 if d.is_empty() {
560 return;
561 }
562 let mut p = 0usize;
563 let n = d[p] as usize;
564 p += 1;
565 for _ in 0..n {
566 if p + 5 > d.len() {
567 return;
568 }
569 self.literals.push(Literal {
570 kind: d[p],
571 open: d[p + 1] as i32,
572 close: d[p + 2] as i32,
573 depth: d[p + 3] as i32,
574 interp: d[p + 4] != 0,
575 });
576 p += 5;
577 }
578 if p >= d.len() {
579 return;
580 }
581 let n = d[p] as usize;
582 p += 1;
583 for _ in 0..n {
584 if p + 7 > d.len() {
585 return;
586 }
587 let len = u32::from_le_bytes([d[p + 3], d[p + 4], d[p + 5], d[p + 6]]) as usize;
588 let (a, b, c) = (d[p] != 0, d[p + 1] != 0, d[p + 2] != 0);
589 p += 7;
590 let len = len.min(d.len() - p);
591 self.heredocs.push(Heredoc {
592 word: d[p..p + len].to_vec(),
593 indent_ok: a,
594 interp: b,
595 started: c,
596 });
597 p += len;
598 }
599 }
600}