pagesift_syntax/scanner/
julia.rs1use super::{ScanLexer, Scanner};
5
6const BLOCK_COMMENT_REST: usize = 0;
7const IMMEDIATE_PAREN: usize = 1;
8const IMMEDIATE_BRACKET: usize = 2;
9const IMMEDIATE_BRACE: usize = 3;
10const IMMEDIATE_STRING_START: usize = 4;
11const IMMEDIATE_COMMAND_START: usize = 5;
12const CONTENT_CMD_1: usize = 6;
13const CONTENT_CMD_1_RAW: usize = 7;
14const CONTENT_CMD_3: usize = 8;
15const CONTENT_CMD_3_RAW: usize = 9;
16const CONTENT_STR_1: usize = 10;
17const CONTENT_STR_1_RAW: usize = 11;
18const CONTENT_STR_3: usize = 12;
19const CONTENT_STR_3_RAW: usize = 13;
20const END_CMD: usize = 14;
21const END_STR: usize = 15;
22
23#[derive(Default)]
24pub struct Julia;
25
26fn scan_content(
27 lx: &mut ScanLexer,
28 content: usize,
29 end_char: char,
30 n_delim: u32,
31 interp: bool,
32) -> bool {
33 let end_symbol = if end_char == '"' { END_STR } else { END_CMD };
34 let mut has_content = false;
35 while lx.lookahead() != '\0' {
36 let next = lx.lookahead();
37 lx.mark_end();
38 if interp && (next == '$' || next == '\\') {
39 lx.set_result(content as u16);
40 return has_content;
41 } else if next == '\\' {
42 lx.next();
44 let c = lx.lookahead();
45 if c == end_char || c == '\\' {
46 lx.set_result(content as u16);
47 return has_content;
48 }
49 } else {
50 let mut is_end = true;
51 for _ in 0..n_delim {
52 if lx.lookahead() == end_char {
53 lx.next();
54 } else {
55 is_end = false;
56 break;
57 }
58 }
59 if is_end {
60 if has_content {
61 lx.set_result(content as u16);
62 } else {
63 lx.mark_end();
64 lx.set_result(end_symbol as u16);
65 }
66 return true;
67 }
68 }
69 lx.next();
70 has_content = true;
71 }
72 false
73}
74
75fn scan_block_comment(lx: &mut ScanLexer) -> bool {
76 let mut after_eq = false;
77 let mut depth = 1u32;
78 loop {
79 match lx.lookahead() {
80 '=' => {
81 lx.next();
82 after_eq = true;
83 }
84 '#' => {
85 lx.next();
86 if after_eq {
87 after_eq = false;
88 depth -= 1;
89 if depth == 0 {
90 lx.set_result(BLOCK_COMMENT_REST as u16);
91 return true;
92 }
93 } else {
94 after_eq = false;
95 if lx.lookahead() == '=' {
96 lx.next();
97 depth += 1;
98 }
99 }
100 }
101 '\0' => return false,
102 _ => {
103 lx.next();
104 after_eq = false;
105 }
106 }
107 }
108}
109
110impl Scanner for Julia {
111 fn scan(&mut self, lx: &mut ScanLexer, v: &[bool]) -> bool {
112 let c = lx.lookahead();
113 for (sym, ch) in [
114 (IMMEDIATE_PAREN, '('),
115 (IMMEDIATE_BRACKET, '['),
116 (IMMEDIATE_BRACE, '{'),
117 (IMMEDIATE_STRING_START, '"'),
118 (IMMEDIATE_COMMAND_START, '`'),
119 ] {
120 if v[sym] && c == ch {
121 lx.set_result(sym as u16);
122 return true;
123 }
124 }
125 if v[BLOCK_COMMENT_REST] && scan_block_comment(lx) {
126 return true;
127 }
128 for (sym, ch, n, interp) in [
129 (CONTENT_STR_1, '"', 1, true),
130 (CONTENT_STR_3, '"', 3, true),
131 (CONTENT_CMD_1, '`', 1, true),
132 (CONTENT_CMD_3, '`', 3, true),
133 (CONTENT_STR_1_RAW, '"', 1, false),
134 (CONTENT_STR_3_RAW, '"', 3, false),
135 (CONTENT_CMD_1_RAW, '`', 1, false),
136 (CONTENT_CMD_3_RAW, '`', 3, false),
137 ] {
138 if v[sym] && scan_content(lx, sym, ch, n, interp) {
139 return true;
140 }
141 }
142 false
143 }
144
145 fn serialize(&self, _out: &mut Vec<u8>) {}
146
147 fn deserialize(&mut self, _data: &[u8]) {}
148}