pagesift_syntax/scanner/
cpp.rs1use super::{ScanLexer, Scanner, is_space};
4
5const RAW_STRING_DELIMITER: usize = 0;
6const RAW_STRING_CONTENT: usize = 1;
7const MAX_DELIMITER_LENGTH: usize = 16;
8
9#[derive(Default)]
10pub struct Cpp {
11 delimiter: Vec<u32>,
12}
13
14impl Cpp {
15 fn scan_delimiter(&mut self, lx: &mut ScanLexer) -> bool {
16 if !self.delimiter.is_empty() {
17 for i in 0..self.delimiter.len() {
19 if lx.lookahead() as u32 != self.delimiter[i] {
20 return false;
21 }
22 lx.next();
23 }
24 self.delimiter.clear();
25 return true;
26 }
27 loop {
28 if self.delimiter.len() >= MAX_DELIMITER_LENGTH
29 || lx.eof()
30 || lx.lookahead() == '\\'
31 || is_space(lx.lookahead())
32 {
33 return false;
34 }
35 if lx.lookahead() == '(' {
36 return !self.delimiter.is_empty();
37 }
38 self.delimiter.push(lx.lookahead() as u32);
39 lx.next();
40 }
41 }
42
43 fn scan_content(&self, lx: &mut ScanLexer) -> bool {
44 let mut idx: i32 = -1;
45 loop {
46 if lx.eof() {
47 lx.mark_end();
48 return true;
49 }
50 if idx >= 0 {
51 if idx as usize == self.delimiter.len() {
52 if lx.lookahead() == '"' {
53 return true;
54 }
55 idx = -1;
56 } else if lx.lookahead() as u32 == self.delimiter[idx as usize] {
57 idx += 1;
58 } else {
59 idx = -1;
60 }
61 }
62 if idx == -1 && lx.lookahead() == ')' {
63 lx.mark_end();
64 idx = 0;
65 }
66 lx.next();
67 }
68 }
69}
70
71impl Scanner for Cpp {
72 fn serialize(&self, out: &mut Vec<u8>) {
73 for c in &self.delimiter {
75 out.extend_from_slice(&c.to_le_bytes());
76 }
77 }
78 fn deserialize(&mut self, d: &[u8]) {
79 self.delimiter.clear();
80 if d.len().is_multiple_of(4) {
81 self.delimiter.extend(
82 d.as_chunks::<4>()
83 .0
84 .iter()
85 .take(MAX_DELIMITER_LENGTH)
86 .map(|c| u32::from_le_bytes([c[0], c[1], c[2], c[3]])),
87 );
88 }
89 }
90
91 fn scan(&mut self, lx: &mut ScanLexer, valid: &[bool]) -> bool {
92 if valid[RAW_STRING_DELIMITER] && valid[RAW_STRING_CONTENT] {
93 return false;
94 }
95 if valid[RAW_STRING_DELIMITER] {
96 lx.set_result(RAW_STRING_DELIMITER as u16);
97 return self.scan_delimiter(lx);
98 }
99 if valid[RAW_STRING_CONTENT] {
100 lx.set_result(RAW_STRING_CONTENT as u16);
101 return self.scan_content(lx);
102 }
103 false
104 }
105}